source: LMDZ6/branches/Amaury_dev/libf/misc/pchsp.f90 @ 5110

Last change on this file since 5110 was 5106, checked in by abarral, 4 months ago

Turn coefils.h into lmdz_coefils.f90
Put filtreg.F90 inside lmdz_filtreg.F90
Turn mod_filtreg_p.F90 into lmdz_filtreg_p.F90
Delete obsolete parafilt.h*
(lint) remove spaces between routine name and args

  • Property copyright set to
    Name of program: LMDZ
    Creation date: 1984
    Version: LMDZ5
    License: CeCILL version 2
    Holder: Laboratoire de m\'et\'eorologie dynamique, CNRS, UMR 8539
    See the license file in the root directory
File size: 13.0 KB
Line 
1!DECK PCHSP
2SUBROUTINE PCHSP(IC, VC, N, X, F, D, INCFD, WK, NWK, IERR)
3  !***BEGIN PROLOGUE  PCHSP
4  !***PURPOSE  Set derivatives needed to determine the Hermite represen-
5         ! tation of the cubic spline interpolant to given data, with
6         ! specified boundary conditions.
7  !***LIBRARY   SLATEC (PCHIP)
8  !***CATEGORY  E1A
9  !***TYPE      SINGLE PRECISION (PCHSP-S, DPCHSP-D)
10  !***KEYWORDS  CUBIC HERMITE INTERPOLATION, PCHIP,
11         !  PIECEWISE CUBIC INTERPOLATION, SPLINE INTERPOLATION
12  !***AUTHOR  Fritsch, F. N., (LLNL)
13         !  Lawrence Livermore National Laboratory
14         !  P.O. Box 808  (L-316)
15         !  Livermore, CA  94550
16         !  FTS 532-4275, (510) 422-4275
17  !***DESCRIPTION
18  !
19  !      PCHSP:   Piecewise Cubic Hermite Spline
20  !
21  ! Computes the Hermite representation of the cubic spline inter-
22  ! polant to the data given in X and F satisfying the boundary
23  ! conditions specified by IC and VC.
24  !
25  ! To facilitate two-dimensional applications, includes an increment
26  ! between successive values of the F- and D-arrays.
27  !
28  ! The resulting piecewise cubic Hermite function may be evaluated
29  ! by PCHFE or PCHFD.
30  !
31  ! NOTE:  This is a modified version of C. de Boor's cubic spline
32  !        routine CUBSPL.
33  !
34  ! ----------------------------------------------------------------------
35  !
36  !  Calling sequence:
37  !
38  !    PARAMETER  (INCFD = ...)
39  !    INTEGER  IC(2), N, NWK, IERR
40  !    REAL  VC(2), X(N), F(INCFD,N), D(INCFD,N), WK(NWK)
41  !
42  !    CALL  PCHSP (IC, VC, N, X, F, D, INCFD, WK, NWK, IERR)
43  !
44  !   Parameters:
45  !
46  ! IC -- (input) integer array of length 2 specifying desired
47  !       boundary conditions:
48  !       IC(1) = IBEG, desired condition at beginning of data.
49  !       IC(2) = IEND, desired condition at end of data.
50  !
51  !       IBEG = 0  to set D(1) so that the third derivative is con-
52  !          tinuous at X(2).  This is the "not a knot" condition
53  !          provided by de Boor's cubic spline routine CUBSPL.
54  !          < This is the default boundary condition. >
55  !       IBEG = 1  if first derivative at X(1) is given in VC(1).
56  !       IBEG = 2  if second derivative at X(1) is given in VC(1).
57  !       IBEG = 3  to use the 3-point difference formula for D(1).
58  !                 (Reverts to the default b.c. if N.LT.3 .)
59  !       IBEG = 4  to use the 4-point difference formula for D(1).
60  !                 (Reverts to the default b.c. if N.LT.4 .)
61  !      NOTES:
62  !       1. An error return is taken if IBEG is out of range.
63  !       2. For the "natural" boundary condition, use IBEG=2 and
64  !          VC(1)=0.
65  !
66  !       IEND may take on the same values as IBEG, but applied to
67  !       derivative at X(N).  In case IEND = 1 or 2, the value is
68  !       given in VC(2).
69  !
70  !      NOTES:
71  !       1. An error return is taken if IEND is out of range.
72  !       2. For the "natural" boundary condition, use IEND=2 and
73  !          VC(2)=0.
74  !
75  ! VC -- (input) real array of length 2 specifying desired boundary
76  !       values, as indicated above.
77  !       VC(1) need be set only if IC(1) = 1 or 2 .
78  !       VC(2) need be set only if IC(2) = 1 or 2 .
79  !
80  ! N -- (input) number of data points.  (Error return if N.LT.2 .)
81  !
82  ! X -- (input) real array of independent variable values.  The
83  !       elements of X must be strictly increasing:
84  !            X(I-1) .LT. X(I),  I = 2(1)N.
85  !       (Error return if not.)
86  !
87  ! F -- (input) real array of dependent variable values to be inter-
88  !       polated.  F(1+(I-1)*INCFD) is value corresponding to X(I).
89  !
90  ! D -- (output) real array of derivative values at the data points.
91  !       These values will determine the cubic spline interpolant
92  !       with the requested boundary conditions.
93  !       The value corresponding to X(I) is stored in
94  !            D(1+(I-1)*INCFD),  I=1(1)N.
95  !       No other entries in D are changed.
96  !
97  ! INCFD -- (input) increment between successive values in F and D.
98  !       This argument is provided primarily for 2-D applications.
99  !       (Error return if  INCFD.LT.1 .)
100  !
101  ! WK -- (scratch) real array of working storage.
102  !
103  ! NWK -- (input) length of work array.
104  !       (Error return if NWK.LT.2*N .)
105  !
106  ! IERR -- (output) error flag.
107  !       Normal return:
108  !          IERR = 0  (no errors).
109  !       "Recoverable" errors:
110  !          IERR = -1  if N.LT.2 .
111  !          IERR = -2  if INCFD.LT.1 .
112  !          IERR = -3  if the X-array is not strictly increasing.
113  !          IERR = -4  if IBEG.LT.0 or IBEG.GT.4 .
114  !          IERR = -5  if IEND.LT.0 of IEND.GT.4 .
115  !          IERR = -6  if both of the above are true.
116  !          IERR = -7  if NWK is too small.
117  !           NOTE:  The above errors are checked in the order listed,
118  !               and following arguments have **NOT** been validated.
119  !         (The D-array has not been changed in any of these cases.)
120  !          IERR = -8  in case of trouble solving the linear system
121  !                     for the interior derivative values.
122  !         (The D-array may have been changed in this case.)
123  !         (             Do **NOT** use it!                )
124  !
125  !***REFERENCES  Carl de Boor, A Practical Guide to Splines, Springer-
126  !             Verlag, New York, 1978, pp. 53-59.
127  !***ROUTINES CALLED  PCHDF, XERMSG
128  !***REVISION HISTORY  (YYMMDD)
129  !   820503  DATE WRITTEN
130  !   820804  Converted to SLATEC library version.
131  !   870707  Minor cosmetic changes to prologue.
132  !   890411  Added SAVE statements (Vers. 3.2).
133  !   890703  Corrected category record.  (WRB)
134  !   890831  Modified array declarations.  (WRB)
135  !   890831  REVISION DATE from Version 3.2
136  !   891214  Prologue converted to Version 4.0 format.  (BAB)
137  !   900315  CALLs to XERROR changed to CALLs to XERMSG.  (THJ)
138  !   920429  Revised format and order of references.  (WRB,FNF)
139  !***END PROLOGUE  PCHSP
140  !  Programming notes:
141  !
142  ! To produce a double precision version, simply:
143  !    a. Change PCHSP to DPCHSP wherever it occurs,
144  !    b. Change the real declarations to double precision, and
145  !    c. Change the constants ZERO, HALF, ... to double precision.
146  !
147  !  DECLARE ARGUMENTS.
148  !
149  INTEGER :: IC(2), N, INCFD, NWK, IERR
150  REAL :: VC(2), X(*), F(INCFD,*), D(INCFD,*), WK(2,*)
151  !
152  !  DECLARE LOCAL VARIABLES.
153  !
154  INTEGER :: IBEG, IEND, INDEX, J, NM1
155  REAL :: G, HALF, ONE, STEMP(3), THREE, TWO, XTEMP(4), ZERO
156  SAVE ZERO, HALF, ONE, TWO, THREE
157  REAL :: PCHDF
158  !
159  DATA  ZERO /0./,  HALF /0.5/,  ONE /1./,  TWO /2./,  THREE /3./
160  !
161  !  VALIDITY-CHECK ARGUMENTS.
162  !
163  !***FIRST EXECUTABLE STATEMENT  PCHSP
164  IF ( N<2 )  GO TO 5001
165  IF ( INCFD<1 )  GO TO 5002
166  DO J = 2, N
167     IF ( X(J)<=X(J-1) )  GO TO 5003
168  END DO
169  !
170  IBEG = IC(1)
171  IEND = IC(2)
172  IERR = 0
173  IF ( (IBEG<0).OR.(IBEG>4) )  IERR = IERR - 1
174  IF ( (IEND<0).OR.(IEND>4) )  IERR = IERR - 2
175  IF ( IERR<0 )  GO TO 5004
176  !
177  !  FUNCTION DEFINITION IS OK -- GO ON.
178  !
179  IF ( NWK < 2*N )  GO TO 5007
180  !
181  !  COMPUTE FIRST DIFFERENCES OF X SEQUENCE AND STORE IN WK(1,.). ALSO,
182  !  COMPUTE FIRST DIVIDED DIFFERENCE OF DATA AND STORE IN WK(2,.).
183  DO J=2,N
184     WK(1,J) = X(J) - X(J-1)
185     WK(2,J) = (F(1,J) - F(1,J-1))/WK(1,J)
186  END DO
187  !
188  !  SET TO DEFAULT BOUNDARY CONDITIONS IF N IS TOO SMALL.
189  !
190  IF ( IBEG>N )  IBEG = 0
191  IF ( IEND>N )  IEND = 0
192  !
193  !  SET UP FOR BOUNDARY CONDITIONS.
194  !
195  IF ( (IBEG==1).OR.(IBEG==2) )  THEN
196     D(1,1) = VC(1)
197  ELSE IF (IBEG > 2)  THEN
198     ! PICK UP FIRST IBEG POINTS, IN REVERSE ORDER.
199     DO J = 1, IBEG
200        INDEX = IBEG-J+1
201        ! INDEX RUNS FROM IBEG DOWN TO 1.
202        XTEMP(J) = X(INDEX)
203        IF (J < IBEG)  STEMP(J) = WK(2,INDEX)
204  END DO
205              ! --------------------------------
206     D(1,1) = PCHDF (IBEG, XTEMP, STEMP, IERR)
207              ! --------------------------------
208     IF (IERR /= 0)  GO TO 5009
209     IBEG = 1
210  ENDIF
211  !
212  IF ( (IEND==1).OR.(IEND==2) )  THEN
213     D(1,N) = VC(2)
214  ELSE IF (IEND > 2)  THEN
215     ! PICK UP LAST IEND POINTS.
216     DO J = 1, IEND
217        INDEX = N-IEND+J
218        ! INDEX RUNS FROM N+1-IEND UP TO N.
219        XTEMP(J) = X(INDEX)
220        IF (J < IEND)  STEMP(J) = WK(2,INDEX+1)
221  END DO
222              ! --------------------------------
223     D(1,N) = PCHDF (IEND, XTEMP, STEMP, IERR)
224              ! --------------------------------
225     IF (IERR /= 0)  GO TO 5009
226     IEND = 1
227  ENDIF
228  !
229  ! --------------------( BEGIN CODING FROM CUBSPL )--------------------
230  !
231  !  **** A TRIDIAGONAL LINEAR SYSTEM FOR THE UNKNOWN SLOPES S(J) OF
232  !  F  AT X(J), J=1,...,N, IS GENERATED AND THEN SOLVED BY GAUSS ELIM-
233  !  INATION, WITH S(J) ENDING UP IN D(1,J), ALL J.
234  ! WK(1,.) AND WK(2,.) ARE USED FOR TEMPORARY STORAGE.
235  !
236  !  CONSTRUCT FIRST EQUATION FROM FIRST BOUNDARY CONDITION, OF THE FORM
237  !         WK(2,1)*S(1) + WK(1,1)*S(2) = D(1,1)
238  !
239  IF (IBEG == 0)  THEN
240     IF (N == 2)  THEN
241        ! NO CONDITION AT LEFT END AND N = 2.
242        WK(2,1) = ONE
243        WK(1,1) = ONE
244        D(1,1) = TWO*WK(2,2)
245     ELSE
246        ! NOT-A-KNOT CONDITION AT LEFT END AND N .GT. 2.
247        WK(2,1) = WK(1,3)
248        WK(1,1) = WK(1,2) + WK(1,3)
249        D(1,1) =((WK(1,2) + TWO*WK(1,1))*WK(2,2)*WK(1,3) &
250              + WK(1,2)**2*WK(2,3)) / WK(1,1)
251     ENDIF
252  ELSE IF (IBEG == 1)  THEN
253     ! SLOPE PRESCRIBED AT LEFT END.
254     WK(2,1) = ONE
255     WK(1,1) = ZERO
256  ELSE
257     ! SECOND DERIVATIVE PRESCRIBED AT LEFT END.
258     WK(2,1) = TWO
259     WK(1,1) = ONE
260     D(1,1) = THREE*WK(2,2) - HALF*WK(1,2)*D(1,1)
261  ENDIF
262  !
263  !  IF THERE ARE INTERIOR KNOTS, GENERATE THE CORRESPONDING EQUATIONS AND
264  !  CARRY OUT THE FORWARD PASS OF GAUSS ELIMINATION, AFTER WHICH THE J-TH
265  !  EQUATION READS    WK(2,J)*S(J) + WK(1,J)*S(J+1) = D(1,J).
266  !
267  NM1 = N-1
268  IF (NM1 > 1)  THEN
269     DO J=2,NM1
270        IF (WK(2,J-1) == ZERO)  GO TO 5008
271        G = -WK(1,J+1)/WK(2,J-1)
272        D(1,J) = G*D(1,J-1) &
273              + THREE*(WK(1,J)*WK(2,J+1) + WK(1,J+1)*WK(2,J))
274        WK(2,J) = G*WK(1,J-1) + TWO*(WK(1,J) + WK(1,J+1))
275  END DO
276  ENDIF
277  !
278  !  CONSTRUCT LAST EQUATION FROM SECOND BOUNDARY CONDITION, OF THE FORM
279  !       (-G*WK(2,N-1))*S(N-1) + WK(2,N)*S(N) = D(1,N)
280  !
281  ! IF SLOPE IS PRESCRIBED AT RIGHT END, ONE CAN GO DIRECTLY TO BACK-
282  ! SUBSTITUTION, SINCE ARRAYS HAPPEN TO BE SET UP JUST RIGHT FOR IT
283  ! AT THIS POINT.
284  IF (IEND == 1)  GO TO 30
285  !
286  IF (IEND == 0)  THEN
287     IF (N==2 .AND. IBEG==0)  THEN
288        ! NOT-A-KNOT AT RIGHT ENDPOINT AND AT LEFT ENDPOINT AND N = 2.
289        D(1,2) = WK(2,2)
290        GO TO 30
291     ELSE IF ((N==2) .OR. (N==3 .AND. IBEG==0))  THEN
292        ! EITHER (N=3 AND NOT-A-KNOT ALSO AT LEFT) OR (N=2 AND *NOT*
293        ! NOT-A-KNOT AT LEFT END POINT).
294        D(1,N) = TWO*WK(2,N)
295        WK(2,N) = ONE
296        IF (WK(2,N-1) == ZERO)  GO TO 5008
297        G = -ONE/WK(2,N-1)
298     ELSE
299        ! NOT-A-KNOT AND N .GE. 3, AND EITHER N.GT.3 OR  ALSO NOT-A-
300        ! KNOT AT LEFT END POINT.
301        G = WK(1,N-1) + WK(1,N)
302        ! DO NOT NEED TO CHECK FOLLOWING DENOMINATORS (X-DIFFERENCES).
303        D(1,N) = ((WK(1,N)+TWO*G)*WK(2,N)*WK(1,N-1) &
304              + WK(1,N)**2*(F(1,N-1)-F(1,N-2))/WK(1,N-1))/G
305        IF (WK(2,N-1) == ZERO)  GO TO 5008
306        G = -G/WK(2,N-1)
307        WK(2,N) = WK(1,N-1)
308     ENDIF
309  ELSE
310     ! SECOND DERIVATIVE PRESCRIBED AT RIGHT ENDPOINT.
311     D(1,N) = THREE*WK(2,N) + HALF*WK(1,N)*D(1,N)
312     WK(2,N) = TWO
313     IF (WK(2,N-1) == ZERO)  GO TO 5008
314     G = -ONE/WK(2,N-1)
315  ENDIF
316  !
317  !  COMPLETE FORWARD PASS OF GAUSS ELIMINATION.
318  !
319  WK(2,N) = G*WK(1,N-1) + WK(2,N)
320  IF (WK(2,N) == ZERO)   GO TO 5008
321  D(1,N) = (G*D(1,N-1) + D(1,N))/WK(2,N)
322  !
323  !  CARRY OUT BACK SUBSTITUTION
324  !
325   30   CONTINUE
326  DO J=NM1,1,-1
327     IF (WK(2,J) == ZERO)  GO TO 5008
328     D(1,J) = (D(1,J) - WK(1,J)*D(1,J+1))/WK(2,J)
329  END DO
330  ! --------------------(  END  CODING FROM CUBSPL )--------------------
331  !
332  !  NORMAL RETURN.
333  !
334  RETURN
335  !
336  !  ERROR RETURNS.
337  !
338 5001   CONTINUE
339  ! N.LT.2 RETURN.
340  IERR = -1
341  CALL XERMSG ('SLATEC', 'PCHSP', &
342        'NUMBER OF DATA POINTS LESS THAN TWO', IERR, 1)
343  RETURN
344  !
345 5002   CONTINUE
346  ! INCFD.LT.1 RETURN.
347  IERR = -2
348  CALL XERMSG ('SLATEC', 'PCHSP', 'INCREMENT LESS THAN ONE', IERR, &
349        1)
350  RETURN
351  !
352 5003   CONTINUE
353  ! X-ARRAY NOT STRICTLY INCREASING.
354  IERR = -3
355  CALL XERMSG ('SLATEC', 'PCHSP', 'X-ARRAY NOT STRICTLY INCREASING' &
356        , IERR, 1)
357  RETURN
358  !
359 5004   CONTINUE
360  ! IC OUT OF RANGE RETURN.
361  IERR = IERR - 3
362  CALL XERMSG ('SLATEC', 'PCHSP', 'IC OUT OF RANGE', IERR, 1)
363  RETURN
364  !
365 5007   CONTINUE
366  ! NWK TOO SMALL RETURN.
367  IERR = -7
368  CALL XERMSG ('SLATEC', 'PCHSP', 'WORK ARRAY TOO SMALL', IERR, 1)
369  RETURN
370  !
371 5008   CONTINUE
372  ! SINGULAR SYSTEM.
373  !   *** THEORETICALLY, THIS CAN ONLY OCCUR IF SUCCESSIVE X-VALUES   ***
374  !   *** ARE EQUAL, WHICH SHOULD ALREADY HAVE BEEN CAUGHT (IERR=-3). ***
375  IERR = -8
376  CALL XERMSG ('SLATEC', 'PCHSP', 'SINGULAR LINEAR SYSTEM', IERR, &
377        1)
378  RETURN
379  !
380 5009   CONTINUE
381  ! ERROR RETURN FROM PCHDF.
382  !   *** THIS CASE SHOULD NEVER OCCUR ***
383  IERR = -9
384  CALL XERMSG ('SLATEC', 'PCHSP', 'ERROR RETURN FROM PCHDF', IERR, &
385        1)
386  RETURN
387  !------------- LAST LINE OF PCHSP FOLLOWS ------------------------------
388END SUBROUTINE PCHSP
Note: See TracBrowser for help on using the repository browser.