Statistiques
| Révision :

root / src / Splin1D.f90 @ 2

Historique | Voir | Annoter | Télécharger (10,99 ko)

1 1 pfleura2
       SUBROUTINE spline(x,y,n,yp1,ypn,y2)
2 1 pfleura2
!Given arrays x(1:n) and y(1:n) containing a tabulated function,
3 1 pfleura2
! i.e., y i = f(xi), with x1<x2< :::<xN , and given values yp1 and ypn
4 1 pfleura2
!for the  rst derivative of the inter- polating function at points 1 and n,
5 1 pfleura2
! respectively, this routine returns an array y2(1:n) of length n
6 1 pfleura2
!which contains the second derivatives of the interpolating function
7 1 pfleura2
!at the tabulated points xi.
8 1 pfleura2
! Ifyp1 and/or ypn are equal to 1*10^30 or larger,
9 1 pfleura2
! the routine is signaled to set the corresponding boundary
10 1 pfleura2
!condition for a natural spline,
11 1 pfleura2
!with zero second derivative on that boundary.
12 1 pfleura2
13 1 pfleura2
         Use Vartypes
14 1 pfleura2
15 1 pfleura2
         IMPLICIT NONE
16 1 pfleura2
17 1 pfleura2
! Number of points
18 1 pfleura2
         INTEGER(KINT) :: n
19 1 pfleura2
! Coordinate to spline
20 1 pfleura2
         REAL(KREAL) :: x(n),y(n)
21 1 pfleura2
! Coefficients to compute
22 1 pfleura2
         REAL(KREAL) :: y2(n)
23 1 pfleura2
! End-points derivatives
24 1 pfleura2
         REAL(KREAL) ::yp1,ypn
25 1 pfleura2
26 1 pfleura2
       INTEGER(KINT), PARAMETER  :: NMAX=500
27 1 pfleura2
28 1 pfleura2
       INTEGER(KINT) :: i,k
29 1 pfleura2
       REAL(KREAL) :: p,qn,sig,un,u(NMAX)
30 1 pfleura2
       LOGICAL Debug
31 1 pfleura2
32 1 pfleura2
  INTERFACE
33 1 pfleura2
     function valid(string) result (isValid)
34 1 pfleura2
       CHARACTER(*), intent(in) :: string
35 1 pfleura2
       logical                  :: isValid
36 1 pfleura2
     END function VALID
37 1 pfleura2
38 1 pfleura2
  END INTERFACE
39 1 pfleura2
40 1 pfleura2
       Debug=valid("spline")
41 1 pfleura2
42 1 pfleura2
       IF (DEBUG) THEN
43 1 pfleura2
          WRITE(*,*) "Spline 1D",n
44 1 pfleura2
          WRITE(*,*) "x:",(x(i),i=1,n)
45 1 pfleura2
          WRITE(*,*) "y:",(y(i),i=1,n)
46 1 pfleura2
       END IF
47 1 pfleura2
48 1 pfleura2
       if (yp1.gt..99e30) then
49 1 pfleura2
! The lower boundary condition is set either to be \natural"
50 1 pfleura2
        y2(1)=0.
51 1 pfleura2
        u(1)=0.
52 1 pfleura2
       else
53 1 pfleura2
! or else to have a speci ed  rst derivative.
54 1 pfleura2
        y2(1)=-0.5
55 1 pfleura2
        u(1)=(3./(x(2)-x(1)))*((y(2)-y(1))/(x(2)-x(1))-yp1)
56 1 pfleura2
      endif
57 1 pfleura2
      do i=2,n-1
58 1 pfleura2
! This is the decomposition loop of the tridiagonal algorithm.
59 1 pfleura2
!y2 and u are used for temporary storage of the decomposed factors.
60 1 pfleura2
       sig=(x(i)-x(i-1))/(x(i+1)-x(i-1))
61 1 pfleura2
        p=sig*y2(i-1)+2.
62 1 pfleura2
       y2(i)=(sig-1.)/p
63 1 pfleura2
       u(i)=(6.*((y(i+1)-y(i))/(x(i+1)-x(i))-(y(i)-y(i-1)) &
64 1 pfleura2
                /(x(i)-x(i-1)))/(x(i+1)-x(i-1))-sig*u(i-1))/p
65 1 pfleura2
       enddo
66 1 pfleura2
       if (ypn.gt..99e30) then
67 1 pfleura2
!The upper boundary condition is set either to be \natural"
68 1 pfleura2
         qn=0.
69 1 pfleura2
         un=0.
70 1 pfleura2
        else
