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