CONOPT
Loading...
Searching...
No Matches
elec.f90
Go to the documentation of this file.
1!> @file elec.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Electron model from COPS test set.
6!!
7!!
8!! This is a CONOPT implementation of the GAMS model:
9!!
10!!
11!! @verbatim
12!! Set i electrons /i1 * i%np%/
13!! ut(i,i) upper triangular part;
14!!
15!! Alias (i,j);
16!! ut(i,j)$(ord(j) > ord(i)) = yes;
17!!
18!! Variables x(i) x-coordinate of the electron
19!! y(i) y-coordinate of the electron
20!! z(i) z-coordinate of the electron
21!! potential Coulomb potential;
22!!
23!! Equations obj objective
24!! ball(i) points on unit ball;
25!!
26!! obj.. potential =e=
27!! sum{ut(i,j), 1.0/sqrt(sqr(x[i]-x[j]) + sqr(y[i]-y[j]) + sqr(z[i]-z[j]))};
28!!
29!! ball(i).. sqr(x(i)) + sqr(y(i)) + sqr(z(i)) =e= 1;
30!!
31!!
32!! * Set the starting point to a quasi-uniform distribution
33!! * of electrons on a unit sphere
34!!
35!! scalar pi a famous constant;
36!! pi = 2*arctan(inf);
37!!
38!! parameter theta(i), phi(i);
39!! theta(i) = 2*pi*uniform(0,1);
40!! phi(i) = pi*uniform(0,1);
41!!
42!! x.l(i) = cos(theta(i))*sin(phi(i));
43!! y.l(i) = sin(theta(i))*sin(phi(i));
44!! z.l(i) = cos(phi(i));
45!! @endverbatim
46!!
47!!
48!! For more information about the individual callbacks, please have a look at the source code.
49
50!> Main program. A simple setup and call of CONOPT
51!!
52Program tutorial
53
54 Use proginfo
55 Use coidef
56 implicit None
57!
58! Declare the user callback routines as Integer, External:
59!
60 Integer, External :: elec_readmatrix ! Mandatory Matrix definition routine defined below
61 Integer, External :: elec_fdeval ! Function and Derivative evaluation routine
62 ! needed a nonlinear model.
63 Integer, External :: std_status ! Standard callback for displaying solution status
64 Integer, External :: std_solution ! Standard callback for displaying solution values
65 Integer, External :: std_message ! Standard callback for managing messages
66 Integer, External :: std_errmsg ! Standard callback for managing error messages
67#if defined(itl)
68!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_ReadMatrix
69!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_FDEval
70!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
71!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
72!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
73!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
74#endif
75!
76! Control vector
77!
78 INTEGER :: numcallback
79 INTEGER, Dimension(:), Pointer :: cntvect
80 INTEGER :: coi_error
81 Integer :: ne ! Number of Electrons
82!
83! Create and initialize a Control Vector
84!
85 call startup
86
87 numcallback = coidef_size()
88 Allocate( cntvect(numcallback) )
89 coi_error = coidef_inifort( cntvect )
90!
91! Tell CONOPT about the size of the model by populating the Control Vector:
92!
93 ne = 200 ! Number of electrons
94 coi_error = max( coi_error, coidef_numvar( cntvect, 3*ne ) ) ! # variables
95 coi_error = max( coi_error, coidef_numcon( cntvect, ne+1 ) ) ! # constraints
96 coi_error = max( coi_error, coidef_numnz( cntvect, 6*ne ) ) ! # nonzeros in the Jacobian
97 coi_error = max( coi_error, coidef_numnlnz( cntvect, 6*ne ) ) ! # of which are nonlinear
98 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
99 coi_error = max( coi_error, coidef_objcon( cntvect, ne+1 ) ) ! Objective is constraint NE+1
100 coi_error = max( coi_error, coidef_optfile( cntvect, 'elec.opt' ) )
101!
102! Tell CONOPT about the callback routines:
103!
104 coi_error = max( coi_error, coidef_readmatrix( cntvect, elec_readmatrix ) )
105 coi_error = max( coi_error, coidef_fdeval( cntvect, elec_fdeval ) )
106 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
107 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
108 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
109 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
110
111#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
112 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
113#endif
114
115 If ( coi_error .ne. 0 ) THEN
116 write(*,*)
117 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
118 write(*,*)
119 call flog( "Skipping Solve due to setup errors", 1 )
120 ENDIF
121!
122! Start CONOPT:
123!
124 coi_error = coi_solve( cntvect )
125 If ( coi_error /= 0 ) then
126 call flog( "Errors encountered during solution", 1 )
127 elseif ( stacalls == 0 .or. solcalls == 0 ) then
128 call flog( "Status or Solution routine was not called", 1 )
129 elseif ( sstat /= 1 .or. mstat /= 2 ) then
130 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
131 endif
132
133 write(*,*)
134 write(*,*) 'End of Electron example. Return code=',coi_error
135
136 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
137
138 call flog( "Successful Solve", 0 )
139
140End Program tutorial
141
142REAL FUNCTION rndx( )
143!
144! Defines a pseudo random number between 0 and 1
145!
146 IMPLICIT NONE
147
148 Integer, save :: seed = 12359
149
150 seed = mod(seed*1027+25,1048576)
151 rndx = float(seed)/float(1048576)
152
153END FUNCTION rndx
154!
155! ============================================================================
156! Define information about the model:
157!
158
159!> Define information about the model
160!!
161!! @include{doc} readMatrix_params.dox
162Integer Function elec_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
163 colsta, rowno, value, nlflag, n, m, nz, &
164 usrmem )
165#if defined(itl)
166!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_ReadMatrix
167#endif
168 implicit none
169 integer, intent (in) :: n ! number of variables
170 integer, intent (in) :: m ! number of constraints
171 integer, intent (in) :: nz ! number of nonzeros
172 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
173 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
174 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
175 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
176 ! (not defined here)
177 integer, intent (out), dimension(m) :: type ! vector of equation types
178 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
179 ! (not defined here)
180 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
181 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
182 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
183 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
184 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
185 real*8 usrmem(*) ! optional user memory
186
187 Integer :: ne
188 Integer :: i, k
189 real*8, parameter :: pi = 3.141592
190 real*8 :: theta, phi
191 Real, External :: rndx
192
193 ne = n / 3
194!
195! Information about Variables:
196! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
197! Default: the status information in Vsta is not used.
198!
199 DO i = 1, ne
200 theta = rndx()
201 phi = rndx()
202 curr(i ) = cos(theta)*sin(phi)
203 curr(i+ ne) = sin(theta)*sin(phi)
204 curr(i+2*ne) = cos(phi)
205 enddo
206!
207! Information about Constraints:
208! Default: Rhs = 0
209! Default: the status information in Esta and the function
210! value in FV are not used.
211! Default: Type: There is no default.
212! 0 = Equality,
213! 1 = Greater than or equal,
214! 2 = Less than or equal,
215! 3 = Non binding.
216!
217! Constraint 1 (Objective)
218! Rhs = 0.0 and type Non binding
219!
220 DO i = 1, ne
221 Type(i) = 0
222 rhs(i) = 1.d0
223 Enddo
224 type(ne+1) = 3
225!
226! Information about the Jacobian. We have to define Rowno, Value,
227! Nlflag and Colsta.
228!
229! Colsta = Start of column indices (No Defaults):
230! Rowno = Row indices
231! Value = Value of derivative (by default only linear
232! derivatives are used)
233! Nlflag = 0 for linear and 1 for nonlinear derivative
234! (not needed for completely linear models)
235!
236! Indices
237! x(1) x(2) .. y(1) y(2) .. z(1) z(2) ..
238! 1: 1 2*Ne+1 3*Ne+1
239! 2: 3 2*Ne+3 3*Ne+3
240! ..
241! Obj: 2 4 2*Ne+2 2*Ne+4 3*Ne+2 3*Ne+4
242!
243! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
244! All nonzeros are nonlinear
245!
246 DO i = 1, n+1
247 colsta(i) = 2*i-1
248 enddo
249 k = 0
250 DO i = 1, ne ! x variablex
251 k = k + 1
252 rowno(k) = i ! Ball constraint
253 nlflag(k) = 1
254 k = k + 1
255 rowno(k) = ne+1 ! Objective
256 nlflag(k) = 1
257 enddo
258 DO i = 1, ne ! y variablex
259 k = k + 1
260 rowno(k) = i ! Ball constraint
261 nlflag(k) = 1
262 k = k + 1
263 rowno(k) = ne+1 ! Objective
264 nlflag(k) = 1
265 enddo
266 DO i = 1, ne ! z variablex
267 k = k + 1
268 rowno(k) = i ! Ball constraint
269 nlflag(k) = 1
270 k = k + 1
271 rowno(k) = ne+1 ! Objective
272 nlflag(k) = 1
273 enddo
274
275 elec_readmatrix = 0 ! Return value means OK
276
277end Function elec_readmatrix
278!
279!==========================================================================
280! Compute nonlinear terms and non-constant Jacobian elements
281!
282
283!> Compute nonlinear terms and non-constant Jacobian elements
284!!
285!! @include{doc} fdeval_params.dox
286Integer Function elec_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
287 n, nz, thread, usrmem )
288#if defined(itl)
289!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_FDEval
290#endif
291 implicit none
292 integer, intent (in) :: n ! number of variables
293 integer, intent (in) :: rowno ! number of the row to be evaluated
294 integer, intent (in) :: nz ! number of nonzeros in this row
295 real*8, intent (in), dimension(n) :: x ! vector of current solution values
296 real*8, intent (in out) :: g ! constraint value
297 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
298 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
299 ! in this row. Ffor information only.
300 integer, intent (in) :: mode ! evaluation mode: 1 = function value
301 ! 2 = derivatives, 3 = both
302 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
303 ! as errcnt is incremented
304 integer, intent (in out) :: errcnt ! error counter to be incremented in case
305 ! of function evaluation errors.
306 integer, intent (in) :: thread
307 real*8 usrmem(*) ! optional user memory
308
309 Integer :: ne
310 Integer :: i, j
311 real*8 :: dist, dist32, tx, ty, tz
312
313 ne = n / 3
314!
315! Row NE+1: the objective function is nonlinear
316!
317 if ( rowno == ne+1 ) then
318!
319! Mode = 1 or 3. Function value:
320!
321 if ( mode .eq. 1 .or. mode .eq. 3 ) then
322 g = 0.d0
323 do i = 2, ne
324 do j = 1, i-1
325 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
326 g = g + 1.d0/sqrt(dist)
327 enddo
328 enddo
329 endif
330!
331! Mode = 2 or 3: Derivative values: w.r.t. x: 2*(x(i)-x(j))*(-1/2)*dist(-3/2)
332!
333 if ( mode .eq. 2 .or. mode .eq. 3 ) then
334 do i = 1, 3*ne
335 jac(i) = 0.d0
336 enddo
337 do i = 2, ne
338 do j = 1, i-1
339 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
340 dist32 = (1.d0/sqrt(dist))**3
341 tx = -(x(i)-x(j))*dist32
342 ty = -(x(i+ne)-x(j+ne))*dist32
343 tz = -(x(i+2*ne)-x(j+2*ne))*dist32
344 jac(i) = jac(i) + tx
345 jac(j) = jac(j) - tx
346 jac(i+ne) = jac(i+ne) + ty
347 jac(j+ne) = jac(j+ne) - ty
348 jac(i+2*ne) = jac(i+2*ne) + tz
349 jac(j+2*ne) = jac(j+2*ne) - tz
350 enddo
351 enddo
352 endif
353!
354 Else ! this is ball constraint rowno
355!
356! Mode = 1 or 3. Function value:
357!
358 i = rowno
359 if ( mode .eq. 1 .or. mode .eq. 3 ) then
360 g = x(i)**2 + x(i+ne)**2 + x(i+2*ne)**2
361 endif
362!
363! Mode = 2 or 3: Derivative values:
364!
365 if ( mode .eq. 2 .or. mode .eq. 3 ) then
366 jac(i) = 2.d0*x(i)
367 jac(i+ne) = 2.d0*x(i+ne)
368 jac(i+2*ne) = 2.d0*x(i+2*ne)
369 endif
370
371 endif
372 elec_fdeval = 0
373
374end Function elec_fdeval
Main program. A simple setup and call of CONOPT.
Definition tutorial.java:14
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function elec_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition elec.f90:288
integer function elec_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition elec.f90:165
integer function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition coistart.f90:629
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition coistart.f90:552
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
float rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq.c:23
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35