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
194End Program tutorial
195
196REAL FUNCTION rndx( )
197!
198! Defines a pseudo random number between 0 and 1
199!
200 Use random
201 IMPLICIT NONE
202
203 seed = mod(seed*1027+25,1048576)
204 rndx = float(seed)/float(1048576)
205
206END FUNCTION rndx
207!
208! ============================================================================
209! Define information about the model:
210!
211
212!> Define information about the model
213!!
214!! @include{doc} readMatrix_params.dox
215Integer Function elec_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
216 colsta, rowno, value, nlflag, n, m, nz, &
217 usrmem )
218#ifdef dec_directives_win32
219!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_ReadMatrix
220#endif
221 implicit none
222 integer, intent (in) :: n ! number of variables
223 integer, intent (in) :: m ! number of constraints
224 integer, intent (in) :: nz ! number of nonzeros
225 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
226 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
227 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
228 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
229 ! (not defined here)
230 integer, intent (out), dimension(m) :: type ! vector of equation types
231 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
232 ! (not defined here)
233 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
234 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
235 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
236 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
237 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
238 real*8 usrmem(*) ! optional user memory
239
240 Integer :: ne
241 Integer :: i, k
242 real*8, parameter :: pi = 3.141592
243 real*8 :: theta, phi
244 Real, External :: rndx
245
246 ne = n / 3
247!
248! Information about Variables:
249! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
250! Default: the status information in Vsta is not used.
251!
252 DO i = 1, ne
253 theta = rndx()
254 phi = rndx()
255 curr(i ) = cos(theta)*sin(phi)
256 curr(i+ ne) = sin(theta)*sin(phi)
257 curr(i+2*ne) = cos(phi)
258 enddo
259!
260! Information about Constraints:
261! Default: Rhs = 0
262! Default: the status information in Esta and the function
263! value in FV are not used.
264! Default: Type: There is no default.
265! 0 = Equality,
266! 1 = Greater than or equal,
267! 2 = Less than or equal,
268! 3 = Non binding.
269!
270! Constraint 1 (Objective)
271! Rhs = 0.0 and type Non binding
272!
273 DO i = 1, ne
274 Type(i) = 0
275 rhs(i) = 1.d0
276 Enddo
277 type(ne+1) = 3
278!
279! Information about the Jacobian. We have to define Rowno, Value,
280! Nlflag and Colsta.
281!
282! Colsta = Start of column indices (No Defaults):
283! Rowno = Row indices
284! Value = Value of derivative (by default only linear
285! derivatives are used)
286! Nlflag = 0 for linear and 1 for nonlinear derivative
287! (not needed for completely linear models)
288!
289! Indices
290! x(1) x(2) .. y(1) y(2) .. z(1) z(2) ..
291! 1: 1 2*Ne+1 3*Ne+1
292! 2: 3 2*Ne+3 3*Ne+3
293! ..
294! Obj: 2 4 2*Ne+2 2*Ne+4 3*Ne+2 3*Ne+4
295!
296! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
297! All nonzeros are nonlinear
298!
299 DO i = 1, n+1
300 colsta(i) = 2*i-1
301 enddo
302 k = 0
303 DO i = 1, ne ! x variablex
304 k = k + 1
305 rowno(k) = i ! Ball constraint
306 nlflag(k) = 1
307 k = k + 1
308 rowno(k) = ne+1 ! Objective
309 nlflag(k) = 1
310 enddo
311 DO i = 1, ne ! y variablex
312 k = k + 1
313 rowno(k) = i ! Ball constraint
314 nlflag(k) = 1
315 k = k + 1
316 rowno(k) = ne+1 ! Objective
317 nlflag(k) = 1
318 enddo
319 DO i = 1, ne ! z variablex
320 k = k + 1
321 rowno(k) = i ! Ball constraint
322 nlflag(k) = 1
323 k = k + 1
324 rowno(k) = ne+1 ! Objective
325 nlflag(k) = 1
326 enddo
327
328 elec_readmatrix = 0 ! Return value means OK
329
330end Function elec_readmatrix
331!
332!==========================================================================
333! Compute nonlinear terms and non-constant Jacobian elements
334!
335
336!> Compute nonlinear terms and non-constant Jacobian elements
337!!
338!! @include{doc} fdeval_params.dox
339Integer Function elec_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
340 n, nz, thread, usrmem )
341#ifdef dec_directives_win32
342!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_FDEval
343#endif
344 implicit none
345 integer, intent (in) :: n ! number of variables
346 integer, intent (in) :: rowno ! number of the row to be evaluated
347 integer, intent (in) :: nz ! number of nonzeros in this row
348 real*8, intent (in), dimension(n) :: x ! vector of current solution values
349 real*8, intent (in out) :: g ! constraint value
350 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
351 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
352 ! in this row. Ffor information only.
353 integer, intent (in) :: mode ! evaluation mode: 1 = function value
354 ! 2 = derivatives, 3 = both
355 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
356 ! as errcnt is incremented
357 integer, intent (in out) :: errcnt ! error counter to be incremented in case
358 ! of function evaluation errors.
359 integer, intent (in) :: thread
360 real*8 usrmem(*) ! optional user memory
361
362 Integer :: ne
363 Integer :: i, j
364 real*8 :: dist, dist32, tx, ty, tz
365
366 ne = n / 3
367!
368! Row NE+1: the objective function is nonlinear
369!
370 if ( rowno == ne+1 ) then
371!
372! Mode = 1 or 3. Function value:
373!
374 if ( mode .eq. 1 .or. mode .eq. 3 ) then
375 g = 0.d0
376 do i = 1, ne-1
377 do j = i+1,ne
378 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
379 g = g + 1.d0/sqrt(dist)
380 enddo
381 enddo
382 endif
383!
384! Mode = 2 or 3: Derivative values: w.r.t. x: 2*(x(i)-x(j))*(-1/2)*dist(-3/2)
385!
386 if ( mode .eq. 2 .or. mode .eq. 3 ) then
387 do i = 1, 3*ne
388 jac(i) = 0.d0
389 enddo
390 do i = 1, ne-1
391 do j = i+1,ne
392 dist = (x(i)-x(j))**2 + (x(i+ne)-x(j+ne))**2 + (x(i+2*ne)-x(j+2*ne))**2
393 dist32 = (1.d0/sqrt(dist))**3
394 tx = -(x(i)-x(j))*dist32
395 ty = -(x(i+ne)-x(j+ne))*dist32
396 tz = -(x(i+2*ne)-x(j+2*ne))*dist32
397 jac(i) = jac(i) + tx
398 jac(j) = jac(j) - tx
399 jac(i+ne) = jac(i+ne) + ty
400 jac(j+ne) = jac(j+ne) - ty
401 jac(i+2*ne) = jac(i+2*ne) + tz
402 jac(j+2*ne) = jac(j+2*ne) - tz
403 enddo
404 enddo
405 endif
406!
407 Else ! this is ball constraint rowno
408!
409! Mode = 1 or 3. Function value:
410!
411 i = rowno
412 if ( mode .eq. 1 .or. mode .eq. 3 ) then
413 g = x(i)**2 + x(i+ne)**2 + x(i+2*ne)**2
414 endif
415!
416! Mode = 2 or 3: Derivative values:
417!
418 if ( mode .eq. 2 .or. mode .eq. 3 ) then
419 jac(i) = 2.d0*x(i)
420 jac(i+ne) = 2.d0*x(i+ne)
421 jac(i+2*ne) = 2.d0*x(i+2*ne)
422 endif
423
424 endif
425 elec_fdeval = 0
426
427end Function elec_fdeval
428
429Integer Function elec_2dlagrsize( NODRV, NumVar, NumCon, NumHess, MaxHess, UsrMem )
430#ifdef dec_directives_win32
431!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrSize
432#endif
433 Use hesscalls
434 Implicit None
435
436 Integer, Intent (IN) :: numvar, numcon, maxhess
437 Integer, Intent (IN OUT) :: nodrv, numhess
438 real*8, Intent(IN OUT) :: usrmem(*)
439!
440! Sizing call -- the hessian is completely dense
441!
442 numhess = numvar * (numvar+1)/2
444
445End Function elec_2dlagrsize
446
447
448!> Specify the structure of the Lagrangian of the Hessian
449!!
450!! @include{doc} 2DLagrStr_params.dox
451Integer Function elec_2dlagrstr( HSRW, HSCL, NODRV, NumVar, NumCon, NumHess, UsrMem )
452#ifdef dec_directives_win32
453!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrStr
454#endif
455 Use hesscalls
456 Implicit None
457
458 Integer, Intent (IN) :: numvar, numcon, numhess
459 Integer, Intent (IN) :: nodrv
460 Integer, Dimension(*) :: hsrw, hscl
461 real*8, Intent(IN OUT) :: usrmem(*)
462
463 integer :: i, j, k
464!
465! Define structure of Hessian: A dense lower-triangular matrix
466!
467 k = 0
468 do i = 1, numvar
469 do j = i, numvar
470 k = k + 1
471 hsrw(k) = j
472 hscl(k) = i
473 enddo
474 enddo
476
477End Function elec_2dlagrstr
478
479
480!> Compute the Lagrangian of the Hessian
481!!
482!! @include{doc} 2DLagrVal_params.dox
483Integer Function elec_2dlagrval( X, U, HSRW, HSCL, HSVL, NODRV, NumVar, NumCon, NumHess, UsrMem )
484#ifdef dec_directives_win32
485!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Elec_2DLagrVal
486#endif
487 Use hesscalls
488 Implicit None
489
490 Integer, Intent (IN) :: numvar, numcon, numhess
491 Integer, Intent (IN OUT) :: nodrv
492 real*8, Dimension(NumVar), Intent (IN) :: x
493 real*8, Dimension(NumCon), Intent (IN) :: u
494 Integer, Dimension(NumHess), Intent (IN) :: hsrw, hscl
495 real*8, Dimension(NumHess), Intent (Out) :: hsvl
496 real*8, Intent(IN OUT) :: usrmem(*)
497
498 Integer :: ne ! Number of electrons
499 integer :: k, l, ix, iy, iz, jx, jy, jz
500 real*8 :: tx, ty, tz, dist, distsq, dist3, dist5
501!
502! Evaluation of 2nd derivatives. Hsvl is zero on entry:
503!
504 ne = numvar / 3
505 hcalls = hcalls + 1
506 k = 0
507! do i = 1, N
508! do j = i, N
509! k = k + 1
510! Hsvl(k) = 0.d0
511! enddo
512! enddo
513 do l = 1, ne ! constraint no l with 3 elements on the diagonal, each with value 2.0
514 k = pos(l,l) ! 1+(l-1)*(2*N+2-l)/2 ! variable x(l)
515 hsvl(k) = hsvl(k) + 2.d0*u(l)
516 k = pos(l+ne,l+ne) ! 1+(l+NE-1)*(2*N+2-(l+NE))/2 ! variable y(l)
517 hsvl(k) = hsvl(k) + 2.d0*u(l)
518 k = pos(l+2*ne,l+2*ne) ! 1+(l+2*NE-1)*(2*N+2-(l+2*NE))/2 ! variable z(l)
519 hsvl(k) = hsvl(k) + 2.d0*u(l)
520 enddo
521 if ( u(ne+1) /= 0.0d0 ) Then ! Nonzero dual on the objective
522 do ix = 1, ne-1
523 iy = ix + ne
524 iz = iy + ne
525 do jx = ix+1, ne
526 jy = jx + ne
527 jz = jy + ne
528! Objective computation:
529! dist = (x(i)-x(j))**2 + (x(i+NE)-x(j+NE))**2 + (x(i+2*NE)-x(j+2*NE))**2
530! g = g + 1.d0/sqrt(dist)
531 tx = x(ix)-x(jx) ! x1 - x2
532 ty = x(iy)-x(jy) ! y1 - y2
533 tz = x(iz)-x(jz) ! z1 - z2
534! The first derivatives of tx, ty, and tz w.r.t. the x variables are +1 or -1
535! The second derivatives of tx, ty, and tz are zero
536 dist = tx**2 + ty**2 + tz**2
537! The first derivatives of dist are
538! d(dist)/dx = d(dist)/d(tx)*d(tx)/dx = 2*tx*(+/-) (+1 for x1 and -1 for x2)
539! and similarly for d(dist)/dy = 2*ty*(+/-1) and d(dist)/dz = 2*tz*(+/-1)
540! The second derivatives of dist are
541! d2(dist)/dxidxj = 2 if xi=xj and 0 otherwise
542! d2(dist)/dyidyj = 2 if yi=yj and 0 otherwise
543! d2(dist)/dzidzj = 2 if zi=zj and 0 otherwise
544! All cross 2nd derivatives dxdy, dxdz, and dydz are zero
545!
546 distsq = 1/sqrt(dist) ! = dist**(-0.5)
547 dist3 = distsq**3
548 dist5 = distsq**5
549! The first derivatives of distsq are
550! d(distsq)/dx = -0.5*dist**(-1.5)*d(dist)/dx = -dist**(-1.5)*tx*(+/-1) = -distsq**3*tx*(+/-1)
551! d(distsq)/dy = -0.5*dist**(-1.5)*d(dist)/dy = -dist**(-1.5)*ty*(+/-1) = -distsq**3*ty*(+/-1)
552! d(distsq)/dz = -0.5*dist**(-1.5)*d(dist)/dz = -dist**(-1.5)*tz*(+/-1) = -distsq**3*tz*(+/-1)
553! The second derivatives of distsq are
554! d2(distsq)/dxidxj = -1.5*dist**(-2.5)*d(dist)/dxj*tx*(+/-1) - dist**(-1.5)*d(tx)/dxj*(+/-1)
555! = -1.5*distsq**5*2*tx*tx*(+/-1) - distsq**3*(+/-1)
556! d2(distsq)/dxidyj = -1.5*dist**(-2.5)*d(dist)/dyj*tx*(+/-1) - dist**(-1.5)*d(tx)/dyj*(+/-1)
557! = -1.5*distsq**5*2*ty*tx*(+/-1)
558 k = pos(ix,ix)
559 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx**2 - dist3
560 k = pos(jx,jx)
561 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx**2 - dist3
562 k = pos(ix,jx)
563 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx**2 + dist3
564 k = pos(iy,iy)
565 hsvl(k) = hsvl(k) + 3.0d0*dist5*ty**2 - dist3
566 k = pos(jy,jy)
567 hsvl(k) = hsvl(k) + 3.0d0*dist5*ty**2 - dist3
568 k = pos(iy,jy)
569 hsvl(k) = hsvl(k) - 3.0d0*dist5*ty**2 + dist3
570 k = pos(iz,iz)
571 hsvl(k) = hsvl(k) + 3.0d0*dist5*tz**2 - dist3
572 k = pos(jz,jz)
573 hsvl(k) = hsvl(k) + 3.0d0*dist5*tz**2 - dist3
574 k = pos(iz,jz)
575 hsvl(k) = hsvl(k) - 3.0d0*dist5*tz**2 + dist3
576!
577! Off-diagonal blocks
578!
579 k = pos(ix,iy)
580 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx*ty
581 k = pos(ix,jy)
582 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx*ty
583 k = pos(jx,iy)
584 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx*ty
585 k = pos(jx,jy)
586 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx*ty
587 k = pos(ix,iz)
588 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx*tz
589 k = pos(ix,jz)
590 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx*tz
591 k = pos(jx,iz)
592 hsvl(k) = hsvl(k) - 3.0d0*dist5*tx*tz
593 k = pos(jx,jz)
594 hsvl(k) = hsvl(k) + 3.0d0*dist5*tx*tz
595 k = pos(iy,iz)
596 hsvl(k) = hsvl(k) + 3.0d0*dist5*ty*tz
597 k = pos(iy,jz)
598 hsvl(k) = hsvl(k) - 3.0d0*dist5*ty*tz
599 k = pos(jy,iz)
600 hsvl(k) = hsvl(k) - 3.0d0*dist5*ty*tz
601 k = pos(jy,jz)
602 hsvl(k) = hsvl(k) + 3.0d0*dist5*ty*tz
603 enddo
604 enddo
605 endif
606
608
609 Contains
610
611 Integer function pos(i,j)
612 Integer, Intent(in) :: i, j
613 pos = 1+(i-1)*(2*numvar+2-i)/2 + (j-i) ! Hessian position for variable (j,i), j >= i
614 End function pos
615
616End 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: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_2dlagrval(x, u, hsrw, hscl, hsvl, nodrv, numvar, numcon, numhess, usrmem)
Compute the Lagrangian of the Hessian.
Definition elec2.f90:460
integer function elec_2dlagrsize(nodrv, numvar, numcon, numhess, maxhess, usrmem)
Definition elec2.f90:412
integer function pos(i, j)
Definition elec2.f90:585
integer function elec_2dlagrstr(hsrw, hscl, nodrv, numvar, numcon, numhess, usrmem)
Specify the structure of the Lagrangian of the Hessian.
Definition elec2.f90:431
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_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
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