71 1 pfleura2
!or else to have a speci ed  rst derivative.
72 1 pfleura2
         qn=0.5
73 1 pfleura2
         un=(3./(x(n)-x(n-1)))*(ypn-(y(n)-y(n-1))/(x(n)-x(n-1)))
74 1 pfleura2
         endif
75 1 pfleura2
        y2(n)=(un-qn*u(n-1))/(qn*y2(n-1)+1.)
76 1 pfleura2
        do k=n-1,1,-1
77 1 pfleura2
         y2(k)=y2(k)*y2(k+1)+u(k)
78 1 pfleura2
        enddo
79 1 pfleura2
80 1 pfleura2
        IF (DEBUG)  WRITE(*,*) "y2:",(y2(i),i=1,n)
81 1 pfleura2
         return
82 1 pfleura2
        END
83 1 pfleura2
84 1 pfleura2
85 1 pfleura2
      SUBROUTINE splint(x,y,N,xa,ya,y2a)
86 1 pfleura2
87 1 pfleura2
         Use Vartypes
88 1 pfleura2
89 1 pfleura2
         IMPLICIT NONE
90 1 pfleura2
91 1 pfleura2
! Number of points
92 1 pfleura2
         INTEGER(KINT) :: n
93 1 pfleura2
! Spline coefficients
94 1 pfleura2
         REAL(KREAL) :: xa(n),ya(n), y2a(n)
95 1 pfleura2
! Y to compute for a given x
96 1 pfleura2
         REAL(KREAL) :: x,y
97 1 pfleura2
98 1 pfleura2
99 1 pfleura2
       LOGICAL debug
