Version: SMASH-1.5
smash::Tabulation Class Reference

#include <tabulation.h>

A class for storing a one-dimensional lookup table of floating-point values.

Definition at line 33 of file tabulation.h.

Collaboration diagram for smash::Tabulation:
[legend]

Public Member Functions

 Tabulation (double x_min, double range, int num, std::function< double(double)> f)
 Construct a new tabulation object. More...
 
double get_value_step (double x) const
 Look up a value from the tabulation (without any interpolation, simply using the closest tabulated value). More...
 
double get_value_linear (double x, Extrapolation extrapolation=Extrapolation::Linear) const
 Look up a value from the tabulation using linear interpolation. More...
 

Protected Attributes

std::vector< double > values_
 vector for storing tabulated values More...
 
const double x_min_
 lower bound for tabulation More...
 
const double x_max_
 upper bound for tabulation More...
 
const double inv_dx_
 inverse step size 1/dx More...
 

Constructor & Destructor Documentation

◆ Tabulation()

smash::Tabulation::Tabulation ( double  x_min,
double  range,
int  num,
std::function< double(double)>  f 
)

Construct a new tabulation object.

Parameters
x_minlower bound of tabulation domain
rangerange (x_max-x_min) of tabulation domain
numnumber of intervals (the number of tabulated points is actually num+1)
fone-dimensional function f(x) which is supposed to be tabulated

Definition at line 12 of file tabulation.cc.

14  : x_min_(x_min), x_max_(x_min + range), inv_dx_(num / range) {
15  values_.resize(num + 1);
16  const double dx = range / num;
17  for (int i = 0; i <= num; i++) {
18  values_[i] = f(x_min_ + i * dx);
19  }
20 }
const double x_min_
lower bound for tabulation
Definition: tabulation.h:80
const double x_max_
upper bound for tabulation
Definition: tabulation.h:83
const double inv_dx_
inverse step size 1/dx
Definition: tabulation.h:86
std::vector< double > values_
vector for storing tabulated values
Definition: tabulation.h:77

Member Function Documentation

◆ get_value_step()

double smash::Tabulation::get_value_step ( double  x) const

Look up a value from the tabulation (without any interpolation, simply using the closest tabulated value).

If

x is below the lower tabulation
bound we return 0, if it is above the upper bound we return the tabulated value at the upper bound.
Parameters
xArgument to tabulated function.
Returns
Tabulated value using constant interpolation.

Definition at line 22 of file tabulation.cc.

22  {
23  if (x < x_min_) {
24  return 0.;
25  }
26  // this rounds correctly because double -> int conversion truncates
27  const unsigned int n = (x - x_min_) * inv_dx_ + 0.5;
28  if (n >= values_.size()) {
29  return values_.back();
30  } else {
31  return values_[n];
32  }
33 }
const double x_min_
lower bound for tabulation
Definition: tabulation.h:80
constexpr int n
Neutron.
const double inv_dx_
inverse step size 1/dx
Definition: tabulation.h:86
std::vector< double > values_
vector for storing tabulated values
Definition: tabulation.h:77

◆ get_value_linear()

double smash::Tabulation::get_value_linear ( double  x,
Extrapolation  extrapolation = Extrapolation::Linear 
) const

Look up a value from the tabulation using linear interpolation.

If

x is below the lower bound we return 0.
If x is above the upper bound, then by default we use linear extrapolation of the two highest tabulated points. Optionally one can also extrapolate with rightmost value or zero. Linear extrapolation is not an arbitrary choice, in fact many functions tabulated in SMASH have a linear asymptotics, e.g. rho(m) functions.
Parameters
xArgument to tabulated function.
extrapolationExtrapolation that should be used for values outside the tabulation.
Returns
Tabulated value using linear interpolation.

Definition at line 35 of file tabulation.cc.

35  {
36  if (x < x_min_) {
37  return 0.;
38  }
39  if (extrapol == Extrapolation::Zero && x > x_max_) {
40  return 0.0;
41  }
42  if (extrapol == Extrapolation::Const && x > x_max_) {
43  return values_.back();
44  }
45  const double index_double = (x - x_min_) * inv_dx_;
46  // here n is the lower index
47  const size_t n =
48  std::min(static_cast<size_t>(index_double), values_.size() - 2);
49  const double r = index_double - n;
50  return values_[n] + (values_[n + 1] - values_[n]) * r;
51 }
const double x_min_
lower bound for tabulation
Definition: tabulation.h:80
const double x_max_
upper bound for tabulation
Definition: tabulation.h:83
constexpr int n
Neutron.
const double inv_dx_
inverse step size 1/dx
Definition: tabulation.h:86
std::vector< double > values_
vector for storing tabulated values
Definition: tabulation.h:77

Member Data Documentation

◆ values_

std::vector<double> smash::Tabulation::values_
protected

vector for storing tabulated values

Definition at line 77 of file tabulation.h.

◆ x_min_

const double smash::Tabulation::x_min_
protected

lower bound for tabulation

Definition at line 80 of file tabulation.h.

◆ x_max_

const double smash::Tabulation::x_max_
protected

upper bound for tabulation

Definition at line 83 of file tabulation.h.

◆ inv_dx_

const double smash::Tabulation::inv_dx_
protected

inverse step size 1/dx

Definition at line 86 of file tabulation.h.


The documentation for this class was generated from the following files: