mod_lookup_table Module

A Fortran 90 module for creating 1D and 2D lookup tables. These tables can be used to efficiently interpolate one or more values.

Author: Jannis Teunissen


Used by

  • module~~mod_lookup_table~~UsedByGraph module~mod_lookup_table mod_lookup_table module~mod_bc_data mod_bc_data module~mod_bc_data->module~mod_lookup_table module~mod_init_datafromfile mod_init_datafromfile module~mod_init_datafromfile->module~mod_lookup_table proc~initialize_amrvac initialize_amrvac proc~initialize_amrvac->module~mod_bc_data proc~initialize_amrvac->module~mod_init_datafromfile

Variables

Type Visibility Attributes Name Initial
integer, private, parameter :: dp = kind(1.0d0)

Derived Types

type, public ::  LT2_loc_t

Type to indicate a location in a 2D lookup table

Components

Type Visibility Attributes Name Initial
real(kind=dp), private :: low_frac(2)

The distance from low_ix (up to low_ix+1), given as a real number between 0 and 1.

integer, private :: low_ix(2)

The x-value lies between low_ix and low_ix+1

type, public ::  LT2_t

The 2D lookup table type

Components

Type Visibility Attributes Name Initial
real(kind=dp), public :: dx(2)

The x-spacing in the lookup coordinate

real(kind=dp), public :: inv_dx(2)

The inverse x-spacing

integer, public :: n_cols

The number of columns/variables

integer, public :: n_points(2)

The size of the table

real(kind=dp), public, allocatable :: rows_cols(:,:,:)
real(kind=dp), public :: x_min(2)

The minimum lookup coordinate

type, public ::  LT3_loc_t

Type to indicate a location in a 3D lookup table

Components

Type Visibility Attributes Name Initial
real(kind=dp), private :: low_frac(3)

The distance from low_ix (up to low_ix+1), given as a real number between 0 and 1.

integer, private :: low_ix(3)

The x-value lies between low_ix and low_ix+1

type, public ::  LT3_t

The 3D lookup table type

Components

Type Visibility Attributes Name Initial
real(kind=dp), public :: dx(3)

The x-spacing in the lookup coordinate

real(kind=dp), public :: inv_dx(3)

The inverse x-spacing

integer, public :: n_cols

The number of columns/variables

integer, public :: n_points(3)

The size of the table

real(kind=dp), public, allocatable :: rows_cols(:,:,:,:)
real(kind=dp), public :: x_min(3)

The minimum lookup coordinate

type, public ::  LT_loc_t

Type to indicate a location in the lookup table, which can be used to speed up multiple lookups of different columns.

Components

Type Visibility Attributes Name Initial
real(kind=dp), private :: low_frac

The distance from low_ix (up to low_ix+1), given as a real number between 0 and 1.

integer, private :: low_ix

The x-value lies between low_ix and low_ix+1

type, public ::  LT_t

The lookup table type. There can be one or more columns, for which values can be looked up for a given 'x-coordinate'.

Components

Type Visibility Attributes Name Initial
real(kind=dp), public, allocatable :: cols_rows(:,:)

The table in column-major order

real(kind=dp), public :: dx

The x-spacing in the lookup coordinate

real(kind=dp), public :: inv_dx

The inverse x-spacing

integer, public :: n_cols

The number of columns

integer, public :: n_points

The number of points

real(kind=dp), public, allocatable :: rows_cols(:,:)

The table in row-major order

real(kind=dp), public :: x_min

The minimum lookup coordinate


Functions

public function LT2_create(x_min, x_max, n_points, n_cols) result(my_lt)

This function returns a new lookup table

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x_min(2)

Minimum coordinate

real(kind=dp), intent(in) :: x_max(2)

Maximum coordinate

integer, intent(in) :: n_points(2)

How many values to store

integer, intent(in) :: n_cols

Number of variables that will be looked up

Return Value type(LT2_t)

public function LT2_create_from_data(x_min, x_max, spaced_data) result(my_lt)

This function returns a new lookup table from regularly spaced data

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x_min(2)

Minimum coordinate

real(kind=dp), intent(in) :: x_max(2)

Maximum coordinate

real(kind=dp), intent(in) :: spaced_data(:,:,:)

Input data of shape (nx, ny, n_cols)

