Nalu
Nalu: a generalized unstructured massively parallel low Mach flow code designed to support a variety of energy applications of interest (most notably Wind ECP) built on the Sierra Toolkit and Trilinos solver Tpetra/Epetra stack. The open source BSD, clause 3 license model has been chosen for the code base. See LICENSE for more information. http://NaluCFD.org
LinearInterpolation.h
Go to the documentation of this file.
1 #ifndef LINEARINTERPOLATION_H
2 #define LINEARINTERPOLATION_H
3 
4 #include <iostream>
5 #include <vector>
6 #include <stdexcept>
7 #include <utility>
8 
9 namespace sierra {
10 namespace nalu {
11 namespace utils {
12 
14 {
16  enum boundLimits {
17  LOWLIM = -2,
18  UPLIM = -1,
19  VALID = 0
20  };
21 
23  enum OobAction {
24  ERROR = 0,
25  WARN,
28  };
29 };
30 
31 template <typename T>
32 using Array1D = std::vector<T>;
33 
34 template <typename T>
36 {
37  typedef typename Array1D<T>::size_type size_type;
38  typedef typename std::pair<OutOfBounds::boundLimits, size_type> index_type;
39 };
40 
45 template <typename T>
46 inline typename InterpTraits<T>::index_type
47 check_bounds(const Array1D<T>& xinp, const T& x)
48 {
49  auto sz = xinp.size();
50 
51  if (sz < 2) {
52  throw std::runtime_error(
53  "Interpolation table contains less than 2 entries.");
54  }
55 
56  if (x < xinp[0]) {
57  return std::make_pair(OutOfBounds::LOWLIM, 0);
58  } else if (x > xinp[sz - 1]) {
59  return std::make_pair(OutOfBounds::UPLIM, sz - 1);
60  } else {
61  return std::make_pair(OutOfBounds::VALID, 0);
62  }
63 }
64 
73 template <typename T>
74 inline typename InterpTraits<T>::index_type
75 find_index(const Array1D<T>& xinp, const T& x)
76 {
77  auto idx = check_bounds(xinp, x);
78  if (idx.first == OutOfBounds::UPLIM || idx.first == OutOfBounds::LOWLIM)
79  return idx;
80 
81  auto sz = xinp.size();
82  for (size_t i = 1; i < sz; i++) {
83  if (x <= xinp[i]) {
84  idx.second = i - 1;
85  break;
86  }
87  }
88  return idx;
89 }
90 
94 template <typename T>
95 void
97  const Array1D<T>& xinp,
98  const Array1D<T>& yinp,
99  const T& xout,
100  T& yout,
102 {
103  auto idx = find_index(xinp, xout);
104 
105  switch (idx.first) {
106  case OutOfBounds::LOWLIM:
107  case OutOfBounds::UPLIM: {
108  switch (oob) {
109  case OutOfBounds::ERROR:
110  throw std::runtime_error("Out of bounds error in interpolation");
111  break;
112 
113  case OutOfBounds::WARN:
114  std::cout
115  << std::endl
116  << "WARNING: Out of bound values encountered during interpolation"
117  << std::endl;
118  // no break here... allow fallthrough
119 
120  case OutOfBounds::CLAMP:
121  yout = yinp[idx.second];
122  break;
123 
125  auto ii = idx.second;
126  if ((ii + 1) == xinp.size())
127  --ii;
128 
129  yout = yinp[ii] +
130  (yinp[ii + 1] - yinp[ii]) / (xinp[ii + 1] - xinp[ii]) *
131  (xout - xinp[ii]);
132  break;
133  }
134  }
135  break;
136  }
137  case OutOfBounds::VALID:
138  auto j = idx.second;
139  T fac = (xout - xinp[j]) / (xinp[j + 1] - xinp[j]);
140  yout = (static_cast<T>(1.0) - fac) * yinp[j] + fac * yinp[j + 1];
141  break;
142  }
143 }
144 
145 } // namespace utils
146 } // namespace nalu
147 } // namespace sierra
148 
149 #endif /* LINEARINTERPOLATION_H */
boundLimits
Out of bounds limit types.
Definition: LinearInterpolation.h:16
Definition: ABLForcingAlgorithm.C:26
Clamp values to the end points.
Definition: LinearInterpolation.h:26
void linear_interp(const Array1D< T > &xinp, const Array1D< T > &yinp, const T &xout, T &yout, OutOfBounds::OobAction oob=OutOfBounds::CLAMP)
Perform a 1-D linear interpolation.
Definition: LinearInterpolation.h:96
xtgt > xarray[N]
Definition: LinearInterpolation.h:18
std::pair< OutOfBounds::boundLimits, size_type > index_type
Definition: LinearInterpolation.h:38
Array1D< T >::size_type size_type
Definition: LinearInterpolation.h:37
xtgt < xarray[0]
Definition: LinearInterpolation.h:17
InterpTraits< T >::index_type check_bounds(const Array1D< T > &xinp, const T &x)
Determine whether the given value is within the limits of the interpolation table.
Definition: LinearInterpolation.h:47
InterpTraits< T >::index_type find_index(const Array1D< T > &xinp, const T &x)
Return an index object corresponding to the x-value based on interpolation table. ...
Definition: LinearInterpolation.h:75
xarray[0] <= xtgt <= xarray[N]
Definition: LinearInterpolation.h:19
OobAction
Flags indicating action to perform on Out of Bounds situation.
Definition: LinearInterpolation.h:23
Definition: LinearInterpolation.h:35
Warn and then CLAMP.
Definition: LinearInterpolation.h:25
Extrapolate linearly based on end point.
Definition: LinearInterpolation.h:27
std::vector< T > Array1D
Definition: LinearInterpolation.h:32
Raise runtime error.
Definition: LinearInterpolation.h:24
Definition: LinearInterpolation.h:13