5 integer,
parameter :: dp = kind(0.0d0)
7 real(dp),
parameter :: x_data(4) = [1.0_dp, 2.75_dp, 3.3_dp, 5.6_dp]
10 print *,
"This program demonstrates the usage of m_lookup_table"
13 print *,
"Create a lookup table with the following options"
14 print *,
" x_min = ", x_data(1)
15 print *,
" x_max = ", x_data(4)
16 print *,
" n_points = ", 20
17 print *,
" n_cols = ", 2
18 my_lt =
lt_create(x_data(1), x_data(4), n_points=20, n_cols=2, &
21 print *,
"The table input is unevenly spaced, using these x-values:"
22 write(*,
"(4e12.4)") x_data
25 print *,
"Setting the first column to y1 = x**2"
28 print *,
"Setting the second column to y2 = sin(x)"
31 print *,
"Adding a third column (which causes re-allocations),"
32 print *,
"which is set to y3 = sqrt(x)"
45 type(
lt_t),
intent(in) :: lt
47 print *,
"----------------------------------------"
48 print *,
"Example 1: Get values from a single column"
50 print *,
"3**2 is about: ",
lt_get_col(lt, 1, 3.0_dp)
51 print *,
"sin(3) is about: ",
lt_get_col(lt, 2, 3.0_dp)
52 print *,
"sqrt(3) is about: ",
lt_get_col(lt, 3, 3.0_dp)
53 print *,
"----------------------------------------"
57 subroutine example_2(lt)
58 type(
lt_t),
intent(in) :: lt
61 print *,
"----------------------------------------"
62 print *,
"Example 2: Get values using a loc (location) object, which"
63 print *,
"can be faster if you look up at the same x-value often"
70 print *,
"----------------------------------------"
72 end subroutine example_2
74 subroutine example_3(lt)
75 type(
lt_t),
intent(in) :: lt
76 real(dp) :: columns(lt%n_cols)
78 print *,
"----------------------------------------"
79 print *,
"Example 3: Get all the columns at the same time."
80 print *,
"This can also be done using a loc object"
84 print *,
"3**2 is about: ", columns(1)
85 print *,
"sin(3) is about: ", columns(2)
86 print *,
"sqrt(3) is about: ", columns(3)
87 print *,
"----------------------------------------"
89 end subroutine example_3
91 subroutine example_4(lt)
92 type(
lt_t),
intent(in) :: lt
94 print *,
"----------------------------------------"
95 print *,
"Example 4: When x > x_max or x < x_min, a lookup"
96 print *,
"returns y(x_max) and y(x_min), respectively"
99 print *,
"The table starts at 1.0, so a lookup at 0.0 gives"
100 print *,
"LT_get_col(lt, 1, 0.0_dp):",
lt_get_col(lt, 1, 0.0_dp)
101 print *,
"which is the same as"
102 print *,
"LT_get_col(lt, 1, 1.0_dp):",
lt_get_col(lt, 1, 1.0_dp)
104 print *,
"The table ends at 5.6, so a lookup at 10.0 gives"
105 print *,
"LT_get_col(lt, 1, 10.0_dp):",
lt_get_col(lt, 1, 10.0_dp)
106 print *,
"which is the same as"
107 print *,
"LT_get_col(lt, 1, 5.6_dp): ",
lt_get_col(lt, 1, 5.6_dp)
108 print *,
"----------------------------------------"
110 end subroutine example_4
112 subroutine example_5(lt)
113 type(
lt_t),
intent(in) :: lt
115 character(len=*),
parameter :: filename =
"saved_table.dat"
117 print *,
"----------------------------------------"
118 print *,
"Example 5: You can store the lookup table in a file,"
119 print *,
"and read it back in. The table is stored in binary,"
120 print *,
"which is not always portable between machines."
122 print *,
"Storing the lookup table in ", filename
125 print *,
"Reading in a lookup table from ", filename
129 print *,
"The results should be the same:"
130 print *,
"3**2 is about: ",
lt_get_col(new_lt, 1, 3.0_dp)
131 print *,
"sin(3) is about: ",
lt_get_col(new_lt, 2, 3.0_dp)
132 print *,
"sqrt(3) is about: ",
lt_get_col(new_lt, 3, 3.0_dp)
133 print *,
"----------------------------------------"
135 end subroutine example_5
A Fortran 90 module for creating lookup tables. These tables can be used to efficiently interpolate o...
subroutine, public lt_set_col(my_lt, col_ix, x, y)
Fill the column with index col_ix after linearly interpolating.
type(lt_t) function, public lt_create(x_min, x_max, n_points, n_cols, xspacing, extrapolate_above)
This function returns a new lookup table.
elemental real(dp) function, public lt_get_col_at_loc(my_lt, col_ix, loc)
Get the value of a single column at a location.
subroutine, public lt_from_file(my_lt, filename)
Read the lookup table from file (in binary, potentially unportable)
pure real(dp) function, dimension(my_lt%n_cols), public lt_get_mcol(my_lt, x)
Get the values of all columns at x.
subroutine, public lt_add_col(my_lt, x, y)
Add a new column by linearly interpolating the (x, y) data.
elemental real(dp) function, public lt_get_col(my_lt, col_ix, x)
Get the value of a single column at x.
integer, parameter, public lt_xspacing_linear
subroutine, public lt_to_file(my_lt, filename)
Write the lookup table to file (in binary, potentially unportable)
elemental type(lt_loc_t) function, public lt_get_loc(my_lt, x)
Get a location in the lookup table.
Type to indicate a location in the lookup table, which can be used to speed up multiple lookups of di...
The lookup table type. There can be one or more columns, for which values can be looked up for a give...