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