hstart.f 13 KB

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