CONOPT
Loading...
Searching...
No Matches
elec2.f90
Go to the documentation of this file.
1!> @file elec2.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Electron model from COPS test set, this time with a Hessian.
6!!
7!! The hessian for this model is extremely dense and in the first solve
8!! CONOPT will not use it.
9!! During the second solve we redefine HessFac so the hessian is used.
10!!
11!! Since the model is non-convex we will several times experience
12!! negative curvature in the SQP sub-solver. With the correct Hessian
13!! this seems to be more of a problems and the extra information is
14!! actually counter-productive -- not because it takes a long time to
15!! evaluate the Hessian but because the use takes time and the number
16!! of iterations grow.
17!!
18!!
19!! This is a CONOPT implementation of the GAMS model:
20!!
21!!
22!! @verbatim
23!! Set i electrons /i1 * i%np%/
24!! ut(i,i) upper triangular part;
25!!
26!! Alias (i,j);
27!! ut(i,j)$(ord(j) > ord(i)) = yes;
28!!
29!! Variables x(i) x-coordinate of the electron
30!! y(i) y-coordinate of the electron
31!! z(i) z-coordinate of the electron
32!! potential Coulomb potential;
33!!
34!! Equations obj objective
35!! ball(i) points on unit ball;
36!!
37!! obj.. potential =e=
38!! sum{ut(i,j), 1.0/sqrt(sqr(x[i]-x[j]) + sqr(y[i]-y[j]) + sqr(z[i]-z[j]))};
39!!
40!! ball(i).. sqr(x(i)) + sqr(y(i)) + sqr(z(i)) =e= 1;
41!!
42!!
43!! * Set the starting point to a quasi-uniform distribution
44!! * of electrons on a unit sphere
45!!
46!! scalar pi a famous constant;
47!! pi = 2*arctan(inf);
48!!
49!! parameter theta(i), phi(i);
50!! theta(i) = 2*pi*uniform(0,1);
51!! phi(i) = pi*uniform(0,1);
52!!
53!! x.l(i) = cos(theta(i))*sin(phi(i));
54!! y.l(i) = sin(theta(i))*sin(phi(i));
55!! z.l(i) = cos(phi(i));
56!! @endverbatim
57!!
58!!
59!! For more information about the individual callbacks, please have a look at the source code.
60
61#if defined(_WIN32) && !defined(_WIN64)
62#define dec_directives_win32
63#endif
64
65Module hesscalls
66 Integer :: Hcalls
67End Module hesscalls
69Module random
70 Integer :: seed
71End Module random
73!> Main program. A simple setup and call of CONOPT
74!!
75Program tutorial
76
78 Use conopt
79 Use hesscalls
80 Use random
81 implicit None
82!
83! Declare the user callback routines as Integer, External:
84!
85 Integer, External :: elec_readmatrix ! Mandatory Matrix definition routine defined below
86 Integer, External :: elec_fdeval ! Function and Derivative evaluation routine
87 ! needed a nonlinear model.
88 Integer, External :: std_status ! Standard callback for displaying solution status
89 Integer, External :: std_solution ! Standard callback for displaying solution values
90 Integer, External :: std_message ! Standard callback for managing messages
91 Integer, External :: std_errmsg ! Standard callback for managing error messages
92 Integer, External :: elec_2dlagrsize ! Callback for second derivatives size
93 Integer, External :: elec_2dlagrstr ! Callback for second derivatives structure
94 Integer, External :: elec_2dlagrval ! Callback for second derivatives values
95#ifdef dec_directives_win32
96!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_ReadMatrix
97!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_FDEval
98!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
99!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
100!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
101!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
102!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrSize
103!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrStr
104!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrVal
105#endif
106!
107! Control vector
108!
109 INTEGER, Dimension(:), Pointer :: cntvect
110 INTEGER :: coi_error
111 Integer :: ne ! Number of Electrons
112!
113! Create and initialize a Control Vector
114!
115 call startup
116
117 coi_error = coi_create( cntvect )
118!
119! Tell CONOPT about the size of the model by populating the Control Vector:
120!
121 ne = 150 ! Number of electrons
122 coi_error = max( coi_error, coidef_numvar( cntvect, 3*ne ) ) ! # variables
123 coi_error = max( coi_error, coidef_numcon( cntvect, ne+1 ) ) ! # constraints
124 coi_error = max( coi_error, coidef_numnz( cntvect, 6*ne ) ) ! # nonzeros in the Jacobian
125 coi_error = max( coi_error, coidef_numnlnz( cntvect, 6*ne ) ) ! # of which are nonlinear
126 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
127 coi_error = max( coi_error, coidef_objcon( cntvect, ne+1 ) ) ! Objective is constraint NE+1
128 coi_error = max( coi_error, coidef_debug2d( cntvect, 0 ) ) ! turn 2nd derivative debugging on or off
129 coi_error = max( coi_error, coidef_optfile( cntvect, 'elec2.opt' ) )
130!
131! Tell CONOPT about the callback routines:
132!
133 coi_error = max( coi_error, coidef_readmatrix( cntvect, elec_readmatrix ) )
134 coi_error = max( coi_error, coidef_fdeval( cntvect, elec_fdeval ) )
135 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
136 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
137 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
138 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
139 coi_error = max( coi_error, coidef_2dlagrsize( cntvect, elec_2dlagrsize ) )
140 coi_error = max( coi_error, coidef_2dlagrstr( cntvect, elec_2dlagrstr ) )
141 coi_error = max( coi_error, coidef_2dlagrval( cntvect, elec_2dlagrval ) )
142
143#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
144 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
145#endif
146
147 If ( coi_error .ne. 0 ) THEN
148 write(*,*)
149 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
150 write(*,*)
151 call flog( "Skipping Solve due to setup errors", 1 )
152 ENDIF
153!
154! Start CONOPT:
155!
156 coi_error = max( coi_error, coidef_hessfac( cntvect, 1.0d0 ) ) ! very little space to prevent Hessian to be used
157 hcalls = 0
158 seed = 12345
159 coi_error = coi_solve( cntvect )
160 If ( coi_error /= 0 ) then
161 call flog( "Solve 1: Errors encountered during solution", 1 )
162 elseif ( stacalls == 0 .or. solcalls == 0 ) then
163 call flog( "Solve 1: Status or Solution routine was not called", 1 )
164 elseif ( sstat /= 1 .or. mstat /= 2 ) then
165 call flog( "Solve 1: Solver and Model Status was not as expected (1,2)", 1 )
166 elseif ( hcalls /= 0 ) then
167 call flog( "Solve 1: Solve used 2nd derivatives but should reject on memory", 1 )
168 endif
169!
170! Start CONOPT again with more space for the Hessian of the Lagrangian
171!
172 coi_error = max( coi_error, coidef_hessfac( cntvect, 0.0d0 ) ) ! enough to allow Hessian to be used
173 hcalls = 0
174 seed = 12345 ! Make sure we get the same model the second time
175 coi_error = coi_solve( cntvect )
176 If ( coi_error /= 0 ) then
177 call flog( "Solve 2: Errors encountered during solution", 1 )
178 elseif ( stacalls == 0 .or. solcalls == 0 ) then
179 call flog( "Solve 2: Status or Solution routine was not called", 1 )
180 elseif ( sstat /= 1 .or. mstat /= 2 ) then
181 call flog( "Solve 2: Solver and Model Status was not as expected (1,2)", 1 )
182 elseif ( hcalls == 0 ) then
183 call flog( "Solve 2: Solve did not use 2nd derivatives but should have enough memory", 1 )
184 endif
185
186
187 write(*,*)
188 write(*,*) 'End of Electron example. Return code=',coi_error
189
190 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
191
192 call flog( "Successful Solve", 0 )
193!
194! Free solution memory
195!
196 call finalize
197
198End Program tutorial
199
200REAL FUNCTION rndx( )
201!
202! Defines a pseudo random number between 0 and 1
203!
204 Use random
205 IMPLICIT NONE
206
207 seed = mod(seed*1027+25,1048576)
208 rndx = float(seed)/float(1048576)
209
210END FUNCTION rndx
211!
212! ============================================================================
213! Define information about the model:
214!
215
216!> Define information about the model
217!!
218!! @include{doc} readMatrix_params.dox
219Integer Function elec_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
220 colsta, rowno, value, nlflag, n, m, nz, &
221 usrmem )
222#ifdef dec_directives_win32
223!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_ReadMatrix
224#endif
225 implicit none
226 integer, intent (in) :: n ! number of variables
227 integer, intent (in) :: m ! number of constraints
228 integer, intent (in) :: nz ! number of nonzeros
229 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
230 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
231 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
232 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
233 ! (not defined here)
234 integer, intent (out), dimension(m) :: type ! vector of equation types
235 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
236 ! (not defined here)
237 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
238 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
239 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
240 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
241 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
242 real*8 usrmem(*) ! optional user memory
243
244 Integer :: ne
245 Integer :: i, k
246 real*8, parameter :: pi = 3.141592
247 real*8 :: theta, phi
248 Real, External :: rndx
249
250 ne = n / 3
251!
252! Information about Variables:
253! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
254! Default: the status information in Vsta is not used.
255!
256 DO i = 1, ne
257 theta = rndx()
258 phi = rndx()
259 curr(i ) = cos(theta)*sin(phi)
260 curr(i+ ne) = sin(theta)*sin(phi)
261 curr(i+2*ne) = cos(phi)
262 enddo
263!
264! Information about Constraints:
265! Default: Rhs = 0
266! Default: the status information in Esta and the function
267! value in FV are not used.
268! Default: Type: There is no default.
269! 0 = Equality,
270! 1 = Greater than or equal,
271! 2 = Less than or equal,
272! 3 = Non binding.
273!
274! Constraint 1 (Objective)
275! Rhs = 0.0 and type Non binding
276!
277 DO i = 1, ne
278 Type(i) = 0
279 rhs(i) = 1.d0
280 Enddo
281 type(ne+1) = 3
282!
283! Information about the Jacobian. We have to define Rowno, Value,
284! Nlflag and Colsta.
285!
286! Colsta = Start of column indices (No Defaults):
287! Rowno = Row indices
288! Value = Value of derivative (by default only linear
289! derivatives are used)
290! Nlflag = 0 for linear and 1 for nonlinear derivative
291! (not needed for completely linear models)
292!
293! Indices
294! x(1) x(2) .. y(1) y(2) .. z(1) z(2) ..
295! 1: 1 2*Ne+1 3*Ne+1
296! 2: 3 2*Ne+3 3*Ne+3
297! ..
298! Obj: 2 4 2*Ne+2 2*Ne+4 3*Ne+2 3*Ne+4
299!
300! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
301! All nonzeros are nonlinear
302!
303 DO i = 1, n+1
304 colsta(i) = 2*i-1
305 enddo
306 k = 0
307 DO i = 1, ne ! x variablex
308 k = k + 1
309 rowno(k) = i ! Ball constraint
310 nlflag(k) = 1
311 k = k + 1
312 rowno(k) = ne+1 ! Objective
313 nlflag(k) = 1
314 enddo
315 DO i = 1, ne ! y variablex
316 k = k + 1
317 rowno(k) = i ! Ball constraint
318 nlflag(k) = 1
319 k = k + 1
320 rowno(k) = ne+1 ! Objective
321 nlflag(k) = 1
322 enddo
323 DO i = 1, ne ! z variablex
324 k = k + 1
325 rowno(k) = i ! Ball constraint
326 nlflag(k) = 1
327 k = k + 1
328 rowno(k) = ne+1 ! Objective
329 nlflag(k) = 1
330 enddo
331
332 elec_readmatrix = 0 ! Return value means OK
333
334end Function elec_readmatrix
335!
336!==========================================================================
337! Compute nonlinear terms and non-constant Jacobian elements
338!
339
340!> Compute nonlinear terms and non-constant Jacobian elements
341!!
342!! @include{doc} fdeval_params.dox
343Integer Function elec_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
344 n, nz, thread, usrmem )
345#ifdef dec_directives_win32
346!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_FDEval
347#endif
348 implicit none
349 integer, intent (in) :: n ! number of variables
350 integer, intent (in) :: rowno ! number of the row to be evaluated
351 integer, intent (in) :: nz ! number of nonzeros in this row
352 real*8, intent (in), dimension(n) :: x ! vector of current solution values
353 real*8, intent (in out) :: g ! constraint value
354 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
355 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
356 ! in this row. Ffor information only.
357 integer, intent (in) :: mode ! evaluation mode: 1 = function value
358 ! 2 = derivatives, 3 = both
359 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
360 ! as errcnt is incremented
361 integer, intent (in out) :: errcnt ! error counter to be incremented in case
362 ! of function evaluation errors.
363 integer, intent (in) :: thread
364 real*8 usrmem(*) ! optional user memory
365
366 Integer :: ne
367 Integer :: i, j
368 real*8 :: dist, dist32, tx, ty, tz
369
370 ne = n / 3
371!
372! Row NE+1: the objective function is nonlinear
373!
374 if ( rowno == ne+1 ) then
375!
376! Mode = 1 or 3. Function value:
377!
378 if ( mode .eq. 1 .or. mode .eq. 3 ) then
379 g = 0.d0
380 do i = 1, ne-1
381 do j = i+1,ne
382 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
383 g = g + 1.d0/sqrt(dist)
384 enddo
385 enddo
386 endif
387!
388! Mode = 2 or 3: Derivative values: w.r.t. x: 2*(x(i)-x(j))*(-1/2)*dist(-3/2)
389!
390 if ( mode .eq. 2 .or. mode .eq. 3 ) then
391 do i = 1, 3*ne
392 jac(i) = 0.d0
393 enddo
394 do i = 1, ne-1
395 do j = i+1,ne
396 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
397 dist32 = (1.d0/sqrt(dist))**3
398 tx = -(x(i)-x(j))*dist32
399 ty = -(x(i+ne)-x(j+ne))*dist32
400 tz = -(x(i+2*ne)-x(j+2*ne))*dist32
401 jac(i) = jac(i) + tx
402 jac(j) = jac(j) - tx
403 jac(i+ne) = jac(i+ne) + ty
404 jac(j+ne) = jac(j+ne) - ty
405 jac(i+2*ne) = jac(i+2*ne) + tz
406 jac(j+2*ne) = jac(j+2*ne) - tz
407 enddo
408 enddo
409 endif
410!
411 Else ! this is ball constraint rowno
412!
413! Mode = 1 or 3. Function value:
414!
415 i = rowno
416 if ( mode .eq. 1 .or. mode .eq. 3 ) then
417 g = x(i)**2 + x(i+ne)**2 + x(i+2*ne)**2
418 endif
419!
420! Mode = 2 or 3: Derivative values:
421!
422 if ( mode .eq. 2 .or. mode .eq. 3 ) then
423 jac(i) = 2.d0*x(i)
424 jac(i+ne) = 2.d0*x(i+ne)
425 jac(i+2*ne) = 2.d0*x(i+2*ne)
426 endif
427
428 endif
429 elec_fdeval = 0
430
431end Function elec_fdeval
432
433Integer Function elec_2dlagrsize( NODRV, NumVar, NumCon, NumHess, MaxHess, UsrMem )
434#ifdef dec_directives_win32
435!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrSize
436#endif
437 Use hesscalls
438 Implicit None
439
440 Integer, Intent (IN) :: numvar, numcon, maxhess
441 Integer, Intent (IN OUT) :: nodrv, numhess
442 real*8, Intent(IN OUT) :: usrmem(*)
443!
444! Sizing call -- the hessian is completely dense
445!
446 numhess = numvar * (numvar+1)/2
448
449End Function elec_2dlagrsize
450
451
452!> Specify the structure of the Lagrangian of the Hessian
453!!
454!! @include{doc} 2DLagrStr_params.dox
455Integer Function elec_2dlagrstr( HSRW, HSCL, NODRV, NumVar, NumCon, NumHess, UsrMem )
456#ifdef dec_directives_win32
457!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrStr
458#endif
459 Use hesscalls
460 Implicit None
461
462 Integer, Intent (IN) :: numvar, numcon, numhess
463 Integer, Intent (IN) :: nodrv
464 Integer, Dimension(*) :: hsrw, hscl
465 real*8, Intent(IN OUT) :: usrmem(*)
466
467 integer :: i, j, k
468!
469! Define structure of Hessian: A dense lower-triangular matrix
470!
471 k = 0
472 do i = 1, numvar
473 do j = i, numvar
474 k = k + 1
475 hsrw(k) = j
476 hscl(k) = i
477 enddo
478 enddo
480
481End Function elec_2dlagrstr
482
483
484!> Compute the Lagrangian of the Hessian
485!!
486!! @include{doc} 2DLagrVal_params.dox
487Integer Function elec_2dlagrval( X, U, HSRW, HSCL, HSVL, NODRV, NumVar, NumCon, NumHess, UsrMem )
488#ifdef dec_directives_win32
489!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrVal
490#endif
491 Use hesscalls
492 Implicit None
493
494 Integer, Intent (IN) :: numvar, numcon, numhess
495 Integer, Intent (IN OUT) :: nodrv
496 real*8, Dimension(NumVar), Intent (IN) :: x
497 real*8, Dimension(NumCon), Intent (IN) :: u
498 Integer, Dimension(NumHess), Intent (IN) :: hsrw, hscl
499 real*8, Dimension(NumHess), Intent (Out) :: hsvl
500 real*8, Intent(IN OUT) :: usrmem(*)
501
502 Integer :: ne ! Number of electrons
503 integer :: k, l, ix, iy, iz, jx, jy, jz
504 real*8 :: tx, ty, tz, dist, distsq, dist3, dist5
505!
506! Evaluation of 2nd derivatives. Hsvl is zero on entry:
507!
508 ne = numvar / 3
509 hcalls = hcalls + 1
510 k = 0
511! do i = 1, N
512! do j = i, N
513! k = k + 1
514! Hsvl(k) = 0.d0
515! enddo
516! enddo
517 do l = 1, ne ! constraint no l with 3 elements on the diagonal, each with value 2.0
518 k = pos(l,l) ! 1+(l-1)*(2*N+2-l)/2 ! variable x(l)
519 hsvl(k) = hsvl(k) + 2.d0*u(l)
520 k = pos(l+ne,l+ne) ! 1+(l+NE-1)*(2*N+2-(l+NE))/2 ! variable y(l)
521 hsvl(k) = hsvl(k) + 2.d0*u(l)
522 k = pos(l+2*ne,l+2*ne) ! 1+(l+2*NE-1)*(2*N+2-(l+2*NE))/2 ! variable z(l)
523 hsvl(k) = hsvl(k) + 2.d0*u(l)
524 enddo
525 if ( u(ne+1) /= 0.0d0 ) Then ! Nonzero dual on the objective
526 do ix = 1, ne-1
527 iy = ix + ne
528 iz = iy + ne
529 do jx = ix+1, ne
530 jy = jx + ne
531 jz = jy + ne
532! Objective computation:
533! dist = (x(i)-x(j))**2 + (x(i+NE)-x(j+NE))**2 + (x(i+2*NE)-x(j+2*NE))**2
534! g = g + 1.d0/sqrt(dist)
535 tx = x(ix)-x(jx) ! x1 - x2
536 ty = x(iy)-x(jy) ! y1 - y2
537 tz = x(iz)-x(jz) ! z1 - z2
538! The first derivatives of tx, ty, and tz w.r.t. the x variables are +1 or -1
539! The second derivatives of tx, ty, and tz are zero
540 dist = tx**2 + ty**2 + tz**2
541! The first derivatives of dist are
542! d(dist)/dx = d(dist)/d(tx)*d(tx)/dx = 2*tx*(+/-) (+1 for x1 and -1 for x2)
543! and similarly for d(dist)/dy = 2*ty*(+/-1) and d(dist)/dz = 2*tz*(+/-1)
544! The second derivatives of dist are
545! d2(dist)/dxidxj = 2 if xi=xj and 0 otherwise
546! d2(dist)/dyidyj = 2 if yi=yj and 0 otherwise
547! d2(dist)/dzidzj = 2 if zi=zj and 0 otherwise
548! All cross 2nd derivatives dxdy, dxdz, and dydz are zero
549!
550 distsq = 1/sqrt(dist) ! = dist**(-0.5)
551 dist3 = distsq**3
552 dist5 = distsq**5
553! The first derivatives of distsq are
554! d(distsq)/dx = -0.5*dist**(-1.5)*d(dist)/dx = -dist**(-1.5)*tx*(+/-1) = -distsq**3*tx*(+/-1)
555! d(distsq)/dy = -0.5*dist**(-1.5)*d(dist)/dy = -dist**(-1.5)*ty*(+/-1) = -distsq**3*ty*(+/-1)
556! d(distsq)/dz = -0.5*dist**(-1.5)*d(dist)/dz = -dist**(-1.5)*tz*(+/-1) = -distsq**3*tz*(+/-1)
557! The second derivatives of distsq are
558! d2(distsq)/dxidxj = -1.5*dist**(-2.5)*d(dist)/dxj*tx*(+/-1) - dist**(-1.5)*d(tx)/dxj*(+/-1)
559! = -1.5*distsq**5*2*tx*tx*(+/-1) - distsq**3*(+/-1)
560! d2(distsq)/dxidyj = -1.5*dist**(-2.5)*d(dist)/dyj*tx*(+/-1) - dist**(-1.5)*d(tx)/dyj*(+/-1)
561! = -1.5*distsq**5*2*ty*tx*(+/-1)
562 k = pos(ix,ix)
563 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx**2 - dist3
564 k = pos(jx,jx)
565 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx**2 - dist3
566 k = pos(ix,jx)
567 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx**2 + dist3
568 k = pos(iy,iy)
569 hsvl(k) = hsvl(k) + 3.0d0*dist5*ty**2 - dist3
570 k = pos(jy,jy)
571 hsvl(k) = hsvl(k) + 3.0d0*dist5*ty**2 - dist3
572 k = pos(iy,jy)
573 hsvl(k) = hsvl(k) - 3.0d0*dist5*ty**2 + dist3
574 k = pos(iz,iz)
575 hsvl(k) = hsvl(k) + 3.0d0*dist5*tz**2 - dist3
576 k = pos(jz,jz)
577 hsvl(k) = hsvl(k) + 3.0d0*dist5*tz**2 - dist3
578 k = pos(iz,jz)
579 hsvl(k) = hsvl(k) - 3.0d0*dist5*tz**2 + dist3
580!
581! Off-diagonal blocks
582!
583 k = pos(ix,iy)
584 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx*ty
585 k = pos(ix,jy)
586 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx*ty
587 k = pos(jx,iy)
588 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx*ty
589 k = pos(jx,jy)
590 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx*ty
591 k = pos(ix,iz)
592 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx*tz
593 k = pos(ix,jz)
594 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx*tz
595 k = pos(jx,iz)
596 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx*tz
597 k = pos(jx,jz)
598 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx*tz
599 k = pos(iy,iz)
600 hsvl(k) = hsvl(k) + 3.0d0*dist5*ty*tz
601 k = pos(iy,jz)
602 hsvl(k) = hsvl(k) - 3.0d0*dist5*ty*tz
603 k = pos(jy,iz)
604 hsvl(k) = hsvl(k) - 3.0d0*dist5*ty*tz
605 k = pos(jy,jz)
606 hsvl(k) = hsvl(k) + 3.0d0*dist5*ty*tz
607 enddo
608 enddo
609 endif
610
612
613 Contains
614
615 Integer function pos(i,j)
616 Integer, Intent(in) :: i, j
617 pos = 1+(i-1)*(2*numvar+2-i)/2 + (j-i) ! Hessian position for variable (j,i), j >= i
618 End function pos
619
620End Function elec_2dlagrval
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_2dlagrval(x, u, hsrw, hscl, hsvl, nodrv, numvar, numcon, numhess, usrmem)
Compute the Lagrangian of the Hessian.
Definition elec2.f90:464
integer function elec_2dlagrsize(nodrv, numvar, numcon, numhess, maxhess, usrmem)
Definition elec2.f90:416
integer function pos(i, j)
Definition elec2.f90:589
integer function elec_2dlagrstr(hsrw, hscl, nodrv, numvar, numcon, numhess, usrmem)
Specify the structure of the Lagrangian of the Hessian.
Definition elec2.f90:435
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_2dlagrstr(cntvect, coi_2dlagrstr)
define callback routine for providing the structure of the second derivatives of the Lagrangian.
Definition conopt.f90:1547
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_2dlagrval(cntvect, coi_2dlagrval)
define callback routine for computing the values of the second derivatives of the Lagrangian.
Definition conopt.f90:1573
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_debug2d(cntvect, debug2d)
turn debugging of 2nd derivatives on and off.
Definition conopt.f90:534
integer(c_int) function coidef_hessfac(cntvect, hessfac)
factor for Hessian density relative to Jacobian density HessFac.
Definition conopt.f90:858
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(c_int) function coidef_2dlagrsize(cntvect, coi_2dlagrsize)
Definition conopt.f90:1521
integer hcalls
Definition elec2.f90:68
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