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 RODAS3_DDM
(NVAR
,NSENSIT
,TIN
,TOUT
,STEPMIN
,STEPMAX
,
24 + STEPMIN
,Y
,ATOL
,RTOL
,
25 + Info
,FUNC_CHEM
,JAC_CHEM
)
34 SUBROUTINE RODAS3_DDM
(N
,NSENSIT
,T
,Tnext
,Hmin
,Hmax
,Hstart
,
36 + Info
,FUNC_CHEM
,JAC_CHEM
)
39 INCLUDE
'KPP_ROOT_params.h'
40 INCLUDE
'KPP_ROOT_global.h'
41 INCLUDE
'KPP_ROOT_sparse.h'
43 C Stiffly accurate Rosenbrock 3(2), with
44 C stiffly accurate embedded formula for error control.
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
83 KPP_REAL y
(NVAR*
(NSENSIT
+1)), ynew
(NVAR*
(NSENSIT
+1))
84 KPP_REAL K1
(NVAR*
(NSENSIT
+1))
85 KPP_REAL K2
(NVAR*
(NSENSIT
+1))
86 KPP_REAL K3
(NVAR*
(NSENSIT
+1))
87 KPP_REAL K4
(NVAR*
(NSENSIT
+1))
88 KPP_REAL Fv
(NVAR
), Hv
(NVAR
)
89 KPP_REAL DFDT
(NVAR*
(NSENSIT
+1))
90 KPP_REAL DJDP
(NVAR*NSENSIT
)
91 KPP_REAL DFDP
(NVAR*NSENSIT
), DFDPDT
(NVAR*NSENSIT
)
92 KPP_REAL JAC
(LU_NONZERO
), AJAC
(LU_NONZERO
)
93 KPP_REAL DJDT
(LU_NONZERO
)
95 KPP_REAL Hmin
,Hmax
,Hstart
,ghinv
,uround
96 KPP_REAL AbsTol
(NVAR
), RelTol
(NVAR
)
97 KPP_REAL T
, Tnext
, Tplus
, H
, Hnew
, elo
98 KPP_REAL ERR
, factor
, facmax
99 KPP_REAL w
, e
, beta1
, beta2
, beta3
, beta4
100 KPP_REAL tau
, x1
, x2
, ytol
, dround
102 INTEGER n
,nfcn
,njac
,Naccept
,Nreject
,i
,j
,ier
104 LOGICAL IsReject
, Autonomous
105 EXTERNAL FUNC_CHEM
, JAC_CHEM
, HESS_CHEM
107 C The method coefficients
108 DOUBLE PRECISION gamma
, gamma2
, gamma3
, gamma4
109 PARAMETER ( gamma
= 0.5D
+00 )
110 PARAMETER ( gamma2
= 1.5D
+00 )
111 PARAMETER ( gamma3
= 0.0D
+00 )
112 PARAMETER ( gamma4
= 0.0D
+00 )
113 DOUBLE PRECISION a21
, a31
, a32
, a41
, a42
, a43
114 PARAMETER ( a21
= 0.0D
+00 )
115 PARAMETER ( a31
= 2.0D
+00 )
116 PARAMETER ( a32
= 0.0D
+00 )
117 PARAMETER ( a41
= 2.0D
+00 )
118 PARAMETER ( a42
= 0.0D
+00 )
119 PARAMETER ( a43
= 1.0D
+00 )
120 DOUBLE PRECISION alpha2
, alpha3
, alpha4
121 PARAMETER ( alpha2
= 0.0D0
)
122 PARAMETER ( alpha3
= 1.0D0
)
123 PARAMETER ( alpha4
= 1.0D0
)
124 DOUBLE PRECISION c21
, c31
, c32
, c41
, c42
, c43
125 PARAMETER ( c21
= 4.0D0
)
126 PARAMETER ( c31
= 1.0D0
)
127 PARAMETER ( c32
= -1.0D0
)
128 PARAMETER ( c41
= 1.0D0
)
129 PARAMETER ( c42
= -1.0D0
)
130 PARAMETER ( c43
= -2.666666666666667D0
)
131 DOUBLE PRECISION b1
, b2
, b3
, b4
132 PARAMETER ( b1
= 2.0D
+00 )
133 PARAMETER ( b2
= 0.0D0
)
134 PARAMETER ( b3
= 1.0D0
)
135 PARAMETER ( b4
= 1.0D0
)
136 DOUBLE PRECISION d1
, d2
, d3
, d4
137 PARAMETER ( d1
= 0.0D0
)
138 PARAMETER ( d2
= 0.0D0
)
139 PARAMETER ( d3
= 0.0D0
)
140 PARAMETER ( d4
= 1.0D0
)
143 c Initialization of counters, etc.
144 Autonomous
= Info
(1) .EQ
. 1
146 dround
= DSQRT
(uround
)
147 IF (Hmax
.le
.0.D0
) THEN
150 H
= DMAX1
(1.d
-8, Hstart
)
158 C === Starting the time loop ===
162 IF ( Tplus
.gt
. Tnext
) THEN
167 C Initial Function, Jacobian, and Hessian Values
168 CALL FUNC_CHEM
(NVAR
, T
, y
, Fv
)
169 CALL JAC_CHEM
(NVAR
, T
, y
, JAC
)
170 CALL HESS_CHEM
( NVAR
, T
, y
, HESS
)
171 IF (DDMTYPE
.EQ
. 1) THEN
172 CALL DFUNDPAR
(NVAR
, NSENSIT
, T
, y
, DFDP
)
175 C The time derivatives for non-Autonomous case
176 IF (.not
. Autonomous
) THEN
177 tau
= DSIGN
(dround*DMAX1
( 1.0d0
, DABS
(T
) ), T
)
178 CALL FUNC_CHEM
(NVAR
, T
+tau
, y
, K2
)
179 CALL JAC_CHEM
(NVAR
, T
+tau
, y
, AJAC
)
182 DFDT
(j
) = ( K2
(j
)-Fv
(j
) )/tau
184 DO 30 j
= 1,LU_NONZERO
185 DJDT
(j
) = ( AJAC
(j
)-JAC
(j
) )/tau
188 CALL Jac_SP_Vec
(DJDT
,y
(i*NVAR
+1),DFDT
(i*NVAR
+1))
192 11 CONTINUE ! From here we restart after a rejected step
194 C Form the Prediction matrix and compute its LU factorization
196 ghinv
= 1.0d0
/(gamma*H
)
201 AJAC
(LU_DIAG
(j
)) = AJAC
(LU_DIAG
(j
)) + ghinv
203 CALL KppDecomp
(AJAC
, ier
)
210 PRINT
*,'ROS4: Singular factorization at T=',T
,'; H=',H
215 C ------------ STAGE 1-------------------------
219 IF (.NOT
. Autonomous
) THEN
222 K1
(j
) = K1
(j
) + beta1*DFDT
(j
)
225 CALL KppSolve
(AJAC
, K1
)
226 C --- If derivative w.r.t. parameters
227 IF (DDMTYPE
.EQ
. 1) THEN
228 CALL DJACDPAR
(NVAR
, NSENSIT
, T
, y
, K1
(1), DJDP
)
230 C --- End of derivative w.r.t. parameters
233 CALL Jac_SP_Vec
(JAC
,y
(i*NVAR
+1),K1
(i*NVAR
+1))
234 CALL Hess_Vec
( HESS
, y
(i*NVAR
+1), K1
(1), Hv
)
236 K1
(i*NVAR
+j
) = K1
(i*NVAR
+j
) + Hv
(j
)
238 IF (.NOT
. Autonomous
) THEN
240 K1
(i*NVAR
+j
) = K1
(i*NVAR
+j
) + beta1*DFDT
(i*NVAR
+j
)
243 C --- If derivative w.r.t. parameters
244 IF (DDMTYPE
.EQ
. 1) THEN
246 K1
(i*NVAR
+j
) = K1
(i*NVAR
+j
) + DFDP
((i
-1)*NVAR
+j
)
247 & + DJDP
((i
-1)*NVAR
+j
)
250 C --- End of derivative w.r.t. parameters
251 CALL KppSolve
(AJAC
, K1
(i*NVAR
+1))
254 C ----------- STAGE 2 -------------------------
255 C Note: uses the same function values as Stage 1
256 C DO 110 j = 1,NVAR*(NSENSIT+1)
257 C ynew(j) = y(j) + a21*K1(j)
259 C CALL FUNC_CHEM(NVAR, T+alpha2*H, ynew, Fv)
260 C IF (DDMTYPE .EQ. 1) THEN
261 C CALL DFUNDPAR(NVAR, NSENSIT, T+alpha2*H, ynew, DFDP)
266 K2
(j
) = Fv
(j
) + beta1*K1
(j
)
268 IF (.NOT
. Autonomous
) THEN
271 K2
(j
) = K2
(j
) + beta2*DFDT
(j
)
274 CALL KppSolve
(AJAC
, K2
)
275 C --- If derivative w.r.t. parameters
276 IF (DDMTYPE
.EQ
. 1) THEN
277 CALL DJACDPAR
(NVAR
, NSENSIT
, T
, y
, K2
(1), DJDP
)
279 C --- End of derivative w.r.t. parameters
281 CALL JAC_CHEM
(NVAR
, T
+alpha2*H
, ynew
, JAC
)
284 CALL Jac_SP_Vec
(JAC
,ynew
(i*NVAR
+1),K2
(i*NVAR
+1))
285 CALL Hess_Vec
( HESS
, y
(i*NVAR
+1), K2
(1), Hv
)
287 K2
(i*NVAR
+j
) = K2
(i*NVAR
+j
) + beta1*K1
(i*NVAR
+j
)
290 IF (.NOT
. Autonomous
) THEN
292 K2
(i*NVAR
+j
) = K2
(i*NVAR
+j
) + beta2*DFDT
(i*NVAR
+j
)
295 C --- If derivative w.r.t. parameters
296 IF (DDMTYPE
.EQ
. 1) THEN
298 K2
(i*NVAR
+j
) = K2
(i*NVAR
+j
) + DFDP
((i
-1)*NVAR
+j
)
299 & + DJDP
((i
-1)*NVAR
+j
)
302 C --- End of derivative w.r.t. parameters
303 CALL KppSolve
(AJAC
, K2
(i*NVAR
+1))
307 C ------------ STAGE 3 -------------------------
308 DO 170 j
= 1,NVAR*
(NSENSIT
+1)
309 ynew
(j
) = y
(j
) + a31*K1
(j
) + a32*K2
(j
)
311 CALL FUNC_CHEM
(NVAR
, T
+alpha3*H
, ynew
, Fv
)
312 IF (DDMTYPE
.EQ
. 1) THEN
313 CALL DFUNDPAR
(NVAR
, NSENSIT
, T
+alpha3*H
, ynew
, DFDP
)
319 K3
(j
) = Fv
(j
) + beta1*K1
(j
) + beta2*K2
(j
)
321 IF (.NOT
. Autonomous
) THEN
324 K3
(j
) = K3
(j
) + beta3*DFDT
(j
)
327 CALL KppSolve
(AJAC
, K3
)
328 C --- If derivative w.r.t. parameters
329 IF (DDMTYPE
.EQ
. 1) THEN
330 CALL DJACDPAR
(NVAR
, NSENSIT
, T
, y
, K3
(1), DJDP
)
332 C --- End of derivative w.r.t. parameters
334 CALL JAC_CHEM
(NVAR
, T
+alpha3*H
, ynew
, JAC
)
337 CALL Jac_SP_Vec
(JAC
,ynew
(i*NVAR
+1),K3
(i*NVAR
+1))
338 CALL Hess_Vec
( HESS
, y
(i*NVAR
+1), K3
(1), Hv
)
340 K3
(i*NVAR
+j
) = K3
(i*NVAR
+j
) + beta1*K1
(i*NVAR
+j
)
341 & + beta2*K2
(i*NVAR
+j
) + Hv
(j
)
343 IF (.NOT
. Autonomous
) THEN
345 K3
(i*NVAR
+j
) = K3
(i*NVAR
+j
) + beta3*DFDT
(i*NVAR
+j
)
348 C --- If derivative w.r.t. parameters
349 IF (DDMTYPE
.EQ
. 1) THEN
351 K3
(i*NVAR
+j
) = K3
(i*NVAR
+j
) + DFDP
((i
-1)*NVAR
+j
)
352 & + DJDP
((i
-1)*NVAR
+j
)
355 C --- End of derivative w.r.t. parameters
356 CALL KppSolve
(AJAC
, K3
(i*NVAR
+1))
359 C ------------ STAGE 4 -------------------------
360 DO 225 j
= 1,NVAR*
(NSENSIT
+1)
361 ynew
(j
) = y
(j
) + a41*K1
(j
) + a42*K2
(j
) + a43*K3
(j
)
363 CALL FUNC_CHEM
(NVAR
, T
+alpha4*H
, ynew
, Fv
)
364 IF (DDMTYPE
.EQ
. 1) THEN
365 CALL DFUNDPAR
(NVAR
, NSENSIT
, T
+alpha4*H
, ynew
, DFDP
)
372 K4
(j
) = Fv
(j
) + beta1*K1
(j
) + beta2*K2
(j
) + beta3*K3
(j
)
374 IF (.NOT
. Autonomous
) THEN
377 K4
(j
) = K4
(j
) + beta4*DFDT
(j
)
380 CALL KppSolve
(AJAC
, K4
)
381 C --- If derivative w.r.t. parameters
382 IF (DDMTYPE
.EQ
. 1) THEN
383 CALL DJACDPAR
(NVAR
, NSENSIT
, T
, y
, K4
(1), DJDP
)
385 C --- End of derivative w.r.t. parameters
389 CALL Jac_SP_Vec
(JAC
,ynew
(i*NVAR
+1),K4
(i*NVAR
+1))
390 CALL Hess_Vec
( HESS
, y
(i*NVAR
+1), K4
(1), Hv
)
392 K4
(i*NVAR
+j
) = K4
(i*NVAR
+j
) + beta1*K1
(i*NVAR
+j
)
393 & + beta2*K2
(i*NVAR
+j
) + beta3*K3
(i*NVAR
+j
)
396 IF (.NOT
. Autonomous
) THEN
398 K4
(i*NVAR
+j
) = K4
(i*NVAR
+j
) + beta4*DFDT
(i*NVAR
+j
)
401 C --- If derivative w.r.t. parameters
402 IF (DDMTYPE
.EQ
. 1) THEN
404 K4
(i*NVAR
+j
) = K4
(i*NVAR
+j
) + DFDP
((i
-1)*NVAR
+j
)
405 & + DJDP
((i
-1)*NVAR
+j
)
408 CALL KppSolve
(AJAC
, K4
(i*NVAR
+1))
412 C ---- The Solution ---
413 DO 280 j
= 1,NVAR*
(NSENSIT
+1)
414 C ynew(j) = y(j) + b1*K1(j) + b2*K2(j) + b3*K3(j) + b4*K4(j)
415 ynew
(j
) = y
(j
) + 2*K1
(j
) + K3
(j
) + K4
(j
)
419 C ====== Error estimation -- can be extended to control sensitivities too ========
423 w
= AbsTol
(i
) + RelTol
(i
)*DMAX1
(DABS
(ynew
(i
)),DABS
(y
(i
)))
424 C e = d1*K1(i) + d2*K2(i) + d3*K3(i) + d4*K4(i)
426 ERR
= ERR
+ ( e
/w
)**2
428 ERR
= DMAX1
( uround
, DSQRT
( ERR
/NVAR
) )
430 C ======= Choose the stepsize ===============================
432 elo
= 3.0D0
! estimator local order
433 factor
= DMAX1
(2.0D
-1,DMIN1
(6.0D0
,ERR**
(1.0D0
/elo
)/.9D0
))
434 Hnew
= DMIN1
(Hmax
,DMAX1
(Hmin
, H
/factor
))
436 C ======= Rejected/Accepted Step ============================
438 IF ( (ERR
.gt
.1).and
.(H
.gt
.Hmin
) ) THEN
443 DO 300 i
=1,NVAR*
(NSENSIT
+1)
447 IF (.NOT
.IsReject
) THEN
448 H
= Hnew
! Do not increase stepsize
if previos step was rejected
454 C ======= End of the time loop ===============================
455 IF ( T
.lt
. Tnext
) GO TO 10
459 C ======= Output Information =================================
471 SUBROUTINE FUNC_CHEM
(N
, T
, Y
, P
)
472 INCLUDE
'KPP_ROOT_params.h'
473 INCLUDE
'KPP_ROOT_global.h'
476 KPP_REAL Y
(NVAR
), P
(NVAR
)
481 CALL Fun
( Y
, FIX
, RCONST
, P
)
487 SUBROUTINE DFUNDPAR
(N
, NSENSIT
, T
, Y
, P
)
488 C --- Computes the partial derivatives of FUNC_CHEM w.r.t. parameters
489 INCLUDE
'KPP_ROOT_params.h'
490 INCLUDE
'KPP_ROOT_global.h'
491 C --- NCOEFF, JCOEFF useful for derivatives w.r.t. rate coefficients
493 INTEGER NCOEFF
, JCOEFF
(NREACT
)
494 COMMON /DDMRCOEFF
/ NCOEFF
, JCOEFF
497 KPP_REAL Y
(NVAR
), P
(NVAR*NSENSIT
)
503 IF (DDMTYPE
.EQ
. 0) THEN
504 C --- Note: the values below are for sensitivities w.r.t. initial values;
505 C --- they may have to be changed for other applications
508 P
(i
+NVAR*
(j
-1)) = 0.0D0
512 C --- Example: the call below is for sensitivities w.r.t. rate coefficients;
513 C --- JCOEFF(1:NSENSIT) are the indices of the NSENSIT rate coefficients
514 C --- w.r.t. which one differentiates
515 CALL dFun_dRcoeff
( Y
, FIX
, NCOEFF
, JCOEFF
, P
)
521 SUBROUTINE JAC_CHEM
(N
, T
, Y
, J
)
522 INCLUDE
'KPP_ROOT_params.h'
523 INCLUDE
'KPP_ROOT_global.h'
526 KPP_REAL Y
(NVAR
), J
(LU_NONZERO
)
531 CALL Jac_SP
( Y
, FIX
, RCONST
, J
)
537 SUBROUTINE DJACDPAR
(N
, NSENSIT
, T
, Y
, U
, P
)
538 C --- Computes the partial derivatives of JAC w.r.t. parameters times user vector U
539 INCLUDE
'KPP_ROOT_params.h'
540 INCLUDE
'KPP_ROOT_global.h'
541 C --- NCOEFF, JCOEFF useful for derivatives w.r.t. rate coefficients
542 INTEGER NCOEFF
, JCOEFF
(NREACT
)
543 COMMON /DDMRCOEFF
/ NCOEFF
, JCOEFF
547 KPP_REAL Y
(NVAR
), U
(NVAR
)
548 KPP_REAL P
(NVAR*NSENSIT
)
554 IF (DDMTYPE
.EQ
. 0) THEN
555 C --- Note: the values below are for sensitivities w.r.t. initial values;
556 C --- they may have to be changed for other applications
559 P
(i
+NVAR*
(j
-1)) = 0.0D0
563 C --- Example: the call below is for sensitivities w.r.t. rate coefficients;
564 C --- JCOEFF(1:NSENSIT) are the indices of the NSENSIT rate coefficients
565 C --- w.r.t. which one differentiates
566 CALL dJac_dRcoeff
( Y
, FIX
, U
, NCOEFF
, JCOEFF
, P
)
573 SUBROUTINE HESS_CHEM
(N
, T
, Y
, HESS
)
574 INCLUDE
'KPP_ROOT_params.h'
575 INCLUDE
'KPP_ROOT_global.h'
578 KPP_REAL Y
(NVAR
), HESS
(NHESS
)
583 CALL Hessian
( Y
, FIX
, RCONST
, HESS
)