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