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#if defined(_WIN32) && !defined(_WIN64)
51#define dec_directives_win32
52#endif
53
54!> Main program. A simple setup and call of CONOPT
55!!
56Program tutorial
57
59 Use conopt
60 implicit None
61!
62! Declare the user callback routines as Integer, External:
63!
64 Integer, External :: elec_readmatrix ! Mandatory Matrix definition routine defined below
65 Integer, External :: elec_fdeval ! Function and Derivative evaluation routine
66 ! needed a nonlinear model.
67 Integer, External :: std_status ! Standard callback for displaying solution status
68 Integer, External :: std_solution ! Standard callback for displaying solution values
69 Integer, External :: std_message ! Standard callback for managing messages
70 Integer, External :: std_errmsg ! Standard callback for managing error messages
71#ifdef dec_directives_win32
72!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_ReadMatrix
73!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_FDEval
74!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
75!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
76!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
77!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
78#endif
79!
80! Control vector
81!
82 INTEGER, Dimension(:), Pointer :: cntvect
83 INTEGER :: coi_error
84 Integer :: ne ! Number of Electrons
85!
86! Create and initialize a Control Vector
87!
88 call startup
89
90 coi_error = coi_create( cntvect )
91!
92! Tell CONOPT about the size of the model by populating the Control Vector:
93!
94 ne = 200 ! Number of electrons
95 coi_error = max( coi_error, coidef_numvar( cntvect, 3*ne ) ) ! # variables
96 coi_error = max( coi_error, coidef_numcon( cntvect, ne+1 ) ) ! # constraints
97 coi_error = max( coi_error, coidef_numnz( cntvect, 6*ne ) ) ! # nonzeros in the Jacobian
98 coi_error = max( coi_error, coidef_numnlnz( cntvect, 6*ne ) ) ! # of which are nonlinear
99 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
100 coi_error = max( coi_error, coidef_objcon( cntvect, ne+1 ) ) ! Objective is constraint NE+1
101 coi_error = max( coi_error, coidef_optfile( cntvect, 'elec.opt' ) )
102!
103! Tell CONOPT about the callback routines:
104!
105 coi_error = max( coi_error, coidef_readmatrix( cntvect, elec_readmatrix ) )
106 coi_error = max( coi_error, coidef_fdeval( cntvect, elec_fdeval ) )
107 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
108 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
109 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
110 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
111
112#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
113 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
114#endif
115
116 If ( coi_error .ne. 0 ) THEN
117 write(*,*)
118 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
119 write(*,*)
120 call flog( "Skipping Solve due to setup errors", 1 )
121 ENDIF
122!
123! Start CONOPT:
124!
125 coi_error = coi_solve( cntvect )
126 If ( coi_error /= 0 ) then
127 call flog( "Errors encountered during solution", 1 )
128 elseif ( stacalls == 0 .or. solcalls == 0 ) then
129 call flog( "Status or Solution routine was not called", 1 )
130 elseif ( sstat /= 1 .or. mstat /= 2 ) then
131 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
132 endif
133
134 write(*,*)
135 write(*,*) 'End of Electron example. Return code=',coi_error
136
137 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
138
139 call flog( "Successful Solve", 0 )
140
141End Program tutorial
142
143REAL FUNCTION rndx( )
144!
145! Defines a pseudo random number between 0 and 1
146!
147 IMPLICIT NONE
148
149 Integer, save :: seed = 12359
150
151 seed = mod(seed*1027+25,1048576)
152 rndx = float(seed)/float(1048576)
153
154END FUNCTION rndx
155!
156! ============================================================================
157! Define information about the model:
158!
159
160!> Define information about the model
161!!
162!! @include{doc} readMatrix_params.dox
163Integer Function elec_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
164 colsta, rowno, value, nlflag, n, m, nz, &
165 usrmem )
166#ifdef dec_directives_win32
167!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_ReadMatrix
168#endif
169 implicit none
170 integer, intent (in) :: n ! number of variables
171 integer, intent (in) :: m ! number of constraints
172 integer, intent (in) :: nz ! number of nonzeros
173 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
174 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
175 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
176 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
177 ! (not defined here)
178 integer, intent (out), dimension(m) :: type ! vector of equation types
179 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
180 ! (not defined here)
181 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
182 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
183 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
184 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
185 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
186 real*8 usrmem(*) ! optional user memory
187
188 Integer :: ne
189 Integer :: i, k
190 real*8, parameter :: pi = 3.141592
191 real*8 :: theta, phi
192 Real, External :: rndx
193
194 ne = n / 3
195!
196! Information about Variables:
197! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
198! Default: the status information in Vsta is not used.
199!
200 DO i = 1, ne
201 theta = rndx()
202 phi = rndx()
203 curr(i ) = cos(theta)*sin(phi)
204 curr(i+ ne) = sin(theta)*sin(phi)
205 curr(i+2*ne) = cos(phi)
206 enddo
207!
208! Information about Constraints:
209! Default: Rhs = 0
210! Default: the status information in Esta and the function
211! value in FV are not used.
212! Default: Type: There is no default.
213! 0 = Equality,
214! 1 = Greater than or equal,
215! 2 = Less than or equal,
216! 3 = Non binding.
217!
218! Constraint 1 (Objective)
219! Rhs = 0.0 and type Non binding
220!
221 DO i = 1, ne
222 Type(i) = 0
223 rhs(i) = 1.d0
224 Enddo
225 type(ne+1) = 3
226!
227! Information about the Jacobian. We have to define Rowno, Value,
228! Nlflag and Colsta.
229!
230! Colsta = Start of column indices (No Defaults):
231! Rowno = Row indices
232! Value = Value of derivative (by default only linear
233! derivatives are used)
234! Nlflag = 0 for linear and 1 for nonlinear derivative
235! (not needed for completely linear models)
236!
237! Indices
238! x(1) x(2) .. y(1) y(2) .. z(1) z(2) ..
239! 1: 1 2*Ne+1 3*Ne+1
240! 2: 3 2*Ne+3 3*Ne+3
241! ..
242! Obj: 2 4 2*Ne+2 2*Ne+4 3*Ne+2 3*Ne+4
243!
244! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
245! All nonzeros are nonlinear
246!
247 DO i = 1, n+1
248 colsta(i) = 2*i-1
249 enddo
250 k = 0
251 DO i = 1, ne ! x variablex
252 k = k + 1
253 rowno(k) = i ! Ball constraint
254 nlflag(k) = 1
255 k = k + 1
256 rowno(k) = ne+1 ! Objective
257 nlflag(k) = 1
258 enddo
259 DO i = 1, ne ! y variablex
260 k = k + 1
261 rowno(k) = i ! Ball constraint
262 nlflag(k) = 1
263 k = k + 1
264 rowno(k) = ne+1 ! Objective
265 nlflag(k) = 1
266 enddo
267 DO i = 1, ne ! z variablex
268 k = k + 1
269 rowno(k) = i ! Ball constraint
270 nlflag(k) = 1
271 k = k + 1
272 rowno(k) = ne+1 ! Objective
273 nlflag(k) = 1
274 enddo
276 elec_readmatrix = 0 ! Return value means OK
277
278end Function elec_readmatrix
279!
280!==========================================================================
281! Compute nonlinear terms and non-constant Jacobian elements
282!
283
284!> Compute nonlinear terms and non-constant Jacobian elements
285!!
286!! @include{doc} fdeval_params.dox
287Integer Function elec_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
288 n, nz, thread, usrmem )
289#ifdef dec_directives_win32
290!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_FDEval
291#endif
292 implicit none
293 integer, intent (in) :: n ! number of variables
294 integer, intent (in) :: rowno ! number of the row to be evaluated
295 integer, intent (in) :: nz ! number of nonzeros in this row
296 real*8, intent (in), dimension(n) :: x ! vector of current solution values
297 real*8, intent (in out) :: g ! constraint value
298 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
299 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
300 ! in this row. Ffor information only.
301 integer, intent (in) :: mode ! evaluation mode: 1 = function value
302 ! 2 = derivatives, 3 = both
303 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
304 ! as errcnt is incremented
305 integer, intent (in out) :: errcnt ! error counter to be incremented in case
306 ! of function evaluation errors.
307 integer, intent (in) :: thread
308 real*8 usrmem(*) ! optional user memory
309
310 Integer :: ne
311 Integer :: i, j
312 real*8 :: dist, dist32, tx, ty, tz
313
314 ne = n / 3
315!
316! Row NE+1: the objective function is nonlinear
317!
318 if ( rowno == ne+1 ) then
319!
320! Mode = 1 or 3. Function value:
321!
322 if ( mode .eq. 1 .or. mode .eq. 3 ) then
323 g = 0.d0
324 do i = 2, ne
325 do j = 1, i-1
326 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
327 g = g + 1.d0/sqrt(dist)
328 enddo
329 enddo
330 endif
331!
332! Mode = 2 or 3: Derivative values: w.r.t. x: 2*(x(i)-x(j))*(-1/2)*dist(-3/2)
333!
334 if ( mode .eq. 2 .or. mode .eq. 3 ) then
335 do i = 1, 3*ne
336 jac(i) = 0.d0
337 enddo
338 do i = 2, ne
339 do j = 1, i-1
340 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
341 dist32 = (1.d0/sqrt(dist))**3
342 tx = -(x(i)-x(j))*dist32
343 ty = -(x(i+ne)-x(j+ne))*dist32
344 tz = -(x(i+2*ne)-x(j+2*ne))*dist32
345 jac(i) = jac(i) + tx
346 jac(j) = jac(j) - tx
347 jac(i+ne) = jac(i+ne) + ty
348 jac(j+ne) = jac(j+ne) - ty
349 jac(i+2*ne) = jac(i+2*ne) + tz
350 jac(j+2*ne) = jac(j+2*ne) - tz
351 enddo
352 enddo
353 endif
354!
355 Else ! this is ball constraint rowno
356!
357! Mode = 1 or 3. Function value:
358!
359 i = rowno
360 if ( mode .eq. 1 .or. mode .eq. 3 ) then
361 g = x(i)**2 + x(i+ne)**2 + x(i+2*ne)**2
362 endif
363!
364! Mode = 2 or 3: Derivative values:
365!
366 if ( mode .eq. 2 .or. mode .eq. 3 ) then
367 jac(i) = 2.d0*x(i)
368 jac(i+ne) = 2.d0*x(i+ne)
369 jac(i+2*ne) = 2.d0*x(i+2*ne)
370 endif
371
372 endif
373 elec_fdeval = 0
374
375end 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: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 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:277
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:157
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_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition conopt.f90:239
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
float rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq.c:22
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41