sdriv2.f 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. *DECK SDRIV2
  2. SUBROUTINE SDRIV2 (N, T, Y, F, TOUT, MSTATE, NROOT, EPS, EWT,
  3. 8 MINT, WORK, LENW, IWORK, LENIW, G, IERFLG)
  4. C***BEGIN PROLOGUE SDRIV2
  5. C***PURPOSE The function of SDRIV2 is to solve N ordinary differential
  6. C equations of the form dY(I)/dT = F(Y(I),T), given the
  7. C initial conditions Y(I) = YI. The program has options to
  8. C allow the solution of both stiff and non-stiff differential
  9. C equations. SDRIV2 uses single precision arithmetic.
  10. C***LIBRARY SLATEC (SDRIVE)
  11. C***CATEGORY I1A2, I1A1B
  12. C***TYPE SINGLE PRECISION (SDRIV2-S, DDRIV2-D, CDRIV2-C)
  13. C***KEYWORDS GEAR'S METHOD, INITIAL VALUE PROBLEMS, ODE,
  14. C ORDINARY DIFFERENTIAL EQUATIONS, SDRIVE, SINGLE PRECISION,
  15. C STIFF
  16. C***AUTHOR Kahaner, D. K., (NIST)
  17. C National Institute of Standards and Technology
  18. C Gaithersburg, MD 20899
  19. C Sutherland, C. D., (LANL)
  20. C Mail Stop D466
  21. C Los Alamos National Laboratory
  22. C Los Alamos, NM 87545
  23. C***DESCRIPTION
  24. C
  25. C I. PARAMETERS .....................................................
  26. C
  27. C The user should use parameter names in the call sequence of SDRIV2
  28. C for those quantities whose value may be altered by SDRIV2. The
  29. C parameters in the call sequence are:
  30. C
  31. C N = (Input) The number of differential equations.
  32. C
  33. C T = The independent variable. On input for the first call, T
  34. C is the initial point. On output, T is the point at which
  35. C the solution is given.
  36. C
  37. C Y = The vector of dependent variables. Y is used as input on
  38. C the first call, to set the initial values. On output, Y
  39. C is the computed solution vector. This array Y is passed
  40. C in the call sequence of the user-provided routines F and
  41. C G. Thus parameters required by F and G can be stored in
  42. C this array in components N+1 and above. (Note: Changes
  43. C by the user to the first N components of this array will
  44. C take effect only after a restart, i.e., after setting
  45. C MSTATE to +1(-1).)
  46. C
  47. C F = A subroutine supplied by the user. The name must be
  48. C declared EXTERNAL in the user's calling program. This
  49. C subroutine is of the form:
  50. C SUBROUTINE F (N, T, Y, YDOT)
  51. C REAL Y(*), YDOT(*)
  52. C .
  53. C .
  54. C YDOT(1) = ...
  55. C .
  56. C .
  57. C YDOT(N) = ...
  58. C END (Sample)
  59. C This computes YDOT = F(Y,T), the right hand side of the
  60. C differential equations. Here Y is a vector of length at
  61. C least N. The actual length of Y is determined by the
  62. C user's declaration in the program which calls SDRIV2.
  63. C Thus the dimensioning of Y in F, while required by FORTRAN
  64. C convention, does not actually allocate any storage. When
  65. C this subroutine is called, the first N components of Y are
  66. C intermediate approximations to the solution components.
  67. C The user should not alter these values. Here YDOT is a
  68. C vector of length N. The user should only compute YDOT(I)
  69. C for I from 1 to N. Normally a return from F passes
  70. C control back to SDRIV2. However, if the user would like
  71. C to abort the calculation, i.e., return control to the
  72. C program which calls SDRIV2, he should set N to zero.
  73. C SDRIV2 will signal this by returning a value of MSTATE
  74. C equal to +6(-6). Altering the value of N in F has no
  75. C effect on the value of N in the call sequence of SDRIV2.
  76. C
  77. C TOUT = (Input) The point at which the solution is desired.
  78. C
  79. C MSTATE = An integer describing the status of integration. The user
  80. C must initialize MSTATE to +1 or -1. If MSTATE is
  81. C positive, the routine will integrate past TOUT and
  82. C interpolate the solution. This is the most efficient
  83. C mode. If MSTATE is negative, the routine will adjust its
  84. C internal step to reach TOUT exactly (useful if a
  85. C singularity exists beyond TOUT.) The meaning of the
  86. C magnitude of MSTATE:
  87. C 1 (Input) Means the first call to the routine. This
  88. C value must be set by the user. On all subsequent
  89. C calls the value of MSTATE should be tested by the
  90. C user. Unless SDRIV2 is to be reinitialized, only the
  91. C sign of MSTATE may be changed by the user. (As a
  92. C convenience to the user who may wish to put out the
  93. C initial conditions, SDRIV2 can be called with
  94. C MSTATE=+1(-1), and TOUT=T. In this case the program
  95. C will return with MSTATE unchanged, i.e.,
  96. C MSTATE=+1(-1).)
  97. C 2 (Output) Means a successful integration. If a normal
  98. C continuation is desired (i.e., a further integration
  99. C in the same direction), simply advance TOUT and call
  100. C again. All other parameters are automatically set.
  101. C 3 (Output)(Unsuccessful) Means the integrator has taken
  102. C 1000 steps without reaching TOUT. The user can
  103. C continue the integration by simply calling SDRIV2
  104. C again. Other than an error in problem setup, the
  105. C most likely cause for this condition is trying to
  106. C integrate a stiff set of equations with the non-stiff
  107. C integrator option. (See description of MINT below.)
  108. C 4 (Output)(Unsuccessful) Means too much accuracy has
  109. C been requested. EPS has been increased to a value
  110. C the program estimates is appropriate. The user can
  111. C continue the integration by simply calling SDRIV2
  112. C again.
  113. C 5 (Output) A root was found at a point less than TOUT.
  114. C The user can continue the integration toward TOUT by
  115. C simply calling SDRIV2 again.
  116. C 6 (Output)(Unsuccessful) N has been set to zero in
  117. C SUBROUTINE F.
  118. C 7 (Output)(Unsuccessful) N has been set to zero in
  119. C FUNCTION G. See description of G below.
  120. C 8 (Output)(Successful) For MSTATE negative, T is beyond
  121. C TOUT. The solution was obtained by interpolation.
  122. C The user can continue the integration by simply
  123. C advancing TOUT and calling SDRIV2 again.
  124. C 9 (Output)(Unsuccessful) The solution could not be
  125. C obtained. The value of IERFLG (see description
  126. C below) for a "Recoverable" situation indicates the
  127. C type of difficulty encountered: either an illegal
  128. C value for a parameter or an inability to continue the
  129. C solution. For this condition the user should take
  130. C corrective action and reset MSTATE to +1(-1) before
  131. C calling SDRIV2 again. Otherwise the program will
  132. C terminate the run.
  133. C
  134. C NROOT = (Input) The number of equations whose roots are desired.
  135. C If NROOT is zero, the root search is not active. This
  136. C option is useful for obtaining output at points which are
  137. C not known in advance, but depend upon the solution, e.g.,
  138. C when some solution component takes on a specified value.
  139. C The root search is carried out using the user-written
  140. C function G (see description of G below.) SDRIV2 attempts
  141. C to find the value of T at which one of the equations
  142. C changes sign. SDRIV2 can find at most one root per
  143. C equation per internal integration step, and will then
  144. C return the solution either at TOUT or at a root, whichever
  145. C occurs first in the direction of integration. The initial
  146. C point is never reported as a root. The index of the
  147. C equation whose root is being reported is stored in the
  148. C sixth element of IWORK.
  149. C NOTE: NROOT is never altered by this program.
  150. C
  151. C EPS = On input, the requested relative accuracy in all solution
  152. C components. EPS = 0 is allowed. On output, the adjusted
  153. C relative accuracy if the input value was too small. The
  154. C value of EPS should be set as large as is reasonable,
  155. C because the amount of work done by SDRIV2 increases as
  156. C EPS decreases.
  157. C
  158. C EWT = (Input) Problem zero, i.e., the smallest physically
  159. C meaningful value for the solution. This is used inter-
  160. C nally to compute an array YWT(I) = MAX(ABS(Y(I)), EWT).
  161. C One step error estimates divided by YWT(I) are kept less
  162. C than EPS. Setting EWT to zero provides pure relative
  163. C error control. However, setting EWT smaller than
  164. C necessary can adversely affect the running time.
  165. C
  166. C MINT = (Input) The integration method flag.
  167. C MINT = 1 Means the Adams methods, and is used for
  168. C non-stiff problems.
  169. C MINT = 2 Means the stiff methods of Gear (i.e., the
  170. C backward differentiation formulas), and is
  171. C used for stiff problems.
  172. C MINT = 3 Means the program dynamically selects the
  173. C Adams methods when the problem is non-stiff
  174. C and the Gear methods when the problem is
  175. C stiff.
  176. C MINT may not be changed without restarting, i.e., setting
  177. C the magnitude of MSTATE to 1.
  178. C
  179. C WORK
  180. C LENW = (Input)
  181. C WORK is an array of LENW real words used
  182. C internally for temporary storage. The user must allocate
  183. C space for this array in the calling program by a statement
  184. C such as
  185. C REAL WORK(...)
  186. C The length of WORK should be at least
  187. C 16*N + 2*NROOT + 250 if MINT is 1, or
  188. C N*N + 10*N + 2*NROOT + 250 if MINT is 2, or
  189. C N*N + 17*N + 2*NROOT + 250 if MINT is 3,
  190. C and LENW should be set to the value used. The contents of
  191. C WORK should not be disturbed between calls to SDRIV2.
  192. C
  193. C IWORK
  194. C LENIW = (Input)
  195. C IWORK is an integer array of length LENIW used internally
  196. C for temporary storage. The user must allocate space for
  197. C this array in the calling program by a statement such as
  198. C INTEGER IWORK(...)
  199. C The length of IWORK should be at least
  200. C 50 if MINT is 1, or
  201. C N+50 if MINT is 2 or 3,
  202. C and LENIW should be set to the value used. The contents
  203. C of IWORK should not be disturbed between calls to SDRIV2.
  204. C
  205. C G = A real FORTRAN function supplied by the user
  206. C if NROOT is not 0. In this case, the name must be
  207. C declared EXTERNAL in the user's calling program. G is
  208. C repeatedly called with different values of IROOT to
  209. C obtain the value of each of the NROOT equations for which
  210. C a root is desired. G is of the form:
  211. C REAL FUNCTION G (N, T, Y, IROOT)
  212. C REAL Y(*)
  213. C GO TO (10, ...), IROOT
  214. C 10 G = ...
  215. C .
  216. C .
  217. C END (Sample)
  218. C Here, Y is a vector of length at least N, whose first N
  219. C components are the solution components at the point T.
  220. C The user should not alter these values. The actual length
  221. C of Y is determined by the user's declaration in the
  222. C program which calls SDRIV2. Thus the dimensioning of Y in
  223. C G, while required by FORTRAN convention, does not actually
  224. C allocate any storage. Normally a return from G passes
  225. C control back to SDRIV2. However, if the user would like
  226. C to abort the calculation, i.e., return control to the
  227. C program which calls SDRIV2, he should set N to zero.
  228. C SDRIV2 will signal this by returning a value of MSTATE
  229. C equal to +7(-7). In this case, the index of the equation
  230. C being evaluated is stored in the sixth element of IWORK.
  231. C Altering the value of N in G has no effect on the value of
  232. C N in the call sequence of SDRIV2.
  233. C
  234. C IERFLG = An error flag. The error number associated with a
  235. C diagnostic message (see Section II-A below) is the same as
  236. C the corresponding value of IERFLG. The meaning of IERFLG:
  237. C 0 The routine completed successfully. (No message is
  238. C issued.)
  239. C 3 (Warning) The number of steps required to reach TOUT
  240. C exceeds MXSTEP.
  241. C 4 (Warning) The value of EPS is too small.
  242. C 11 (Warning) For MSTATE negative, T is beyond TOUT.
  243. C The solution was obtained by interpolation.
  244. C 15 (Warning) The integration step size is below the
  245. C roundoff level of T. (The program issues this
  246. C message as a warning but does not return control to
  247. C the user.)
  248. C 22 (Recoverable) N is not positive.
  249. C 23 (Recoverable) MINT is less than 1 or greater than 3 .
  250. C 26 (Recoverable) The magnitude of MSTATE is either 0 or
  251. C greater than 9 .
  252. C 27 (Recoverable) EPS is less than zero.
  253. C 32 (Recoverable) Insufficient storage has been allocated
  254. C for the WORK array.
  255. C 33 (Recoverable) Insufficient storage has been allocated
  256. C for the IWORK array.
  257. C 41 (Recoverable) The integration step size has gone
  258. C to zero.
  259. C 42 (Recoverable) The integration step size has been
  260. C reduced about 50 times without advancing the
  261. C solution. The problem setup may not be correct.
  262. C 999 (Fatal) The magnitude of MSTATE is 9 .
  263. C
  264. C II. OTHER COMMUNICATION TO THE USER ...............................
  265. C
  266. C A. The solver communicates to the user through the parameters
  267. C above. In addition it writes diagnostic messages through the
  268. C standard error handling program XERMSG. A complete description
  269. C of XERMSG is given in "Guide to the SLATEC Common Mathematical
  270. C Library" by Kirby W. Fong et al.. At installations which do not
  271. C have this error handling package the short but serviceable
  272. C routine, XERMSG, available with this package, can be used. That
  273. C program uses the file named OUTPUT to transmit messages.
  274. C
  275. C B. The first three elements of WORK and the first five elements of
  276. C IWORK will contain the following statistical data:
  277. C AVGH The average step size used.
  278. C HUSED The step size last used (successfully).
  279. C AVGORD The average order used.
  280. C IMXERR The index of the element of the solution vector that
  281. C contributed most to the last error test.
  282. C NQUSED The order last used (successfully).
  283. C NSTEP The number of steps taken since last initialization.
  284. C NFE The number of evaluations of the right hand side.
  285. C NJE The number of evaluations of the Jacobian matrix.
  286. C
  287. C III. REMARKS ......................................................
  288. C
  289. C A. On any return from SDRIV2 all information necessary to continue
  290. C the calculation is contained in the call sequence parameters,
  291. C including the work arrays. Thus it is possible to suspend one
  292. C problem, integrate another, and then return to the first.
  293. C
  294. C B. If this package is to be used in an overlay situation, the user
  295. C must declare in the primary overlay the variables in the call
  296. C sequence to SDRIV2.
  297. C
  298. C C. When the routine G is not required, difficulties associated with
  299. C an unsatisfied external can be avoided by using the name of the
  300. C routine which calculates the right hand side of the differential
  301. C equations in place of G in the call sequence of SDRIV2.
  302. C
  303. C IV. USAGE .........................................................
  304. C
  305. C PROGRAM SAMPLE
  306. C EXTERNAL F
  307. C PARAMETER(MINT = 1, NROOT = 0, N = ...,
  308. C 8 LENW = 16*N + 2*NROOT + 250, LENIW = 50)
  309. C C N is the number of equations
  310. C REAL EPS, EWT, T, TOUT, WORK(LENW), Y(N)
  311. C INTEGER IWORK(LENIW)
  312. C OPEN(FILE='TAPE6', UNIT=6, STATUS='NEW')
  313. C C Initial point
  314. C T = 0.
  315. C C Set initial conditions
  316. C DO 10 I = 1,N
  317. C 10 Y(I) = ...
  318. C TOUT = T
  319. C EWT = ...
  320. C MSTATE = 1
  321. C EPS = ...
  322. C 20 CALL SDRIV2 (N, T, Y, F, TOUT, MSTATE, NROOT, EPS, EWT,
  323. C 8 MINT, WORK, LENW, IWORK, LENIW, F, IERFLG)
  324. C C Next to last argument is not
  325. C C F if rootfinding is used.
  326. C IF (MSTATE .GT. 2) STOP
  327. C WRITE(6, 100) TOUT, (Y(I), I=1,N)
  328. C TOUT = TOUT + 1.
  329. C IF (TOUT .LE. 10.) GO TO 20
  330. C 100 FORMAT(...)
  331. C END (Sample)
  332. C
  333. C***REFERENCES C. W. Gear, Numerical Initial Value Problems in
  334. C Ordinary Differential Equations, Prentice-Hall, 1971.
  335. C***ROUTINES CALLED SDRIV3, XERMSG
  336. C***REVISION HISTORY (YYMMDD)
  337. C 790601 DATE WRITTEN
  338. C 900329 Initial submission to SLATEC.
  339. C***END PROLOGUE SDRIV2
  340. EXTERNAL F, G
  341. REAL EPS, EWT, EWTCOM(1), G, HMAX, T, TOUT,
  342. 8 WORK(*), Y(*)
  343. INTEGER IWORK(*)
  344. INTEGER IERFLG, IERROR, IMPL, LENIW, LENW, MINT, MITER, ML,
  345. 8 MSTATE, MU, MXORD, MXSTEP, N, NDE, NROOT, NSTATE, NTASK
  346. CHARACTER INTGR1*8
  347. PARAMETER(IMPL = 0, MXSTEP = 1000)
  348. C***FIRST EXECUTABLE STATEMENT SDRIV2
  349. IF (ABS(MSTATE) .EQ. 9) THEN
  350. IERFLG = 999
  351. CALL XERMSG('SLATEC', 'SDRIV2',
  352. 8 'Illegal input. The magnitude of MSTATE IS 9 .',
  353. 8 IERFLG, 2)
  354. RETURN
  355. ELSE IF (ABS(MSTATE) .EQ. 0 .OR. ABS(MSTATE) .GT. 9) THEN
  356. WRITE(INTGR1, '(I8)') MSTATE
  357. IERFLG = 26
  358. CALL XERMSG('SLATEC', 'SDRIV2',
  359. 8 'Illegal input. The magnitude of MSTATE, '//INTGR1//
  360. 8 ' is not in the range 1 to 8 .', IERFLG, 1)
  361. MSTATE = SIGN(9, MSTATE)
  362. RETURN
  363. END IF
  364. IF (MINT .LT. 1 .OR. MINT .GT. 3) THEN
  365. WRITE(INTGR1, '(I8)') MINT
  366. IERFLG = 23
  367. CALL XERMSG('SLATEC', 'SDRIV2',
  368. 8 'Illegal input. Improper value for the integration method '//
  369. 8 'flag, '//INTGR1//' .', IERFLG, 1)
  370. MSTATE = SIGN(9, MSTATE)
  371. RETURN
  372. END IF
  373. IF (MSTATE .GE. 0) THEN
  374. NSTATE = MSTATE
  375. NTASK = 1
  376. ELSE
  377. NSTATE = - MSTATE
  378. NTASK = 3
  379. END IF
  380. EWTCOM(1) = EWT
  381. IF (EWT .NE. 0.E0) THEN
  382. IERROR = 3
  383. ELSE
  384. IERROR = 2
  385. END IF
  386. IF (MINT .EQ. 1) THEN
  387. MITER = 0
  388. MXORD = 12
  389. ELSE IF (MINT .EQ. 2) THEN
  390. MITER = 2
  391. MXORD = 5
  392. ELSE IF (MINT .EQ. 3) THEN
  393. MITER = 2
  394. MXORD = 12
  395. END IF
  396. HMAX = 2.E0*ABS(TOUT - T)
  397. CALL SDRIV3 (N, T, Y, F, NSTATE, TOUT, NTASK, NROOT, EPS, EWTCOM,
  398. 8 IERROR, MINT, MITER, IMPL, ML, MU, MXORD, HMAX, WORK,
  399. 8 LENW, IWORK, LENIW, F, F, NDE, MXSTEP, G, F, IERFLG)
  400. IF (NSTATE .LE. 7) THEN
  401. MSTATE = SIGN(NSTATE, MSTATE)
  402. ELSE IF (NSTATE .EQ. 11) THEN
  403. MSTATE = SIGN(8, MSTATE)
  404. ELSE IF (NSTATE .GT. 11) THEN
  405. MSTATE = SIGN(9, MSTATE)
  406. END IF
  407. RETURN
  408. END