1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
C --------------------------------------------------------------------
C Programmer(s): Jimmy Almgren-Bell @ LLNL
C Based on prior version by: Scott D. Cohen, Alan C. Hindmarsh and
C Radu Serban @ LLNL
C --------------------------------------------------------------------
C SUNDIALS Copyright Start
C Copyright (c) 2002-2022, Lawrence Livermore National Security
C and Southern Methodist University.
C All rights reserved.
C
C See the top-level LICENSE and NOTICE files for details.
C
C SPDX-License-Identifier: BSD-3-Clause
C SUNDIALS Copyright End
C --------------------------------------------------------------------
C FCVODE Example Problem: Robertson kinetics, direct linear solver
C with dense user Jacobian.
C
C The following is a simple example problem, with the coding
C needed for its solution by CVODE. The problem is from chemical
C kinetics, and consists of the following three rate equations:
C
C dy1/dt = -.04*y1 + 1.e4*y2*y3
C dy2/dt = .04*y1 - 1.e4*y2*y3 - 3.e7*y2**2
C dy3/dt = 3.e7*y2**2
C
C on the interval from t = 0.0 to t = 4.e10, with initial
C conditions:
C
C y1 = 1.0, y2 = y3 = 0.
C
C The problem is stiff. While integrating the system, we also
C enable the root finding feature to find the points at which
C y1 = 1.e-4 or at which y3 = 0.01. The following coding solves
C this problem with CVODE, using the Fortran/C interface routine
C package. This solution uses the BDF method and a user-supplied
C Jacobian routine, and prints results at t = .4, 4., ..., 4.e10.
C It uses ITOL = 2 and ATOL much smaller for y2 than y1 or y3
C because y2 has much smaller values. The constraint y_i >= 0
C is posed for all components. At the end of the run,
C various counters of interest are printed.
C --------------------------------------------------------------------
C
IMPLICIT NONE
C
INTEGER*4 IER, LNST, LNFE, LNSETUP, LNNI, LNCF, LNETF, LNJE, LNGE
INTEGER*4 METH, ITOL, ITASK, JOUT, NOUT, IERROOT
INTEGER*4 INFO(2)
INTEGER*4 I
DOUBLE PRECISION CONSTR(3)
C The following declaration specification should match C type long int
INTEGER*8 NEQ, IPAR, IOUT(25)
DOUBLE PRECISION RTOL, T, T0, TOUT
DOUBLE PRECISION Y(3), ATOL(3), ROUT(10), RPAR
C
DATA LNST/3/, LNFE/4/, LNETF/5/, LNCF/6/, LNNI/7/, LNSETUP/8/,
1 LNGE/12/, LNJE/17/
C
NEQ = 3
T0 = 0.0D0
Y(1) = 1.0D0
Y(2) = 0.0D0
Y(3) = 0.0D0
METH = 2
ITOL = 2
RTOL = 1.0D-4
ATOL(1) = 1.0D-6
ATOL(2) = 1.0D-11
ATOL(3) = 1.0D-5
TOUT = 0.4D0
ITASK = 1
JOUT = 0
NOUT = 12
CONSTR(1) = 1.0D0
CONSTR(2) = 1.0D0
CONSTR(3) = 1.0D0
C
WRITE(6,10) NEQ
10 FORMAT('Dense example problem:'//
1 ' Robertson kinetics, NEQ = ', I2//)
C create serial vector
CALL FNVINITS(1, NEQ, IER)
IF (IER .NE. 0) THEN
WRITE(6,20) IER
20 FORMAT(///' SUNDIALS_ERROR: FNVINITS returned IER = ', I5)
STOP
ENDIF
C initialize dense matrix module
CALL FSUNDENSEMATINIT(1, NEQ, NEQ, IER)
IF (IER .NE. 0) THEN
WRITE(6,25) IER
25 FORMAT(///' SUNDIALS_ERROR: FSUNDENSEMATINIT returned IER = ',
1 I5)
STOP
ENDIF
C initialize dense linear solver module
CALL FSUNDENSELINSOLINIT(1, IER)
IF (IER .NE. 0) THEN
WRITE(6,28) IER
28 FORMAT(///' SUNDIALS_ERROR: FSUNDENSELINSOLINIT returned IER = '
1 , I5)
STOP
ENDIF
C Call FCVMALLOC to create the solver memory and specify the
C Backward Differentiation Formula
CALL FCVMALLOC(T0, Y, METH, ITOL, RTOL, ATOL,
1 IOUT, ROUT, IPAR, RPAR, IER)
IF (IER .NE. 0) THEN
WRITE(6,30) IER
30 FORMAT(///' SUNDIALS_ERROR: FCVMALLOC returned IER = ', I5)
STOP
ENDIF
C Call FCVROOTINIT to specify the root function g with 2 components
CALL FCVROOTINIT(2, IER)
IF (IER .NE. 0) THEN
WRITE(6,35) IER
35 FORMAT(///' SUNDIALS_ERROR: FCVROOTINIT returned IER = ', I5)
CALL FCVFREE
STOP
ENDIF
C attach the matrix and linear solver modules to CVLs interface
CALL FCVLSINIT(IER)
IF (IER .NE. 0) THEN
WRITE(6,40) IER
40 FORMAT(///' SUNDIALS_ERROR: FCVLSINIT returned IER = ', I5)
CALL FCVFREE
STOP
ENDIF
C indicate a dense Jacobian function is provided
CALL FCVDENSESETJAC(1, IER)
IF (IER .NE. 0) THEN
WRITE(6,45) IER
45 FORMAT(///' SUNDIALS_ERROR: FCVDENSESETJAC returned IER = ', I5)
CALL FCVFREE
STOP
ENDIF
C initialize constraints
CALL FCVSETVIN("CONSTR_VEC", CONSTR, IER)
IF (IER .NE. 0) THEN
WRITE(6,50) IER
50 FORMAT(///' SUNDIALS_ERROR: FCVSETVIN return IER = ', I5)
CALL FCVFREE
STOP
ENDIF
DO WHILE(JOUT .LT. NOUT)
C
CALL FCVODE(TOUT, T, Y, ITASK, IER)
C
WRITE(6,55) T, Y(1), Y(2), Y(3)
55 FORMAT('At t = ', E12.4, ' y = ', 3E14.6)
C
IF (IER .LT. 0) THEN
WRITE(6,65) IER, IOUT(15)
65 FORMAT(///' SUNDIALS_ERROR: FCVODE returned IER = ', I5, /,
1 ' Linear Solver returned IER = ', I5)
CALL FCVROOTFREE
CALL FCVFREE
STOP
ENDIF
C
IF (IER .EQ. 2) THEN
CALL FCVROOTINFO(2, INFO, IERROOT)
IF (IERROOT .LT. 0) THEN
WRITE(6,70) IERROOT
70 FORMAT(///' SUNDIALS_ERROR: FCVROOTINFO returned IER = ',
1 I5)
CALL FCVROOTFREE
CALL FCVFREE
STOP
ENDIF
WRITE(6,75) (INFO(I), I = 1, 2)
75 FORMAT(5X, 'Above is a root, INFO() = ', 2I3)
ENDIF
C
IF (IER .EQ. 0) THEN
TOUT = TOUT * 10.0D0
JOUT = JOUT + 1
ENDIF
C
ENDDO
C
CALL FCVDKY(T, 1, Y, IER)
IF (IER .NE. 0) THEN
WRITE(6,85) IER
85 FORMAT(///' SUNDIALS_ERROR: FCVDKY returned IER = ', I4)
CALL FCVROOTFREE
CALL FCVFREE
STOP
ENDIF
WRITE(6,90) Y(1), Y(2), Y(3)
90 FORMAT(/'Final value of ydot = ', 3E14.6)
C
WRITE(6,95) IOUT(LNST), IOUT(LNFE), IOUT(LNJE), IOUT(LNSETUP),
1 IOUT(LNNI), IOUT(LNCF), IOUT(LNETF), IOUT(LNGE)
95 FORMAT(//'Final statistics:'//
1 ' No. steps = ', I4, ' No. f-s = ', I4,
2 ' No. J-s = ', I4, ' No. LU-s = ', I4/
3 ' No. nonlinear iterations = ', I4/
4 ' No. nonlinear convergence failures = ', I4/
5 ' No. error test failures = ', I4/
6 ' No. root function evals = ', I4)
C
CALL FCVROOTFREE
CALL FCVFREE
C
STOP
END
C ----------------------------------------------------------------
SUBROUTINE FCVFUN(T, Y, YDOT, IPAR, RPAR, IER)
C Fortran routine for right-hand side function
IMPLICIT NONE
C
C The following declaration specification should match C type long int
INTEGER*8 IPAR(*)
INTEGER*4 IER
DOUBLE PRECISION T, Y(*), YDOT(*), RPAR(*)
C
YDOT(1) = -0.04D0 * Y(1) + 1.0D4 * Y(2) * Y(3)
YDOT(3) = 3.0D7 * Y(2) * Y(2)
YDOT(2) = -YDOT(1) - YDOT(3)
C
IER = 0
C
RETURN
END
C ----------------------------------------------------------------
SUBROUTINE FCVROOTFN(T, Y, G, IPAR, RPAR, IER)
C Fortran routine for root finding
IMPLICIT NONE
C
DOUBLE PRECISION T, Y(*), G(*), RPAR(*)
C The following declaration specification should match C type long int
INTEGER*8 IPAR(*)
INTEGER*4 IER
C
G(1) = Y(1) - 1.0D-4
G(2) = Y(3) - 1.0D-2
C
IER = 0
RETURN
END
C ----------------------------------------------------------------
SUBROUTINE FCVDJAC(N, T, Y, FY, JAC, H, IPAR, RPAR,
1 V1, V2, V3, IER)
C Fortran routine for dense user-supplied Jacobian
IMPLICIT NONE
C
C The following declaration specification should match C type long int
INTEGER*8 N, IPAR(*)
INTEGER*4 IER
DOUBLE PRECISION T, Y(*), FY(*), JAC(N,*), H, RPAR(*)
DOUBLE PRECISION V1(*), V2(*), V3(*)
C
DOUBLE PRECISION Y1, Y2, Y3
C
Y1 = Y(1)
Y2 = Y(2)
Y3 = Y(3)
JAC(1,1) = -0.04D0
JAC(1,2) = 1.0D4 * Y3
JAC(1,3) = 1.0D4 * Y2
JAC(2,1) = 0.04D0
JAC(2,2) = -1.0D4 * Y3 - 6.0D7 * Y2
JAC(2,3) = -1.0D4 * Y2
JAC(3,3) = 0.0D0
JAC(3,2) = 6.0D7 * Y2
JAC(3,3) = 0.0D0
C
IER = 0
C
RETURN
END
|