1 SUBROUTINE INTEGRATE
( TIN
, TOUT
)
3 INCLUDE
'KPP_ROOT_params.h'
4 INCLUDE
'KPP_ROOT_global.h'
13 EXTERNAL FUNC_CHEM
, JAC_CHEM
15 C--------------------------------
16 INTEGER N_stepss
, N_accepteds
, N_rejecteds
, N_jacs
, ITOL
, IERR
17 SAVE N_stepss
, N_accepteds
, N_rejecteds
, N_jacs
18 C--------------------------------
20 INFO
(1) = 1 ! Autonomous
23 CALL ROS2
(NVAR
,TIN
,TOUT
,STEPMIN
,STEPMAX
,
24 + STEPMIN
,VAR
,ATOL
,RTOL
,
25 + Info
,FUNC_CHEM
,JAC_CHEM
)
27 C--------------------------------
28 N_stepss
=N_stepss
+Info
(4)+Info
(5)
29 N_accepteds
=N_accepteds
+Info
(4)
30 N_rejecteds
=N_rejecteds
+Info
(5)
32 PRINT*
,'Step=',N_stepss
,' Acc=',N_accepteds
,' Rej=',N_rejecteds
,
34 C--------------------------------
42 SUBROUTINE ROS2
(N
,T
,Tnext
,Hmin
,Hmax
,Hstart
,
44 + Info
,FUNC_CHEM
,JAC_CHEM
)
46 INCLUDE
'KPP_ROOT_params.h'
47 INCLUDE
'KPP_ROOT_sparse.h'
50 C Y = Vector of (NVAR) concentrations, contains the
51 C initial values on input
52 C [T, Tnext] = the integration interval
53 C Hmin, Hmax = lower and upper bounds for the selected step-size.
54 C Note that for Step = Hmin the current computed
55 C solution is unconditionally accepted by the error
57 C AbsTol, RelTol = (NVAR) dimensional vectors of
58 C componentwise absolute and relative tolerances.
59 C FUNC_CHEM = name of routine of derivatives. KPP syntax.
60 C See the header below.
61 C JAC_CHEM = name of routine that computes the Jacobian, in
62 C sparse format. KPP syntax. See the header below.
63 C Info(1) = 1 for autonomous system
64 C = 0 for nonautonomous system
65 C Info(2) = 1 for third order embedded formula
66 C = 0 for first order embedded formula
68 C Note: Stage 3 used to build strongly A-stable order 3 formula for error control
69 C Embed3 = (Info(2).EQ.1)
70 C IF Embed3 = .true. THEN the third order embedded formula is used
71 C .false. THEN a first order embedded formula is used
75 C Y = the values of concentrations at TEND.
76 C T = equals TEND on output.
77 C Info(2) = # of FUNC_CHEM CALLs.
78 C Info(3) = # of JAC_CHEM CALLs.
79 C Info(4) = # of accepted steps.
80 C Info(5) = # of rejected steps.
82 KPP_REAL K1
(NVAR
), K2
(NVAR
), K3
(NVAR
)
83 KPP_REAL F1
(NVAR
), JAC
(LU_NONZERO
)
85 KPP_REAL Hmin
,Hmax
,Hnew
,Hstart
,ghinv
,uround
86 KPP_REAL Y
(NVAR
), Ynew
(NVAR
)
87 KPP_REAL AbsTol
(NVAR
), RelTol
(NVAR
)
88 KPP_REAL T
, Tnext
, H
, Hold
, Tplus
89 KPP_REAL ERR
, factor
, facmax
90 KPP_REAL tau
, beta
, elo
, dround
, a21
, c21
, c31
, c32
91 KPP_REAL gamma3
, d1
, d2
, d3
, gam
92 INTEGER n
,nfcn
,njac
,Naccept
,Nreject
,i
,j
,ier
94 LOGICAL IsReject
, Autonomous
, Embed3
95 EXTERNAL FUNC_CHEM
, JAC_CHEM
97 KPP_REAL gamma
, m1
, m2
, alpha
, beta1
, beta2
, delta
, w
, e
99 c Initialization of counters, etc.
100 Autonomous
= Info
(1) .EQ
. 1
101 Embed3
= Info
(2) .EQ
. 1
103 dround
= dsqrt
(uround
)
104 H
= DMAX1
(1.d
-8, Hmin
)
113 gamma
= 1.d0
+ 1.d0
/sqrt
(2.d0
)
115 m1
= -3.d0
/(2.d0*gamma
)
116 m2
= -1.d0
/(2.d0*gamma
)
118 c31
= -1.0D0
/gamma**2*
(1.0D0
-7.0D0*gamma
+9.0D0*gamma**2
)
119 & /(-1.0D0
+2.0D0*gamma
)
120 c32
= -1.0D0
/gamma**2*
(1.0D0
-6.0D0*gamma
+6.0D0*gamma**2
)
121 & /(-1.0D0
+2.0D0*gamma
)/2
122 gamma3
= 0.5D0
- 2*gamma
123 d1
= ((-9.0D0*gamma
+8.0D0*gamma**2
+2.0D0
)/gamma**2
/
124 & (-1.0D0
+2*gamma
))/6.0D0
125 d2
= ((-1.0D0
+3.0D0*gamma
)/gamma**2
/
126 & (-1.0D0
+2.0D0*gamma
))/6.0D0
127 d3
= -1.0D0
/(3.0D0*gamma
)
129 C === Start the time loop ===
130 DO WHILE (T
.LT
. Tnext
)
135 IF ( Tplus
.gt
. Tnext
) THEN
140 CALL JAC_CHEM
( T
, Y
, JAC
)
143 ghinv
= -1.0d0
/(gamma*H
)
145 JAC
(LU_DIAG
(j
)) = JAC
(LU_DIAG
(j
)) + ghinv
147 CALL KppDecomp
(JAC
, ier
)
154 PRINT
*,'IER <> 0, H=',H
159 CALL FUNC_CHEM
( T
, Y
, F1
)
161 C ====== NONAUTONOMOUS CASE ===============
162 IF (.NOT
. Autonomous
) THEN
163 tau
= DSIGN
(DROUND*DMAX1
( 1.0d
-6, DABS
(T
) ), T
)
164 CALL FUNC_CHEM
( T
+tau
, Y
, K2
)
167 DFDT
(j
) = ( K2
(j
)-F1
(j
) )/tau
169 END IF ! .NOT
.Autonomous
171 C ----- STAGE 1 -----
176 IF (.NOT
.Autonomous
) THEN
178 K1
(j
) = K1
(j
) + delta*DFDT
(j
)
180 END IF ! .NOT
.Autonomous
181 CALL KppSolve
(JAC
, K1
)
183 C ----- STAGE 2 -----
185 Ynew
(j
) = Y
(j
) + a21*K1
(j
)
187 CALL FUNC_CHEM
( T
+H
, Ynew
, F1
)
191 K2
(j
) = F1
(j
) + beta*K1
(j
)
193 IF (.NOT
.Autonomous
) THEN
196 K2
(j
) = K2
(j
) + delta*DFDT
(j
)
198 END IF ! .NOT
.Autonomous
199 CALL KppSolve
(JAC
, K2
)
201 C ----- STAGE 3 -----
207 K3
(j
) = F1
(j
) + beta1*K1
(j
) + beta2*K2
(j
)
209 IF (.NOT
.Autonomous
) THEN
211 K3
(j
) = K3
(j
) + delta*DFDT
(j
)
213 END IF ! .NOT
.Autonomous
214 CALL KppSolve
(JAC
, K3
)
218 C ---- The Solution ---
220 Ynew
(j
) = Y
(j
) + m1*K1
(j
) + m2*K2
(j
)
224 C ====== Error estimation ========
228 w
= AbsTol
(i
) + RelTol
(i
)*DMAX1
(DABS
(Y
(i
)),DABS
(Ynew
(i
)))
230 e
= d1*K1
(i
) + d2*K2
(i
) + d3*K3
(i
)
232 e
= 1.d0
/(2.d0*gamma
)*(K1
(i
)+K2
(i
))
234 ERR
= ERR
+ ( e
/w
)**2
236 ERR
= DMAX1
( uround
, DSQRT
( ERR
/NVAR
) )
238 C ======= Choose the stepsize ===============================
241 elo
= 3.0D0
! estimator local order
245 factor
= DMAX1
(2.0D
-1,DMIN1
(6.0D0
,ERR**
(1.0D0
/elo
)/.9D0
))
246 Hnew
= DMIN1
(Hmax
,DMAX1
(Hmin
, H
/factor
))
248 C ======= Rejected/Accepted Step ============================
250 IF ( (ERR
.gt
.1).and
.(H
.gt
.Hmin
) ) THEN
259 IF (.NOT
.IsReject
) THEN
260 H
= Hnew
! Do not increase stepsize
IF previous step was rejected
266 C ======= END of the time loop ===============================
270 C ======= Output Information =================================
281 SUBROUTINE FUNC_CHEM
( T
, Y
, P
)
282 INCLUDE
'KPP_ROOT_params.h'
283 INCLUDE
'KPP_ROOT_global.h'
285 KPP_REAL Y
(NVAR
), P
(NVAR
)
290 CALL Fun
( Y
, FIX
, RCONST
, P
)
296 SUBROUTINE JAC_CHEM
( T
, Y
, J
)
297 INCLUDE
'KPP_ROOT_params.h'
298 INCLUDE
'KPP_ROOT_global.h'
300 KPP_REAL Y
(NVAR
), J
(LU_NONZERO
)
305 CALL Jac_SP
( Y
, FIX
, RCONST
, J
)