CONOPT
Loading...
Searching...
No Matches
square3.f90
Go to the documentation of this file.
1!> @file square3.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! A square model where we pretend that the second and third constraints are completely
6!! nonlinear.
7!!
8!! \f{eqnarray*}{
9!! &x1 + x2 = 10 \\
10!! &x1 - x2 = 0\\
11!! &x1 + x2 \leq 9
12!! \f}
13!!
14!! The model is almost like square2 but the initial values are the
15!! values that satisfies the first two constriants but violates the
16!! last.
17!!
18!!
19!! For more information about the individual callbacks, please have a look at the source code.
20
21!> Main program. A simple setup and call of CONOPT
22!!
23Program square
24
25 Use proginfo
26 Use coidef
27 implicit None
28!
29! Declare the user callback routines as Integer, External:
30!
31 Integer, External :: sq_readmatrix ! Mandatory Matrix definition routine defined below
32 Integer, External :: sq_fdeval ! Function and Derivative evaluation routine
33 ! needed a nonlinear model.
34 Integer, External :: std_status ! Standard callback for displaying solution status
35 Integer, External :: std_solution ! Standard callback for displaying solution values
36 Integer, External :: std_message ! Standard callback for managing messages
37 Integer, External :: std_errmsg ! Standard callback for managing error messages
38#if defined(itl)
39!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
40!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
41!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
42!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
43!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
44!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
45#endif
46!
47! Control vector
48!
49 INTEGER :: numcallback
50 INTEGER, Dimension(:), Pointer :: cntvect
51 INTEGER :: coi_error
52
53 call startup
54!
55! Create and initialize a Control Vector
56!
57 numcallback = coidef_size()
58 Allocate( cntvect(numcallback) )
59 coi_error = coidef_inifort( cntvect )
60!
61! Tell CONOPT about the size of the model by populating the Control Vector:
62!
63 coi_error = max( coi_error, coidef_numvar( cntvect, 2 ) ) ! 2 variables
64 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
65 coi_error = max( coi_error, coidef_numnz( cntvect, 6 ) ) ! 6 nonzeros in the Jacobian
66 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) ) ! 4 of which are nonlinear
67 coi_error = max( coi_error, coidef_square( cntvect, 1 ) ) ! 1 means Square system
68 coi_error = max( coi_error, coidef_optfile( cntvect, 'square3.opt' ) )
69!
70! Tell CONOPT about the callback routines:
71!
72 coi_error = max( coi_error, coidef_readmatrix( cntvect, sq_readmatrix ) )
73 coi_error = max( coi_error, coidef_fdeval( cntvect, sq_fdeval ) )
74 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
75 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
76 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
77 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
78
79#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
80 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
81#endif
82
83 If ( coi_error .ne. 0 ) THEN
84 write(*,*)
85 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
86 write(*,*)
87 call flog( "Skipping Solve due to setup errors", 1 )
88 ENDIF
89!
90! Start CONOPT:
91!
92 coi_error = coi_solve( cntvect )
93
94 write(*,*)
95 write(*,*) 'End of Square3 example. Return code=',coi_error
96
97 If ( coi_error /= 0 ) then
98 call flog( "Errors encountered during solution", 1 )
99 elseif ( stacalls == 0 .or. solcalls == 0 ) then
100 call flog( "Status or Solution routine was not called", 1 )
101 elseif ( sstat /= 1 .or. mstat < 4 .or. mstat > 5 ) then
102 call flog( "Solver or Model status not as expected (1,4) or (1,5)", 1 )
103 endif
104
105 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
106
107 call flog( "Successful Solve", 0 )
108
109End Program square
110!
111! ============================================================================
112! Define information about the model:
113!
114
115!> Define information about the model
116!!
117!! @include{doc} readMatrix_params.dox
118Integer Function sq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
119 colsta, rowno, value, nlflag, n, m, nz, &
120 usrmem )
121#if defined(itl)
122!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
123#endif
124 implicit none
125 integer, intent (in) :: n ! number of variables
126 integer, intent (in) :: m ! number of constraints
127 integer, intent (in) :: nz ! number of nonzeros
128 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
129 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
130 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
131 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
132 ! (not defined here)
133 integer, intent (out), dimension(m) :: type ! vector of equation types
134 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
135 ! (not defined here)
136 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
137 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
138 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
139 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
140 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
141 real*8 usrmem(*) ! optional user memory
142!
143! Information about Variables:
144! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
145! Default: the status information in Vsta is not used.
146!
147! Initial x1 = x2 = 5:
148!
149 curr(1) = 5.0d0
150 curr(2) = 5.0d0
151!
152! Information about Constraints:
153! Default: Rhs = 0
154! Default: the status information in Esta and the function
155! value in FV are not used.
156! Default: Type: There is no default.
157! 0 = Equality,
158! 1 = Greater than or equal,
159! 2 = Less than or equal,
160! 3 = Non binding.
161!
162! Constraint 1
163! Rhs = 10 and type Equal
164!
165 rhs(1) = 10.d0
166 type(1) = 0
167!
168! Constraint 2
169! Rhs = 0 and type Equality
170!
171 type(2) = 0
172!
173! Constraint 3
174! Rhs = 9 and type Less than
175!
176 rhs(3) = 9.d0
177 type(3) = 2
178!
179! Information about the Jacobian. We use the standard method with
180! Rowno, Value, Nlflag and Colsta and we do not use Colno.
181!
182! Colsta = Start of column indices (No Defaults):
183! Rowno = Row indices
184! Value = Value of derivative (by default only linear
185! derivatives are used)
186! Nlflag = 0 for linear and 1 for nonlinear derivative
187! (not needed for completely linear models)
188!
189! Indices
190! x(1) x(2)
191! 1: 1 4
192! 2: 2 5
193! 3: 2 6
194!
195 colsta(1) = 1
196 colsta(2) = 4
197 colsta(3) = 7
198 rowno(1) = 1
199 rowno(2) = 2
200 rowno(3) = 3
201 rowno(4) = 1
202 rowno(5) = 2
203 rowno(6) = 3
204!
205! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
206! x(1) x(2)
207! 1: L L
208! 2: NL NL
209! 3: NL NL
210!
211 nlflag(1) = 0
212 nlflag(2) = 1
213 nlflag(3) = 1
214 nlflag(4) = 0
215 nlflag(5) = 1
216 nlflag(6) = 1
217!
218! Value (Linear only)
219! x(1) x(2) x(3) x(4)
220! 1: 1 1
221! 2: NL NL
222! 3: NL NL
223!
224 value(1) = 1.d0
225 value(4) = 1.d0
226
227 sq_readmatrix = 0 ! Return value means OK
228
229end Function sq_readmatrix
230!
231!==========================================================================
232! Compute nonlinear terms and non-constant Jacobian elements
233!
234
235!> Compute nonlinear terms and non-constant Jacobian elements
236!!
237!! @include{doc} fdeval_params.dox
238Integer Function sq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
239 n, nz, thread, usrmem )
240#if defined(itl)
241!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
242#endif
243 implicit none
244 integer, intent (in) :: n ! number of variables
245 integer, intent (in) :: rowno ! number of the row to be evaluated
246 integer, intent (in) :: nz ! number of nonzeros in this row
247 real*8, intent (in), dimension(n) :: x ! vector of current solution values
248 real*8, intent (in out) :: g ! constraint value
249 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
250 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
251 ! in this row. Ffor information only.
252 integer, intent (in) :: mode ! evaluation mode: 1 = function value
253 ! 2 = derivatives, 3 = both
254 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
255 ! as errcnt is incremented
256 integer, intent (in out) :: errcnt ! error counter to be incremented in case
257 ! of function evaluation errors.
258 integer, intent (in) :: thread
259 real*8 usrmem(*) ! optional user memory
260!
261! Row 1: Is declared as linear and should not be called.
262!
263 if ( rowno .eq. 1 ) then
264 sq_fdeval = 1
265 return
266!
267! Row 2: x1 + x2 assumed to be nonlinear
268!
269 elseif ( rowno .eq. 2 ) then
270!
271! Mode = 1 or 3: Function value
272!
273 if ( mode .eq. 1 .or. mode .eq. 3 ) then
274 g = x(1) - x(2)
275 endif
276!
277! Mode = 2 or 3: Derivatives
278!
279 if ( mode .eq. 2 .or. mode .eq. 3 ) then
280 jac(1) = 1.d0
281 jac(2) = -1.d0
282 endif
283 elseif ( rowno .eq. 3 ) then
284!
285! Mode = 1 or 3: Function value
286!
287 if ( mode .eq. 1 .or. mode .eq. 3 ) then
288 g = x(1) + x(2)
289 endif
290!
291! Mode = 2 or 3: Derivatives
292!
293 if ( mode .eq. 2 .or. mode .eq. 3 ) then
294 jac(1) = 1.d0
295 jac(2) = 1.d0
296 endif
297 endif
298 sq_fdeval = 0
299
300end Function sq_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_square(cntvect, square)
square models.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35
integer function sq_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition square.f90:248
integer function sq_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition square.f90:147
program square
Main program. A simple setup and call of CONOPT.
Definition square.f90:17