Return Value type(LT2_t)

public elemental function LT2_get_col(my_lt, col_ix, x1, x2) result(col_value)

Get the value of a single column at x

Arguments

Type IntentOptional Attributes Name
type(LT2_t), intent(in) :: my_lt
integer, intent(in) :: col_ix
real(kind=dp), intent(in) :: x1
real(kind=dp), intent(in) :: x2

Return Value real(kind=dp)

public elemental function LT2_get_col_at_loc(my_lt, col_ix, loc) result(col_value)

Get the value of a single column at a location

Arguments

Type IntentOptional Attributes Name
type(LT2_t), intent(in) :: my_lt
integer, intent(in) :: col_ix
type(LT2_loc_t), intent(in) :: loc

Return Value real(kind=dp)

public elemental function LT2_get_loc(my_lt, x1, x2) result(my_loc)

Get a location in the lookup table

Arguments

Type IntentOptional Attributes Name
type(LT2_t), intent(in) :: my_lt
real(kind=dp), intent(in) :: x1
real(kind=dp), intent(in) :: x2

Return Value type(LT2_loc_t)

public function LT3_create(x_min, x_max, n_points, n_cols) result(my_lt)

This function returns a new lookup table

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x_min(3)

Minimum coordinate

real(kind=dp), intent(in) :: x_max(3)

Maximum coordinate

integer, intent(in) :: n_points(3)

How many values to store

integer, intent(in) :: n_cols

Number of variables that will be looked up

Return Value type(LT3_t)

public function LT3_create_from_data(x_min, x_max, spaced_data) result(my_lt)

This function returns a new lookup table from regularly spaced data

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x_min(3)

Minimum coordinate

real(kind=dp), intent(in) :: x_max(3)

Maximum coordinate

real(kind=dp), intent(in) :: spaced_data(:,:,:,:)

Input data of shape (nx, ny, nz, n_cols)

Return Value type(LT3_t)

public elemental function LT3_get_col(my_lt, col_ix, x1, x2, x3) result(col_value)

Get the value of a single column at x

Arguments

Type IntentOptional Attributes Name
type(LT3_t), intent(in) :: my_lt
integer, intent(in) :: col_ix
real(kind=dp), intent(in) :: x1
real(kind=dp), intent(in) :: x2
real(kind=dp), intent(in) :: x3

Return Value real(kind=dp)

public elemental function LT3_get_col_at_loc(my_lt, col_ix, loc) result(col_value)

Get the value of a single column at a location

Arguments

Type IntentOptional Attributes Name
type(LT3_t), intent(in) :: my_lt
integer, intent(in) :: col_ix
type(LT3_loc_t), intent(in) :: loc

Return Value real(kind=dp)

public elemental function LT3_get_loc(my_lt, x1, x2, x3) result(my_loc)

Get a location in the lookup table

Arguments

Type IntentOptional Attributes Name
type(LT3_t), intent(in) :: my_lt
real(kind=dp), intent(in) :: x1
real(kind=dp), intent(in) :: x2
real(kind=dp), intent(in) :: x3

Return Value type(LT3_loc_t)

public function LT_create(x_min, x_max, n_points, n_cols) result(my_lt)

This function returns a new lookup table

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x_min

Minimum x-coordinate

real(kind=dp), intent(in) :: x_max

Maximum x-coordinate

integer, intent(in) :: n_points

How many x-values to store

integer, intent(in) :: n_cols

Number of variables that will be looked up

Return Value type(LT_t)

public function LT_create_from_data(x_min, x_max, spaced_data) result(my_lt)

This function returns a new lookup table from regularly spaced data

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x_min

Minimum coordinate

real(kind=dp), intent(in) :: x_max

Maximum coordinate

real(kind=dp), intent(in) :: spaced_data(:,:)

Input data of shape (n_points, n_cols)

Return Value type(LT_t)

public elemental function LT_get_col(my_lt, col_ix, x) result(col_value)

Get the value of a single column at x

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(in) :: my_lt
integer, intent(in) :: col_ix
real(kind=dp), intent(in) :: x

Return Value real(kind=dp)

public elemental function LT_get_col_at_loc(my_lt, col_ix, loc) result(col_value)

