Skip to content

Commit c8ef129

Browse files
author
nshaffer
committed
Finished cubic spline and tweaked errors.
1 parent 778f7dd commit c8ef129

File tree

11 files changed

+330
-31
lines changed

11 files changed

+330
-31
lines changed

error.f90

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
module error
22
implicit none
33
contains
4-
subroutine throw(msg, u)
5-
character(len=*), intent(in) :: msg
6-
integer, optional, intent(in) :: u
7-
if (present(u)) then
8-
write(u, *) "Error: " // trim(msg)
4+
subroutine throw(msg, unit)
5+
character(*), intent(in) :: msg
6+
integer, optional, intent(in) :: unit
7+
if (present(unit)) then
8+
write(unit, *) "Error: " // trim(msg)
99
else
1010
print *, "Error: " // trim(msg)
1111
end if
1212
stop
1313
end subroutine throw
1414

15-
subroutine warn(msg, u)
16-
character(len=*), intent(in) :: msg
17-
integer, optional, intent(in) :: u
18-
if (present(u)) then
19-
write(u, *) "Warning: " // trim(msg)
15+
subroutine warn(msg, unit)
16+
character(*), intent(in) :: msg
17+
integer, optional, intent(in) :: unit
18+
if (present(unit)) then
19+
write(unit, *) "Warning: " // trim(msg)
2020
else
2121
print *, "Warning: " // trim(msg)
2222
end if

error.mod

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

error.o

2.41 KB
Binary file not shown.

interp.f90

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,6 @@ subroutine linear_coeff(xx, yy, bb, n)
2020
end do
2121
end subroutine linear_coeff
2222

23-
real(dp) function linear_eval(x, xx, yy, bb, n) result(s)
24-
! Evaluates the piecewise linear interpolant
25-
! S_i(x) = y_i + b_i*(x-x_i)
26-
! at a point x.
27-
integer, intent(in) :: n
28-
real(dp), intent(in) :: x
29-
real(dp), dimension(n), intent(in) :: xx, yy
30-
real(dp), dimension(n-1), intent(in) :: bb
31-
32-
integer :: i
33-
34-
if ( (x.lt.xx(1)).or.(x.gt.xx(n)) ) then
35-
call warn("Requested abcissa lies outside the given range. Defaulting to 0.d0.")
36-
s = 0.d0
37-
else
38-
i = isearch(x, xx, n)
39-
s = yy(i) + bb(i)*(x - xx(i))
40-
end if
41-
end function linear_eval
42-
4323
subroutine cubic_coeff(xx, yy, bb, cc, dd, n)
4424
! Computes the N coefficients of the natural cubic spline fitting
4525
! the set of N knots (xx, y).
@@ -110,6 +90,26 @@ subroutine cubic_coeff(xx, yy, bb, cc, dd, n)
11090
cc(n) = 3.d0*cc(n)
11191
dd(n) = dd(nm1)
11292
end subroutine cubic_coeff
93+
94+
real(dp) function linear_eval(x, xx, yy, bb, n) result(s)
95+
! Evaluates the piecewise linear interpolant
96+
! S_i(x) = y_i + b_i*(x-x_i)
97+
! at a point x.
98+
integer, intent(in) :: n
99+
real(dp), intent(in) :: x
100+
real(dp), dimension(n), intent(in) :: xx, yy
101+
real(dp), dimension(n-1), intent(in) :: bb
102+
103+
integer :: i
104+
105+
if ( (x.lt.xx(1)).or.(x.gt.xx(n)) ) then
106+
call warn("Requested abcissa lies outside the given range. Defaulting to 0.d0.")
107+
s = 0.d0
108+
else
109+
i = isearch(x, xx, n)
110+
s = yy(i) + bb(i)*(x - xx(i))
111+
end if
112+
end function linear_eval
113113

114114
real(dp) function cubic_eval(x, xx, yy, bb, cc, dd, n) result(s)
115115
! Evaluates the cubic spline
@@ -121,8 +121,8 @@ real(dp) function cubic_eval(x, xx, yy, bb, cc, dd, n) result(s)
121121
real(dp), dimension(n), intent(in) :: bb, cc, dd
122122
integer :: i
123123
if ( (x.lt.xx(1)).or.(x.gt.xx(n)) ) then
124+
call warn("Requested abcissa lies outside the given range. Defaulting to 0.")
124125
s = 0.d0
125-
call warn("Requested abcissa lies outside the given range. Defaulting to 0.d0.")
126126
else
127127
i = isearch(x, xx, n)
128128
s = yy(i) + bb(i)*(x-xx(i)) + cc(i)*(x-xx(i))**2 + dd(i)*(x-xx(i))**3

interp.mod

Lines changed: 151 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interp.o

5.09 KB
Binary file not shown.

iotools.mod

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iotools.o

4.09 KB
Binary file not shown.

test_cubic.f90

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
program test_cubic
2+
use types, only: dp
3+
use interp, only: cubic_coeff, cubic_eval
4+
implicit none
5+
6+
integer, parameter :: n = 5
7+
real(dp), dimension(n) :: xx, yy
8+
real(dp), dimension(n) :: bb, cc, dd
9+
integer :: i
10+
real(dp) :: x, y
11+
real(dp), dimension(500) :: rr, ss
12+
13+
xx(1) = 0.d0
14+
do i = 2, n
15+
xx(i) = xx(i-1) + 0.5d0
16+
end do
17+
yy = sin(xx)
18+
19+
call cubic_coeff(xx, yy, bb, cc, dd, n)
20+
21+
x = 1.2d0
22+
y = cubic_eval(x, xx, yy, bb, cc, dd, n) ! Should be 0.93148217019426938
23+
print *, y
24+
25+
x = 4.d0
26+
y = cubic_eval(x, xx, yy, bb, cc, dd, n) ! Should warn and be 0.d0
27+
print *, y
28+
29+
end program test_cubic
30+

0 commit comments

Comments
 (0)