MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
interpolation.f90
Go to the documentation of this file.
1 
7 module interpolation
8  use constants, only: dp
9  implicit none
10  private
11  public pwl_interp_1d
12 contains
13 !!********************************BEGINNING*************************************
14 !!minimal working example (you have to supply xy values in "refl.dat" and change value of xi)
15 !subroutine test_pwl_interp_1d
16 ! integer :: fi=154 !file index
17 ! integer :: i !counter
18 ! integer, parameter :: nd=101 !number of data points
19 ! real(dp), dimension(nd) :: xd,yd !data for interpolation
20 ! integer :: ni=1 !number of points, where we want to interpolate
21 ! real(dp) :: xi(1) !x-values of points, where we want to interpolate
22 ! real(dp) :: yi(1) !interpolated y-values
23 ! open(fi,file="refl.dat")
24 ! do i=1,nd
25 ! read(fi,*) xd(i),yd(i)
26 ! enddo
27 ! close(fi)
28 ! xi=2e-6_dp
29 ! call pwl_interp_1d ( nd, xd, yd, ni, xi, yi )
30 ! write(*,*) xi,yi
31 !end subroutine test_pwl_interp_1d
32 !!***********************************END****************************************
33 
34 
39 subroutine pwl_interp_1d ( nd, xd, yd, ni, xi, yi )
40 
41 !*****************************************************************************80
42 !
43 !! PWL_INTERP_1D evaluates the piecewise linear interpolant.
44 !
45 ! Discussion:
46 !
47 ! The piecewise linear interpolant L(ND,XD,YD)(X) is the piecewise
48 ! linear function which interpolates the data (XD(I),YD(I)) for I = 1
49 ! to ND.
50 !
51 ! Licensing:
52 !
53 ! This code is distributed under the GNU LGPL license.
54 !
55 ! Modified:
56 !
57 ! 22 September 2012
58 !
59 ! Author:
60 !
61 ! John Burkardt
62 !
63 ! Parameters:
64 !
65 ! Input, integer ( kind = 4 ) ND, the number of data points.
66 ! ND must be at least 1.
67 !
68 ! Input, real ( kind = 8 ) XD(ND), the data points.
69 !
70 ! Input, real ( kind = 8 ) YD(ND), the data values.
71 !
72 ! Input, integer ( kind = 4 ) NI, the number of interpolation points.
73 !
74 ! Input, real ( kind = 8 ) XI(NI), the interpolation points.
75 !
76 ! Output, real ( kind = 8 ) YI(NI), the interpolated values.
77 !
78  implicit none
79 
80  integer ( kind = 4 ) nd
81  integer ( kind = 4 ) ni
82 
83  integer ( kind = 4 ) i
84  integer ( kind = 4 ) k
85  real ( kind = dp ) t
86  real ( kind = dp ) xd(nd)
87  real ( kind = dp ) yd(nd)
88  real ( kind = dp ) xi(ni)
89  real ( kind = dp ) yi(ni)
90 
91  yi(1:ni) = 0.0e+00_dp
92 
93  if ( nd == 1 ) then
94  yi(1:ni) = yd(1)
95  return
96  end if
97 
98  do i = 1, ni
99 
100  if ( xi(i) <= xd(1) ) then
101 
102  t = ( xi(i) - xd(1) ) / ( xd(2) - xd(1) )
103  yi(i) = ( 1.0e+00_dp - t ) * yd(1) + t * yd(2)
104 
105  else if ( xd(nd) <= xi(i) ) then
106 
107  t = ( xi(i) - xd(nd-1) ) / ( xd(nd) - xd(nd-1) )
108  yi(i) = ( 1.0e+00_dp - t ) * yd(nd-1) + t * yd(nd)
109 
110  else
111 
112  do k = 2, nd
113 
114  if ( xd(k-1) <= xi(i) .and. xi(i) <= xd(k) ) then
115 
116  t = ( xi(i) - xd(k-1) ) / ( xd(k) - xd(k-1) )
117  yi(i) = ( 1.0e+00_dp - t ) * yd(k-1) + t * yd(k)
118  exit
119 
120  end if
121 
122  end do
123 
124  end if
125 
126  end do
127 
128  return
129 end subroutine pwl_interp_1d
130 end module interpolation