added README_changes.txt
[wrffire.git] / wrfv2_fire / chem / module_cmu_dvode_solver.F
blobd3db438e311521c7509818838173be60cc49f395
1 !**********************************************************************************  
2 ! This computer software was prepared by Battelle Memorial Institute, hereinafter
3 ! the Contractor, under Contract No. DE-AC05-76RL0 1830 with the Department of 
4 ! Energy (DOE). NEITHER THE GOVERNMENT NOR THE CONTRACTOR MAKES ANY WARRANTY,
5 ! EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.
7 ! MOSAIC module: see module_mosaic_driver.F for information and terms of use
8 !**********************************************************************************  
10 ! 27-oct-2005 rce - do not declare functions that are in the module
11 !-----------------------------------------------------------------------
12 !   module_cmu_dvode_solver - from vode.f & vode_subs.f on 27-oct-2005
13 !           (vode.f - downloaded from www.netlib.org on 28-jul-2004)
14 !           (vode_subs.f - created on 28-jul-2004
15 !               by downloading following from www.netlib.org
16 !               1.  daxpy, dcopy, ddot, dnrm2, dscal, idamax from blas
17 !               2.  dgbfa, dbgsl, dgefa, dgesl from linpack)
19 !       first converted to lowercase
20 !       then converted to fortran-90
21 !       then converted to module
22 !           for this step, had to comment out declarations of any
23 !               functions that are part of the module
24 !       also changed common block names to reduce potential for conflicts
25 !           dvod01 --> dvod_cmn01
26 !           dvod02 --> dvod_cmn02
27 !-----------------------------------------------------------------------
29       module module_cmu_dvode_solver
33       contains
37 !-----------------------------------------------------------------------
38 !   vode.f - downloaded from www.netlib.org on 28-jul-2004
39 !-----------------------------------------------------------------------
41 !deck dvode
42       subroutine dvode (f, neq, y, t, tout, itol, rtol, atol, itask,   &
43                   istate, iopt, rwork, lrw, iwork, liw, jac, mf,   &
44                   rpar, ipar)
45       external f, jac
46       double precision y, t, tout, rtol, atol, rwork, rpar
47       integer neq, itol, itask, istate, iopt, lrw, iwork, liw,   &
48               mf, ipar
49       dimension y(*), rtol(*), atol(*), rwork(lrw), iwork(liw),   &
50                 rpar(*), ipar(*)
51 !-----------------------------------------------------------------------
52 ! dvode: variable-coefficient ordinary differential equation solver,
53 ! with fixed-leading-coefficient implementation.
54 ! this version is in double precision.
56 ! dvode solves the initial value problem for stiff or nonstiff
57 ! systems of first order odes,
58 !     dy/dt = f(t,y) ,  or, in component form,
59 !     dy(i)/dt = f(i) = f(i,t,y(1),y(2),...,y(neq)) (i = 1,...,neq).
60 ! dvode is a package based on the episode and episodeb packages, and
61 ! on the odepack user interface standard, with minor modifications.
62 !-----------------------------------------------------------------------
63 ! authors:
64 !               peter n. brown and alan c. hindmarsh
65 !               center for applied scientific computing, l-561
66 !               lawrence livermore national laboratory
67 !               livermore, ca 94551
68 ! and
69 !               george d. byrne
70 !               illinois institute of technology
71 !               chicago, il 60616
72 !-----------------------------------------------------------------------
73 ! references:
75 ! 1. p. n. brown, g. d. byrne, and a. c. hindmarsh, 'vode: a variable
76 !    coefficient ode solver,' siam j. sci. stat. comput., 10 (1989),
77 !    pp. 1038-1051.  also, llnl report ucrl-98412, june 1988.
78 ! 2. g. d. byrne and a. c. hindmarsh, 'a polyalgorithm for the
79 !    numerical solution of ordinary differential equations,'
80 !    acm trans. math. software, 1 (1975), pp. 71-96.
81 ! 3. a. c. hindmarsh and g. d. byrne, 'episode: an effective package
82 !    for the integration of systems of ordinary differential
83 !    equations,' llnl report ucid-30112, rev. 1, april 1977.
84 ! 4. g. d. byrne and a. c. hindmarsh, 'episodeb: an experimental
85 !    package for the integration of systems of ordinary differential
86 !    equations with banded jacobians,' llnl report ucid-30132, april
87 !    1976.
88 ! 5. a. c. hindmarsh, 'odepack, a systematized collection of ode
89 !    solvers,' in scientific computing, r. s. stepleman et al., eds.,
90 !    north-holland, amsterdam, 1983, pp. 55-64.
91 ! 6. k. r. jackson and r. sacks-davis, 'an alternative implementation
92 !    of variable step-size multistep formulas for stiff odes,' acm
93 !    trans. math. software, 6 (1980), pp. 295-318.
94 !-----------------------------------------------------------------------
95 ! summary of usage.
97 ! communication between the user and the dvode package, for normal
98 ! situations, is summarized here.  this summary describes only a subset
99 ! of the full set of options available.  see the full description for
100 ! details, including optional communication, nonstandard options,
101 ! and instructions for special situations.  see also the example
102 ! problem (with program and output) following this summary.
104 ! a. first provide a subroutine of the form:
105 !           subroutine f (neq, t, y, ydot, rpar, ipar)
106 !           double precision t, y(neq), ydot(neq), rpar
107 ! which supplies the vector function f by loading ydot(i) with f(i).
109 ! b. next determine (or guess) whether or not the problem is stiff.
110 ! stiffness occurs when the jacobian matrix df/dy has an eigenvalue
111 ! whose real part is negative and large in magnitude, compared to the
112 ! reciprocal of the t span of interest.  if the problem is nonstiff,
113 ! use a method flag mf = 10.  if it is stiff, there are four standard
114 ! choices for mf (21, 22, 24, 25), and dvode requires the jacobian
115 ! matrix in some form.  in these cases (mf .gt. 0), dvode will use a
116 ! saved copy of the jacobian matrix.  if this is undesirable because of
117 ! storage limitations, set mf to the corresponding negative value
118 ! (-21, -22, -24, -25).  (see full description of mf below.)
119 ! the jacobian matrix is regarded either as full (mf = 21 or 22),
120 ! or banded (mf = 24 or 25).  in the banded case, dvode requires two
121 ! half-bandwidth parameters ml and mu.  these are, respectively, the
122 ! widths of the lower and upper parts of the band, excluding the main
123 ! diagonal.  thus the band consists of the locations (i,j) with
124 ! i-ml .le. j .le. i+mu, and the full bandwidth is ml+mu+1.
126 ! c. if the problem is stiff, you are encouraged to supply the jacobian
127 ! directly (mf = 21 or 24), but if this is not feasible, dvode will
128 ! compute it internally by difference quotients (mf = 22 or 25).
129 ! if you are supplying the jacobian, provide a subroutine of the form:
130 !           subroutine jac (neq, t, y, ml, mu, pd, nrowpd, rpar, ipar)
131 !           double precision t, y(neq), pd(nrowpd,neq), rpar
132 ! which supplies df/dy by loading pd as follows:
133 !     for a full jacobian (mf = 21), load pd(i,j) with df(i)/dy(j),
134 ! the partial derivative of f(i) with respect to y(j).  (ignore the
135 ! ml and mu arguments in this case.)
136 !     for a banded jacobian (mf = 24), load pd(i-j+mu+1,j) with
137 ! df(i)/dy(j), i.e. load the diagonal lines of df/dy into the rows of
138 ! pd from the top down.
139 !     in either case, only nonzero elements need be loaded.
141 ! d. write a main program which calls subroutine dvode once for
142 ! each point at which answers are desired.  this should also provide
143 ! for possible use of logical unit 6 for output of error messages
144 ! by dvode.  on the first call to dvode, supply arguments as follows:
145 ! f      = name of subroutine for right-hand side vector f.
146 !          this name must be declared external in calling program.
147 ! neq    = number of first order odes.
148 ! y      = array of initial values, of length neq.
149 ! t      = the initial value of the independent variable.
150 ! tout   = first point where output is desired (.ne. t).
151 ! itol   = 1 or 2 according as atol (below) is a scalar or array.
152 ! rtol   = relative tolerance parameter (scalar).
153 ! atol   = absolute tolerance parameter (scalar or array).
154 !          the estimated local error in y(i) will be controlled so as
155 !          to be roughly less (in magnitude) than
156 !             ewt(i) = rtol*abs(y(i)) + atol     if itol = 1, or
157 !             ewt(i) = rtol*abs(y(i)) + atol(i)  if itol = 2.
158 !          thus the local error test passes if, in each component,
159 !          either the absolute error is less than atol (or atol(i)),
160 !          or the relative error is less than rtol.
161 !          use rtol = 0.0 for pure absolute error control, and
162 !          use atol = 0.0 (or atol(i) = 0.0) for pure relative error
163 !          control.  caution: actual (global) errors may exceed these
164 !          local tolerances, so choose them conservatively.
165 ! itask  = 1 for normal computation of output values of y at t = tout.
166 ! istate = integer flag (input and output).  set istate = 1.
167 ! iopt   = 0 to indicate no optional input used.
168 ! rwork  = real work array of length at least:
169 !             20 + 16*neq                      for mf = 10,
170 !             22 +  9*neq + 2*neq**2           for mf = 21 or 22,
171 !             22 + 11*neq + (3*ml + 2*mu)*neq  for mf = 24 or 25.
172 ! lrw    = declared length of rwork (in user's dimension statement).
173 ! iwork  = integer work array of length at least:
174 !             30        for mf = 10,
175 !             30 + neq  for mf = 21, 22, 24, or 25.
176 !          if mf = 24 or 25, input in iwork(1),iwork(2) the lower
177 !          and upper half-bandwidths ml,mu.
178 ! liw    = declared length of iwork (in user's dimension statement).
179 ! jac    = name of subroutine for jacobian matrix (mf = 21 or 24).
180 !          if used, this name must be declared external in calling
181 !          program.  if not used, pass a dummy name.
182 ! mf     = method flag.  standard values are:
183 !          10 for nonstiff (adams) method, no jacobian used.
184 !          21 for stiff (bdf) method, user-supplied full jacobian.
185 !          22 for stiff method, internally generated full jacobian.
186 !          24 for stiff method, user-supplied banded jacobian.
187 !          25 for stiff method, internally generated banded jacobian.
188 ! rpar,ipar = user-defined real and integer arrays passed to f and jac.
189 ! note that the main program must declare arrays y, rwork, iwork,
190 ! and possibly atol, rpar, and ipar.
192 ! e. the output from the first call (or any call) is:
193 !      y = array of computed values of y(t) vector.
194 !      t = corresponding value of independent variable (normally tout).
195 ! istate = 2  if dvode was successful, negative otherwise.
196 !          -1 means excess work done on this call. (perhaps wrong mf.)
197 !          -2 means excess accuracy requested. (tolerances too small.)
198 !          -3 means illegal input detected. (see printed message.)
199 !          -4 means repeated error test failures. (check all input.)
200 !          -5 means repeated convergence failures. (perhaps bad
201 !             jacobian supplied or wrong choice of mf or tolerances.)
202 !          -6 means error weight became zero during problem. (solution
203 !             component i vanished, and atol or atol(i) = 0.)
205 ! f. to continue the integration after a successful return, simply
206 ! reset tout and call dvode again.  no other parameters need be reset.
208 !-----------------------------------------------------------------------
209 ! example problem
211 ! the following is a simple example problem, with the coding
212 ! needed for its solution by dvode.  the problem is from chemical
213 ! kinetics, and consists of the following three rate equations:
214 !     dy1/dt = -.04*y1 + 1.e4*y2*y3
215 !     dy2/dt = .04*y1 - 1.e4*y2*y3 - 3.e7*y2**2
216 !     dy3/dt = 3.e7*y2**2
217 ! on the interval from t = 0.0 to t = 4.e10, with initial conditions
218 ! y1 = 1.0, y2 = y3 = 0.  the problem is stiff.
220 ! the following coding solves this problem with dvode, using mf = 21
221 ! and printing results at t = .4, 4., ..., 4.e10.  it uses
222 ! itol = 2 and atol much smaller for y2 than y1 or y3 because
223 ! y2 has much smaller values.
224 ! at the end of the run, statistical quantities of interest are
225 ! printed. (see optional output in the full description below.)
226 ! to generate fortran source code, replace c in column 1 with a blank
227 ! in the coding below.
229 !     external fex, jex
230 !     double precision atol, rpar, rtol, rwork, t, tout, y
231 !     dimension y(3), atol(3), rwork(67), iwork(33)
232 !     neq = 3
233 !     y(1) = 1.0d0
234 !     y(2) = 0.0d0
235 !     y(3) = 0.0d0
236 !     t = 0.0d0
237 !     tout = 0.4d0
238 !     itol = 2
239 !     rtol = 1.d-4
240 !     atol(1) = 1.d-8
241 !     atol(2) = 1.d-14
242 !     atol(3) = 1.d-6
243 !     itask = 1
244 !     istate = 1
245 !     iopt = 0
246 !     lrw = 67
247 !     liw = 33
248 !     mf = 21
249 !     do 40 iout = 1,12
250 !       call dvode(fex,neq,y,t,tout,itol,rtol,atol,itask,istate,
251 !    1            iopt,rwork,lrw,iwork,liw,jex,mf,rpar,ipar)
252 !       write(6,20)t,y(1),y(2),y(3)
253 ! 20    format(' at t =',d12.4,'   y =',3d14.6)
254 !       if (istate .lt. 0) go to 80
255 ! 40    tout = tout*10.
256 !     write(6,60) iwork(11),iwork(12),iwork(13),iwork(19),
257 !    1            iwork(20),iwork(21),iwork(22)
258 ! 60  format(/' no. steps =',i4,'   no. f-s =',i4,
259 !    1       '   no. j-s =',i4,'   no. lu-s =',i4/
260 !    2       '  no. nonlinear iterations =',i4/
261 !    3       '  no. nonlinear convergence failures =',i4/
262 !    4       '  no. error test failures =',i4/)
263 !     stop
264 ! 80  write(6,90)istate
265 ! 90  format(///' error halt: istate =',i3)
266 !     stop
267 !     end
269 !     subroutine fex (neq, t, y, ydot, rpar, ipar)
270 !     double precision rpar, t, y, ydot
271 !     dimension y(neq), ydot(neq)
272 !     ydot(1) = -.04d0*y(1) + 1.d4*y(2)*y(3)
273 !     ydot(3) = 3.d7*y(2)*y(2)
274 !     ydot(2) = -ydot(1) - ydot(3)
275 !     return
276 !     end
278 !     subroutine jex (neq, t, y, ml, mu, pd, nrpd, rpar, ipar)
279 !     double precision pd, rpar, t, y
280 !     dimension y(neq), pd(nrpd,neq)
281 !     pd(1,1) = -.04d0
282 !     pd(1,2) = 1.d4*y(3)
283 !     pd(1,3) = 1.d4*y(2)
284 !     pd(2,1) = .04d0
285 !     pd(2,3) = -pd(1,3)
286 !     pd(3,2) = 6.d7*y(2)
287 !     pd(2,2) = -pd(1,2) - pd(3,2)
288 !     return
289 !     end
291 ! the following output was obtained from the above program on a
292 ! cray-1 computer with the cft compiler.
294 ! at t =  4.0000e-01   y =  9.851680e-01  3.386314e-05  1.479817e-02
295 ! at t =  4.0000e+00   y =  9.055255e-01  2.240539e-05  9.445214e-02
296 ! at t =  4.0000e+01   y =  7.158108e-01  9.184883e-06  2.841800e-01
297 ! at t =  4.0000e+02   y =  4.505032e-01  3.222940e-06  5.494936e-01
298 ! at t =  4.0000e+03   y =  1.832053e-01  8.942690e-07  8.167938e-01
299 ! at t =  4.0000e+04   y =  3.898560e-02  1.621875e-07  9.610142e-01
300 ! at t =  4.0000e+05   y =  4.935882e-03  1.984013e-08  9.950641e-01
301 ! at t =  4.0000e+06   y =  5.166183e-04  2.067528e-09  9.994834e-01
302 ! at t =  4.0000e+07   y =  5.201214e-05  2.080593e-10  9.999480e-01
303 ! at t =  4.0000e+08   y =  5.213149e-06  2.085271e-11  9.999948e-01
304 ! at t =  4.0000e+09   y =  5.183495e-07  2.073399e-12  9.999995e-01
305 ! at t =  4.0000e+10   y =  5.450996e-08  2.180399e-13  9.999999e-01
307 ! no. steps = 595   no. f-s = 832   no. j-s =  13   no. lu-s = 112
308 !  no. nonlinear iterations = 831
309 !  no. nonlinear convergence failures =   0
310 !  no. error test failures =  22
311 !-----------------------------------------------------------------------
312 ! full description of user interface to dvode.
314 ! the user interface to dvode consists of the following parts.
316 ! i.   the call sequence to subroutine dvode, which is a driver
317 !      routine for the solver.  this includes descriptions of both
318 !      the call sequence arguments and of user-supplied routines.
319 !      following these descriptions is
320 !        * a description of optional input available through the
321 !          call sequence,
322 !        * a description of optional output (in the work arrays), and
323 !        * instructions for interrupting and restarting a solution.
325 ! ii.  descriptions of other routines in the dvode package that may be
326 !      (optionally) called by the user.  these provide the ability to
327 !      alter error message handling, save and restore the internal
328 !      common, and obtain specified derivatives of the solution y(t).
330 ! iii. descriptions of common blocks to be declared in overlay
331 !      or similar environments.
333 ! iv.  description of two routines in the dvode package, either of
334 !      which the user may replace with his own version, if desired.
335 !      these relate to the measurement of errors.
337 !-----------------------------------------------------------------------
338 ! part i.  call sequence.
340 ! the call sequence parameters used for input only are
341 !     f, neq, tout, itol, rtol, atol, itask, iopt, lrw, liw, jac, mf,
342 ! and those used for both input and output are
343 !     y, t, istate.
344 ! the work arrays rwork and iwork are also used for conditional and
345 ! optional input and optional output.  (the term output here refers
346 ! to the return from subroutine dvode to the user's calling program.)
348 ! the legality of input parameters will be thoroughly checked on the
349 ! initial call for the problem, but not checked thereafter unless a
350 ! change in input parameters is flagged by istate = 3 in the input.
352 ! the descriptions of the call arguments are as follows.
354 ! f      = the name of the user-supplied subroutine defining the
355 !          ode system.  the system must be put in the first-order
356 !          form dy/dt = f(t,y), where f is a vector-valued function
357 !          of the scalar t and the vector y.  subroutine f is to
358 !          compute the function f.  it is to have the form
359 !               subroutine f (neq, t, y, ydot, rpar, ipar)
360 !               double precision t, y(neq), ydot(neq), rpar
361 !          where neq, t, and y are input, and the array ydot = f(t,y)
362 !          is output.  y and ydot are arrays of length neq.
363 !          subroutine f should not alter y(1),...,y(neq).
364 !          f must be declared external in the calling program.
366 !          subroutine f may access user-defined real and integer
367 !          work arrays rpar and ipar, which are to be dimensioned
368 !          in the main program.
370 !          if quantities computed in the f routine are needed
371 !          externally to dvode, an extra call to f should be made
372 !          for this purpose, for consistent and accurate results.
373 !          if only the derivative dy/dt is needed, use dvindy instead.
375 ! neq    = the size of the ode system (number of first order
376 !          ordinary differential equations).  used only for input.
377 !          neq may not be increased during the problem, but
378 !          can be decreased (with istate = 3 in the input).
380 ! y      = a real array for the vector of dependent variables, of
381 !          length neq or more.  used for both input and output on the
382 !          first call (istate = 1), and only for output on other calls.
383 !          on the first call, y must contain the vector of initial
384 !          values.  in the output, y contains the computed solution
385 !          evaluated at t.  if desired, the y array may be used
386 !          for other purposes between calls to the solver.
388 !          this array is passed as the y argument in all calls to
389 !          f and jac.
391 ! t      = the independent variable.  in the input, t is used only on
392 !          the first call, as the initial point of the integration.
393 !          in the output, after each call, t is the value at which a
394 !          computed solution y is evaluated (usually the same as tout).
395 !          on an error return, t is the farthest point reached.
397 ! tout   = the next value of t at which a computed solution is desired.
398 !          used only for input.
400 !          when starting the problem (istate = 1), tout may be equal
401 !          to t for one call, then should .ne. t for the next call.
402 !          for the initial t, an input value of tout .ne. t is used
403 !          in order to determine the direction of the integration
404 !          (i.e. the algebraic sign of the step sizes) and the rough
405 !          scale of the problem.  integration in either direction
406 !          (forward or backward in t) is permitted.
408 !          if itask = 2 or 5 (one-step modes), tout is ignored after
409 !          the first call (i.e. the first call with tout .ne. t).
410 !          otherwise, tout is required on every call.
412 !          if itask = 1, 3, or 4, the values of tout need not be
413 !          monotone, but a value of tout which backs up is limited
414 !          to the current internal t interval, whose endpoints are
415 !          tcur - hu and tcur.  (see optional output, below, for
416 !          tcur and hu.)
418 ! itol   = an indicator for the type of error control.  see
419 !          description below under atol.  used only for input.
421 ! rtol   = a relative error tolerance parameter, either a scalar or
422 !          an array of length neq.  see description below under atol.
423 !          input only.
425 ! atol   = an absolute error tolerance parameter, either a scalar or
426 !          an array of length neq.  input only.
428 !          the input parameters itol, rtol, and atol determine
429 !          the error control performed by the solver.  the solver will
430 !          control the vector e = (e(i)) of estimated local errors
431 !          in y, according to an inequality of the form
432 !                      rms-norm of ( e(i)/ewt(i) )   .le.   1,
433 !          where       ewt(i) = rtol(i)*abs(y(i)) + atol(i),
434 !          and the rms-norm (root-mean-square norm) here is
435 !          rms-norm(v) = sqrt(sum v(i)**2 / neq).  here ewt = (ewt(i))
436 !          is a vector of weights which must always be positive, and
437 !          the values of rtol and atol should all be non-negative.
438 !          the following table gives the types (scalar/array) of
439 !          rtol and atol, and the corresponding form of ewt(i).
441 !             itol    rtol       atol          ewt(i)
442 !              1     scalar     scalar     rtol*abs(y(i)) + atol
443 !              2     scalar     array      rtol*abs(y(i)) + atol(i)
444 !              3     array      scalar     rtol(i)*abs(y(i)) + atol
445 !              4     array      array      rtol(i)*abs(y(i)) + atol(i)
447 !          when either of these parameters is a scalar, it need not
448 !          be dimensioned in the user's calling program.
450 !          if none of the above choices (with itol, rtol, and atol
451 !          fixed throughout the problem) is suitable, more general
452 !          error controls can be obtained by substituting
453 !          user-supplied routines for the setting of ewt and/or for
454 !          the norm calculation.  see part iv below.
456 !          if global errors are to be estimated by making a repeated
457 !          run on the same problem with smaller tolerances, then all
458 !          components of rtol and atol (i.e. of ewt) should be scaled
459 !          down uniformly.
461 ! itask  = an index specifying the task to be performed.
462 !          input only.  itask has the following values and meanings.
463 !          1  means normal computation of output values of y(t) at
464 !             t = tout (by overshooting and interpolating).
465 !          2  means take one step only and return.
466 !          3  means stop at the first internal mesh point at or
467 !             beyond t = tout and return.
468 !          4  means normal computation of output values of y(t) at
469 !             t = tout but without overshooting t = tcrit.
470 !             tcrit must be input as rwork(1).  tcrit may be equal to
471 !             or beyond tout, but not behind it in the direction of
472 !             integration.  this option is useful if the problem
473 !             has a singularity at or beyond t = tcrit.
474 !          5  means take one step, without passing tcrit, and return.
475 !             tcrit must be input as rwork(1).
477 !          note:  if itask = 4 or 5 and the solver reaches tcrit
478 !          (within roundoff), it will return t = tcrit (exactly) to
479 !          indicate this (unless itask = 4 and tout comes before tcrit,
480 !          in which case answers at t = tout are returned first).
482 ! istate = an index used for input and output to specify the
483 !          the state of the calculation.
485 !          in the input, the values of istate are as follows.
486 !          1  means this is the first call for the problem
487 !             (initializations will be done).  see note below.
488 !          2  means this is not the first call, and the calculation
489 !             is to continue normally, with no change in any input
490 !             parameters except possibly tout and itask.
491 !             (if itol, rtol, and/or atol are changed between calls
492 !             with istate = 2, the new values will be used but not
493 !             tested for legality.)
494 !          3  means this is not the first call, and the
495 !             calculation is to continue normally, but with
496 !             a change in input parameters other than
497 !             tout and itask.  changes are allowed in
498 !             neq, itol, rtol, atol, iopt, lrw, liw, mf, ml, mu,
499 !             and any of the optional input except h0.
500 !             (see iwork description for ml and mu.)
501 !          note:  a preliminary call with tout = t is not counted
502 !          as a first call here, as no initialization or checking of
503 !          input is done.  (such a call is sometimes useful to include
504 !          the initial conditions in the output.)
505 !          thus the first call for which tout .ne. t requires
506 !          istate = 1 in the input.
508 !          in the output, istate has the following values and meanings.
509 !           1  means nothing was done, as tout was equal to t with
510 !              istate = 1 in the input.
511 !           2  means the integration was performed successfully.
512 !          -1  means an excessive amount of work (more than mxstep
513 !              steps) was done on this call, before completing the
514 !              requested task, but the integration was otherwise
515 !              successful as far as t.  (mxstep is an optional input
516 !              and is normally 500.)  to continue, the user may
517 !              simply reset istate to a value .gt. 1 and call again.
518 !              (the excess work step counter will be reset to 0.)
519 !              in addition, the user may increase mxstep to avoid
520 !              this error return.  (see optional input below.)
521 !          -2  means too much accuracy was requested for the precision
522 !              of the machine being used.  this was detected before
523 !              completing the requested task, but the integration
524 !              was successful as far as t.  to continue, the tolerance
525 !              parameters must be reset, and istate must be set
526 !              to 3.  the optional output tolsf may be used for this
527 !              purpose.  (note: if this condition is detected before
528 !              taking any steps, then an illegal input return
529 !              (istate = -3) occurs instead.)
530 !          -3  means illegal input was detected, before taking any
531 !              integration steps.  see written message for details.
532 !              note:  if the solver detects an infinite loop of calls
533 !              to the solver with illegal input, it will cause
534 !              the run to stop.
535 !          -4  means there were repeated error test failures on
536 !              one attempted step, before completing the requested
537 !              task, but the integration was successful as far as t.
538 !              the problem may have a singularity, or the input
539 !              may be inappropriate.
540 !          -5  means there were repeated convergence test failures on
541 !              one attempted step, before completing the requested
542 !              task, but the integration was successful as far as t.
543 !              this may be caused by an inaccurate jacobian matrix,
544 !              if one is being used.
545 !          -6  means ewt(i) became zero for some i during the
546 !              integration.  pure relative error control (atol(i)=0.0)
547 !              was requested on a variable which has now vanished.
548 !              the integration was successful as far as t.
550 !          note:  since the normal output value of istate is 2,
551 !          it does not need to be reset for normal continuation.
552 !          also, since a negative input value of istate will be
553 !          regarded as illegal, a negative output value requires the
554 !          user to change it, and possibly other input, before
555 !          calling the solver again.
557 ! iopt   = an integer flag to specify whether or not any optional
558 !          input is being used on this call.  input only.
559 !          the optional input is listed separately below.
560 !          iopt = 0 means no optional input is being used.
561 !                   default values will be used in all cases.
562 !          iopt = 1 means optional input is being used.
564 ! rwork  = a real working array (double precision).
565 !          the length of rwork must be at least
566 !             20 + nyh*(maxord + 1) + 3*neq + lwm    where
567 !          nyh    = the initial value of neq,
568 !          maxord = 12 (if meth = 1) or 5 (if meth = 2) (unless a
569 !                   smaller value is given as an optional input),
570 !          lwm = length of work space for matrix-related data:
571 !          lwm = 0             if miter = 0,
572 !          lwm = 2*neq**2 + 2  if miter = 1 or 2, and mf.gt.0,
573 !          lwm = neq**2 + 2    if miter = 1 or 2, and mf.lt.0,
574 !          lwm = neq + 2       if miter = 3,
575 !          lwm = (3*ml+2*mu+2)*neq + 2 if miter = 4 or 5, and mf.gt.0,
576 !          lwm = (2*ml+mu+1)*neq + 2   if miter = 4 or 5, and mf.lt.0.
577 !          (see the mf description for meth and miter.)
578 !          thus if maxord has its default value and neq is constant,
579 !          this length is:
580 !             20 + 16*neq                    for mf = 10,
581 !             22 + 16*neq + 2*neq**2         for mf = 11 or 12,
582 !             22 + 16*neq + neq**2           for mf = -11 or -12,
583 !             22 + 17*neq                    for mf = 13,
584 !             22 + 18*neq + (3*ml+2*mu)*neq  for mf = 14 or 15,
585 !             22 + 17*neq + (2*ml+mu)*neq    for mf = -14 or -15,
586 !             20 +  9*neq                    for mf = 20,
587 !             22 +  9*neq + 2*neq**2         for mf = 21 or 22,
588 !             22 +  9*neq + neq**2           for mf = -21 or -22,
589 !             22 + 10*neq                    for mf = 23,
590 !             22 + 11*neq + (3*ml+2*mu)*neq  for mf = 24 or 25.
591 !             22 + 10*neq + (2*ml+mu)*neq    for mf = -24 or -25.
592 !          the first 20 words of rwork are reserved for conditional
593 !          and optional input and optional output.
595 !          the following word in rwork is a conditional input:
596 !            rwork(1) = tcrit = critical value of t which the solver
597 !                       is not to overshoot.  required if itask is
598 !                       4 or 5, and ignored otherwise.  (see itask.)
600 ! lrw    = the length of the array rwork, as declared by the user.
601 !          (this will be checked by the solver.)
603 ! iwork  = an integer work array.  the length of iwork must be at least
604 !             30        if miter = 0 or 3 (mf = 10, 13, 20, 23), or
605 !             30 + neq  otherwise (abs(mf) = 11,12,14,15,21,22,24,25).
606 !          the first 30 words of iwork are reserved for conditional and
607 !          optional input and optional output.
609 !          the following 2 words in iwork are conditional input:
610 !            iwork(1) = ml     these are the lower and upper
611 !            iwork(2) = mu     half-bandwidths, respectively, of the
612 !                       banded jacobian, excluding the main diagonal.
613 !                       the band is defined by the matrix locations
614 !                       (i,j) with i-ml .le. j .le. i+mu.  ml and mu
615 !                       must satisfy  0 .le.  ml,mu  .le. neq-1.
616 !                       these are required if miter is 4 or 5, and
617 !                       ignored otherwise.  ml and mu may in fact be
618 !                       the band parameters for a matrix to which
619 !                       df/dy is only approximately equal.
621 ! liw    = the length of the array iwork, as declared by the user.
622 !          (this will be checked by the solver.)
624 ! note:  the work arrays must not be altered between calls to dvode
625 ! for the same problem, except possibly for the conditional and
626 ! optional input, and except for the last 3*neq words of rwork.
627 ! the latter space is used for internal scratch space, and so is
628 ! available for use by the user outside dvode between calls, if
629 ! desired (but not for use by f or jac).
631 ! jac    = the name of the user-supplied routine (miter = 1 or 4) to
632 !          compute the jacobian matrix, df/dy, as a function of
633 !          the scalar t and the vector y.  it is to have the form
634 !               subroutine jac (neq, t, y, ml, mu, pd, nrowpd,
635 !                               rpar, ipar)
636 !               double precision t, y(neq), pd(nrowpd,neq), rpar
637 !          where neq, t, y, ml, mu, and nrowpd are input and the array
638 !          pd is to be loaded with partial derivatives (elements of the
639 !          jacobian matrix) in the output.  pd must be given a first
640 !          dimension of nrowpd.  t and y have the same meaning as in
641 !          subroutine f.
642 !               in the full matrix case (miter = 1), ml and mu are
643 !          ignored, and the jacobian is to be loaded into pd in
644 !          columnwise manner, with df(i)/dy(j) loaded into pd(i,j).
645 !               in the band matrix case (miter = 4), the elements
646 !          within the band are to be loaded into pd in columnwise
647 !          manner, with diagonal lines of df/dy loaded into the rows
648 !          of pd. thus df(i)/dy(j) is to be loaded into pd(i-j+mu+1,j).
649 !          ml and mu are the half-bandwidth parameters. (see iwork).
650 !          the locations in pd in the two triangular areas which
651 !          correspond to nonexistent matrix elements can be ignored
652 !          or loaded arbitrarily, as they are overwritten by dvode.
653 !               jac need not provide df/dy exactly.  a crude
654 !          approximation (possibly with a smaller bandwidth) will do.
655 !               in either case, pd is preset to zero by the solver,
656 !          so that only the nonzero elements need be loaded by jac.
657 !          each call to jac is preceded by a call to f with the same
658 !          arguments neq, t, and y.  thus to gain some efficiency,
659 !          intermediate quantities shared by both calculations may be
660 !          saved in a user common block by f and not recomputed by jac,
661 !          if desired.  also, jac may alter the y array, if desired.
662 !          jac must be declared external in the calling program.
663 !               subroutine jac may access user-defined real and integer
664 !          work arrays, rpar and ipar, whose dimensions are set by the
665 !          user in the main program.
667 ! mf     = the method flag.  used only for input.  the legal values of
668 !          mf are 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25,
669 !          -11, -12, -14, -15, -21, -22, -24, -25.
670 !          mf is a signed two-digit integer, mf = jsv*(10*meth + miter).
671 !          jsv = sign(mf) indicates the jacobian-saving strategy:
672 !            jsv =  1 means a copy of the jacobian is saved for reuse
673 !                     in the corrector iteration algorithm.
674 !            jsv = -1 means a copy of the jacobian is not saved
675 !                     (valid only for miter = 1, 2, 4, or 5).
676 !          meth indicates the basic linear multistep method:
677 !            meth = 1 means the implicit adams method.
678 !            meth = 2 means the method based on backward
679 !                     differentiation formulas (bdf-s).
680 !          miter indicates the corrector iteration method:
681 !            miter = 0 means functional iteration (no jacobian matrix
682 !                      is involved).
683 !            miter = 1 means chord iteration with a user-supplied
684 !                      full (neq by neq) jacobian.
685 !            miter = 2 means chord iteration with an internally
686 !                      generated (difference quotient) full jacobian
687 !                      (using neq extra calls to f per df/dy value).
688 !            miter = 3 means chord iteration with an internally
689 !                      generated diagonal jacobian approximation
690 !                      (using 1 extra call to f per df/dy evaluation).
691 !            miter = 4 means chord iteration with a user-supplied
692 !                      banded jacobian.
693 !            miter = 5 means chord iteration with an internally
694 !                      generated banded jacobian (using ml+mu+1 extra
695 !                      calls to f per df/dy evaluation).
696 !          if miter = 1 or 4, the user must supply a subroutine jac
697 !          (the name is arbitrary) as described above under jac.
698 !          for other values of miter, a dummy argument can be used.
700 ! rpar     user-specified array used to communicate real parameters
701 !          to user-supplied subroutines.  if rpar is a vector, then
702 !          it must be dimensioned in the user's main program.  if it
703 !          is unused or it is a scalar, then it need not be
704 !          dimensioned.
706 ! ipar     user-specified array used to communicate integer parameter
707 !          to user-supplied subroutines.  the comments on dimensioning
708 !          rpar apply to ipar.
709 !-----------------------------------------------------------------------
710 ! optional input.
712 ! the following is a list of the optional input provided for in the
713 ! call sequence.  (see also part ii.)  for each such input variable,
714 ! this table lists its name as used in this documentation, its
715 ! location in the call sequence, its meaning, and the default value.
716 ! the use of any of this input requires iopt = 1, and in that
717 ! case all of this input is examined.  a value of zero for any
718 ! of these optional input variables will cause the default value to be
719 ! used.  thus to use a subset of the optional input, simply preload
720 ! locations 5 to 10 in rwork and iwork to 0.0 and 0 respectively, and
721 ! then set those of interest to nonzero values.
723 ! name    location      meaning and default value
725 ! h0      rwork(5)  the step size to be attempted on the first step.
726 !                   the default value is determined by the solver.
728 ! hmax    rwork(6)  the maximum absolute step size allowed.
729 !                   the default value is infinite.
731 ! hmin    rwork(7)  the minimum absolute step size allowed.
732 !                   the default value is 0.  (this lower bound is not
733 !                   enforced on the final step before reaching tcrit
734 !                   when itask = 4 or 5.)
736 ! maxord  iwork(5)  the maximum order to be allowed.  the default
737 !                   value is 12 if meth = 1, and 5 if meth = 2.
738 !                   if maxord exceeds the default value, it will
739 !                   be reduced to the default value.
740 !                   if maxord is changed during the problem, it may
741 !                   cause the current order to be reduced.
743 ! mxstep  iwork(6)  maximum number of (internally defined) steps
744 !                   allowed during one call to the solver.
745 !                   the default value is 500.
747 ! mxhnil  iwork(7)  maximum number of messages printed (per problem)
748 !                   warning that t + h = t on a step (h = step size).
749 !                   this must be positive to result in a non-default
750 !                   value.  the default value is 10.
752 !-----------------------------------------------------------------------
753 ! optional output.
755 ! as optional additional output from dvode, the variables listed
756 ! below are quantities related to the performance of dvode
757 ! which are available to the user.  these are communicated by way of
758 ! the work arrays, but also have internal mnemonic names as shown.
759 ! except where stated otherwise, all of this output is defined
760 ! on any successful return from dvode, and on any return with
761 ! istate = -1, -2, -4, -5, or -6.  on an illegal input return
762 ! (istate = -3), they will be unchanged from their existing values
763 ! (if any), except possibly for tolsf, lenrw, and leniw.
764 ! on any error return, output relevant to the error will be defined,
765 ! as noted below.
767 ! name    location      meaning
769 ! hu      rwork(11) the step size in t last used (successfully).
771 ! hcur    rwork(12) the step size to be attempted on the next step.
773 ! tcur    rwork(13) the current value of the independent variable
774 !                   which the solver has actually reached, i.e. the
775 !                   current internal mesh point in t.  in the output,
776 !                   tcur will always be at least as far from the
777 !                   initial value of t as the current argument t,
778 !                   but may be farther (if interpolation was done).
780 ! tolsf   rwork(14) a tolerance scale factor, greater than 1.0,
781 !                   computed when a request for too much accuracy was
782 !                   detected (istate = -3 if detected at the start of
783 !                   the problem, istate = -2 otherwise).  if itol is
784 !                   left unaltered but rtol and atol are uniformly
785 !                   scaled up by a factor of tolsf for the next call,
786 !                   then the solver is deemed likely to succeed.
787 !                   (the user may also ignore tolsf and alter the
788 !                   tolerance parameters in any other way appropriate.)
790 ! nst     iwork(11) the number of steps taken for the problem so far.
792 ! nfe     iwork(12) the number of f evaluations for the problem so far.
794 ! nje     iwork(13) the number of jacobian evaluations so far.
796 ! nqu     iwork(14) the method order last used (successfully).
798 ! nqcur   iwork(15) the order to be attempted on the next step.
800 ! imxer   iwork(16) the index of the component of largest magnitude in
801 !                   the weighted local error vector ( e(i)/ewt(i) ),
802 !                   on an error return with istate = -4 or -5.
804 ! lenrw   iwork(17) the length of rwork actually required.
805 !                   this is defined on normal returns and on an illegal
806 !                   input return for insufficient storage.
808 ! leniw   iwork(18) the length of iwork actually required.
809 !                   this is defined on normal returns and on an illegal
810 !                   input return for insufficient storage.
812 ! nlu     iwork(19) the number of matrix lu decompositions so far.
814 ! nni     iwork(20) the number of nonlinear (newton) iterations so far.
816 ! ncfn    iwork(21) the number of convergence failures of the nonlinear
817 !                   solver so far.
819 ! netf    iwork(22) the number of error test failures of the integrator
820 !                   so far.
822 ! the following two arrays are segments of the rwork array which
823 ! may also be of interest to the user as optional output.
824 ! for each array, the table below gives its internal name,
825 ! its base address in rwork, and its description.
827 ! name    base address      description
829 ! yh      21             the nordsieck history array, of size nyh by
830 !                        (nqcur + 1), where nyh is the initial value
831 !                        of neq.  for j = 0,1,...,nqcur, column j+1
832 !                        of yh contains hcur**j/factorial(j) times
833 !                        the j-th derivative of the interpolating
834 !                        polynomial currently representing the
835 !                        solution, evaluated at t = tcur.
837 ! acor     lenrw-neq+1   array of size neq used for the accumulated
838 !                        corrections on each step, scaled in the output
839 !                        to represent the estimated local error in y
840 !                        on the last step.  this is the vector e in
841 !                        the description of the error control.  it is
842 !                        defined only on a successful return from dvode.
844 !-----------------------------------------------------------------------
845 ! interrupting and restarting
847 ! if the integration of a given problem by dvode is to be
848 ! interrrupted and then later continued, such as when restarting
849 ! an interrupted run or alternating between two or more ode problems,
850 ! the user should save, following the return from the last dvode call
851 ! prior to the interruption, the contents of the call sequence
852 ! variables and internal common blocks, and later restore these
853 ! values before the next dvode call for that problem.  to save
854 ! and restore the common blocks, use subroutine dvsrco, as
855 ! described below in part ii.
857 ! in addition, if non-default values for either lun or mflag are
858 ! desired, an extra call to xsetun and/or xsetf should be made just
859 ! before continuing the integration.  see part ii below for details.
861 !-----------------------------------------------------------------------
862 ! part ii.  other routines callable.
864 ! the following are optional calls which the user may make to
865 ! gain additional capabilities in conjunction with dvode.
866 ! (the routines xsetun and xsetf are designed to conform to the
867 ! slatec error handling package.)
869 !     form of call                  function
870 !  call xsetun(lun)           set the logical unit number, lun, for
871 !                             output of messages from dvode, if
872 !                             the default is not desired.
873 !                             the default value of lun is 6.
875 !  call xsetf(mflag)          set a flag to control the printing of
876 !                             messages by dvode.
877 !                             mflag = 0 means do not print. (danger:
878 !                             this risks losing valuable information.)
879 !                             mflag = 1 means print (the default).
881 !                             either of the above calls may be made at
882 !                             any time and will take effect immediately.
884 !  call dvsrco(rsav,isav,job) saves and restores the contents of
885 !                             the internal common blocks used by
886 !                             dvode. (see part iii below.)
887 !                             rsav must be a real array of length 49
888 !                             or more, and isav must be an integer
889 !                             array of length 40 or more.
890 !                             job=1 means save common into rsav/isav.
891 !                             job=2 means restore common from rsav/isav.
892 !                                dvsrco is useful if one is
893 !                             interrupting a run and restarting
894 !                             later, or alternating between two or
895 !                             more problems solved with dvode.
897 !  call dvindy(,,,,,)         provide derivatives of y, of various
898 !        (see below.)         orders, at a specified point t, if
899 !                             desired.  it may be called only after
900 !                             a successful return from dvode.
902 ! the detailed instructions for using dvindy are as follows.
903 ! the form of the call is:
905 !  call dvindy (t, k, rwork(21), nyh, dky, iflag)
907 ! the input parameters are:
909 ! t         = value of independent variable where answers are desired
910 !             (normally the same as the t last returned by dvode).
911 !             for valid results, t must lie between tcur - hu and tcur.
912 !             (see optional output for tcur and hu.)
913 ! k         = integer order of the derivative desired.  k must satisfy
914 !             0 .le. k .le. nqcur, where nqcur is the current order
915 !             (see optional output).  the capability corresponding
916 !             to k = 0, i.e. computing y(t), is already provided
917 !             by dvode directly.  since nqcur .ge. 1, the first
918 !             derivative dy/dt is always available with dvindy.
919 ! rwork(21) = the base address of the history array yh.
920 ! nyh       = column length of yh, equal to the initial value of neq.
922 ! the output parameters are:
924 ! dky       = a real array of length neq containing the computed value
925 !             of the k-th derivative of y(t).
926 ! iflag     = integer flag, returned as 0 if k and t were legal,
927 !             -1 if k was illegal, and -2 if t was illegal.
928 !             on an error return, a message is also written.
929 !-----------------------------------------------------------------------
930 ! part iii.  common blocks.
931 ! if dvode is to be used in an overlay situation, the user
932 ! must declare, in the primary overlay, the variables in:
933 !   (1) the call sequence to dvode,
934 !   (2) the two internal common blocks
935 !         /dvod_cmn01/  of length  81  (48 double precision words
936 !                         followed by 33 integer words),
937 !         /dvod_cmn02/  of length  9  (1 double precision word
938 !                         followed by 8 integer words),
940 ! if dvode is used on a system in which the contents of internal
941 ! common blocks are not preserved between calls, the user should
942 ! declare the above two common blocks in his main program to insure
943 ! that their contents are preserved.
945 !-----------------------------------------------------------------------
946 ! part iv.  optionally replaceable solver routines.
948 ! below are descriptions of two routines in the dvode package which
949 ! relate to the measurement of errors.  either routine can be
950 ! replaced by a user-supplied version, if desired.  however, since such
951 ! a replacement may have a major impact on performance, it should be
952 ! done only when absolutely necessary, and only with great caution.
953 ! (note: the means by which the package version of a routine is
954 ! superseded by the user's version may be system-dependent.)
956 ! (a) dewset.
957 ! the following subroutine is called just before each internal
958 ! integration step, and sets the array of error weights, ewt, as
959 ! described under itol/rtol/atol above:
960 !     subroutine dewset (neq, itol, rtol, atol, ycur, ewt)
961 ! where neq, itol, rtol, and atol are as in the dvode call sequence,
962 ! ycur contains the current dependent variable vector, and
963 ! ewt is the array of weights set by dewset.
965 ! if the user supplies this subroutine, it must return in ewt(i)
966 ! (i = 1,...,neq) a positive quantity suitable for comparison with
967 ! errors in y(i).  the ewt array returned by dewset is passed to the
968 ! dvnorm routine (see below.), and also used by dvode in the computation
969 ! of the optional output imxer, the diagonal jacobian approximation,
970 ! and the increments for difference quotient jacobians.
972 ! in the user-supplied version of dewset, it may be desirable to use
973 ! the current values of derivatives of y.  derivatives up to order nq
974 ! are available from the history array yh, described above under
975 ! optional output.  in dewset, yh is identical to the ycur array,
976 ! extended to nq + 1 columns with a column length of nyh and scale
977 ! factors of h**j/factorial(j).  on the first call for the problem,
978 ! given by nst = 0, nq is 1 and h is temporarily set to 1.0.
979 ! nyh is the initial value of neq.  the quantities nq, h, and nst
980 ! can be obtained by including in dewset the statements:
981 !     double precision rvod, h, hu
982 !     common /dvod_cmn01/ rvod(48), ivod(33)
983 !     common /dvod_cmn02/ hu, ncfn, netf, nfe, nje, nlu, nni, nqu, nst
984 !     nq = ivod(28)
985 !     h = rvod(21)
986 ! thus, for example, the current value of dy/dt can be obtained as
987 ! ycur(nyh+i)/h  (i=1,...,neq)  (and the division by h is
988 ! unnecessary when nst = 0).
990 ! (b) dvnorm.
991 ! the following is a real function routine which computes the weighted
992 ! root-mean-square norm of a vector v:
993 !     d = dvnorm (n, v, w)
994 ! where:
995 !   n = the length of the vector,
996 !   v = real array of length n containing the vector,
997 !   w = real array of length n containing weights,
998 !   d = sqrt( (1/n) * sum(v(i)*w(i))**2 ).
999 ! dvnorm is called with n = neq and with w(i) = 1.0/ewt(i), where
1000 ! ewt is as set by subroutine dewset.
1002 ! if the user supplies this function, it should return a non-negative
1003 ! value of dvnorm suitable for use in the error control in dvode.
1004 ! none of the arguments should be altered by dvnorm.
1005 ! for example, a user-supplied dvnorm routine might:
1006 !   -substitute a max-norm of (v(i)*w(i)) for the rms-norm, or
1007 !   -ignore some components of v in the norm, with the effect of
1008 !    suppressing the error control on those components of y.
1009 !-----------------------------------------------------------------------
1010 ! revision history (yyyymmdd)
1011 !  19890615  date written.  initial release.
1012 !  19890922  added interrupt/restart ability, minor changes throughout.
1013 !  19910228  minor revisions in line format,  prologue, etc.
1014 !  19920227  modifications by d. pang:
1015 !            (1) applied subgennam to get generic intrinsic names.
1016 !            (2) changed intrinsic names to generic in comments.
1017 !            (3) added *deck lines before each routine.
1018 !  19920721  names of routines and labeled common blocks changed, so as
1019 !            to be unique in combined single/double precision code (ach).
1020 !  19920722  minor revisions to prologue (ach).
1021 !  19920831  conversion to double precision done (ach).
1022 !  19921106  fixed minor bug: etaq,etaqm1 in dvstep save statement (ach).
1023 !  19921118  changed lunsav/mflgsv to ixsav (ach).
1024 !  19941222  removed mf overwrite; attached sign to h in estimated second
1025 !            deriv. in dvhin; misc. comment changes throughout (ach).
1026 !  19970515  minor corrections to comments in prologue, dvjac (ach).
1027 !  19981111  corrected block b by adding final line, go to 200 (ach).
1028 !  20020430  various upgrades (ach): use odepack error handler package.
1029 !            replaced d1mach by dumach.  various changes to main
1030 !            prologue and other routine prologues.
1031 !-----------------------------------------------------------------------
1032 ! other routines in the dvode package.
1034 ! in addition to subroutine dvode, the dvode package includes the
1035 ! following subroutines and function routines:
1036 !  dvhin     computes an approximate step size for the initial step.
1037 !  dvindy    computes an interpolated value of the y vector at t = tout.
1038 !  dvstep    is the core integrator, which does one step of the
1039 !            integration and the associated error control.
1040 !  dvset     sets all method coefficients and test constants.
1041 !  dvnlsd    solves the underlying nonlinear system -- the corrector.
1042 !  dvjac     computes and preprocesses the jacobian matrix j = df/dy
1043 !            and the newton iteration matrix p = i - (h/l1)*j.
1044 !  dvsol     manages solution of linear system in chord iteration.
1045 !  dvjust    adjusts the history array on a change of order.
1046 !  dewset    sets the error weight vector ewt before each step.
1047 !  dvnorm    computes the weighted r.m.s. norm of a vector.
1048 !  dvsrco    is a user-callable routine to save and restore
1049 !            the contents of the internal common blocks.
1050 !  dacopy    is a routine to copy one two-dimensional array to another.
1051 !  dgefa and dgesl   are routines from linpack for solving full
1052 !            systems of linear algebraic equations.
1053 !  dgbfa and dgbsl   are routines from linpack for solving banded
1054 !            linear systems.
1055 !  daxpy, dscal, and dcopy are basic linear algebra modules (blas).
1056 !  dumach    sets the unit roundoff of the machine.
1057 !  xerrwd, xsetun, xsetf, ixsav, and iumach handle the printing of all
1058 !            error messages and warnings.  xerrwd is machine-dependent.
1059 ! note:  dvnorm, dumach, ixsav, and iumach are function routines.
1060 ! all the others are subroutines.
1062 !-----------------------------------------------------------------------
1064 ! type declarations for labeled common block dvod_cmn01 --------------------
1066       double precision acnrm, ccmxj, conp, crate, drc, el,   &
1067            eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
1068            rc, rl1, tau, tq, tn, uround
1069       integer icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
1070               l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
1071               locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
1072               n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
1073               nslp, nyh
1075 ! type declarations for labeled common block dvod_cmn02 --------------------
1077       double precision hu
1078       integer ncfn, netf, nfe, nje, nlu, nni, nqu, nst
1080 ! type declarations for local variables --------------------------------
1082 ! 27-oct-2005 rce - do not declare functions that are in the module
1083 !     external dvnlsd
1084       logical ihit
1085       double precision atoli, big, ewti, four, h0, hmax, hmx, hun, one,   &
1086          pt2, rh, rtoli, size, tcrit, tnext, tolsf, tp, two, zero
1087       integer i, ier, iflag, imxer, jco, kgo, leniw, lenj, lenp, lenrw,   &
1088          lenwm, lf0, mband, mfa, ml, mord, mu, mxhnl0, mxstp0, niter,   &
1089          nslast
1090       character*80 msg
1092 ! type declaration for function subroutines called ---------------------
1094 ! 27-oct-2005 rce - do not declare functions that are in the module
1095 !     double precision dumach, dvnorm
1097       dimension mord(2)
1098 !-----------------------------------------------------------------------
1099 ! the following fortran-77 declaration is to cause the values of the
1100 ! listed (local) variables to be saved between calls to dvode.
1101 !-----------------------------------------------------------------------
1102       save mord, mxhnl0, mxstp0
1103       save zero, one, two, four, pt2, hun
1104 !-----------------------------------------------------------------------
1105 ! the following internal common blocks contain variables which are
1106 ! communicated between subroutines in the dvode package, or which are
1107 ! to be saved between calls to dvode.
1108 ! in each block, real variables precede integers.
1109 ! the block /dvod_cmn01/ appears in subroutines dvode, dvindy, dvstep,
1110 ! dvset, dvnlsd, dvjac, dvsol, dvjust and dvsrco.
1111 ! the block /dvod_cmn02/ appears in subroutines dvode, dvindy, dvstep,
1112 ! dvnlsd, dvjac, and dvsrco.
1114 ! the variables stored in the internal common blocks are as follows:
1116 ! acnrm  = weighted r.m.s. norm of accumulated correction vectors.
1117 ! ccmxj  = threshhold on drc for updating the jacobian. (see drc.)
1118 ! conp   = the saved value of tq(5).
1119 ! crate  = estimated corrector convergence rate constant.
1120 ! drc    = relative change in h*rl1 since last dvjac call.
1121 ! el     = real array of integration coefficients.  see dvset.
1122 ! eta    = saved tentative ratio of new to old h.
1123 ! etamax = saved maximum value of eta to be allowed.
1124 ! h      = the step size.
1125 ! hmin   = the minimum absolute value of the step size h to be used.
1126 ! hmxi   = inverse of the maximum absolute value of h to be used.
1127 !          hmxi = 0.0 is allowed and corresponds to an infinite hmax.
1128 ! hnew   = the step size to be attempted on the next step.
1129 ! hscal  = stepsize in scaling of yh array.
1130 ! prl1   = the saved value of rl1.
1131 ! rc     = ratio of current h*rl1 to value on last dvjac call.
1132 ! rl1    = the reciprocal of the coefficient el(1).
1133 ! tau    = real vector of past nq step sizes, length 13.
1134 ! tq     = a real vector of length 5 in which dvset stores constants
1135 !          used for the convergence test, the error test, and the
1136 !          selection of h at a new order.
1137 ! tn     = the independent variable, updated on each step taken.
1138 ! uround = the machine unit roundoff.  the smallest positive real number
1139 !          such that  1.0 + uround .ne. 1.0
1140 ! icf    = integer flag for convergence failure in dvnlsd:
1141 !            0 means no failures.
1142 !            1 means convergence failure with out of date jacobian
1143 !                   (recoverable error).
1144 !            2 means convergence failure with current jacobian or
1145 !                   singular matrix (unrecoverable error).
1146 ! init   = saved integer flag indicating whether initialization of the
1147 !          problem has been done (init = 1) or not.
1148 ! ipup   = saved flag to signal updating of newton matrix.
1149 ! jcur   = output flag from dvjac showing jacobian status:
1150 !            jcur = 0 means j is not current.
1151 !            jcur = 1 means j is current.
1152 ! jstart = integer flag used as input to dvstep:
1153 !            0  means perform the first step.
1154 !            1  means take a new step continuing from the last.
1155 !            -1 means take the next step with a new value of maxord,
1156 !                  hmin, hmxi, n, meth, miter, and/or matrix parameters.
1157 !          on return, dvstep sets jstart = 1.
1158 ! jsv    = integer flag for jacobian saving, = sign(mf).
1159 ! kflag  = a completion code from dvstep with the following meanings:
1160 !               0      the step was succesful.
1161 !              -1      the requested error could not be achieved.
1162 !              -2      corrector convergence could not be achieved.
1163 !              -3, -4  fatal error in vnls (can not occur here).
1164 ! kuth   = input flag to dvstep showing whether h was reduced by the
1165 !          driver.  kuth = 1 if h was reduced, = 0 otherwise.
1166 ! l      = integer variable, nq + 1, current order plus one.
1167 ! lmax   = maxord + 1 (used for dimensioning).
1168 ! locjs  = a pointer to the saved jacobian, whose storage starts at
1169 !          wm(locjs), if jsv = 1.
1170 ! lyh, lewt, lacor, lsavf, lwm, liwm = saved integer pointers
1171 !          to segments of rwork and iwork.
1172 ! maxord = the maximum order of integration method to be allowed.
1173 ! meth/miter = the method flags.  see mf.
1174 ! msbj   = the maximum number of steps between j evaluations, = 50.
1175 ! mxhnil = saved value of optional input mxhnil.
1176 ! mxstep = saved value of optional input mxstep.
1177 ! n      = the number of first-order odes, = neq.
1178 ! newh   = saved integer to flag change of h.
1179 ! newq   = the method order to be used on the next step.
1180 ! nhnil  = saved counter for occurrences of t + h = t.
1181 ! nq     = integer variable, the current integration method order.
1182 ! nqnyh  = saved value of nq*nyh.
1183 ! nqwait = a counter controlling the frequency of order changes.
1184 !          an order change is about to be considered if nqwait = 1.
1185 ! nslj   = the number of steps taken as of the last jacobian update.
1186 ! nslp   = saved value of nst as of last newton matrix update.
1187 ! nyh    = saved value of the initial value of neq.
1188 ! hu     = the step size in t last used.
1189 ! ncfn   = number of nonlinear convergence failures so far.
1190 ! netf   = the number of error test failures of the integrator so far.
1191 ! nfe    = the number of f evaluations for the problem so far.
1192 ! nje    = the number of jacobian evaluations so far.
1193 ! nlu    = the number of matrix lu decompositions so far.
1194 ! nni    = number of nonlinear iterations so far.
1195 ! nqu    = the method order last used.
1196 ! nst    = the number of steps taken for the problem so far.
1197 !-----------------------------------------------------------------------
1198       common /dvod_cmn01/ acnrm, ccmxj, conp, crate, drc, el(13),   &
1199                       eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
1200                       rc, rl1, tau(13), tq(5), tn, uround,   &
1201                       icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
1202                       l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
1203                       locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
1204                       n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
1205                       nslp, nyh
1206       common /dvod_cmn02/ hu, ncfn, netf, nfe, nje, nlu, nni, nqu, nst
1208       data  mord(1) /12/, mord(2) /5/, mxstp0 /500/, mxhnl0 /10/
1209       data zero /0.0d0/, one /1.0d0/, two /2.0d0/, four /4.0d0/,   &
1210            pt2 /0.2d0/, hun /100.0d0/
1211 !-----------------------------------------------------------------------
1212 ! block a.
1213 ! this code block is executed on every call.
1214 ! it tests istate and itask for legality and branches appropriately.
1215 ! if istate .gt. 1 but the flag init shows that initialization has
1216 ! not yet been done, an error return occurs.
1217 ! if istate = 1 and tout = t, return immediately.
1218 !-----------------------------------------------------------------------
1219       if (istate .lt. 1 .or. istate .gt. 3) go to 601
1220       if (itask .lt. 1 .or. itask .gt. 5) go to 602
1221       if (istate .eq. 1) go to 10
1222       if (init .ne. 1) go to 603
1223       if (istate .eq. 2) go to 200
1224       go to 20
1225  10   init = 0
1226       if (tout .eq. t) return
1227 !-----------------------------------------------------------------------
1228 ! block b.
1229 ! the next code block is executed for the initial call (istate = 1),
1230 ! or for a continuation call with parameter changes (istate = 3).
1231 ! it contains checking of all input and various initializations.
1233 ! first check legality of the non-optional input neq, itol, iopt,
1234 ! mf, ml, and mu.
1235 !-----------------------------------------------------------------------
1236  20   if (neq .le. 0) go to 604
1237       if (istate .eq. 1) go to 25
1238       if (neq .gt. n) go to 605
1239  25   n = neq
1240       if (itol .lt. 1 .or. itol .gt. 4) go to 606
1241       if (iopt .lt. 0 .or. iopt .gt. 1) go to 607
1242       jsv = sign(1,mf)
1243       mfa = abs(mf)
1244       meth = mfa/10
1245       miter = mfa - 10*meth
1246       if (meth .lt. 1 .or. meth .gt. 2) go to 608
1247       if (miter .lt. 0 .or. miter .gt. 5) go to 608
1248       if (miter .le. 3) go to 30
1249       ml = iwork(1)
1250       mu = iwork(2)
1251       if (ml .lt. 0 .or. ml .ge. n) go to 609
1252       if (mu .lt. 0 .or. mu .ge. n) go to 610
1253  30   continue
1254 ! next process and check the optional input. ---------------------------
1255       if (iopt .eq. 1) go to 40
1256       maxord = mord(meth)
1257       mxstep = mxstp0
1258       mxhnil = mxhnl0
1259       if (istate .eq. 1) h0 = zero
1260       hmxi = zero
1261       hmin = zero
1262       go to 60
1263  40   maxord = iwork(5)
1264       if (maxord .lt. 0) go to 611
1265       if (maxord .eq. 0) maxord = 100
1266       maxord = min(maxord,mord(meth))
1267       mxstep = iwork(6)
1268       if (mxstep .lt. 0) go to 612
1269       if (mxstep .eq. 0) mxstep = mxstp0
1270       mxhnil = iwork(7)
1271       if (mxhnil .lt. 0) go to 613
1272       if (mxhnil .eq. 0) mxhnil = mxhnl0
1273       if (istate .ne. 1) go to 50
1274       h0 = rwork(5)
1275       if ((tout - t)*h0 .lt. zero) go to 614
1276  50   hmax = rwork(6)
1277       if (hmax .lt. zero) go to 615
1278       hmxi = zero
1279       if (hmax .gt. zero) hmxi = one/hmax
1280       hmin = rwork(7)
1281       if (hmin .lt. zero) go to 616
1282 !-----------------------------------------------------------------------
1283 ! set work array pointers and check lengths lrw and liw.
1284 ! pointers to segments of rwork and iwork are named by prefixing l to
1285 ! the name of the segment.  e.g., the segment yh starts at rwork(lyh).
1286 ! segments of rwork (in order) are denoted  yh, wm, ewt, savf, acor.
1287 ! within wm, locjs is the location of the saved jacobian (jsv .gt. 0).
1288 !-----------------------------------------------------------------------
1289  60   lyh = 21
1290       if (istate .eq. 1) nyh = n
1291       lwm = lyh + (maxord + 1)*nyh
1292       jco = max(0,jsv)
1293       if (miter .eq. 0) lenwm = 0
1294       if (miter .eq. 1 .or. miter .eq. 2) then
1295         lenwm = 2 + (1 + jco)*n*n
1296         locjs = n*n + 3
1297       endif
1298       if (miter .eq. 3) lenwm = 2 + n
1299       if (miter .eq. 4 .or. miter .eq. 5) then
1300         mband = ml + mu + 1
1301         lenp = (mband + ml)*n
1302         lenj = mband*n
1303         lenwm = 2 + lenp + jco*lenj
1304         locjs = lenp + 3
1305         endif
1306       lewt = lwm + lenwm
1307       lsavf = lewt + n
1308       lacor = lsavf + n
1309       lenrw = lacor + n - 1
1310       iwork(17) = lenrw
1311       liwm = 1
1312       leniw = 30 + n
1313       if (miter .eq. 0 .or. miter .eq. 3) leniw = 30
1314       iwork(18) = leniw
1315       if (lenrw .gt. lrw) go to 617
1316       if (leniw .gt. liw) go to 618
1317 ! check rtol and atol for legality. ------------------------------------
1318       rtoli = rtol(1)
1319       atoli = atol(1)
1320       do 70 i = 1,n
1321         if (itol .ge. 3) rtoli = rtol(i)
1322         if (itol .eq. 2 .or. itol .eq. 4) atoli = atol(i)
1323         if (rtoli .lt. zero) go to 619
1324         if (atoli .lt. zero) go to 620
1325  70     continue
1326       if (istate .eq. 1) go to 100
1327 ! if istate = 3, set flag to signal parameter changes to dvstep. -------
1328       jstart = -1
1329       if (nq .le. maxord) go to 90
1330 ! maxord was reduced below nq.  copy yh(*,maxord+2) into savf. ---------
1331       call dcopy (n, rwork(lwm), 1, rwork(lsavf), 1)
1332 ! reload wm(1) = rwork(lwm), since lwm may have changed. ---------------
1333  90   if (miter .gt. 0) rwork(lwm) = sqrt(uround)
1334       go to 200
1335 !-----------------------------------------------------------------------
1336 ! block c.
1337 ! the next block is for the initial call only (istate = 1).
1338 ! it contains all remaining initializations, the initial call to f,
1339 ! and the calculation of the initial step size.
1340 ! the error weights in ewt are inverted after being loaded.
1341 !-----------------------------------------------------------------------
1342  100  uround = dumach()
1343       tn = t
1344       if (itask .ne. 4 .and. itask .ne. 5) go to 110
1345       tcrit = rwork(1)
1346       if ((tcrit - tout)*(tout - t) .lt. zero) go to 625
1347       if (h0 .ne. zero .and. (t + h0 - tcrit)*h0 .gt. zero)   &
1348          h0 = tcrit - t
1349  110  jstart = 0
1350       if (miter .gt. 0) rwork(lwm) = sqrt(uround)
1351       ccmxj = pt2
1352       msbj = 50
1353       nhnil = 0
1354       nst = 0
1355       nje = 0
1356       nni = 0
1357       ncfn = 0
1358       netf = 0
1359       nlu = 0
1360       nslj = 0
1361       nslast = 0
1362       hu = zero
1363       nqu = 0
1364 ! initial call to f.  (lf0 points to yh(*,2).) -------------------------
1365       lf0 = lyh + nyh
1366       call f (n, t, y, rwork(lf0), rpar, ipar)
1367       nfe = 1
1368 ! load the initial value vector in yh. ---------------------------------
1369       call dcopy (n, y, 1, rwork(lyh), 1)
1370 ! load and invert the ewt array.  (h is temporarily set to 1.0.) -------
1371       nq = 1
1372       h = one
1373       call dewset (n, itol, rtol, atol, rwork(lyh), rwork(lewt))
1374       do 120 i = 1,n
1375         if (rwork(i+lewt-1) .le. zero) go to 621
1376  120    rwork(i+lewt-1) = one/rwork(i+lewt-1)
1377       if (h0 .ne. zero) go to 180
1378 ! call dvhin to set initial step size h0 to be attempted. --------------
1379       call dvhin (n, t, rwork(lyh), rwork(lf0), f, rpar, ipar, tout,   &
1380          uround, rwork(lewt), itol, atol, y, rwork(lacor), h0,   &
1381          niter, ier)
1382       nfe = nfe + niter
1383       if (ier .ne. 0) go to 622
1384 ! adjust h0 if necessary to meet hmax bound. ---------------------------
1385  180  rh = abs(h0)*hmxi
1386       if (rh .gt. one) h0 = h0/rh
1387 ! load h with h0 and scale yh(*,2) by h0. ------------------------------
1388       h = h0
1389       call dscal (n, h0, rwork(lf0), 1)
1390       go to 270
1391 !-----------------------------------------------------------------------
1392 ! block d.
1393 ! the next code block is for continuation calls only (istate = 2 or 3)
1394 ! and is to check stop conditions before taking a step.
1395 !-----------------------------------------------------------------------
1396  200  nslast = nst
1397       kuth = 0
1398       go to (210, 250, 220, 230, 240), itask
1399  210  if ((tn - tout)*h .lt. zero) go to 250
1400       call dvindy (tout, 0, rwork(lyh), nyh, y, iflag)
1401       if (iflag .ne. 0) go to 627
1402       t = tout
1403       go to 420
1404  220  tp = tn - hu*(one + hun*uround)
1405       if ((tp - tout)*h .gt. zero) go to 623
1406       if ((tn - tout)*h .lt. zero) go to 250
1407       go to 400
1408  230  tcrit = rwork(1)
1409       if ((tn - tcrit)*h .gt. zero) go to 624
1410       if ((tcrit - tout)*h .lt. zero) go to 625
1411       if ((tn - tout)*h .lt. zero) go to 245
1412       call dvindy (tout, 0, rwork(lyh), nyh, y, iflag)
1413       if (iflag .ne. 0) go to 627
1414       t = tout
1415       go to 420
1416  240  tcrit = rwork(1)
1417       if ((tn - tcrit)*h .gt. zero) go to 624
1418  245  hmx = abs(tn) + abs(h)
1419       ihit = abs(tn - tcrit) .le. hun*uround*hmx
1420       if (ihit) go to 400
1421       tnext = tn + hnew*(one + four*uround)
1422       if ((tnext - tcrit)*h .le. zero) go to 250
1423       h = (tcrit - tn)*(one - four*uround)
1424       kuth = 1
1425 !-----------------------------------------------------------------------
1426 ! block e.
1427 ! the next block is normally executed for all calls and contains
1428 ! the call to the one-step core integrator dvstep.
1430 ! this is a looping point for the integration steps.
1432 ! first check for too many steps being taken, update ewt (if not at
1433 ! start of problem), check for too much accuracy being requested, and
1434 ! check for h below the roundoff level in t.
1435 !-----------------------------------------------------------------------
1436  250  continue
1437       if ((nst-nslast) .ge. mxstep) go to 500
1438       call dewset (n, itol, rtol, atol, rwork(lyh), rwork(lewt))
1439       do 260 i = 1,n
1440         if (rwork(i+lewt-1) .le. zero) go to 510
1441  260    rwork(i+lewt-1) = one/rwork(i+lewt-1)
1442  270  tolsf = uround*dvnorm (n, rwork(lyh), rwork(lewt))
1443       if (tolsf .le. one) go to 280
1444       tolsf = tolsf*two
1445       if (nst .eq. 0) go to 626
1446       go to 520
1447  280  if ((tn + h) .ne. tn) go to 290
1448       nhnil = nhnil + 1
1449       if (nhnil .gt. mxhnil) go to 290
1450       msg = 'dvode--  warning: internal t (=r1) and h (=r2) are'
1451       call xerrwd (msg, 50, 101, 1, 0, 0, 0, 0, zero, zero)
1452       msg='      such that in the machine, t + h = t on the next step  '
1453       call xerrwd (msg, 60, 101, 1, 0, 0, 0, 0, zero, zero)
1454       msg = '      (h = step size). solver will continue anyway'
1455       call xerrwd (msg, 50, 101, 1, 0, 0, 0, 2, tn, h)
1456       if (nhnil .lt. mxhnil) go to 290
1457       msg = 'dvode--  above warning has been issued i1 times.  '
1458       call xerrwd (msg, 50, 102, 1, 0, 0, 0, 0, zero, zero)
1459       msg = '      it will not be issued again for this problem'
1460       call xerrwd (msg, 50, 102, 1, 1, mxhnil, 0, 0, zero, zero)
1461  290  continue
1462 !-----------------------------------------------------------------------
1463 ! call dvstep (y, yh, nyh, yh, ewt, savf, vsav, acor,
1464 !              wm, iwm, f, jac, f, dvnlsd, rpar, ipar)
1465 !-----------------------------------------------------------------------
1466       call dvstep (y, rwork(lyh), nyh, rwork(lyh), rwork(lewt),   &
1467          rwork(lsavf), y, rwork(lacor), rwork(lwm), iwork(liwm),   &
1468          f, jac, f, dvnlsd, rpar, ipar)
1469       kgo = 1 - kflag
1470 ! branch on kflag.  note: in this version, kflag can not be set to -3.
1471 !  kflag .eq. 0,   -1,  -2
1472       go to (300, 530, 540), kgo
1473 !-----------------------------------------------------------------------
1474 ! block f.
1475 ! the following block handles the case of a successful return from the
1476 ! core integrator (kflag = 0).  test for stop conditions.
1477 !-----------------------------------------------------------------------
1478  300  init = 1
1479       kuth = 0
1480       go to (310, 400, 330, 340, 350), itask
1481 ! itask = 1.  if tout has been reached, interpolate. -------------------
1482  310  if ((tn - tout)*h .lt. zero) go to 250
1483       call dvindy (tout, 0, rwork(lyh), nyh, y, iflag)
1484       t = tout
1485       go to 420
1486 ! itask = 3.  jump to exit if tout was reached. ------------------------
1487  330  if ((tn - tout)*h .ge. zero) go to 400
1488       go to 250
1489 ! itask = 4.  see if tout or tcrit was reached.  adjust h if necessary.
1490  340  if ((tn - tout)*h .lt. zero) go to 345
1491       call dvindy (tout, 0, rwork(lyh), nyh, y, iflag)
1492       t = tout
1493       go to 420
1494  345  hmx = abs(tn) + abs(h)
1495       ihit = abs(tn - tcrit) .le. hun*uround*hmx
1496       if (ihit) go to 400
1497       tnext = tn + hnew*(one + four*uround)
1498       if ((tnext - tcrit)*h .le. zero) go to 250
1499       h = (tcrit - tn)*(one - four*uround)
1500       kuth = 1
1501       go to 250
1502 ! itask = 5.  see if tcrit was reached and jump to exit. ---------------
1503  350  hmx = abs(tn) + abs(h)
1504       ihit = abs(tn - tcrit) .le. hun*uround*hmx
1505 !-----------------------------------------------------------------------
1506 ! block g.
1507 ! the following block handles all successful returns from dvode.
1508 ! if itask .ne. 1, y is loaded from yh and t is set accordingly.
1509 ! istate is set to 2, and the optional output is loaded into the work
1510 ! arrays before returning.
1511 !-----------------------------------------------------------------------
1512  400  continue
1513       call dcopy (n, rwork(lyh), 1, y, 1)
1514       t = tn
1515       if (itask .ne. 4 .and. itask .ne. 5) go to 420
1516       if (ihit) t = tcrit
1517  420  istate = 2
1518       rwork(11) = hu
1519       rwork(12) = hnew
1520       rwork(13) = tn
1521       iwork(11) = nst
1522       iwork(12) = nfe
1523       iwork(13) = nje
1524       iwork(14) = nqu
1525       iwork(15) = newq
1526       iwork(19) = nlu
1527       iwork(20) = nni
1528       iwork(21) = ncfn
1529       iwork(22) = netf
1530       return
1531 !-----------------------------------------------------------------------
1532 ! block h.
1533 ! the following block handles all unsuccessful returns other than
1534 ! those for illegal input.  first the error message routine is called.
1535 ! if there was an error test or convergence test failure, imxer is set.
1536 ! then y is loaded from yh, and t is set to tn.
1537 ! the optional output is loaded into the work arrays before returning.
1538 !-----------------------------------------------------------------------
1539 ! the maximum number of steps was taken before reaching tout. ----------
1540  500  msg = 'dvode--  at current t (=r1), mxstep (=i1) steps   '
1541       call xerrwd (msg, 50, 201, 1, 0, 0, 0, 0, zero, zero)
1542       msg = '      taken on this call before reaching tout     '
1543       call xerrwd (msg, 50, 201, 1, 1, mxstep, 0, 1, tn, zero)
1544       istate = -1
1545       go to 580
1546 ! ewt(i) .le. 0.0 for some i (not at start of problem). ----------------
1547  510  ewti = rwork(lewt+i-1)
1548       msg = 'dvode--  at t (=r1), ewt(i1) has become r2 .le. 0.'
1549       call xerrwd (msg, 50, 202, 1, 1, i, 0, 2, tn, ewti)
1550       istate = -6
1551       go to 580
1552 ! too much accuracy requested for machine precision. -------------------
1553  520  msg = 'dvode--  at t (=r1), too much accuracy requested  '
1554       call xerrwd (msg, 50, 203, 1, 0, 0, 0, 0, zero, zero)
1555       msg = '      for precision of machine:   see tolsf (=r2) '
1556       call xerrwd (msg, 50, 203, 1, 0, 0, 0, 2, tn, tolsf)
1557       rwork(14) = tolsf
1558       istate = -2
1559       go to 580
1560 ! kflag = -1.  error test failed repeatedly or with abs(h) = hmin. -----
1561  530  msg = 'dvode--  at t(=r1) and step size h(=r2), the error'
1562       call xerrwd (msg, 50, 204, 1, 0, 0, 0, 0, zero, zero)
1563       msg = '      test failed repeatedly or with abs(h) = hmin'
1564       call xerrwd (msg, 50, 204, 1, 0, 0, 0, 2, tn, h)
1565       istate = -4
1566       go to 560
1567 ! kflag = -2.  convergence failed repeatedly or with abs(h) = hmin. ----
1568  540  msg = 'dvode--  at t (=r1) and step size h (=r2), the    '
1569       call xerrwd (msg, 50, 205, 1, 0, 0, 0, 0, zero, zero)
1570       msg = '      corrector convergence failed repeatedly     '
1571       call xerrwd (msg, 50, 205, 1, 0, 0, 0, 0, zero, zero)
1572       msg = '      or with abs(h) = hmin   '
1573       call xerrwd (msg, 30, 205, 1, 0, 0, 0, 2, tn, h)
1574       istate = -5
1575 ! compute imxer if relevant. -------------------------------------------
1576  560  big = zero
1577       imxer = 1
1578       do 570 i = 1,n
1579         size = abs(rwork(i+lacor-1)*rwork(i+lewt-1))
1580         if (big .ge. size) go to 570
1581         big = size
1582         imxer = i
1583  570    continue
1584       iwork(16) = imxer
1585 ! set y vector, t, and optional output. --------------------------------
1586  580  continue
1587       call dcopy (n, rwork(lyh), 1, y, 1)
1588       t = tn
1589       rwork(11) = hu
1590       rwork(12) = h
1591       rwork(13) = tn
1592       iwork(11) = nst
1593       iwork(12) = nfe
1594       iwork(13) = nje
1595       iwork(14) = nqu
1596       iwork(15) = nq
1597       iwork(19) = nlu
1598       iwork(20) = nni
1599       iwork(21) = ncfn
1600       iwork(22) = netf
1601       return
1602 !-----------------------------------------------------------------------
1603 ! block i.
1604 ! the following block handles all error returns due to illegal input
1605 ! (istate = -3), as detected before calling the core integrator.
1606 ! first the error message routine is called.   if the illegal input
1607 ! is a negative istate, the run is aborted (apparent infinite loop).
1608 !-----------------------------------------------------------------------
1609  601  msg = 'dvode--  istate (=i1) illegal '
1610       call xerrwd (msg, 30, 1, 1, 1, istate, 0, 0, zero, zero)
1611       if (istate .lt. 0) go to 800
1612       go to 700
1613  602  msg = 'dvode--  itask (=i1) illegal  '
1614       call xerrwd (msg, 30, 2, 1, 1, itask, 0, 0, zero, zero)
1615       go to 700
1616  603  msg='dvode--  istate (=i1) .gt. 1 but dvode not initialized      '
1617       call xerrwd (msg, 60, 3, 1, 1, istate, 0, 0, zero, zero)
1618       go to 700
1619  604  msg = 'dvode--  neq (=i1) .lt. 1     '
1620       call xerrwd (msg, 30, 4, 1, 1, neq, 0, 0, zero, zero)
1621       go to 700
1622  605  msg = 'dvode--  istate = 3 and neq increased (i1 to i2)  '
1623       call xerrwd (msg, 50, 5, 1, 2, n, neq, 0, zero, zero)
1624       go to 700
1625  606  msg = 'dvode--  itol (=i1) illegal   '
1626       call xerrwd (msg, 30, 6, 1, 1, itol, 0, 0, zero, zero)
1627       go to 700
1628  607  msg = 'dvode--  iopt (=i1) illegal   '
1629       call xerrwd (msg, 30, 7, 1, 1, iopt, 0, 0, zero, zero)
1630       go to 700
1631  608  msg = 'dvode--  mf (=i1) illegal     '
1632       call xerrwd (msg, 30, 8, 1, 1, mf, 0, 0, zero, zero)
1633       go to 700
1634  609  msg = 'dvode--  ml (=i1) illegal:  .lt.0 or .ge.neq (=i2)'
1635       call xerrwd (msg, 50, 9, 1, 2, ml, neq, 0, zero, zero)
1636       go to 700
1637  610  msg = 'dvode--  mu (=i1) illegal:  .lt.0 or .ge.neq (=i2)'
1638       call xerrwd (msg, 50, 10, 1, 2, mu, neq, 0, zero, zero)
1639       go to 700
1640  611  msg = 'dvode--  maxord (=i1) .lt. 0  '
1641       call xerrwd (msg, 30, 11, 1, 1, maxord, 0, 0, zero, zero)
1642       go to 700
1643  612  msg = 'dvode--  mxstep (=i1) .lt. 0  '
1644       call xerrwd (msg, 30, 12, 1, 1, mxstep, 0, 0, zero, zero)
1645       go to 700
1646  613  msg = 'dvode--  mxhnil (=i1) .lt. 0  '
1647       call xerrwd (msg, 30, 13, 1, 1, mxhnil, 0, 0, zero, zero)
1648       go to 700
1649  614  msg = 'dvode--  tout (=r1) behind t (=r2)      '
1650       call xerrwd (msg, 40, 14, 1, 0, 0, 0, 2, tout, t)
1651       msg = '      integration direction is given by h0 (=r1)  '
1652       call xerrwd (msg, 50, 14, 1, 0, 0, 0, 1, h0, zero)
1653       go to 700
1654  615  msg = 'dvode--  hmax (=r1) .lt. 0.0  '
1655       call xerrwd (msg, 30, 15, 1, 0, 0, 0, 1, hmax, zero)
1656       go to 700
1657  616  msg = 'dvode--  hmin (=r1) .lt. 0.0  '
1658       call xerrwd (msg, 30, 16, 1, 0, 0, 0, 1, hmin, zero)
1659       go to 700
1660  617  continue
1661       msg='dvode--  rwork length needed, lenrw (=i1), exceeds lrw (=i2)'
1662       call xerrwd (msg, 60, 17, 1, 2, lenrw, lrw, 0, zero, zero)
1663       go to 700
1664  618  continue
1665       msg='dvode--  iwork length needed, leniw (=i1), exceeds liw (=i2)'
1666       call xerrwd (msg, 60, 18, 1, 2, leniw, liw, 0, zero, zero)
1667       go to 700
1668  619  msg = 'dvode--  rtol(i1) is r1 .lt. 0.0        '
1669       call xerrwd (msg, 40, 19, 1, 1, i, 0, 1, rtoli, zero)
1670       go to 700
1671  620  msg = 'dvode--  atol(i1) is r1 .lt. 0.0        '
1672       call xerrwd (msg, 40, 20, 1, 1, i, 0, 1, atoli, zero)
1673       go to 700
1674  621  ewti = rwork(lewt+i-1)
1675       msg = 'dvode--  ewt(i1) is r1 .le. 0.0         '
1676       call xerrwd (msg, 40, 21, 1, 1, i, 0, 1, ewti, zero)
1677       go to 700
1678  622  continue
1679       msg='dvode--  tout (=r1) too close to t(=r2) to start integration'
1680       call xerrwd (msg, 60, 22, 1, 0, 0, 0, 2, tout, t)
1681       go to 700
1682  623  continue
1683       msg='dvode--  itask = i1 and tout (=r1) behind tcur - hu (= r2)  '
1684       call xerrwd (msg, 60, 23, 1, 1, itask, 0, 2, tout, tp)
1685       go to 700
1686  624  continue
1687       msg='dvode--  itask = 4 or 5 and tcrit (=r1) behind tcur (=r2)   '
1688       call xerrwd (msg, 60, 24, 1, 0, 0, 0, 2, tcrit, tn)
1689       go to 700
1690  625  continue
1691       msg='dvode--  itask = 4 or 5 and tcrit (=r1) behind tout (=r2)   '
1692       call xerrwd (msg, 60, 25, 1, 0, 0, 0, 2, tcrit, tout)
1693       go to 700
1694  626  msg = 'dvode--  at start of problem, too much accuracy   '
1695       call xerrwd (msg, 50, 26, 1, 0, 0, 0, 0, zero, zero)
1696       msg='      requested for precision of machine:   see tolsf (=r1) '
1697       call xerrwd (msg, 60, 26, 1, 0, 0, 0, 1, tolsf, zero)
1698       rwork(14) = tolsf
1699       go to 700
1700  627  msg='dvode--  trouble from dvindy.  itask = i1, tout = r1.       '
1701       call xerrwd (msg, 60, 27, 1, 1, itask, 0, 1, tout, zero)
1703  700  continue
1704       istate = -3
1705       return
1707  800  msg = 'dvode--  run aborted:  apparent infinite loop     '
1708       call xerrwd (msg, 50, 303, 2, 0, 0, 0, 0, zero, zero)
1709       return
1710 !----------------------- end of subroutine dvode -----------------------
1711       end subroutine dvode
1712 !deck dvhin
1713       subroutine dvhin (n, t0, y0, ydot, f, rpar, ipar, tout, uround,   &
1714          ewt, itol, atol, y, temp, h0, niter, ier)
1715       external f
1716       double precision t0, y0, ydot, rpar, tout, uround, ewt, atol, y,   &
1717          temp, h0
1718       integer n, ipar, itol, niter, ier
1719       dimension y0(*), ydot(*), ewt(*), atol(*), y(*),   &
1720          temp(*), rpar(*), ipar(*)
1721 !-----------------------------------------------------------------------
1722 ! call sequence input -- n, t0, y0, ydot, f, rpar, ipar, tout, uround,
1723 !                        ewt, itol, atol, y, temp
1724 ! call sequence output -- h0, niter, ier
1725 ! common block variables accessed -- none
1727 ! subroutines called by dvhin:  f
1728 ! function routines called by dvhi: dvnorm
1729 !-----------------------------------------------------------------------
1730 ! this routine computes the step size, h0, to be attempted on the
1731 ! first step, when the user has not supplied a value for this.
1733 ! first we check that tout - t0 differs significantly from zero.  then
1734 ! an iteration is done to approximate the initial second derivative
1735 ! and this is used to define h from w.r.m.s.norm(h**2 * yddot / 2) = 1.
1736 ! a bias factor of 1/2 is applied to the resulting h.
1737 ! the sign of h0 is inferred from the initial values of tout and t0.
1739 ! communication with dvhin is done with the following variables:
1741 ! n      = size of ode system, input.
1742 ! t0     = initial value of independent variable, input.
1743 ! y0     = vector of initial conditions, input.
1744 ! ydot   = vector of initial first derivatives, input.
1745 ! f      = name of subroutine for right-hand side f(t,y), input.
1746 ! rpar, ipar = dummy names for user's real and integer work arrays.
1747 ! tout   = first output value of independent variable
1748 ! uround = machine unit roundoff
1749 ! ewt, itol, atol = error weights and tolerance parameters
1750 !                   as described in the driver routine, input.
1751 ! y, temp = work arrays of length n.
1752 ! h0     = step size to be attempted, output.
1753 ! niter  = number of iterations (and of f evaluations) to compute h0,
1754 !          output.
1755 ! ier    = the error flag, returned with the value
1756 !          ier = 0  if no trouble occurred, or
1757 !          ier = -1 if tout and t0 are considered too close to proceed.
1758 !-----------------------------------------------------------------------
1760 ! type declarations for local variables --------------------------------
1762       double precision afi, atoli, delyi, h, half, hg, hlb, hnew, hrat,   &
1763            hub, hun, pt1, t1, tdist, tround, two, yddnrm
1764       integer i, iter
1766 ! type declaration for function subroutines called ---------------------
1768 ! 27-oct-2005 rce - do not declare functions that are in the module
1769 !     double precision dvnorm
1770 !-----------------------------------------------------------------------
1771 ! the following fortran-77 declaration is to cause the values of the
1772 ! listed (local) variables to be saved between calls to this integrator.
1773 !-----------------------------------------------------------------------
1774       save half, hun, pt1, two
1775       data half /0.5d0/, hun /100.0d0/, pt1 /0.1d0/, two /2.0d0/
1777       niter = 0
1778       tdist = abs(tout - t0)
1779       tround = uround*max(abs(t0),abs(tout))
1780       if (tdist .lt. two*tround) go to 100
1782 ! set a lower bound on h based on the roundoff level in t0 and tout. ---
1783       hlb = hun*tround
1784 ! set an upper bound on h based on tout-t0 and the initial y and ydot. -
1785       hub = pt1*tdist
1786       atoli = atol(1)
1787       do 10 i = 1, n
1788         if (itol .eq. 2 .or. itol .eq. 4) atoli = atol(i)
1789         delyi = pt1*abs(y0(i)) + atoli
1790         afi = abs(ydot(i))
1791         if (afi*hub .gt. delyi) hub = delyi/afi
1792  10     continue
1794 ! set initial guess for h as geometric mean of upper and lower bounds. -
1795       iter = 0
1796       hg = sqrt(hlb*hub)
1797 ! if the bounds have crossed, exit with the mean value. ----------------
1798       if (hub .lt. hlb) then
1799         h0 = hg
1800         go to 90
1801       endif
1803 ! looping point for iteration. -----------------------------------------
1804  50   continue
1805 ! estimate the second derivative as a difference quotient in f. --------
1806       h = sign (hg, tout - t0)
1807       t1 = t0 + h
1808       do 60 i = 1, n
1809  60     y(i) = y0(i) + h*ydot(i)
1810       call f (n, t1, y, temp, rpar, ipar)
1811       do 70 i = 1, n
1812  70     temp(i) = (temp(i) - ydot(i))/h
1813       yddnrm = dvnorm (n, temp, ewt)
1814 ! get the corresponding new value of h. --------------------------------
1815       if (yddnrm*hub*hub .gt. two) then
1816         hnew = sqrt(two/yddnrm)
1817       else
1818         hnew = sqrt(hg*hub)
1819       endif
1820       iter = iter + 1
1821 !-----------------------------------------------------------------------
1822 ! test the stopping conditions.
1823 ! stop if the new and previous h values differ by a factor of .lt. 2.
1824 ! stop if four iterations have been done.  also, stop with previous h
1825 ! if hnew/hg .gt. 2 after first iteration, as this probably means that
1826 ! the second derivative value is bad because of cancellation error.
1827 !-----------------------------------------------------------------------
1828       if (iter .ge. 4) go to 80
1829       hrat = hnew/hg
1830       if ( (hrat .gt. half) .and. (hrat .lt. two) ) go to 80
1831       if ( (iter .ge. 2) .and. (hnew .gt. two*hg) ) then
1832         hnew = hg
1833         go to 80
1834       endif
1835       hg = hnew
1836       go to 50
1838 ! iteration done.  apply bounds, bias factor, and sign.  then exit. ----
1839  80   h0 = hnew*half
1840       if (h0 .lt. hlb) h0 = hlb
1841       if (h0 .gt. hub) h0 = hub
1842  90   h0 = sign(h0, tout - t0)
1843       niter = iter
1844       ier = 0
1845       return
1846 ! error return for tout - t0 too small. --------------------------------
1847  100  ier = -1
1848       return
1849 !----------------------- end of subroutine dvhin -----------------------
1850       end subroutine dvhin
1851 !deck dvindy
1852       subroutine dvindy (t, k, yh, ldyh, dky, iflag)
1853       double precision t, yh, dky
1854       integer k, ldyh, iflag
1855       dimension yh(ldyh,*), dky(*)
1856 !-----------------------------------------------------------------------
1857 ! call sequence input -- t, k, yh, ldyh
1858 ! call sequence output -- dky, iflag
1859 ! common block variables accessed:
1860 !     /dvod_cmn01/ --  h, tn, uround, l, n, nq
1861 !     /dvod_cmn02/ --  hu
1863 ! subroutines called by dvindy: dscal, xerrwd
1864 ! function routines called by dvindy: none
1865 !-----------------------------------------------------------------------
1866 ! dvindy computes interpolated values of the k-th derivative of the
1867 ! dependent variable vector y, and stores it in dky.  this routine
1868 ! is called within the package with k = 0 and t = tout, but may
1869 ! also be called by the user for any k up to the current order.
1870 ! (see detailed instructions in the usage documentation.)
1871 !-----------------------------------------------------------------------
1872 ! the computed values in dky are gotten by interpolation using the
1873 ! nordsieck history array yh.  this array corresponds uniquely to a
1874 ! vector-valued polynomial of degree nqcur or less, and dky is set
1875 ! to the k-th derivative of this polynomial at t.
1876 ! the formula for dky is:
1877 !              q
1878 !  dky(i)  =  sum  c(j,k) * (t - tn)**(j-k) * h**(-j) * yh(i,j+1)
1879 !             j=k
1880 ! where  c(j,k) = j*(j-1)*...*(j-k+1), q = nqcur, tn = tcur, h = hcur.
1881 ! the quantities  nq = nqcur, l = nq+1, n, tn, and h are
1882 ! communicated by common.  the above sum is done in reverse order.
1883 ! iflag is returned negative if either k or t is out of bounds.
1885 ! discussion above and comments in driver explain all variables.
1886 !-----------------------------------------------------------------------
1888 ! type declarations for labeled common block dvod_cmn01 --------------------
1890       double precision acnrm, ccmxj, conp, crate, drc, el,   &
1891            eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
1892            rc, rl1, tau, tq, tn, uround
1893       integer icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
1894               l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
1895               locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
1896               n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
1897               nslp, nyh
1899 ! type declarations for labeled common block dvod_cmn02 --------------------
1901       double precision hu
1902       integer ncfn, netf, nfe, nje, nlu, nni, nqu, nst
1904 ! type declarations for local variables --------------------------------
1906       double precision c, hun, r, s, tfuzz, tn1, tp, zero
1907       integer i, ic, j, jb, jb2, jj, jj1, jp1
1908       character*80 msg
1909 !-----------------------------------------------------------------------
1910 ! the following fortran-77 declaration is to cause the values of the
1911 ! listed (local) variables to be saved between calls to this integrator.
1912 !-----------------------------------------------------------------------
1913       save hun, zero
1915       common /dvod_cmn01/ acnrm, ccmxj, conp, crate, drc, el(13),   &
1916                       eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
1917                       rc, rl1, tau(13), tq(5), tn, uround,   &
1918                       icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
1919                       l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
1920                       locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
1921                       n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
1922                       nslp, nyh
1923       common /dvod_cmn02/ hu, ncfn, netf, nfe, nje, nlu, nni, nqu, nst
1925       data hun /100.0d0/, zero /0.0d0/
1927       iflag = 0
1928       if (k .lt. 0 .or. k .gt. nq) go to 80
1929       tfuzz = hun*uround*(tn + hu)
1930       tp = tn - hu - tfuzz
1931       tn1 = tn + tfuzz
1932       if ((t-tp)*(t-tn1) .gt. zero) go to 90
1934       s = (t - tn)/h
1935       ic = 1
1936       if (k .eq. 0) go to 15
1937       jj1 = l - k
1938       do 10 jj = jj1, nq
1939  10     ic = ic*jj
1940  15   c = real(ic)
1941       do 20 i = 1, n
1942  20     dky(i) = c*yh(i,l)
1943       if (k .eq. nq) go to 55
1944       jb2 = nq - k
1945       do 50 jb = 1, jb2
1946         j = nq - jb
1947         jp1 = j + 1
1948         ic = 1
1949         if (k .eq. 0) go to 35
1950         jj1 = jp1 - k
1951         do 30 jj = jj1, j
1952  30       ic = ic*jj
1953  35     c = real(ic)
1954         do 40 i = 1, n
1955  40       dky(i) = c*yh(i,jp1) + s*dky(i)
1956  50     continue
1957       if (k .eq. 0) return
1958  55   r = h**(-k)
1959       call dscal (n, r, dky, 1)
1960       return
1962  80   msg = 'dvindy-- k (=i1) illegal      '
1963       call xerrwd (msg, 30, 51, 1, 1, k, 0, 0, zero, zero)
1964       iflag = -1
1965       return
1966  90   msg = 'dvindy-- t (=r1) illegal      '
1967       call xerrwd (msg, 30, 52, 1, 0, 0, 0, 1, t, zero)
1968       msg='      t not in interval tcur - hu (= r1) to tcur (=r2)      '
1969       call xerrwd (msg, 60, 52, 1, 0, 0, 0, 2, tp, tn)
1970       iflag = -2
1971       return
1972 !----------------------- end of subroutine dvindy ----------------------
1973       end subroutine dvindy
1974 !deck dvstep
1975       subroutine dvstep (y, yh, ldyh, yh1, ewt, savf, vsav, acor,   &
1976                         wm, iwm, f, jac, psol, vnls, rpar, ipar)
1977       external f, jac, psol, vnls
1978       double precision y, yh, yh1, ewt, savf, vsav, acor, wm, rpar
1979       integer ldyh, iwm, ipar
1980       dimension y(*), yh(ldyh,*), yh1(*), ewt(*), savf(*), vsav(*),   &
1981          acor(*), wm(*), iwm(*), rpar(*), ipar(*)
1982 !-----------------------------------------------------------------------
1983 ! call sequence input -- y, yh, ldyh, yh1, ewt, savf, vsav,
1984 !                        acor, wm, iwm, f, jac, psol, vnls, rpar, ipar
1985 ! call sequence output -- yh, acor, wm, iwm
1986 ! common block variables accessed:
1987 !     /dvod_cmn01/  acnrm, el(13), h, hmin, hmxi, hnew, hscal, rc, tau(13),
1988 !               tq(5), tn, jcur, jstart, kflag, kuth,
1989 !               l, lmax, maxord, n, newq, nq, nqwait
1990 !     /dvod_cmn02/  hu, ncfn, netf, nfe, nqu, nst
1992 ! subroutines called by dvstep: f, daxpy, dcopy, dscal,
1993 !                               dvjust, vnls, dvset
1994 ! function routines called by dvstep: dvnorm
1995 !-----------------------------------------------------------------------
1996 ! dvstep performs one step of the integration of an initial value
1997 ! problem for a system of ordinary differential equations.
1998 ! dvstep calls subroutine vnls for the solution of the nonlinear system
1999 ! arising in the time step.  thus it is independent of the problem
2000 ! jacobian structure and the type of nonlinear system solution method.
2001 ! dvstep returns a completion flag kflag (in common).
2002 ! a return with kflag = -1 or -2 means either abs(h) = hmin or 10
2003 ! consecutive failures occurred.  on a return with kflag negative,
2004 ! the values of tn and the yh array are as of the beginning of the last
2005 ! step, and h is the last step size attempted.
2007 ! communication with dvstep is done with the following variables:
2009 ! y      = an array of length n used for the dependent variable vector.
2010 ! yh     = an ldyh by lmax array containing the dependent variables
2011 !          and their approximate scaled derivatives, where
2012 !          lmax = maxord + 1.  yh(i,j+1) contains the approximate
2013 !          j-th derivative of y(i), scaled by h**j/factorial(j)
2014 !          (j = 0,1,...,nq).  on entry for the first step, the first
2015 !          two columns of yh must be set from the initial values.
2016 ! ldyh   = a constant integer .ge. n, the first dimension of yh.
2017 !          n is the number of odes in the system.
2018 ! yh1    = a one-dimensional array occupying the same space as yh.
2019 ! ewt    = an array of length n containing multiplicative weights
2020 !          for local error measurements.  local errors in y(i) are
2021 !          compared to 1.0/ewt(i) in various error tests.
2022 ! savf   = an array of working storage, of length n.
2023 !          also used for input of yh(*,maxord+2) when jstart = -1
2024 !          and maxord .lt. the current order nq.
2025 ! vsav   = a work array of length n passed to subroutine vnls.
2026 ! acor   = a work array of length n, used for the accumulated
2027 !          corrections.  on a successful return, acor(i) contains
2028 !          the estimated one-step local error in y(i).
2029 ! wm,iwm = real and integer work arrays associated with matrix
2030 !          operations in vnls.
2031 ! f      = dummy name for the user supplied subroutine for f.
2032 ! jac    = dummy name for the user supplied jacobian subroutine.
2033 ! psol   = dummy name for the subroutine passed to vnls, for
2034 !          possible use there.
2035 ! vnls   = dummy name for the nonlinear system solving subroutine,
2036 !          whose real name is dependent on the method used.
2037 ! rpar, ipar = dummy names for user's real and integer work arrays.
2038 !-----------------------------------------------------------------------
2040 ! type declarations for labeled common block dvod_cmn01 --------------------
2042       double precision acnrm, ccmxj, conp, crate, drc, el,   &
2043            eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
2044            rc, rl1, tau, tq, tn, uround
2045       integer icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
2046               l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
2047               locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
2048               n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
2049               nslp, nyh
2051 ! type declarations for labeled common block dvod_cmn02 --------------------
2053       double precision hu
2054       integer ncfn, netf, nfe, nje, nlu, nni, nqu, nst
2056 ! type declarations for local variables --------------------------------
2058       double precision addon, bias1,bias2,bias3, cnquot, ddn, dsm, dup,   &
2059            etacf, etamin, etamx1, etamx2, etamx3, etamxf,   &
2060            etaq, etaqm1, etaqp1, flotl, one, onepsm,   &
2061            r, thresh, told, zero
2062       integer i, i1, i2, iback, j, jb, kfc, kfh, mxncf, ncf, nflag
2064 ! type declaration for function subroutines called ---------------------
2066 ! 27-oct-2005 rce - do not declare functions that are in the module
2067 !     double precision dvnorm
2068 !-----------------------------------------------------------------------
2069 ! the following fortran-77 declaration is to cause the values of the
2070 ! listed (local) variables to be saved between calls to this integrator.
2071 !-----------------------------------------------------------------------
2072       save addon, bias1, bias2, bias3,   &
2073            etacf, etamin, etamx1, etamx2, etamx3, etamxf, etaq, etaqm1,   &
2074            kfc, kfh, mxncf, onepsm, thresh, one, zero
2075 !-----------------------------------------------------------------------
2076       common /dvod_cmn01/ acnrm, ccmxj, conp, crate, drc, el(13),   &
2077                       eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
2078                       rc, rl1, tau(13), tq(5), tn, uround,   &
2079                       icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
2080                       l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
2081                       locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
2082                       n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
2083                       nslp, nyh
2084       common /dvod_cmn02/ hu, ncfn, netf, nfe, nje, nlu, nni, nqu, nst
2086       data kfc/-3/, kfh/-7/, mxncf/10/
2087       data addon  /1.0d-6/,    bias1  /6.0d0/,     bias2  /6.0d0/,   &
2088            bias3  /10.0d0/,    etacf  /0.25d0/,    etamin /0.1d0/,   &
2089            etamxf /0.2d0/,     etamx1 /1.0d4/,     etamx2 /10.0d0/,   &
2090            etamx3 /10.0d0/,    onepsm /1.00001d0/, thresh /1.5d0/
2091       data one/1.0d0/, zero/0.0d0/
2093       kflag = 0
2094       told = tn
2095       ncf = 0
2096       jcur = 0
2097       nflag = 0
2098       if (jstart .gt. 0) go to 20
2099       if (jstart .eq. -1) go to 100
2100 !-----------------------------------------------------------------------
2101 ! on the first call, the order is set to 1, and other variables are
2102 ! initialized.  etamax is the maximum ratio by which h can be increased
2103 ! in a single step.  it is normally 10, but is larger during the
2104 ! first step to compensate for the small initial h.  if a failure
2105 ! occurs (in corrector convergence or error test), etamax is set to 1
2106 ! for the next increase.
2107 !-----------------------------------------------------------------------
2108       lmax = maxord + 1
2109       nq = 1
2110       l = 2
2111       nqnyh = nq*ldyh
2112       tau(1) = h
2113       prl1 = one
2114       rc = zero
2115       etamax = etamx1
2116       nqwait = 2
2117       hscal = h
2118       go to 200
2119 !-----------------------------------------------------------------------
2120 ! take preliminary actions on a normal continuation step (jstart.gt.0).
2121 ! if the driver changed h, then eta must be reset and newh set to 1.
2122 ! if a change of order was dictated on the previous step, then
2123 ! it is done here and appropriate adjustments in the history are made.
2124 ! on an order decrease, the history array is adjusted by dvjust.
2125 ! on an order increase, the history array is augmented by a column.
2126 ! on a change of step size h, the history array yh is rescaled.
2127 !-----------------------------------------------------------------------
2128  20   continue
2129       if (kuth .eq. 1) then
2130         eta = min(eta,h/hscal)
2131         newh = 1
2132         endif
2133  50   if (newh .eq. 0) go to 200
2134       if (newq .eq. nq) go to 150
2135       if (newq .lt. nq) then
2136         call dvjust (yh, ldyh, -1)
2137         nq = newq
2138         l = nq + 1
2139         nqwait = l
2140         go to 150
2141         endif
2142       if (newq .gt. nq) then
2143         call dvjust (yh, ldyh, 1)
2144         nq = newq
2145         l = nq + 1
2146         nqwait = l
2147         go to 150
2148       endif
2149 !-----------------------------------------------------------------------
2150 ! the following block handles preliminaries needed when jstart = -1.
2151 ! if n was reduced, zero out part of yh to avoid undefined references.
2152 ! if maxord was reduced to a value less than the tentative order newq,
2153 ! then nq is set to maxord, and a new h ratio eta is chosen.
2154 ! otherwise, we take the same preliminary actions as for jstart .gt. 0.
2155 ! in any case, nqwait is reset to l = nq + 1 to prevent further
2156 ! changes in order for that many steps.
2157 ! the new h ratio eta is limited by the input h if kuth = 1,
2158 ! by hmin if kuth = 0, and by hmxi in any case.
2159 ! finally, the history array yh is rescaled.
2160 !-----------------------------------------------------------------------
2161  100  continue
2162       lmax = maxord + 1
2163       if (n .eq. ldyh) go to 120
2164       i1 = 1 + (newq + 1)*ldyh
2165       i2 = (maxord + 1)*ldyh
2166       if (i1 .gt. i2) go to 120
2167       do 110 i = i1, i2
2168  110    yh1(i) = zero
2169  120  if (newq .le. maxord) go to 140
2170       flotl = real(lmax)
2171       if (maxord .lt. nq-1) then
2172         ddn = dvnorm (n, savf, ewt)/tq(1)
2173         eta = one/((bias1*ddn)**(one/flotl) + addon)
2174         endif
2175       if (maxord .eq. nq .and. newq .eq. nq+1) eta = etaq
2176       if (maxord .eq. nq-1 .and. newq .eq. nq+1) then
2177         eta = etaqm1
2178         call dvjust (yh, ldyh, -1)
2179         endif
2180       if (maxord .eq. nq-1 .and. newq .eq. nq) then
2181         ddn = dvnorm (n, savf, ewt)/tq(1)
2182         eta = one/((bias1*ddn)**(one/flotl) + addon)
2183         call dvjust (yh, ldyh, -1)
2184         endif
2185       eta = min(eta,one)
2186       nq = maxord
2187       l = lmax
2188  140  if (kuth .eq. 1) eta = min(eta,abs(h/hscal))
2189       if (kuth .eq. 0) eta = max(eta,hmin/abs(hscal))
2190       eta = eta/max(one,abs(hscal)*hmxi*eta)
2191       newh = 1
2192       nqwait = l
2193       if (newq .le. maxord) go to 50
2194 ! rescale the history array for a change in h by a factor of eta. ------
2195  150  r = one
2196       do 180 j = 2, l
2197         r = r*eta
2198         call dscal (n, r, yh(1,j), 1 )
2199  180    continue
2200       h = hscal*eta
2201       hscal = h
2202       rc = rc*eta
2203       nqnyh = nq*ldyh
2204 !-----------------------------------------------------------------------
2205 ! this section computes the predicted values by effectively
2206 ! multiplying the yh array by the pascal triangle matrix.
2207 ! dvset is called to calculate all integration coefficients.
2208 ! rc is the ratio of new to old values of the coefficient h/el(2)=h/l1.
2209 !-----------------------------------------------------------------------
2210  200  tn = tn + h
2211       i1 = nqnyh + 1
2212       do 220 jb = 1, nq
2213         i1 = i1 - ldyh
2214         do 210 i = i1, nqnyh
2215  210      yh1(i) = yh1(i) + yh1(i+ldyh)
2216  220  continue
2217       call dvset
2218       rl1 = one/el(2)
2219       rc = rc*(rl1/prl1)
2220       prl1 = rl1
2222 ! call the nonlinear system solver. ------------------------------------
2224       call vnls (y, yh, ldyh, vsav, savf, ewt, acor, iwm, wm,   &
2225                  f, jac, psol, nflag, rpar, ipar)
2227       if (nflag .eq. 0) go to 450
2228 !-----------------------------------------------------------------------
2229 ! the vnls routine failed to achieve convergence (nflag .ne. 0).
2230 ! the yh array is retracted to its values before prediction.
2231 ! the step size h is reduced and the step is retried, if possible.
2232 ! otherwise, an error exit is taken.
2233 !-----------------------------------------------------------------------
2234         ncf = ncf + 1
2235         ncfn = ncfn + 1
2236         etamax = one
2237         tn = told
2238         i1 = nqnyh + 1
2239         do 430 jb = 1, nq
2240           i1 = i1 - ldyh
2241           do 420 i = i1, nqnyh
2242  420        yh1(i) = yh1(i) - yh1(i+ldyh)
2243  430      continue
2244         if (nflag .lt. -1) go to 680
2245         if (abs(h) .le. hmin*onepsm) go to 670
2246         if (ncf .eq. mxncf) go to 670
2247         eta = etacf
2248         eta = max(eta,hmin/abs(h))
2249         nflag = -1
2250         go to 150
2251 !-----------------------------------------------------------------------
2252 ! the corrector has converged (nflag = 0).  the local error test is
2253 ! made and control passes to statement 500 if it fails.
2254 !-----------------------------------------------------------------------
2255  450  continue
2256       dsm = acnrm/tq(2)
2257       if (dsm .gt. one) go to 500
2258 !-----------------------------------------------------------------------
2259 ! after a successful step, update the yh and tau arrays and decrement
2260 ! nqwait.  if nqwait is then 1 and nq .lt. maxord, then acor is saved
2261 ! for use in a possible order increase on the next step.
2262 ! if etamax = 1 (a failure occurred this step), keep nqwait .ge. 2.
2263 !-----------------------------------------------------------------------
2264       kflag = 0
2265       nst = nst + 1
2266       hu = h
2267       nqu = nq
2268       do 470 iback = 1, nq
2269         i = l - iback
2270  470    tau(i+1) = tau(i)
2271       tau(1) = h
2272       do 480 j = 1, l
2273         call daxpy (n, el(j), acor, 1, yh(1,j), 1 )
2274  480    continue
2275       nqwait = nqwait - 1
2276       if ((l .eq. lmax) .or. (nqwait .ne. 1)) go to 490
2277       call dcopy (n, acor, 1, yh(1,lmax), 1 )
2278       conp = tq(5)
2279  490  if (etamax .ne. one) go to 560
2280       if (nqwait .lt. 2) nqwait = 2
2281       newq = nq
2282       newh = 0
2283       eta = one
2284       hnew = h
2285       go to 690
2286 !-----------------------------------------------------------------------
2287 ! the error test failed.  kflag keeps track of multiple failures.
2288 ! restore tn and the yh array to their previous values, and prepare
2289 ! to try the step again.  compute the optimum step size for the
2290 ! same order.  after repeated failures, h is forced to decrease
2291 ! more rapidly.
2292 !-----------------------------------------------------------------------
2293  500  kflag = kflag - 1
2294       netf = netf + 1
2295       nflag = -2
2296       tn = told
2297       i1 = nqnyh + 1
2298       do 520 jb = 1, nq
2299         i1 = i1 - ldyh
2300         do 510 i = i1, nqnyh
2301  510      yh1(i) = yh1(i) - yh1(i+ldyh)
2302  520  continue
2303       if (abs(h) .le. hmin*onepsm) go to 660
2304       etamax = one
2305       if (kflag .le. kfc) go to 530
2306 ! compute ratio of new h to current h at the current order. ------------
2307       flotl = real(l)
2308       eta = one/((bias2*dsm)**(one/flotl) + addon)
2309       eta = max(eta,hmin/abs(h),etamin)
2310       if ((kflag .le. -2) .and. (eta .gt. etamxf)) eta = etamxf
2311       go to 150
2312 !-----------------------------------------------------------------------
2313 ! control reaches this section if 3 or more consecutive failures
2314 ! have occurred.  it is assumed that the elements of the yh array
2315 ! have accumulated errors of the wrong order.  the order is reduced
2316 ! by one, if possible.  then h is reduced by a factor of 0.1 and
2317 ! the step is retried.  after a total of 7 consecutive failures,
2318 ! an exit is taken with kflag = -1.
2319 !-----------------------------------------------------------------------
2320  530  if (kflag .eq. kfh) go to 660
2321       if (nq .eq. 1) go to 540
2322       eta = max(etamin,hmin/abs(h))
2323       call dvjust (yh, ldyh, -1)
2324       l = nq
2325       nq = nq - 1
2326       nqwait = l
2327       go to 150
2328  540  eta = max(etamin,hmin/abs(h))
2329       h = h*eta
2330       hscal = h
2331       tau(1) = h
2332       call f (n, tn, y, savf, rpar, ipar)
2333       nfe = nfe + 1
2334       do 550 i = 1, n
2335  550    yh(i,2) = h*savf(i)
2336       nqwait = 10
2337       go to 200
2338 !-----------------------------------------------------------------------
2339 ! if nqwait = 0, an increase or decrease in order by one is considered.
2340 ! factors etaq, etaqm1, etaqp1 are computed by which h could
2341 ! be multiplied at order q, q-1, or q+1, respectively.
2342 ! the largest of these is determined, and the new order and
2343 ! step size set accordingly.
2344 ! a change of h or nq is made only if h increases by at least a
2345 ! factor of thresh.  if an order change is considered and rejected,
2346 ! then nqwait is set to 2 (reconsider it after 2 steps).
2347 !-----------------------------------------------------------------------
2348 ! compute ratio of new h to current h at the current order. ------------
2349  560  flotl = real(l)
2350       etaq = one/((bias2*dsm)**(one/flotl) + addon)
2351       if (nqwait .ne. 0) go to 600
2352       nqwait = 2
2353       etaqm1 = zero
2354       if (nq .eq. 1) go to 570
2355 ! compute ratio of new h to current h at the current order less one. ---
2356       ddn = dvnorm (n, yh(1,l), ewt)/tq(1)
2357       etaqm1 = one/((bias1*ddn)**(one/(flotl - one)) + addon)
2358  570  etaqp1 = zero
2359       if (l .eq. lmax) go to 580
2360 ! compute ratio of new h to current h at current order plus one. -------
2361       cnquot = (tq(5)/conp)*(h/tau(2))**l
2362       do 575 i = 1, n
2363  575    savf(i) = acor(i) - cnquot*yh(i,lmax)
2364       dup = dvnorm (n, savf, ewt)/tq(3)
2365       etaqp1 = one/((bias3*dup)**(one/(flotl + one)) + addon)
2366  580  if (etaq .ge. etaqp1) go to 590
2367       if (etaqp1 .gt. etaqm1) go to 620
2368       go to 610
2369  590  if (etaq .lt. etaqm1) go to 610
2370  600  eta = etaq
2371       newq = nq
2372       go to 630
2373  610  eta = etaqm1
2374       newq = nq - 1
2375       go to 630
2376  620  eta = etaqp1
2377       newq = nq + 1
2378       call dcopy (n, acor, 1, yh(1,lmax), 1)
2379 ! test tentative new h against thresh, etamax, and hmxi, then exit. ----
2380  630  if (eta .lt. thresh .or. etamax .eq. one) go to 640
2381       eta = min(eta,etamax)
2382       eta = eta/max(one,abs(h)*hmxi*eta)
2383       newh = 1
2384       hnew = h*eta
2385       go to 690
2386  640  newq = nq
2387       newh = 0
2388       eta = one
2389       hnew = h
2390       go to 690
2391 !-----------------------------------------------------------------------
2392 ! all returns are made through this section.
2393 ! on a successful return, etamax is reset and acor is scaled.
2394 !-----------------------------------------------------------------------
2395  660  kflag = -1
2396       go to 720
2397  670  kflag = -2
2398       go to 720
2399  680  if (nflag .eq. -2) kflag = -3
2400       if (nflag .eq. -3) kflag = -4
2401       go to 720
2402  690  etamax = etamx3
2403       if (nst .le. 10) etamax = etamx2
2404  700  r = one/tq(2)
2405       call dscal (n, r, acor, 1)
2406  720  jstart = 1
2407       return
2408 !----------------------- end of subroutine dvstep ----------------------
2409       end subroutine dvstep
2410 !deck dvset
2411       subroutine dvset
2412 !-----------------------------------------------------------------------
2413 ! call sequence communication: none
2414 ! common block variables accessed:
2415 !     /dvod_cmn01/ -- el(13), h, tau(13), tq(5), l(= nq + 1),
2416 !                 meth, nq, nqwait
2418 ! subroutines called by dvset: none
2419 ! function routines called by dvset: none
2420 !-----------------------------------------------------------------------
2421 ! dvset is called by dvstep and sets coefficients for use there.
2423 ! for each order nq, the coefficients in el are calculated by use of
2424 !  the generating polynomial lambda(x), with coefficients el(i).
2425 !      lambda(x) = el(1) + el(2)*x + ... + el(nq+1)*(x**nq).
2426 ! for the backward differentiation formulas,
2427 !                                     nq-1
2428 !      lambda(x) = (1 + x/xi*(nq)) * product (1 + x/xi(i) ) .
2429 !                                     i = 1
2430 ! for the adams formulas,
2431 !                              nq-1
2432 !      (d/dx) lambda(x) = c * product (1 + x/xi(i) ) ,
2433 !                              i = 1
2434 !      lambda(-1) = 0,    lambda(0) = 1,
2435 ! where c is a normalization constant.
2436 ! in both cases, xi(i) is defined by
2437 !      h*xi(i) = t sub n  -  t sub (n-i)
2438 !              = h + tau(1) + tau(2) + ... tau(i-1).
2441 ! in addition to variables described previously, communication
2442 ! with dvset uses the following:
2443 !   tau    = a vector of length 13 containing the past nq values
2444 !            of h.
2445 !   el     = a vector of length 13 in which vset stores the
2446 !            coefficients for the corrector formula.
2447 !   tq     = a vector of length 5 in which vset stores constants
2448 !            used for the convergence test, the error test, and the
2449 !            selection of h at a new order.
2450 !   meth   = the basic method indicator.
2451 !   nq     = the current order.
2452 !   l      = nq + 1, the length of the vector stored in el, and
2453 !            the number of columns of the yh array being used.
2454 !   nqwait = a counter controlling the frequency of order changes.
2455 !            an order change is about to be considered if nqwait = 1.
2456 !-----------------------------------------------------------------------
2458 ! type declarations for labeled common block dvod_cmn01 --------------------
2460       double precision acnrm, ccmxj, conp, crate, drc, el,   &
2461            eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
2462            rc, rl1, tau, tq, tn, uround
2463       integer icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
2464               l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
2465               locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
2466               n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
2467               nslp, nyh
2469 ! type declarations for local variables --------------------------------
2471       double precision ahatn0, alph0, cnqm1, cortes, csum, elp, em,   &
2472            em0, floti, flotl, flotnq, hsum, one, rxi, rxis, s, six,   &
2473            t1, t2, t3, t4, t5, t6, two, xi, zero
2474       integer i, iback, j, jp1, nqm1, nqm2
2476       dimension em(13)
2477 !-----------------------------------------------------------------------
2478 ! the following fortran-77 declaration is to cause the values of the
2479 ! listed (local) variables to be saved between calls to this integrator.
2480 !-----------------------------------------------------------------------
2481       save cortes, one, six, two, zero
2483       common /dvod_cmn01/ acnrm, ccmxj, conp, crate, drc, el(13),   &
2484                       eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
2485                       rc, rl1, tau(13), tq(5), tn, uround,   &
2486                       icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
2487                       l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
2488                       locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
2489                       n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
2490                       nslp, nyh
2492       data cortes /0.1d0/
2493       data one  /1.0d0/, six /6.0d0/, two /2.0d0/, zero /0.0d0/
2495       flotl = real(l)
2496       nqm1 = nq - 1
2497       nqm2 = nq - 2
2498       go to (100, 200), meth
2500 ! set coefficients for adams methods. ----------------------------------
2501  100  if (nq .ne. 1) go to 110
2502       el(1) = one
2503       el(2) = one
2504       tq(1) = one
2505       tq(2) = two
2506       tq(3) = six*tq(2)
2507       tq(5) = one
2508       go to 300
2509  110  hsum = h
2510       em(1) = one
2511       flotnq = flotl - one
2512       do 115 i = 2, l
2513  115    em(i) = zero
2514       do 150 j = 1, nqm1
2515         if ((j .ne. nqm1) .or. (nqwait .ne. 1)) go to 130
2516         s = one
2517         csum = zero
2518         do 120 i = 1, nqm1
2519           csum = csum + s*em(i)/real(i+1)
2520  120      s = -s
2521         tq(1) = em(nqm1)/(flotnq*csum)
2522  130    rxi = h/hsum
2523         do 140 iback = 1, j
2524           i = (j + 2) - iback
2525  140      em(i) = em(i) + em(i-1)*rxi
2526         hsum = hsum + tau(j)
2527  150    continue
2528 ! compute integral from -1 to 0 of polynomial and of x times it. -------
2529       s = one
2530       em0 = zero
2531       csum = zero
2532       do 160 i = 1, nq
2533         floti = real(i)
2534         em0 = em0 + s*em(i)/floti
2535         csum = csum + s*em(i)/(floti+one)
2536  160    s = -s
2537 ! in el, form coefficients of normalized integrated polynomial. --------
2538       s = one/em0
2539       el(1) = one
2540       do 170 i = 1, nq
2541  170    el(i+1) = s*em(i)/real(i)
2542       xi = hsum/h
2543       tq(2) = xi*em0/csum
2544       tq(5) = xi/el(l)
2545       if (nqwait .ne. 1) go to 300
2546 ! for higher order control constant, multiply polynomial by 1+x/xi(q). -
2547       rxi = one/xi
2548       do 180 iback = 1, nq
2549         i = (l + 1) - iback
2550  180    em(i) = em(i) + em(i-1)*rxi
2551 ! compute integral of polynomial. --------------------------------------
2552       s = one
2553       csum = zero
2554       do 190 i = 1, l
2555         csum = csum + s*em(i)/real(i+1)
2556  190    s = -s
2557       tq(3) = flotl*em0/csum
2558       go to 300
2560 ! set coefficients for bdf methods. ------------------------------------
2561  200  do 210 i = 3, l
2562  210    el(i) = zero
2563       el(1) = one
2564       el(2) = one
2565       alph0 = -one
2566       ahatn0 = -one
2567       hsum = h
2568       rxi = one
2569       rxis = one
2570       if (nq .eq. 1) go to 240
2571       do 230 j = 1, nqm2
2572 ! in el, construct coefficients of (1+x/xi(1))*...*(1+x/xi(j+1)). ------
2573         hsum = hsum + tau(j)
2574         rxi = h/hsum
2575         jp1 = j + 1
2576         alph0 = alph0 - one/real(jp1)
2577         do 220 iback = 1, jp1
2578           i = (j + 3) - iback
2579  220      el(i) = el(i) + el(i-1)*rxi
2580  230    continue
2581       alph0 = alph0 - one/real(nq)
2582       rxis = -el(2) - alph0
2583       hsum = hsum + tau(nqm1)
2584       rxi = h/hsum
2585       ahatn0 = -el(2) - rxi
2586       do 235 iback = 1, nq
2587         i = (nq + 2) - iback
2588  235    el(i) = el(i) + el(i-1)*rxis
2589  240  t1 = one - ahatn0 + alph0
2590       t2 = one + real(nq)*t1
2591       tq(2) = abs(alph0*t2/t1)
2592       tq(5) = abs(t2/(el(l)*rxi/rxis))
2593       if (nqwait .ne. 1) go to 300
2594       cnqm1 = rxis/el(l)
2595       t3 = alph0 + one/real(nq)
2596       t4 = ahatn0 + rxi
2597       elp = t3/(one - t4 + t3)
2598       tq(1) = abs(elp/cnqm1)
2599       hsum = hsum + tau(nq)
2600       rxi = h/hsum
2601       t5 = alph0 - one/real(nq+1)
2602       t6 = ahatn0 - rxi
2603       elp = t2/(one - t6 + t5)
2604       tq(3) = abs(elp*rxi*(flotl + one)*t5)
2605  300  tq(4) = cortes*tq(2)
2606       return
2607 !----------------------- end of subroutine dvset -----------------------
2608       end subroutine dvset
2609 !deck dvjust
2610       subroutine dvjust (yh, ldyh, iord)
2611       double precision yh
2612       integer ldyh, iord
2613       dimension yh(ldyh,*)
2614 !-----------------------------------------------------------------------
2615 ! call sequence input -- yh, ldyh, iord
2616 ! call sequence output -- yh
2617 ! common block input -- nq, meth, lmax, hscal, tau(13), n
2618 ! common block variables accessed:
2619 !     /dvod_cmn01/ -- hscal, tau(13), lmax, meth, n, nq,
2621 ! subroutines called by dvjust: daxpy
2622 ! function routines called by dvjust: none
2623 !-----------------------------------------------------------------------
2624 ! this subroutine adjusts the yh array on reduction of order,
2625 ! and also when the order is increased for the stiff option (meth = 2).
2626 ! communication with dvjust uses the following:
2627 ! iord  = an integer flag used when meth = 2 to indicate an order
2628 !         increase (iord = +1) or an order decrease (iord = -1).
2629 ! hscal = step size h used in scaling of nordsieck array yh.
2630 !         (if iord = +1, dvjust assumes that hscal = tau(1).)
2631 ! see references 1 and 2 for details.
2632 !-----------------------------------------------------------------------
2634 ! type declarations for labeled common block dvod_cmn01 --------------------
2636       double precision acnrm, ccmxj, conp, crate, drc, el,   &
2637            eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
2638            rc, rl1, tau, tq, tn, uround
2639       integer icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
2640               l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
2641               locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
2642               n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
2643               nslp, nyh
2645 ! type declarations for local variables --------------------------------
2647       double precision alph0, alph1, hsum, one, prod, t1, xi,xiold, zero
2648       integer i, iback, j, jp1, lp1, nqm1, nqm2, nqp1
2649 !-----------------------------------------------------------------------
2650 ! the following fortran-77 declaration is to cause the values of the
2651 ! listed (local) variables to be saved between calls to this integrator.
2652 !-----------------------------------------------------------------------
2653       save one, zero
2655       common /dvod_cmn01/ acnrm, ccmxj, conp, crate, drc, el(13),   &
2656                       eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
2657                       rc, rl1, tau(13), tq(5), tn, uround,   &
2658                       icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
2659                       l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
2660                       locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
2661                       n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
2662                       nslp, nyh
2664       data one /1.0d0/, zero /0.0d0/
2666       if ((nq .eq. 2) .and. (iord .ne. 1)) return
2667       nqm1 = nq - 1
2668       nqm2 = nq - 2
2669       go to (100, 200), meth
2670 !-----------------------------------------------------------------------
2671 ! nonstiff option...
2672 ! check to see if the order is being increased or decreased.
2673 !-----------------------------------------------------------------------
2674  100  continue
2675       if (iord .eq. 1) go to 180
2676 ! order decrease. ------------------------------------------------------
2677       do 110 j = 1, lmax
2678  110    el(j) = zero
2679       el(2) = one
2680       hsum = zero
2681       do 130 j = 1, nqm2
2682 ! construct coefficients of x*(x+xi(1))*...*(x+xi(j)). -----------------
2683         hsum = hsum + tau(j)
2684         xi = hsum/hscal
2685         jp1 = j + 1
2686         do 120 iback = 1, jp1
2687           i = (j + 3) - iback
2688  120      el(i) = el(i)*xi + el(i-1)
2689  130    continue
2690 ! construct coefficients of integrated polynomial. ---------------------
2691       do 140 j = 2, nqm1
2692  140    el(j+1) = real(nq)*el(j)/real(j)
2693 ! subtract correction terms from yh array. -----------------------------
2694       do 170 j = 3, nq
2695         do 160 i = 1, n
2696  160      yh(i,j) = yh(i,j) - yh(i,l)*el(j)
2697  170    continue
2698       return
2699 ! order increase. ------------------------------------------------------
2700 ! zero out next column in yh array. ------------------------------------
2701  180  continue
2702       lp1 = l + 1
2703       do 190 i = 1, n
2704  190    yh(i,lp1) = zero
2705       return
2706 !-----------------------------------------------------------------------
2707 ! stiff option...
2708 ! check to see if the order is being increased or decreased.
2709 !-----------------------------------------------------------------------
2710  200  continue
2711       if (iord .eq. 1) go to 300
2712 ! order decrease. ------------------------------------------------------
2713       do 210 j = 1, lmax
2714  210    el(j) = zero
2715       el(3) = one
2716       hsum = zero
2717       do 230 j = 1,nqm2
2718 ! construct coefficients of x*x*(x+xi(1))*...*(x+xi(j)). ---------------
2719         hsum = hsum + tau(j)
2720         xi = hsum/hscal
2721         jp1 = j + 1
2722         do 220 iback = 1, jp1
2723           i = (j + 4) - iback
2724  220      el(i) = el(i)*xi + el(i-1)
2725  230    continue
2726 ! subtract correction terms from yh array. -----------------------------
2727       do 250 j = 3,nq
2728         do 240 i = 1, n
2729  240      yh(i,j) = yh(i,j) - yh(i,l)*el(j)
2730  250    continue
2731       return
2732 ! order increase. ------------------------------------------------------
2733  300  do 310 j = 1, lmax
2734  310    el(j) = zero
2735       el(3) = one
2736       alph0 = -one
2737       alph1 = one
2738       prod = one
2739       xiold = one
2740       hsum = hscal
2741       if (nq .eq. 1) go to 340
2742       do 330 j = 1, nqm1
2743 ! construct coefficients of x*x*(x+xi(1))*...*(x+xi(j)). ---------------
2744         jp1 = j + 1
2745         hsum = hsum + tau(jp1)
2746         xi = hsum/hscal
2747         prod = prod*xi
2748         alph0 = alph0 - one/real(jp1)
2749         alph1 = alph1 + one/xi
2750         do 320 iback = 1, jp1
2751           i = (j + 4) - iback
2752  320      el(i) = el(i)*xiold + el(i-1)
2753         xiold = xi
2754  330    continue
2755  340  continue
2756       t1 = (-alph0 - alph1)/prod
2757 ! load column l + 1 in yh array. ---------------------------------------
2758       lp1 = l + 1
2759       do 350 i = 1, n
2760  350    yh(i,lp1) = t1*yh(i,lmax)
2761 ! add correction terms to yh array. ------------------------------------
2762       nqp1 = nq + 1
2763       do 370 j = 3, nqp1
2764         call daxpy (n, el(j), yh(1,lp1), 1, yh(1,j), 1 )
2765  370  continue
2766       return
2767 !----------------------- end of subroutine dvjust ----------------------
2768       end subroutine dvjust
2769 !deck dvnlsd
2770       subroutine dvnlsd (y, yh, ldyh, vsav, savf, ewt, acor, iwm, wm,   &
2771                        f, jac, pdum, nflag, rpar, ipar)
2772       external f, jac, pdum
2773       double precision y, yh, vsav, savf, ewt, acor, wm, rpar
2774       integer ldyh, iwm, nflag, ipar
2775       dimension y(*), yh(ldyh,*), vsav(*), savf(*), ewt(*), acor(*),   &
2776                 iwm(*), wm(*), rpar(*), ipar(*)
2777 !-----------------------------------------------------------------------
2778 ! call sequence input -- y, yh, ldyh, savf, ewt, acor, iwm, wm,
2779 !                        f, jac, nflag, rpar, ipar
2780 ! call sequence output -- yh, acor, wm, iwm, nflag
2781 ! common block variables accessed:
2782 !     /dvod_cmn01/ acnrm, crate, drc, h, rc, rl1, tq(5), tn, icf,
2783 !                jcur, meth, miter, n, nslp
2784 !     /dvod_cmn02/ hu, ncfn, netf, nfe, nje, nlu, nni, nqu, nst
2786 ! subroutines called by dvnlsd: f, daxpy, dcopy, dscal, dvjac, dvsol
2787 ! function routines called by dvnlsd: dvnorm
2788 !-----------------------------------------------------------------------
2789 ! subroutine dvnlsd is a nonlinear system solver, which uses functional
2790 ! iteration or a chord (modified newton) method.  for the chord method
2791 ! direct linear algebraic system solvers are used.  subroutine dvnlsd
2792 ! then handles the corrector phase of this integration package.
2794 ! communication with dvnlsd is done with the following variables. (for
2795 ! more details, please see the comments in the driver subroutine.)
2797 ! y          = the dependent variable, a vector of length n, input.
2798 ! yh         = the nordsieck (taylor) array, ldyh by lmax, input
2799 !              and output.  on input, it contains predicted values.
2800 ! ldyh       = a constant .ge. n, the first dimension of yh, input.
2801 ! vsav       = unused work array.
2802 ! savf       = a work array of length n.
2803 ! ewt        = an error weight vector of length n, input.
2804 ! acor       = a work array of length n, used for the accumulated
2805 !              corrections to the predicted y vector.
2806 ! wm,iwm     = real and integer work arrays associated with matrix
2807 !              operations in chord iteration (miter .ne. 0).
2808 ! f          = dummy name for user supplied routine for f.
2809 ! jac        = dummy name for user supplied jacobian routine.
2810 ! pdum       = unused dummy subroutine name.  included for uniformity
2811 !              over collection of integrators.
2812 ! nflag      = input/output flag, with values and meanings as follows:
2813 !              input
2814 !                  0 first call for this time step.
2815 !                 -1 convergence failure in previous call to dvnlsd.
2816 !                 -2 error test failure in dvstep.
2817 !              output
2818 !                  0 successful completion of nonlinear solver.
2819 !                 -1 convergence failure or singular matrix.
2820 !                 -2 unrecoverable error in matrix preprocessing
2821 !                    (cannot occur here).
2822 !                 -3 unrecoverable error in solution (cannot occur
2823 !                    here).
2824 ! rpar, ipar = dummy names for user's real and integer work arrays.
2826 ! ipup       = own variable flag with values and meanings as follows:
2827 !              0,            do not update the newton matrix.
2828 !              miter .ne. 0, update newton matrix, because it is the
2829 !                            initial step, order was changed, the error
2830 !                            test failed, or an update is indicated by
2831 !                            the scalar rc or step counter nst.
2833 ! for more details, see comments in driver subroutine.
2834 !-----------------------------------------------------------------------
2835 ! type declarations for labeled common block dvod_cmn01 --------------------
2837       double precision acnrm, ccmxj, conp, crate, drc, el,   &
2838            eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
2839            rc, rl1, tau, tq, tn, uround
2840       integer icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
2841               l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
2842               locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
2843               n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
2844               nslp, nyh
2846 ! type declarations for labeled common block dvod_cmn02 --------------------
2848       double precision hu
2849       integer ncfn, netf, nfe, nje, nlu, nni, nqu, nst
2851 ! type declarations for local variables --------------------------------
2853       double precision ccmax, crdown, cscale, dcon, del, delp, one,   &
2854            rdiv, two, zero
2855       integer i, ierpj, iersl, m, maxcor, msbp
2857 ! type declaration for function subroutines called ---------------------
2859 ! 27-oct-2005 rce - do not declare functions that are in the module
2860 !     double precision dvnorm
2861 !-----------------------------------------------------------------------
2862 ! the following fortran-77 declaration is to cause the values of the
2863 ! listed (local) variables to be saved between calls to this integrator.
2864 !-----------------------------------------------------------------------
2865       save ccmax, crdown, maxcor, msbp, rdiv, one, two, zero
2867       common /dvod_cmn01/ acnrm, ccmxj, conp, crate, drc, el(13),   &
2868                       eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
2869                       rc, rl1, tau(13), tq(5), tn, uround,   &
2870                       icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
2871                       l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
2872                       locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
2873                       n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
2874                       nslp, nyh
2875       common /dvod_cmn02/ hu, ncfn, netf, nfe, nje, nlu, nni, nqu, nst
2877       data ccmax /0.3d0/, crdown /0.3d0/, maxcor /3/, msbp /20/,   &
2878            rdiv  /2.0d0/
2879       data one /1.0d0/, two /2.0d0/, zero /0.0d0/
2880 !-----------------------------------------------------------------------
2881 ! on the first step, on a change of method order, or after a
2882 ! nonlinear convergence failure with nflag = -2, set ipup = miter
2883 ! to force a jacobian update when miter .ne. 0.
2884 !-----------------------------------------------------------------------
2885       if (jstart .eq. 0) nslp = 0
2886       if (nflag .eq. 0) icf = 0
2887       if (nflag .eq. -2) ipup = miter
2888       if ( (jstart .eq. 0) .or. (jstart .eq. -1) ) ipup = miter
2889 ! if this is functional iteration, set crate .eq. 1 and drop to 220
2890       if (miter .eq. 0) then
2891         crate = one
2892         go to 220
2893       endif
2894 !-----------------------------------------------------------------------
2895 ! rc is the ratio of new to old values of the coefficient h/el(2)=h/l1.
2896 ! when rc differs from 1 by more than ccmax, ipup is set to miter
2897 ! to force dvjac to be called, if a jacobian is involved.
2898 ! in any case, dvjac is called at least every msbp steps.
2899 !-----------------------------------------------------------------------
2900       drc = abs(rc-one)
2901       if (drc .gt. ccmax .or. nst .ge. nslp+msbp) ipup = miter
2902 !-----------------------------------------------------------------------
2903 ! up to maxcor corrector iterations are taken.  a convergence test is
2904 ! made on the r.m.s. norm of each correction, weighted by the error
2905 ! weight vector ewt.  the sum of the corrections is accumulated in the
2906 ! vector acor(i).  the yh array is not altered in the corrector loop.
2907 !-----------------------------------------------------------------------
2908  220  m = 0
2909       delp = zero
2910       call dcopy (n, yh(1,1), 1, y, 1 )
2911       call f (n, tn, y, savf, rpar, ipar)
2912       nfe = nfe + 1
2913       if (ipup .le. 0) go to 250
2914 !-----------------------------------------------------------------------
2915 ! if indicated, the matrix p = i - h*rl1*j is reevaluated and
2916 ! preprocessed before starting the corrector iteration.  ipup is set
2917 ! to 0 as an indicator that this has been done.
2918 !-----------------------------------------------------------------------
2919       call dvjac (y, yh, ldyh, ewt, acor, savf, wm, iwm, f, jac, ierpj,   &
2920                  rpar, ipar)
2921       ipup = 0
2922       rc = one
2923       drc = zero
2924       crate = one
2925       nslp = nst
2926 ! if matrix is singular, take error return to force cut in step size. --
2927       if (ierpj .ne. 0) go to 430
2928  250  do 260 i = 1,n
2929  260    acor(i) = zero
2930 ! this is a looping point for the corrector iteration. -----------------
2931  270  if (miter .ne. 0) go to 350
2932 !-----------------------------------------------------------------------
2933 ! in the case of functional iteration, update y directly from
2934 ! the result of the last function evaluation.
2935 !-----------------------------------------------------------------------
2936       do 280 i = 1,n
2937  280    savf(i) = rl1*(h*savf(i) - yh(i,2))
2938       do 290 i = 1,n
2939  290    y(i) = savf(i) - acor(i)
2940       del = dvnorm (n, y, ewt)
2941       do 300 i = 1,n
2942  300    y(i) = yh(i,1) + savf(i)
2943       call dcopy (n, savf, 1, acor, 1)
2944       go to 400
2945 !-----------------------------------------------------------------------
2946 ! in the case of the chord method, compute the corrector error,
2947 ! and solve the linear system with that as right-hand side and
2948 ! p as coefficient matrix.  the correction is scaled by the factor
2949 ! 2/(1+rc) to account for changes in h*rl1 since the last dvjac call.
2950 !-----------------------------------------------------------------------
2951  350  do 360 i = 1,n
2952  360    y(i) = (rl1*h)*savf(i) - (rl1*yh(i,2) + acor(i))
2953       call dvsol (wm, iwm, y, iersl)
2954       nni = nni + 1
2955       if (iersl .gt. 0) go to 410
2956       if (meth .eq. 2 .and. rc .ne. one) then
2957         cscale = two/(one + rc)
2958         call dscal (n, cscale, y, 1)
2959       endif
2960       del = dvnorm (n, y, ewt)
2961       call daxpy (n, one, y, 1, acor, 1)
2962       do 380 i = 1,n
2963  380    y(i) = yh(i,1) + acor(i)
2964 !-----------------------------------------------------------------------
2965 ! test for convergence.  if m .gt. 0, an estimate of the convergence
2966 ! rate constant is stored in crate, and this is used in the test.
2967 !-----------------------------------------------------------------------
2968  400  if (m .ne. 0) crate = max(crdown*crate,del/delp)
2969       dcon = del*min(one,crate)/tq(4)
2970       if (dcon .le. one) go to 450
2971       m = m + 1
2972       if (m .eq. maxcor) go to 410
2973       if (m .ge. 2 .and. del .gt. rdiv*delp) go to 410
2974       delp = del
2975       call f (n, tn, y, savf, rpar, ipar)
2976       nfe = nfe + 1
2977       go to 270
2979  410  if (miter .eq. 0 .or. jcur .eq. 1) go to 430
2980       icf = 1
2981       ipup = miter
2982       go to 220
2984  430  continue
2985       nflag = -1
2986       icf = 2
2987       ipup = miter
2988       return
2990 ! return for successful step. ------------------------------------------
2991  450  nflag = 0
2992       jcur = 0
2993       icf = 0
2994       if (m .eq. 0) acnrm = del
2995       if (m .gt. 0) acnrm = dvnorm (n, acor, ewt)
2996       return
2997 !----------------------- end of subroutine dvnlsd ----------------------
2998       end subroutine dvnlsd
2999 !deck dvjac
3000       subroutine dvjac (y, yh, ldyh, ewt, ftem, savf, wm, iwm, f, jac,   &
3001                        ierpj, rpar, ipar)
3002       external f, jac
3003       double precision y, yh, ewt, ftem, savf, wm, rpar
3004       integer ldyh, iwm, ierpj, ipar
3005       dimension y(*), yh(ldyh,*), ewt(*), ftem(*), savf(*),   &
3006          wm(*), iwm(*), rpar(*), ipar(*)
3007 !-----------------------------------------------------------------------
3008 ! call sequence input -- y, yh, ldyh, ewt, ftem, savf, wm, iwm,
3009 !                        f, jac, rpar, ipar
3010 ! call sequence output -- wm, iwm, ierpj
3011 ! common block variables accessed:
3012 !     /dvod_cmn01/  ccmxj, drc, h, rl1, tn, uround, icf, jcur, locjs,
3013 !               miter, msbj, n, nslj
3014 !     /dvod_cmn02/  nfe, nst, nje, nlu
3016 ! subroutines called by dvjac: f, jac, dacopy, dcopy, dgbfa, dgefa,
3017 !                              dscal
3018 ! function routines called by dvjac: dvnorm
3019 !-----------------------------------------------------------------------
3020 ! dvjac is called by dvnlsd to compute and process the matrix
3021 ! p = i - h*rl1*j , where j is an approximation to the jacobian.
3022 ! here j is computed by the user-supplied routine jac if
3023 ! miter = 1 or 4, or by finite differencing if miter = 2, 3, or 5.
3024 ! if miter = 3, a diagonal approximation to j is used.
3025 ! if jsv = -1, j is computed from scratch in all cases.
3026 ! if jsv = 1 and miter = 1, 2, 4, or 5, and if the saved value of j is
3027 ! considered acceptable, then p is constructed from the saved j.
3028 ! j is stored in wm and replaced by p.  if miter .ne. 3, p is then
3029 ! subjected to lu decomposition in preparation for later solution
3030 ! of linear systems with p as coefficient matrix. this is done
3031 ! by dgefa if miter = 1 or 2, and by dgbfa if miter = 4 or 5.
3033 ! communication with dvjac is done with the following variables.  (for
3034 ! more details, please see the comments in the driver subroutine.)
3035 ! y          = vector containing predicted values on entry.
3036 ! yh         = the nordsieck array, an ldyh by lmax array, input.
3037 ! ldyh       = a constant .ge. n, the first dimension of yh, input.
3038 ! ewt        = an error weight vector of length n.
3039 ! savf       = array containing f evaluated at predicted y, input.
3040 ! wm         = real work space for matrices.  in the output, it contains
3041 !              the inverse diagonal matrix if miter = 3 and the lu
3042 !              decomposition of p if miter is 1, 2 , 4, or 5.
3043 !              storage of matrix elements starts at wm(3).
3044 !              storage of the saved jacobian starts at wm(locjs).
3045 !              wm also contains the following matrix-related data:
3046 !              wm(1) = sqrt(uround), used in numerical jacobian step.
3047 !              wm(2) = h*rl1, saved for later use if miter = 3.
3048 ! iwm        = integer work space containing pivot information,
3049 !              starting at iwm(31), if miter is 1, 2, 4, or 5.
3050 !              iwm also contains band parameters ml = iwm(1) and
3051 !              mu = iwm(2) if miter is 4 or 5.
3052 ! f          = dummy name for the user supplied subroutine for f.
3053 ! jac        = dummy name for the user supplied jacobian subroutine.
3054 ! rpar, ipar = dummy names for user's real and integer work arrays.
3055 ! rl1        = 1/el(2) (input).
3056 ! ierpj      = output error flag,  = 0 if no trouble, 1 if the p
3057 !              matrix is found to be singular.
3058 ! jcur       = output flag to indicate whether the jacobian matrix
3059 !              (or approximation) is now current.
3060 !              jcur = 0 means j is not current.
3061 !              jcur = 1 means j is current.
3062 !-----------------------------------------------------------------------
3064 ! type declarations for labeled common block dvod_cmn01 --------------------
3066       double precision acnrm, ccmxj, conp, crate, drc, el,   &
3067            eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
3068            rc, rl1, tau, tq, tn, uround
3069       integer icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
3070               l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
3071               locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
3072               n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
3073               nslp, nyh
3075 ! type declarations for labeled common block dvod_cmn02 --------------------
3077       double precision hu
3078       integer ncfn, netf, nfe, nje, nlu, nni, nqu, nst
3080 ! type declarations for local variables --------------------------------
3082       double precision con, di, fac, hrl1, one, pt1, r, r0, srur, thou,   &
3083            yi, yj, yjj, zero
3084       integer i, i1, i2, ier, ii, j, j1, jj, jok, lenp, mba, mband,   &
3085               meb1, meband, ml, ml3, mu, np1
3087 ! type declaration for function subroutines called ---------------------
3089 ! 27-oct-2005 rce - do not declare functions that are in the module
3090 !     double precision dvnorm
3091 !-----------------------------------------------------------------------
3092 ! the following fortran-77 declaration is to cause the values of the
3093 ! listed (local) variables to be saved between calls to this subroutine.
3094 !-----------------------------------------------------------------------
3095       save one, pt1, thou, zero
3096 !-----------------------------------------------------------------------
3097       common /dvod_cmn01/ acnrm, ccmxj, conp, crate, drc, el(13),   &
3098                       eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
3099                       rc, rl1, tau(13), tq(5), tn, uround,   &
3100                       icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
3101                       l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
3102                       locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
3103                       n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
3104                       nslp, nyh
3105       common /dvod_cmn02/ hu, ncfn, netf, nfe, nje, nlu, nni, nqu, nst
3107       data one /1.0d0/, thou /1000.0d0/, zero /0.0d0/, pt1 /0.1d0/
3109       ierpj = 0
3110       hrl1 = h*rl1
3111 ! see whether j should be evaluated (jok = -1) or not (jok = 1). -------
3112       jok = jsv
3113       if (jsv .eq. 1) then
3114         if (nst .eq. 0 .or. nst .gt. nslj+msbj) jok = -1
3115         if (icf .eq. 1 .and. drc .lt. ccmxj) jok = -1
3116         if (icf .eq. 2) jok = -1
3117       endif
3118 ! end of setting jok. --------------------------------------------------
3120       if (jok .eq. -1 .and. miter .eq. 1) then
3121 ! if jok = -1 and miter = 1, call jac to evaluate jacobian. ------------
3122       nje = nje + 1
3123       nslj = nst
3124       jcur = 1
3125       lenp = n*n
3126       do 110 i = 1,lenp
3127  110    wm(i+2) = zero
3128       call jac (n, tn, y, 0, 0, wm(3), n, rpar, ipar)
3129       if (jsv .eq. 1) call dcopy (lenp, wm(3), 1, wm(locjs), 1)
3130       endif
3132       if (jok .eq. -1 .and. miter .eq. 2) then
3133 ! if miter = 2, make n calls to f to approximate the jacobian. ---------
3134       nje = nje + 1
3135       nslj = nst
3136       jcur = 1
3137       fac = dvnorm (n, savf, ewt)
3138       r0 = thou*abs(h)*uround*real(n)*fac
3139       if (r0 .eq. zero) r0 = one
3140       srur = wm(1)
3141       j1 = 2
3142       do 230 j = 1,n
3143         yj = y(j)
3144         r = max(srur*abs(yj),r0/ewt(j))
3145         y(j) = y(j) + r
3146         fac = one/r
3147         call f (n, tn, y, ftem, rpar, ipar)
3148         do 220 i = 1,n
3149  220      wm(i+j1) = (ftem(i) - savf(i))*fac
3150         y(j) = yj
3151         j1 = j1 + n
3152  230    continue
3153       nfe = nfe + n
3154       lenp = n*n
3155       if (jsv .eq. 1) call dcopy (lenp, wm(3), 1, wm(locjs), 1)
3156       endif
3158       if (jok .eq. 1 .and. (miter .eq. 1 .or. miter .eq. 2)) then
3159       jcur = 0
3160       lenp = n*n
3161       call dcopy (lenp, wm(locjs), 1, wm(3), 1)
3162       endif
3164       if (miter .eq. 1 .or. miter .eq. 2) then
3165 ! multiply jacobian by scalar, add identity, and do lu decomposition. --
3166       con = -hrl1
3167       call dscal (lenp, con, wm(3), 1)
3168       j = 3
3169       np1 = n + 1
3170       do 250 i = 1,n
3171         wm(j) = wm(j) + one
3172  250    j = j + np1
3173       nlu = nlu + 1
3174       call dgefa (wm(3), n, n, iwm(31), ier)
3175       if (ier .ne. 0) ierpj = 1
3176       return
3177       endif
3178 ! end of code block for miter = 1 or 2. --------------------------------
3180       if (miter .eq. 3) then
3181 ! if miter = 3, construct a diagonal approximation to j and p. ---------
3182       nje = nje + 1
3183       jcur = 1
3184       wm(2) = hrl1
3185       r = rl1*pt1
3186       do 310 i = 1,n
3187  310    y(i) = y(i) + r*(h*savf(i) - yh(i,2))
3188       call f (n, tn, y, wm(3), rpar, ipar)
3189       nfe = nfe + 1
3190       do 320 i = 1,n
3191         r0 = h*savf(i) - yh(i,2)
3192         di = pt1*r0 - h*(wm(i+2) - savf(i))
3193         wm(i+2) = one
3194         if (abs(r0) .lt. uround/ewt(i)) go to 320
3195         if (abs(di) .eq. zero) go to 330
3196         wm(i+2) = pt1*r0/di
3197  320    continue
3198       return
3199  330  ierpj = 1
3200       return
3201       endif
3202 ! end of code block for miter = 3. -------------------------------------
3204 ! set constants for miter = 4 or 5. ------------------------------------
3205       ml = iwm(1)
3206       mu = iwm(2)
3207       ml3 = ml + 3
3208       mband = ml + mu + 1
3209       meband = mband + ml
3210       lenp = meband*n
3212       if (jok .eq. -1 .and. miter .eq. 4) then
3213 ! if jok = -1 and miter = 4, call jac to evaluate jacobian. ------------
3214       nje = nje + 1
3215       nslj = nst
3216       jcur = 1
3217       do 410 i = 1,lenp
3218  410    wm(i+2) = zero
3219       call jac (n, tn, y, ml, mu, wm(ml3), meband, rpar, ipar)
3220       if (jsv .eq. 1)   &
3221          call dacopy (mband, n, wm(ml3), meband, wm(locjs), mband)
3222       endif
3224       if (jok .eq. -1 .and. miter .eq. 5) then
3225 ! if miter = 5, make ml+mu+1 calls to f to approximate the jacobian. ---
3226       nje = nje + 1
3227       nslj = nst
3228       jcur = 1
3229       mba = min(mband,n)
3230       meb1 = meband - 1
3231       srur = wm(1)
3232       fac = dvnorm (n, savf, ewt)
3233       r0 = thou*abs(h)*uround*real(n)*fac
3234       if (r0 .eq. zero) r0 = one
3235       do 560 j = 1,mba
3236         do 530 i = j,n,mband
3237           yi = y(i)
3238           r = max(srur*abs(yi),r0/ewt(i))
3239  530      y(i) = y(i) + r
3240         call f (n, tn, y, ftem, rpar, ipar)
3241         do 550 jj = j,n,mband
3242           y(jj) = yh(jj,1)
3243           yjj = y(jj)
3244           r = max(srur*abs(yjj),r0/ewt(jj))
3245           fac = one/r
3246           i1 = max(jj-mu,1)
3247           i2 = min(jj+ml,n)
3248           ii = jj*meb1 - ml + 2
3249           do 540 i = i1,i2
3250  540        wm(ii+i) = (ftem(i) - savf(i))*fac
3251  550      continue
3252  560    continue
3253       nfe = nfe + mba
3254       if (jsv .eq. 1)   &
3255          call dacopy (mband, n, wm(ml3), meband, wm(locjs), mband)
3256       endif
3258       if (jok .eq. 1) then
3259       jcur = 0
3260       call dacopy (mband, n, wm(locjs), mband, wm(ml3), meband)
3261       endif
3263 ! multiply jacobian by scalar, add identity, and do lu decomposition.
3264       con = -hrl1
3265       call dscal (lenp, con, wm(3), 1 )
3266       ii = mband + 2
3267       do 580 i = 1,n
3268         wm(ii) = wm(ii) + one
3269  580    ii = ii + meband
3270       nlu = nlu + 1
3271       call dgbfa (wm(3), meband, n, ml, mu, iwm(31), ier)
3272       if (ier .ne. 0) ierpj = 1
3273       return
3274 ! end of code block for miter = 4 or 5. --------------------------------
3276 !----------------------- end of subroutine dvjac -----------------------
3277       end subroutine dvjac
3278 !deck dacopy
3279       subroutine dacopy (nrow, ncol, a, nrowa, b, nrowb)
3280       double precision a, b
3281       integer nrow, ncol, nrowa, nrowb
3282       dimension a(nrowa,ncol), b(nrowb,ncol)
3283 !-----------------------------------------------------------------------
3284 ! call sequence input -- nrow, ncol, a, nrowa, nrowb
3285 ! call sequence output -- b
3286 ! common block variables accessed -- none
3288 ! subroutines called by dacopy: dcopy
3289 ! function routines called by dacopy: none
3290 !-----------------------------------------------------------------------
3291 ! this routine copies one rectangular array, a, to another, b,
3292 ! where a and b may have different row dimensions, nrowa and nrowb.
3293 ! the data copied consists of nrow rows and ncol columns.
3294 !-----------------------------------------------------------------------
3295       integer ic
3297       do 20 ic = 1,ncol
3298         call dcopy (nrow, a(1,ic), 1, b(1,ic), 1)
3299  20     continue
3301       return
3302 !----------------------- end of subroutine dacopy ----------------------
3303       end subroutine dacopy
3304 !deck dvsol
3305       subroutine dvsol (wm, iwm, x, iersl)
3306       double precision wm, x
3307       integer iwm, iersl
3308       dimension wm(*), iwm(*), x(*)
3309 !-----------------------------------------------------------------------
3310 ! call sequence input -- wm, iwm, x
3311 ! call sequence output -- x, iersl
3312 ! common block variables accessed:
3313 !     /dvod_cmn01/ -- h, rl1, miter, n
3315 ! subroutines called by dvsol: dgesl, dgbsl
3316 ! function routines called by dvsol: none
3317 !-----------------------------------------------------------------------
3318 ! this routine manages the solution of the linear system arising from
3319 ! a chord iteration.  it is called if miter .ne. 0.
3320 ! if miter is 1 or 2, it calls dgesl to accomplish this.
3321 ! if miter = 3 it updates the coefficient h*rl1 in the diagonal
3322 ! matrix, and then computes the solution.
3323 ! if miter is 4 or 5, it calls dgbsl.
3324 ! communication with dvsol uses the following variables:
3325 ! wm    = real work space containing the inverse diagonal matrix if
3326 !         miter = 3 and the lu decomposition of the matrix otherwise.
3327 !         storage of matrix elements starts at wm(3).
3328 !         wm also contains the following matrix-related data:
3329 !         wm(1) = sqrt(uround) (not used here),
3330 !         wm(2) = hrl1, the previous value of h*rl1, used if miter = 3.
3331 ! iwm   = integer work space containing pivot information, starting at
3332 !         iwm(31), if miter is 1, 2, 4, or 5.  iwm also contains band
3333 !         parameters ml = iwm(1) and mu = iwm(2) if miter is 4 or 5.
3334 ! x     = the right-hand side vector on input, and the solution vector
3335 !         on output, of length n.
3336 ! iersl = output flag.  iersl = 0 if no trouble occurred.
3337 !         iersl = 1 if a singular matrix arose with miter = 3.
3338 !-----------------------------------------------------------------------
3340 ! type declarations for labeled common block dvod_cmn01 --------------------
3342       double precision acnrm, ccmxj, conp, crate, drc, el,   &
3343            eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
3344            rc, rl1, tau, tq, tn, uround
3345       integer icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
3346               l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
3347               locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
3348               n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
3349               nslp, nyh
3351 ! type declarations for local variables --------------------------------
3353       integer i, meband, ml, mu
3354       double precision di, hrl1, one, phrl1, r, zero
3355 !-----------------------------------------------------------------------
3356 ! the following fortran-77 declaration is to cause the values of the
3357 ! listed (local) variables to be saved between calls to this integrator.
3358 !-----------------------------------------------------------------------
3359       save one, zero
3361       common /dvod_cmn01/ acnrm, ccmxj, conp, crate, drc, el(13),   &
3362                       eta, etamax, h, hmin, hmxi, hnew, hscal, prl1,   &
3363                       rc, rl1, tau(13), tq(5), tn, uround,   &
3364                       icf, init, ipup, jcur, jstart, jsv, kflag, kuth,   &
3365                       l, lmax, lyh, lewt, lacor, lsavf, lwm, liwm,   &
3366                       locjs, maxord, meth, miter, msbj, mxhnil, mxstep,   &
3367                       n, newh, newq, nhnil, nq, nqnyh, nqwait, nslj,   &
3368                       nslp, nyh
3370       data one /1.0d0/, zero /0.0d0/
3372       iersl = 0
3373       go to (100, 100, 300, 400, 400), miter
3374  100  call dgesl (wm(3), n, n, iwm(31), x, 0)
3375       return
3377  300  phrl1 = wm(2)
3378       hrl1 = h*rl1
3379       wm(2) = hrl1
3380       if (hrl1 .eq. phrl1) go to 330
3381       r = hrl1/phrl1
3382       do 320 i = 1,n
3383         di = one - r*(one - one/wm(i+2))
3384         if (abs(di) .eq. zero) go to 390
3385  320    wm(i+2) = one/di
3387  330  do 340 i = 1,n
3388  340    x(i) = wm(i+2)*x(i)
3389       return
3390  390  iersl = 1
3391       return
3393  400  ml = iwm(1)
3394       mu = iwm(2)
3395       meband = 2*ml + mu + 1
3396       call dgbsl (wm(3), meband, n, ml, mu, iwm(31), x, 0)
3397       return
3398 !----------------------- end of subroutine dvsol -----------------------
3399       end subroutine dvsol
3400 !deck dvsrco
3401       subroutine dvsrco (rsav, isav, job)
3402       double precision rsav
3403       integer isav, job
3404       dimension rsav(*), isav(*)
3405 !-----------------------------------------------------------------------
3406 ! call sequence input -- rsav, isav, job
3407 ! call sequence output -- rsav, isav
3408 ! common block variables accessed -- all of /dvod_cmn01/ and /dvod_cmn02/
3410 ! subroutines/functions called by dvsrco: none
3411 !-----------------------------------------------------------------------
3412 ! this routine saves or restores (depending on job) the contents of the
3413 ! common blocks dvod_cmn01 and dvod_cmn02, which are used internally by dvode.
3415 ! rsav = real array of length 49 or more.
3416 ! isav = integer array of length 41 or more.
3417 ! job  = flag indicating to save or restore the common blocks:
3418 !        job  = 1 if common is to be saved (written to rsav/isav).
3419 !        job  = 2 if common is to be restored (read from rsav/isav).
3420 !        a call with job = 2 presumes a prior call with job = 1.
3421 !-----------------------------------------------------------------------
3422       double precision rvod1, rvod2
3423       integer ivod1, ivod2
3424       integer i, leniv1, leniv2, lenrv1, lenrv2
3425 !-----------------------------------------------------------------------
3426 ! the following fortran-77 declaration is to cause the values of the
3427 ! listed (local) variables to be saved between calls to this integrator.
3428 !-----------------------------------------------------------------------
3429       save lenrv1, leniv1, lenrv2, leniv2
3431       common /dvod_cmn01/ rvod1(48), ivod1(33)
3432       common /dvod_cmn02/ rvod2(1), ivod2(8)
3433       data lenrv1/48/, leniv1/33/, lenrv2/1/, leniv2/8/
3435       if (job .eq. 2) go to 100
3436       do 10 i = 1,lenrv1
3437  10     rsav(i) = rvod1(i)
3438       do 15 i = 1,lenrv2
3439  15     rsav(lenrv1+i) = rvod2(i)
3441       do 20 i = 1,leniv1
3442  20     isav(i) = ivod1(i)
3443       do 25 i = 1,leniv2
3444  25     isav(leniv1+i) = ivod2(i)
3446       return
3448  100  continue
3449       do 110 i = 1,lenrv1
3450  110     rvod1(i) = rsav(i)
3451       do 115 i = 1,lenrv2
3452  115     rvod2(i) = rsav(lenrv1+i)
3454       do 120 i = 1,leniv1
3455  120     ivod1(i) = isav(i)
3456       do 125 i = 1,leniv2
3457  125     ivod2(i) = isav(leniv1+i)
3459       return
3460 !----------------------- end of subroutine dvsrco ----------------------
3461       end subroutine dvsrco
3462 !deck dewset
3463       subroutine dewset (n, itol, rtol, atol, ycur, ewt)
3464 !***begin prologue  dewset
3465 !***subsidiary
3466 !***purpose  set error weight vector.
3467 !***type      double precision (sewset-s, dewset-d)
3468 !***author  hindmarsh, alan c., (llnl)
3469 !***description
3471 !  this subroutine sets the error weight vector ewt according to
3472 !      ewt(i) = rtol(i)*abs(ycur(i)) + atol(i),  i = 1,...,n,
3473 !  with the subscript on rtol and/or atol possibly replaced by 1 above,
3474 !  depending on the value of itol.
3476 !***see also  dlsode
3477 !***routines called  (none)
3478 !***revision history  (yymmdd)
3479 !   791129  date written
3480 !   890501  modified prologue to slatec/ldoc format.  (fnf)
3481 !   890503  minor cosmetic changes.  (fnf)
3482 !   930809  renamed to allow single/double precision versions. (ach)
3483 !***end prologue  dewset
3484 !**end
3485       integer n, itol
3486       integer i
3487       double precision rtol, atol, ycur, ewt
3488       dimension rtol(*), atol(*), ycur(n), ewt(n)
3490 !***first executable statement  dewset
3491       go to (10, 20, 30, 40), itol
3492  10   continue
3493       do 15 i = 1,n
3494  15     ewt(i) = rtol(1)*abs(ycur(i)) + atol(1)
3495       return
3496  20   continue
3497       do 25 i = 1,n
3498  25     ewt(i) = rtol(1)*abs(ycur(i)) + atol(i)
3499       return
3500  30   continue
3501       do 35 i = 1,n
3502  35     ewt(i) = rtol(i)*abs(ycur(i)) + atol(1)
3503       return
3504  40   continue
3505       do 45 i = 1,n
3506  45     ewt(i) = rtol(i)*abs(ycur(i)) + atol(i)
3507       return
3508 !----------------------- end of subroutine dewset ----------------------
3509       end subroutine dewset
3510 !deck dvnorm
3511       double precision function dvnorm (n, v, w)
3512 !***begin prologue  dvnorm
3513 !***subsidiary
3514 !***purpose  weighted root-mean-square vector norm.
3515 !***type      double precision (svnorm-s, dvnorm-d)
3516 !***author  hindmarsh, alan c., (llnl)
3517 !***description
3519 !  this function routine computes the weighted root-mean-square norm
3520 !  of the vector of length n contained in the array v, with weights
3521 !  contained in the array w of length n:
3522 !    dvnorm = sqrt( (1/n) * sum( v(i)*w(i) )**2 )
3524 !***see also  dlsode
3525 !***routines called  (none)
3526 !***revision history  (yymmdd)
3527 !   791129  date written
3528 !   890501  modified prologue to slatec/ldoc format.  (fnf)
3529 !   890503  minor cosmetic changes.  (fnf)
3530 !   930809  renamed to allow single/double precision versions. (ach)
3531 !***end prologue  dvnorm
3532 !**end
3533       integer n,   i
3534       double precision v, w,   sum
3535       dimension v(n), w(n)
3537 !***first executable statement  dvnorm
3538       sum = 0.0d0
3539       do 10 i = 1,n
3540  10     sum = sum + (v(i)*w(i))**2
3541       dvnorm = sqrt(sum/n)
3542       return
3543 !----------------------- end of function dvnorm ------------------------
3544       end function dvnorm
3545 !deck xerrwd
3546       subroutine xerrwd (msg, nmes, nerr, level, ni, i1, i2, nr, r1, r2)
3547 !***begin prologue  xerrwd
3548 !***subsidiary
3549 !***purpose  write error message with values.
3550 !***category  r3c
3551 !***type      double precision (xerrwv-s, xerrwd-d)
3552 !***author  hindmarsh, alan c., (llnl)
3553 !***description
3555 !  subroutines xerrwd, xsetf, xsetun, and the function routine ixsav,
3556 !  as given here, constitute a simplified version of the slatec error
3557 !  handling package.
3559 !  all arguments are input arguments.
3561 !  msg    = the message (character array).
3562 !  nmes   = the length of msg (number of characters).
3563 !  nerr   = the error number (not used).
3564 !  level  = the error level..
3565 !           0 or 1 means recoverable (control returns to caller).
3566 !           2 means fatal (run is aborted--see note below).
3567 !  ni     = number of integers (0, 1, or 2) to be printed with message.
3568 !  i1,i2  = integers to be printed, depending on ni.
3569 !  nr     = number of reals (0, 1, or 2) to be printed with message.
3570 !  r1,r2  = reals to be printed, depending on nr.
3572 !  note..  this routine is machine-dependent and specialized for use
3573 !  in limited context, in the following ways..
3574 !  1. the argument msg is assumed to be of type character, and
3575 !     the message is printed with a format of (1x,a).
3576 !  2. the message is assumed to take only one line.
3577 !     multi-line messages are generated by repeated calls.
3578 !  3. if level = 2, control passes to the statement   stop
3579 !     to abort the run.  this statement may be machine-dependent.
3580 !  4. r1 and r2 are assumed to be in double precision and are printed
3581 !     in d21.13 format.
3583 !***routines called  ixsav
3584 !***revision history  (yymmdd)
3585 !   920831  date written
3586 !   921118  replaced mflgsv/lunsav by ixsav. (ach)
3587 !   930329  modified prologue to slatec format. (fnf)
3588 !   930407  changed msg from character*1 array to variable. (fnf)
3589 !   930922  minor cosmetic change. (fnf)
3590 !***end prologue  xerrwd
3592 !*internal notes:
3594 ! for a different default logical unit number, ixsav (or a subsidiary
3595 ! routine that it calls) will need to be modified.
3596 ! for a different run-abort command, change the statement following
3597 ! statement 100 at the end.
3598 !-----------------------------------------------------------------------
3599 ! subroutines called by xerrwd.. none
3600 ! function routine called by xerrwd.. ixsav
3601 !-----------------------------------------------------------------------
3602 !**end
3604 !  declare arguments.
3606       double precision r1, r2
3607       integer nmes, nerr, level, ni, i1, i2, nr
3608       character*(*) msg
3610 !  declare local variables.
3612 ! 27-oct-2005 rce - do not declare functions that are in the module
3613 !     integer lunit, ixsav, mesflg
3614       integer lunit,        mesflg
3616 !  get logical unit number and message print flag.
3618 !***first executable statement  xerrwd
3619       lunit = ixsav (1, 0, .false.)
3620       mesflg = ixsav (2, 0, .false.)
3621       if (mesflg .eq. 0) go to 100
3623 !  write the message.
3625       write (lunit,10)  msg
3626  10   format(1x,a)
3627       if (ni .eq. 1) write (lunit, 20) i1
3628  20   format(6x,'in above message,  i1 =',i10)
3629       if (ni .eq. 2) write (lunit, 30) i1,i2
3630  30   format(6x,'in above message,  i1 =',i10,3x,'i2 =',i10)
3631       if (nr .eq. 1) write (lunit, 40) r1
3632  40   format(6x,'in above message,  r1 =',d21.13)
3633       if (nr .eq. 2) write (lunit, 50) r1,r2
3634  50   format(6x,'in above,  r1 =',d21.13,3x,'r2 =',d21.13)
3636 !  abort the run if level = 2.
3638  100  if (level .ne. 2) return
3639       stop
3640 !----------------------- end of subroutine xerrwd ----------------------
3641       end subroutine xerrwd
3642 !deck xsetf
3643       subroutine xsetf (mflag)
3644 !***begin prologue  xsetf
3645 !***purpose  reset the error print control flag.
3646 !***category  r3a
3647 !***type      all (xsetf-a)
3648 !***keywords  error control
3649 !***author  hindmarsh, alan c., (llnl)
3650 !***description
3652 !   xsetf sets the error print control flag to mflag:
3653 !      mflag=1 means print all messages (the default).
3654 !      mflag=0 means no printing.
3656 !***see also  xerrwd, xerrwv
3657 !***references  (none)
3658 !***routines called  ixsav
3659 !***revision history  (yymmdd)
3660 !   921118  date written
3661 !   930329  added slatec format prologue. (fnf)
3662 !   930407  corrected see also section. (fnf)
3663 !   930922  made user-callable, and other cosmetic changes. (fnf)
3664 !***end prologue  xsetf
3666 ! subroutines called by xsetf.. none
3667 ! function routine called by xsetf.. ixsav
3668 !-----------------------------------------------------------------------
3669 !**end
3670 ! 27-oct-2005 rce - do not declare functions that are in the module
3671 !     integer mflag, junk, ixsav
3672       integer mflag, junk
3674 !***first executable statement  xsetf
3675       if (mflag .eq. 0 .or. mflag .eq. 1) junk = ixsav (2,mflag,.true.)
3676       return
3677 !----------------------- end of subroutine xsetf -----------------------
3678       end subroutine xsetf
3679 !deck xsetun
3680       subroutine xsetun (lun)
3681 !***begin prologue  xsetun
3682 !***purpose  reset the logical unit number for error messages.
3683 !***category  r3b
3684 !***type      all (xsetun-a)
3685 !***keywords  error control
3686 !***description
3688 !   xsetun sets the logical unit number for error messages to lun.
3690 !***author  hindmarsh, alan c., (llnl)
3691 !***see also  xerrwd, xerrwv
3692 !***references  (none)
3693 !***routines called  ixsav
3694 !***revision history  (yymmdd)
3695 !   921118  date written
3696 !   930329  added slatec format prologue. (fnf)
3697 !   930407  corrected see also section. (fnf)
3698 !   930922  made user-callable, and other cosmetic changes. (fnf)
3699 !***end prologue  xsetun
3701 ! subroutines called by xsetun.. none
3702 ! function routine called by xsetun.. ixsav
3703 !-----------------------------------------------------------------------
3704 !**end
3705 ! 27-oct-2005 rce - do not declare functions that are in the module
3706 !     integer lun, junk, ixsav
3707       integer lun, junk
3709 !***first executable statement  xsetun
3710       if (lun .gt. 0) junk = ixsav (1,lun,.true.)
3711       return
3712 !----------------------- end of subroutine xsetun ----------------------
3713       end subroutine xsetun
3714 !deck ixsav
3715       integer function ixsav (ipar, ivalue, iset)
3716 !***begin prologue  ixsav
3717 !***subsidiary
3718 !***purpose  save and recall error message control parameters.
3719 !***category  r3c
3720 !***type      all (ixsav-a)
3721 !***author  hindmarsh, alan c., (llnl)
3722 !***description
3724 !  ixsav saves and recalls one of two error message parameters:
3725 !    lunit, the logical unit number to which messages are printed, and
3726 !    mesflg, the message print flag.
3727 !  this is a modification of the slatec library routine j4save.
3729 !  saved local variables..
3730 !   lunit  = logical unit number for messages.  the default is obtained
3731 !            by a call to iumach (may be machine-dependent).
3732 !   mesflg = print control flag..
3733 !            1 means print all messages (the default).
3734 !            0 means no printing.
3736 !  on input..
3737 !    ipar   = parameter indicator (1 for lunit, 2 for mesflg).
3738 !    ivalue = the value to be set for the parameter, if iset = .true.
3739 !    iset   = logical flag to indicate whether to read or write.
3740 !             if iset = .true., the parameter will be given
3741 !             the value ivalue.  if iset = .false., the parameter
3742 !             will be unchanged, and ivalue is a dummy argument.
3744 !  on return..
3745 !    ixsav = the (old) value of the parameter.
3747 !***see also  xerrwd, xerrwv
3748 !***routines called  iumach
3749 !***revision history  (yymmdd)
3750 !   921118  date written
3751 !   930329  modified prologue to slatec format. (fnf)
3752 !   930915  added iumach call to get default output unit.  (ach)
3753 !   930922  minor cosmetic changes. (fnf)
3754 !   010425  type declaration for iumach added. (ach)
3755 !***end prologue  ixsav
3757 ! subroutines called by ixsav.. none
3758 ! function routine called by ixsav.. iumach
3759 !-----------------------------------------------------------------------
3760 !**end
3761       logical iset
3762       integer ipar, ivalue
3763 !-----------------------------------------------------------------------
3764 ! 27-oct-2005 rce - do not declare functions that are in the module
3765 !     integer iumach, lunit, mesflg
3766       integer         lunit, mesflg
3767 !-----------------------------------------------------------------------
3768 ! the following fortran-77 declaration is to cause the values of the
3769 ! listed (local) variables to be saved between calls to this routine.
3770 !-----------------------------------------------------------------------
3771       save lunit, mesflg
3772       data lunit/-1/, mesflg/1/
3774 !***first executable statement  ixsav
3775       if (ipar .eq. 1) then
3776         if (lunit .eq. -1) lunit = iumach()
3777         ixsav = lunit
3778         if (iset) lunit = ivalue
3779         endif
3781       if (ipar .eq. 2) then
3782         ixsav = mesflg
3783         if (iset) mesflg = ivalue
3784         endif
3786       return
3787 !----------------------- end of function ixsav -------------------------
3788       end function ixsav
3789 !deck iumach
3790       integer function iumach()
3791 !***begin prologue  iumach
3792 !***purpose  provide standard output unit number.
3793 !***category  r1
3794 !***type      integer (iumach-i)
3795 !***keywords  machine constants
3796 !***author  hindmarsh, alan c., (llnl)
3797 !***description
3798 ! *usage:
3799 !        integer  lout, iumach
3800 !        lout = iumach()
3802 ! *function return values:
3803 !     lout : the standard logical unit for fortran output.
3805 !***references  (none)
3806 !***routines called  (none)
3807 !***revision history  (yymmdd)
3808 !   930915  date written
3809 !   930922  made user-callable, and other cosmetic changes. (fnf)
3810 !***end prologue  iumach
3812 !*internal notes:
3813 !  the built-in value of 6 is standard on a wide range of fortran
3814 !  systems.  this may be machine-dependent.
3815 !**end
3816 !***first executable statement  iumach
3817       iumach = 6
3819       return
3820 !----------------------- end of function iumach ------------------------
3821       end function iumach
3822 !deck dumach
3823       double precision function dumach ()
3824 !***begin prologue  dumach
3825 !***purpose  compute the unit roundoff of the machine.
3826 !***category  r1
3827 !***type      double precision (rumach-s, dumach-d)
3828 !***keywords  machine constants
3829 !***author  hindmarsh, alan c., (llnl)
3830 !***description
3831 ! *usage:
3832 !        double precision  a, dumach
3833 !        a = dumach()
3835 ! *function return values:
3836 !     a : the unit roundoff of the machine.
3838 ! *description:
3839 !     the unit roundoff is defined as the smallest positive machine
3840 !     number u such that  1.0 + u .ne. 1.0.  this is computed by dumach
3841 !     in a machine-independent manner.
3843 !***references  (none)
3844 !***routines called  dumsum
3845 !***revision history  (yyyymmdd)
3846 !   19930216  date written
3847 !   19930818  added slatec-format prologue.  (fnf)
3848 !   20030707  added dumsum to force normal storage of comp.  (ach)
3849 !***end prologue  dumach
3851       double precision u, comp
3852 !***first executable statement  dumach
3853       u = 1.0d0
3854  10   u = u*0.5d0
3855       call dumsum(1.0d0, u, comp)
3856       if (comp .ne. 1.0d0) go to 10
3857       dumach = u*2.0d0
3858       return
3859 !----------------------- end of function dumach ------------------------
3860       end function dumach
3861       subroutine dumsum(a,b,c)
3862 !     routine to force normal storing of a + b, for dumach.
3863       double precision a, b, c
3864       c = a + b
3865       return
3866       end subroutine dumsum
3868 !-----------------------------------------------------------------------
3869 !   vode_subs.f - created on 28-jul-2004
3870 !       by downloading following from www.netlib.org
3871 !       1.  daxpy, dcopy, ddot, dnrm2, dscal, idamax from blas
3872 !       2.  dgbfa, dbgsl, dgefa, dgesl from linpack
3874 !   27-oct-2005 rce - change '1' dimensions in subr dgefa & dgesl
3875 !-----------------------------------------------------------------------
3878 !-----------------------------------------------------------------------
3879       subroutine daxpy(n,da,dx,incx,dy,incy)
3881 !     constant times a vector plus a vector.
3882 !     uses unrolled loops for increments equal to one.
3883 !     jack dongarra, linpack, 3/11/78.
3884 !     modified 12/3/93, array(1) declarations changed to array(*)
3886       double precision dx(*),dy(*),da
3887       integer i,incx,incy,ix,iy,m,mp1,n
3889       if(n.le.0)return
3890       if (da .eq. 0.0d0) return
3891       if(incx.eq.1.and.incy.eq.1)go to 20
3893 !        code for unequal increments or equal increments
3894 !          not equal to 1
3896       ix = 1
3897       iy = 1
3898       if(incx.lt.0)ix = (-n+1)*incx + 1
3899       if(incy.lt.0)iy = (-n+1)*incy + 1
3900       do 10 i = 1,n
3901         dy(iy) = dy(iy) + da*dx(ix)
3902         ix = ix + incx
3903         iy = iy + incy
3904    10 continue
3905       return
3907 !        code for both increments equal to 1
3910 !        clean-up loop
3912    20 m = mod(n,4)
3913       if( m .eq. 0 ) go to 40
3914       do 30 i = 1,m
3915         dy(i) = dy(i) + da*dx(i)
3916    30 continue
3917       if( n .lt. 4 ) return
3918    40 mp1 = m + 1
3919       do 50 i = mp1,n,4
3920         dy(i) = dy(i) + da*dx(i)
3921         dy(i + 1) = dy(i + 1) + da*dx(i + 1)
3922         dy(i + 2) = dy(i + 2) + da*dx(i + 2)
3923         dy(i + 3) = dy(i + 3) + da*dx(i + 3)
3924    50 continue
3925       return
3926       end subroutine daxpy
3929 !-----------------------------------------------------------------------
3930       subroutine  dcopy(n,dx,incx,dy,incy)
3932 !     copies a vector, x, to a vector, y.
3933 !     uses unrolled loops for increments equal to one.
3934 !     jack dongarra, linpack, 3/11/78.
3935 !     modified 12/3/93, array(1) declarations changed to array(*)
3937       double precision dx(*),dy(*)
3938       integer i,incx,incy,ix,iy,m,mp1,n
3940       if(n.le.0)return
3941       if(incx.eq.1.and.incy.eq.1)go to 20
3943 !        code for unequal increments or equal increments
3944 !          not equal to 1
3946       ix = 1
3947       iy = 1
3948       if(incx.lt.0)ix = (-n+1)*incx + 1
3949       if(incy.lt.0)iy = (-n+1)*incy + 1
3950       do 10 i = 1,n
3951         dy(iy) = dx(ix)
3952         ix = ix + incx
3953         iy = iy + incy
3954    10 continue
3955       return
3957 !        code for both increments equal to 1
3960 !        clean-up loop
3962    20 m = mod(n,7)
3963       if( m .eq. 0 ) go to 40
3964       do 30 i = 1,m
3965         dy(i) = dx(i)
3966    30 continue
3967       if( n .lt. 7 ) return
3968    40 mp1 = m + 1
3969       do 50 i = mp1,n,7
3970         dy(i) = dx(i)
3971         dy(i + 1) = dx(i + 1)
3972         dy(i + 2) = dx(i + 2)
3973         dy(i + 3) = dx(i + 3)
3974         dy(i + 4) = dx(i + 4)
3975         dy(i + 5) = dx(i + 5)
3976         dy(i + 6) = dx(i + 6)
3977    50 continue
3978       return
3979       end subroutine  dcopy
3982       double precision function ddot(n,dx,incx,dy,incy)
3984 !     forms the dot product of two vectors.
3985 !     uses unrolled loops for increments equal to one.
3986 !     jack dongarra, linpack, 3/11/78.
3987 !     modified 12/3/93, array(1) declarations changed to array(*)
3989       double precision dx(*),dy(*),dtemp
3990       integer i,incx,incy,ix,iy,m,mp1,n
3992       ddot = 0.0d0
3993       dtemp = 0.0d0
3994       if(n.le.0)return
3995       if(incx.eq.1.and.incy.eq.1)go to 20
3997 !        code for unequal increments or equal increments
3998 !          not equal to 1
4000       ix = 1
4001       iy = 1
4002       if(incx.lt.0)ix = (-n+1)*incx + 1
4003       if(incy.lt.0)iy = (-n+1)*incy + 1
4004       do 10 i = 1,n
4005         dtemp = dtemp + dx(ix)*dy(iy)
4006         ix = ix + incx
4007         iy = iy + incy
4008    10 continue
4009       ddot = dtemp
4010       return
4012 !        code for both increments equal to 1
4015 !        clean-up loop
4017    20 m = mod(n,5)
4018       if( m .eq. 0 ) go to 40
4019       do 30 i = 1,m
4020         dtemp = dtemp + dx(i)*dy(i)
4021    30 continue
4022       if( n .lt. 5 ) go to 60
4023    40 mp1 = m + 1
4024       do 50 i = mp1,n,5
4025         dtemp = dtemp + dx(i)*dy(i) + dx(i + 1)*dy(i + 1) +   &
4026          dx(i + 2)*dy(i + 2) + dx(i + 3)*dy(i + 3) + dx(i + 4)*dy(i + 4)
4027    50 continue
4028    60 ddot = dtemp
4029       return
4030       end function ddot
4033 !-----------------------------------------------------------------------
4034       double precision function dnrm2 ( n, x, incx )
4035 !     .. scalar arguments ..
4036       integer                           incx, n
4037 !     .. array arguments ..
4038       double precision                  x( * )
4039 !     ..
4041 !  dnrm2 returns the euclidean norm of a vector via the function
4042 !  name, so that
4044 !     dnrm2 := sqrt( x'*x )
4048 !  -- this version written on 25-october-1982.
4049 !     modified on 14-october-1993 to inline the call to dlassq.
4050 !     sven hammarling, nag ltd.
4053 !     .. parameters ..
4054       double precision      one         , zero
4055       parameter           ( one = 1.0d+0, zero = 0.0d+0 )
4056 !     .. local scalars ..
4057       integer               ix
4058       double precision      absxi, norm, scale, ssq
4059 !     .. intrinsic functions ..
4060       intrinsic             abs, sqrt
4061 !     ..
4062 !     .. executable statements ..
4063       if( n.lt.1 .or. incx.lt.1 )then
4064          norm  = zero
4065       else if( n.eq.1 )then
4066          norm  = abs( x( 1 ) )
4067       else
4068          scale = zero
4069          ssq   = one
4070 !        the following loop is equivalent to this call to the lapack
4071 !        auxiliary routine:
4072 !        call dlassq( n, x, incx, scale, ssq )
4074          do 10, ix = 1, 1 + ( n - 1 )*incx, incx
4075             if( x( ix ).ne.zero )then
4076                absxi = abs( x( ix ) )
4077                if( scale.lt.absxi )then
4078                   ssq   = one   + ssq*( scale/absxi )**2
4079                   scale = absxi
4080                else
4081                   ssq   = ssq   +     ( absxi/scale )**2
4082                end if
4083             end if
4084    10    continue
4085          norm  = scale * sqrt( ssq )
4086       end if
4088       dnrm2 = norm
4089       return
4091 !     end of dnrm2.
4093       end function dnrm2
4096 !-----------------------------------------------------------------------
4097       subroutine  dscal(n,da,dx,incx)
4099 !     scales a vector by a constant.
4100 !     uses unrolled loops for increment equal to one.
4101 !     jack dongarra, linpack, 3/11/78.
4102 !     modified 3/93 to return if incx .le. 0.
4103 !     modified 12/3/93, array(1) declarations changed to array(*)
4105       double precision da,dx(*)
4106       integer i,incx,m,mp1,n,nincx
4108       if( n.le.0 .or. incx.le.0 )return
4109       if(incx.eq.1)go to 20
4111 !        code for increment not equal to 1
4113       nincx = n*incx
4114       do 10 i = 1,nincx,incx
4115         dx(i) = da*dx(i)
4116    10 continue
4117       return
4119 !        code for increment equal to 1
4122 !        clean-up loop
4124    20 m = mod(n,5)
4125       if( m .eq. 0 ) go to 40
4126       do 30 i = 1,m
4127         dx(i) = da*dx(i)
4128    30 continue
4129       if( n .lt. 5 ) return
4130    40 mp1 = m + 1
4131       do 50 i = mp1,n,5
4132         dx(i) = da*dx(i)
4133         dx(i + 1) = da*dx(i + 1)
4134         dx(i + 2) = da*dx(i + 2)
4135         dx(i + 3) = da*dx(i + 3)
4136         dx(i + 4) = da*dx(i + 4)
4137    50 continue
4138       return
4139       end subroutine  dscal
4142 !-----------------------------------------------------------------------
4143       integer function idamax(n,dx,incx)
4145 !     finds the index of element having max. absolute value.
4146 !     jack dongarra, linpack, 3/11/78.
4147 !     modified 3/93 to return if incx .le. 0.
4148 !     modified 12/3/93, array(1) declarations changed to array(*)
4150       double precision dx(*),dmax
4151       integer i,incx,ix,n
4153       idamax = 0
4154       if( n.lt.1 .or. incx.le.0 ) return
4155       idamax = 1
4156       if(n.eq.1)return
4157       if(incx.eq.1)go to 20
4159 !        code for increment not equal to 1
4161       ix = 1
4162       dmax = dabs(dx(1))
4163       ix = ix + incx
4164       do 10 i = 2,n
4165          if(dabs(dx(ix)).le.dmax) go to 5
4166          idamax = i
4167          dmax = dabs(dx(ix))
4168     5    ix = ix + incx
4169    10 continue
4170       return
4172 !        code for increment equal to 1
4174    20 dmax = dabs(dx(1))
4175       do 30 i = 2,n
4176          if(dabs(dx(i)).le.dmax) go to 30
4177          idamax = i
4178          dmax = dabs(dx(i))
4179    30 continue
4180       return
4181       end function idamax
4184 !-----------------------------------------------------------------------
4185       subroutine dgbfa(abd,lda,n,ml,mu,ipvt,info)
4186       integer lda,n,ml,mu,ipvt(1),info
4187       double precision abd(lda,1)
4189 !     dgbfa factors a double precision band matrix by elimination.
4191 !     dgbfa is usually called by dgbco, but it can be called
4192 !     directly with a saving in time if  rcond  is not needed.
4194 !     on entry
4196 !        abd     double precision(lda, n)
4197 !                contains the matrix in band storage.  the columns
4198 !                of the matrix are stored in the columns of  abd  and
4199 !                the diagonals of the matrix are stored in rows
4200 !                ml+1 through 2*ml+mu+1 of  abd .
4201 !                see the comments below for details.
4203 !        lda     integer
4204 !                the leading dimension of the array  abd .
4205 !                lda must be .ge. 2*ml + mu + 1 .
4207 !        n       integer
4208 !                the order of the original matrix.
4210 !        ml      integer
4211 !                number of diagonals below the main diagonal.
4212 !                0 .le. ml .lt. n .
4214 !        mu      integer
4215 !                number of diagonals above the main diagonal.
4216 !                0 .le. mu .lt. n .
4217 !                more efficient if  ml .le. mu .
4218 !     on return
4220 !        abd     an upper triangular matrix in band storage and
4221 !                the multipliers which were used to obtain it.
4222 !                the factorization can be written  a = l*u  where
4223 !                l  is a product of permutation and unit lower
4224 !                triangular matrices and  u  is upper triangular.
4226 !        ipvt    integer(n)
4227 !                an integer vector of pivot indices.
4229 !        info    integer
4230 !                = 0  normal value.
4231 !                = k  if  u(k,k) .eq. 0.0 .  this is not an error
4232 !                     condition for this subroutine, but it does
4233 !                     indicate that dgbsl will divide by zero if
4234 !                     called.  use  rcond  in dgbco for a reliable
4235 !                     indication of singularity.
4237 !     band storage
4239 !           if  a  is a band matrix, the following program segment
4240 !           will set up the input.
4242 !                   ml = (band width below the diagonal)
4243 !                   mu = (band width above the diagonal)
4244 !                   m = ml + mu + 1
4245 !                   do 20 j = 1, n
4246 !                      i1 = max0(1, j-mu)
4247 !                      i2 = min0(n, j+ml)
4248 !                      do 10 i = i1, i2
4249 !                         k = i - j + m
4250 !                         abd(k,j) = a(i,j)
4251 !                10    continue
4252 !                20 continue
4254 !           this uses rows  ml+1  through  2*ml+mu+1  of  abd .
4255 !           in addition, the first  ml  rows in  abd  are used for
4256 !           elements generated during the triangularization.
4257 !           the total number of rows needed in  abd  is  2*ml+mu+1 .
4258 !           the  ml+mu by ml+mu  upper left triangle and the
4259 !           ml by ml  lower right triangle are not referenced.
4261 !     linpack. this version dated 08/14/78 .
4262 !     cleve moler, university of new mexico, argonne national lab.
4264 !     subroutines and functions
4266 !     blas daxpy,dscal,idamax
4267 !     fortran max0,min0
4269 !     internal variables
4271       double precision t
4272 ! 27-oct-2005 rce - do not declare functions that are in the module
4273 !     integer i,idamax,i0,j,ju,jz,j0,j1,k,kp1,l,lm,m,mm,nm1
4274       integer i,       i0,j,ju,jz,j0,j1,k,kp1,l,lm,m,mm,nm1
4277       m = ml + mu + 1
4278       info = 0
4280 !     zero initial fill-in columns
4282       j0 = mu + 2
4283       j1 = min0(n,m) - 1
4284       if (j1 .lt. j0) go to 30
4285       do 20 jz = j0, j1
4286          i0 = m + 1 - jz
4287          do 10 i = i0, ml
4288             abd(i,jz) = 0.0d0
4289    10    continue
4290    20 continue
4291    30 continue
4292       jz = j1
4293       ju = 0
4295 !     gaussian elimination with partial pivoting
4297       nm1 = n - 1
4298       if (nm1 .lt. 1) go to 130
4299       do 120 k = 1, nm1
4300          kp1 = k + 1
4302 !        zero next fill-in column
4304          jz = jz + 1
4305          if (jz .gt. n) go to 50
4306          if (ml .lt. 1) go to 50
4307             do 40 i = 1, ml
4308                abd(i,jz) = 0.0d0
4309    40       continue
4310    50    continue
4312 !        find l = pivot index
4314          lm = min0(ml,n-k)
4315          l = idamax(lm+1,abd(m,k),1) + m - 1
4316          ipvt(k) = l + k - m
4318 !        zero pivot implies this column already triangularized
4320          if (abd(l,k) .eq. 0.0d0) go to 100
4322 !           interchange if necessary
4324             if (l .eq. m) go to 60
4325                t = abd(l,k)
4326                abd(l,k) = abd(m,k)
4327                abd(m,k) = t
4328    60       continue
4330 !           compute multipliers
4332             t = -1.0d0/abd(m,k)
4333             call dscal(lm,t,abd(m+1,k),1)
4335 !           row elimination with column indexing
4337             ju = min0(max0(ju,mu+ipvt(k)),n)
4338             mm = m
4339             if (ju .lt. kp1) go to 90
4340             do 80 j = kp1, ju
4341                l = l - 1
4342                mm = mm - 1
4343                t = abd(l,j)
4344                if (l .eq. mm) go to 70
4345                   abd(l,j) = abd(mm,j)
4346                   abd(mm,j) = t
4347    70          continue
4348                call daxpy(lm,t,abd(m+1,k),1,abd(mm+1,j),1)
4349    80       continue
4350    90       continue
4351          go to 110
4352   100    continue
4353             info = k
4354   110    continue
4355   120 continue
4356   130 continue
4357       ipvt(n) = n
4358       if (abd(m,n) .eq. 0.0d0) info = n
4359       return
4360       end subroutine dgbfa
4363 !-----------------------------------------------------------------------
4364       subroutine dgbsl(abd,lda,n,ml,mu,ipvt,b,job)
4365       integer lda,n,ml,mu,ipvt(1),job
4366       double precision abd(lda,1),b(1)
4368 !     dgbsl solves the double precision band system
4369 !     a * x = b  or  trans(a) * x = b
4370 !     using the factors computed by dgbco or dgbfa.
4372 !     on entry
4374 !        abd     double precision(lda, n)
4375 !                the output from dgbco or dgbfa.
4377 !        lda     integer
4378 !                the leading dimension of the array  abd .
4380 !        n       integer
4381 !                the order of the original matrix.
4383 !        ml      integer
4384 !                number of diagonals below the main diagonal.
4386 !        mu      integer
4387 !                number of diagonals above the main diagonal.
4389 !        ipvt    integer(n)
4390 !                the pivot vector from dgbco or dgbfa.
4392 !        b       double precision(n)
4393 !                the right hand side vector.
4395 !        job     integer
4396 !                = 0         to solve  a*x = b ,
4397 !                = nonzero   to solve  trans(a)*x = b , where
4398 !                            trans(a)  is the transpose.
4400 !     on return
4402 !        b       the solution vector  x .
4404 !     error condition
4406 !        a division by zero will occur if the input factor contains a
4407 !        zero on the diagonal.  technically this indicates singularity
4408 !        but it is often caused by improper arguments or improper
4409 !        setting of lda .  it will not occur if the subroutines are
4410 !        called correctly and if dgbco has set rcond .gt. 0.0
4411 !        or dgbfa has set info .eq. 0 .
4413 !     to compute  inverse(a) * c  where  c  is a matrix
4414 !     with  p  columns
4415 !           call dgbco(abd,lda,n,ml,mu,ipvt,rcond,z)
4416 !           if (rcond is too small) go to ...
4417 !           do 10 j = 1, p
4418 !              call dgbsl(abd,lda,n,ml,mu,ipvt,c(1,j),0)
4419 !        10 continue
4421 !     linpack. this version dated 08/14/78 .
4422 !     cleve moler, university of new mexico, argonne national lab.
4424 !     subroutines and functions
4426 !     blas daxpy,ddot
4427 !     fortran min0
4429 !     internal variables
4431 ! 27-oct-2005 rce - do not declare functions that are in the module
4432 !     double precision ddot,t
4433       double precision      t
4434       integer k,kb,l,la,lb,lm,m,nm1
4436       m = mu + ml + 1
4437       nm1 = n - 1
4438       if (job .ne. 0) go to 50
4440 !        job = 0 , solve  a * x = b
4441 !        first solve l*y = b
4443          if (ml .eq. 0) go to 30
4444          if (nm1 .lt. 1) go to 30
4445             do 20 k = 1, nm1
4446                lm = min0(ml,n-k)
4447                l = ipvt(k)
4448                t = b(l)
4449                if (l .eq. k) go to 10
4450                   b(l) = b(k)
4451                   b(k) = t
4452    10          continue
4453                call daxpy(lm,t,abd(m+1,k),1,b(k+1),1)
4454    20       continue
4455    30    continue
4457 !        now solve  u*x = y
4459          do 40 kb = 1, n
4460             k = n + 1 - kb
4461             b(k) = b(k)/abd(m,k)
4462             lm = min0(k,m) - 1
4463             la = m - lm
4464             lb = k - lm
4465             t = -b(k)
4466             call daxpy(lm,t,abd(la,k),1,b(lb),1)
4467    40    continue
4468       go to 100
4469    50 continue
4471 !        job = nonzero, solve  trans(a) * x = b
4472 !        first solve  trans(u)*y = b
4474          do 60 k = 1, n
4475             lm = min0(k,m) - 1
4476             la = m - lm
4477             lb = k - lm
4478             t = ddot(lm,abd(la,k),1,b(lb),1)
4479             b(k) = (b(k) - t)/abd(m,k)
4480    60    continue
4482 !        now solve trans(l)*x = y
4484          if (ml .eq. 0) go to 90
4485          if (nm1 .lt. 1) go to 90
4486             do 80 kb = 1, nm1
4487                k = n - kb
4488                lm = min0(ml,n-k)
4489                b(k) = b(k) + ddot(lm,abd(m+1,k),1,b(k+1),1)
4490                l = ipvt(k)
4491                if (l .eq. k) go to 70
4492                   t = b(l)
4493                   b(l) = b(k)
4494                   b(k) = t
4495    70          continue
4496    80       continue
4497    90    continue
4498   100 continue
4499       return
4500       end subroutine dgbsl
4503 !-----------------------------------------------------------------------
4504       subroutine dgefa(a,lda,n,ipvt,info)
4505 ! 27-oct-2005 rce - change '1' dimensions
4506 !     integer lda,n,ipvt(1),info
4507       integer lda,n,ipvt(n),info
4508 !     double precision a(lda,1)
4509       double precision a(lda,n)
4511 !     dgefa factors a double precision matrix by gaussian elimination.
4513 !     dgefa is usually called by dgeco, but it can be called
4514 !     directly with a saving in time if  rcond  is not needed.
4515 !     (time for dgeco) = (1 + 9/n)*(time for dgefa) .
4517 !     on entry
4519 !        a       double precision(lda, n)
4520 !                the matrix to be factored.
4522 !        lda     integer
4523 !                the leading dimension of the array  a .
4525 !        n       integer
4526 !                the order of the matrix  a .
4528 !     on return
4530 !        a       an upper triangular matrix and the multipliers
4531 !                which were used to obtain it.
4532 !                the factorization can be written  a = l*u  where
4533 !                l  is a product of permutation and unit lower
4534 !                triangular matrices and  u  is upper triangular.
4536 !        ipvt    integer(n)
4537 !                an integer vector of pivot indices.
4539 !        info    integer
4540 !                = 0  normal value.
4541 !                = k  if  u(k,k) .eq. 0.0 .  this is not an error
4542 !                     condition for this subroutine, but it does
4543 !                     indicate that dgesl or dgedi will divide by zero
4544 !                     if called.  use  rcond  in dgeco for a reliable
4545 !                     indication of singularity.
4547 !     linpack. this version dated 08/14/78 .
4548 !     cleve moler, university of new mexico, argonne national lab.
4550 !     subroutines and functions
4552 !     blas daxpy,dscal,idamax
4554 !     internal variables
4556       double precision t
4557 ! 27-oct-2005 rce - do not declare functions that are in the module
4558 !     integer idamax,j,k,kp1,l,nm1
4559       integer        j,k,kp1,l,nm1
4562 !     gaussian elimination with partial pivoting
4564       info = 0
4565       nm1 = n - 1
4566       if (nm1 .lt. 1) go to 70
4567       do 60 k = 1, nm1
4568          kp1 = k + 1
4570 !        find l = pivot index
4572          l = idamax(n-k+1,a(k,k),1) + k - 1
4573          ipvt(k) = l
4575 !        zero pivot implies this column already triangularized
4577          if (a(l,k) .eq. 0.0d0) go to 40
4579 !           interchange if necessary
4581             if (l .eq. k) go to 10
4582                t = a(l,k)
4583                a(l,k) = a(k,k)
4584                a(k,k) = t
4585    10       continue
4587 !           compute multipliers
4589             t = -1.0d0/a(k,k)
4590             call dscal(n-k,t,a(k+1,k),1)
4592 !           row elimination with column indexing
4594             do 30 j = kp1, n
4595                t = a(l,j)
4596                if (l .eq. k) go to 20
4597                   a(l,j) = a(k,j)
4598                   a(k,j) = t
4599    20          continue
4600                call daxpy(n-k,t,a(k+1,k),1,a(k+1,j),1)
4601    30       continue
4602          go to 50
4603    40    continue
4604             info = k
4605    50    continue
4606    60 continue
4607    70 continue
4608       ipvt(n) = n
4609       if (a(n,n) .eq. 0.0d0) info = n
4610       return
4611       end subroutine dgefa
4614 !-----------------------------------------------------------------------
4615       subroutine dgesl(a,lda,n,ipvt,b,job)
4616 ! 27-oct-2005 rce - change '1' dimensions
4617 !     integer lda,n,ipvt(1),job
4618       integer lda,n,ipvt(n),job
4619 !     double precision a(lda,1),b(1)
4620       double precision a(lda,n),b(n)
4622 !     dgesl solves the double precision system
4623 !     a * x = b  or  trans(a) * x = b
4624 !     using the factors computed by dgeco or dgefa.
4626 !     on entry
4628 !        a       double precision(lda, n)
4629 !                the output from dgeco or dgefa.
4631 !        lda     integer
4632 !                the leading dimension of the array  a .
4634 !        n       integer
4635 !                the order of the matrix  a .
4637 !        ipvt    integer(n)
4638 !                the pivot vector from dgeco or dgefa.
4640 !        b       double precision(n)
4641 !                the right hand side vector.
4643 !        job     integer
4644 !                = 0         to solve  a*x = b ,
4645 !                = nonzero   to solve  trans(a)*x = b  where
4646 !                            trans(a)  is the transpose.
4648 !     on return
4650 !        b       the solution vector  x .
4652 !     error condition
4654 !        a division by zero will occur if the input factor contains a
4655 !        zero on the diagonal.  technically this indicates singularity
4656 !        but it is often caused by improper arguments or improper
4657 !        setting of lda .  it will not occur if the subroutines are
4658 !        called correctly and if dgeco has set rcond .gt. 0.0
4659 !        or dgefa has set info .eq. 0 .
4661 !     to compute  inverse(a) * c  where  c  is a matrix
4662 !     with  p  columns
4663 !           call dgeco(a,lda,n,ipvt,rcond,z)
4664 !           if (rcond is too small) go to ...
4665 !           do 10 j = 1, p
4666 !              call dgesl(a,lda,n,ipvt,c(1,j),0)
4667 !        10 continue
4669 !     linpack. this version dated 08/14/78 .
4670 !     cleve moler, university of new mexico, argonne national lab.
4672 !     subroutines and functions
4674 !     blas daxpy,ddot
4676 !     internal variables
4678 ! 27-oct-2005 rce - do not declare functions that are in the module
4679 !     double precision ddot,t
4680       double precision      t
4681       integer k,kb,l,nm1
4683       nm1 = n - 1
4684       if (job .ne. 0) go to 50
4686 !        job = 0 , solve  a * x = b
4687 !        first solve  l*y = b
4689          if (nm1 .lt. 1) go to 30
4690          do 20 k = 1, nm1
4691             l = ipvt(k)
4692             t = b(l)
4693             if (l .eq. k) go to 10
4694                b(l) = b(k)
4695                b(k) = t
4696    10       continue
4697             call daxpy(n-k,t,a(k+1,k),1,b(k+1),1)
4698    20    continue
4699    30    continue
4701 !        now solve  u*x = y
4703          do 40 kb = 1, n
4704             k = n + 1 - kb
4705             b(k) = b(k)/a(k,k)
4706             t = -b(k)
4707             call daxpy(k-1,t,a(1,k),1,b(1),1)
4708    40    continue
4709       go to 100
4710    50 continue
4712 !        job = nonzero, solve  trans(a) * x = b
4713 !        first solve  trans(u)*y = b
4715          do 60 k = 1, n
4716             t = ddot(k-1,a(1,k),1,b(1),1)
4717             b(k) = (b(k) - t)/a(k,k)
4718    60    continue
4720 !        now solve trans(l)*x = y
4722          if (nm1 .lt. 1) go to 90
4723          do 80 kb = 1, nm1
4724             k = n - kb
4725             b(k) = b(k) + ddot(n-k,a(k+1,k),1,b(k+1),1)
4726             l = ipvt(k)
4727             if (l .eq. k) go to 70
4728                t = b(l)
4729                b(l) = b(k)
4730                b(k) = t
4731    70       continue
4732    80    continue
4733    90    continue
4734   100 continue
4735       return
4736       end subroutine dgesl
4740       end module module_cmu_dvode_solver