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!
106! Free solution memory
107!
108 call finalize
109
110End Program largeres2
111!
112! ============================================================================
113! Define information about the model:
114!
115
116!> Define information about the model
117!!
118!! @include{doc} readMatrix_params.dox
119Integer Function lr_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
120 colsta, rowno, value, nlflag, n, m, nz, &
121 usrmem )
122#ifdef dec_directives_win32
123!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lr_ReadMatrix
124#endif
125 implicit none
126 integer, intent (in) :: n ! number of variables
127 integer, intent (in) :: m ! number of constraints
128 integer, intent (in) :: nz ! number of nonzeros
129 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
130 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
131 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
132 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
133 ! (not defined here)
134 integer, intent (out), dimension(m) :: type ! vector of equation types
135 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
136 ! (not defined here)
137 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
138 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
139 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
140 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
141 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
142 real*8 usrmem(*) ! optional user memory
143
144!
145! Information about Variables:
146! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
147! Default: the status information in Vsta is not used.
148!
149 curr(1) = 100.d0
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!
161! Constraint 1
162! Rhs = 10.0 and type Equality
163!
164 type(1) = 0
165 rhs(1) = 10.0d0
166!
167! Information about the Jacobian. We have to define Rowno, Value,
168! Nlflag and Colsta.
169!
170! Colsta = Start of column indices (No Defaults):
171! Rowno = Row indices
172! Value = Value of derivative (by default only linear
173! derivatives are used)
174! Nlflag = 0 for linear and 1 for nonlinear derivative
175! (not needed for completely linear models)
176!
177! Indices
178! x(1)
179! 1: 1
180!
181! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
182! x(1)
183! 1: NL
184!
185! Value (Linear only)
186!
187 colsta(1) = 1
188 colsta(2) = 2
189 rowno(1) = 1
190 nlflag(1) = 1
192 lr_readmatrix = 0 ! Return value means OK
193
194end Function lr_readmatrix
195!
196!==========================================================================
197! Compute nonlinear terms and non-constant Jacobian elements
198!
199
200!> Compute nonlinear terms and non-constant Jacobian elements
201!!
202!! @include{doc} fdeval_params.dox
203Integer Function lr_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
204 n, nz, thread, usrmem )
205#ifdef dec_directives_win32
206!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lr_FDEval
207#endif
208 implicit none
209 integer, intent (in) :: n ! number of variables
210 integer, intent (in) :: rowno ! number of the row to be evaluated
211 integer, intent (in) :: nz ! number of nonzeros in this row
212 real*8, intent (in), dimension(n) :: x ! vector of current solution values
213 real*8, intent (in out) :: g ! constraint value
214 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
215 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
216 ! in this row. Ffor information only.
217 integer, intent (in) :: mode ! evaluation mode: 1 = function value
218 ! 2 = derivatives, 3 = both
219 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
220 ! as errcnt is incremented
221 integer, intent (in out) :: errcnt ! error counter to be incremented in case
222 ! of function evaluation errors.
223 integer, intent (in) :: thread
224 real*8 usrmem(*) ! optional user memory
225
226!
227! Row 1: exp(x)
228!
229 if ( rowno .eq. 1 ) then
230!
231! Mode = 1 or 3. Function value:
232!
233 if ( mode .eq. 1 .or. mode .eq. 3 ) then
234 g = exp(x(1))
235 endif
236!
237! Mode = 2 or 3: Derivative values:
238!
239 if ( mode .eq. 2 .or. mode .eq. 3 ) then
240 jac(1) = exp(x(1))
241 endif
242 lr_fdeval = 0
243 else
244 lr_fdeval = 1
245 endif
246
247end Function lr_fdeval
Main program. A simple setup and call of CONOPT.
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_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.
subroutine finalize
Definition comdecl.f90:79
subroutine flog(msg, code)
Definition comdecl.f90:62
subroutine startup
Definition comdecl.f90:41