Version: SMASH-1.5
tabulation.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 
8 #include "smash/tabulation.h"
9 
10 namespace smash {
11 
12 Tabulation::Tabulation(double x_min, double range, int num,
13  std::function<double(double)> f)
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 }
21 
22 double Tabulation::get_value_step(double x) const {
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 }
34 
35 double Tabulation::get_value_linear(double x, Extrapolation extrapol) const {
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 }
52 
53 } // namespace smash
double get_value_linear(double x, Extrapolation extrapolation=Extrapolation::Linear) const
Look up a value from the tabulation using linear interpolation.
Definition: tabulation.cc:35
Extrapolation
The kind of extrapolation used by the tabulation.
Definition: tabulation.h:24
Tabulation(double x_min, double range, int num, std::function< double(double)> f)
Construct a new tabulation object.
Definition: tabulation.cc:12
const double x_min_
lower bound for tabulation
Definition: tabulation.h:80
const double x_max_
upper bound for tabulation
Definition: tabulation.h:83
double get_value_step(double x) const
Look up a value from the tabulation (without any interpolation, simply using the closest tabulated va...
Definition: tabulation.cc:22
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
Definition: action.h:24