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