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