MPI-AMRVAC 3.2
The MPI - Adaptive Mesh Refinement - Versatile Advection Code (development version)
Loading...
Searching...
No Matches
mod_spline.t
Go to the documentation of this file.
2 use mod_comm_lib, only: mpistop
3 implicit none
4contains
5!Copied from:
6!https://ww2.odu.edu/~agodunov/computing/programs/book2/Ch01/spline.f90
7!!interpolation START
8 subroutine spline (x, y, b, c, d, n)
9!======================================================================
10! Calculate the coefficients b(i), c(i), and d(i), i=1,2,...,n
11! for cubic spline interpolation
12! s(x) = y(i) + b(i)*(x-x(i)) + c(i)*(x-x(i))**2 + d(i)*(x-x(i))**3
13! for x(i) <= x <= x(i+1)
14! Alex G: January 2010
15!----------------------------------------------------------------------
16! input..
17! x = the arrays of data abscissas (in strictly increasing order)
18! y = the arrays of data ordinates
19! n = size of the arrays xi() and yi() (n>=2)
20! output..
21! b, c, d = arrays of spline coefficients
22! comments ...
23! spline.f90 program is based on fortran version of program spline.f
24! the accompanying function fspline can be used for interpolation
25!======================================================================
26implicit none
27integer n
28double precision x(n), y(n), b(n), c(n), d(n)
29integer i, j, gap
30double precision h
31
32gap = n-1
33! check input
34if ( n < 2 ) return
35if ( n < 3 ) then
36 b(1) = (y(2)-y(1))/(x(2)-x(1)) ! linear interpolation
37 c(1) = 0.
38 d(1) = 0.
39 b(2) = b(1)
40 c(2) = 0.
41 d(2) = 0.
42 return
43end if
44!
45! step 1: preparation
46!
47d(1) = x(2) - x(1)
48c(2) = (y(2) - y(1))/d(1)
49do i = 2, gap
50 d(i) = x(i+1) - x(i)
51 b(i) = 2.0*(d(i-1) + d(i))
52 c(i+1) = (y(i+1) - y(i))/d(i)
53 c(i) = c(i+1) - c(i)
54end do
55!
56! step 2: end conditions
57!
58b(1) = -d(1)
59b(n) = -d(n-1)
60c(1) = 0.0
61c(n) = 0.0
62if(n /= 3) then
63 c(1) = c(3)/(x(4)-x(2)) - c(2)/(x(3)-x(1))
64 c(n) = c(n-1)/(x(n)-x(n-2)) - c(n-2)/(x(n-1)-x(n-3))
65 c(1) = c(1)*d(1)**2/(x(4)-x(1))
66 c(n) = -c(n)*d(n-1)**2/(x(n)-x(n-3))
67end if
68!
69! step 3: forward elimination
70!
71do i = 2, n
72 h = d(i-1)/b(i-1)
73 b(i) = b(i) - h*d(i-1)
74 c(i) = c(i) - h*c(i-1)
75end do
76!
77! step 4: back substitution
78!
79c(n) = c(n)/b(n)
80do j = 1, gap
81 i = n-j
82 c(i) = (c(i) - d(i)*c(i+1))/b(i)
83end do
84!
85! step 5: compute spline coefficients
86!
87b(n) = (y(n) - y(gap))/d(gap) + d(gap)*(c(gap) + 2.0*c(n))
88do i = 1, gap
89 b(i) = (y(i+1) - y(i))/d(i) - d(i)*(c(i+1) + 2.0*c(i))
90 d(i) = (c(i+1) - c(i))/d(i)
91 c(i) = 3.*c(i)
92end do
93c(n) = 3.0*c(n)
94d(n) = d(n-1)
95end subroutine spline
96
97 function ispline(u, x, y, b, c, d, n)
98!======================================================================
99! function ispline evaluates the cubic spline interpolation at point z
100! ispline = y(i)+b(i)*(u-x(i))+c(i)*(u-x(i))**2+d(i)*(u-x(i))**3
101! where x(i) <= u <= x(i+1)
102!----------------------------------------------------------------------
103! input..
104! u = the abscissa at which the spline is to be evaluated
105! x, y = the arrays of given data points
106! b, c, d = arrays of spline coefficients computed by spline
107! n = the number of data points
108! output:
109! ispline = interpolated value at point u
110!=======================================================================
111implicit none
112double precision ispline
113integer n
114double precision u, x(n), y(n), b(n), c(n), d(n)
115integer i, j, k
116double precision dx
117
118! if u is ouside the x() interval take a boundary value (left or right)
119if(u <= x(1)) then
120 ispline = y(1)
121 return
122end if
123if(u >= x(n)) then
124 ispline = y(n)
125 return
126end if
127
128!*
129! binary search for for i, such that x(i) <= u <= x(i+1)
130!*
131i = 1
132j = n+1
133do while (j > i+1)
134 k = (i+j)/2
135 if(u < x(k)) then
136 j=k
137 else
138 i=k
139 end if
140end do
141!*
142! evaluate spline interpolation
143!*
144dx = u - x(i)
145ispline = y(i) + dx*(b(i) + dx*(c(i) + dx*d(i)))
146end function ispline
147
148end module mod_spline
subroutine, public mpistop(message)
Exit MPI-AMRVAC with an error message.
double precision function ispline(u, x, y, b, c, d, n)
Definition mod_spline.t:98
subroutine spline(x, y, b, c, d, n)
Definition mod_spline.t:9