Get the value of a single column at a location

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(in) :: my_lt
integer, intent(in) :: col_ix
type(LT_loc_t), intent(in) :: loc

Return Value real(kind=dp)

public elemental function LT_get_loc(my_lt, x) result(my_loc)

Get a location in the lookup table

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(in) :: my_lt
real(kind=dp), intent(in) :: x

Return Value type(LT_loc_t)

public pure function LT_get_mcol(my_lt, x) result(col_values)

Get the values of all columns at x

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(in) :: my_lt
real(kind=dp), intent(in) :: x

Return Value real(kind=dp), (my_lt%n_cols)

public pure function LT_get_mcol_at_loc(my_lt, loc) result(col_values)

Get the values of all columns at a location

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(in) :: my_lt
type(LT_loc_t), intent(in) :: loc

Return Value real(kind=dp), (my_lt%n_cols)

public pure function LT_get_spaced_data(in_x, in_y, new_x) result(out_yy)

Linearly interpolate the (x, y) input data to the new_x coordinates

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: in_x(:)
real(kind=dp), intent(in) :: in_y(:)
real(kind=dp), intent(in) :: new_x(:)

Return Value real(kind=dp), (size(new_x))

public pure function LT_get_xdata(x_min, dx, n_points) result(xdata)

Returns the x-coordinates of the lookup table

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x_min
real(kind=dp), intent(in) :: dx
integer, intent(in) :: n_points

Return Value real(kind=dp), (n_points)

public pure function find_index_adaptive(list, val) result(ix)

Adaptive search (combination of linear and binary search) of sorted list for the smallest ix such that list(ix) >= val. On failure, returns size(list)+1

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: list(:)

Sorted list

real(kind=dp), intent(in) :: val

Value to search for

Return Value integer

public pure function find_index_bsearch(list, val) result(ix)

Binary search of sorted list for the smallest ix such that list(ix) >= val. On failure, returns size(list)+1

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: list(:)

Sorted list

real(kind=dp), intent(in) :: val

Value to search for

Return Value integer

public pure function find_index_linear(list, val) result(ix)

Linear search of sorted list for the smallest ix such that list(ix) >= val. On failure, returns size(list)+1

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: list(:)

Sorted list

real(kind=dp), intent(in) :: val

Value to search for

Return Value integer


Subroutines

public pure subroutine LT2_set_col(my_lt, col_ix, x1, x2, y)

Fill the column with index col_ix using linearly interpolated data

Arguments

Type IntentOptional Attributes Name
type(LT2_t), intent(inout) :: my_lt
integer, intent(in) :: col_ix
real(kind=dp), intent(in) :: x1(:)
real(kind=dp), intent(in) :: x2(:)
real(kind=dp), intent(in) :: y(:,:)

public pure subroutine LT_add_col(my_lt, x, y)

Add a new column by linearly interpolating the (x, y) data

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(inout) :: my_lt
real(kind=dp), intent(in) :: x(:)
real(kind=dp), intent(in) :: y(:)

public subroutine LT_from_file(my_lt, filename)

Read the lookup table from file (in binary, potentially unportable)

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(inout) :: my_lt
character(len=*), intent(in) :: filename

public pure subroutine LT_get_data(my_lt, x_data, cols_rows)

Get the x-coordinates and the columns of the lookup table

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(in) :: my_lt
real(kind=dp), intent(out) :: x_data(:)
real(kind=dp), intent(out) :: cols_rows(:,:)

public pure subroutine LT_lin_interp_list(x_list, y_list, x_value, y_value)

Compute by use of linear interpolation the value in the middle

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x_list(:)
real(kind=dp), intent(in) :: y_list(:)
real(kind=dp), intent(in) :: x_value
real(kind=dp), intent(out) :: y_value

public pure subroutine LT_set_col(my_lt, col_ix, x, y)

Fill the column with index col_ix using the linearly interpolated (x, y) data

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(inout) :: my_lt
integer, intent(in) :: col_ix
real(kind=dp), intent(in) :: x(:)
real(kind=dp), intent(in) :: y(:)

public subroutine LT_to_file(my_lt, filename)

Write the lookup table to file (in binary, potentially unportable)

Arguments

Type IntentOptional Attributes Name
type(LT_t), intent(in) :: my_lt
character(len=*), intent(in) :: filename