37 public equispaced_linear_interpolate_function_1d
41 type :: equispaced_linear_interpolate_function_1d
49 real(dl) :: null_value
50 logical :: has_null_value
51 real(dl) :: grid_width
54 real(dl),
allocatable,
dimension(:) :: x
55 real(dl),
allocatable,
dimension(:) :: y
56 real(dl),
allocatable,
dimension(:) :: yp
57 real(dl),
allocatable,
dimension(:) :: ypp
58 real(dl),
allocatable,
dimension(:) :: yppp
59 real(dl),
allocatable,
dimension(:) :: yint
63 procedure :: initialize => equispacedlinearintepolatefunction1dinit
64 procedure :: precompute => equispacedlinearintepolatefunction1dprecompute
65 procedure ::
value => equispacedlinearintepolatefunction1dvalue
66 procedure :: first_derivative => equispacedlinearintepolatefunction1dfirstderivative
67 procedure :: second_derivative => equispacedlinearintepolatefunction1dsecondderivative
68 procedure :: third_derivative => equispacedlinearintepolatefunction1dthirdderivative
69 procedure :: integral => equispacedlinearintepolatefunction1dintegral
70 procedure :: initialize_derivatives => equispacedlinearintepolatefunction1dinitderivatives
72 end type equispaced_linear_interpolate_function_1d
80 subroutine equispacedlinearintepolatefunction1dinit( self, num_points, x_initial, x_final, null_value )
84 class(equispaced_linear_interpolate_function_1d) :: self
86 integer ,
intent(in) :: num_points
87 real(dl),
intent(in) :: x_initial
88 real(dl),
intent(in) :: x_final
89 real(dl),
intent(in),
optional :: null_value
94 if (
present(null_value) )
then 95 self%has_null_value = .true.
96 self%null_value = null_value
98 self%has_null_value = .false.
99 self%null_value = 0._dl
103 if (
allocated(self%x) )
deallocate( self%x )
105 self%num_points = num_points
107 allocate( self%x( self%num_points ) )
110 self%x_initial = x_initial
111 self%x_final = x_final
114 self%grid_width = ( self%x_final -self%x_initial )/
REAL( self%num_points -1 )
117 do i=1, self%num_points
118 self%x(i) = self%x_initial +
REAL(i-1)*self%grid_width
122 if (
allocated(self%y) )
deallocate( self%y )
123 if (
allocated(self%yp) )
deallocate( self%yp )
124 if (
allocated(self%ypp) )
deallocate( self%ypp )
125 if (
allocated(self%yppp) )
deallocate( self%yppp )
126 if (
allocated(self%yint) )
deallocate( self%yint )
128 allocate( self%y( self%num_points ) )
129 allocate( self%yp( self%num_points ) )
130 allocate( self%ypp( self%num_points ) )
131 allocate( self%yppp( self%num_points ) )
132 allocate( self%yint( self%num_points ) )
134 end subroutine equispacedlinearintepolatefunction1dinit
139 subroutine equispacedlinearintepolatefunction1dprecompute( self, x, ind, mu )
143 class(equispaced_linear_interpolate_function_1d) :: self
144 real(dl),
intent(in) :: x
145 integer ,
intent(out) :: ind
146 real(dl),
intent(out) :: mu
151 ind = int( ( x-self%x_initial)/self%grid_width ) +1
158 end subroutine equispacedlinearintepolatefunction1dprecompute
162 function equispacedlinearintepolatefunction1dvalue( self, x, index, coeff )
166 class(equispaced_linear_interpolate_function_1d) :: self
167 real(dl),
intent(in) :: x
168 integer ,
intent(in),
optional :: index
169 real(dl),
intent(in),
optional :: coeff
170 real(dl) :: equispacedlinearintepolatefunction1dvalue
172 real(dl) :: x1, x2, y1, y2, mu
175 equispacedlinearintepolatefunction1dvalue = self%null_value
176 if ( self%has_null_value )
then 178 if ( x <= self%x_initial .or. x >= self%x_final )
return 181 if ( x <= self%x_initial )
then 182 equispacedlinearintepolatefunction1dvalue = self%y(1)
186 if ( x >= self%x_final )
then 187 equispacedlinearintepolatefunction1dvalue = self%y(self%num_points)
193 if (
present(index) )
then 196 ind = int( ( x-self%x_initial)/self%grid_width ) +1
200 if (
present(coeff) )
then 215 equispacedlinearintepolatefunction1dvalue = y1*( 1._dl -mu ) +y2*mu
217 end function equispacedlinearintepolatefunction1dvalue
221 function equispacedlinearintepolatefunction1dfirstderivative( self, x, index, coeff )
225 class(equispaced_linear_interpolate_function_1d) :: self
226 real(dl),
intent(in) :: x
227 integer ,
intent(in),
optional :: index
228 real(dl),
intent(in),
optional :: coeff
229 real(dl) :: equispacedlinearintepolatefunction1dfirstderivative
232 real(dl) :: x1, x2, y1, y2, mu
235 equispacedlinearintepolatefunction1dfirstderivative = self%null_value
236 if ( self%has_null_value )
then 238 if ( x <= self%x_initial .or. x >= self%x_final )
return 241 if ( x <= self%x_initial )
then 242 equispacedlinearintepolatefunction1dfirstderivative = self%yp(1)
246 if ( x >= self%x_final )
then 247 equispacedlinearintepolatefunction1dfirstderivative = self%yp(self%num_points)
253 if (
present(index) )
then 256 ind = int( ( x-self%x_initial)/self%grid_width ) +1
260 if (
present(coeff) )
then 275 equispacedlinearintepolatefunction1dfirstderivative = y1*( 1._dl -mu ) +y2*mu
277 end function equispacedlinearintepolatefunction1dfirstderivative
281 function equispacedlinearintepolatefunction1dsecondderivative( self, x, index, coeff )
285 class(equispaced_linear_interpolate_function_1d) :: self
286 real(dl),
intent(in) :: x
287 integer ,
intent(in),
optional :: index
288 real(dl),
intent(in),
optional :: coeff
289 real(dl) :: equispacedlinearintepolatefunction1dsecondderivative
292 real(dl) :: x1, x2, y1, y2, mu
295 equispacedlinearintepolatefunction1dsecondderivative = self%null_value
296 if ( self%has_null_value )
then 298 if ( x <= self%x_initial .or. x >= self%x_final )
return 301 if ( x <= self%x_initial )
then 302 equispacedlinearintepolatefunction1dsecondderivative = self%ypp(1)
306 if ( x >= self%x_final )
then 307 equispacedlinearintepolatefunction1dsecondderivative = self%ypp(self%num_points)
313 if (
present(index) )
then 316 ind = int( ( x-self%x_initial)/self%grid_width ) +1
320 if (
present(coeff) )
then 335 equispacedlinearintepolatefunction1dsecondderivative = y1*( 1._dl -mu ) +y2*mu
337 end function equispacedlinearintepolatefunction1dsecondderivative
341 function equispacedlinearintepolatefunction1dthirdderivative( self, x, index, coeff )
345 class(equispaced_linear_interpolate_function_1d) :: self
346 real(dl),
intent(in) :: x
347 integer ,
intent(in),
optional :: index
348 real(dl),
intent(in),
optional :: coeff
349 real(dl) :: equispacedlinearintepolatefunction1dthirdderivative
352 real(dl) :: x1, x2, y1, y2, mu
355 equispacedlinearintepolatefunction1dthirdderivative = self%null_value
356 if ( self%has_null_value )
then 358 if ( x <= self%x_initial .or. x >= self%x_final )
return 361 if ( x <= self%x_initial )
then 362 equispacedlinearintepolatefunction1dthirdderivative = self%yppp(1)
366 if ( x >= self%x_final )
then 367 equispacedlinearintepolatefunction1dthirdderivative = self%yppp(self%num_points)
373 if (
present(index) )
then 376 ind = int( ( x-self%x_initial)/self%grid_width ) +1
380 if (
present(coeff) )
then 392 y2 = self%yppp(ind+1)
395 equispacedlinearintepolatefunction1dthirdderivative = y1*( 1._dl -mu ) +y2*mu
397 end function equispacedlinearintepolatefunction1dthirdderivative
401 function equispacedlinearintepolatefunction1dintegral( self, x, index, coeff )
405 class(equispaced_linear_interpolate_function_1d) :: self
406 real(dl),
intent(in) :: x
407 integer ,
intent(in),
optional :: index
408 real(dl),
intent(in),
optional :: coeff
409 real(dl) :: equispacedlinearintepolatefunction1dintegral
412 real(dl) :: x1, x2, y1, y2, mu
415 equispacedlinearintepolatefunction1dintegral = self%null_value
416 if ( self%has_null_value )
then 418 if ( x <= self%x_initial .or. x >= self%x_final )
return 421 if ( x <= self%x_initial )
then 422 equispacedlinearintepolatefunction1dintegral = self%yint(1)
426 if ( x >= self%x_final )
then 427 equispacedlinearintepolatefunction1dintegral = self%yint(self%num_points)
433 if (
present(index) )
then 436 ind = int( ( x-self%x_initial)/self%grid_width ) +1
440 if (
present(coeff) )
then 452 y2 = self%yint(ind+1)
455 equispacedlinearintepolatefunction1dintegral = y1*( 1._dl -mu ) +y2*mu
457 end function equispacedlinearintepolatefunction1dintegral
462 subroutine equispacedlinearintepolatefunction1dinitderivatives( self )
466 class(equispaced_linear_interpolate_function_1d) :: self
468 write(*,*)
'ERROR: not yet implemented (IW)' 471 end subroutine equispacedlinearintepolatefunction1dinitderivatives
This module contains the class that can be used for 1D linearly interpolated functions on an equispac...
This module contains various generic algorithms that are useful to EFTCAMB.