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