100 1 pfleura2
101 1 pfleura2
! Given the arrays xa(1:n) and ya(1:n) of length n, which tabulate a
102 1 pfleura2
! function (with the xai's in order), and given the array y2a(1:n),
103 1 pfleura2
!which is the output from spline above, and given a value of x,
104 1 pfleura2
! this routine returns a cubic-spline interpolated value y.
105 1 pfleura2
       INTEGER(KINT) :: k,khi,klo
106 1 pfleura2
       REAL(KREAL) :: a,b,h
107 1 pfleura2
108 1 pfleura2
109 1 pfleura2
  INTERFACE
110 1 pfleura2
     function valid(string) result (isValid)
111 1 pfleura2
       CHARACTER(*), intent(in) :: string
112 1 pfleura2
       logical                  :: isValid
113 1 pfleura2
     END function VALID
114 1 pfleura2
115 1 pfleura2
  END INTERFACE
116 1 pfleura2
117 1 pfleura2
       Debug=valid("splint")
118 1 pfleura2
119 1 pfleura2
120 1 pfleura2
!           if (debug) THEN
121 1 pfleura2
!       WRITE(*,*) "Splint 1D",n
122 1 pfleura2
!       WRITE(*,*) "xa:",(xa(i),i=1,n)
123 1 pfleura2
!       WRITE(*,*) "ya:",(ya(i),i=1,n)
124 1 pfleura2
!       WRITE(*,*) "y2a:",(y2a(i),i=1,n)
125 1 pfleura2
!        END IF
126 1 pfleura2
127 1 pfleura2
128 1 pfleura2
129 1 pfleura2
          klo=1
130 1 pfleura2
!We will  find the right place in the table by means of bisection.
131 1 pfleura2
! This is optimal if sequential calls to this routine are at
132 1 pfleura2
!random values of x. If sequential calls are in order, and closely
133 1 pfleura2
! spaced, one would do better to store previous values of klo and khi
134 1 pfleura2
! and test if they remain appropriate on the next call.
135 1 pfleura2
          khi=n
136 1 pfleura2
 !         WRITE(*,*) xa(klo),xa(khi),n,x
137 1 pfleura2
 1        if (khi-klo.gt.1) then
138 1 pfleura2
             k=(khi+klo)/2
139 1 pfleura2
             if(xa(k).gt.x) then
140 1 pfleura2
                khi=k
141 1 pfleura2
             else
142 1 pfleura2
                klo=k
143 1 pfleura2
             endif
144 1 pfleura2
          goto 1
145 1 pfleura2
          endif
146 1 pfleura2
! klo and khi now bracket the input value of x.
147 1 pfleura2
          h=xa(khi)-xa(klo)
148 1 pfleura2
! The xa's must be distinct.
149 2 pfleura2
          if (h.eq.0.) THEN
150 2 pfleura2
             WRITE(*,*) 'bad xa input in splint'
151 2 pfleura2
             STOP
152 2 pfleura2
          END IF
153 1 pfleura2
! Cubic spline polynomial is now evaluated.
154 1 pfleura2
           a=(xa(khi)-x)/h
155 1 pfleura2
           b=(x-xa(klo))/h
156 1 pfleura2
           y=a*ya(klo)+b*ya(khi)+ ((a**3-a)*y2a(klo)+(b**3-b)*y2a(khi)) &
157 1 pfleura2
                *(h**2)/6.
158 1 pfleura2
159 1 pfleura2
!           WRITE(*,*) "Splint1D x,y",x,y
160 1 pfleura2
          return
161 1 pfleura2
          END
162 1 pfleura2
163 1 pfleura2
164 1 pfleura2
! SUBROUTINE splinder *************************************************
165 1 pfleura2
! *********************************************************************
166 1 pfleura2
167 1 pfleura2
      SUBROUTINE splinder(x,y,N,xa,ya,y2a)
168 1 pfleura2
         Use Vartypes
169 1 pfleura2
170 1 pfleura2
         IMPLICIT NONE
171 1 pfleura2
172 1 pfleura2
! Number of points
173 1 pfleura2
         INTEGER(KINT) :: n
174 1 pfleura2
! Spline coefficients
175 1 pfleura2
         REAL(KREAL) :: xa(n),ya(n), y2a(n)
176 1 pfleura2
! Y to compute for a given x
177 1 pfleura2
         REAL(KREAL) :: x,y
178 1 pfleura2
179 1 pfleura2
180 1 pfleura2
       LOGICAL debug
181 1 pfleura2
182 1 pfleura2
! Given the arrays xa(1:n) and ya(1:n) of length n, which tabulate a
183 1 pfleura2
! function (with the xai's in order), and given the array y2a(1:n),
184 1 pfleura2
! which is the output from spline above, and given a value of x,
185 1 pfleura2
! this routine returns a cubic-spline interpolated value y first DERIVATIVE.
186 1 pfleura2
! this routine returns a cubic-spline interpolated value y.
187 1 pfleura2
       INTEGER(KINT) :: k,khi,klo
188 1 pfleura2
       REAL(KREAL) :: a,b,h
189 1 pfleura2
190 1 pfleura2
191 1 pfleura2
  INTERFACE
192 1 pfleura2
     function valid(string) result (isValid)
193 1 pfleura2
       CHARACTER(*), intent(in) :: string
194 1 pfleura2
       logical                  :: isValid
195 1 pfleura2
     END function VALID
196 1 pfleura2
197 1 pfleura2
  END INTERFACE
198 1 pfleura2
199 1 pfleura2
200 1 pfleura2
       Debug=valid("splinder")
201 1 pfleura2
202 1 pfleura2
203 1 pfleura2
204 1 pfleura2
          klo=1
205 1 pfleura2
! We will  find the right place in the table by means of bisection.
206 1 pfleura2
! This is optimal if sequential calls to this routine are at
207 1 pfleura2
!random values of x. If sequential calls are in order, and closely
208 1 pfleura2
! spaced, one would do better to store previous values of klo and khi
209 1 pfleura2
! and test if they remain appropriate on the next call.
210 1 pfleura2
          khi=n
211 1 pfleura2
 !         WRITE(*,*) xa(klo),xa(khi),n,x
212 1 pfleura2
 1        if (khi-klo.gt.1) then
213 1 pfleura2
             k=(khi+klo)/2
214 1 pfleura2
             if(xa(k).gt.x) then
215 1 pfleura2
                khi=k
216 1 pfleura2
             else
217 1 pfleura2
                klo=k
218 1 pfleura2
             endif
219 1 pfleura2
          goto 1
220 1 pfleura2
          endif
221 1 pfleura2
! klo and khi now bracket the input value of x.
222 1 pfleura2
          h=xa(khi)-xa(klo)
223 1 pfleura2
! The xa's must be distinct.
224 2 pfleura2
          if (h.eq.0.) THEN
225 2 pfleura2
             WRITE(*,*) 'bad xa input in splinder'
226 2 pfleura2
             STOP
227 2 pfleura2
          END IF
228 1 pfleura2
! Cubic spline polynomial is now evaluated.
229 1 pfleura2
           a=(xa(khi)-x)/h
230 1 pfleura2
           b=(x-xa(klo))/h
231 1 pfleura2
! Formula taken from the Numerical Recipies book.
232 1 pfleura2
           y=(ya(khi)-ya(klo))/h - (3*a**2-1)/6.*h*y2a(klo)+ &
233 1 pfleura2
                (3*b**2-1)/6.*h*y2a(khi)
234 1 pfleura2
          return
235 1 pfleura2
          END
236 1 pfleura2
237 1 pfleura2
238 1 pfleura2
239 1 pfleura2
! SUBROUTINE splintder *************************************************
240 1 pfleura2
! *********************************************************************
241 1 pfleura2
242 1 pfleura2
      SUBROUTINE splintDer(x,y,der,N,xa,ya,y2a)
243 1 pfleura2
244 1 pfleura2
! Given the arrays xa(1:n) and ya(1:n) of length n, which tabulate a
245 1 pfleura2
! function (with the xai's in order), and given the array y2a(1:n),
246 1 pfleura2
! which is the output from spline above, and given a value of x,
247 1 pfleura2
! this routine returns a cubic-spline interpolated value y.
248 1 pfleura2
! and the derivative der.
249 1 pfleura2
250 1 pfleura2
         Use Vartypes
251 1 pfleura2
252 1 pfleura2
         IMPLICIT NONE
253 1 pfleura2
254 1 pfleura2
! Number of points
255 1 pfleura2
         INTEGER(KINT) :: n
256 1 pfleura2
! Spline coefficients
257 1 pfleura2
         REAL(KREAL) :: xa(n),ya(n), y2a(n)
258 1 pfleura2
! Y to compute for a given x
259 1 pfleura2
         REAL(KREAL) :: x,y, der
260 1 pfleura2
261 1 pfleura2
262 1 pfleura2
       LOGICAL debug
263 1 pfleura2
264 1 pfleura2
       INTEGER(KINT) :: k,khi,klo
265 1 pfleura2
       REAL(KREAL) :: a,b,h
266 1 pfleura2
267 1 pfleura2
  INTERFACE
268 1 pfleura2
     function valid(string) result (isValid)
269 1 pfleura2
       CHARACTER(*), intent(in) :: string
270 1 pfleura2
       logical                  :: isValid
271 1 pfleura2
     END function VALID
272 1 pfleura2
273 1 pfleura2
  END INTERFACE
274 1 pfleura2
275 1 pfleura2
       Debug=valid("splintder")
276 1 pfleura2
277 1 pfleura2
278 1 pfleura2
!           if (debug) THEN
279 1 pfleura2
!       WRITE(*,*) "SplintDer 1D",n
280 1 pfleura2
!       WRITE(*,*) "xa:",(xa(i),i=1,n)
281 1 pfleura2
!       WRITE(*,*) "ya:",(ya(i),i=1,n)
282 1 pfleura2
!       WRITE(*,*) "y2a:",(y2a(i),i=1,n)
283 1 pfleura2
!        END IF
284 1 pfleura2
285 1 pfleura2
286 1 pfleura2
287 1 pfleura2
          klo=1
288 1 pfleura2
! We will  find the right place in the table by means of bisection.
289 1 pfleura2
! This is optimal if sequential calls to this routine are at
290 1 pfleura2
! random values of x. If sequential calls are in order, and closely
291 1 pfleura2
! spaced, one would do better to store previous values of klo and khi
292 1 pfleura2
! and test if they remain appropriate on the next call.
293 1 pfleura2
          khi=n
294 1 pfleura2
 !         WRITE(*,*) xa(klo),xa(khi),n,x
295 1 pfleura2
 1        if (khi-klo.gt.1) then
296 1 pfleura2
             k=(khi+klo)/2
297 1 pfleura2
             if(xa(k).gt.x) then
298 1 pfleura2
                khi=k
299 1 pfleura2
             else
300 1 pfleura2
                klo=k
301 1 pfleura2
             endif
302 1 pfleura2
          goto 1
303 1 pfleura2
          endif
304 1 pfleura2
! klo and khi now bracket the input value of x.
305 1 pfleura2
          h=xa(khi)-xa(klo)
306 1 pfleura2
! The xa's must be distinct.
307 2 pfleura2
          if (h.eq.0.) THEN
308 2 pfleura2
             WRITE(*,*) 'bad xa input in splintDer'
309 2 pfleura2
             STOP
310 2 pfleura2
          END IF
311 2 pfleura2
312 1 pfleura2
! Cubic spline polynomial is now evaluated.
313 1 pfleura2
           a=(xa(khi)-x)/h
314 1 pfleura2
           b=(x-xa(klo))/h
315 1 pfleura2
           y=a*ya(klo)+b*ya(khi)+ ((a**3-a)*y2a(klo)+(b**3-b)*y2a(khi)) &
316 1 pfleura2
                *(h**2)/6.
317 1 pfleura2
! Formula taken from the Numerical Recipies book.
318 1 pfleura2
           der=(ya(khi)-ya(klo))/h - (3*a**2-1)/6.*h*y2a(klo)+ &
319 1 pfleura2
                (3*b**2-1)/6.*h*y2a(khi)
320 1 pfleura2
          return
321 1 pfleura2
        END SUBROUTINE splintDer
322 1 pfleura2
323 1 pfleura2
324 1 pfleura2
325 1 pfleura2
      SUBROUTINE LinearInt(x,y,der,N,xa,ya)
326 1 pfleura2
327 1 pfleura2
! Given the arrays xa(1:n) and ya(1:n) of length n, which tabulate a
328 2 pfleura2
! function (with the xai's in order),  and given a value of x,
329 2 pfleura2
! this routine returns a linear interpolated value y and its derivative der
330 1 pfleura2
331 1 pfleura2
         Use Vartypes
332 1 pfleura2
333 1 pfleura2
         IMPLICIT NONE
334 1 pfleura2
335 1 pfleura2
! Number of points
336 1 pfleura2
         INTEGER(KINT) :: n
337 1 pfleura2
! Spline coefficients
338 1 pfleura2
         REAL(KREAL) :: xa(n),ya(n)
339 1 pfleura2
! Y to compute for a given x
340 1 pfleura2
         REAL(KREAL) :: x,y, der
341 1 pfleura2
342 1 pfleura2
343 1 pfleura2
       LOGICAL debug
344 1 pfleura2
345 1 pfleura2
       INTEGER(KINT) :: k,khi,klo
346 1 pfleura2
       REAL(KREAL) :: a,b,h
347 1 pfleura2
348 1 pfleura2
  INTERFACE
349 1 pfleura2
     function valid(string) result (isValid)
350 1 pfleura2
       CHARACTER(*), intent(in) :: string
351 1 pfleura2
       logical                  :: isValid
352 1 pfleura2
     END function VALID
353 1 pfleura2
354 1 pfleura2
  END INTERFACE
355 1 pfleura2
356 1 pfleura2
       Debug=valid("linearint")
357 1 pfleura2
358 1 pfleura2
359 1 pfleura2
!           if (debug) THEN
360 1 pfleura2
!       WRITE(*,*) "SplintDer 1D",n
361 1 pfleura2
!       WRITE(*,*) "xa:",(xa(i),i=1,n)
362 1 pfleura2
!       WRITE(*,*) "ya:",(ya(i),i=1,n)
363 1 pfleura2
!       WRITE(*,*) "y2a:",(y2a(i),i=1,n)
364 1 pfleura2
!        END IF
365 1 pfleura2
366 1 pfleura2
367 1 pfleura2
368 1 pfleura2
          klo=1
369 1 pfleura2
! We will  find the right place in the table by means of bisection.
370 1 pfleura2
! This is optimal if sequential calls to this routine are at
371 1 pfleura2
! random values of x. If sequential calls are in order, and closely
372 1 pfleura2
! spaced, one would do better to store previous values of klo and khi
373 1 pfleura2
! and test if they remain appropriate on the next call.
374 1 pfleura2
          khi=n
375 1 pfleura2
 !         WRITE(*,*) xa(klo),xa(khi),n,x
376 1 pfleura2
 1        if (khi-klo.gt.1) then
377 1 pfleura2
             k=(khi+klo)/2
378 1 pfleura2
             if(xa(k).gt.x) then
379 1 pfleura2
                khi=k
380 1 pfleura2
             else
381 1 pfleura2
                klo=k
382 1 pfleura2
             endif
383 1 pfleura2
          goto 1
384 1 pfleura2
          endif
385 1 pfleura2
! klo and khi now bracket the input value of x.
386 1 pfleura2
          h=xa(khi)-xa(klo)
387 1 pfleura2
! The xa's must be distinct.
388 2 pfleura2
          if (h.eq.0.) THEN
389 2 pfleura2
             WRITE(*,*) 'bad xa input in LinearIntDer'
390 2 pfleura2
             STOP
391 2 pfleura2
          END IF
392 2 pfleura2
393 1 pfleura2
! Linear int now evaluated.
394 1 pfleura2
           a=(xa(khi)-x)/h
395 1 pfleura2
           b=(x-xa(klo))/h
396 1 pfleura2
           y=a*ya(klo)+b*ya(khi)
397 1 pfleura2
! Formula taken from the Numerical Recipies book.
398 1 pfleura2
           der=a+b
399 1 pfleura2
          return
400 1 pfleura2
        END SUBROUTINE LinearInt