CONOPT
Loading...
Searching...
No Matches
stress1.f90
Go to the documentation of this file.
1!> @file stress1.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is a CONOPT implementation of the gams model
6!!
7!! @verbatim
8!! set i / i1*i10000/
9!! variable x(i), obj;
10!! equation objdef; objdef .. obj =E= prod(i, x(i)*x(i) );
11!! x.lo(i) = 1;
12!! model m / all /;
13!! solve m using nlp minimizing obj;
14!! @endverbatim
15!!
16!!
17!! For more information about the individual callbacks, please have a look at the source code.
18
19#if defined(_WIN32) && !defined(_WIN64)
20#define dec_directives_win32
21#endif
22
23Module stress1data
24 integer, Parameter :: N = 15000
25End Module stress1data
27!> Main program. A simple setup and call of CONOPT
28!!
29Program stress1
30 Use proginfo
32 Use stress1data
33 Implicit none
34!
35! Declare the user callback routines as Integer, External:
36!
37 Integer, External :: stress_readmatrix ! Mandatory Matrix definition routine defined below
38 Integer, External :: stress_fdeval ! Function and Derivative evaluation routine
39 ! needed a nonlinear model.
40 Integer, External :: std_status ! Standard callback for displaying solution status
41 Integer, External :: std_solution ! standard callback for displaying solution values
42 Integer, External :: std_message ! Standard callback for managing messages
43 Integer, External :: std_errmsg ! Standard callback for managing error messages
44 Integer, External :: stress_2dlagrstr ! Hessian structure evaluation routine
45 Integer, External :: stress_2dlagrval ! Hessian value evaluation routine
46#ifdef dec_directives_win32
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Stress_ReadMatrix
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Stress_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!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Stress_2DLagrStr
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Stress_2DLagrVal
55#endif
56!
57! Control vector
58!
59 INTEGER, Dimension(:), Pointer :: cntvect
60 INTEGER :: coi_error
61
62 call startup
63!
64! Create and initialize a Control Vector
65!
66 coi_error = coi_create( cntvect )
67!
68! Number of variables (excl. slacks): N
69!
70 coi_error = max( coi_error, coidef_numvar( cntvect, n ) )
71!
72! Number of equations: 1 objective
73!
74 coi_error = max( coi_error, coidef_numcon( cntvect, 1 ) )
75!
76! Number of nonzeros in the Jacobian: N
77!
78 coi_error = max( coi_error, coidef_numnz( cntvect, n ) )
79!
80! Number of nonlinear nonzeros. N
81!
82 coi_error = max( coi_error, coidef_numnlnz( cntvect, n ) )
83!
84! Number of nonzeros in the Hessian. N*(N+1)/2
85!
86 coi_error = max( coi_error, coidef_numhess( cntvect, n*(n+1)/2 ) )
87!
88! Direction: -1 = minimization.
89!
90 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) )
91!
92! Objective: Constraint no 1
93!
94 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) )
95!
96! Turn Hessian debugging off
97!
98 coi_error = max( coi_error, coidef_debug2d( cntvect, 0 ) )
99!
100! Options file = stress1.opt
101!
102 coi_error = max( coi_error, coidef_optfile( cntvect, 'stress1.opt' ) )
103!
104! Tell CONOPT about the callback routines:
105!
106 coi_error = max( coi_error, coidef_readmatrix( cntvect, stress_readmatrix ) )
107 coi_error = max( coi_error, coidef_fdeval( cntvect, stress_fdeval ) )
108 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
109 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
110 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
111 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
112 coi_error = max( coi_error, coidef_2dlagrstr( cntvect, stress_2dlagrstr ) )
113 coi_error = max( coi_error, coidef_2dlagrval( cntvect, stress_2dlagrval ) )
114
115#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
116 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
117#endif
118
119 If ( coi_error .ne. 0 ) THEN
120 write(*,*)
121 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
122 write(*,*)
123 call flog( "Skipping First Solve due to setup errors", 1 )
124 ENDIF
125!
126! Start CONOPT:
127!
128 coi_error = coi_solve( cntvect )
129 If ( coi_error .ne. 0 ) THEN
130 write(*,*)
131 write(*,*) '**** Fatal Error while Solving model. Return Code=',coi_error
132 write(*,*)
133 call flog( "Errors encountered during Solve.", 1 )
134 elseif ( stacalls == 0 .or. solcalls == 0 ) then
135 call flog( "Status or Solution routine was not called during Solve", 1 )
136 else if ( mstat /= 2 .or. sstat /= 1 ) then
137 call flog( "Incorrect Model or Solver Status during solve", 1 )
138 ENDIF
139
140 write(*,*)
141 write(*,*) 'End of Stress1 Model. Return code=',coi_error
142
143 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
144
145 call flog( "Successful Solve", 0 )
146
147end Program stress1
148!
149! =====================================================================
150! Define information about the model structure
151!
152
153!> Define information about the model
154!!
155!! @include{doc} readMatrix_params.dox
156Integer Function stress_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
157 colsta, rowno, value, nlflag, nc, mc, nz, usrmem )
158#ifdef dec_directives_win32
159!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Stress_ReadMatrix
160#endif
161 Use stress1data
162 implicit none
163 integer, intent (in) :: nc ! number of variables
164 integer, intent (in) :: mc ! number of constraints
165 integer, intent (in) :: nz ! number of nonzeros
166 real*8, intent (in out), dimension(nc) :: lower ! vector of lower bounds
167 real*8, intent (in out), dimension(nc) :: curr ! vector of initial values
168 real*8, intent (in out), dimension(nc) :: upper ! vector of upper bounds
169 integer, intent (in out), dimension(nc) :: vsta ! vector of initial variable status
170 ! Defined during restarts
171 integer, intent (out), dimension(mc) :: type ! vector of equation types
172 integer, intent (in out), dimension(mc) :: esta ! vector of initial equation status
173 ! Defined during restarts
174 real*8, intent (in out), dimension(mc) :: rhs ! vector of right hand sides
175 integer, intent (in out), dimension(nc+1) :: colsta ! vector with start of column indices
176 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
177 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
178 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
179 real*8 usrmem(*) ! optional user memory
180
181 Integer :: i
182!
183! Define the information for the columns.
184!
185! We should not supply status information, vsta.
186!
187 do i = 1, n
188 lower(i) = 1.d0
189 curr(i) = 1.d0
190 enddo
191!
192! Define the information for the rows
193!
194! We order the constraints as follows: The objective is first,
195! followed by tddef, sdef, csdef, ddef, rdef, and revdef for
196! the first period, the same for the second period, etc.
197!
198! The objective is a nonbinding constraint:
199!
200 type(1) = 3
201!
202! Define the structure and content of the Jacobian:
203!
204 do i = 1, n
205 colsta(i) = i
206 rowno(i) = 1
207 nlflag(i) = 1
208 enddo
209 colsta(n+1) = n+1
210
212
213end Function stress_readmatrix
214!
215! =====================================================================
216! Compute nonlinear terms and non-constant Jacobian elements
217!
218
219!> Compute nonlinear terms and non-constant Jacobian elements
220!!
221!! @include{doc} fdeval_params.dox
222Integer Function stress_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
223 nc, nz, thread, usrmem )
224#ifdef dec_directives_win32
225!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Stress_FDEval
226#endif
227 Use stress1data
228 implicit none
229 integer, intent (in) :: nc ! number of variables
230 integer, intent (in) :: rowno ! number of the row to be evaluated
231 integer, intent (in) :: nz ! number of nonzeros in this row
232 real*8, intent (in), dimension(nc) :: x ! vector of current solution values
233 real*8, intent (in out) :: g ! constraint value
234 real*8, intent (in out), dimension(nc) :: jac ! vector of derivatives for current constraint
235 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
236 ! in this row. Ffor information only.
237 integer, intent (in) :: mode ! evaluation mode: 1 = function value
238 ! 2 = derivatives, 3 = both
239 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
240 ! as errcnt is incremented
241 integer, intent (in out) :: errcnt ! error counter to be incremented in case
242 ! of function evaluation errors.
243 integer, intent (in) :: thread
244 real*8 usrmem(*) ! optional user memory
245
246 integer :: i
247 real*8 :: obj
248!
249 if ( rowno == 1 ) then
250 obj = 1.0d0
251 do i = 1, n
252 obj = obj * x(i)*x(i)
253 enddo
254 if ( mode == 1 .or. mode == 3 ) then
255 g = obj
256 endif
257 if ( mode == 2 .or. mode == 3 ) then
258 do i = 1, n
259 jac(i) = 2.d0*obj/x(i)
260 enddo
261 endif
262 else
264! Error - this equation is not nonlinear
265!
266 write(*,*)
267 write(*,*) 'Error. FDEval called with rowno=',rowno
268 write(*,*)
269 stress_fdeval = 1
270 Return
271 endif
272 stress_fdeval = 0
273
274end Function stress_fdeval
275
276
277!> Specify the structure of the Lagrangian of the Hessian
278!!
279!! @include{doc} 2DLagrStr_params.dox
280Integer Function stress_2dlagrstr( HSRW, HSCL, NODRV, Nc, Mc, NHESS, UsrMem )
281#ifdef dec_directives_win32
282!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Stress_2DLagrStr
283#endif
284 USE stress1data
285 Implicit None
286
287 Integer, Intent (IN) :: nc, mc, nhess
288 Integer, Intent (IN OUT) :: nodrv
289 Integer, Dimension(NHess), Intent (Out) :: hsrw, hscl
290 real*8, Intent(IN OUT) :: usrmem(*)
291
292 Integer :: i, j, iz
293!
294! Define structure of Hessian
295!
296 if ( n /= nc ) then
297 write(*,*) 'Struct: Number of variables should be=',n,' but CONOPT uses',nc
298 stress_2dlagrstr = 1; Return
299 stop
300 endif
301 if ( mc /= 1 ) then
302 write(*,*) 'Struct: Number of equations should be=',+1,' but CONOPT uses',mc
303 stress_2dlagrstr = 1; Return
304 endif
305 if ( nhess /= n*(n+1)/2 ) then
306 write(*,*) 'Struct: Number of Hessian elements should be=',n*(n+1)/2,' but CONOPT uses',nhess
307 stress_2dlagrstr = 1; Return
308 endif
309 iz = 0
310 Do i = 1, n
311 Do j = i, n
312 iz = iz + 1
313 hscl(iz) = i
314 hsrw(iz) = j
315 Enddo
316 Enddo
317
319
320End Function stress_2dlagrstr
321
322
323!> Compute the Lagrangian of the Hessian
324!!
325!! @include{doc} 2DLagrVal_params.dox
326Integer Function stress_2dlagrval( X, U, HSRW, HSCL, HSVL, NODRV, Nc, Mc, NHESS, UsrMem )
327#ifdef dec_directives_win32
328!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Stress_2DLagrVal
329#endif
330 USE stress1data
331 Implicit None
332
333 Integer, Intent (IN) :: nc, mc, nhess
334 Integer, Intent (IN OUT) :: nodrv
335 real*8, Dimension(Nc), Intent (IN) :: x
336 real*8, Dimension(Mc), Intent (IN) :: u
337 Integer, Dimension(Nhess), Intent (IN) :: hsrw, hscl
338 real*8, Dimension(NHess), Intent (Out) :: hsvl
339 real*8, Intent(IN OUT) :: usrmem(*)
340
341 Integer :: i, j, iz
342!
343! Normal Evaluation mode -- we are testing memory only so we return a unit matrix.
344!
345 iz = 0
346 Do i = 1, n
347 iz = iz + 1
348 hsvl(iz) = 1.0d0
349 Do j = i+1, n
350 iz = iz + 1
351 hsvl(iz) = 0.0d0
352 Enddo
353 Enddo
354
356
357End Function stress_2dlagrval
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_2dlagrstr(cntvect, coi_2dlagrstr)
define callback routine for providing the structure of the second derivatives of the Lagrangian.
Definition conopt.f90:1547
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_2dlagrval(cntvect, coi_2dlagrval)
define callback routine for computing the values of the second derivatives of the Lagrangian.
Definition conopt.f90:1573
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_debug2d(cntvect, debug2d)
turn debugging of 2nd derivatives on and off.
Definition conopt.f90:534
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_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition conopt.f90:213
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 coidef_numhess(cntvect, numhess)
defines the Number of Hessian Nonzeros.
Definition conopt.f90:188
integer(c_int) function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition conopt.f90:239
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
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
integer, parameter n
Definition stress1.f90:26
integer function stress_2dlagrstr(hsrw, hscl, nodrv, nc, mc, nhess, usrmem)
Specify the structure of the Lagrangian of the Hessian.
Definition stress1.f90:264
integer function stress_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, nc, mc, nz, usrmem)
Define information about the model.
Definition stress1.f90:147
integer function stress_2dlagrval(x, u, hsrw, hscl, hsvl, nodrv, nc, mc, nhess, usrmem)
Compute the Lagrangian of the Hessian.
Definition stress1.f90:307
program stress1
Main program. A simple setup and call of CONOPT.
Definition stress1.f90:31
integer function stress_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, nc, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition stress1.f90:210