CONOPT
Loading...
Searching...
No Matches
largeres2.f90
Go to the documentation of this file.
1!> @file largeres2.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Example with a Large Residual in the initial point.
6!!
7!!
8!! For more information about the individual callbacks, please have a look at the source code.
9
10#if defined(_WIN32) && !defined(_WIN64)
11#define dec_directives_win32
12#endif
13
14!> Main program. A simple setup and call of CONOPT
15!!
16Program largeres2
17
19 Use conopt
20 implicit None
21!
22! Declare the user callback routines as Integer, External:
23!
24 Integer, External :: lr_readmatrix ! Mandatory Matrix definition routine defined below
25 Integer, External :: lr_fdeval ! Function and Derivative evaluation routine
26 ! needed a nonlinear model.
27 Integer, External :: std_status ! Standard callback for displaying solution status
28 Integer, External :: std_solution ! Standard callback for displaying solution values
29 Integer, External :: std_message ! Standard callback for managing messages
30 Integer, External :: std_errmsg ! Standard callback for managing error messages
31#ifdef dec_directives_win32
32!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lr_ReadMatrix
33!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lr_FDEval
34!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
35!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
36!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
37!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
38#endif
39!
40! Control vector
41!
42 INTEGER, Dimension(:), Pointer :: cntvect
43 INTEGER :: coi_error
44!
45! The model is
46!
47! min x
48! exp(x) = 10
49! x.l = 100
50!
51! with x starting at a value where the function overflows.
52! The difference between LargeRes1 and LargeRes2 is the initial point.
53! In LargeRes1 the function value is above Rtmaxv but not really large.
54! In LargeRes2 the function value is really large, above 1.e30.
55! When an error happens in the initial point CONOPT will just
56! return with COI_Error = 400
57!
58! Create and initialize a Control Vector
59!
60 call startup
61 coi_error = coi_create( cntvect )
62!
63! Tell CONOPT about the size of the model by populating the Control Vector:
64!
65 coi_error = max( coi_error, coidef_numvar( cntvect, 1 ) ) ! # variables
66 coi_error = max( coi_error, coidef_numcon( cntvect, 1 ) ) ! # constraints
67 coi_error = max( coi_error, coidef_numnz( cntvect, 1 ) ) ! # nonzeros in the Jacobian
68 coi_error = max( coi_error, coidef_numnlnz( cntvect, 1 ) ) ! # of which are nonlinear
69 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
70 coi_error = max( coi_error, coidef_objvar( cntvect, 1 ) ) ! Objective is variable 1
71 coi_error = max( coi_error, coidef_optfile( cntvect, 'largeres.opt' ) )
72!
73! Tell CONOPT about the callback routines:
74!
75 coi_error = max( coi_error, coidef_readmatrix( cntvect, lr_readmatrix ) )
76 coi_error = max( coi_error, coidef_fdeval( cntvect, lr_fdeval ) )
77 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
78 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
79 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
80 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
81
82#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
83 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
84#endif
85
86 If ( coi_error .ne. 0 ) THEN
87 write(*,*)
88 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
89 write(*,*)
90 call flog( "Skipping Solve due to setup errors", 1 )
91 ENDIF
92!
93 coi_error = coi_solve( cntvect )
94
95 write(*,*)
96 write(*,*) 'End of Large Residual example. Return code=',coi_error
97
98 If ( coi_error /= 400 ) then
99 call flog( "COI_Solve was expected to return 400.", 1 )
100 endif
101
102 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
103
104 call flog( "Successful Solve", 0 )
105
106End Program largeres2
107!
108! ============================================================================
109! Define information about the model:
110!
111
112!> Define information about the model
113!!
114!! @include{doc} readMatrix_params.dox
115Integer Function lr_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
116 colsta, rowno, value, nlflag, n, m, nz, &
117 usrmem )
118#ifdef dec_directives_win32
119!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lr_ReadMatrix
120#endif
121 implicit none
122 integer, intent (in) :: n ! number of variables
123 integer, intent (in) :: m ! number of constraints
124 integer, intent (in) :: nz ! number of nonzeros
125 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
126 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
127 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
128 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
129 ! (not defined here)
130 integer, intent (out), dimension(m) :: type ! vector of equation types
131 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
132 ! (not defined here)
133 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
134 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
135 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
136 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
137 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
138 real*8 usrmem(*) ! optional user memory
139
140!
141! Information about Variables:
142! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
143! Default: the status information in Vsta is not used.
144!
145 curr(1) = 100.d0
146!
147! Information about Constraints:
148! Default: Rhs = 0
149! Default: the status information in Esta and the function
150! value in FV are not used.
151! Default: Type: There is no default.
152! 0 = Equality,
153! 1 = Greater than or equal,
154! 2 = Less than or equal,
155! 3 = Non binding.
156!
157! Constraint 1
158! Rhs = 10.0 and type Equality
159!
160 type(1) = 0
161 rhs(1) = 10.0d0
162!
163! Information about the Jacobian. We have to define Rowno, Value,
164! Nlflag and Colsta.
165!
166! Colsta = Start of column indices (No Defaults):
167! Rowno = Row indices
168! Value = Value of derivative (by default only linear
169! derivatives are used)
170! Nlflag = 0 for linear and 1 for nonlinear derivative
171! (not needed for completely linear models)
172!
173! Indices
174! x(1)
175! 1: 1
176!
177! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
178! x(1)
179! 1: NL
180!
181! Value (Linear only)
182!
183 colsta(1) = 1
184 colsta(2) = 2
185 rowno(1) = 1
186 nlflag(1) = 1
188 lr_readmatrix = 0 ! Return value means OK
189
190end Function lr_readmatrix
191!
192!==========================================================================
193! Compute nonlinear terms and non-constant Jacobian elements
194!
195
196!> Compute nonlinear terms and non-constant Jacobian elements
197!!
198!! @include{doc} fdeval_params.dox
199Integer Function lr_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
200 n, nz, thread, usrmem )
201#ifdef dec_directives_win32
202!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lr_FDEval
203#endif
204 implicit none
205 integer, intent (in) :: n ! number of variables
206 integer, intent (in) :: rowno ! number of the row to be evaluated
207 integer, intent (in) :: nz ! number of nonzeros in this row
208 real*8, intent (in), dimension(n) :: x ! vector of current solution values
209 real*8, intent (in out) :: g ! constraint value
210 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
211 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
212 ! in this row. Ffor information only.
213 integer, intent (in) :: mode ! evaluation mode: 1 = function value
214 ! 2 = derivatives, 3 = both
215 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
216 ! as errcnt is incremented
217 integer, intent (in out) :: errcnt ! error counter to be incremented in case
218 ! of function evaluation errors.
219 integer, intent (in) :: thread
220 real*8 usrmem(*) ! optional user memory
221
222!
223! Row 1: exp(x)
224!
225 if ( rowno .eq. 1 ) then
226!
227! Mode = 1 or 3. Function value:
228!
229 if ( mode .eq. 1 .or. mode .eq. 3 ) then
230 g = exp(x(1))
231 endif
232!
233! Mode = 2 or 3: Derivative values:
234!
235 if ( mode .eq. 2 .or. mode .eq. 3 ) then
236 jac(1) = exp(x(1))
237 endif
238 lr_fdeval = 0
239 else
240 lr_fdeval = 1
241 endif
242
243end Function lr_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_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_objvar(cntvect, objvar)
defines the Objective Variable.
Definition conopt.f90:257
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 function lr_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
integer function lr_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
program largeres2
Main program. A simple setup and call of CONOPT.
Definition largeres2.f90:18
subroutine flog(msg, code)
Definition comdecl.f90:62
subroutine startup
Definition comdecl.f90:41