Version: SMASH-3.1
smash::Tabulation Class Reference

#include <tabulation.h>

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

Definition at line 35 of file tabulation.h.

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, Extrapolation extrapolation=Extrapolation::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...
 

Protected Attributes

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

Constructor & Destructor Documentation

◆ Tabulation() [1/2]

smash::Tabulation::Tabulation ( )
inline

Construct an empty tabulation object.

Definition at line 40 of file tabulation.h.

40 : values_({}), x_min_(0.0), x_max_(0.0), inv_dx_(0.0) {}
double x_min_
lower bound for tabulation
Definition: tabulation.h:113
double inv_dx_
inverse step size 1/dx
Definition: tabulation.h:119
double x_max_
upper bound for tabulation
Definition: tabulation.h:116
std::vector< double > values_
vector for storing tabulated values
Definition: tabulation.h:110

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

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

Member Function Documentation

◆ is_empty()

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

Definition at line 59 of file tabulation.h.

59 { return values_.empty(); }

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

167  {
168  sha256::Hash hash_from_stream = sread_hash(stream);
169  Tabulation t;
170  if (hash != hash_from_stream) {
171  return t;
172  }
173  t.x_min_ = sread_double(stream);
174  t.x_max_ = sread_double(stream);
175  t.inv_dx_ = sread_double(stream);
176  t.values_ = sread_vector(stream);
177  return t;
178 }
Tabulation()
Construct an empty tabulation object.
Definition: tabulation.h:40
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:127
static double sread_double(std::ifstream &stream)
Read binary representation of a double.
Definition: tabulation.cc:72
static sha256::Hash sread_hash(std::ifstream &stream)
Read binary representation of a SHA256 hash.
Definition: tabulation.cc:153

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

25  {
26  if (x < x_min_) {
27  return 0.;
28  }
29  // this rounds correctly because double -> int conversion truncates
30  const unsigned int n = (x - x_min_) * inv_dx_ + 0.5;
31  if (n >= values_.size()) {
32  return values_.back();
33  } else {
34  return values_[n];
35  }
36 }
constexpr int n
Neutron.

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

38  {
39  if (x < x_min_) {
40  return 0.;
41  }
42  if (extrapol == Extrapolation::Zero && x > x_max_) {
43  return 0.0;
44  }
45  if (extrapol == Extrapolation::Const && x > x_max_) {
46  return values_.back();
47  }
48  const double index_double = (x - x_min_) * inv_dx_;
49  // here n is the lower index
50  const size_t n =
51  std::min(static_cast<size_t>(index_double), values_.size() - 2);
52  const double r = index_double - n;
53  return values_[n] + (values_[n + 1] - values_[n]) * r;
54 }

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

159  {
160  swrite(stream, hash);
161  swrite(stream, x_min_);
162  swrite(stream, x_max_);
163  swrite(stream, inv_dx_);
164  swrite(stream, values_);
165 }
static void swrite(std::ofstream &stream, double x)
Write binary representation to stream.
Definition: tabulation.cc:62

Member Data Documentation

◆ values_

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

vector for storing tabulated values

Definition at line 110 of file tabulation.h.

◆ x_min_

double smash::Tabulation::x_min_
protected

lower bound for tabulation

Definition at line 113 of file tabulation.h.

◆ x_max_

double smash::Tabulation::x_max_
protected

upper bound for tabulation

Definition at line 116 of file tabulation.h.

◆ inv_dx_

double smash::Tabulation::inv_dx_
protected

inverse step size 1/dx

Definition at line 119 of file tabulation.h.


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