dhstrt.f 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. *DECK DHSTRT
  2. SUBROUTINE DHSTRT (DF, NEQ, A, B, Y, YPRIME, ETOL, MORDER, SMALL,
  3. + BIG, SPY, PV, YP, SF, RPAR, IPAR, H)
  4. C***BEGIN PROLOGUE DHSTRT
  5. C***SUBSIDIARY
  6. C***PURPOSE Subsidiary to DDEABM, DDEBDF and DDERKF
  7. C***LIBRARY SLATEC
  8. C***TYPE DOUBLE PRECISION (HSTART-S, DHSTRT-D)
  9. C***AUTHOR Watts, H. A., (SNLA)
  10. C***DESCRIPTION
  11. C
  12. C DHSTRT computes a starting step size to be used in solving initial
  13. C value problems in ordinary differential equations.
  14. C
  15. C **********************************************************************
  16. C ABSTRACT
  17. C
  18. C Subroutine DHSTRT computes a starting step size to be used by an
  19. C initial value method in solving ordinary differential equations.
  20. C It is based on an estimate of the local Lipschitz constant for the
  21. C differential equation (lower bound on a norm of the Jacobian) ,
  22. C a bound on the differential equation (first derivative) , and
  23. C a bound on the partial derivative of the equation with respect to
  24. C the independent variable.
  25. C (all approximated near the initial point A)
  26. C
  27. C Subroutine DHSTRT uses a function subprogram DHVNRM for computing
  28. C a vector norm. The maximum norm is presently utilized though it
  29. C can easily be replaced by any other vector norm. It is presumed
  30. C that any replacement norm routine would be carefully coded to
  31. C prevent unnecessary underflows or overflows from occurring, and
  32. C also, would not alter the vector or number of components.
  33. C
  34. C **********************************************************************
  35. C On input you must provide the following
  36. C
  37. C DF -- This is a subroutine of the form
  38. C DF(X,U,UPRIME,RPAR,IPAR)
  39. C which defines the system of first order differential
  40. C equations to be solved. For the given values of X and the
  41. C vector U(*)=(U(1),U(2),...,U(NEQ)) , the subroutine must
  42. C evaluate the NEQ components of the system of differential
  43. C equations DU/DX=DF(X,U) and store the derivatives in the
  44. C array UPRIME(*), that is, UPRIME(I) = * DU(I)/DX * for
  45. C equations I=1,...,NEQ.
  46. C
  47. C Subroutine DF must not alter X or U(*). You must declare
  48. C the name DF in an external statement in your program that
  49. C calls DHSTRT. You must dimension U and UPRIME in DF.
  50. C
  51. C RPAR and IPAR are DOUBLE PRECISION and INTEGER parameter
  52. C arrays which you can use for communication between your
  53. C program and subroutine DF. They are not used or altered by
  54. C DHSTRT. If you do not need RPAR or IPAR, ignore these
  55. C parameters by treating them as dummy arguments. If you do
  56. C choose to use them, dimension them in your program and in
  57. C DF as arrays of appropriate length.
  58. C
  59. C NEQ -- This is the number of (first order) differential equations
  60. C to be integrated.
  61. C
  62. C A -- This is the initial point of integration.
  63. C
  64. C B -- This is a value of the independent variable used to define
  65. C the direction of integration. A reasonable choice is to
  66. C set B to the first point at which a solution is desired.
  67. C You can also use B, if necessary, to restrict the length
  68. C of the first integration step because the algorithm will
  69. C not compute a starting step length which is bigger than
  70. C ABS(B-A), unless B has been chosen too close to A.
  71. C (it is presumed that DHSTRT has been called with B
  72. C different from A on the machine being used. Also see the
  73. C discussion about the parameter SMALL.)
  74. C
  75. C Y(*) -- This is the vector of initial values of the NEQ solution
  76. C components at the initial point A.
  77. C
  78. C YPRIME(*) -- This is the vector of derivatives of the NEQ
  79. C solution components at the initial point A.
  80. C (defined by the differential equations in subroutine DF)
  81. C
  82. C ETOL -- This is the vector of error tolerances corresponding to
  83. C the NEQ solution components. It is assumed that all
  84. C elements are positive. Following the first integration
  85. C step, the tolerances are expected to be used by the
  86. C integrator in an error test which roughly requires that
  87. C ABS(LOCAL ERROR) .LE. ETOL
  88. C for each vector component.
  89. C
  90. C MORDER -- This is the order of the formula which will be used by
  91. C the initial value method for taking the first integration
  92. C step.
  93. C
  94. C SMALL -- This is a small positive machine dependent constant
  95. C which is used for protecting against computations with
  96. C numbers which are too small relative to the precision of
  97. C floating point arithmetic. SMALL should be set to
  98. C (approximately) the smallest positive DOUBLE PRECISION
  99. C number such that (1.+SMALL) .GT. 1. on the machine being
  100. C used. The quantity SMALL**(3/8) is used in computing
  101. C increments of variables for approximating derivatives by
  102. C differences. Also the algorithm will not compute a
  103. C starting step length which is smaller than
  104. C 100*SMALL*ABS(A).
  105. C
  106. C BIG -- This is a large positive machine dependent constant which
  107. C is used for preventing machine overflows. A reasonable
  108. C choice is to set big to (approximately) the square root of
  109. C the largest DOUBLE PRECISION number which can be held in
  110. C the machine.
  111. C
  112. C SPY(*),PV(*),YP(*),SF(*) -- These are DOUBLE PRECISION work
  113. C arrays of length NEQ which provide the routine with needed
  114. C storage space.
  115. C
  116. C RPAR,IPAR -- These are parameter arrays, of DOUBLE PRECISION and
  117. C INTEGER type, respectively, which can be used for
  118. C communication between your program and the DF subroutine.
  119. C They are not used or altered by DHSTRT.
  120. C
  121. C **********************************************************************
  122. C On Output (after the return from DHSTRT),
  123. C
  124. C H -- is an appropriate starting step size to be attempted by the
  125. C differential equation method.
  126. C
  127. C All parameters in the call list remain unchanged except for
  128. C the working arrays SPY(*),PV(*),YP(*), and SF(*).
  129. C
  130. C **********************************************************************
  131. C
  132. C***SEE ALSO DDEABM, DDEBDF, DDERKF
  133. C***ROUTINES CALLED DHVNRM
  134. C***REVISION HISTORY (YYMMDD)
  135. C 820301 DATE WRITTEN
  136. C 890531 Changed all specific intrinsics to generic. (WRB)
  137. C 890831 Modified array declarations. (WRB)
  138. C 890911 Removed unnecessary intrinsics. (WRB)
  139. C 891024 Changed references from DVNORM to DHVNRM. (WRB)
  140. C 891214 Prologue converted to Version 4.0 format. (BAB)
  141. C 900328 Added TYPE section. (WRB)
  142. C 910722 Updated AUTHOR section. (ALS)
  143. C***END PROLOGUE DHSTRT
  144. C
  145. INTEGER IPAR, J, K, LK, MORDER, NEQ
  146. DOUBLE PRECISION A, ABSDX, B, BIG, DA, DELF, DELY,
  147. 1 DFDUB, DFDXB, DHVNRM,
  148. 2 DX, DY, ETOL, FBND, H, PV, RELPER, RPAR, SF, SMALL, SPY,
  149. 3 SRYDPB, TOLEXP, TOLMIN, TOLP, TOLSUM, Y, YDPB, YP, YPRIME
  150. DIMENSION Y(*),YPRIME(*),ETOL(*),SPY(*),PV(*),YP(*),
  151. 1 SF(*),RPAR(*),IPAR(*)
  152. EXTERNAL DF
  153. C
  154. C ..................................................................
  155. C
  156. C BEGIN BLOCK PERMITTING ...EXITS TO 160
  157. C***FIRST EXECUTABLE STATEMENT DHSTRT
  158. DX = B - A
  159. ABSDX = ABS(DX)
  160. RELPER = SMALL**0.375D0
  161. C
  162. C ...............................................................
  163. C
  164. C COMPUTE AN APPROXIMATE BOUND (DFDXB) ON THE PARTIAL
  165. C DERIVATIVE OF THE EQUATION WITH RESPECT TO THE
  166. C INDEPENDENT VARIABLE. PROTECT AGAINST AN OVERFLOW.
  167. C ALSO COMPUTE A BOUND (FBND) ON THE FIRST DERIVATIVE
  168. C LOCALLY.
  169. C
  170. DA = SIGN(MAX(MIN(RELPER*ABS(A),ABSDX),
  171. 1 100.0D0*SMALL*ABS(A)),DX)
  172. IF (DA .EQ. 0.0D0) DA = RELPER*DX
  173. CALL DF(A+DA,Y,SF,RPAR,IPAR)
  174. DO 10 J = 1, NEQ
  175. YP(J) = SF(J) - YPRIME(J)
  176. 10 CONTINUE
  177. DELF = DHVNRM(YP,NEQ)
  178. DFDXB = BIG
  179. IF (DELF .LT. BIG*ABS(DA)) DFDXB = DELF/ABS(DA)
  180. FBND = DHVNRM(SF,NEQ)
  181. C
  182. C ...............................................................
  183. C
  184. C COMPUTE AN ESTIMATE (DFDUB) OF THE LOCAL LIPSCHITZ
  185. C CONSTANT FOR THE SYSTEM OF DIFFERENTIAL EQUATIONS. THIS
  186. C ALSO REPRESENTS AN ESTIMATE OF THE NORM OF THE JACOBIAN
  187. C LOCALLY. THREE ITERATIONS (TWO WHEN NEQ=1) ARE USED TO
  188. C ESTIMATE THE LIPSCHITZ CONSTANT BY NUMERICAL DIFFERENCES.
  189. C THE FIRST PERTURBATION VECTOR IS BASED ON THE INITIAL
  190. C DERIVATIVES AND DIRECTION OF INTEGRATION. THE SECOND
  191. C PERTURBATION VECTOR IS FORMED USING ANOTHER EVALUATION OF
  192. C THE DIFFERENTIAL EQUATION. THE THIRD PERTURBATION VECTOR
  193. C IS FORMED USING PERTURBATIONS BASED ONLY ON THE INITIAL
  194. C VALUES. COMPONENTS THAT ARE ZERO ARE ALWAYS CHANGED TO
  195. C NON-ZERO VALUES (EXCEPT ON THE FIRST ITERATION). WHEN
  196. C INFORMATION IS AVAILABLE, CARE IS TAKEN TO ENSURE THAT
  197. C COMPONENTS OF THE PERTURBATION VECTOR HAVE SIGNS WHICH ARE
  198. C CONSISTENT WITH THE SLOPES OF LOCAL SOLUTION CURVES.
  199. C ALSO CHOOSE THE LARGEST BOUND (FBND) FOR THE FIRST
  200. C DERIVATIVE.
  201. C
  202. C PERTURBATION VECTOR SIZE IS HELD
  203. C CONSTANT FOR ALL ITERATIONS. COMPUTE
  204. C THIS CHANGE FROM THE
  205. C SIZE OF THE VECTOR OF INITIAL
  206. C VALUES.
  207. DELY = RELPER*DHVNRM(Y,NEQ)
  208. IF (DELY .EQ. 0.0D0) DELY = RELPER
  209. DELY = SIGN(DELY,DX)
  210. DELF = DHVNRM(YPRIME,NEQ)
  211. FBND = MAX(FBND,DELF)
  212. IF (DELF .EQ. 0.0D0) GO TO 30
  213. C USE INITIAL DERIVATIVES FOR FIRST PERTURBATION
  214. DO 20 J = 1, NEQ
  215. SPY(J) = YPRIME(J)
  216. YP(J) = YPRIME(J)
  217. 20 CONTINUE
  218. GO TO 50
  219. 30 CONTINUE
  220. C CANNOT HAVE A NULL PERTURBATION VECTOR
  221. DO 40 J = 1, NEQ
  222. SPY(J) = 0.0D0
  223. YP(J) = 1.0D0
  224. 40 CONTINUE
  225. DELF = DHVNRM(YP,NEQ)
  226. 50 CONTINUE
  227. C
  228. DFDUB = 0.0D0
  229. LK = MIN(NEQ+1,3)
  230. DO 140 K = 1, LK
  231. C DEFINE PERTURBED VECTOR OF INITIAL VALUES
  232. DO 60 J = 1, NEQ
  233. PV(J) = Y(J) + DELY*(YP(J)/DELF)
  234. 60 CONTINUE
  235. IF (K .EQ. 2) GO TO 80
  236. C EVALUATE DERIVATIVES ASSOCIATED WITH PERTURBED
  237. C VECTOR AND COMPUTE CORRESPONDING DIFFERENCES
  238. CALL DF(A,PV,YP,RPAR,IPAR)
  239. DO 70 J = 1, NEQ
  240. PV(J) = YP(J) - YPRIME(J)
  241. 70 CONTINUE
  242. GO TO 100
  243. 80 CONTINUE
  244. C USE A SHIFTED VALUE OF THE INDEPENDENT VARIABLE
  245. C IN COMPUTING ONE ESTIMATE
  246. CALL DF(A+DA,PV,YP,RPAR,IPAR)
  247. DO 90 J = 1, NEQ
  248. PV(J) = YP(J) - SF(J)
  249. 90 CONTINUE
  250. 100 CONTINUE
  251. C CHOOSE LARGEST BOUNDS ON THE FIRST DERIVATIVE
  252. C AND A LOCAL LIPSCHITZ CONSTANT
  253. FBND = MAX(FBND,DHVNRM(YP,NEQ))
  254. DELF = DHVNRM(PV,NEQ)
  255. C ...EXIT
  256. IF (DELF .GE. BIG*ABS(DELY)) GO TO 150
  257. DFDUB = MAX(DFDUB,DELF/ABS(DELY))
  258. C ......EXIT
  259. IF (K .EQ. LK) GO TO 160
  260. C CHOOSE NEXT PERTURBATION VECTOR
  261. IF (DELF .EQ. 0.0D0) DELF = 1.0D0
  262. DO 130 J = 1, NEQ
  263. IF (K .EQ. 2) GO TO 110
  264. DY = ABS(PV(J))
  265. IF (DY .EQ. 0.0D0) DY = DELF
  266. GO TO 120
  267. 110 CONTINUE
  268. DY = Y(J)
  269. IF (DY .EQ. 0.0D0) DY = DELY/RELPER
  270. 120 CONTINUE
  271. IF (SPY(J) .EQ. 0.0D0) SPY(J) = YP(J)
  272. IF (SPY(J) .NE. 0.0D0) DY = SIGN(DY,SPY(J))
  273. YP(J) = DY
  274. 130 CONTINUE
  275. DELF = DHVNRM(YP,NEQ)
  276. 140 CONTINUE
  277. 150 CONTINUE
  278. C
  279. C PROTECT AGAINST AN OVERFLOW
  280. DFDUB = BIG
  281. 160 CONTINUE
  282. C
  283. C ..................................................................
  284. C
  285. C COMPUTE A BOUND (YDPB) ON THE NORM OF THE SECOND DERIVATIVE
  286. C
  287. YDPB = DFDXB + DFDUB*FBND
  288. C
  289. C ..................................................................
  290. C
  291. C DEFINE THE TOLERANCE PARAMETER UPON WHICH THE STARTING STEP
  292. C SIZE IS TO BE BASED. A VALUE IN THE MIDDLE OF THE ERROR
  293. C TOLERANCE RANGE IS SELECTED.
  294. C
  295. TOLMIN = BIG
  296. TOLSUM = 0.0D0
  297. DO 170 K = 1, NEQ
  298. TOLEXP = LOG10(ETOL(K))
  299. TOLMIN = MIN(TOLMIN,TOLEXP)
  300. TOLSUM = TOLSUM + TOLEXP
  301. 170 CONTINUE
  302. TOLP = 10.0D0**(0.5D0*(TOLSUM/NEQ + TOLMIN)/(MORDER+1))
  303. C
  304. C ..................................................................
  305. C
  306. C COMPUTE A STARTING STEP SIZE BASED ON THE ABOVE FIRST AND
  307. C SECOND DERIVATIVE INFORMATION
  308. C
  309. C RESTRICT THE STEP LENGTH TO BE NOT BIGGER
  310. C THAN ABS(B-A). (UNLESS B IS TOO CLOSE
  311. C TO A)
  312. H = ABSDX
  313. C
  314. IF (YDPB .NE. 0.0D0 .OR. FBND .NE. 0.0D0) GO TO 180
  315. C
  316. C BOTH FIRST DERIVATIVE TERM (FBND) AND SECOND
  317. C DERIVATIVE TERM (YDPB) ARE ZERO
  318. IF (TOLP .LT. 1.0D0) H = ABSDX*TOLP
  319. GO TO 200
  320. 180 CONTINUE
  321. C
  322. IF (YDPB .NE. 0.0D0) GO TO 190
  323. C
  324. C ONLY SECOND DERIVATIVE TERM (YDPB) IS ZERO
  325. IF (TOLP .LT. FBND*ABSDX) H = TOLP/FBND
  326. GO TO 200
  327. 190 CONTINUE
  328. C
  329. C SECOND DERIVATIVE TERM (YDPB) IS NON-ZERO
  330. SRYDPB = SQRT(0.5D0*YDPB)
  331. IF (TOLP .LT. SRYDPB*ABSDX) H = TOLP/SRYDPB
  332. 200 CONTINUE
  333. C
  334. C FURTHER RESTRICT THE STEP LENGTH TO BE NOT
  335. C BIGGER THAN 1/DFDUB
  336. IF (H*DFDUB .GT. 1.0D0) H = 1.0D0/DFDUB
  337. C
  338. C FINALLY, RESTRICT THE STEP LENGTH TO BE NOT
  339. C SMALLER THAN 100*SMALL*ABS(A). HOWEVER, IF
  340. C A=0. AND THE COMPUTED H UNDERFLOWED TO ZERO,
  341. C THE ALGORITHM RETURNS SMALL*ABS(B) FOR THE
  342. C STEP LENGTH.
  343. H = MAX(H,100.0D0*SMALL*ABS(A))
  344. IF (H .EQ. 0.0D0) H = SMALL*ABS(B)
  345. C
  346. C NOW SET DIRECTION OF INTEGRATION
  347. H = SIGN(H,DX)
  348. C
  349. RETURN
  350. END