1 SUBROUTINE INTEGRATE
( NSENSIT
, Y
, TIN
, TOUT
)
3 INCLUDE
'KPP_ROOT_params.h'
4 INCLUDE
'KPP_ROOT_global.h'
10 C Y - Concentrations and Sensitivities
11 KPP_REAL Y
(NVAR*
(NSENSIT
+1))
12 C --- Note: Y contains: (1:NVAR) concentrations, followed by
13 C --- (1:NVAR) sensitivities w.r.t. first parameter, followed by
14 C --- etc., followed by
15 C --- (1:NVAR) sensitivities w.r.t. NSENSIT's parameter
19 EXTERNAL FUNC_CHEM
, JAC_CHEM
23 CALL ROS4_DDM
(NVAR
,NSENSIT
,TIN
,TOUT
,STEPMIN
,STEPMAX
,
24 + STEPMIN
,Y
,ATOL
,RTOL
,
25 + Info
,FUNC_CHEM
,JAC_CHEM
)
34 SUBROUTINE ROS4_DDM
(N
,NSENSIT
,T
,Tnext
,Hmin
,Hmax
,Hstart
,
36 + Info
,FUNC_CHEM
,JAC_CHEM
)
38 INCLUDE
'KPP_ROOT_params.h'
39 INCLUDE
'KPP_ROOT_global.h'
40 INCLUDE
'KPP_ROOT_sparse.h'
42 C Four Stages, Fourth Order L-stable Rosenbrock Method,
43 C with embedded L-stable, third order method for error control
44 C Simplified version of E. Hairer's atmros4; the coefficients are slightly different
46 C Direct decoupled computation of sensitivities.
47 C The global variable DDMTYPE distinguishes between:
48 C DDMTYPE = 0 : sensitivities w.r.t. initial values
49 C DDMTYPE = 1 : sensitivities w.r.t. parameters
52 C y = Vector of: (1:NVAR) concentrations, followed by
53 C (1:NVAR) sensitivities w.r.t. first parameter, followed by
55 C (1:NVAR) sensitivities w.r.t. NSENSIT's parameter
56 C (y contains initial values at input, final values at output)
57 C [T, Tnext] = the integration interval
58 C Hmin, Hmax = lower and upper bounds for the selected step-size.
59 C Note that for Step = Hmin the current computed
60 C solution is unconditionally accepted by the error
62 C AbsTol, RelTol = (NVAR) dimensional vectors of
63 C componentwise absolute and relative tolerances.
64 C FUNC_CHEM = name of routine of derivatives. KPP syntax.
65 C See the header below.
66 C JAC_CHEM = name of routine that computes the Jacobian, in
67 C sparse format. KPP syntax. See the header below.
68 C Info(1) = 1 for Autonomous system
69 C = 0 for nonAutonomous system
72 C y = the values of concentrations and sensitivities at Tend.
73 C T = equals TENDon output.
74 C Info(2) = # of FUNC_CHEM CALLs.
75 C Info(3) = # of JAC_CHEM CALLs.
76 C Info(4) = # of accepted steps.
77 C Info(5) = # of rejected steps.
79 C Adrian Sandu, December 2001
84 KPP_REAL y
(NVAR*
(NSENSIT
+1)), ynew
(NVAR*
(NSENSIT
+1))
85 KPP_REAL K1
(NVAR*
(NSENSIT
+1))
86 KPP_REAL K2
(NVAR*
(NSENSIT
+1))
87 KPP_REAL K3
(NVAR*
(NSENSIT
+1))
88 KPP_REAL K4
(NVAR*
(NSENSIT
+1))
89 KPP_REAL Fv
(NVAR
), Hv
(NVAR
)
90 KPP_REAL DFDT
(NVAR*
(NSENSIT
+1))
91 KPP_REAL DFDP
(NVAR*NSENSIT
), DFDPDT
(NVAR*NSENSIT
)
92 KPP_REAL DJDP
(NVAR*NSENSIT
)
93 KPP_REAL JAC
(LU_NONZERO
), AJAC
(LU_NONZERO
)
94 KPP_REAL DJDT
(LU_NONZERO
)
96 KPP_REAL Hmin
,Hmax
,Hstart
,ghinv
,uround
97 KPP_REAL AbsTol
(NVAR
), RelTol
(NVAR
)
98 KPP_REAL T
, Tnext
, Tplus
, H
, Hnew
, elo
99 KPP_REAL ERR
, factor
, facmax
, dround
, tau
100 KPP_REAL w
, e
, beta1
, beta2
, beta3
, beta4
102 INTEGER n
,nfcn
,njac
,Naccept
,Nreject
,i
,j
,ier
104 LOGICAL IsReject
, Autonomous
105 EXTERNAL FUNC_CHEM
, JAC_CHEM
, HESS_CHEM
108 C The method coefficients
109 C DOUBLE PRECISION gamma, gamma2, gamma3, gamma4
110 C PARAMETER ( gamma = 0.57281606D0 )
111 C PARAMETER ( gamma2 = -1.769177067112013949170520D0 )
112 C PARAMETER ( gamma3 = 0.759293964293209853670967D0 )
113 C PARAMETER ( gamma4 = -0.104894621490955803206743D0 )
114 C DOUBLE PRECISION a21, a31, a32, a41, a42, a43
115 C PARAMETER ( a21 = 2.00000000000000000000000D0 )
116 C PARAMETER ( a31 = 1.86794814949823713234476D0 )
117 C PARAMETER ( a32 = 0.23444556851723885002322D0 )
118 C DOUBLE PRECISION alpha2, alpha3, alpha4
119 C PARAMETER ( alpha2 = 1.145632120D0 )
120 C PARAMETER ( alpha3 = 0.655214975973133829477748D0 )
121 C DOUBLE PRECISION c21, c31, c32, c41, c42, c43
122 C PARAMETER ( c21 = -7.137649943349979830369260D0 )
123 C PARAMETER ( c31 = 2.580923666509657714488050D0 )
124 C PARAMETER ( c32 = 0.651629887302032023387417D0 )
125 C PARAMETER ( c41 = -2.137115266506619116806370D0 )
126 C PARAMETER ( c42 = -0.321469531339951070769241D0 )
127 C PARAMETER ( c43 = -0.694966049282445225157329D0 )
128 C DOUBLE PRECISION m1, m2, m3, m4, mhat1, mhat2, mhat3, mhat4
129 C PARAMETER ( m1 = 2.255566228604565243728840D0 )
130 C PARAMETER ( m2 = 0.287055063194157607662630D0 )
131 C PARAMETER ( m3 = 0.435311963379983213402707D0 )
132 C PARAMETER ( m4 = 1.093507656403247803214820D0 )
133 C PARAMETER ( mhat1 = 2.068399160527583734258670D0 )
134 C PARAMETER ( mhat2 = 0.238681352067532797956493D0 )
135 C PARAMETER ( mhat3 = 0.363373345435391708261747D0 )
136 C PARAMETER ( mhat4 = 0.366557127936155144309163D0 )
137 C DOUBLE PRECISION e1, e2, e3, e4
138 c PARAMETER ( e1 = 1.8716706807698191283861888D-01 )
139 c PARAMETER ( e2 = 4.8373711126624835410225955D-02 )
140 c PARAMETER ( e3 = 7.1938617944591554120847832D-02 )
141 c PARAMETER ( e4 = 7.2695052846709262706070831D-01 )
142 C PARAMETER ( e1 = -0.2815431932141155D+00 )
143 C PARAMETER ( e2 = -0.7276199124938920D-01 )
144 C PARAMETER ( e3 = -0.1082196201495311D+00 )
145 C PARAMETER ( e4 = -0.1093502252409163D+01 )
146 C The method coefficients
147 DOUBLE PRECISION gamma
, gamma2
, gamma3
, gamma4
148 PARAMETER ( gamma
= 0.5728200000000000D
+00 )
149 PARAMETER ( gamma2
= -0.1769193891319233D
+01 )
150 PARAMETER ( gamma3
= 0.7592633437920482D
+00 )
151 PARAMETER ( gamma4
= -0.1049021087100450D
+00 )
152 DOUBLE PRECISION a21
, a31
, a32
, a41
, a42
, a43
153 PARAMETER ( a21
= 0.2000000000000000D
+01 )
154 PARAMETER ( a31
= 0.1867943637803922D
+01 )
155 PARAMETER ( a32
= 0.2344449711399156D
+00 )
156 DOUBLE PRECISION alpha2
, alpha3
157 PARAMETER ( alpha2
= 0.1145640000000000D
+01 )
158 PARAMETER ( alpha3
= 0.6552168638155900D
+00 )
159 DOUBLE PRECISION c21
, c31
, c32
, c41
, c42
, c43
160 PARAMETER ( c21
= -0.7137615036412310D
+01 )
161 PARAMETER ( c31
= 0.2580708087951457D
+01 )
162 PARAMETER ( c32
= 0.6515950076447975D
+00 )
163 PARAMETER ( c41
= -0.2137148994382534D
+01 )
164 PARAMETER ( c42
= -0.3214669691237626D
+00 )
165 PARAMETER ( c43
= -0.6949742501781779D
+00 )
166 DOUBLE PRECISION b1
, b2
, b3
, b4
167 PARAMETER ( b1
= 0.2255570073418735D
+01 )
168 PARAMETER ( b2
= 0.2870493262186792D
+00 )
169 PARAMETER ( b3
= 0.4353179431840180D
+00 )
170 PARAMETER ( b4
= 0.1093502252409163D
+01 )
171 DOUBLE PRECISION d1
, d2
, d3
, d4
172 PARAMETER ( d1
= -0.2815431932141155D
+00 )
173 PARAMETER ( d2
= -0.7276199124938920D
-01 )
174 PARAMETER ( d3
= -0.1082196201495311D
+00 )
175 PARAMETER ( d4
= -0.1093502252409163D
+01 )
178 c Initialization of counters, etc.
179 Autonomous
= Info
(1) .EQ
. 1
181 dround
= DSQRT
(uround
)
182 IF (Hmax
.le
.0.D0
) THEN
185 H
= DMAX1
(1.d
-8, Hstart
)
193 C === Starting the time loop ===
197 IF ( Tplus
.gt
. Tnext
) THEN
202 C Initial Function, Jacobian, and Hessian Values
203 CALL FUNC_CHEM
(NVAR
, T
, y
, Fv
)
204 CALL JAC_CHEM
(NVAR
, T
, y
, JAC
)
205 CALL HESS_CHEM
( NVAR
, T
, y
, HESS
)
206 IF (DDMTYPE
.EQ
. 1) THEN
207 CALL DFUNDPAR
(NVAR
, NSENSIT
, T
, y
, DFDP
)
210 C The time derivatives for non-Autonomous case
211 IF (.not
. Autonomous
) THEN
212 tau
= DSIGN
(dround*DMAX1
( 1.0d0
, DABS
(T
) ), T
)
213 CALL FUNC_CHEM
(NVAR
, T
+tau
, y
, K2
)
214 CALL JAC_CHEM
(NVAR
, T
+tau
, y
, AJAC
)
217 DFDT
(j
) = ( K2
(j
)-Fv
(j
) )/tau
219 DO 30 j
= 1,LU_NONZERO
220 DJDT
(j
) = ( AJAC
(j
)-JAC
(j
) )/tau
223 CALL Jac_SP_Vec
(DJDT
,y
(i*NVAR
+1),DFDT
(i*NVAR
+1))
227 11 CONTINUE ! From here we restart after a rejected step
229 C Form the Prediction matrix and compute its LU factorization
231 ghinv
= 1.0d0
/(gamma*H
)
236 AJAC
(LU_DIAG
(j
)) = AJAC
(LU_DIAG
(j
)) + ghinv
238 CALL KppDecomp
(AJAC
, ier
)
245 PRINT
*,'ROS4: Singular factorization at T=',T
,'; H=',H
250 C ------------ STAGE 1-------------------------
254 IF (.NOT
. Autonomous
) THEN
257 K1
(j
) = K1
(j
) + beta1*DFDT
(j
)
260 CALL KppSolve
(AJAC
, K1
)
261 C --- If derivative w.r.t. parameters
262 IF (DDMTYPE
.EQ
. 1) THEN
263 CALL DJACDPAR
(NVAR
, NSENSIT
, T
, y
, K1
(1), DJDP
)
265 C --- End of derivative w.r.t. parameters
268 CALL Jac_SP_Vec
(JAC
,y
(i*NVAR
+1),K1
(i*NVAR
+1))
269 CALL Hess_Vec
( HESS
, y
(i*NVAR
+1), K1
(1), Hv
)
271 K1
(i*NVAR
+j
) = K1
(i*NVAR
+j
) + Hv
(j
)
273 IF (.NOT
. Autonomous
) THEN
275 K1
(i*NVAR
+j
) = K1
(i*NVAR
+j
) + beta1*DFDT
(i*NVAR
+j
)
278 C --- If derivative w.r.t. parameters
279 IF (DDMTYPE
.EQ
. 1) THEN
281 K1
(i*NVAR
+j
) = K1
(i*NVAR
+j
) + DFDP
((i
-1)*NVAR
+j
)
282 & + DJDP
((i
-1)*NVAR
+j
)
285 C --- End of derivative w.r.t. parameters
286 CALL KppSolve
(AJAC
, K1
(i*NVAR
+1))
289 C ----------- STAGE 2 -------------------------
290 DO 110 j
= 1,NVAR*
(NSENSIT
+1)
291 ynew
(j
) = y
(j
) + a21*K1
(j
)
293 CALL FUNC_CHEM
(NVAR
, T
+alpha2*H
, ynew
, Fv
)
294 IF (DDMTYPE
.EQ
. 1) THEN
295 CALL DFUNDPAR
(NVAR
, NSENSIT
, T
+alpha2*H
, ynew
, DFDP
)
300 K2
(j
) = Fv
(j
) + beta1*K1
(j
)
302 IF (.NOT
. Autonomous
) THEN
305 K2
(j
) = K2
(j
) + beta2*DFDT
(j
)
308 CALL KppSolve
(AJAC
, K2
)
309 C --- If derivative w.r.t. parameters
310 IF (DDMTYPE
.EQ
. 1) THEN
311 CALL DJACDPAR
(NVAR
, NSENSIT
, T
, y
, K2
(1), DJDP
)
313 C --- End of derivative w.r.t. parameters
315 CALL JAC_CHEM
(NVAR
, T
+alpha2*H
, ynew
, JAC
)
318 CALL Jac_SP_Vec
(JAC
,ynew
(i*NVAR
+1),K2
(i*NVAR
+1))
319 CALL Hess_Vec
( HESS
, y
(i*NVAR
+1), K2
(1), Hv
)
321 K2
(i*NVAR
+j
) = K2
(i*NVAR
+j
) + beta1*K1
(i*NVAR
+j
)
324 IF (.NOT
. Autonomous
) THEN
326 K2
(i*NVAR
+j
) = K2
(i*NVAR
+j
) + beta2*DFDT
(i*NVAR
+j
)
329 C --- If derivative w.r.t. parameters
330 IF (DDMTYPE
.EQ
. 1) THEN
332 K2
(i*NVAR
+j
) = K2
(i*NVAR
+j
) + DFDP
((i
-1)*NVAR
+j
)
333 & + DJDP
((i
-1)*NVAR
+j
)
336 C --- End of derivative w.r.t. parameters
337 CALL KppSolve
(AJAC
, K2
(i*NVAR
+1))
341 C ------------ STAGE 3 -------------------------
342 DO 170 j
= 1,NVAR*
(NSENSIT
+1)
343 ynew
(j
) = y
(j
) + a31*K1
(j
) + a32*K2
(j
)
345 CALL FUNC_CHEM
(NVAR
, T
+alpha3*H
, ynew
, Fv
)
346 IF (DDMTYPE
.EQ
. 1) THEN
347 CALL DFUNDPAR
(NVAR
, NSENSIT
, T
+alpha3*H
, ynew
, DFDP
)
353 K3
(j
) = Fv
(j
) + beta1*K1
(j
) + beta2*K2
(j
)
355 IF (.NOT
. Autonomous
) THEN
358 K3
(j
) = K3
(j
) + beta3*DFDT
(j
)
361 CALL KppSolve
(AJAC
, K3
)
362 C --- If derivative w.r.t. parameters
363 IF (DDMTYPE
.EQ
. 1) THEN
364 CALL DJACDPAR
(NVAR
, NSENSIT
, T
, y
, K3
(1), DJDP
)
366 C --- End of derivative w.r.t. parameters
368 CALL JAC_CHEM
(NVAR
, T
+alpha3*H
, ynew
, JAC
)
371 CALL Jac_SP_Vec
(JAC
,ynew
(i*NVAR
+1),K3
(i*NVAR
+1))
372 CALL Hess_Vec
( HESS
, y
(i*NVAR
+1), K3
(1), Hv
)
374 K3
(i*NVAR
+j
) = K3
(i*NVAR
+j
) + beta1*K1
(i*NVAR
+j
)
375 & + beta2*K2
(i*NVAR
+j
) + Hv
(j
)
377 IF (.NOT
. Autonomous
) THEN
379 K3
(i*NVAR
+j
) = K3
(i*NVAR
+j
) + beta3*DFDT
(i*NVAR
+j
)
382 C --- If derivative w.r.t. parameters
383 IF (DDMTYPE
.EQ
. 1) THEN
385 K3
(i*NVAR
+j
) = K3
(i*NVAR
+j
) + DFDP
((i
-1)*NVAR
+j
)
386 & + DJDP
((i
-1)*NVAR
+j
)
389 C --- End of derivative w.r.t. parameters
390 CALL KppSolve
(AJAC
, K3
(i*NVAR
+1))
393 C ------------ STAGE 4 -------------------------
394 C Note: uses the same function values as stage 3
399 K4
(j
) = Fv
(j
) + beta1*K1
(j
) + beta2*K2
(j
) + beta3*K3
(j
)
401 IF (.NOT
. Autonomous
) THEN
404 K4
(j
) = K4
(j
) + beta4*DFDT
(j
)
407 CALL KppSolve
(AJAC
, K4
)
408 C --- If derivative w.r.t. parameters
409 IF (DDMTYPE
.EQ
. 1) THEN
410 CALL DJACDPAR
(NVAR
, NSENSIT
, T
, y
, K4
(1), DJDP
)
412 C --- End of derivative w.r.t. parameters
416 CALL Jac_SP_Vec
(JAC
,ynew
(i*NVAR
+1),K4
(i*NVAR
+1))
417 CALL Hess_Vec
( HESS
, y
(i*NVAR
+1), K4
(1), Hv
)
419 K4
(i*NVAR
+j
) = K4
(i*NVAR
+j
) + beta1*K1
(i*NVAR
+j
)
420 & + beta2*K2
(i*NVAR
+j
) + beta3*K3
(i*NVAR
+j
)
423 IF (.NOT
. Autonomous
) THEN
425 K4
(i*NVAR
+j
) = K4
(i*NVAR
+j
) + beta4*DFDT
(i*NVAR
+j
)
428 C --- If derivative w.r.t. parameters
429 IF (DDMTYPE
.EQ
. 1) THEN
431 K4
(i*NVAR
+j
) = K4
(i*NVAR
+j
) + DFDP
((i
-1)*NVAR
+j
)
432 & + DJDP
((i
-1)*NVAR
+j
)
435 CALL KppSolve
(AJAC
, K4
(i*NVAR
+1))
439 C ---- The Solution ---
440 DO 280 j
= 1,NVAR*
(NSENSIT
+1)
441 ynew
(j
) = y
(j
) + b1*K1
(j
) + b2*K2
(j
) + b3*K3
(j
) + b4*K4
(j
)
445 C ====== Error estimation -- can be extended to control sensitivities too ========
449 w
= AbsTol
(i
) + RelTol
(i
)*DMAX1
(DABS
(ynew
(i
)),DABS
(y
(i
)))
450 e
= d1*K1
(i
) + d2*K2
(i
) + d3*K3
(i
) + d4*K4
(i
)
451 ERR
= ERR
+ ( e
/w
)**2
453 ERR
= DMAX1
( uround
, DSQRT
( ERR
/NVAR
) )
455 C ======= Choose the stepsize ===============================
457 elo
= 4.0D0
! estimator local order
458 factor
= DMAX1
(2.0D
-1,DMIN1
(6.0D0
,ERR**
(1.0D0
/elo
)/.9D0
))
459 Hnew
= DMIN1
(Hmax
,DMAX1
(Hmin
, H
/factor
))
461 C ======= Rejected/Accepted Step ============================
463 IF ( (ERR
.gt
.1).and
.(H
.gt
.Hmin
) ) THEN
468 DO 300 i
=1,NVAR*
(NSENSIT
+1)
472 IF (.NOT
.IsReject
) THEN
473 H
= Hnew
! Do not increase stepsize
if previos step was rejected
479 C ======= End of the time loop ===============================
480 IF ( T
.lt
. Tnext
) GO TO 10
484 C ======= Output Information =================================
496 SUBROUTINE FUNC_CHEM
(N
, T
, Y
, P
)
497 INCLUDE
'KPP_ROOT_params.h'
498 INCLUDE
'KPP_ROOT_global.h'
500 KPP_REAL Y
(NVAR
), P
(NVAR
)
505 CALL Fun
( Y
, FIX
, RCONST
, P
)
511 SUBROUTINE DFUNDPAR
(N
, NSENSIT
, T
, Y
, P
)
512 C --- Computes the partial derivatives of FUNC_CHEM w.r.t. parameters
513 INCLUDE
'KPP_ROOT_params.h'
514 INCLUDE
'KPP_ROOT_global.h'
515 C --- NCOEFF, JCOEFF useful for derivatives w.r.t. rate coefficients
516 INTEGER NCOEFF
, JCOEFF
(NREACT
)
517 COMMON /DDMRCOEFF
/ NCOEFF
, JCOEFF
520 KPP_REAL Y
(NVAR
), P
(NVAR*NSENSIT
)
526 IF (DDMTYPE
.EQ
. 0) THEN
527 C --- Note: the values below are for sensitivities w.r.t. initial values;
528 C --- they may have to be changed for other applications
531 P
(i
+NVAR*
(j
-1)) = 0.0D0
535 C --- Example: the call below is for sensitivities w.r.t. rate coefficients;
536 C --- JCOEFF(1:NSENSIT) are the indices of the NSENSIT rate coefficients
537 C --- w.r.t. which one differentiates
538 CALL dFun_dRcoeff
( Y
, FIX
, NCOEFF
, JCOEFF
, P
)
544 SUBROUTINE JAC_CHEM
(N
, T
, Y
, J
)
545 INCLUDE
'KPP_ROOT_params.h'
546 INCLUDE
'KPP_ROOT_global.h'
549 KPP_REAL Y
(NVAR
), J
(LU_NONZERO
)
554 CALL Jac_SP
( Y
, FIX
, RCONST
, J
)
560 SUBROUTINE DJACDPAR
(N
, NSENSIT
, T
, Y
, U
, P
)
561 C --- Computes the partial derivatives of JAC w.r.t. parameters times user vector U
562 INCLUDE
'KPP_ROOT_params.h'
563 INCLUDE
'KPP_ROOT_global.h'
564 C --- NCOEFF, JCOEFF useful for derivatives w.r.t. rate coefficients
566 INTEGER NCOEFF
, JCOEFF
(NREACT
)
567 COMMON /DDMRCOEFF
/ NCOEFF
, JCOEFF
570 KPP_REAL Y
(NVAR
), U
(NVAR
)
571 KPP_REAL P
(NVAR*NSENSIT
)
577 IF (DDMTYPE
.EQ
. 0) THEN
578 C --- Note: the values below are for sensitivities w.r.t. initial values;
579 C --- they may have to be changed for other applications
582 P
(i
+NVAR*
(j
-1)) = 0.0D0
586 C --- Example: the call below is for sensitivities w.r.t. rate coefficients;
587 C --- JCOEFF(1:NSENSIT) are the indices of the NSENSIT rate coefficients
588 C --- w.r.t. which one differentiates
589 CALL dJac_dRcoeff
( Y
, FIX
, U
, NCOEFF
, JCOEFF
, P
)
596 SUBROUTINE HESS_CHEM
(N
, T
, Y
, HESS
)
597 INCLUDE
'KPP_ROOT_params.h'
598 INCLUDE
'KPP_ROOT_global.h'
601 KPP_REAL Y
(NVAR
), HESS
(NHESS
)
606 CALL Hessian
( Y
, FIX
, RCONST
, HESS
)