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[
8!! x1 + x2 = 10
9!! \f]
10!! \f[
11!! x1 - x2 = 0
12!! \f]
13!!
14!!
15!! For more information about the individual callbacks, please have a look at the source code.
16
17#if defined(_WIN32) && !defined(_WIN64)
18#define dec_directives_win32
19#endif
20
21!> Main program. A simple setup and call of CONOPT
22!!
23Program square
24
26 Use conopt
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#ifdef dec_directives_win32
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, Dimension(:), Pointer :: cntvect
50 INTEGER :: coi_error
51
52 Integer :: i
53 Logical :: error
54
55 call startup
56!
57! Create and initialize a Control Vector
58!
59 coi_error = coi_create( 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, 2 ) ) ! 2 constraints
65 coi_error = max( coi_error, coidef_numnz( cntvect, 4 ) ) ! 4 nonzeros in the Jacobian
66 coi_error = max( coi_error, coidef_numnlnz( cntvect, 2 ) ) ! 2 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, 'square.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(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
80 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_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! Allocate space for the solution and status vectors.
91!
92 do_allocate = .true.
93!
94! Start CONOPT:
95!
96 coi_error = coi_solve( cntvect )
97
98 write(*,*)
99 write(*,*) 'End of Square example. Return code=',coi_error
100
101 If ( coi_error /= 0 ) then
102 call flog( "Errors encountered during solution", 1 )
103 elseif ( stacalls == 0 .or. solcalls == 0 ) then
104 call flog( "Status or Solution routine was not called", 1 )
105 elseif ( sstat /= 1 .or. mstat /= 16 ) then
106 call flog( "Solver or Model status not as expected (1,16)", 1 )
107 elseif ( obj /= 0.d0 ) Then
108 call flog( "Objective for square model was not as expected 0.0", 1 )
109 else
110!
111! Check the primal and dual solution itself
112!
113 error = .false.
114 do i = 1, 2
115 if ( abs(xprim(i)-5.0d0) > 1.d-7 ) error = .true.
116 if ( abs(xdual(i)) > 1.d-7 ) error = .true.
117 if ( abs(udual(i)) > 1.d-7 ) error = .true.
118 enddo
119 if ( abs(uprim(1)-10.d0) > 1.e-7 ) error = .true.
120 if ( abs(uprim(2)- 0.d0) > 1.e-7 ) error = .true.
121 if ( error ) call flog( "Numerical solution was not as expected.", 1 )
122!!
123!! Check the status information
124!!
125! do i = 1, 2
126! if ( Xbasc(i) /= 2 ) error = .true. ! Basic
127! if ( XStat(i) /= 0 ) error = .true. ! Normal
128! if ( Ubasc(i) /= 0 ) error = .true. ! Lower
129! if ( UStat(i) /= 0 ) error = .true. ! Normal
130! enddo
131! if ( Error ) call flog( "Status information was not as expected.", 1 )
132 endif
133
134 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
135
136 call flog( "Successful Solve", 0 )
137!
138! Free solution memory
139!
140 call finalize
141
142End Program square
143!
144! ============================================================================
145! Define information about the model:
146!
147
148!> Define information about the model
149!!
150!! @include{doc} readMatrix_params.dox
151Integer Function sq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
152 colsta, rowno, value, nlflag, n, m, nz, &
153 usrmem )
154#ifdef dec_directives_win32
155!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_ReadMatrix
156#endif
157 implicit none
158 integer, intent (in) :: n ! number of variables
159 integer, intent (in) :: m ! number of constraints
160 integer, intent (in) :: nz ! number of nonzeros
161 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
162 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
163 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
164 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
165 ! (not defined here)
166 integer, intent (out), dimension(m) :: type ! vector of equation types
167 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
168 ! (not defined here)
169 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
170 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
171 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
172 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
173 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
174 real*8 usrmem(*) ! optional user memory
175!
176! Information about Variables:
177! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
178! Default: the status information in Vsta is not used.
179!
180! Information about Constraints:
181! Default: Rhs = 0
182! Default: the status information in Esta and the function
183! value in FV are not used.
184! Default: Type: There is no default.
185! 0 = Equality,
186! 1 = Greater than or equal,
187! 2 = Less than or equal,
188! 3 = Non binding.
189!
190! Constraint 1
191! Rhs = 10 and type Equal
192!
193 rhs(1) = 10.d0
194 type(1) = 0
195!
196! Constraint 2
197! Rhs = 0 and type Equality
198!
199 type(2) = 0
200!
201! Information about the Jacobian. CONOPT expects a columnwise
202! representation in Rowno, Value, Nlflag and Colsta.
203!
204! Colsta = Start of column indices (No Defaults):
205! Rowno = Row indices
206! Value = Value of derivative (by default only linear
207! derivatives are used)
208! Nlflag = 0 for linear and 1 for nonlinear derivative
209! (not needed for completely linear models)
210!
211! Indices
212! x(1) x(2)
213! 1: 1 3
214! 2: 2 4
215!
216 colsta(1) = 1
217 colsta(2) = 3
218 colsta(3) = 5
219 rowno(1) = 1
220 rowno(2) = 2
221 rowno(3) = 1
222 rowno(4) = 2
223!
224! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
225! x(1) x(2)
226! 1: L L
227! 2: NL NL
228!
229 nlflag(1) = 0
230 nlflag(2) = 1
231 nlflag(3) = 0
232 nlflag(4) = 1
233!
234! Value (Linear only)
235! x(1) x(2) x(3) x(4)
236! 1: 1 1
237! 2: NL NL
238!
239 value(1) = 1.d0
240 value(3) = 1.d0
242 sq_readmatrix = 0 ! Return value means OK
243
244end Function sq_readmatrix
245!
246!==========================================================================
247! Compute nonlinear terms and non-constant Jacobian elements
248!
249
250!> Compute nonlinear terms and non-constant Jacobian elements
251!!
252!! @include{doc} fdeval_params.dox
253Integer Function sq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
254 n, nz, thread, usrmem )
255#ifdef dec_directives_win32
256!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Sq_FDEval
257#endif
258 implicit none
259 integer, intent (in) :: n ! number of variables
260 integer, intent (in) :: rowno ! number of the row to be evaluated
261 integer, intent (in) :: nz ! number of nonzeros in this row
262 real*8, intent (in), dimension(n) :: x ! vector of current solution values
263 real*8, intent (in out) :: g ! constraint value
264 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
265 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
266 ! in this row. Ffor information only.
267 integer, intent (in) :: mode ! evaluation mode: 1 = function value
268 ! 2 = derivatives, 3 = both
269 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
270 ! as errcnt is incremented
271 integer, intent (in out) :: errcnt ! error counter to be incremented in case
272 ! of function evaluation errors.
273 integer, intent (in) :: thread
274 real*8 usrmem(*) ! optional user memory
275!
276! Row 1: Is declared as linear and should not be called.
277!
278 if ( rowno .eq. 1 ) then
279 sq_fdeval = 1
280 return
281!
282! Row 2: x1 + x2 assumed to be nonlinear
283!
284 elseif ( rowno .eq. 2 ) then
285!
286! Mode = 1 or 3: Function value
287!
288 if ( mode .eq. 1 .or. mode .eq. 3 ) then
289 g = x(1) - x(2)
290 endif
291!
292! Mode = 2 or 3: Derivatives
293!
294 if ( mode .eq. 2 .or. mode .eq. 3 ) then
295 jac(1) = 1.d0
296 jac(2) = -1.d0
297 endif
298 endif
299 sq_fdeval = 0
300
301end Function sq_fdeval
Main program. A simple setup and call of CONOPT.
Definition square.java:14
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
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
subroutine finalize
Definition comdecl.f90:79
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:243
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:145