Version: SMASH-3.4
smash::Tabulation Class Reference

#include <tabulation.h>

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

Definition at line 30 of file tabulation.h.

Collaboration diagram for smash::Tabulation:
[legend]

Public Member Functions

 Tabulation ()
 Construct an empty tabulation object. More...
 
 Tabulation (double x_min, double range, size_t num, std::function< double(double)> f)
 Construct a new tabulation object. More...
 
bool is_empty () const
 
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, ExtrapolationType extrapolation=ExtrapolationType::Linear) const
 Look up a value from the tabulation using linear interpolation. More...
 
void write (std::ofstream &stream, sha256::Hash hash) const
 Write a binary representation of the tabulation to a stream. More...
 

Static Public Member Functions

static Tabulation from_file (std::ifstream &stream, sha256::Hash hash)
 Construct a tabulation object by reading binary data from a stream. More...
 

Private Member Functions

double linear_approximation_ (double x) const
 Linear approximation to interpolate and extrapolate tabulations. More...
 

Private Attributes

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

Constructor & Destructor Documentation

◆ Tabulation() [1/2]

smash::Tabulation::Tabulation ( )
inline

Construct an empty tabulation object.

Definition at line 35 of file tabulation.h.

35 : values_({}), x_min_(0.0), x_max_(0.0), inv_dx_(0.0) {}
double x_min_
lower bound for tabulation
Definition: tabulation.h:111
double inv_dx_
inverse step size 1/dx
Definition: tabulation.h:117
double x_max_
upper bound for tabulation
Definition: tabulation.h:114
std::vector< double > values_
vector for storing tabulated values
Definition: tabulation.h:108

◆ Tabulation() [2/2]

smash::Tabulation::Tabulation ( double  x_min,
double  range,
size_t  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
Returns
Construct object.
Exceptions
ifless than two values are tabulated.

Definition at line 14 of file tabulation.cc.

16  : x_min_(x_min), x_max_(x_min + range), inv_dx_(num / range) {
17  if (num < 2) {
18  throw std::runtime_error("Tabulation needs at least two values");
19  }
20  values_.resize(num + 1);
21  const double dx = range / num;
22  for (size_t i = 0; i <= num; i++) {
23  values_[i] = f(x_min_ + i * dx);
24  }
25 }

Member Function Documentation

◆ is_empty()

bool smash::Tabulation::is_empty ( ) const
inline
Returns
whether the tabulation is empty.

Definition at line 54 of file tabulation.h.

54 { return values_.empty(); }
Here is the caller graph for this function:

◆ from_file()

Tabulation smash::Tabulation::from_file ( std::ifstream &  stream,
sha256::Hash  hash 
)
static

Construct a tabulation object by reading binary data from a stream.

Parameters
streamStream containing the binary representation of the tabulation.
hashHash corresponding to the particle properties for which the tabulation was created.
Returns
(true, tabulation) if the given hash matches the one given by the stream, (false, empty) otherwise.

Definition at line 176 of file tabulation.cc.

176  {
177  sha256::Hash hash_from_stream = sread_hash(stream);
178  Tabulation t;
179  if (hash != hash_from_stream) {
180  return t;
181  }
182  t.x_min_ = sread_double(stream);
183  t.x_max_ = sread_double(stream);
184  t.inv_dx_ = sread_double(stream);
185  t.values_ = sread_vector(stream);
186  return t;
187 }
Tabulation()
Construct an empty tabulation object.
Definition: tabulation.h:35
std::array< uint8_t, HASH_SIZE > Hash
A SHA256 hash.
Definition: sha256.h:25
static std::vector< double > sread_vector(std::ifstream &stream)
Read binary representation of a vector of doubles.
Definition: tabulation.cc:136
static double sread_double(std::ifstream &stream)
Read binary representation of a double.
Definition: tabulation.cc:81
static sha256::Hash sread_hash(std::ifstream &stream)
Read binary representation of a SHA256 hash.
Definition: tabulation.cc:162
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 27 of file tabulation.cc.

27  {
28  if (x < x_min_) {
29  return 0.;
30  }
31  const unsigned int n =
32  numeric_cast<unsigned int>(std::floor((x - x_min_) * inv_dx_ + 0.5));
33  if (n >= values_.size()) {
34  return values_.back();
35  } else {
36  return values_[n];
37  }
38 }
constexpr int n
Neutron.

◆ get_value_linear()

double smash::Tabulation::get_value_linear ( double  x,
ExtrapolationType  extrapolation = ExtrapolationType::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 type that is used for values greater than the maximum x in the tabulation.
Returns
Tabulated value using linear interpolation.
Exceptions
std::invalid_argumentif unsupported extrapolation type is requested.

Definition at line 40 of file tabulation.cc.

41  {
42  if (x < x_min_) {
43  return 0.;
44  }
45  switch (extrapolation_type_for_x_beyond_xmax) {
47  if (x > x_max_) {
48  return 0.;
49  }
50  [[fallthrough]];
52  if (x > x_max_) {
53  return values_.back();
54  }
55  [[fallthrough]];
57  return linear_approximation_(x);
58  default:
59  throw std::invalid_argument(
60  "The provided extrapolation type is not supported. Valid types are "
61  "'Zero', 'Constant', and 'Linear'.");
62  }
63 }
double linear_approximation_(double x) const
Linear approximation to interpolate and extrapolate tabulations.
Definition: tabulation.h:120
@ Linear
Extrapolate using a linear approach.
@ Constant
Extrapolate using a constant value.
@ Zero
Extrapolate with zero.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write()

void smash::Tabulation::write ( std::ofstream &  stream,
sha256::Hash  hash 
) const

Write a binary representation of the tabulation to a stream.

Parameters
streamStream to which the binary representation is written.
hashHash corresponding to the particle properties for which the tabulation was created.

Definition at line 168 of file tabulation.cc.

168  {
169  swrite(stream, hash);
170  swrite(stream, x_min_);
171  swrite(stream, x_max_);
172  swrite(stream, inv_dx_);
173  swrite(stream, values_);
174 }
static void swrite(std::ofstream &stream, double x)
Write binary representation to stream.
Definition: tabulation.cc:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ linear_approximation_()

double smash::Tabulation::linear_approximation_ ( double  x) const
inlineprivate

Linear approximation to interpolate and extrapolate tabulations.

Definition at line 120 of file tabulation.h.

120  {
121  const double index_double = (x - x_min_) * inv_dx_;
122  // here n is the lower index
123  const size_t n =
124  std::min(static_cast<size_t>(index_double), values_.size() - 2);
125  const double r = index_double - n;
126  return values_[n] + (values_[n + 1] - values_[n]) * r;
127  }
Here is the caller graph for this function:

Member Data Documentation

◆ values_

std::vector<double> smash::Tabulation::values_ {}
private

vector for storing tabulated values

Definition at line 108 of file tabulation.h.

◆ x_min_

double smash::Tabulation::x_min_ = smash_NaN<double>
private

lower bound for tabulation

Definition at line 111 of file tabulation.h.

◆ x_max_

double smash::Tabulation::x_max_ = smash_NaN<double>
private

upper bound for tabulation

Definition at line 114 of file tabulation.h.

◆ inv_dx_

double smash::Tabulation::inv_dx_ = smash_NaN<double>
private

inverse step size 1/dx

Definition at line 117 of file tabulation.h.


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