1 SUBROUTINE INTEGRATE
( TIN
, TOUT
)
3 IMPLICIT KPP_REAL
(A
-H
,O
-Z
)
4 INCLUDE
'KPP_ROOT_Parameters.h'
5 INCLUDE
'KPP_ROOT_Global.h'
13 PARAMETER (LWORK
=2*NVAR*NVAR
+12*NVAR
+7,LIWORK
=2*NVAR
+7)
14 PARAMETER (LRCONT
=5*NVAR
+2)
18 COMMON /CONT
/ ICONT
(4),RCONT
(LRCONT
)
19 EXTERNAL FUNC_CHEM
,JAC_CHEM
21 ITOL
=1 ! --- VECTOR TOLERANCES
22 IJAC
=1 ! --- COMPUTE THE JACOBIAN ANALYTICALLY
23 IMAS
=0 ! --- DIFFERENTIAL EQUATION IS IN EXPLICIT FORM
32 CALL ATMSDIRK
(NVAR
,FUNC_CHEM
,TIN
,VAR
,TOUT
,STEPMIN
,
34 & JAC_CHEM
,IJAC
, FUNC_CHEM
,IMAS
,
35 & WORK
,LWORK
,IWORK
,LIWORK
,LRCONT
,IDID
)
38 print
*,'ATMSDIRK: Unsucessfull exit at T=',
39 & TIN
,' (IDID=',IDID
,')'
46 SUBROUTINE ATMSDIRK
(N
,FCN
,X
,Y
,XEND
,H
,
48 & JAC
,IJAC
, MAS
,IMAS
,
49 & WORK
,LWORK
,IWORK
,LIWORK
,LRCONT
,IDID
)
50 C ----------------------------------------------------------
51 C *** *** *** *** *** *** *** *** *** *** *** *** ***
53 C *** *** *** *** *** *** *** *** *** *** *** *** ***
54 IMPLICIT KPP_REAL
(A
-H
,O
-Z
)
55 DIMENSION Y
(N
),AbsTol
(1),RelTol
(1),WORK
(LWORK
),IWORK
(LIWORK
)
56 LOGICAL IMPLCT
,JBAND
,ARRET
58 COMMON/STAT
/NFCN
,NJAC
,NSTEP
,NACCPT
,NREJCT
,NDEC
,NSOL
60 C *** *** *** *** *** *** ***
61 C SETTING THE PARAMETERS
62 C *** *** *** *** *** *** ***
71 C -------- SWITCH FOR TRANSFORMATION OF JACOBIAN TO HESS_CHEM FORM ---
73 C -------- NMAX , THE MAXIMAL NUMBER OF STEPS -----
79 WRITE(6,*)' WRONG INPUT IWORK(2)=',IWORK
(2)
83 C -------- NIT MAXIMAL NUMBER OF NEWTON ITERATIONS
89 WRITE(6,*)' CURIOUS INPUT IWORK(3)=',IWORK
(3)
93 C -------- METH SWITCH FOR THE COEFFICIENTS OF THE METHOD
95 C -------- UROUND SMALLEST NUMBER SATISFYING 1.D0+UROUND>1.D0
96 IF(WORK
(1).EQ
.0.D0
)THEN
100 IF(UROUND
.LE
.1.D
-19.OR
.UROUND
.GE
.1.D0
)THEN
101 WRITE(6,*)' COEFFICIENTS HAVE 20 DIGITS, UROUND=',WORK
(1)
105 C --------- SAFE SAFETY FACTOR IN STEP SIZE PREDICTION
106 IF(WORK
(2).EQ
.0.D0
)THEN
110 IF(SAFE
.LE
..001D0
.OR
.SAFE
.GE
.1.D0
)THEN
111 WRITE(6,*)' CURIOUS INPUT FOR WORK(2)=',WORK
(2)
115 C ------ THET DECIDES WHETHER THE JACOBIAN SHOULD BE RECOMPUTED;
116 IF(WORK
(3).EQ
.0.D0
)THEN
121 C --- FNEWT STOPPING CRIERION FOR NEWTON'S METHOD, USUALLY CHOSEN <1.
122 IF(WORK
(4).EQ
.0.D0
)THEN
127 C --- QUOT1 AND QUOT2: IF QUOT1 < HNEW/HOLD < QUOT2, STEP SIZE = CONST.
128 IF(WORK
(5).EQ
.0.D0
)THEN
133 IF(WORK
(6).EQ
.0.D0
)THEN
138 C -------- MAXIMAL STEP SIZE
139 IF(WORK
(7).EQ
.0.D0
)THEN
144 C --------- CHECK IF TOLERANCES ARE O.K.
146 IF (AbsTol
(1).LE
.0.D0
.OR
.RelTol
(1).LE
.10.D0*UROUND
) THEN
147 WRITE (6,*) ' TOLERANCES ARE TOO SMALL'
152 IF (AbsTol
(I
).LE
.0.D0
.OR
.RelTol
(I
).LE
.10.D0*UROUND
) THEN
153 WRITE (6,*) ' TOLERANCES(',I
,') ARE TOO SMALL'
159 C *** *** *** *** *** *** *** *** *** *** *** *** ***
160 C COMPUTATION OF ARRAY ENTRIES
161 C *** *** *** *** *** *** *** *** *** *** *** *** ***
162 C ---- IMPLICIT, BANDED OR NOT ?
165 C -------- COMPUTATION OF THE ROW-DIMENSIONS OF THE 2-ARRAYS ---
168 C -- MATRIX E FOR LINEAR ALGEBRA
178 C ------- PREPARE THE ENTRY-POINTS FOR THE ARRAYS IN WORK -----
195 C ------ TOTAL STORAGE REQUIREMENT -----------
197 IF(ISTORE
.GT
.LWORK
)THEN
198 WRITE(6,*)' INSUFFICIENT STORAGE FOR WORK, MIN. LWORK=',ISTORE
201 C ------- ENTRY POINTS FOR INTEGER WORKSPACE -----
204 C --------- TOTAL REQUIREMENT ---------------
206 IF(ISTORE
.GT
.LIWORK
)THEN
207 WRITE(6,*)' INSUFF. STORAGE FOR IWORK, MIN. LIWORK=',ISTORE
210 C --------- CONTROL OF LENGTH OF COMMON BLOCK "CONT" -------
211 IF(LRCONT
.LT
.(5*N
+2))THEN
212 WRITE(6,*)' INSUFF. STORAGE FOR RCONT, MIN. LRCONT=',5*N
+2
215 C ------ WHEN A FAIL HAS OCCURED, WE RETURN WITH IDID=-1
220 C -------- CALL TO CORE INTEGRATOR ------------
221 CALL SDICOR
(N
,FCN
,X
,Y
,XEND
,HMAX
,H
,RelTol
,AbsTol
,ITOL
,
222 & JAC
,IJAC
,MLJAC
,MUJAC
,MAS
,MLMAS
,MUMAS
,IOUT
,IDID
,
223 & NMAX
,UROUND
,SAFE
,THET
,FNEWT
,QUOT1
,QUOT2
,NIT
,METH
,NHESS1
,
224 & IMPLCT
,JBAND
,LDJAC
,LDE
,LDMAS2
,
225 & WORK
(IEYHAT
),WORK
(IEZ
),WORK
(IEY0
),WORK
(IEZ1
),WORK
(IEZ2
),
226 & WORK
(IEZ3
),WORK
(IEZ4
),WORK
(IEZ5
),WORK
(IESCAL
),WORK
(IEF1
),
227 & WORK
(IEG1
),WORK
(IEH1
),WORK
(IEJAC
),WORK
(IEE
),
228 & WORK
(IEMAS
),IWORK
(IEIP
),IWORK
(IEHES
))
229 C ----------- RETURN -----------
235 C ----- ... AND HERE IS THE CORE INTEGRATOR ----------
237 SUBROUTINE SDICOR
(N
,FCN
,X
,Y
,XEND
,HMAX
,H
,RelTol
,AbsTol
,ITOL
,
238 & JAC
,IJAC
,MLJAC
,MUJAC
,MAS
,MLMAS
,MUMAS
,IOUT
,IDID
,
239 & NMAX
,UROUND
,SAFE
,THET
,FNEWT
,QUOT1
,QUOT2
,NIT
,METH
,NHESS1
,
240 & IMPLCT
,BANDED
,LDJAC
,LE
,LDMAS
,
241 & YHAT
,Z
,Y0
,Z1
,Z2
,Z3
,Z4
,Z5
,SCAL
,F1
,G1
,H1
,FJAC
,E
,FMAS
,IP
,IPHES
)
242 C ----------------------------------------------------------
243 C CORE INTEGRATOR FOR SDIRK4
244 C PARAMETERS SAME AS IN SDIRK4 WITH WORKSPACE ADDED
245 C ----------------------------------------------------------
247 C ----------------------------------------------------------
248 IMPLICIT KPP_REAL
(A
-H
,O
-Z
)
249 INCLUDE
'KPP_ROOT_Parameters.h'
250 INCLUDE
'KPP_ROOT_Sparse.h'
251 KPP_REAL Y
(N
),YHAT
(N
),Z
(N
),Y0
(N
),Z1
(N
),Z2
(N
),Z3
(N
),Z4
(N
),Z5
(N
)
252 KPP_REAL SCAL
(N
),F1
(N
),G1
(N
),H1
(N
)
253 KPP_REAL FJAC
(LU_NONZERO
),E
(LU_NONZERO
),FMAS
(LDMAS
,N
)
254 KPP_REAL AbsTol
(1),RelTol
(1)
255 INTEGER IP
(N
),IPHES
(N
)
256 LOGICAL REJECT
,FIRST
,IMPLCT
,BANDED
,CALJAC
,NEWTRE
257 COMMON /CONT
/NN
,NN2
,NN3
,NN4
,XOLD
,HSOL
,CONT
(5*NVAR
)
258 COMMON/STAT
/NFCN
,NJAC
,NSTEP
,NACCPT
,NREJCT
,NDEC
,NSOL
259 EXTERNAL MAS
, FCN
, JAC
261 C *** *** *** *** *** *** ***
263 C *** *** *** *** *** *** ***
265 C --------- DUPLIFY N FOR COMMON BLOCK CONT -----
271 C ------- COMPUTE MASS MATRIX FOR IMPLICIT CASE ----------
272 IF(IMPLCT
) CALL MAS
(N
,FMAS
,LDMAS
)
274 C ---------- CONSTANTS ---------
277 C ---------- METHOD WITH GAMMA = 4/15 ---------------
281 C4
=2881.0D0
/28965.0D0
+GAMMA
283 ALPH31
=1577061.0D0
/922880.0D0
284 ALPH32
=-23427.0D0
/115360.0D0
285 ALPH41
=647163682356923881.0D0
/2414496535205978880.0D0
286 ALPH42
=-593512117011179.0D0
/3245291041943520.0D0
287 ALPH43
=559907973726451.0D0
/1886325418129671.0D0
288 ALPH51
=724545451.0D0
/796538880.0D0
289 ALPH52
=-830832077.0D0
/267298560.0D0
290 ALPH53
=30957577.0D0
/2509272.0D0
291 ALPH54
=-69863904375173.0D0
/6212571137048.0D0
292 E1
=7752107607.0D0
/11393456128.0D0
293 E2
=-17881415427.0D0
/11470078208.0D0
294 E3
=2433277665.0D0
/179459416.0D0
295 E4
=-96203066666797.0D0
/6212571137048.0D0
296 D11
= 24.74416644927758D0
297 D12
= -4.325375951824688D0
298 D13
= 41.39683763286316D0
299 D14
= -61.04144619901784D0
300 D15
= -3.391332232917013D0
301 D21
= -51.98245719616925D0
302 D22
= 10.52501981094525D0
303 D23
= -154.2067922191855D0
304 D24
= 214.3082125319825D0
305 D25
= 14.71166018088679D0
306 D31
= 33.14347947522142D0
307 D32
= -19.72986789558523D0
308 D33
= 230.4878502285804D0
309 D34
= -287.6629744338197D0
310 D35
= -18.99932366302254D0
311 D41
= -5.905188728329743D0
312 D42
= 13.53022403646467D0
313 D43
= -117.6778956422581D0
314 D44
= 134.3962081008550D0
315 D45
= 8.678995715052762D0
317 ANU1
= 0.9838473040915402D0
318 ANU2
= 0.3969226768377252D0
319 AMU1
= 0.6563374010466914D0
320 AMU3
= 0.3372498196189311D0
322 PRINT
*, 'WRONG CHOICE OF <METH>'
324 POSNEG
=SIGN
(1.D0
,XEND
-X
)
325 HMAX1
=MIN
(ABS
(HMAX
),ABS
(XEND
-X
))
326 IF (ABS
(H
).LE
.10.D0*UROUND
) H
=1.0D
-6
343 8 SCAL
(I
)=1.D0
/ ( AbsTol
(1)+RelTol
(1)*DABS
(Y
(I
)) )
346 9 SCAL
(I
)=1.D0
/ ( AbsTol
(I
)+RelTol
(I
)*DABS
(Y
(I
)) )
349 C --- BASIC INTEGRATION STEP
352 C *** *** *** *** *** *** ***
353 C COMPUTATION OF THE JACOBIAN
354 C *** *** *** *** *** *** ***
360 C *** *** *** *** *** *** ***
361 C COMPUTE THE MATRIX E AND ITS DECOMPOSITION
362 C *** *** *** *** *** *** ***
366 ELSE ! EXPLICIT SYSTEM
367 C --- THE MATRIX E (MAS=IDENTITY, JACOBIAN A FULL MATRIX)
370 c 525 E(I,J)=-FJAC(I,J)
371 c 526 E(J,J)=E(J,J)+FAC1
372 c CALL DEC(N,LE,E,IP,IER)
378 E
(IDG
) = E
(IDG
) + FAC1
380 CALL KppDecomp
( E
, IER
)
382 IF (IER
.NE
.0) GOTO 79
387 IF (NSTEP
.GT
.NMAX
.OR
.X
+.1D0*H
.EQ
.X
.OR
.ABS
(H
).LE
.UROUND
) GOTO 79
389 C --- LOOP FOR THE 5 STAGES
390 FACCO1
=DMAX1
(FACCO1
,UROUND
)**0.8D0
391 FACCO2
=DMAX1
(FACCO2
,UROUND
)**0.8D0
392 FACCO3
=DMAX1
(FACCO3
,UROUND
)**0.8D0
393 FACCO4
=DMAX1
(FACCO4
,UROUND
)**0.8D0
394 FACCO5
=DMAX1
(FACCO5
,UROUND
)**0.8D0
396 C *** *** *** *** *** *** ***
397 C STARTING VALUES FOR NEWTON ITERATION
398 C *** *** *** *** *** *** ***
400 IF (ISTAGE
.EQ
.1) THEN
402 IF (FIRST
.OR
.NEWTRE
) THEN
409 232 Z
(I
)=S*
(CONT
(I
+NN
)+S*
(CONT
(I
+NN2
)+S*
(CONT
(I
+NN3
)
410 & +S*CONT
(I
+NN4
))))-YHAT
(I
)
417 IF (ISTAGE
.EQ
.2) THEN
425 IF (ISTAGE
.EQ
.3) THEN
430 Z
(I
)=ANU1*Z1I
+ANU2*Z2I
431 231 G1
(I
)=ALPH31*Z1I
+ALPH32*Z2I
434 IF (ISTAGE
.EQ
.4) THEN
439 Z
(I
)=AMU1*Z1I
+AMU3*Z3I
440 331 G1
(I
)=ALPH41*Z1I
+ALPH42*Z2
(I
)+ALPH43*Z3I
443 IF (ISTAGE
.EQ
.5) THEN
450 Z
(I
)=E1*Z1I
+E2*Z2I
+E3*Z3I
+E4*Z4I
452 431 G1
(I
)=ALPH51*Z1I
+ALPH52*Z2I
+ALPH53*Z3I
+ALPH54*Z4I
458 C *** *** *** *** *** *** *** *** *** *** ***
459 C LOOP FOR THE SIMPLIFIED NEWTON ITERATION
460 C *** *** *** *** *** *** *** *** *** *** ***
463 IF (REJECT
) THETA
=2*ABS
(THET
)
465 IF (NEWT
.GE
.NIT
) THEN
473 C --- COMPUTE THE RIGHT-HAND SIDE
477 CALL FCN
(N
,XCH
,CONT
,F1
)
480 C --- KppSolve THE LINEAR SYSTEMS
485 345 F1
(I
)=H1
(I
)*FAC1
+F1
(I
)
486 C CALL SOL(N,LE,E,F1,IP)
493 57 DYNO
=DYNO
+(F1
(I
)*SCAL
(I
))**2
497 C 57 DYNO=DMAX1( DYNO, DABS(F1(I)*SCAL(I)) )
500 C --- BAD CONVERGENCE OR NUMBER OF ITERATIONS TO LARGE
501 IF (NEWT
.GE
.2.AND
.NEWT
.LT
.NIT
) THEN
503 IF (THETA
.LT
.0.99D0
) THEN
504 FACCON
=THETA
/(1.0D0
-THETA
)
505 DYTH
=FACCON*DYNO*THETA**
(NIT
-1-NEWT
)
506 QNEWT
=DMAX1
(1.0D
-4,DMIN1
(16.0D0
,DYTH
/FNEWT
))
507 IF (QNEWT
.GE
.1.0D0
) THEN
508 H
=.8D0*H*QNEWT**
(-1.0D0
/(NIT
-NEWT
))
523 IF (FACCON*DYNO
.GT
.FNEWT
) GOTO 40
525 C --- END OF SIMPILFIED NEWTON
526 IF (ISTAGE
.EQ
.1) THEN
532 IF (ISTAGE
.EQ
.2) THEN
538 IF (ISTAGE
.EQ
.3) THEN
544 IF (ISTAGE
.EQ
.4) THEN
550 IF (ISTAGE
.EQ
.5) THEN
559 C *** *** *** *** *** *** ***
561 C *** *** *** *** *** *** ***
567 461 CONT
(I
)=FAC1*
(Z5
(I
)-YHAT
(I
))
570 CALL KppSolve
(E
, CONT
)
575 64 ERR
=ERR
+(CONT
(I
)*SCAL
(I
))**2
576 ERR
=DMAX1
(DSQRT
(ERR
/N
),1.D
-10)
580 c 64 ERR=DMAX1( ERR, DABS( CONT(I)*SCAL(I) ) )
582 C --- COMPUTATION OF HNEW
583 C --- WE REQUIRE .25<=HNEW/H<=10.
584 FAC
=DMIN1
(SAFE
,CFAC
/(NEWT
+2*NIT
))
585 QUOT
=DMAX1
(.25D0
,DMIN1
(10.D0
,(ERR
)**.25D0
/FAC
))
588 C *** *** *** *** *** *** ***
589 C IS THE ERROR SMALL ENOUGH ?
590 C *** *** *** *** *** *** ***
591 IF (ERR
.LT
.1.D0
) THEN
592 C --- STEP IS ACCEPTED
597 C --- COEFFICIENTS FOR CONTINUOUS SOLUTION
606 CONT
(I
+NN
) =D11*Z1I
+D12*Z2I
+D13*Z3I
+D14*Z4I
+D15*Z5I
607 CONT
(I
+NN2
)=D21*Z1I
+D22*Z2I
+D23*Z3I
+D24*Z4I
+D25*Z5I
608 CONT
(I
+NN3
)=D31*Z1I
+D32*Z2I
+D33*Z3I
+D34*Z4I
+D35*Z5I
609 CONT
(I
+NN4
)=D41*Z1I
+D42*Z2I
+D43*Z3I
+D44*Z4I
+D45*Z5I
612 SCAL
(I
)=1.D0
/( AbsTol
(1)+RelTol
(1)*DABS
(Y
(I
)) )
614 SCAL
(I
)=1.D0
/( AbsTol
(I
)+RelTol
(I
)*DABS
(Y
(I
)) )
619 IF ((X
-XEND
)*POSNEG
+UROUND
.GT
.0.D0
) THEN
624 IF (IJAC
.EQ
.0) CALL FCN
(N
,X
,Y
,Y0
)
626 HNEW
=POSNEG*DMIN1
(DABS
(HNEW
),HMAX1
)
628 IF (REJECT
) HNEW
=POSNEG*DMIN1
(DABS
(HNEW
),DABS
(H
))
631 IF ((X
+HNEW
/QUOT1
-XEND
)*POSNEG
.GT
.0.D0
) THEN
635 IF (THETA
.LE
.THET
.AND
.QT
.GE
.QUOT1
.AND
.QT
.LE
.QUOT2
) GOTO 30
638 IF (THETA
.LE
.THET
) GOTO 20
642 C --- STEP IS REJECTED
649 IF (NACCPT
.GE
.1) NREJCT
=NREJCT
+1
654 C --- UNEXPECTED STEP-REJECTION
657 WRITE (6,*) ' MATRIX IS SINGULAR, IER=',IER
,' X=',X
,' H=',H
659 IF (NSING
.GE
.6) GOTO 79
667 79 WRITE (6,979) X
,H
,IER
668 979 FORMAT(' EXIT OF SDIRK4 AT X=',D14
.7
,' H=',D14
.7
,' IER=',I4
)
675 SUBROUTINE FUNC_CHEM
(N
, T
, Y
, P
)
676 INCLUDE
'KPP_ROOT_Parameters.h'
677 INCLUDE
'KPP_ROOT_Global.h'
680 KPP_REAL Y
(NVAR
), P
(NVAR
)
685 CALL Fun
( Y
, FIX
, RCONST
, P
)
691 SUBROUTINE JAC_CHEM
(N
, T
, Y
, J
)
692 INCLUDE
'KPP_ROOT_Parameters.h'
693 INCLUDE
'KPP_ROOT_Global.h'
696 KPP_REAL Y
(NVAR
), J
(LU_NONZERO
)
701 CALL Jac_SP
( Y
, FIX
, RCONST
, J
)