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