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