dpchic.f 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. *DECK DPCHIC
  2. SUBROUTINE DPCHIC (IC, VC, SWITCH, N, X, F, D, INCFD, WK, NWK,
  3. + IERR)
  4. C***BEGIN PROLOGUE DPCHIC
  5. C***PURPOSE Set derivatives needed to determine a piecewise monotone
  6. C piecewise cubic Hermite interpolant to given data.
  7. C User control is available over boundary conditions and/or
  8. C treatment of points where monotonicity switches direction.
  9. C***LIBRARY SLATEC (PCHIP)
  10. C***CATEGORY E1A
  11. C***TYPE DOUBLE PRECISION (PCHIC-S, DPCHIC-D)
  12. C***KEYWORDS CUBIC HERMITE INTERPOLATION, MONOTONE INTERPOLATION,
  13. C PCHIP, PIECEWISE CUBIC INTERPOLATION,
  14. C SHAPE-PRESERVING INTERPOLATION
  15. C***AUTHOR Fritsch, F. N., (LLNL)
  16. C Lawrence Livermore National Laboratory
  17. C P.O. Box 808 (L-316)
  18. C Livermore, CA 94550
  19. C FTS 532-4275, (510) 422-4275
  20. C***DESCRIPTION
  21. C
  22. C DPCHIC: Piecewise Cubic Hermite Interpolation Coefficients.
  23. C
  24. C Sets derivatives needed to determine a piecewise monotone piece-
  25. C wise cubic interpolant to the data given in X and F satisfying the
  26. C boundary conditions specified by IC and VC.
  27. C
  28. C The treatment of points where monotonicity switches direction is
  29. C controlled by argument SWITCH.
  30. C
  31. C To facilitate two-dimensional applications, includes an increment
  32. C between successive values of the F- and D-arrays.
  33. C
  34. C The resulting piecewise cubic Hermite function may be evaluated
  35. C by DPCHFE or DPCHFD.
  36. C
  37. C ----------------------------------------------------------------------
  38. C
  39. C Calling sequence:
  40. C
  41. C PARAMETER (INCFD = ...)
  42. C INTEGER IC(2), N, NWK, IERR
  43. C DOUBLE PRECISION VC(2), SWITCH, X(N), F(INCFD,N), D(INCFD,N),
  44. C WK(NWK)
  45. C
  46. C CALL DPCHIC (IC, VC, SWITCH, N, X, F, D, INCFD, WK, NWK, IERR)
  47. C
  48. C Parameters:
  49. C
  50. C IC -- (input) integer array of length 2 specifying desired
  51. C boundary conditions:
  52. C IC(1) = IBEG, desired condition at beginning of data.
  53. C IC(2) = IEND, desired condition at end of data.
  54. C
  55. C IBEG = 0 for the default boundary condition (the same as
  56. C used by DPCHIM).
  57. C If IBEG.NE.0, then its sign indicates whether the boundary
  58. C derivative is to be adjusted, if necessary, to be
  59. C compatible with monotonicity:
  60. C IBEG.GT.0 if no adjustment is to be performed.
  61. C IBEG.LT.0 if the derivative is to be adjusted for
  62. C monotonicity.
  63. C
  64. C Allowable values for the magnitude of IBEG are:
  65. C IBEG = 1 if first derivative at X(1) is given in VC(1).
  66. C IBEG = 2 if second derivative at X(1) is given in VC(1).
  67. C IBEG = 3 to use the 3-point difference formula for D(1).
  68. C (Reverts to the default b.c. if N.LT.3 .)
  69. C IBEG = 4 to use the 4-point difference formula for D(1).
  70. C (Reverts to the default b.c. if N.LT.4 .)
  71. C IBEG = 5 to set D(1) so that the second derivative is con-
  72. C tinuous at X(2). (Reverts to the default b.c. if N.LT.4.)
  73. C This option is somewhat analogous to the "not a knot"
  74. C boundary condition provided by DPCHSP.
  75. C
  76. C NOTES (IBEG):
  77. C 1. An error return is taken if ABS(IBEG).GT.5 .
  78. C 2. Only in case IBEG.LE.0 is it guaranteed that the
  79. C interpolant will be monotonic in the first interval.
  80. C If the returned value of D(1) lies between zero and
  81. C 3*SLOPE(1), the interpolant will be monotonic. This
  82. C is **NOT** checked if IBEG.GT.0 .
  83. C 3. If IBEG.LT.0 and D(1) had to be changed to achieve mono-
  84. C tonicity, a warning error is returned.
  85. C
  86. C IEND may take on the same values as IBEG, but applied to
  87. C derivative at X(N). In case IEND = 1 or 2, the value is
  88. C given in VC(2).
  89. C
  90. C NOTES (IEND):
  91. C 1. An error return is taken if ABS(IEND).GT.5 .
  92. C 2. Only in case IEND.LE.0 is it guaranteed that the
  93. C interpolant will be monotonic in the last interval.
  94. C If the returned value of D(1+(N-1)*INCFD) lies between
  95. C zero and 3*SLOPE(N-1), the interpolant will be monotonic.
  96. C This is **NOT** checked if IEND.GT.0 .
  97. C 3. If IEND.LT.0 and D(1+(N-1)*INCFD) had to be changed to
  98. C achieve monotonicity, a warning error is returned.
  99. C
  100. C VC -- (input) real*8 array of length 2 specifying desired boundary
  101. C values, as indicated above.
  102. C VC(1) need be set only if IC(1) = 1 or 2 .
  103. C VC(2) need be set only if IC(2) = 1 or 2 .
  104. C
  105. C SWITCH -- (input) indicates desired treatment of points where
  106. C direction of monotonicity switches:
  107. C Set SWITCH to zero if interpolant is required to be mono-
  108. C tonic in each interval, regardless of monotonicity of data.
  109. C NOTES:
  110. C 1. This will cause D to be set to zero at all switch
  111. C points, thus forcing extrema there.
  112. C 2. The result of using this option with the default boun-
  113. C dary conditions will be identical to using DPCHIM, but
  114. C will generally cost more compute time.
  115. C This option is provided only to facilitate comparison
  116. C of different switch and/or boundary conditions.
  117. C Set SWITCH nonzero to use a formula based on the 3-point
  118. C difference formula in the vicinity of switch points.
  119. C If SWITCH is positive, the interpolant on each interval
  120. C containing an extremum is controlled to not deviate from
  121. C the data by more than SWITCH*DFLOC, where DFLOC is the
  122. C maximum of the change of F on this interval and its two
  123. C immediate neighbors.
  124. C If SWITCH is negative, no such control is to be imposed.
  125. C
  126. C N -- (input) number of data points. (Error return if N.LT.2 .)
  127. C
  128. C X -- (input) real*8 array of independent variable values. The
  129. C elements of X must be strictly increasing:
  130. C X(I-1) .LT. X(I), I = 2(1)N.
  131. C (Error return if not.)
  132. C
  133. C F -- (input) real*8 array of dependent variable values to be
  134. C interpolated. F(1+(I-1)*INCFD) is value corresponding to
  135. C X(I).
  136. C
  137. C D -- (output) real*8 array of derivative values at the data
  138. C points. These values will determine a monotone cubic
  139. C Hermite function on each subinterval on which the data
  140. C are monotonic, except possibly adjacent to switches in
  141. C monotonicity. The value corresponding to X(I) is stored in
  142. C D(1+(I-1)*INCFD), I=1(1)N.
  143. C No other entries in D are changed.
  144. C
  145. C INCFD -- (input) increment between successive values in F and D.
  146. C This argument is provided primarily for 2-D applications.
  147. C (Error return if INCFD.LT.1 .)
  148. C
  149. C WK -- (scratch) real*8 array of working storage. The user may
  150. C wish to know that the returned values are:
  151. C WK(I) = H(I) = X(I+1) - X(I) ;
  152. C WK(N-1+I) = SLOPE(I) = (F(1,I+1) - F(1,I)) / H(I)
  153. C for I = 1(1)N-1.
  154. C
  155. C NWK -- (input) length of work array.
  156. C (Error return if NWK.LT.2*(N-1) .)
  157. C
  158. C IERR -- (output) error flag.
  159. C Normal return:
  160. C IERR = 0 (no errors).
  161. C Warning errors:
  162. C IERR = 1 if IBEG.LT.0 and D(1) had to be adjusted for
  163. C monotonicity.
  164. C IERR = 2 if IEND.LT.0 and D(1+(N-1)*INCFD) had to be
  165. C adjusted for monotonicity.
  166. C IERR = 3 if both of the above are true.
  167. C "Recoverable" errors:
  168. C IERR = -1 if N.LT.2 .
  169. C IERR = -2 if INCFD.LT.1 .
  170. C IERR = -3 if the X-array is not strictly increasing.
  171. C IERR = -4 if ABS(IBEG).GT.5 .
  172. C IERR = -5 if ABS(IEND).GT.5 .
  173. C IERR = -6 if both of the above are true.
  174. C IERR = -7 if NWK.LT.2*(N-1) .
  175. C (The D-array has not been changed in any of these cases.)
  176. C NOTE: The above errors are checked in the order listed,
  177. C and following arguments have **NOT** been validated.
  178. C
  179. C***REFERENCES 1. F. N. Fritsch, Piecewise Cubic Hermite Interpolation
  180. C Package, Report UCRL-87285, Lawrence Livermore Natio-
  181. C nal Laboratory, July 1982. [Poster presented at the
  182. C SIAM 30th Anniversary Meeting, 19-23 July 1982.]
  183. C 2. F. N. Fritsch and J. Butland, A method for construc-
  184. C ting local monotone piecewise cubic interpolants, SIAM
  185. C Journal on Scientific and Statistical Computing 5, 2
  186. C (June 1984), pp. 300-304.
  187. C 3. F. N. Fritsch and R. E. Carlson, Monotone piecewise
  188. C cubic interpolation, SIAM Journal on Numerical Ana-
  189. C lysis 17, 2 (April 1980), pp. 238-246.
  190. C***ROUTINES CALLED DPCHCE, DPCHCI, DPCHCS, XERMSG
  191. C***REVISION HISTORY (YYMMDD)
  192. C 820218 DATE WRITTEN
  193. C 820804 Converted to SLATEC library version.
  194. C 870707 Corrected XERROR calls for d.p. name(s).
  195. C 870813 Updated Reference 2.
  196. C 890206 Corrected XERROR calls.
  197. C 890411 Added SAVE statements (Vers. 3.2).
  198. C 890531 Changed all specific intrinsics to generic. (WRB)
  199. C 890703 Corrected category record. (WRB)
  200. C 890831 Modified array declarations. (WRB)
  201. C 891006 Cosmetic changes to prologue. (WRB)
  202. C 891006 REVISION DATE from Version 3.2
  203. C 891214 Prologue converted to Version 4.0 format. (BAB)
  204. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  205. C 920429 Revised format and order of references. (WRB,FNF)
  206. C***END PROLOGUE DPCHIC
  207. C Programming notes:
  208. C
  209. C To produce a single precision version, simply:
  210. C a. Change DPCHIC to PCHIC wherever it occurs,
  211. C b. Change DPCHCE to PCHCE wherever it occurs,
  212. C c. Change DPCHCI to PCHCI wherever it occurs,
  213. C d. Change DPCHCS to PCHCS wherever it occurs,
  214. C e. Change the double precision declarations to real, and
  215. C f. Change the constant ZERO to single precision.
  216. C
  217. C DECLARE ARGUMENTS.
  218. C
  219. INTEGER IC(2), N, INCFD, NWK, IERR
  220. DOUBLE PRECISION VC(2), SWITCH, X(*), F(INCFD,*), D(INCFD,*),
  221. * WK(NWK)
  222. C
  223. C DECLARE LOCAL VARIABLES.
  224. C
  225. INTEGER I, IBEG, IEND, NLESS1
  226. DOUBLE PRECISION ZERO
  227. SAVE ZERO
  228. DATA ZERO /0.D0/
  229. C
  230. C VALIDITY-CHECK ARGUMENTS.
  231. C
  232. C***FIRST EXECUTABLE STATEMENT DPCHIC
  233. IF ( N.LT.2 ) GO TO 5001
  234. IF ( INCFD.LT.1 ) GO TO 5002
  235. DO 1 I = 2, N
  236. IF ( X(I).LE.X(I-1) ) GO TO 5003
  237. 1 CONTINUE
  238. C
  239. IBEG = IC(1)
  240. IEND = IC(2)
  241. IERR = 0
  242. IF (ABS(IBEG) .GT. 5) IERR = IERR - 1
  243. IF (ABS(IEND) .GT. 5) IERR = IERR - 2
  244. IF (IERR .LT. 0) GO TO 5004
  245. C
  246. C FUNCTION DEFINITION IS OK -- GO ON.
  247. C
  248. NLESS1 = N - 1
  249. IF ( NWK .LT. 2*NLESS1 ) GO TO 5007
  250. C
  251. C SET UP H AND SLOPE ARRAYS.
  252. C
  253. DO 20 I = 1, NLESS1
  254. WK(I) = X(I+1) - X(I)
  255. WK(NLESS1+I) = (F(1,I+1) - F(1,I)) / WK(I)
  256. 20 CONTINUE
  257. C
  258. C SPECIAL CASE N=2 -- USE LINEAR INTERPOLATION.
  259. C
  260. IF (NLESS1 .GT. 1) GO TO 1000
  261. D(1,1) = WK(2)
  262. D(1,N) = WK(2)
  263. GO TO 3000
  264. C
  265. C NORMAL CASE (N .GE. 3) .
  266. C
  267. 1000 CONTINUE
  268. C
  269. C SET INTERIOR DERIVATIVES AND DEFAULT END CONDITIONS.
  270. C
  271. C --------------------------------------
  272. CALL DPCHCI (N, WK(1), WK(N), D, INCFD)
  273. C --------------------------------------
  274. C
  275. C SET DERIVATIVES AT POINTS WHERE MONOTONICITY SWITCHES DIRECTION.
  276. C
  277. IF (SWITCH .EQ. ZERO) GO TO 3000
  278. C ----------------------------------------------------
  279. CALL DPCHCS (SWITCH, N, WK(1), WK(N), D, INCFD, IERR)
  280. C ----------------------------------------------------
  281. IF (IERR .NE. 0) GO TO 5008
  282. C
  283. C SET END CONDITIONS.
  284. C
  285. 3000 CONTINUE
  286. IF ( (IBEG.EQ.0) .AND. (IEND.EQ.0) ) GO TO 5000
  287. C -------------------------------------------------------
  288. CALL DPCHCE (IC, VC, N, X, WK(1), WK(N), D, INCFD, IERR)
  289. C -------------------------------------------------------
  290. IF (IERR .LT. 0) GO TO 5009
  291. C
  292. C NORMAL RETURN.
  293. C
  294. 5000 CONTINUE
  295. RETURN
  296. C
  297. C ERROR RETURNS.
  298. C
  299. 5001 CONTINUE
  300. C N.LT.2 RETURN.
  301. IERR = -1
  302. CALL XERMSG ('SLATEC', 'DPCHIC',
  303. + 'NUMBER OF DATA POINTS LESS THAN TWO', IERR, 1)
  304. RETURN
  305. C
  306. 5002 CONTINUE
  307. C INCFD.LT.1 RETURN.
  308. IERR = -2
  309. CALL XERMSG ('SLATEC', 'DPCHIC', 'INCREMENT LESS THAN ONE', IERR,
  310. + 1)
  311. RETURN
  312. C
  313. 5003 CONTINUE
  314. C X-ARRAY NOT STRICTLY INCREASING.
  315. IERR = -3
  316. CALL XERMSG ('SLATEC', 'DPCHIC',
  317. + 'X-ARRAY NOT STRICTLY INCREASING', IERR, 1)
  318. RETURN
  319. C
  320. 5004 CONTINUE
  321. C IC OUT OF RANGE RETURN.
  322. IERR = IERR - 3
  323. CALL XERMSG ('SLATEC', 'DPCHIC', 'IC OUT OF RANGE', IERR, 1)
  324. RETURN
  325. C
  326. 5007 CONTINUE
  327. C NWK .LT. 2*(N-1) RETURN.
  328. IERR = -7
  329. CALL XERMSG ('SLATEC', 'DPCHIC', 'WORK ARRAY TOO SMALL', IERR, 1)
  330. RETURN
  331. C
  332. 5008 CONTINUE
  333. C ERROR RETURN FROM DPCHCS.
  334. IERR = -8
  335. CALL XERMSG ('SLATEC', 'DPCHIC', 'ERROR RETURN FROM DPCHCS',
  336. + IERR, 1)
  337. RETURN
  338. C
  339. 5009 CONTINUE
  340. C ERROR RETURN FROM DPCHCE.
  341. C *** THIS CASE SHOULD NEVER OCCUR ***
  342. IERR = -9
  343. CALL XERMSG ('SLATEC', 'DPCHIC', 'ERROR RETURN FROM DPCHCE',
  344. + IERR, 1)
  345. RETURN
  346. C------------- LAST LINE OF DPCHIC FOLLOWS -----------------------------
  347. END