sdriv3.f 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. *DECK SDRIV3
  2. SUBROUTINE SDRIV3 (N, T, Y, F, NSTATE, TOUT, NTASK, NROOT, EPS,
  3. 8 EWT, IERROR, MINT, MITER, IMPL, ML, MU, MXORD, HMAX, WORK,
  4. 8 LENW, IWORK, LENIW, JACOBN, FA, NDE, MXSTEP, G, USERS, IERFLG)
  5. C***BEGIN PROLOGUE SDRIV3
  6. C***PURPOSE The function of SDRIV3 is to solve N ordinary differential
  7. C equations of the form dY(I)/dT = F(Y(I),T), given the
  8. C initial conditions Y(I) = YI. The program has options to
  9. C allow the solution of both stiff and non-stiff differential
  10. C equations. Other important options are available. SDRIV3
  11. C uses single precision arithmetic.
  12. C***LIBRARY SLATEC (SDRIVE)
  13. C***CATEGORY I1A2, I1A1B
  14. C***TYPE SINGLE PRECISION (SDRIV3-S, DDRIV3-D, CDRIV3-C)
  15. C***KEYWORDS GEAR'S METHOD, INITIAL VALUE PROBLEMS, ODE,
  16. C ORDINARY DIFFERENTIAL EQUATIONS, SDRIVE, SINGLE PRECISION,
  17. C STIFF
  18. C***AUTHOR Kahaner, D. K., (NIST)
  19. C National Institute of Standards and Technology
  20. C Gaithersburg, MD 20899
  21. C Sutherland, C. D., (LANL)
  22. C Mail Stop D466
  23. C Los Alamos National Laboratory
  24. C Los Alamos, NM 87545
  25. C***DESCRIPTION
  26. C
  27. C I. ABSTRACT .......................................................
  28. C
  29. C The primary function of SDRIV3 is to solve N ordinary differential
  30. C equations of the form dY(I)/dT = F(Y(I),T), given the initial
  31. C conditions Y(I) = YI. The program has options to allow the
  32. C solution of both stiff and non-stiff differential equations. In
  33. C addition, SDRIV3 may be used to solve:
  34. C 1. The initial value problem, A*dY(I)/dT = F(Y(I),T), where A is
  35. C a non-singular matrix depending on Y and T.
  36. C 2. The hybrid differential/algebraic initial value problem,
  37. C A*dY(I)/dT = F(Y(I),T), where A is a vector (whose values may
  38. C depend upon Y and T) some of whose components will be zero
  39. C corresponding to those equations which are algebraic rather
  40. C than differential.
  41. C SDRIV3 is to be called once for each output point of T.
  42. C
  43. C II. PARAMETERS ....................................................
  44. C
  45. C The user should use parameter names in the call sequence of SDRIV3
  46. C for those quantities whose value may be altered by SDRIV3. The
  47. C parameters in the call sequence are:
  48. C
  49. C N = (Input) The number of dependent functions whose solution
  50. C is desired. N must not be altered during a problem.
  51. C
  52. C T = The independent variable. On input for the first call, T
  53. C is the initial point. On output, T is the point at which
  54. C the solution is given.
  55. C
  56. C Y = The vector of dependent variables. Y is used as input on
  57. C the first call, to set the initial values. On output, Y
  58. C is the computed solution vector. This array Y is passed
  59. C in the call sequence of the user-provided routines F,
  60. C JACOBN, FA, USERS, and G. Thus parameters required by
  61. C those routines can be stored in this array in components
  62. C N+1 and above. (Note: Changes by the user to the first
  63. C N components of this array will take effect only after a
  64. C restart, i.e., after setting NSTATE to 1 .)
  65. C
  66. C F = A subroutine supplied by the user. The name must be
  67. C declared EXTERNAL in the user's calling program. This
  68. C subroutine is of the form:
  69. C SUBROUTINE F (N, T, Y, YDOT)
  70. C REAL Y(*), YDOT(*)
  71. C .
  72. C .
  73. C YDOT(1) = ...
  74. C .
  75. C .
  76. C YDOT(N) = ...
  77. C END (Sample)
  78. C This computes YDOT = F(Y,T), the right hand side of the
  79. C differential equations. Here Y is a vector of length at
  80. C least N. The actual length of Y is determined by the
  81. C user's declaration in the program which calls SDRIV3.
  82. C Thus the dimensioning of Y in F, while required by FORTRAN
  83. C convention, does not actually allocate any storage. When
  84. C this subroutine is called, the first N components of Y are
  85. C intermediate approximations to the solution components.
  86. C The user should not alter these values. Here YDOT is a
  87. C vector of length N. The user should only compute YDOT(I)
  88. C for I from 1 to N. Normally a return from F passes
  89. C control back to SDRIV3. However, if the user would like
  90. C to abort the calculation, i.e., return control to the
  91. C program which calls SDRIV3, he should set N to zero.
  92. C SDRIV3 will signal this by returning a value of NSTATE
  93. C equal to 6 . Altering the value of N in F has no effect
  94. C on the value of N in the call sequence of SDRIV3.
  95. C
  96. C NSTATE = An integer describing the status of integration. The
  97. C meaning of NSTATE is as follows:
  98. C 1 (Input) Means the first call to the routine. This
  99. C value must be set by the user. On all subsequent
  100. C calls the value of NSTATE should be tested by the
  101. C user, but must not be altered. (As a convenience to
  102. C the user who may wish to put out the initial
  103. C conditions, SDRIV3 can be called with NSTATE=1, and
  104. C TOUT=T. In this case the program will return with
  105. C NSTATE unchanged, i.e., NSTATE=1.)
  106. C 2 (Output) Means a successful integration. If a normal
  107. C continuation is desired (i.e., a further integration
  108. C in the same direction), simply advance TOUT and call
  109. C again. All other parameters are automatically set.
  110. C 3 (Output)(Unsuccessful) Means the integrator has taken
  111. C MXSTEP steps without reaching TOUT. The user can
  112. C continue the integration by simply calling SDRIV3
  113. C again.
  114. C 4 (Output)(Unsuccessful) Means too much accuracy has
  115. C been requested. EPS has been increased to a value
  116. C the program estimates is appropriate. The user can
  117. C continue the integration by simply calling SDRIV3
  118. C again.
  119. C 5 (Output) A root was found at a point less than TOUT.
  120. C The user can continue the integration toward TOUT by
  121. C simply calling SDRIV3 again.
  122. C 6 (Output)(Unsuccessful) N has been set to zero in
  123. C SUBROUTINE F.
  124. C 7 (Output)(Unsuccessful) N has been set to zero in
  125. C FUNCTION G. See description of G below.
  126. C 8 (Output)(Unsuccessful) N has been set to zero in
  127. C SUBROUTINE JACOBN. See description of JACOBN below.
  128. C 9 (Output)(Unsuccessful) N has been set to zero in
  129. C SUBROUTINE FA. See description of FA below.
  130. C 10 (Output)(Unsuccessful) N has been set to zero in
  131. C SUBROUTINE USERS. See description of USERS below.
  132. C 11 (Output)(Successful) For NTASK = 2 or 3, T is beyond
  133. C TOUT. The solution was obtained by interpolation.
  134. C The user can continue the integration by simply
  135. C advancing TOUT and calling SDRIV3 again.
  136. C 12 (Output)(Unsuccessful) The solution could not be
  137. C obtained. The value of IERFLG (see description
  138. C below) for a "Recoverable" situation indicates the
  139. C type of difficulty encountered: either an illegal
  140. C value for a parameter or an inability to continue the
  141. C solution. For this condition the user should take
  142. C corrective action and reset NSTATE to 1 before
  143. C calling SDRIV3 again. Otherwise the program will
  144. C terminate the run.
  145. C
  146. C TOUT = (Input) The point at which the solution is desired. The
  147. C position of TOUT relative to T on the first call
  148. C determines the direction of integration.
  149. C
  150. C NTASK = (Input) An index specifying the manner of returning the
  151. C solution, according to the following:
  152. C NTASK = 1 Means SDRIV3 will integrate past TOUT and
  153. C interpolate the solution. This is the most
  154. C efficient mode.
  155. C NTASK = 2 Means SDRIV3 will return the solution after
  156. C each internal integration step, or at TOUT,
  157. C whichever comes first. In the latter case,
  158. C the program integrates exactly to TOUT.
  159. C NTASK = 3 Means SDRIV3 will adjust its internal step to
  160. C reach TOUT exactly (useful if a singularity
  161. C exists beyond TOUT.)
  162. C
  163. C NROOT = (Input) The number of equations whose roots are desired.
  164. C If NROOT is zero, the root search is not active. This
  165. C option is useful for obtaining output at points which are
  166. C not known in advance, but depend upon the solution, e.g.,
  167. C when some solution component takes on a specified value.
  168. C The root search is carried out using the user-written
  169. C function G (see description of G below.) SDRIV3 attempts
  170. C to find the value of T at which one of the equations
  171. C changes sign. SDRIV3 can find at most one root per
  172. C equation per internal integration step, and will then
  173. C return the solution either at TOUT or at a root, whichever
  174. C occurs first in the direction of integration. The initial
  175. C point is never reported as a root. The index of the
  176. C equation whose root is being reported is stored in the
  177. C sixth element of IWORK.
  178. C NOTE: NROOT is never altered by this program.
  179. C
  180. C EPS = On input, the requested relative accuracy in all solution
  181. C components. EPS = 0 is allowed. On output, the adjusted
  182. C relative accuracy if the input value was too small. The
  183. C value of EPS should be set as large as is reasonable,
  184. C because the amount of work done by SDRIV3 increases as EPS
  185. C decreases.
  186. C
  187. C EWT = (Input) Problem zero, i.e., the smallest, nonzero,
  188. C physically meaningful value for the solution. (Array,
  189. C possibly of length one. See following description of
  190. C IERROR.) Setting EWT smaller than necessary can adversely
  191. C affect the running time.
  192. C
  193. C IERROR = (Input) Error control indicator. A value of 3 is
  194. C suggested for most problems. Other choices and detailed
  195. C explanations of EWT and IERROR are given below for those
  196. C who may need extra flexibility.
  197. C
  198. C These last three input quantities EPS, EWT and IERROR
  199. C control the accuracy of the computed solution. EWT and
  200. C IERROR are used internally to compute an array YWT. One
  201. C step error estimates divided by YWT(I) are kept less than
  202. C EPS in root mean square norm.
  203. C IERROR (Set by the user) =
  204. C 1 Means YWT(I) = 1. (Absolute error control)
  205. C EWT is ignored.
  206. C 2 Means YWT(I) = ABS(Y(I)), (Relative error control)
  207. C EWT is ignored.
  208. C 3 Means YWT(I) = MAX(ABS(Y(I)), EWT(1)).
  209. C 4 Means YWT(I) = MAX(ABS(Y(I)), EWT(I)).
  210. C This choice is useful when the solution components
  211. C have differing scales.
  212. C 5 Means YWT(I) = EWT(I).
  213. C If IERROR is 3, EWT need only be dimensioned one.
  214. C If IERROR is 4 or 5, the user must dimension EWT at least
  215. C N, and set its values.
  216. C
  217. C MINT = (Input) The integration method indicator.
  218. C MINT = 1 Means the Adams methods, and is used for
  219. C non-stiff problems.
  220. C MINT = 2 Means the stiff methods of Gear (i.e., the
  221. C backward differentiation formulas), and is
  222. C used for stiff problems.
  223. C MINT = 3 Means the program dynamically selects the
  224. C Adams methods when the problem is non-stiff
  225. C and the Gear methods when the problem is
  226. C stiff. When using the Adams methods, the
  227. C program uses a value of MITER=0; when using
  228. C the Gear methods, the program uses the value
  229. C of MITER provided by the user. Only a value
  230. C of IMPL = 0 and a value of MITER = 1, 2, 4, or
  231. C 5 is allowed for this option. The user may
  232. C not alter the value of MINT or MITER without
  233. C restarting, i.e., setting NSTATE to 1.
  234. C
  235. C MITER = (Input) The iteration method indicator.
  236. C MITER = 0 Means functional iteration. This value is
  237. C suggested for non-stiff problems.
  238. C MITER = 1 Means chord method with analytic Jacobian.
  239. C In this case, the user supplies subroutine
  240. C JACOBN (see description below).
  241. C MITER = 2 Means chord method with Jacobian calculated
  242. C internally by finite differences.
  243. C MITER = 3 Means chord method with corrections computed
  244. C by the user-written routine USERS (see
  245. C description of USERS below.) This option
  246. C allows all matrix algebra and storage
  247. C decisions to be made by the user. When using
  248. C a value of MITER = 3, the subroutine FA is
  249. C not required, even if IMPL is not 0. For
  250. C further information on using this option, see
  251. C Section IV-E below.
  252. C MITER = 4 Means the same as MITER = 1 but the A and
  253. C Jacobian matrices are assumed to be banded.
  254. C MITER = 5 Means the same as MITER = 2 but the A and
  255. C Jacobian matrices are assumed to be banded.
  256. C
  257. C IMPL = (Input) The implicit method indicator.
  258. C IMPL = 0 Means solving dY(I)/dT = F(Y(I),T).
  259. C IMPL = 1 Means solving A*dY(I)/dT = F(Y(I),T), non-
  260. C singular A (see description of FA below.)
  261. C Only MINT = 1 or 2, and MITER = 1, 2, 3, 4,
  262. C or 5 are allowed for this option.
  263. C IMPL = 2,3 Means solving certain systems of hybrid
  264. C differential/algebraic equations (see
  265. C description of FA below.) Only MINT = 2 and
  266. C MITER = 1, 2, 3, 4, or 5, are allowed for
  267. C this option.
  268. C The value of IMPL must not be changed during a problem.
  269. C
  270. C ML = (Input) The lower half-bandwidth in the case of a banded
  271. C A or Jacobian matrix. (I.e., maximum(R-C) for nonzero
  272. C A(R,C).)
  273. C
  274. C MU = (Input) The upper half-bandwidth in the case of a banded
  275. C A or Jacobian matrix. (I.e., maximum(C-R).)
  276. C
  277. C MXORD = (Input) The maximum order desired. This is .LE. 12 for
  278. C the Adams methods and .LE. 5 for the Gear methods. Normal
  279. C value is 12 and 5, respectively. If MINT is 3, the
  280. C maximum order used will be MIN(MXORD, 12) when using the
  281. C Adams methods, and MIN(MXORD, 5) when using the Gear
  282. C methods. MXORD must not be altered during a problem.
  283. C
  284. C HMAX = (Input) The maximum magnitude of the step size that will
  285. C be used for the problem. This is useful for ensuring that
  286. C important details are not missed. If this is not the
  287. C case, a large value, such as the interval length, is
  288. C suggested.
  289. C
  290. C WORK
  291. C LENW = (Input)
  292. C WORK is an array of LENW real words used
  293. C internally for temporary storage. The user must allocate
  294. C space for this array in the calling program by a statement
  295. C such as
  296. C REAL WORK(...)
  297. C The following table gives the required minimum value for
  298. C the length of WORK, depending on the value of IMPL and
  299. C MITER. LENW should be set to the value used. The
  300. C contents of WORK should not be disturbed between calls to
  301. C SDRIV3.
  302. C
  303. C IMPL = 0 1 2 3
  304. C ---------------------------------------------------------
  305. C MITER = 0 (MXORD+4)*N Not allowed Not allowed Not allowed
  306. C + 2*NROOT
  307. C + 250
  308. C
  309. C 1,2 N*N + 2*N*N + N*N + N*(N + NDE)
  310. C (MXORD+5)*N (MXORD+5)*N (MXORD+6)*N + (MXORD+5)*N
  311. C + 2*NROOT + 2*NROOT + 2*NROOT + 2*NROOT
  312. C + 250 + 250 + 250 + 250
  313. C
  314. C 3 (MXORD+4)*N (MXORD+4)*N (MXORD+4)*N (MXORD+4)*N
  315. C + 2*NROOT + 2*NROOT + 2*NROOT + 2*NROOT
  316. C + 250 + 250 + 250 + 250
  317. C
  318. C 4,5 (2*ML+MU+1) 2*(2*ML+MU+1) (2*ML+MU+1) (2*ML+MU+1)*
  319. C *N + *N + *N + (N+NDE) +
  320. C (MXORD+5)*N (MXORD+5)*N (MXORD+6)*N + (MXORD+5)*N
  321. C + 2*NROOT + 2*NROOT + 2*NROOT + 2*NROOT
  322. C + 250 + 250 + 250 + 250
  323. C ---------------------------------------------------------
  324. C
  325. C IWORK
  326. C LENIW = (Input)
  327. C IWORK is an integer array of length LENIW used internally
  328. C for temporary storage. The user must allocate space for
  329. C this array in the calling program by a statement such as
  330. C INTEGER IWORK(...)
  331. C The length of IWORK should be at least
  332. C 50 if MITER is 0 or 3, or
  333. C N+50 if MITER is 1, 2, 4, or 5, or MINT is 3,
  334. C and LENIW should be set to the value used. The contents
  335. C of IWORK should not be disturbed between calls to SDRIV3.
  336. C
  337. C JACOBN = A subroutine supplied by the user, if MITER is 1 or 4.
  338. C If this is the case, the name must be declared EXTERNAL in
  339. C the user's calling program. Given a system of N
  340. C differential equations, it is meaningful to speak about
  341. C the partial derivative of the I-th right hand side with
  342. C respect to the J-th dependent variable. In general there
  343. C are N*N such quantities. Often however the equations can
  344. C be ordered so that the I-th differential equation only
  345. C involves dependent variables with index near I, e.g., I+1,
  346. C I-2. Such a system is called banded. If, for all I, the
  347. C I-th equation depends on at most the variables
  348. C Y(I-ML), Y(I-ML+1), ... , Y(I), Y(I+1), ... , Y(I+MU)
  349. C then we call ML+MU+1 the bandwidth of the system. In a
  350. C banded system many of the partial derivatives above are
  351. C automatically zero. For the cases MITER = 1, 2, 4, and 5,
  352. C some of these partials are needed. For the cases
  353. C MITER = 2 and 5 the necessary derivatives are
  354. C approximated numerically by SDRIV3, and we only ask the
  355. C user to tell SDRIV3 the value of ML and MU if the system
  356. C is banded. For the cases MITER = 1 and 4 the user must
  357. C derive these partials algebraically and encode them in
  358. C subroutine JACOBN. By computing these derivatives the
  359. C user can often save 20-30 per cent of the computing time.
  360. C Usually, however, the accuracy is not much affected and
  361. C most users will probably forego this option. The optional
  362. C user-written subroutine JACOBN has the form:
  363. C SUBROUTINE JACOBN (N, T, Y, DFDY, MATDIM, ML, MU)
  364. C REAL Y(*), DFDY(MATDIM,*)
  365. C .
  366. C .
  367. C Calculate values of DFDY
  368. C .
  369. C .
  370. C END (Sample)
  371. C Here Y is a vector of length at least N. The actual
  372. C length of Y is determined by the user's declaration in the
  373. C program which calls SDRIV3. Thus the dimensioning of Y in
  374. C JACOBN, while required by FORTRAN convention, does not
  375. C actually allocate any storage. When this subroutine is
  376. C called, the first N components of Y are intermediate
  377. C approximations to the solution components. The user
  378. C should not alter these values. If the system is not
  379. C banded (MITER=1), the partials of the I-th equation with
  380. C respect to the J-th dependent function are to be stored in
  381. C DFDY(I,J). Thus partials of the I-th equation are stored
  382. C in the I-th row of DFDY. If the system is banded
  383. C (MITER=4), then the partials of the I-th equation with
  384. C respect to Y(J) are to be stored in DFDY(K,J), where
  385. C K=I-J+MU+1 . Normally a return from JACOBN passes control
  386. C back to SDRIV3. However, if the user would like to abort
  387. C the calculation, i.e., return control to the program which
  388. C calls SDRIV3, he should set N to zero. SDRIV3 will signal
  389. C this by returning a value of NSTATE equal to +8(-8).
  390. C Altering the value of N in JACOBN has no effect on the
  391. C value of N in the call sequence of SDRIV3.
  392. C
  393. C FA = A subroutine supplied by the user if IMPL is not zero, and
  394. C MITER is not 3. If so, the name must be declared EXTERNAL
  395. C in the user's calling program. This subroutine computes
  396. C the array A, where A*dY(I)/dT = F(Y(I),T).
  397. C There are three cases:
  398. C
  399. C IMPL=1.
  400. C Subroutine FA is of the form:
  401. C SUBROUTINE FA (N, T, Y, A, MATDIM, ML, MU, NDE)
  402. C REAL Y(*), A(MATDIM,*)
  403. C .
  404. C .
  405. C Calculate ALL values of A
  406. C .
  407. C .
  408. C END (Sample)
  409. C In this case A is assumed to be a nonsingular matrix,
  410. C with the same structure as DFDY (see JACOBN description
  411. C above). Programming considerations prevent complete
  412. C generality. If MITER is 1 or 2, A is assumed to be full
  413. C and the user must compute and store all values of
  414. C A(I,J), I,J=1, ... ,N. If MITER is 4 or 5, A is assumed
  415. C to be banded with lower and upper half bandwidth ML and
  416. C MU. The left hand side of the I-th equation is a linear
  417. C combination of dY(I-ML)/dT, dY(I-ML+1)/dT, ... ,
  418. C dY(I)/dT, ... , dY(I+MU-1)/dT, dY(I+MU)/dT. Thus in the
  419. C I-th equation, the coefficient of dY(J)/dT is to be
  420. C stored in A(K,J), where K=I-J+MU+1.
  421. C NOTE: The array A will be altered between calls to FA.
  422. C
  423. C IMPL=2.
  424. C Subroutine FA is of the form:
  425. C SUBROUTINE FA (N, T, Y, A, MATDIM, ML, MU, NDE)
  426. C REAL Y(*), A(*)
  427. C .
  428. C .
  429. C Calculate non-zero values of A(1),...,A(NDE)
  430. C .
  431. C .
  432. C END (Sample)
  433. C In this case it is assumed that the system is ordered by
  434. C the user so that the differential equations appear
  435. C first, and the algebraic equations appear last. The
  436. C algebraic equations must be written in the form:
  437. C 0 = F(Y(I),T). When using this option it is up to the
  438. C user to provide initial values for the Y(I) that satisfy
  439. C the algebraic equations as well as possible. It is
  440. C further assumed that A is a vector of length NDE. All
  441. C of the components of A, which may depend on T, Y(I),
  442. C etc., must be set by the user to non-zero values.
  443. C
  444. C IMPL=3.
  445. C Subroutine FA is of the form:
  446. C SUBROUTINE FA (N, T, Y, A, MATDIM, ML, MU, NDE)
  447. C REAL Y(*), A(MATDIM,*)
  448. C .
  449. C .
  450. C Calculate ALL values of A
  451. C .
  452. C .
  453. C END (Sample)
  454. C In this case A is assumed to be a nonsingular NDE by NDE
  455. C matrix with the same structure as DFDY (see JACOBN
  456. C description above). Programming considerations prevent
  457. C complete generality. If MITER is 1 or 2, A is assumed
  458. C to be full and the user must compute and store all
  459. C values of A(I,J), I,J=1, ... ,NDE. If MITER is 4 or 5,
  460. C A is assumed to be banded with lower and upper half
  461. C bandwidths ML and MU. The left hand side of the I-th
  462. C equation is a linear combination of dY(I-ML)/dT,
  463. C dY(I-ML+1)/dT, ... , dY(I)/dT, ... , dY(I+MU-1)/dT,
  464. C dY(I+MU)/dT. Thus in the I-th equation, the coefficient
  465. C of dY(J)/dT is to be stored in A(K,J), where K=I-J+MU+1.
  466. C It is assumed that the system is ordered by the user so
  467. C that the differential equations appear first, and the
  468. C algebraic equations appear last. The algebraic
  469. C equations must be written in the form 0 = F(Y(I),T).
  470. C When using this option it is up to the user to provide
  471. C initial values for the Y(I) that satisfy the algebraic
  472. C equations as well as possible.
  473. C NOTE: For IMPL = 3, the array A will be altered between
  474. C calls to FA.
  475. C Here Y is a vector of length at least N. The actual
  476. C length of Y is determined by the user's declaration in the
  477. C program which calls SDRIV3. Thus the dimensioning of Y in
  478. C FA, while required by FORTRAN convention, does not
  479. C actually allocate any storage. When this subroutine is
  480. C called, the first N components of Y are intermediate
  481. C approximations to the solution components. The user
  482. C should not alter these values. FA is always called
  483. C immediately after calling F, with the same values of T
  484. C and Y. Normally a return from FA passes control back to
  485. C SDRIV3. However, if the user would like to abort the
  486. C calculation, i.e., return control to the program which
  487. C calls SDRIV3, he should set N to zero. SDRIV3 will signal
  488. C this by returning a value of NSTATE equal to +9(-9).
  489. C Altering the value of N in FA has no effect on the value
  490. C of N in the call sequence of SDRIV3.
  491. C
  492. C NDE = (Input) The number of differential equations. This is
  493. C required only for IMPL = 2 or 3, with NDE .LT. N.
  494. C
  495. C MXSTEP = (Input) The maximum number of internal steps allowed on
  496. C one call to SDRIV3.
  497. C
  498. C G = A real FORTRAN function supplied by the user
  499. C if NROOT is not 0. In this case, the name must be
  500. C declared EXTERNAL in the user's calling program. G is
  501. C repeatedly called with different values of IROOT to obtain
  502. C the value of each of the NROOT equations for which a root
  503. C is desired. G is of the form:
  504. C REAL FUNCTION G (N, T, Y, IROOT)
  505. C REAL Y(*)
  506. C GO TO (10, ...), IROOT
  507. C 10 G = ...
  508. C .
  509. C .
  510. C END (Sample)
  511. C Here, Y is a vector of length at least N, whose first N
  512. C components are the solution components at the point T.
  513. C The user should not alter these values. The actual length
  514. C of Y is determined by the user's declaration in the
  515. C program which calls SDRIV3. Thus the dimensioning of Y in
  516. C G, while required by FORTRAN convention, does not actually
  517. C allocate any storage. Normally a return from G passes
  518. C control back to SDRIV3. However, if the user would like
  519. C to abort the calculation, i.e., return control to the
  520. C program which calls SDRIV3, he should set N to zero.
  521. C SDRIV3 will signal this by returning a value of NSTATE
  522. C equal to +7(-7). In this case, the index of the equation
  523. C being evaluated is stored in the sixth element of IWORK.
  524. C Altering the value of N in G has no effect on the value of
  525. C N in the call sequence of SDRIV3.
  526. C
  527. C USERS = A subroutine supplied by the user, if MITER is 3.
  528. C If this is the case, the name must be declared EXTERNAL in
  529. C the user's calling program. The routine USERS is called
  530. C by SDRIV3 when certain linear systems must be solved. The
  531. C user may choose any method to form, store and solve these
  532. C systems in order to obtain the solution result that is
  533. C returned to SDRIV3. In particular, this allows sparse
  534. C matrix methods to be used. The call sequence for this
  535. C routine is:
  536. C
  537. C SUBROUTINE USERS (Y, YH, YWT, SAVE1, SAVE2, T, H, EL,
  538. C 8 IMPL, N, NDE, IFLAG)
  539. C REAL Y(*), YH(*), YWT(*), SAVE1(*),
  540. C 8 SAVE2(*), T, H, EL
  541. C
  542. C The input variable IFLAG indicates what action is to be
  543. C taken. Subroutine USERS should perform the following
  544. C operations, depending on the value of IFLAG and IMPL.
  545. C
  546. C IFLAG = 0
  547. C IMPL = 0. USERS is not called.
  548. C IMPL = 1, 2 or 3. Solve the system A*X = SAVE2,
  549. C returning the result in SAVE2. The array SAVE1 can
  550. C be used as a work array. For IMPL = 1, there are N
  551. C components to the system, and for IMPL = 2 or 3,
  552. C there are NDE components to the system.
  553. C
  554. C IFLAG = 1
  555. C IMPL = 0. Compute, decompose and store the matrix
  556. C (I - H*EL*J), where I is the identity matrix and J
  557. C is the Jacobian matrix of the right hand side. The
  558. C array SAVE1 can be used as a work array.
  559. C IMPL = 1, 2 or 3. Compute, decompose and store the
  560. C matrix (A - H*EL*J). The array SAVE1 can be used as
  561. C a work array.
  562. C
  563. C IFLAG = 2
  564. C IMPL = 0. Solve the system
  565. C (I - H*EL*J)*X = H*SAVE2 - YH - SAVE1,
  566. C returning the result in SAVE2.
  567. C IMPL = 1, 2 or 3. Solve the system
  568. C (A - H*EL*J)*X = H*SAVE2 - A*(YH + SAVE1)
  569. C returning the result in SAVE2.
  570. C The array SAVE1 should not be altered.
  571. C If IFLAG is 0 and IMPL is 1 or 2 and the matrix A is
  572. C singular, or if IFLAG is 1 and one of the matrices
  573. C (I - H*EL*J), (A - H*EL*J) is singular, the INTEGER
  574. C variable IFLAG is to be set to -1 before RETURNing.
  575. C Normally a return from USERS passes control back to
  576. C SDRIV3. However, if the user would like to abort the
  577. C calculation, i.e., return control to the program which
  578. C calls SDRIV3, he should set N to zero. SDRIV3 will signal
  579. C this by returning a value of NSTATE equal to +10(-10).
  580. C Altering the value of N in USERS has no effect on the
  581. C value of N in the call sequence of SDRIV3.
  582. C
  583. C IERFLG = An error flag. The error number associated with a
  584. C diagnostic message (see Section III-A below) is the same
  585. C as the corresponding value of IERFLG. The meaning of
  586. C IERFLG:
  587. C 0 The routine completed successfully. (No message is
  588. C issued.)
  589. C 3 (Warning) The number of steps required to reach TOUT
  590. C exceeds MXSTEP.
  591. C 4 (Warning) The value of EPS is too small.
  592. C 11 (Warning) For NTASK = 2 or 3, T is beyond TOUT.
  593. C The solution was obtained by interpolation.
  594. C 15 (Warning) The integration step size is below the
  595. C roundoff level of T. (The program issues this
  596. C message as a warning but does not return control to
  597. C the user.)
  598. C 22 (Recoverable) N is not positive.
  599. C 23 (Recoverable) MINT is less than 1 or greater than 3 .
  600. C 24 (Recoverable) MITER is less than 0 or greater than
  601. C 5 .
  602. C 25 (Recoverable) IMPL is less than 0 or greater than 3 .
  603. C 26 (Recoverable) The value of NSTATE is less than 1 or
  604. C greater than 12 .
  605. C 27 (Recoverable) EPS is less than zero.
  606. C 28 (Recoverable) MXORD is not positive.
  607. C 29 (Recoverable) For MINT = 3, either MITER = 0 or 3, or
  608. C IMPL = 0 .
  609. C 30 (Recoverable) For MITER = 0, IMPL is not 0 .
  610. C 31 (Recoverable) For MINT = 1, IMPL is 2 or 3 .
  611. C 32 (Recoverable) Insufficient storage has been allocated
  612. C for the WORK array.
  613. C 33 (Recoverable) Insufficient storage has been allocated
  614. C for the IWORK array.
  615. C 41 (Recoverable) The integration step size has gone
  616. C to zero.
  617. C 42 (Recoverable) The integration step size has been
  618. C reduced about 50 times without advancing the
  619. C solution. The problem setup may not be correct.
  620. C 43 (Recoverable) For IMPL greater than 0, the matrix A
  621. C is singular.
  622. C 999 (Fatal) The value of NSTATE is 12 .
  623. C
  624. C III. OTHER COMMUNICATION TO THE USER ..............................
  625. C
  626. C A. The solver communicates to the user through the parameters
  627. C above. In addition it writes diagnostic messages through the
  628. C standard error handling program XERMSG. A complete description
  629. C of XERMSG is given in "Guide to the SLATEC Common Mathematical
  630. C Library" by Kirby W. Fong et al.. At installations which do not
  631. C have this error handling package the short but serviceable
  632. C routine, XERMSG, available with this package, can be used. That
  633. C program uses the file named OUTPUT to transmit messages.
  634. C
  635. C B. The first three elements of WORK and the first five elements of
  636. C IWORK will contain the following statistical data:
  637. C AVGH The average step size used.
  638. C HUSED The step size last used (successfully).
  639. C AVGORD The average order used.
  640. C IMXERR The index of the element of the solution vector that
  641. C contributed most to the last error test.
  642. C NQUSED The order last used (successfully).
  643. C NSTEP The number of steps taken since last initialization.
  644. C NFE The number of evaluations of the right hand side.
  645. C NJE The number of evaluations of the Jacobian matrix.
  646. C
  647. C IV. REMARKS .......................................................
  648. C
  649. C A. Other routines used:
  650. C SDNTP, SDZRO, SDSTP, SDNTL, SDPST, SDCOR, SDCST,
  651. C SDPSC, and SDSCL;
  652. C SGEFA, SGESL, SGBFA, SGBSL, and SNRM2 (from LINPACK)
  653. C R1MACH (from the Bell Laboratories Machine Constants Package)
  654. C XERMSG (from the SLATEC Common Math Library)
  655. C The last seven routines above, not having been written by the
  656. C present authors, are not explicitly part of this package.
  657. C
  658. C B. On any return from SDRIV3 all information necessary to continue
  659. C the calculation is contained in the call sequence parameters,
  660. C including the work arrays. Thus it is possible to suspend one
  661. C problem, integrate another, and then return to the first.
  662. C
  663. C C. If this package is to be used in an overlay situation, the user
  664. C must declare in the primary overlay the variables in the call
  665. C sequence to SDRIV3.
  666. C
  667. C D. Changing parameters during an integration.
  668. C The value of NROOT, EPS, EWT, IERROR, MINT, MITER, or HMAX may
  669. C be altered by the user between calls to SDRIV3. For example, if
  670. C too much accuracy has been requested (the program returns with
  671. C NSTATE = 4 and an increased value of EPS) the user may wish to
  672. C increase EPS further. In general, prudence is necessary when
  673. C making changes in parameters since such changes are not
  674. C implemented until the next integration step, which is not
  675. C necessarily the next call to SDRIV3. This can happen if the
  676. C program has already integrated to a point which is beyond the
  677. C new point TOUT.
  678. C
  679. C E. As the price for complete control of matrix algebra, the SDRIV3
  680. C USERS option puts all responsibility for Jacobian matrix
  681. C evaluation on the user. It is often useful to approximate
  682. C numerically all or part of the Jacobian matrix. However this
  683. C must be done carefully. The FORTRAN sequence below illustrates
  684. C the method we recommend. It can be inserted directly into
  685. C subroutine USERS to approximate Jacobian elements in rows I1
  686. C to I2 and columns J1 to J2.
  687. C REAL DFDY(N,N), EPSJ, H, R, R1MACH,
  688. C 8 SAVE1(N), SAVE2(N), T, UROUND, Y(N), YJ, YWT(N)
  689. C UROUND = R1MACH(4)
  690. C EPSJ = SQRT(UROUND)
  691. C DO 30 J = J1,J2
  692. C R = EPSJ*MAX(ABS(YWT(J)), ABS(Y(J)))
  693. C IF (R .EQ. 0.E0) R = YWT(J)
  694. C YJ = Y(J)
  695. C Y(J) = Y(J) + R
  696. C CALL F (N, T, Y, SAVE1)
  697. C IF (N .EQ. 0) RETURN
  698. C Y(J) = YJ
  699. C DO 20 I = I1,I2
  700. C 20 DFDY(I,J) = (SAVE1(I) - SAVE2(I))/R
  701. C 30 CONTINUE
  702. C Many problems give rise to structured sparse Jacobians, e.g.,
  703. C block banded. It is possible to approximate them with fewer
  704. C function evaluations than the above procedure uses; see Curtis,
  705. C Powell and Reid, J. Inst. Maths Applics, (1974), Vol. 13,
  706. C pp. 117-119.
  707. C
  708. C F. When any of the routines JACOBN, FA, G, or USERS, is not
  709. C required, difficulties associated with unsatisfied externals can
  710. C be avoided by using the name of the routine which calculates the
  711. C right hand side of the differential equations in place of the
  712. C corresponding name in the call sequence of SDRIV3.
  713. C
  714. C***REFERENCES C. W. Gear, Numerical Initial Value Problems in
  715. C Ordinary Differential Equations, Prentice-Hall, 1971.
  716. C***ROUTINES CALLED R1MACH, SDNTP, SDSTP, SDZRO, SGBFA, SGBSL, SGEFA,
  717. C SGESL, SNRM2, XERMSG
  718. C***REVISION HISTORY (YYMMDD)
  719. C 790601 DATE WRITTEN
  720. C 900329 Initial submission to SLATEC.
  721. C***END PROLOGUE SDRIV3
  722. EXTERNAL F, JACOBN, FA, G, USERS
  723. REAL AE, BIG, EPS, EWT(*), G, GLAST, GNOW, H, HMAX,
  724. 8 HSIGN, HUSED, NROUND, RE, R1MACH, SIZE, SNRM2, SUM, T, TLAST,
  725. 8 TOUT, TROOT, UROUND, WORK(*), Y(*)
  726. INTEGER I, IA, IAVGH, IAVGRD, ICNVRG, IDFDY, IEL, IERFLG, IERROR,
  727. 8 IFAC, IFLAG, IGNOW, IH, IHMAX, IHOLD, IHSIGN, IHUSED,
  728. 8 IJROOT, IJSTPL, IJTASK, IMNT, IMNTLD, IMPL, IMTR, IMTRLD,
  729. 8 IMTRSV, IMXERR, IMXORD, IMXRDS, INDMXR, INDPRT, INDPVT,
  730. 8 INDTRT, INFE, INFO, INJE, INQ, INQUSE, INROOT, INRTLD,
  731. 8 INSTEP, INWAIT, IRC, IRMAX, IROOT, IMACH1, IMACH4, ISAVE1,
  732. 8 ISAVE2, IT, ITOUT, ITQ, ITREND, ITROOT, IWORK(*), IYH,
  733. 8 IYWT, J, JSTATE, JTROOT, LENCHK, LENIW, LENW, LIWCHK,
  734. 8 MATDIM, MAXORD, MINT, MITER, ML, MU, MXORD, MXSTEP, N,
  735. 8 NDE, NDECOM, NPAR, NROOT, NSTATE, NSTEPL, NTASK
  736. LOGICAL CONVRG
  737. CHARACTER INTGR1*8, INTGR2*8, RL1*16, RL2*16
  738. PARAMETER(NROUND = 20.E0)
  739. PARAMETER(IAVGH = 1, IHUSED = 2, IAVGRD = 3,
  740. 8 IEL = 4, IH = 160, IHMAX = 161, IHOLD = 162,
  741. 8 IHSIGN = 163, IRC = 164, IRMAX = 165, IT = 166,
  742. 8 ITOUT = 167, ITQ = 168, ITREND = 204, IMACH1 = 205,
  743. 8 IMACH4 = 206, IYH = 251,
  744. 8 INDMXR = 1, INQUSE = 2, INSTEP = 3, INFE = 4, INJE = 5,
  745. 8 INROOT = 6, ICNVRG = 7, IJROOT = 8, IJTASK = 9,
  746. 8 IMNTLD = 10, IMTRLD = 11, INQ = 12, INRTLD = 13,
  747. 8 INDTRT = 14, INWAIT = 15, IMNT = 16, IMTRSV = 17,
  748. 8 IMTR = 18, IMXRDS = 19, IMXORD = 20, INDPRT = 21,
  749. 8 IJSTPL = 22, INDPVT = 51)
  750. C***FIRST EXECUTABLE STATEMENT SDRIV3
  751. IF (NSTATE .EQ. 12) THEN
  752. IERFLG = 999
  753. CALL XERMSG('SLATEC', 'SDRIV3',
  754. 8 'Illegal input. The value of NSTATE is 12 .', IERFLG, 2)
  755. RETURN
  756. ELSE IF (NSTATE .LT. 1 .OR. NSTATE .GT. 12) THEN
  757. WRITE(INTGR1, '(I8)') NSTATE
  758. IERFLG = 26
  759. CALL XERMSG('SLATEC', 'SDRIV3',
  760. 8 'Illegal input. Improper value for NSTATE(= '//INTGR1//').',
  761. 8 IERFLG, 1)
  762. NSTATE = 12
  763. RETURN
  764. END IF
  765. NPAR = N
  766. IF (EPS .LT. 0.E0) THEN
  767. WRITE(RL1, '(E16.8)') EPS
  768. IERFLG = 27
  769. CALL XERMSG('SLATEC', 'SDRIV3',
  770. 8 'Illegal input. EPS, '//RL1//', is negative.', IERFLG, 1)
  771. NSTATE = 12
  772. RETURN
  773. END IF
  774. IF (N .LE. 0) THEN
  775. WRITE(INTGR1, '(I8)') N
  776. IERFLG = 22
  777. CALL XERMSG('SLATEC', 'SDRIV3',
  778. 8 'Illegal input. Number of equations, '//INTGR1//
  779. 8 ', is not positive.', IERFLG, 1)
  780. NSTATE = 12
  781. RETURN
  782. END IF
  783. IF (MXORD .LE. 0) THEN
  784. WRITE(INTGR1, '(I8)') MXORD
  785. IERFLG = 28
  786. CALL XERMSG('SLATEC', 'SDRIV3',
  787. 8 'Illegal input. Maximum order, '//INTGR1//
  788. 8 ', is not positive.', IERFLG, 1)
  789. NSTATE = 12
  790. RETURN
  791. END IF
  792. IF (MINT .LT. 1 .OR. MINT .GT. 3) THEN
  793. WRITE(INTGR1, '(I8)') MINT
  794. IERFLG = 23
  795. CALL XERMSG('SLATEC', 'SDRIV3',
  796. 8 'Illegal input. Improper value for the integration method '//
  797. 8 'flag, '//INTGR1//' .', IERFLG, 1)
  798. NSTATE = 12
  799. RETURN
  800. ELSE IF (MITER .LT. 0 .OR. MITER .GT. 5) THEN
  801. WRITE(INTGR1, '(I8)') MITER
  802. IERFLG = 24
  803. CALL XERMSG('SLATEC', 'SDRIV3',
  804. 8 'Illegal input. Improper value for MITER(= '//INTGR1//').',
  805. 8 IERFLG, 1)
  806. NSTATE = 12
  807. RETURN
  808. ELSE IF (IMPL .LT. 0 .OR. IMPL .GT. 3) THEN
  809. WRITE(INTGR1, '(I8)') IMPL
  810. IERFLG = 25
  811. CALL XERMSG('SLATEC', 'SDRIV3',
  812. 8 'Illegal input. Improper value for IMPL(= '//INTGR1//').',
  813. 8 IERFLG, 1)
  814. NSTATE = 12
  815. RETURN
  816. ELSE IF (MINT .EQ. 3 .AND.
  817. 8 (MITER .EQ. 0 .OR. MITER .EQ. 3 .OR. IMPL .NE. 0)) THEN
  818. WRITE(INTGR1, '(I8)') MITER
  819. WRITE(INTGR2, '(I8)') IMPL
  820. IERFLG = 29
  821. CALL XERMSG('SLATEC', 'SDRIV3',
  822. 8 'Illegal input. For MINT = 3, the value of MITER, '//INTGR1//
  823. 8 ', and/or IMPL, '//INTGR2//', is not allowed.', IERFLG, 1)
  824. NSTATE = 12
  825. RETURN
  826. ELSE IF ((IMPL .GE. 1 .AND. IMPL .LE. 3) .AND. MITER .EQ. 0) THEN
  827. WRITE(INTGR1, '(I8)') IMPL
  828. IERFLG = 30
  829. CALL XERMSG('SLATEC', 'SDRIV3',
  830. 8 'Illegal input. For MITER = 0, the value of IMPL, '//INTGR1//
  831. 8 ', is not allowed.', IERFLG, 1)
  832. NSTATE = 12
  833. RETURN
  834. ELSE IF ((IMPL .EQ. 2 .OR. IMPL .EQ. 3) .AND. MINT .EQ. 1) THEN
  835. WRITE(INTGR1, '(I8)') IMPL
  836. IERFLG = 31
  837. CALL XERMSG('SLATEC', 'SDRIV3',
  838. 8 'Illegal input. For MINT = 1, the value of IMPL, '//INTGR1//
  839. 8 ', is not allowed.', IERFLG, 1)
  840. NSTATE = 12
  841. RETURN
  842. END IF
  843. IF (MITER .EQ. 0 .OR. MITER .EQ. 3) THEN
  844. LIWCHK = INDPVT - 1
  845. ELSE IF (MITER .EQ. 1 .OR. MITER .EQ. 2 .OR. MITER .EQ. 4 .OR.
  846. 8 MITER .EQ. 5) THEN
  847. LIWCHK = INDPVT + N - 1
  848. END IF
  849. IF (LENIW .LT. LIWCHK) THEN
  850. WRITE(INTGR1, '(I8)') LIWCHK
  851. IERFLG = 33
  852. CALL XERMSG('SLATEC', 'SDRIV3',
  853. 8 'Illegal input. Insufficient storage allocated for the '//
  854. 8 'IWORK array. Based on the value of the input parameters '//
  855. 8 'involved, the required storage is '//INTGR1//' .', IERFLG, 1)
  856. NSTATE = 12
  857. RETURN
  858. END IF
  859. C Allocate the WORK array
  860. C IYH is the index of YH in WORK
  861. IF (MINT .EQ. 1 .OR. MINT .EQ. 3) THEN
  862. MAXORD = MIN(MXORD, 12)
  863. ELSE IF (MINT .EQ. 2) THEN
  864. MAXORD = MIN(MXORD, 5)
  865. END IF
  866. IDFDY = IYH + (MAXORD + 1)*N
  867. C IDFDY is the index of DFDY
  868. C
  869. IF (MITER .EQ. 0 .OR. MITER .EQ. 3) THEN
  870. IYWT = IDFDY
  871. ELSE IF (MITER .EQ. 1 .OR. MITER .EQ. 2) THEN
  872. IYWT = IDFDY + N*N
  873. ELSE IF (MITER .EQ. 4 .OR. MITER .EQ. 5) THEN
  874. IYWT = IDFDY + (2*ML + MU + 1)*N
  875. END IF
  876. C IYWT is the index of YWT
  877. ISAVE1 = IYWT + N
  878. C ISAVE1 is the index of SAVE1
  879. ISAVE2 = ISAVE1 + N
  880. C ISAVE2 is the index of SAVE2
  881. IGNOW = ISAVE2 + N
  882. C IGNOW is the index of GNOW
  883. ITROOT = IGNOW + NROOT
  884. C ITROOT is the index of TROOT
  885. IFAC = ITROOT + NROOT
  886. C IFAC is the index of FAC
  887. IF (MITER .EQ. 2 .OR. MITER .EQ. 5 .OR. MINT .EQ. 3) THEN
  888. IA = IFAC + N
  889. ELSE
  890. IA = IFAC
  891. END IF
  892. C IA is the index of A
  893. IF (IMPL .EQ. 0 .OR. MITER .EQ. 3) THEN
  894. LENCHK = IA - 1
  895. ELSE IF (IMPL .EQ. 1 .AND. (MITER .EQ. 1 .OR. MITER .EQ. 2)) THEN
  896. LENCHK = IA - 1 + N*N
  897. ELSE IF (IMPL .EQ. 1 .AND. (MITER .EQ. 4 .OR. MITER .EQ. 5)) THEN
  898. LENCHK = IA - 1 + (2*ML + MU + 1)*N
  899. ELSE IF (IMPL .EQ. 2 .AND. MITER .NE. 3) THEN
  900. LENCHK = IA - 1 + N
  901. ELSE IF (IMPL .EQ. 3 .AND. (MITER .EQ. 1 .OR. MITER .EQ. 2)) THEN
  902. LENCHK = IA - 1 + N*NDE
  903. ELSE IF (IMPL .EQ. 3 .AND. (MITER .EQ. 4 .OR. MITER .EQ. 5)) THEN
  904. LENCHK = IA - 1 + (2*ML + MU + 1)*NDE
  905. END IF
  906. IF (LENW .LT. LENCHK) THEN
  907. WRITE(INTGR1, '(I8)') LENCHK
  908. IERFLG = 32
  909. CALL XERMSG('SLATEC', 'SDRIV3',
  910. 8 'Illegal input. Insufficient storage allocated for the '//
  911. 8 'WORK array. Based on the value of the input parameters '//
  912. 8 'involved, the required storage is '//INTGR1//' .', IERFLG, 1)
  913. NSTATE = 12
  914. RETURN
  915. END IF
  916. IF (MITER .EQ. 0 .OR. MITER .EQ. 3) THEN
  917. MATDIM = 1
  918. ELSE IF (MITER .EQ. 1 .OR. MITER .EQ. 2) THEN
  919. MATDIM = N
  920. ELSE IF (MITER .EQ. 4 .OR. MITER .EQ. 5) THEN
  921. MATDIM = 2*ML + MU + 1
  922. END IF
  923. IF (IMPL .EQ. 0 .OR. IMPL .EQ. 1) THEN
  924. NDECOM = N
  925. ELSE IF (IMPL .EQ. 2 .OR. IMPL .EQ. 3) THEN
  926. NDECOM = NDE
  927. END IF
  928. IF (NSTATE .EQ. 1) THEN
  929. C Initialize parameters
  930. IF (MINT .EQ. 1 .OR. MINT .EQ. 3) THEN
  931. IWORK(IMXORD) = MIN(MXORD, 12)
  932. ELSE IF (MINT .EQ. 2) THEN
  933. IWORK(IMXORD) = MIN(MXORD, 5)
  934. END IF
  935. IWORK(IMXRDS) = MXORD
  936. IF (MINT .EQ. 1 .OR. MINT .EQ. 2) THEN
  937. IWORK(IMNT) = MINT
  938. IWORK(IMTR) = MITER
  939. IWORK(IMNTLD) = MINT
  940. IWORK(IMTRLD) = MITER
  941. ELSE IF (MINT .EQ. 3) THEN
  942. IWORK(IMNT) = 1
  943. IWORK(IMTR) = 0
  944. IWORK(IMNTLD) = IWORK(IMNT)
  945. IWORK(IMTRLD) = IWORK(IMTR)
  946. IWORK(IMTRSV) = MITER
  947. END IF
  948. WORK(IHMAX) = HMAX
  949. UROUND = R1MACH (4)
  950. WORK(IMACH4) = UROUND
  951. WORK(IMACH1) = R1MACH (1)
  952. IF (NROOT .NE. 0) THEN
  953. RE = UROUND
  954. AE = WORK(IMACH1)
  955. END IF
  956. H = (TOUT - T)*(1.E0 - 4.E0*UROUND)
  957. H = SIGN(MIN(ABS(H), HMAX), H)
  958. WORK(IH) = H
  959. HSIGN = SIGN(1.E0, H)
  960. WORK(IHSIGN) = HSIGN
  961. IWORK(IJTASK) = 0
  962. WORK(IAVGH) = 0.E0
  963. WORK(IHUSED) = 0.E0
  964. WORK(IAVGRD) = 0.E0
  965. IWORK(INDMXR) = 0
  966. IWORK(INQUSE) = 0
  967. IWORK(INSTEP) = 0
  968. IWORK(IJSTPL) = 0
  969. IWORK(INFE) = 0
  970. IWORK(INJE) = 0
  971. IWORK(INROOT) = 0
  972. WORK(IT) = T
  973. IWORK(ICNVRG) = 0
  974. IWORK(INDPRT) = 0
  975. C Set initial conditions
  976. DO 30 I = 1,N
  977. 30 WORK(I+IYH-1) = Y(I)
  978. IF (T .EQ. TOUT) RETURN
  979. GO TO 180
  980. ELSE
  981. UROUND = WORK(IMACH4)
  982. IF (NROOT .NE. 0) THEN
  983. RE = UROUND
  984. AE = WORK(IMACH1)
  985. END IF
  986. END IF
  987. C On a continuation, check
  988. C that output points have
  989. C been or will be overtaken.
  990. IF (IWORK(ICNVRG) .EQ. 1) THEN
  991. CONVRG = .TRUE.
  992. ELSE
  993. CONVRG = .FALSE.
  994. END IF
  995. T = WORK(IT)
  996. H = WORK(IH)
  997. HSIGN = WORK(IHSIGN)
  998. IF (IWORK(IJTASK) .EQ. 0) GO TO 180
  999. C
  1000. C IWORK(IJROOT) flags unreported
  1001. C roots, and is set to the value of
  1002. C NTASK when a root was last selected.
  1003. C It is set to zero when all roots
  1004. C have been reported. IWORK(INROOT)
  1005. C contains the index and WORK(ITOUT)
  1006. C contains the value of the root last
  1007. C selected to be reported.
  1008. C IWORK(INRTLD) contains the value of
  1009. C NROOT and IWORK(INDTRT) contains
  1010. C the value of ITROOT when the array
  1011. C of roots was last calculated.
  1012. IF (NROOT .NE. 0) THEN
  1013. IF (IWORK(IJROOT) .GT. 0) THEN
  1014. C TOUT has just been reported.
  1015. C If TROOT .LE. TOUT, report TROOT.
  1016. IF (NSTATE .NE. 5) THEN
  1017. IF (TOUT*HSIGN .GE. WORK(ITOUT)*HSIGN) THEN
  1018. TROOT = WORK(ITOUT)
  1019. CALL SDNTP (H, 0, N, IWORK(INQ), T, TROOT, WORK(IYH), Y)
  1020. T = TROOT
  1021. NSTATE = 5
  1022. IERFLG = 0
  1023. GO TO 580
  1024. END IF
  1025. C A root has just been reported.
  1026. C Select the next root.
  1027. ELSE
  1028. TROOT = T
  1029. IROOT = 0
  1030. DO 50 I = 1,IWORK(INRTLD)
  1031. JTROOT = I + IWORK(INDTRT) - 1
  1032. IF (WORK(JTROOT)*HSIGN .LE. TROOT*HSIGN) THEN
  1033. C
  1034. C Check for multiple roots.
  1035. C
  1036. IF (WORK(JTROOT) .EQ. WORK(ITOUT) .AND.
  1037. 8 I .GT. IWORK(INROOT)) THEN
  1038. IROOT = I
  1039. TROOT = WORK(JTROOT)
  1040. GO TO 60
  1041. END IF
  1042. IF (WORK(JTROOT)*HSIGN .GT. WORK(ITOUT)*HSIGN) THEN
  1043. IROOT = I
  1044. TROOT = WORK(JTROOT)
  1045. END IF
  1046. END IF
  1047. 50 CONTINUE
  1048. 60 IWORK(INROOT) = IROOT
  1049. WORK(ITOUT) = TROOT
  1050. IWORK(IJROOT) = NTASK
  1051. IF (NTASK .EQ. 1) THEN
  1052. IF (IROOT .EQ. 0) THEN
  1053. IWORK(IJROOT) = 0
  1054. ELSE
  1055. IF (TOUT*HSIGN .GE. TROOT*HSIGN) THEN
  1056. CALL SDNTP (H, 0, N, IWORK(INQ), T, TROOT, WORK(IYH),
  1057. 8 Y)
  1058. NSTATE = 5
  1059. T = TROOT
  1060. IERFLG = 0
  1061. GO TO 580
  1062. END IF
  1063. END IF
  1064. ELSE IF (NTASK .EQ. 2 .OR. NTASK .EQ. 3) THEN
  1065. C
  1066. C If there are no more roots, or the
  1067. C user has altered TOUT to be less
  1068. C than a root, set IJROOT to zero.
  1069. C
  1070. IF (IROOT .EQ. 0 .OR. (TOUT*HSIGN .LT. TROOT*HSIGN)) THEN
  1071. IWORK(IJROOT) = 0
  1072. ELSE
  1073. CALL SDNTP (H, 0, N, IWORK(INQ), T, TROOT, WORK(IYH),
  1074. 8 Y)
  1075. NSTATE = 5
  1076. IERFLG = 0
  1077. T = TROOT
  1078. GO TO 580
  1079. END IF
  1080. END IF
  1081. END IF
  1082. END IF
  1083. END IF
  1084. C
  1085. IF (NTASK .EQ. 1) THEN
  1086. NSTATE = 2
  1087. IF (T*HSIGN .GE. TOUT*HSIGN) THEN
  1088. CALL SDNTP (H, 0, N, IWORK(INQ), T, TOUT, WORK(IYH), Y)
  1089. T = TOUT
  1090. IERFLG = 0
  1091. GO TO 580
  1092. END IF
  1093. ELSE IF (NTASK .EQ. 2) THEN
  1094. C Check if TOUT has
  1095. C been reset .LT. T
  1096. IF (T*HSIGN .GT. TOUT*HSIGN) THEN
  1097. WRITE(RL1, '(E16.8)') T
  1098. WRITE(RL2, '(E16.8)') TOUT
  1099. IERFLG = 11
  1100. CALL XERMSG('SLATEC', 'SDRIV3',
  1101. 8 'While integrating exactly to TOUT, T, '//RL1//
  1102. 8 ', was beyond TOUT, '//RL2//' . Solution obtained by '//
  1103. 8 'interpolation.', IERFLG, 0)
  1104. NSTATE = 11
  1105. CALL SDNTP (H, 0, N, IWORK(INQ), T, TOUT, WORK(IYH), Y)
  1106. T = TOUT
  1107. GO TO 580
  1108. END IF
  1109. C Determine if TOUT has been overtaken
  1110. C
  1111. IF (ABS(TOUT - T).LE.NROUND*UROUND*MAX(ABS(T), ABS(TOUT))) THEN
  1112. T = TOUT
  1113. NSTATE = 2
  1114. IERFLG = 0
  1115. GO TO 560
  1116. END IF
  1117. C If there are no more roots
  1118. C to report, report T.
  1119. IF (NSTATE .EQ. 5) THEN
  1120. NSTATE = 2
  1121. IERFLG = 0
  1122. GO TO 560
  1123. END IF
  1124. NSTATE = 2
  1125. C See if TOUT will
  1126. C be overtaken.
  1127. IF ((T + H)*HSIGN .GT. TOUT*HSIGN) THEN
  1128. H = TOUT - T
  1129. IF ((T + H)*HSIGN .GT. TOUT*HSIGN) H = H*(1.E0 - 4.E0*UROUND)
  1130. WORK(IH) = H
  1131. IF (H .EQ. 0.E0) GO TO 670
  1132. IWORK(IJTASK) = -1
  1133. END IF
  1134. ELSE IF (NTASK .EQ. 3) THEN
  1135. NSTATE = 2
  1136. IF (T*HSIGN .GT. TOUT*HSIGN) THEN
  1137. WRITE(RL1, '(E16.8)') T
  1138. WRITE(RL2, '(E16.8)') TOUT
  1139. IERFLG = 11
  1140. CALL XERMSG('SLATEC', 'SDRIV3',
  1141. 8 'While integrating exactly to TOUT, T, '//RL1//
  1142. 8 ', was beyond TOUT, '//RL2//' . Solution obtained by '//
  1143. 8 'interpolation.', IERFLG, 0)
  1144. NSTATE = 11
  1145. CALL SDNTP (H, 0, N, IWORK(INQ), T, TOUT, WORK(IYH), Y)
  1146. T = TOUT
  1147. GO TO 580
  1148. END IF
  1149. IF (ABS(TOUT - T).LE.NROUND*UROUND*MAX(ABS(T), ABS(TOUT))) THEN
  1150. T = TOUT
  1151. IERFLG = 0
  1152. GO TO 560
  1153. END IF
  1154. IF ((T + H)*HSIGN .GT. TOUT*HSIGN) THEN
  1155. H = TOUT - T
  1156. IF ((T + H)*HSIGN .GT. TOUT*HSIGN) H = H*(1.E0 - 4.E0*UROUND)
  1157. WORK(IH) = H
  1158. IF (H .EQ. 0.E0) GO TO 670
  1159. IWORK(IJTASK) = -1
  1160. END IF
  1161. END IF
  1162. C Implement changes in MINT, MITER, and/or HMAX.
  1163. C
  1164. IF ((MINT .NE. IWORK(IMNTLD) .OR. MITER .NE. IWORK(IMTRLD)) .AND.
  1165. 8 MINT .NE. 3 .AND. IWORK(IMNTLD) .NE. 3) IWORK(IJTASK) = -1
  1166. IF (HMAX .NE. WORK(IHMAX)) THEN
  1167. H = SIGN(MIN(ABS(H), HMAX), H)
  1168. IF (H .NE. WORK(IH)) THEN
  1169. IWORK(IJTASK) = -1
  1170. WORK(IH) = H
  1171. END IF
  1172. WORK(IHMAX) = HMAX
  1173. END IF
  1174. C
  1175. 180 NSTEPL = IWORK(INSTEP)
  1176. DO 190 I = 1,N
  1177. 190 Y(I) = WORK(I+IYH-1)
  1178. IF (NROOT .NE. 0) THEN
  1179. DO 200 I = 1,NROOT
  1180. WORK(I+IGNOW-1) = G (NPAR, T, Y, I)
  1181. IF (NPAR .EQ. 0) THEN
  1182. IWORK(INROOT) = I
  1183. NSTATE = 7
  1184. RETURN
  1185. END IF
  1186. 200 CONTINUE
  1187. END IF
  1188. IF (IERROR .EQ. 1) THEN
  1189. DO 230 I = 1,N
  1190. 230 WORK(I+IYWT-1) = 1.E0
  1191. GO TO 410
  1192. ELSE IF (IERROR .EQ. 5) THEN
  1193. DO 250 I = 1,N
  1194. 250 WORK(I+IYWT-1) = EWT(I)
  1195. GO TO 410
  1196. END IF
  1197. C Reset YWT array. Looping point.
  1198. 260 IF (IERROR .EQ. 2) THEN
  1199. DO 280 I = 1,N
  1200. IF (Y(I) .EQ. 0.E0) GO TO 290
  1201. 280 WORK(I+IYWT-1) = ABS(Y(I))
  1202. GO TO 410
  1203. 290 IF (IWORK(IJTASK) .EQ. 0) THEN
  1204. CALL F (NPAR, T, Y, WORK(ISAVE2))
  1205. IF (NPAR .EQ. 0) THEN
  1206. NSTATE = 6
  1207. RETURN
  1208. END IF
  1209. IWORK(INFE) = IWORK(INFE) + 1
  1210. IF (MITER .EQ. 3 .AND. IMPL .NE. 0) THEN
  1211. IFLAG = 0
  1212. CALL USERS (Y, WORK(IYH), WORK(IYWT), WORK(ISAVE1),
  1213. 8 WORK(ISAVE2), T, H, WORK(IEL), IMPL, NPAR,
  1214. 8 NDECOM, IFLAG)
  1215. IF (IFLAG .EQ. -1) GO TO 690
  1216. IF (NPAR .EQ. 0) THEN
  1217. NSTATE = 10
  1218. RETURN
  1219. END IF
  1220. ELSE IF (IMPL .EQ. 1) THEN
  1221. IF (MITER .EQ. 1 .OR. MITER .EQ. 2) THEN
  1222. CALL FA (NPAR, T, Y, WORK(IA), MATDIM, ML, MU, NDECOM)
  1223. IF (NPAR .EQ. 0) THEN
  1224. NSTATE = 9
  1225. RETURN
  1226. END IF
  1227. CALL SGEFA (WORK(IA), MATDIM, N, IWORK(INDPVT), INFO)
  1228. IF (INFO .NE. 0) GO TO 690
  1229. CALL SGESL (WORK(IA), MATDIM, N, IWORK(INDPVT),
  1230. 8 WORK(ISAVE2), 0)
  1231. ELSE IF (MITER .EQ. 4 .OR. MITER .EQ. 5) THEN
  1232. CALL FA (NPAR, T, Y, WORK(IA+ML), MATDIM, ML, MU, NDECOM)
  1233. IF (NPAR .EQ. 0) THEN
  1234. NSTATE = 9
  1235. RETURN
  1236. END IF
  1237. CALL SGBFA (WORK(IA), MATDIM, N, ML, MU, IWORK(INDPVT),
  1238. 8 INFO)
  1239. IF (INFO .NE. 0) GO TO 690
  1240. CALL SGBSL (WORK(IA), MATDIM, N, ML, MU, IWORK(INDPVT),
  1241. 8 WORK(ISAVE2), 0)
  1242. END IF
  1243. ELSE IF (IMPL .EQ. 2) THEN
  1244. CALL FA (NPAR, T, Y, WORK(IA), MATDIM, ML, MU, NDECOM)
  1245. IF (NPAR .EQ. 0) THEN
  1246. NSTATE = 9
  1247. RETURN
  1248. END IF
  1249. DO 340 I = 1,NDECOM
  1250. IF (WORK(I+IA-1) .EQ. 0.E0) GO TO 690
  1251. 340 WORK(I+ISAVE2-1) = WORK(I+ISAVE2-1)/WORK(I+IA-1)
  1252. ELSE IF (IMPL .EQ. 3) THEN
  1253. IF (MITER .EQ. 1 .OR. MITER .EQ. 2) THEN
  1254. CALL FA (NPAR, T, Y, WORK(IA), MATDIM, ML, MU, NDECOM)
  1255. IF (NPAR .EQ. 0) THEN
  1256. NSTATE = 9
  1257. RETURN
  1258. END IF
  1259. CALL SGEFA (WORK(IA), MATDIM, NDE, IWORK(INDPVT), INFO)
  1260. IF (INFO .NE. 0) GO TO 690
  1261. CALL SGESL (WORK(IA), MATDIM, NDE, IWORK(INDPVT),
  1262. 8 WORK(ISAVE2), 0)
  1263. ELSE IF (MITER .EQ. 4 .OR. MITER .EQ. 5) THEN
  1264. CALL FA (NPAR, T, Y, WORK(IA+ML), MATDIM, ML, MU, NDECOM)
  1265. IF (NPAR .EQ. 0) THEN
  1266. NSTATE = 9
  1267. RETURN
  1268. END IF
  1269. CALL SGBFA (WORK(IA), MATDIM, NDE, ML, MU, IWORK(INDPVT),
  1270. 8 INFO)
  1271. IF (INFO .NE. 0) GO TO 690
  1272. CALL SGBSL (WORK(IA), MATDIM, NDE, ML, MU, IWORK(INDPVT),
  1273. 8 WORK(ISAVE2), 0)
  1274. END IF
  1275. END IF
  1276. END IF
  1277. DO 360 J = I,N
  1278. IF (Y(J) .NE. 0.E0) THEN
  1279. WORK(J+IYWT-1) = ABS(Y(J))
  1280. ELSE
  1281. IF (IWORK(IJTASK) .EQ. 0) THEN
  1282. WORK(J+IYWT-1) = ABS(H*WORK(J+ISAVE2-1))
  1283. ELSE
  1284. WORK(J+IYWT-1) = ABS(WORK(J+IYH+N-1))
  1285. END IF
  1286. END IF
  1287. IF (WORK(J+IYWT-1) .EQ. 0.E0) WORK(J+IYWT-1) = UROUND
  1288. 360 CONTINUE
  1289. ELSE IF (IERROR .EQ. 3) THEN
  1290. DO 380 I = 1,N
  1291. 380 WORK(I+IYWT-1) = MAX(EWT(1), ABS(Y(I)))
  1292. ELSE IF (IERROR .EQ. 4) THEN
  1293. DO 400 I = 1,N
  1294. 400 WORK(I+IYWT-1) = MAX(EWT(I), ABS(Y(I)))
  1295. END IF
  1296. C
  1297. 410 DO 420 I = 1,N
  1298. 420 WORK(I+ISAVE2-1) = Y(I)/WORK(I+IYWT-1)
  1299. SUM = SNRM2(N, WORK(ISAVE2), 1)/SQRT(REAL(N))
  1300. SUM = MAX(1.E0, SUM)
  1301. IF (EPS .LT. SUM*UROUND) THEN
  1302. EPS = SUM*UROUND*(1.E0 + 10.E0*UROUND)
  1303. WRITE(RL1, '(E16.8)') T
  1304. WRITE(RL2, '(E16.8)') EPS
  1305. IERFLG = 4
  1306. CALL XERMSG('SLATEC', 'SDRIV3',
  1307. 8 'At T, '//RL1//', the requested accuracy, EPS, was not '//
  1308. 8 'obtainable with the machine precision. EPS has been '//
  1309. 8 'increased to '//RL2//' .', IERFLG, 0)
  1310. NSTATE = 4
  1311. GO TO 560
  1312. END IF
  1313. IF (ABS(H) .GE. UROUND*ABS(T)) THEN
  1314. IWORK(INDPRT) = 0
  1315. ELSE IF (IWORK(INDPRT) .EQ. 0) THEN
  1316. WRITE(RL1, '(E16.8)') T
  1317. WRITE(RL2, '(E16.8)') H
  1318. IERFLG = 15
  1319. CALL XERMSG('SLATEC', 'SDRIV3',
  1320. 8 'At T, '//RL1//', the step size, '//RL2//', is smaller '//
  1321. 8 'than the roundoff level of T. This may occur if there is '//
  1322. 8 'an abrupt change in the right hand side of the '//
  1323. 8 'differential equations.', IERFLG, 0)
  1324. IWORK(INDPRT) = 1
  1325. END IF
  1326. IF (NTASK.NE.2) THEN
  1327. IF ((IWORK(INSTEP)-NSTEPL) .EQ. MXSTEP) THEN
  1328. WRITE(RL1, '(E16.8)') T
  1329. WRITE(INTGR1, '(I8)') MXSTEP
  1330. WRITE(RL2, '(E16.8)') TOUT
  1331. IERFLG = 3
  1332. CALL XERMSG('SLATEC', 'SDRIV3',
  1333. 8 'At T, '//RL1//', '//INTGR1//' steps have been taken '//
  1334. 8 'without reaching TOUT, '//RL2//' .', IERFLG, 0)
  1335. NSTATE = 3
  1336. GO TO 560
  1337. END IF
  1338. END IF
  1339. C
  1340. C CALL SDSTP (EPS, F, FA, HMAX, IMPL, IERROR, JACOBN, MATDIM,
  1341. C 8 MAXORD, MINT, MITER, ML, MU, N, NDE, YWT, UROUND,
  1342. C 8 USERS, AVGH, AVGORD, H, HUSED, JTASK, MNTOLD, MTROLD,
  1343. C 8 NFE, NJE, NQUSED, NSTEP, T, Y, YH, A, CONVRG,
  1344. C 8 DFDY, EL, FAC, HOLD, IPVT, JSTATE, JSTEPL, NQ, NWAIT,
  1345. C 8 RC, RMAX, SAVE1, SAVE2, TQ, TREND, ISWFLG, MTRSV,
  1346. C 8 MXRDSV)
  1347. C
  1348. CALL SDSTP (EPS, F, FA, WORK(IHMAX), IMPL, IERROR, JACOBN,
  1349. 8 MATDIM, IWORK(IMXORD), IWORK(IMNT), IWORK(IMTR), ML,
  1350. 8 MU, NPAR, NDECOM, WORK(IYWT), UROUND, USERS,
  1351. 8 WORK(IAVGH), WORK(IAVGRD), WORK(IH), HUSED,
  1352. 8 IWORK(IJTASK), IWORK(IMNTLD), IWORK(IMTRLD),
  1353. 8 IWORK(INFE), IWORK(INJE), IWORK(INQUSE),
  1354. 8 IWORK(INSTEP), WORK(IT), Y, WORK(IYH), WORK(IA),
  1355. 8 CONVRG, WORK(IDFDY), WORK(IEL), WORK(IFAC),
  1356. 8 WORK(IHOLD), IWORK(INDPVT), JSTATE, IWORK(IJSTPL),
  1357. 8 IWORK(INQ), IWORK(INWAIT), WORK(IRC), WORK(IRMAX),
  1358. 8 WORK(ISAVE1), WORK(ISAVE2), WORK(ITQ), WORK(ITREND),
  1359. 8 MINT, IWORK(IMTRSV), IWORK(IMXRDS))
  1360. T = WORK(IT)
  1361. H = WORK(IH)
  1362. IF (CONVRG) THEN
  1363. IWORK(ICNVRG) = 1
  1364. ELSE
  1365. IWORK(ICNVRG) = 0
  1366. END IF
  1367. GO TO (470, 670, 680, 690, 690, 660, 660, 660, 660, 660), JSTATE
  1368. 470 IWORK(IJTASK) = 1
  1369. C Determine if a root has been overtaken
  1370. IF (NROOT .NE. 0) THEN
  1371. IROOT = 0
  1372. DO 500 I = 1,NROOT
  1373. GLAST = WORK(I+IGNOW-1)
  1374. GNOW = G (NPAR, T, Y, I)
  1375. IF (NPAR .EQ. 0) THEN
  1376. IWORK(INROOT) = I
  1377. NSTATE = 7
  1378. RETURN
  1379. END IF
  1380. WORK(I+IGNOW-1) = GNOW
  1381. IF (GLAST*GNOW .GT. 0.E0) THEN
  1382. WORK(I+ITROOT-1) = T + H
  1383. ELSE
  1384. IF (GNOW .EQ. 0.E0) THEN
  1385. WORK(I+ITROOT-1) = T
  1386. IROOT = I
  1387. ELSE
  1388. IF (GLAST .EQ. 0.E0) THEN
  1389. WORK(I+ITROOT-1) = T + H
  1390. ELSE
  1391. IF (ABS(HUSED) .GE. UROUND*ABS(T)) THEN
  1392. TLAST = T - HUSED
  1393. IROOT = I
  1394. TROOT = T
  1395. CALL SDZRO (AE, G, H, NPAR, IWORK(INQ), IROOT, RE, T,
  1396. 8 WORK(IYH), UROUND, TROOT, TLAST,
  1397. 8 GNOW, GLAST, Y)
  1398. DO 480 J = 1,N
  1399. 480 Y(J) = WORK(IYH+J-1)
  1400. IF (NPAR .EQ. 0) THEN
  1401. IWORK(INROOT) = I
  1402. NSTATE = 7
  1403. RETURN
  1404. END IF
  1405. WORK(I+ITROOT-1) = TROOT
  1406. ELSE
  1407. WORK(I+ITROOT-1) = T
  1408. IROOT = I
  1409. END IF
  1410. END IF
  1411. END IF
  1412. END IF
  1413. 500 CONTINUE
  1414. IF (IROOT .EQ. 0) THEN
  1415. IWORK(IJROOT) = 0
  1416. C Select the first root
  1417. ELSE
  1418. IWORK(IJROOT) = NTASK
  1419. IWORK(INRTLD) = NROOT
  1420. IWORK(INDTRT) = ITROOT
  1421. TROOT = T + H
  1422. DO 510 I = 1,NROOT
  1423. IF (WORK(I+ITROOT-1)*HSIGN .LT. TROOT*HSIGN) THEN
  1424. TROOT = WORK(I+ITROOT-1)
  1425. IROOT = I
  1426. END IF
  1427. 510 CONTINUE
  1428. IWORK(INROOT) = IROOT
  1429. WORK(ITOUT) = TROOT
  1430. IF (TROOT*HSIGN .LE. TOUT*HSIGN) THEN
  1431. CALL SDNTP (H, 0, N, IWORK(INQ), T, TROOT, WORK(IYH), Y)
  1432. NSTATE = 5
  1433. T = TROOT
  1434. IERFLG = 0
  1435. GO TO 580
  1436. END IF
  1437. END IF
  1438. END IF
  1439. C Test for NTASK condition to be satisfied
  1440. NSTATE = 2
  1441. IF (NTASK .EQ. 1) THEN
  1442. IF (T*HSIGN .LT. TOUT*HSIGN) GO TO 260
  1443. CALL SDNTP (H, 0, N, IWORK(INQ), T, TOUT, WORK(IYH), Y)
  1444. T = TOUT
  1445. IERFLG = 0
  1446. GO TO 580
  1447. C TOUT is assumed to have been attained
  1448. C exactly if T is within twenty roundoff
  1449. C units of TOUT, relative to MAX(TOUT, T).
  1450. C
  1451. ELSE IF (NTASK .EQ. 2) THEN
  1452. IF (ABS(TOUT - T).LE.NROUND*UROUND*MAX(ABS(T), ABS(TOUT))) THEN
  1453. T = TOUT
  1454. ELSE
  1455. IF ((T + H)*HSIGN .GT. TOUT*HSIGN) THEN
  1456. H = TOUT - T
  1457. IF ((T + H)*HSIGN.GT.TOUT*HSIGN) H = H*(1.E0 - 4.E0*UROUND)
  1458. WORK(IH) = H
  1459. IF (H .EQ. 0.E0) GO TO 670
  1460. IWORK(IJTASK) = -1
  1461. END IF
  1462. END IF
  1463. ELSE IF (NTASK .EQ. 3) THEN
  1464. IF (ABS(TOUT - T).LE.NROUND*UROUND*MAX(ABS(T), ABS(TOUT))) THEN
  1465. T = TOUT
  1466. ELSE
  1467. IF ((T + H)*HSIGN .GT. TOUT*HSIGN) THEN
  1468. H = TOUT - T
  1469. IF ((T + H)*HSIGN.GT.TOUT*HSIGN) H = H*(1.E0 - 4.E0*UROUND)
  1470. WORK(IH) = H
  1471. IF (H .EQ. 0.E0) GO TO 670
  1472. IWORK(IJTASK) = -1
  1473. END IF
  1474. GO TO 260
  1475. END IF
  1476. END IF
  1477. IERFLG = 0
  1478. C All returns are made through this
  1479. C section. IMXERR is determined.
  1480. 560 DO 570 I = 1,N
  1481. 570 Y(I) = WORK(I+IYH-1)
  1482. 580 IF (IWORK(IJTASK) .EQ. 0) RETURN
  1483. BIG = 0.E0
  1484. IMXERR = 1
  1485. DO 590 I = 1,N
  1486. C SIZE = ABS(ERROR(I)/YWT(I))
  1487. SIZE = ABS(WORK(I+ISAVE1-1)/WORK(I+IYWT-1))
  1488. IF (BIG .LT. SIZE) THEN
  1489. BIG = SIZE
  1490. IMXERR = I
  1491. END IF
  1492. 590 CONTINUE
  1493. IWORK(INDMXR) = IMXERR
  1494. WORK(IHUSED) = HUSED
  1495. RETURN
  1496. C
  1497. 660 NSTATE = JSTATE
  1498. RETURN
  1499. C Fatal errors are processed here
  1500. C
  1501. 670 WRITE(RL1, '(E16.8)') T
  1502. IERFLG = 41
  1503. CALL XERMSG('SLATEC', 'SDRIV3',
  1504. 8 'At T, '//RL1//', the attempted step size has gone to '//
  1505. 8 'zero. Often this occurs if the problem setup is incorrect.',
  1506. 8 IERFLG, 1)
  1507. NSTATE = 12
  1508. RETURN
  1509. C
  1510. 680 WRITE(RL1, '(E16.8)') T
  1511. IERFLG = 42
  1512. CALL XERMSG('SLATEC', 'SDRIV3',
  1513. 8 'At T, '//RL1//', the step size has been reduced about 50 '//
  1514. 8 'times without advancing the solution. Often this occurs '//
  1515. 8 'if the problem setup is incorrect.', IERFLG, 1)
  1516. NSTATE = 12
  1517. RETURN
  1518. C
  1519. 690 WRITE(RL1, '(E16.8)') T
  1520. IERFLG = 43
  1521. CALL XERMSG('SLATEC', 'SDRIV3',
  1522. 8 'At T, '//RL1//', while solving A*YDOT = F, A is singular.',
  1523. 8 IERFLG, 1)
  1524. NSTATE = 12
  1525. RETURN
  1526. END