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)
139 call flog( "Successful Solve", 0 )
140!
141! Free solution memory
142!
143 call finalize
144
145End Program tutorial
146
147REAL FUNCTION rndx( )
148!
149! Defines a pseudo random number between 0 and 1
150!
151 IMPLICIT NONE
152
153 Integer, save :: seed = 12359
154
155 seed = mod(seed*1027+25,1048576)
156 rndx = float(seed)/float(1048576)
157
158END FUNCTION rndx
159!
160! ============================================================================
161! Define information about the model:
162!
163
164!> Define information about the model
165!!
166!! @include{doc} readMatrix_params.dox
167Integer Function elec_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
168 colsta, rowno, value, nlflag, n, m, nz, &
169 usrmem )
170#ifdef dec_directives_win32
171!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_ReadMatrix
172#endif
173 implicit none
174 integer, intent (in) :: n ! number of variables
175 integer, intent (in) :: m ! number of constraints
176 integer, intent (in) :: nz ! number of nonzeros
177 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
178 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
179 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
180 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
181 ! (not defined here)
182 integer, intent (out), dimension(m) :: type ! vector of equation types
183 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
184 ! (not defined here)
185 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
186 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
187 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
188 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
189 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
190 real*8 usrmem(*) ! optional user memory
191
192 Integer :: ne
193 Integer :: i, k
194 real*8, parameter :: pi = 3.141592
195 real*8 :: theta, phi
196 Real, External :: rndx
197
198 ne = n / 3
199!
200! Information about Variables:
201! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
202! Default: the status information in Vsta is not used.
203!
204 DO i = 1, ne
205 theta = rndx()
206 phi = rndx()
207 curr(i ) = cos(theta)*sin(phi)
208 curr(i+ ne) = sin(theta)*sin(phi)
209 curr(i+2*ne) = cos(phi)
210 enddo
211!
212! Information about Constraints:
213! Default: Rhs = 0
214! Default: the status information in Esta and the function
215! value in FV are not used.
216! Default: Type: There is no default.
217! 0 = Equality,
218! 1 = Greater than or equal,
219! 2 = Less than or equal,
220! 3 = Non binding.
221!
222! Constraint 1 (Objective)
223! Rhs = 0.0 and type Non binding
224!
225 DO i = 1, ne
226 Type(i) = 0
227 rhs(i) = 1.d0
228 Enddo
229 type(ne+1) = 3
230!
231! Information about the Jacobian. We have to define Rowno, Value,
232! Nlflag and Colsta.
233!
234! Colsta = Start of column indices (No Defaults):
235! Rowno = Row indices
236! Value = Value of derivative (by default only linear
237! derivatives are used)
238! Nlflag = 0 for linear and 1 for nonlinear derivative
239! (not needed for completely linear models)
240!
241! Indices
242! x(1) x(2) .. y(1) y(2) .. z(1) z(2) ..
243! 1: 1 2*Ne+1 3*Ne+1
244! 2: 3 2*Ne+3 3*Ne+3
245! ..
246! Obj: 2 4 2*Ne+2 2*Ne+4 3*Ne+2 3*Ne+4
247!
248! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
249! All nonzeros are nonlinear
250!
251 DO i = 1, n+1
252 colsta(i) = 2*i-1
253 enddo
254 k = 0
255 DO i = 1, ne ! x variablex
256 k = k + 1
257 rowno(k) = i ! Ball constraint
258 nlflag(k) = 1
259 k = k + 1
260 rowno(k) = ne+1 ! Objective
261 nlflag(k) = 1
262 enddo
263 DO i = 1, ne ! y variablex
264 k = k + 1
265 rowno(k) = i ! Ball constraint
266 nlflag(k) = 1
267 k = k + 1
268 rowno(k) = ne+1 ! Objective
269 nlflag(k) = 1
270 enddo
271 DO i = 1, ne ! z variablex
272 k = k + 1
273 rowno(k) = i ! Ball constraint
274 nlflag(k) = 1
275 k = k + 1
276 rowno(k) = ne+1 ! Objective
277 nlflag(k) = 1
278 enddo
280 elec_readmatrix = 0 ! Return value means OK
281
282end Function elec_readmatrix
283!
284!==========================================================================
285! Compute nonlinear terms and non-constant Jacobian elements
286!
287
288!> Compute nonlinear terms and non-constant Jacobian elements
289!!
290!! @include{doc} fdeval_params.dox
291Integer Function elec_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
292 n, nz, thread, usrmem )
293#ifdef dec_directives_win32
294!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_FDEval
295#endif
296 implicit none
297 integer, intent (in) :: n ! number of variables
298 integer, intent (in) :: rowno ! number of the row to be evaluated
299 integer, intent (in) :: nz ! number of nonzeros in this row
300 real*8, intent (in), dimension(n) :: x ! vector of current solution values
301 real*8, intent (in out) :: g ! constraint value
302 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
303 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
304 ! in this row. Ffor information only.
305 integer, intent (in) :: mode ! evaluation mode: 1 = function value
306 ! 2 = derivatives, 3 = both
307 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
308 ! as errcnt is incremented
309 integer, intent (in out) :: errcnt ! error counter to be incremented in case
310 ! of function evaluation errors.
311 integer, intent (in) :: thread
312 real*8 usrmem(*) ! optional user memory
313
314 Integer :: ne
315 Integer :: i, j
316 real*8 :: dist, dist32, tx, ty, tz
317
318 ne = n / 3
319!
320! Row NE+1: the objective function is nonlinear
321!
322 if ( rowno == ne+1 ) then
323!
324! Mode = 1 or 3. Function value:
325!
326 if ( mode .eq. 1 .or. mode .eq. 3 ) then
327 g = 0.d0
328 do i = 2, ne
329 do j = 1, i-1
330 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
331 g = g + 1.d0/sqrt(dist)
332 enddo
333 enddo
334 endif
335!
336! Mode = 2 or 3: Derivative values: w.r.t. x: 2*(x(i)-x(j))*(-1/2)*dist(-3/2)
337!
338 if ( mode .eq. 2 .or. mode .eq. 3 ) then
339 do i = 1, 3*ne
340 jac(i) = 0.d0
341 enddo
342 do i = 2, ne
343 do j = 1, i-1
344 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
345 dist32 = (1.d0/sqrt(dist))**3
346 tx = -(x(i)-x(j))*dist32
347 ty = -(x(i+ne)-x(j+ne))*dist32
348 tz = -(x(i+2*ne)-x(j+2*ne))*dist32
349 jac(i) = jac(i) + tx
350 jac(j) = jac(j) - tx
351 jac(i+ne) = jac(i+ne) + ty
352 jac(j+ne) = jac(j+ne) - ty
353 jac(i+2*ne) = jac(i+2*ne) + tz
354 jac(j+2*ne) = jac(j+2*ne) - tz
355 enddo
356 enddo
357 endif
358!
359 Else ! this is ball constraint rowno
360!
361! Mode = 1 or 3. Function value:
362!
363 i = rowno
364 if ( mode .eq. 1 .or. mode .eq. 3 ) then
365 g = x(i)**2 + x(i+ne)**2 + x(i+2*ne)**2
366 endif
367!
368! Mode = 2 or 3: Derivative values:
369!
370 if ( mode .eq. 2 .or. mode .eq. 3 ) then
371 jac(i) = 2.d0*x(i)
372 jac(i+ne) = 2.d0*x(i+ne)
373 jac(i+2*ne) = 2.d0*x(i+2*ne)
374 endif
375
376 endif
377 elec_fdeval = 0
378
379end 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: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 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:281
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:161
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
subroutine finalize
Definition comdecl.f90:79
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