dsteps.f 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. *DECK DSTEPS
  2. SUBROUTINE DSTEPS (DF, NEQN, Y, X, H, EPS, WT, START, HOLD, K,
  3. + KOLD, CRASH, PHI, P, YP, PSI, ALPHA, BETA, SIG, V, W, G,
  4. + PHASE1, NS, NORND, KSTEPS, TWOU, FOURU, XOLD, KPREV, IVC, IV,
  5. + KGI, GI, RPAR, IPAR)
  6. C***BEGIN PROLOGUE DSTEPS
  7. C***PURPOSE Integrate a system of first order ordinary differential
  8. C equations one step.
  9. C***LIBRARY SLATEC (DEPAC)
  10. C***CATEGORY I1A1B
  11. C***TYPE DOUBLE PRECISION (STEPS-S, DSTEPS-D)
  12. C***KEYWORDS ADAMS METHOD, DEPAC, INITIAL VALUE PROBLEMS, ODE,
  13. C ORDINARY DIFFERENTIAL EQUATIONS, PREDICTOR-CORRECTOR
  14. C***AUTHOR Shampine, L. F., (SNLA)
  15. C Gordon, M. K., (SNLA)
  16. C MODIFIED BY H.A. WATTS
  17. C***DESCRIPTION
  18. C
  19. C Written by L. F. Shampine and M. K. Gordon
  20. C
  21. C Abstract
  22. C
  23. C Subroutine DSTEPS is normally used indirectly through subroutine
  24. C DDEABM . Because DDEABM suffices for most problems and is much
  25. C easier to use, using it should be considered before using DSTEPS
  26. C alone.
  27. C
  28. C Subroutine DSTEPS integrates a system of NEQN first order ordinary
  29. C differential equations one step, normally from X to X+H, using a
  30. C modified divided difference form of the Adams Pece formulas. Local
  31. C extrapolation is used to improve absolute stability and accuracy.
  32. C The code adjusts its order and step size to control the local error
  33. C per unit step in a generalized sense. Special devices are included
  34. C to control roundoff error and to detect when the user is requesting
  35. C too much accuracy.
  36. C
  37. C This code is completely explained and documented in the text,
  38. C Computer Solution of Ordinary Differential Equations, The Initial
  39. C Value Problem by L. F. Shampine and M. K. Gordon.
  40. C Further details on use of this code are available in "Solving
  41. C Ordinary Differential Equations with ODE, STEP, and INTRP",
  42. C by L. F. Shampine and M. K. Gordon, SLA-73-1060.
  43. C
  44. C
  45. C The parameters represent --
  46. C DF -- subroutine to evaluate derivatives
  47. C NEQN -- number of equations to be integrated
  48. C Y(*) -- solution vector at X
  49. C X -- independent variable
  50. C H -- appropriate step size for next step. Normally determined by
  51. C code
  52. C EPS -- local error tolerance
  53. C WT(*) -- vector of weights for error criterion
  54. C START -- logical variable set .TRUE. for first step, .FALSE.
  55. C otherwise
  56. C HOLD -- step size used for last successful step
  57. C K -- appropriate order for next step (determined by code)
  58. C KOLD -- order used for last successful step
  59. C CRASH -- logical variable set .TRUE. when no step can be taken,
  60. C .FALSE. otherwise.
  61. C YP(*) -- derivative of solution vector at X after successful
  62. C step
  63. C KSTEPS -- counter on attempted steps
  64. C TWOU -- 2.*U where U is machine unit roundoff quantity
  65. C FOURU -- 4.*U where U is machine unit roundoff quantity
  66. C RPAR,IPAR -- parameter arrays which you may choose to use
  67. C for communication between your program and subroutine F.
  68. C They are not altered or used by DSTEPS.
  69. C The variables X,XOLD,KOLD,KGI and IVC and the arrays Y,PHI,ALPHA,G,
  70. C W,P,IV and GI are required for the interpolation subroutine SINTRP.
  71. C The remaining variables and arrays are included in the call list
  72. C only to eliminate local retention of variables between calls.
  73. C
  74. C Input to DSTEPS
  75. C
  76. C First call --
  77. C
  78. C The user must provide storage in his calling program for all arrays
  79. C in the call list, namely
  80. C
  81. C DIMENSION Y(NEQN),WT(NEQN),PHI(NEQN,16),P(NEQN),YP(NEQN),PSI(12),
  82. C 1 ALPHA(12),BETA(12),SIG(13),V(12),W(12),G(13),GI(11),IV(10),
  83. C 2 RPAR(*),IPAR(*)
  84. C
  85. C **Note**
  86. C
  87. C The user must also declare START , CRASH , PHASE1 and NORND
  88. C logical variables and DF an EXTERNAL subroutine, supply the
  89. C subroutine DF(X,Y,YP) to evaluate
  90. C DY(I)/DX = YP(I) = DF(X,Y(1),Y(2),...,Y(NEQN))
  91. C and initialize only the following parameters.
  92. C NEQN -- number of equations to be integrated
  93. C Y(*) -- vector of initial values of dependent variables
  94. C X -- initial value of the independent variable
  95. C H -- nominal step size indicating direction of integration
  96. C and maximum size of step. Must be variable
  97. C EPS -- local error tolerance per step. Must be variable
  98. C WT(*) -- vector of non-zero weights for error criterion
  99. C START -- .TRUE.
  100. C YP(*) -- vector of initial derivative values
  101. C KSTEPS -- set KSTEPS to zero
  102. C TWOU -- 2.*U where U is machine unit roundoff quantity
  103. C FOURU -- 4.*U where U is machine unit roundoff quantity
  104. C Define U to be the machine unit roundoff quantity by calling
  105. C the function routine D1MACH, U = D1MACH(4), or by
  106. C computing U so that U is the smallest positive number such
  107. C that 1.0+U .GT. 1.0.
  108. C
  109. C DSTEPS requires that the L2 norm of the vector with components
  110. C LOCAL ERROR(L)/WT(L) be less than EPS for a successful step. The
  111. C array WT allows the user to specify an error test appropriate
  112. C for his problem. For example,
  113. C WT(L) = 1.0 specifies absolute error,
  114. C = ABS(Y(L)) error relative to the most recent value of the
  115. C L-th component of the solution,
  116. C = ABS(YP(L)) error relative to the most recent value of
  117. C the L-th component of the derivative,
  118. C = MAX(WT(L),ABS(Y(L))) error relative to the largest
  119. C magnitude of L-th component obtained so far,
  120. C = ABS(Y(L))*RELERR/EPS + ABSERR/EPS specifies a mixed
  121. C relative-absolute test where RELERR is relative
  122. C error, ABSERR is absolute error and EPS =
  123. C MAX(RELERR,ABSERR) .
  124. C
  125. C Subsequent calls --
  126. C
  127. C Subroutine DSTEPS is designed so that all information needed to
  128. C continue the integration, including the step size H and the order
  129. C K , is returned with each step. With the exception of the step
  130. C size, the error tolerance, and the weights, none of the parameters
  131. C should be altered. The array WT must be updated after each step
  132. C to maintain relative error tests like those above. Normally the
  133. C integration is continued just beyond the desired endpoint and the
  134. C solution interpolated there with subroutine SINTRP . If it is
  135. C impossible to integrate beyond the endpoint, the step size may be
  136. C reduced to hit the endpoint since the code will not take a step
  137. C larger than the H input. Changing the direction of integration,
  138. C i.e., the sign of H , requires the user set START = .TRUE. before
  139. C calling DSTEPS again. This is the only situation in which START
  140. C should be altered.
  141. C
  142. C Output from DSTEPS
  143. C
  144. C Successful Step --
  145. C
  146. C The subroutine returns after each successful step with START and
  147. C CRASH set .FALSE. . X represents the independent variable
  148. C advanced one step of length HOLD from its value on input and Y
  149. C the solution vector at the new value of X . All other parameters
  150. C represent information corresponding to the new X needed to
  151. C continue the integration.
  152. C
  153. C Unsuccessful Step --
  154. C
  155. C When the error tolerance is too small for the machine precision,
  156. C the subroutine returns without taking a step and CRASH = .TRUE. .
  157. C An appropriate step size and error tolerance for continuing are
  158. C estimated and all other information is restored as upon input
  159. C before returning. To continue with the larger tolerance, the user
  160. C just calls the code again. A restart is neither required nor
  161. C desirable.
  162. C
  163. C***REFERENCES L. F. Shampine and M. K. Gordon, Solving ordinary
  164. C differential equations with ODE, STEP, and INTRP,
  165. C Report SLA-73-1060, Sandia Laboratories, 1973.
  166. C***ROUTINES CALLED D1MACH, DHSTRT
  167. C***REVISION HISTORY (YYMMDD)
  168. C 740101 DATE WRITTEN
  169. C 890531 Changed all specific intrinsics to generic. (WRB)
  170. C 890831 Modified array declarations. (WRB)
  171. C 890831 REVISION DATE from Version 3.2
  172. C 891214 Prologue converted to Version 4.0 format. (BAB)
  173. C 920501 Reformatted the REFERENCES section. (WRB)
  174. C***END PROLOGUE DSTEPS
  175. C
  176. INTEGER I, IFAIL, IM1, IP1, IPAR, IQ, J, K, KM1, KM2, KNEW,
  177. 1 KOLD, KP1, KP2, KSTEPS, L, LIMIT1, LIMIT2, NEQN, NS, NSM2,
  178. 2 NSP1, NSP2
  179. DOUBLE PRECISION ABSH, ALPHA, BETA, BIG, D1MACH,
  180. 1 EPS, ERK, ERKM1, ERKM2, ERKP1, ERR,
  181. 2 FOURU, G, GI, GSTR, H, HNEW, HOLD, P, P5EPS, PHI, PSI, R,
  182. 3 REALI, REALNS, RHO, ROUND, RPAR, SIG, TAU, TEMP1,
  183. 4 TEMP2, TEMP3, TEMP4, TEMP5, TEMP6, TWO, TWOU, U, V, W, WT,
  184. 5 X, XOLD, Y, YP
  185. LOGICAL START,CRASH,PHASE1,NORND
  186. DIMENSION Y(*),WT(*),PHI(NEQN,16),P(*),YP(*),PSI(12),
  187. 1 ALPHA(12),BETA(12),SIG(13),V(12),W(12),G(13),GI(11),IV(10),
  188. 2 RPAR(*),IPAR(*)
  189. DIMENSION TWO(13),GSTR(13)
  190. EXTERNAL DF
  191. SAVE TWO, GSTR
  192. C
  193. DATA TWO(1),TWO(2),TWO(3),TWO(4),TWO(5),TWO(6),TWO(7),TWO(8),
  194. 1 TWO(9),TWO(10),TWO(11),TWO(12),TWO(13)
  195. 2 /2.0D0,4.0D0,8.0D0,16.0D0,32.0D0,64.0D0,128.0D0,256.0D0,
  196. 3 512.0D0,1024.0D0,2048.0D0,4096.0D0,8192.0D0/
  197. DATA GSTR(1),GSTR(2),GSTR(3),GSTR(4),GSTR(5),GSTR(6),GSTR(7),
  198. 1 GSTR(8),GSTR(9),GSTR(10),GSTR(11),GSTR(12),GSTR(13)
  199. 2 /0.5D0,0.0833D0,0.0417D0,0.0264D0,0.0188D0,0.0143D0,0.0114D0,
  200. 3 0.00936D0,0.00789D0,0.00679D0,0.00592D0,0.00524D0,0.00468D0/
  201. C
  202. C *** BEGIN BLOCK 0 ***
  203. C CHECK IF STEP SIZE OR ERROR TOLERANCE IS TOO SMALL FOR MACHINE
  204. C PRECISION. IF FIRST STEP, INITIALIZE PHI ARRAY AND ESTIMATE A
  205. C STARTING STEP SIZE.
  206. C ***
  207. C
  208. C IF STEP SIZE IS TOO SMALL, DETERMINE AN ACCEPTABLE ONE
  209. C
  210. C***FIRST EXECUTABLE STATEMENT DSTEPS
  211. CRASH = .TRUE.
  212. IF(ABS(H) .GE. FOURU*ABS(X)) GO TO 5
  213. H = SIGN(FOURU*ABS(X),H)
  214. RETURN
  215. 5 P5EPS = 0.5D0*EPS
  216. C
  217. C IF ERROR TOLERANCE IS TOO SMALL, INCREASE IT TO AN ACCEPTABLE VALUE
  218. C
  219. ROUND = 0.0D0
  220. DO 10 L = 1,NEQN
  221. 10 ROUND = ROUND + (Y(L)/WT(L))**2
  222. ROUND = TWOU*SQRT(ROUND)
  223. IF(P5EPS .GE. ROUND) GO TO 15
  224. EPS = 2.0D0*ROUND*(1.0D0 + FOURU)
  225. RETURN
  226. 15 CRASH = .FALSE.
  227. G(1) = 1.0D0
  228. G(2) = 0.5D0
  229. SIG(1) = 1.0D0
  230. IF(.NOT.START) GO TO 99
  231. C
  232. C INITIALIZE. COMPUTE APPROPRIATE STEP SIZE FOR FIRST STEP
  233. C
  234. C CALL DF(X,Y,YP,RPAR,IPAR)
  235. C SUM = 0.0
  236. DO 20 L = 1,NEQN
  237. PHI(L,1) = YP(L)
  238. 20 PHI(L,2) = 0.0D0
  239. C20 SUM = SUM + (YP(L)/WT(L))**2
  240. C SUM = SQRT(SUM)
  241. C ABSH = ABS(H)
  242. C IF(EPS .LT. 16.0*SUM*H*H) ABSH = 0.25*SQRT(EPS/SUM)
  243. C H = SIGN(MAX(ABSH,FOURU*ABS(X)),H)
  244. C
  245. U = D1MACH(4)
  246. BIG = SQRT(D1MACH(2))
  247. CALL DHSTRT(DF,NEQN,X,X+H,Y,YP,WT,1,U,BIG,
  248. 1 PHI(1,3),PHI(1,4),PHI(1,5),PHI(1,6),RPAR,IPAR,H)
  249. C
  250. HOLD = 0.0D0
  251. K = 1
  252. KOLD = 0
  253. KPREV = 0
  254. START = .FALSE.
  255. PHASE1 = .TRUE.
  256. NORND = .TRUE.
  257. IF(P5EPS .GT. 100.0D0*ROUND) GO TO 99
  258. NORND = .FALSE.
  259. DO 25 L = 1,NEQN
  260. 25 PHI(L,15) = 0.0D0
  261. 99 IFAIL = 0
  262. C *** END BLOCK 0 ***
  263. C
  264. C *** BEGIN BLOCK 1 ***
  265. C COMPUTE COEFFICIENTS OF FORMULAS FOR THIS STEP. AVOID COMPUTING
  266. C THOSE QUANTITIES NOT CHANGED WHEN STEP SIZE IS NOT CHANGED.
  267. C ***
  268. C
  269. 100 KP1 = K+1
  270. KP2 = K+2
  271. KM1 = K-1
  272. KM2 = K-2
  273. C
  274. C NS IS THE NUMBER OF DSTEPS TAKEN WITH SIZE H, INCLUDING THE CURRENT
  275. C ONE. WHEN K.LT.NS, NO COEFFICIENTS CHANGE
  276. C
  277. IF(H .NE. HOLD) NS = 0
  278. IF (NS.LE.KOLD) NS = NS+1
  279. NSP1 = NS+1
  280. IF (K .LT. NS) GO TO 199
  281. C
  282. C COMPUTE THOSE COMPONENTS OF ALPHA(*),BETA(*),PSI(*),SIG(*) WHICH
  283. C ARE CHANGED
  284. C
  285. BETA(NS) = 1.0D0
  286. REALNS = NS
  287. ALPHA(NS) = 1.0D0/REALNS
  288. TEMP1 = H*REALNS
  289. SIG(NSP1) = 1.0D0
  290. IF(K .LT. NSP1) GO TO 110
  291. DO 105 I = NSP1,K
  292. IM1 = I-1
  293. TEMP2 = PSI(IM1)
  294. PSI(IM1) = TEMP1
  295. BETA(I) = BETA(IM1)*PSI(IM1)/TEMP2
  296. TEMP1 = TEMP2 + H
  297. ALPHA(I) = H/TEMP1
  298. REALI = I
  299. 105 SIG(I+1) = REALI*ALPHA(I)*SIG(I)
  300. 110 PSI(K) = TEMP1
  301. C
  302. C COMPUTE COEFFICIENTS G(*)
  303. C
  304. C INITIALIZE V(*) AND SET W(*).
  305. C
  306. IF(NS .GT. 1) GO TO 120
  307. DO 115 IQ = 1,K
  308. TEMP3 = IQ*(IQ+1)
  309. V(IQ) = 1.0D0/TEMP3
  310. 115 W(IQ) = V(IQ)
  311. IVC = 0
  312. KGI = 0
  313. IF (K .EQ. 1) GO TO 140
  314. KGI = 1
  315. GI(1) = W(2)
  316. GO TO 140
  317. C
  318. C IF ORDER WAS RAISED, UPDATE DIAGONAL PART OF V(*)
  319. C
  320. 120 IF(K .LE. KPREV) GO TO 130
  321. IF (IVC .EQ. 0) GO TO 122
  322. JV = KP1 - IV(IVC)
  323. IVC = IVC - 1
  324. GO TO 123
  325. 122 JV = 1
  326. TEMP4 = K*KP1
  327. V(K) = 1.0D0/TEMP4
  328. W(K) = V(K)
  329. IF (K .NE. 2) GO TO 123
  330. KGI = 1
  331. GI(1) = W(2)
  332. 123 NSM2 = NS-2
  333. IF(NSM2 .LT. JV) GO TO 130
  334. DO 125 J = JV,NSM2
  335. I = K-J
  336. V(I) = V(I) - ALPHA(J+1)*V(I+1)
  337. 125 W(I) = V(I)
  338. IF (I .NE. 2) GO TO 130
  339. KGI = NS - 1
  340. GI(KGI) = W(2)
  341. C
  342. C UPDATE V(*) AND SET W(*)
  343. C
  344. 130 LIMIT1 = KP1 - NS
  345. TEMP5 = ALPHA(NS)
  346. DO 135 IQ = 1,LIMIT1
  347. V(IQ) = V(IQ) - TEMP5*V(IQ+1)
  348. 135 W(IQ) = V(IQ)
  349. G(NSP1) = W(1)
  350. IF (LIMIT1 .EQ. 1) GO TO 137
  351. KGI = NS
  352. GI(KGI) = W(2)
  353. 137 W(LIMIT1+1) = V(LIMIT1+1)
  354. IF (K .GE. KOLD) GO TO 140
  355. IVC = IVC + 1
  356. IV(IVC) = LIMIT1 + 2
  357. C
  358. C COMPUTE THE G(*) IN THE WORK VECTOR W(*)
  359. C
  360. 140 NSP2 = NS + 2
  361. KPREV = K
  362. IF(KP1 .LT. NSP2) GO TO 199
  363. DO 150 I = NSP2,KP1
  364. LIMIT2 = KP2 - I
  365. TEMP6 = ALPHA(I-1)
  366. DO 145 IQ = 1,LIMIT2
  367. 145 W(IQ) = W(IQ) - TEMP6*W(IQ+1)
  368. 150 G(I) = W(1)
  369. 199 CONTINUE
  370. C *** END BLOCK 1 ***
  371. C
  372. C *** BEGIN BLOCK 2 ***
  373. C PREDICT A SOLUTION P(*), EVALUATE DERIVATIVES USING PREDICTED
  374. C SOLUTION, ESTIMATE LOCAL ERROR AT ORDER K AND ERRORS AT ORDERS K,
  375. C K-1, K-2 AS IF CONSTANT STEP SIZE WERE USED.
  376. C ***
  377. C
  378. C INCREMENT COUNTER ON ATTEMPTED DSTEPS
  379. C
  380. KSTEPS = KSTEPS + 1
  381. C
  382. C CHANGE PHI TO PHI STAR
  383. C
  384. IF(K .LT. NSP1) GO TO 215
  385. DO 210 I = NSP1,K
  386. TEMP1 = BETA(I)
  387. DO 205 L = 1,NEQN
  388. 205 PHI(L,I) = TEMP1*PHI(L,I)
  389. 210 CONTINUE
  390. C
  391. C PREDICT SOLUTION AND DIFFERENCES
  392. C
  393. 215 DO 220 L = 1,NEQN
  394. PHI(L,KP2) = PHI(L,KP1)
  395. PHI(L,KP1) = 0.0D0
  396. 220 P(L) = 0.0D0
  397. DO 230 J = 1,K
  398. I = KP1 - J
  399. IP1 = I+1
  400. TEMP2 = G(I)
  401. DO 225 L = 1,NEQN
  402. P(L) = P(L) + TEMP2*PHI(L,I)
  403. 225 PHI(L,I) = PHI(L,I) + PHI(L,IP1)
  404. 230 CONTINUE
  405. IF(NORND) GO TO 240
  406. DO 235 L = 1,NEQN
  407. TAU = H*P(L) - PHI(L,15)
  408. P(L) = Y(L) + TAU
  409. 235 PHI(L,16) = (P(L) - Y(L)) - TAU
  410. GO TO 250
  411. 240 DO 245 L = 1,NEQN
  412. 245 P(L) = Y(L) + H*P(L)
  413. 250 XOLD = X
  414. X = X + H
  415. ABSH = ABS(H)
  416. CALL DF(X,P,YP,RPAR,IPAR)
  417. C
  418. C ESTIMATE ERRORS AT ORDERS K,K-1,K-2
  419. C
  420. ERKM2 = 0.0D0
  421. ERKM1 = 0.0D0
  422. ERK = 0.0D0
  423. DO 265 L = 1,NEQN
  424. TEMP3 = 1.0D0/WT(L)
  425. TEMP4 = YP(L) - PHI(L,1)
  426. IF(KM2)265,260,255
  427. 255 ERKM2 = ERKM2 + ((PHI(L,KM1)+TEMP4)*TEMP3)**2
  428. 260 ERKM1 = ERKM1 + ((PHI(L,K)+TEMP4)*TEMP3)**2
  429. 265 ERK = ERK + (TEMP4*TEMP3)**2
  430. IF(KM2)280,275,270
  431. 270 ERKM2 = ABSH*SIG(KM1)*GSTR(KM2)*SQRT(ERKM2)
  432. 275 ERKM1 = ABSH*SIG(K)*GSTR(KM1)*SQRT(ERKM1)
  433. 280 TEMP5 = ABSH*SQRT(ERK)
  434. ERR = TEMP5*(G(K)-G(KP1))
  435. ERK = TEMP5*SIG(KP1)*GSTR(K)
  436. KNEW = K
  437. C
  438. C TEST IF ORDER SHOULD BE LOWERED
  439. C
  440. IF(KM2)299,290,285
  441. 285 IF(MAX(ERKM1,ERKM2) .LE. ERK) KNEW = KM1
  442. GO TO 299
  443. 290 IF(ERKM1 .LE. 0.5D0*ERK) KNEW = KM1
  444. C
  445. C TEST IF STEP SUCCESSFUL
  446. C
  447. 299 IF(ERR .LE. EPS) GO TO 400
  448. C *** END BLOCK 2 ***
  449. C
  450. C *** BEGIN BLOCK 3 ***
  451. C THE STEP IS UNSUCCESSFUL. RESTORE X, PHI(*,*), PSI(*) .
  452. C IF THIRD CONSECUTIVE FAILURE, SET ORDER TO ONE. IF STEP FAILS MORE
  453. C THAN THREE TIMES, CONSIDER AN OPTIMAL STEP SIZE. DOUBLE ERROR
  454. C TOLERANCE AND RETURN IF ESTIMATED STEP SIZE IS TOO SMALL FOR MACHINE
  455. C PRECISION.
  456. C ***
  457. C
  458. C RESTORE X, PHI(*,*) AND PSI(*)
  459. C
  460. PHASE1 = .FALSE.
  461. X = XOLD
  462. DO 310 I = 1,K
  463. TEMP1 = 1.0D0/BETA(I)
  464. IP1 = I+1
  465. DO 305 L = 1,NEQN
  466. 305 PHI(L,I) = TEMP1*(PHI(L,I) - PHI(L,IP1))
  467. 310 CONTINUE
  468. IF(K .LT. 2) GO TO 320
  469. DO 315 I = 2,K
  470. 315 PSI(I-1) = PSI(I) - H
  471. C
  472. C ON THIRD FAILURE, SET ORDER TO ONE. THEREAFTER, USE OPTIMAL STEP
  473. C SIZE
  474. C
  475. 320 IFAIL = IFAIL + 1
  476. TEMP2 = 0.5D0
  477. IF(IFAIL - 3) 335,330,325
  478. 325 IF(P5EPS .LT. 0.25D0*ERK) TEMP2 = SQRT(P5EPS/ERK)
  479. 330 KNEW = 1
  480. 335 H = TEMP2*H
  481. K = KNEW
  482. NS = 0
  483. IF(ABS(H) .GE. FOURU*ABS(X)) GO TO 340
  484. CRASH = .TRUE.
  485. H = SIGN(FOURU*ABS(X),H)
  486. EPS = EPS + EPS
  487. RETURN
  488. 340 GO TO 100
  489. C *** END BLOCK 3 ***
  490. C
  491. C *** BEGIN BLOCK 4 ***
  492. C THE STEP IS SUCCESSFUL. CORRECT THE PREDICTED SOLUTION, EVALUATE
  493. C THE DERIVATIVES USING THE CORRECTED SOLUTION AND UPDATE THE
  494. C DIFFERENCES. DETERMINE BEST ORDER AND STEP SIZE FOR NEXT STEP.
  495. C ***
  496. 400 KOLD = K
  497. HOLD = H
  498. C
  499. C CORRECT AND EVALUATE
  500. C
  501. TEMP1 = H*G(KP1)
  502. IF(NORND) GO TO 410
  503. DO 405 L = 1,NEQN
  504. TEMP3 = Y(L)
  505. RHO = TEMP1*(YP(L) - PHI(L,1)) - PHI(L,16)
  506. Y(L) = P(L) + RHO
  507. PHI(L,15) = (Y(L) - P(L)) - RHO
  508. 405 P(L) = TEMP3
  509. GO TO 420
  510. 410 DO 415 L = 1,NEQN
  511. TEMP3 = Y(L)
  512. Y(L) = P(L) + TEMP1*(YP(L) - PHI(L,1))
  513. 415 P(L) = TEMP3
  514. 420 CALL DF(X,Y,YP,RPAR,IPAR)
  515. C
  516. C UPDATE DIFFERENCES FOR NEXT STEP
  517. C
  518. DO 425 L = 1,NEQN
  519. PHI(L,KP1) = YP(L) - PHI(L,1)
  520. 425 PHI(L,KP2) = PHI(L,KP1) - PHI(L,KP2)
  521. DO 435 I = 1,K
  522. DO 430 L = 1,NEQN
  523. 430 PHI(L,I) = PHI(L,I) + PHI(L,KP1)
  524. 435 CONTINUE
  525. C
  526. C ESTIMATE ERROR AT ORDER K+1 UNLESS:
  527. C IN FIRST PHASE WHEN ALWAYS RAISE ORDER,
  528. C ALREADY DECIDED TO LOWER ORDER,
  529. C STEP SIZE NOT CONSTANT SO ESTIMATE UNRELIABLE
  530. C
  531. ERKP1 = 0.0D0
  532. IF(KNEW .EQ. KM1 .OR. K .EQ. 12) PHASE1 = .FALSE.
  533. IF(PHASE1) GO TO 450
  534. IF(KNEW .EQ. KM1) GO TO 455
  535. IF(KP1 .GT. NS) GO TO 460
  536. DO 440 L = 1,NEQN
  537. 440 ERKP1 = ERKP1 + (PHI(L,KP2)/WT(L))**2
  538. ERKP1 = ABSH*GSTR(KP1)*SQRT(ERKP1)
  539. C
  540. C USING ESTIMATED ERROR AT ORDER K+1, DETERMINE APPROPRIATE ORDER
  541. C FOR NEXT STEP
  542. C
  543. IF(K .GT. 1) GO TO 445
  544. IF(ERKP1 .GE. 0.5D0*ERK) GO TO 460
  545. GO TO 450
  546. 445 IF(ERKM1 .LE. MIN(ERK,ERKP1)) GO TO 455
  547. IF(ERKP1 .GE. ERK .OR. K .EQ. 12) GO TO 460
  548. C
  549. C HERE ERKP1 .LT. ERK .LT. MAX(ERKM1,ERKM2) ELSE ORDER WOULD HAVE
  550. C BEEN LOWERED IN BLOCK 2. THUS ORDER IS TO BE RAISED
  551. C
  552. C RAISE ORDER
  553. C
  554. 450 K = KP1
  555. ERK = ERKP1
  556. GO TO 460
  557. C
  558. C LOWER ORDER
  559. C
  560. 455 K = KM1
  561. ERK = ERKM1
  562. C
  563. C WITH NEW ORDER DETERMINE APPROPRIATE STEP SIZE FOR NEXT STEP
  564. C
  565. 460 HNEW = H + H
  566. IF(PHASE1) GO TO 465
  567. IF(P5EPS .GE. ERK*TWO(K+1)) GO TO 465
  568. HNEW = H
  569. IF(P5EPS .GE. ERK) GO TO 465
  570. TEMP2 = K+1
  571. R = (P5EPS/ERK)**(1.0D0/TEMP2)
  572. HNEW = ABSH*MAX(0.5D0,MIN(0.9D0,R))
  573. HNEW = SIGN(MAX(HNEW,FOURU*ABS(X)),H)
  574. 465 H = HNEW
  575. RETURN
  576. C *** END BLOCK 4 ***
  577. END