CONOPT
Loading...
Searching...
No Matches
pinadd2err.f90
Go to the documentation of this file.
1!> @file pinadd2err.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is a CONOPT implementation of the Pindyck model from the
6!! GAMS model library. The implementation is similar to the one
7!! in pindyck, but this time we first solve the model for 16
8!! periods and then we gradually increase the number of periods
9!! one at a time up to 20.
10!!
11!!
12!! For more information about the individual callbacks, please have a look at the source code.
13
15 integer :: t
16 integer, Parameter :: tmin = 16, tmax = 20
17 integer, Parameter :: vpp = 7 ! Variables per period
18 integer, Parameter :: epp = 6 ! Equations per period
19 Integer, Parameter :: hpp = 5 ! Hessian elements per period -- see in 2DLagr* routines
20 real*8, Dimension(Tmax*Vpp) :: xkeep ! Store previous solutions
21 Integer, Dimension(Tmax*Vpp) :: xstas ! Store previous status
22 Integer, Dimension(Tmax*Epp) :: estat ! for variables and equations
23End Module pinadd2errdata
24
25!> Main program. A simple setup and call of CONOPT
26!!
28 Use proginfo
29 Use coidef
31 Implicit none
32!
33! Declare the user callback routines as Integer, External:
34!
35 Integer, External :: pin_readmatrix ! Mandatory Matrix definition routine defined below
36 Integer, External :: pin_fdeval ! Function and Derivative evaluation routine
37 ! needed a nonlinear model.
38 Integer, External :: std_status ! Standard callback for displaying solution status
39 Integer, External :: pin_solution ! Specialized callback for displaying solution values
40 Integer, External :: std_message ! Standard callback for managing messages
41 Integer, External :: std_errmsg ! Standard callback for managing error messages
42 Integer, External :: pin_2dlagrstr ! Hessian structure evaluation routine
43 Integer, External :: pin_2dlagrval ! Hessian value evaluation routine
44#if defined(itl)
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_ReadMatrix
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_FDEval
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_Solution
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrStr
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrVal
53#endif
54!
55! Control vector
56!
57 INTEGER :: numcallback
58 INTEGER, Dimension(:), Pointer :: cntvect
59 INTEGER :: coi_error
60
61 call startup
62!
63! Create and initialize a Control Vector
64!
65 numcallback = coidef_size()
66 Allocate( cntvect(numcallback) )
67 coi_error = coidef_inifort( cntvect )
68!
69! Define the number of time periods, T.
70!
71 t = tmin
72!
73! Tell CONOPT about the size of the model by populating the Control Vector:
74!
75! Number of variables (excl. slacks): Vpp per period
76!
77 coi_error = max( coi_error, coidef_numvar( cntvect, vpp * t ) )
78!
79! Number of equations: 1 objective + Epp per period
80!
81 coi_error = max( coi_error, coidef_numcon( cntvect, 1 + epp * t ) )
82!
83! Number of nonzeros in the Jacobian. See the counting in ReadMatrix below:
84! For each period there is 1 in the objective, 16 for unlagged
85! variables and 4 for lagged variables.
86!
87 coi_error = max( coi_error, coidef_numnz( cntvect, 17 * t + 4 * (t-1) ) )
88!
89! Number of nonlinear nonzeros. 5 unlagged for each period.
90!
91 coi_error = max( coi_error, coidef_numnlnz( cntvect, 5 * t ) )
92!
93! Number of nonzeros in the Hessian. 5 for each period.
94!
95 coi_error = max( coi_error, coidef_numhess( cntvect, hpp * t ) )
96!
97! Direction: +1 = maximization.
98!
99 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) )
100!
101! Objective: Constraint no 1
102!
103 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) )
104!
105! Turn function debugging on in the initial point
106!
107 coi_error = max( coi_error, coidef_debugfv( cntvect, -1 ) )
108!
109! Turn Hessian debugging on in the initial point
110!
111 coi_error = max( coi_error, coidef_debug2d( cntvect, -1 ) )
112!
113! Options file = test.opt
114!
115 coi_error = max( coi_error, coidef_optfile( cntvect, 'test.opt' ) )
116!
117! Tell CONOPT about the callback routines:
118!
119 coi_error = max( coi_error, coidef_readmatrix( cntvect, pin_readmatrix ) )
120 coi_error = max( coi_error, coidef_fdeval( cntvect, pin_fdeval ) )
121 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
122 coi_error = max( coi_error, coidef_solution( cntvect, pin_solution ) )
123 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
124 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
125 coi_error = max( coi_error, coidef_2dlagrstr( cntvect, pin_2dlagrstr ) )
126 coi_error = max( coi_error, coidef_2dlagrval( cntvect, pin_2dlagrval ) )
127
128#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
129 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
130#endif
131
132 If ( coi_error .ne. 0 ) THEN
133 write(*,*)
134 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
135 write(*,*)
136 call flog( "Skipping First Solve due to setup errors", 1 )
137 ENDIF
138!
139! Start CONOPT:
140!
141 coi_error = coi_solve( cntvect )
142!
143! We have an intentional error in Pin_2DLagr and we have turned debugging on.
144! COI_Solve should still return 0, but the Solver status should return 5, Evaluation
145! Error Limit, and the Model status should be Intermediate Infeasible (6) or Intermediate
146! Nonoptimal (7).
147!
148 If ( coi_error .ne. 0 ) THEN
149 write(*,*)
150 write(*,*) '**** Fatal Error while Solving first model.'
151 write(*,*)
152 call flog( "Errors encountered during first Solve.", 1 )
153 elseif ( stacalls == 0 .or. solcalls == 0 ) then
154 call flog( "Status or Solution routine was not called during first Solve", 1 )
155 elseif ( mstat < 6 .or. mstat > 7 .or. sstat /= 5 ) then
156 call flog( "Incorrect Model or Solver Status during first solve", 1 )
157 ENDIF
158!
159! Now increase the time horizon gradually up to 20.
160! All sizes that depend on T must be registered again.
161!
162 Do t = tmin+1, tmax
163 coi_error = max( coi_error, coidef_numvar( cntvect, 7 * t ) )
164 coi_error = max( coi_error, coidef_numcon( cntvect, 1 + 6 * t ) )
165 coi_error = max( coi_error, coidef_numnz( cntvect, 17 * t + 4 * (t-1) ) )
166 coi_error = max( coi_error, coidef_numnlnz( cntvect, 5 * t ) )
167 coi_error = max( coi_error, coidef_numhess( cntvect, hpp * t ) )
168!
169! This time we have status information copied from a previous solve, i.e.
170! IniStat = 2
171!
172 coi_error = max( coi_error, coidef_inistat( cntvect, 2 ) )
173!
174! Turn Hessian debugging off again
175!
176 coi_error = max( coi_error, coidef_debug2d( cntvect, 0 ) )
177 If ( coi_error .ne. 0 ) THEN
178 write(*,*)
179 write(*,*) '**** Fatal Error while revising CONOPT Sizes.'
180 write(*,*)
181 call flog( "Skipping Later Solve due to setup errors", 1 )
182 ENDIF
183 stacalls = 0; solcalls = 0; ! reset so we can test if called.
184 coi_error = coi_solve( cntvect )
185 If ( coi_error .ne. 0 ) THEN
186 write(*,*)
187 write(*,*) '**** Fatal Error while Solving for T=',t
188 write(*,*)
189 call flog( "Errors encountered during later Solve.", 1 )
190 elseif ( stacalls == 0 .or. solcalls == 0 ) then
191 call flog( "Status or Solution routine was not called during later Solve", 1 )
192 else if ( mstat /= 2 .or. sstat /= 1 ) then
193 call flog( "Incorrect Model or Solver Status during later solve", 1 )
194 ENDIF
195 Enddo
196 write(*,*)
197 write(*,*) 'End of Pinadd2err Model. Return code=',coi_error
198
199 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
200
201 call flog( "Successful Solve", 0 )
202
203end Program pinadd2err
204!
205! =====================================================================
206! Define information about the model structure
207!
208
209!> Define information about the model
210!!
211!! @include{doc} readMatrix_params.dox
212Integer Function pin_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
213 colsta, rowno, value, nlflag, n, m, nz, usrmem )
214#if defined(itl)
215!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_ReadMatrix
216#endif
218 implicit none
219 integer, intent (in) :: n ! number of variables
220 integer, intent (in) :: m ! number of constraints
221 integer, intent (in) :: nz ! number of nonzeros
222 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
223 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
224 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
225 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
226 ! Defined during restarts
227 integer, intent (out), dimension(m) :: type ! vector of equation types
228 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
229 ! Defined during restarts
230 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
231 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
232 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
233 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
234 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
235 real*8 usrmem(*) ! optional user memory
236
237 Integer :: it, is, i, icol, iz, iold
238!
239! Define the information for the columns.
240!
241! We should not supply status information, vsta.
242!
243! We order the variables as follows:
244! td, cs, s, d, r, p, and rev. All variables for period 1 appears
245! first followed by all variables for period 2 etc.
246!
247! td, cs, s, and d have lower bounds of 0, r and p have lower
248! bounds of 1, and rev has no lower bound.
249! All have infinite upper bounds (default).
250! The initial value of td is 18, s is 7, cs is 7*t, d is td-s,
251! p is 14, and r is r(t-1)-d. No initial value for rev.
252!
253 do it = 1, t
254 is = vpp*(it-1)
255 lower(is+1) = 0.d0
256 lower(is+2) = 0.d0
257 lower(is+3) = 0.d0
258 lower(is+4) = 0.d0
259 lower(is+5) = 1.d0
260 lower(is+6) = 1.d0
261 curr(is+1) = 18.d0
262 curr(is+2) = 7.d0*it
263 curr(is+3) = 7.d0
264 curr(is+4) = curr(is+1) - curr(is+3)
265 if ( it .gt. 1 ) then
266 curr(is+5) = curr(is+5-vpp) - curr(is+4)
267 else
268 curr(is+5) = 500.d0 - curr(is+4)
269 endif
270 curr(is+6) = 14.d0
271 enddo
272 If ( t > tmin ) Then
273!
274! This is a restart: Use the initial values from last solve for
275! the variables in the first periods and extrapolate the last
276! period using a linear extrapolation between the last two
277!
278 iold = vpp*(t-1)
279 curr(1:iold) = xkeep(1:iold)
280 do i = 1, vpp
281 curr(iold+i) = curr(iold+i-vpp) + (curr(iold+i-vpp)-curr(iold+i-2*vpp))
282 enddo
283!
284! The variables from the last solve are given the old status and
285! the variables in the new period are given those in the last
286! pariod. Similarly with the Equation status:
287!
288 vsta(1:iold) = xstas(1:iold)
289 do i = 1, vpp
290 vsta(iold+i) = xstas(iold+i-vpp)
291 enddo
292 iold = 6*(t-1)+1
293 esta(1:iold) = estat(1:iold)
294 do i = 1, epp
295 esta(iold+i) = estat(iold+i-epp)
296 enddo
297 endif
298!
299! Define the information for the rows
300!
301! We order the constraints as follows: The objective is first,
302! followed by tddef, sdef, csdef, ddef, rdef, and revdef for
303! the first period, the same for the second period, etc.
304!
305! The objective is a nonbinding constraint:
306!
307 type(1) = 3
308!
309! All others are equalities:
310!
311 do i = 2, m
312 type(i) = 0
313 enddo
314!
315! Right hand sides: In all periods except the first, only tddef
316! has a nonzero right hand side of 1+2.3*1.015**(t-1).
317! In the initial period there are contributions from lagged
318! variables in the constraints that have lagged variables.
319!
320 do it = 1, t
321 is = 1 + 6*(it-1)
322 rhs(is+1) = 1.d0+2.3d0*1.015d0**(it-1)
323 enddo
324!
325! tddef: + 0.87*td(0)
326!
327 rhs(2) = rhs(2) + 0.87d0*18.d0
328!
329! sdef: +0.75*s(0)
330!
331 rhs(3) = 0.75d0*6.5d0
332!
333! csdef: +1*cs(0)
334!
335 rhs(4) = 0.d0
336!
337! rdef: +1*r(0)
338!
339 rhs(6) = 500.d0
340!
341! Define the structure and content of the Jacobian:
342! To help define the Jacobian pattern and values it can be useful to
343! make a picture of the Jacobian. We describe the variables for one
344! period and the constraints they are part of:
345!
346! td cs s d r p rev
347! Obj (1+r)**(1-t)
348! Period t:
349! tddef 1.0 0.13
350! sdef NL 1.0 NL
351! csdef 1.0 -1.0
352! ddef -1.0 1.0 1.0
353! rdef 1.0 1.0
354! revdef NL NL NL 1.0
355! Period t+1:
356! tddef -0.87
357! sdef -0.75
358! csdef -1.0
359! ddef
360! rdef -1.0
361! revdef
362!
363! The Jacobian has to be sorted column-wise so we will just define
364! the elements column by column according to the table above:
365!
366 iz = 1
367 icol = 1
368 do it = 1, t
369!
370! is points to the position before the first equation for the period
371!
372 is = 1 + 6*(it-1)
373!
374! Column td:
375!
376 colsta(icol) = iz
377 icol = icol + 1
378 rowno(iz) = is+1
379 value(iz) = +1.d0
380 nlflag(iz) = 0
381 iz = iz + 1
382 rowno(iz) = is+4
383 value(iz) = -1.d0
384 nlflag(iz) = 0
385 iz = iz + 1
386 if ( it .lt. t ) then
387 rowno(iz) = is+7
388 value(iz) = -0.87d0
389 nlflag(iz) = 0
390 iz = iz + 1
391 endif
392!
393! Column cs
394!
395 colsta(icol) = iz
396 icol = icol + 1
397 rowno(iz) = is+2
398 nlflag(iz) = 1
399 iz = iz + 1
400 rowno(iz) = is+3
401 value(iz) = +1.d0
402 nlflag(iz) = 0
403 iz = iz + 1
404 if ( it .lt. t ) then
405 rowno(iz) = is+9
406 value(iz) = -1.d0
407 nlflag(iz) = 0
408 iz = iz + 1
409 endif
410!
411! Column s
412!
413 colsta(icol) = iz
414 icol = icol + 1
415 rowno(iz) = is+2
416 value(iz) = +1.d0
417 nlflag(iz) = 0
418 iz = iz + 1
419 rowno(iz) = is+3
420 value(iz) = -1.d0
421 nlflag(iz) = 0
422 iz = iz + 1
423 rowno(iz) = is+4
424 value(iz) = +1.d0
425 nlflag(iz) = 0
426 iz = iz + 1
427 if ( it .lt. t ) then
428 rowno(iz) = is+8
429 value(iz) = -0.75d0
430 nlflag(iz) = 0
431 iz = iz + 1
432 endif
433!
434! Column d:
435!
436 colsta(icol) = iz
437 icol = icol + 1
438 rowno(iz) = is+4
439 value(iz) = +1.d0
440 nlflag(iz) = 0
441 iz = iz + 1
442 rowno(iz) = is+5
443 value(iz) = +1.d0
444 nlflag(iz) = 0
445 iz = iz + 1
446 rowno(iz) = is+6
447 nlflag(iz) = 1
448 iz = iz + 1
449!
450! Column r:
451!
452 colsta(icol) = iz
453 icol = icol + 1
454 rowno(iz) = is+5
455 value(iz) = +1.d0
456 nlflag(iz) = 0
457 iz = iz + 1
458 rowno(iz) = is+6
459 nlflag(iz) = 1
460 iz = iz + 1
461 if ( it .lt. t ) then
462 rowno(iz) = is+11
463 value(iz) = -1.d0
464 nlflag(iz) = 0
465 iz = iz + 1
466 endif
467!
468! Column p:
469!
470 colsta(icol) = iz
471 icol = icol + 1
472 rowno(iz) = is+1
473 value(iz) = +0.13d0
474 nlflag(iz) = 0
475 iz = iz + 1
476 rowno(iz) = is+2
477 nlflag(iz) = 1
478 iz = iz + 1
479 rowno(iz) = is+6
480 nlflag(iz) = 1
481 iz = iz + 1
482!
483! Column rev:
484!
485 colsta(icol) = iz
486 icol = icol + 1
487 rowno(iz) = +1
488 value(iz) = 1.05d0**(1-it)
489 nlflag(iz) = 0
490 iz = iz + 1
491 rowno(iz) = is+6
492 value(iz) = 1.d0
493 nlflag(iz) = 0
494 iz = iz + 1
495 enddo
496 colsta(icol) = iz
497
499
500end Function pin_readmatrix
501!
502! =====================================================================
503! Compute nonlinear terms and non-constant Jacobian elements
504!
505
506!> Compute nonlinear terms and non-constant Jacobian elements
507!!
508!! @include{doc} fdeval_params.dox
509Integer Function pin_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
510 n, nz, thread, usrmem )
511#if defined(itl)
512!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_FDEval
513#endif
515 implicit none
516 integer, intent (in) :: n ! number of variables
517 integer, intent (in) :: rowno ! number of the row to be evaluated
518 integer, intent (in) :: nz ! number of nonzeros in this row
519 real*8, intent (in), dimension(n) :: x ! vector of current solution values
520 real*8, intent (in out) :: g ! constraint value
521 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
522 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
523 ! in this row. Ffor information only.
524 integer, intent (in) :: mode ! evaluation mode: 1 = function value
525 ! 2 = derivatives, 3 = both
526 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
527 ! as errcnt is incremented
528 integer, intent (in out) :: errcnt ! error counter to be incremented in case
529 ! of function evaluation errors.
530 integer, intent (in) :: thread
531 real*8 usrmem(*) ! optional user memory
532
533 integer :: it, is
534 real*8 :: h1, h2
535!
536! Compute the number of the period
537!
538 it = (rowno+4) / epp
539 is = vpp*(it-1)
540 if ( rowno == (it-1)*epp+3 ) then
541!
542! sdef equation. Nonlinear term = -(1.1+0.1*p)*1.02**(-cs/7)
543!
544 h1 = (1.1d0+0.1d0*x(is+6))
545 h2 = 1.02d0**(-x(is+2)/7.d0)
546 if ( mode == 1 .or. mode == 3 ) then
547 g = -h1*h2
548 endif
549 if ( mode == 2 .or. mode == 3 ) then
550 jac(is+2) = h1*h2*log(1.02d0)/7.d0
551 jac(is+6) = -h2*0.1d0
552 endif
553 elseif ( rowno == (it-1)*epp+7 ) then
554!
555! revdef equation. Nonlinear term = -d*(p-250/r)
556!
557 if ( mode == 1 .or. mode == 3 ) then
558 g = -x(is+4)*(x(is+6)-250.d0/x(is+5))
559 endif
560 if ( mode == 2 .or. mode == 3 ) then
561 jac(is+4) = -(x(is+6)-250.d0/x(is+5))
562 jac(is+5) = -x(is+4)*250d0/x(is+5)**2
563 jac(is+6) = -x(is+4) ;
564! jac(is+6) = +x(is+4) ! Potential error
565 endif
566 else
567!
568! Error - this equation is not nonlinear
569!
570 write(*,*)
571 write(*,*) 'Error. FDEval called with rowno=',rowno
572 write(*,*)
573 pin_fdeval = 1
574 Return
575 endif
576 pin_fdeval = 0
577
578end Function pin_fdeval
579
580Integer Function pin_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
581#if defined(itl)
582!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_Solution
583#endif
584
586 Use proginfo
587 IMPLICIT NONE
588 INTEGER, Intent(IN) :: n, m
589 INTEGER, Intent(IN), Dimension(N) :: xbas, xsta
590 INTEGER, Intent(IN), Dimension(M) :: ybas, ysta
591 real*8, Intent(IN), Dimension(N) :: xval, xmar
592 real*8, Intent(IN), Dimension(M) :: yval, ymar
593 real*8, Intent(IN OUT) :: usrmem(*)
594 character*6, parameter, dimension(7) :: vname = (/'td ','cs ','s ','d ','r ','p ','rev '/)
595 character*6, parameter, dimension(6) :: ename = (/'tddef ','sdef ','csdef ','ddef ','rdef ','revdef'/)
596
597 INTEGER :: i, it, i1
598 CHARACTER*5, Parameter, Dimension(4) :: stat = (/ 'Lower','Upper','Basic','Super' /)
599
600 solcalls = solcalls + 1
601 If ( t < tmax ) Then
602 Write(*,"(A,i3)") 'Saving primal solution and status information for T =',t
603 xkeep(1:n) = xval(1:n)
604 xstas(1:n) = xbas(1:n)
605 estat(1:m) = ybas(1:m)
606 pin_solution = 0
607 Return
608 Endif
609
610 WRITE(10,"(/' Variable Solution value Reduced cost B-stat'/)")
611 i = 0
612 do it = 1, t
613 DO i1 = 1, vpp
614 i = i + 1
615 WRITE(10,"(1X,A6,i2,1p,E20.6,E16.6,4X,A5 )") vname(i1), it, xval(i), xmar(i), stat(1+xbas(i))
616 ENDDO
617 enddo
618
619 WRITE(10,"(/' Constrnt Activity level Marginal cost B-stat'/)")
620 i = 1
621 WRITE(10,"(1x,'Objective',1P,E19.6,E16.6,4X,A5 )") yval(i), ymar(i), stat(1+ybas(i))
622 do it = 1, t
623 do i1 = 1, epp
624 i = i + 1
625 WRITE(10,"(1x,A6,i2,1P,E20.6,E16.6,4X,A5 )") ename(i1),it, yval(i), ymar(i), stat(1+ybas(i))
626 enddo
627 ENDDO
628
629 pin_solution = 0
630
631END Function pin_solution
632
633
634!> Specify the structure of the Lagrangian of the Hessian
635!!
636!! @include{doc} 2DLagrStr_params.dox
637Integer Function pin_2dlagrstr( HSRW, HSCL, NODRV, N, M, NHESS, UsrMem )
638#if defined(itl)
639!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrStr
640#endif
642 Implicit None
643
644 Integer, Intent (IN) :: n, m, nhess
645 Integer, Intent (IN OUT) :: nodrv
646 Integer, Dimension(NHess), Intent (Out) :: hsrw, hscl
647 real*8, Intent(IN OUT) :: usrmem(*)
648
649 Integer :: it, is, ic
650!
651! There are two nonlinear constraints, Sdef and RevDef and they have
652! the following Hessians (for each time period):
653!
654! Revdef equation. Nonlinear term = -d*(p-250/r)
655! Hessian (diagonal and lower triangle): A total of 3 nonzero elements per period
656! 4 5 6
657! d r p
658! 4 : d
659! 5 : r -250/r**2 500*d/r**3
660! 6 : p -1
661!
662! Sdef: Nonlinear term = -(1.1+0.1p)*1.02**(-cs/7)
663! Hessian (diagonal and lower triangle): A total of 2 nonzero elements per period
664! 2 6
665! cs p
666! 2 : cs -(1.1+0.1p)*1.02**(-cs/7)*(log(1.02)/7)**2
667! 6 : p 0.1*1.02**(-cs/7)*(log(1.02)72)
668!
669! The two Hessian matrices do not overlap so the total number of nonzeros is
670! 5 per period. The structure using numerical indices for rows and columns and
671! running indices for the Hessian element is:
672!
673! 2 4 5 6
674! 2 1
675! 4
676! 5 3 5
677! 6 2 4
678!
679! and the same with names of variables and contributing equations
680! cs d r p
681! cs sdef
682! d
683! r revdef revdef
684! p sdef revdef
685!
686!
687! Define structure of Hessian
688!
689 if ( n /= vpp*t ) then
690 write(*,*) 'Struct: Number of variables should be=',vpp*t,' but CONOPT uses',n
691 write(*,*) 'Number of periods=',t
692 pin_2dlagrstr = 1; Return
693 stop
694 endif
695 if ( m /= epp*t+1 ) then
696 write(*,*) 'Struct: Number of equations should be=',epp*t+1,' but CONOPT uses',m
697 write(*,*) 'Number of periods=',t
698 pin_2dlagrstr = 1; Return
699 endif
700 Do it = 1, t
701 is = hpp*(it-1)
702 ic = vpp*(it-1)
703 hscl(is+1) = ic+2; hsrw(is+1) = ic+2
704 hscl(is+2) = ic+2; hsrw(is+2) = ic+6
705 hscl(is+3) = ic+4; hsrw(is+3) = ic+5
706 hsrw(is+3) = ic+4; ! Potential Error
707 hscl(is+4) = ic+4; hsrw(is+4) = ic+6
708 hscl(is+5) = ic+5; hsrw(is+5) = ic+5
709 Enddo
710
711 pin_2dlagrstr = 0
712
713End Function pin_2dlagrstr
714
715
716!> Compute the Lagrangian of the Hessian
717!!
718!! @include{doc} 2DLagrVal_params.dox
719Integer Function pin_2dlagrval( X, U, HSRW, HSCL, HSVL, NODRV, N, M, NHESS, UsrMem )
720#if defined(itl)
721!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrVal
722#endif
724 Implicit None
725
726 Integer, Intent (IN) :: n, m, nhess
727 Integer, Intent (IN OUT) :: nodrv
728 real*8, Dimension(N), Intent (IN) :: x
729 real*8, Dimension(M), Intent (IN) :: u
730 Integer, Dimension(Nhess), Intent (IN) :: hsrw, hscl
731 real*8, Dimension(NHess), Intent (Out) :: hsvl
732 real*8, Intent(IN OUT) :: usrmem(*)
733
734 Integer :: it, is, ic, ie
735 real*8 :: cs, d, r, p
736 real*8 :: h1, h2
737!
738! There are two nonlinear constraints, Sdef and RevDef and they have
739! the following Hessians (for each time period):
740!
741! Revdef equation. Nonlinear term = -d*(p-250/r)
742! Hessian (diagonal and lower triangle): A total of 3 nonzero elements per period
743! 4 5 6
744! d r p
745! 4 : d
746! 5 : r -250/r**2 500*d/r**3
747! 6 : p -1
748!
749! Sdef: Nonlinear term = -(1.1+0.1p)*1.02**(-cs/7)
750! Hessian (diagonal and lower triangle): A total of 2 nonzero elements per period
751! 2 6
752! cs p
753! 2 : cs -(1.1+0.1p)*1.02**(-cs/7)*(log(1.02)/7)**2
754! 6 : p 0.1*1.02**(-cs/7)*(log(1.02)72)
755!
756! The two Hessian matrices do not overlap so the total number of nonzeros is
757! 5 per period. The structure using numerical indices for rows and columns and
758! running indices for the Hessian element is:
759!
760! 2 4 5 6
761! 2 1
762! 4
763! 5 3 5
764! 6 2 4
765!
766! and the same with names of variables and contributing equations
767! cs d r p
768! cs sdef
769! d
770! r revdef revdef
771! p sdef revdef
772!
773!
774! Normal Evaluation mode
775!
776 if ( n /= vpp*t ) then
777 write(*,*) 'Eval: Number of variables should be=',vpp*t,' but CONOPT uses',n
778 write(*,*) 'Number of periods=',t
779 pin_2dlagrval = 1; REturn
780 endif
781 if ( m /= epp*t+1 ) then
782 write(*,*) 'Eval: Number of equations should be=',epp*t+1,' but CONOPT uses',m
783 write(*,*) 'Number of periods=',t
784 pin_2dlagrval = 1; Return
785 endif
786 Do it = 1, t
787 is = hpp*(it-1)
788 ic = vpp*(it-1)
789 ie = epp*(it-1) + 1
790 cs = x(ic+2)
791 d = x(ic+4)
792 r = x(ic+5)
793 p = x(ic+6)
794 h1 = 1.1d0+0.1d0*p
795 h2 = 1.02d0**(-cs/7.d0)
796 hsvl(is+1) = -h1*h2*(log(1.02)/7.d0)**2 * u(ie+2) ! Sdef = equation 2
797 hsvl(is+2) = 0.1d0*h2*(log(1.02)/7.d0) * u(ie+2)
798 hsvl(is+3) = -250.d0/r**2 * u(ie+6) ! Revdef = equation 6
799 hsvl(is+4) = -1.d0 * u(ie+6)
800 hsvl(is+5) = 500.d0*d/ r**3 * u(ie+6)
801 enddo
802
803 pin_2dlagrval = 0
804
805End Function pin_2dlagrval
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 pin_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition fvboth.f90:436
integer function pin_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition fvboth.f90:165
integer function pin_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition fvboth.f90:545
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_2dlagrstr(cntvect, coi_2dlagrstr)
define callback routine for providing the structure of the second derivatives of the Lagrangian.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_2dlagrval(cntvect, coi_2dlagrval)
define callback routine for computing the values of the second derivatives of the Lagrangian.
integer function coidef_inistat(cntvect, inistat)
handling of the initial status values.
integer function coidef_debugfv(cntvect, debugfv)
turn Debugging of FDEval on and off.
integer function coidef_debug2d(cntvect, debug2d)
turn debugging of 2nd derivatives on and off.
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_numhess(cntvect, numhess)
defines the Number of Hessian Nonzeros.
Definition coistart.f90:513
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
integer, dimension(tmax *epp) estat
integer, parameter tmax
integer, parameter vpp
integer, parameter epp
integer, parameter hpp
integer, parameter tmin
integer, dimension(tmax *vpp) xstas
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
integer function pin_2dlagrstr(hsrw, hscl, nodrv, n, m, nhess, usrmem)
Specify the structure of the Lagrangian of the Hessian.
Definition pinadd2.f90:626
integer function pin_2dlagrval(x, u, hsrw, hscl, hsvl, nodrv, n, m, nhess, usrmem)
Compute the Lagrangian of the Hessian.
Definition pinadd2.f90:707
program pinadd2err
Main program. A simple setup and call of CONOPT.