pchfd.f 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. *DECK PCHFD
  2. SUBROUTINE PCHFD (N, X, F, D, INCFD, SKIP, NE, XE, FE, DE, IERR)
  3. C***BEGIN PROLOGUE PCHFD
  4. C***PURPOSE Evaluate a piecewise cubic Hermite function and its first
  5. C derivative at an array of points. May be used by itself
  6. C for Hermite interpolation, or as an evaluator for PCHIM
  7. C or PCHIC. If only function values are required, use
  8. C PCHFE instead.
  9. C***LIBRARY SLATEC (PCHIP)
  10. C***CATEGORY E3, H1
  11. C***TYPE SINGLE PRECISION (PCHFD-S, DPCHFD-D)
  12. C***KEYWORDS CUBIC HERMITE DIFFERENTIATION, CUBIC HERMITE EVALUATION,
  13. C HERMITE INTERPOLATION, PCHIP, PIECEWISE CUBIC EVALUATION
  14. C***AUTHOR Fritsch, F. N., (LLNL)
  15. C Lawrence Livermore National Laboratory
  16. C P.O. Box 808 (L-316)
  17. C Livermore, CA 94550
  18. C FTS 532-4275, (510) 422-4275
  19. C***DESCRIPTION
  20. C
  21. C PCHFD: Piecewise Cubic Hermite Function and Derivative
  22. C evaluator
  23. C
  24. C Evaluates the cubic Hermite function defined by N, X, F, D, to-
  25. C gether with its first derivative, at the points XE(J), J=1(1)NE.
  26. C
  27. C If only function values are required, use PCHFE, instead.
  28. C
  29. C To provide compatibility with PCHIM and PCHIC, includes an
  30. C increment between successive values of the F- and D-arrays.
  31. C
  32. C ----------------------------------------------------------------------
  33. C
  34. C Calling sequence:
  35. C
  36. C PARAMETER (INCFD = ...)
  37. C INTEGER N, NE, IERR
  38. C REAL X(N), F(INCFD,N), D(INCFD,N), XE(NE), FE(NE), DE(NE)
  39. C LOGICAL SKIP
  40. C
  41. C CALL PCHFD (N, X, F, D, INCFD, SKIP, NE, XE, FE, DE, IERR)
  42. C
  43. C Parameters:
  44. C
  45. C N -- (input) number of data points. (Error return if N.LT.2 .)
  46. C
  47. C X -- (input) real array of independent variable values. The
  48. C elements of X must be strictly increasing:
  49. C X(I-1) .LT. X(I), I = 2(1)N.
  50. C (Error return if not.)
  51. C
  52. C F -- (input) real array of function values. F(1+(I-1)*INCFD) is
  53. C the value corresponding to X(I).
  54. C
  55. C D -- (input) real array of derivative values. D(1+(I-1)*INCFD) is
  56. C the value corresponding to X(I).
  57. C
  58. C INCFD -- (input) increment between successive values in F and D.
  59. C (Error return if INCFD.LT.1 .)
  60. C
  61. C SKIP -- (input/output) logical variable which should be set to
  62. C .TRUE. if the user wishes to skip checks for validity of
  63. C preceding parameters, or to .FALSE. otherwise.
  64. C This will save time in case these checks have already
  65. C been performed (say, in PCHIM or PCHIC).
  66. C SKIP will be set to .TRUE. on normal return.
  67. C
  68. C NE -- (input) number of evaluation points. (Error return if
  69. C NE.LT.1 .)
  70. C
  71. C XE -- (input) real array of points at which the functions are to
  72. C be evaluated.
  73. C
  74. C
  75. C NOTES:
  76. C 1. The evaluation will be most efficient if the elements
  77. C of XE are increasing relative to X;
  78. C that is, XE(J) .GE. X(I)
  79. C implies XE(K) .GE. X(I), all K.GE.J .
  80. C 2. If any of the XE are outside the interval [X(1),X(N)],
  81. C values are extrapolated from the nearest extreme cubic,
  82. C and a warning error is returned.
  83. C
  84. C FE -- (output) real array of values of the cubic Hermite function
  85. C defined by N, X, F, D at the points XE.
  86. C
  87. C DE -- (output) real array of values of the first derivative of
  88. C the same function at the points XE.
  89. C
  90. C IERR -- (output) error flag.
  91. C Normal return:
  92. C IERR = 0 (no errors).
  93. C Warning error:
  94. C IERR.GT.0 means that extrapolation was performed at
  95. C IERR points.
  96. C "Recoverable" errors:
  97. C IERR = -1 if N.LT.2 .
  98. C IERR = -2 if INCFD.LT.1 .
  99. C IERR = -3 if the X-array is not strictly increasing.
  100. C IERR = -4 if NE.LT.1 .
  101. C (Output arrays have not been changed in any of these cases.)
  102. C NOTE: The above errors are checked in the order listed,
  103. C and following arguments have **NOT** been validated.
  104. C IERR = -5 if an error has occurred in the lower-level
  105. C routine CHFDV. NB: this should never happen.
  106. C Notify the author **IMMEDIATELY** if it does.
  107. C
  108. C***REFERENCES (NONE)
  109. C***ROUTINES CALLED CHFDV, XERMSG
  110. C***REVISION HISTORY (YYMMDD)
  111. C 811020 DATE WRITTEN
  112. C 820803 Minor cosmetic changes for release 1.
  113. C 870707 Minor cosmetic changes to prologue.
  114. C 890531 Changed all specific intrinsics to generic. (WRB)
  115. C 890831 Modified array declarations. (WRB)
  116. C 890831 REVISION DATE from Version 3.2
  117. C 891214 Prologue converted to Version 4.0 format. (BAB)
  118. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  119. C***END PROLOGUE PCHFD
  120. C Programming notes:
  121. C
  122. C 1. To produce a double precision version, simply:
  123. C a. Change PCHFD to DPCHFD, and CHFDV to DCHFDV, wherever they
  124. C occur,
  125. C b. Change the real declaration to double precision,
  126. C
  127. C 2. Most of the coding between the call to CHFDV and the end of
  128. C the IR-loop could be eliminated if it were permissible to
  129. C assume that XE is ordered relative to X.
  130. C
  131. C 3. CHFDV does not assume that X1 is less than X2. thus, it would
  132. C be possible to write a version of PCHFD that assumes a strict-
  133. C ly decreasing X-array by simply running the IR-loop backwards
  134. C (and reversing the order of appropriate tests).
  135. C
  136. C 4. The present code has a minor bug, which I have decided is not
  137. C worth the effort that would be required to fix it.
  138. C If XE contains points in [X(N-1),X(N)], followed by points .LT.
  139. C X(N-1), followed by points .GT.X(N), the extrapolation points
  140. C will be counted (at least) twice in the total returned in IERR.
  141. C
  142. C DECLARE ARGUMENTS.
  143. C
  144. INTEGER N, INCFD, NE, IERR
  145. REAL X(*), F(INCFD,*), D(INCFD,*), XE(*), FE(*), DE(*)
  146. LOGICAL SKIP
  147. C
  148. C DECLARE LOCAL VARIABLES.
  149. C
  150. INTEGER I, IERC, IR, J, JFIRST, NEXT(2), NJ
  151. C
  152. C VALIDITY-CHECK ARGUMENTS.
  153. C
  154. C***FIRST EXECUTABLE STATEMENT PCHFD
  155. IF (SKIP) GO TO 5
  156. C
  157. IF ( N.LT.2 ) GO TO 5001
  158. IF ( INCFD.LT.1 ) GO TO 5002
  159. DO 1 I = 2, N
  160. IF ( X(I).LE.X(I-1) ) GO TO 5003
  161. 1 CONTINUE
  162. C
  163. C FUNCTION DEFINITION IS OK, GO ON.
  164. C
  165. 5 CONTINUE
  166. IF ( NE.LT.1 ) GO TO 5004
  167. IERR = 0
  168. SKIP = .TRUE.
  169. C
  170. C LOOP OVER INTERVALS. ( INTERVAL INDEX IS IL = IR-1 . )
  171. C ( INTERVAL IS X(IL).LE.X.LT.X(IR) . )
  172. JFIRST = 1
  173. IR = 2
  174. 10 CONTINUE
  175. C
  176. C SKIP OUT OF LOOP IF HAVE PROCESSED ALL EVALUATION POINTS.
  177. C
  178. IF (JFIRST .GT. NE) GO TO 5000
  179. C
  180. C LOCATE ALL POINTS IN INTERVAL.
  181. C
  182. DO 20 J = JFIRST, NE
  183. IF (XE(J) .GE. X(IR)) GO TO 30
  184. 20 CONTINUE
  185. J = NE + 1
  186. GO TO 40
  187. C
  188. C HAVE LOCATED FIRST POINT BEYOND INTERVAL.
  189. C
  190. 30 CONTINUE
  191. IF (IR .EQ. N) J = NE + 1
  192. C
  193. 40 CONTINUE
  194. NJ = J - JFIRST
  195. C
  196. C SKIP EVALUATION IF NO POINTS IN INTERVAL.
  197. C
  198. IF (NJ .EQ. 0) GO TO 50
  199. C
  200. C EVALUATE CUBIC AT XE(I), I = JFIRST (1) J-1 .
  201. C
  202. C ----------------------------------------------------------------
  203. CALL CHFDV (X(IR-1),X(IR), F(1,IR-1),F(1,IR), D(1,IR-1),D(1,IR),
  204. * NJ, XE(JFIRST), FE(JFIRST), DE(JFIRST), NEXT, IERC)
  205. C ----------------------------------------------------------------
  206. IF (IERC .LT. 0) GO TO 5005
  207. C
  208. IF (NEXT(2) .EQ. 0) GO TO 42
  209. C IF (NEXT(2) .GT. 0) THEN
  210. C IN THE CURRENT SET OF XE-POINTS, THERE ARE NEXT(2) TO THE
  211. C RIGHT OF X(IR).
  212. C
  213. IF (IR .LT. N) GO TO 41
  214. C IF (IR .EQ. N) THEN
  215. C THESE ARE ACTUALLY EXTRAPOLATION POINTS.
  216. IERR = IERR + NEXT(2)
  217. GO TO 42
  218. 41 CONTINUE
  219. C ELSE
  220. C WE SHOULD NEVER HAVE GOTTEN HERE.
  221. GO TO 5005
  222. C ENDIF
  223. C ENDIF
  224. 42 CONTINUE
  225. C
  226. IF (NEXT(1) .EQ. 0) GO TO 49
  227. C IF (NEXT(1) .GT. 0) THEN
  228. C IN THE CURRENT SET OF XE-POINTS, THERE ARE NEXT(1) TO THE
  229. C LEFT OF X(IR-1).
  230. C
  231. IF (IR .GT. 2) GO TO 43
  232. C IF (IR .EQ. 2) THEN
  233. C THESE ARE ACTUALLY EXTRAPOLATION POINTS.
  234. IERR = IERR + NEXT(1)
  235. GO TO 49
  236. 43 CONTINUE
  237. C ELSE
  238. C XE IS NOT ORDERED RELATIVE TO X, SO MUST ADJUST
  239. C EVALUATION INTERVAL.
  240. C
  241. C FIRST, LOCATE FIRST POINT TO LEFT OF X(IR-1).
  242. DO 44 I = JFIRST, J-1
  243. IF (XE(I) .LT. X(IR-1)) GO TO 45
  244. 44 CONTINUE
  245. C NOTE-- CANNOT DROP THROUGH HERE UNLESS THERE IS AN ERROR
  246. C IN CHFDV.
  247. GO TO 5005
  248. C
  249. 45 CONTINUE
  250. C RESET J. (THIS WILL BE THE NEW JFIRST.)
  251. J = I
  252. C
  253. C NOW FIND OUT HOW FAR TO BACK UP IN THE X-ARRAY.
  254. DO 46 I = 1, IR-1
  255. IF (XE(J) .LT. X(I)) GO TO 47
  256. 46 CONTINUE
  257. C NB-- CAN NEVER DROP THROUGH HERE, SINCE XE(J).LT.X(IR-1).
  258. C
  259. 47 CONTINUE
  260. C AT THIS POINT, EITHER XE(J) .LT. X(1)
  261. C OR X(I-1) .LE. XE(J) .LT. X(I) .
  262. C RESET IR, RECOGNIZING THAT IT WILL BE INCREMENTED BEFORE
  263. C CYCLING.
  264. IR = MAX(1, I-1)
  265. C ENDIF
  266. C ENDIF
  267. 49 CONTINUE
  268. C
  269. JFIRST = J
  270. C
  271. C END OF IR-LOOP.
  272. C
  273. 50 CONTINUE
  274. IR = IR + 1
  275. IF (IR .LE. N) GO TO 10
  276. C
  277. C NORMAL RETURN.
  278. C
  279. 5000 CONTINUE
  280. RETURN
  281. C
  282. C ERROR RETURNS.
  283. C
  284. 5001 CONTINUE
  285. C N.LT.2 RETURN.
  286. IERR = -1
  287. CALL XERMSG ('SLATEC', 'PCHFD',
  288. + 'NUMBER OF DATA POINTS LESS THAN TWO', IERR, 1)
  289. RETURN
  290. C
  291. 5002 CONTINUE
  292. C INCFD.LT.1 RETURN.
  293. IERR = -2
  294. CALL XERMSG ('SLATEC', 'PCHFD', 'INCREMENT LESS THAN ONE', IERR,
  295. + 1)
  296. RETURN
  297. C
  298. 5003 CONTINUE
  299. C X-ARRAY NOT STRICTLY INCREASING.
  300. IERR = -3
  301. CALL XERMSG ('SLATEC', 'PCHFD', 'X-ARRAY NOT STRICTLY INCREASING'
  302. + , IERR, 1)
  303. RETURN
  304. C
  305. 5004 CONTINUE
  306. C NE.LT.1 RETURN.
  307. IERR = -4
  308. CALL XERMSG ('SLATEC', 'PCHFD',
  309. + 'NUMBER OF EVALUATION POINTS LESS THAN ONE', IERR, 1)
  310. RETURN
  311. C
  312. 5005 CONTINUE
  313. C ERROR RETURN FROM CHFDV.
  314. C *** THIS CASE SHOULD NEVER OCCUR ***
  315. IERR = -5
  316. CALL XERMSG ('SLATEC', 'PCHFD',
  317. + 'ERROR RETURN FROM CHFDV -- FATAL', IERR, 2)
  318. RETURN
  319. C------------- LAST LINE OF PCHFD FOLLOWS ------------------------------
  320. END