13#ifndef CD_LOOKUPTABLEIMPLEM_H
14#define CD_LOOKUPTABLEIMPLEM_H
25#include <CD_NamespaceHeader.H>
27template <
typename T,
size_t N,
typename I>
33template <
typename T,
size_t N,
typename I>
38 m_grid = std::make_tuple(LookupTable::Spacing::Uniform, 0, -1.0, -1.0, -1.0);
39 m_rangeStrategyLo = LookupTable::OutOfRangeStrategy::Constant;
40 m_rangeStrategyHi = LookupTable::OutOfRangeStrategy::Constant;
43 m_structuredData.clear();
46template <
typename T,
size_t N,
typename I>
47template <
typename... Ts>
51 std::array<T,
sizeof...(Ts)> arr = {(T)x...};
53 m_rawData.emplace_back(arr);
56template <
typename T,
size_t N,
typename I>
60 m_rawData.emplace_back(x);
63template <
typename T,
size_t N,
typename I>
67 for (
auto& r : m_rawData) {
68 const auto tmp = r[a_columnOne];
70 r[a_columnOne] = r[a_columnTwo];
74 for (
auto& r : m_structuredData) {
75 const auto tmp = r[a_columnOne];
77 r[a_columnOne] = r[a_columnTwo];
82 if (a_columnOne == std::get<4>(m_grid)) {
83 std::get<4>(m_grid) = a_columnOne;
85 else if (a_columnTwo == std::get<4>(m_grid)) {
86 std::get<4>(m_grid) = a_columnTwo;
91template <
typename T,
size_t N,
typename I>
96 for (
auto& r : m_rawData) {
100 for (
auto& r : m_structuredData) {
104 if (m_isGood && K == std::get<2>(m_grid)) {
105 std::get<4>(m_grid) *= a_scale;
109template <
typename T,
size_t N,
typename I>
113 std::vector<std::array<T, N + 1>> truncatedData;
115 for (
const auto& xy : m_rawData) {
116 if (xy[a_column] >= a_min && xy[a_column] <= a_max) {
117 truncatedData.emplace_back(xy);
121 m_rawData = truncatedData;
124template <
typename T,
size_t N,
typename I>
128 m_rangeStrategyLo = a_strategy;
131template <
typename T,
size_t N,
typename I>
135 m_rangeStrategyHi = a_strategy;
138template <
typename T,
size_t N,
typename I>
141 const size_t& a_numPoints,
145 const std::string baseError =
"LookupTable1D<T,N,I>::prepareTable";
147 if (a_numPoints <= 1) {
148 throw std::runtime_error(baseError +
" - must have 'a_numPoints > 1'");
152 using Values = std::array<T, N + 1>;
153 std::vector<Values> sortedData = m_rawData;
155 std::sort(sortedData.begin(),
157 [i = a_independentVariable](
const Values& X1,
const Values& X2) ->
bool {
158 return X1[i] < X2[i];
166 xmin = sortedData.front()[a_independentVariable];
167 xmax = sortedData.back()[a_independentVariable];
169 std::vector<T> coords(a_numPoints);
172 case LookupTable::Spacing::Uniform: {
173 delta = (xmax - xmin) / (a_numPoints - 1);
175 for (
size_t i = 0; i < a_numPoints; i++) {
176 coords[i] = xmin + i * delta;
181 case LookupTable::Spacing::Exponential: {
182 if (xmin <= std::numeric_limits<T>::min()) {
183 throw std::runtime_error(baseError +
" - but must have all 'x > 0.0' for logarithmic grid");
187 delta = log10(xmax / xmin) / (a_numPoints - 1);
189 for (
int i = 0; i < a_numPoints; i++) {
190 coords[i] = xmin * std::pow(10.0, i * delta);
197 throw std::runtime_error(baseError +
" - logic bust (unsupported table spacing system)");
203 m_structuredData.clear();
207 for (
const auto& x : coords) {
208 while (curRow < m_rawData.size() - 1) {
209 const auto& row1 = m_rawData[curRow];
210 const auto& row2 = m_rawData[curRow + 1];
212 const auto x1 = row1[a_independentVariable];
213 const auto x2 = row2[a_independentVariable];
215 if (x >= x1 && x <= x2) {
216 const auto t = (x - x1) / (x2 - x1);
218 std::array<T, N + 1> interpRow;
220 for (
size_t i = 0; i <= N; i++) {
221 interpRow[i] = row1[i] + t * (row2[i] - row1[i]);
224 m_structuredData.emplace_back(interpRow);
233 m_grid = std::make_tuple(a_spacing, a_independentVariable, xmin, xmax, delta);
238template <
typename T,
size_t N,
typename I>
239inline std::vector<std::array<T, N + 1>>&
245template <
typename T,
size_t N,
typename I>
246inline std::vector<std::array<T, N + 1>>&
249 return (m_structuredData);
252template <
typename T,
size_t N,
typename I>
253inline const std::vector<std::array<T, N + 1>>&
259template <
typename T,
size_t N,
typename I>
260inline const std::vector<std::array<T, N + 1>>&
263 return (m_structuredData);
266template <
typename T,
size_t N,
typename I>
270 this->writeToFile(a_file, m_rawData);
273template <
typename T,
size_t N,
typename I>
277 this->writeToFile(a_file, m_structuredData);
280template <
typename T,
size_t N,
typename I>
284 this->outputData(a_ostream, m_rawData);
287template <
typename T,
size_t N,
typename I>
291 this->outputData(a_ostream, m_structuredData);
294template <
typename T,
size_t N,
typename I>
297 const std::vector<std::array<T, N + 1>>& a_data)
const noexcept
299 for (
const auto& r : a_data) {
300 for (
const auto& c : r) {
301 a_ostream << std::left << std::setw(14) << c;
307template <
typename T,
size_t N,
typename I>
310 const std::vector<std::array<T, N + 1>>& a_data)
const noexcept
319 this->outputData(file, a_data);
326template <
typename T,
size_t N,
typename I>
331 const T& x0 = std::get<2>(m_grid);
332 const T& delta = std::get<4>(m_grid);
337 case LookupTable::Spacing::Uniform: {
338 idxLo = std::floor((a_x - x0) / delta);
342 case LookupTable::Spacing::Exponential: {
343 idxLo = std::floor(log10(a_x / x0) / delta);
348 throw std::runtime_error(
"LookupTable1D<T,N,I>::getIndexLo - logic bust (unsupported table spacing system)");
355template <
typename T,
size_t N,
typename I>
356inline std::array<T, N + 1>
360 throw std::runtime_error(
"LookupTable1D<T, N, I>::interpolate(array) but need to call 'prepareTable first'");
363 const size_t& indVar = std::get<1>(m_grid);
364 const T& xmin = std::get<2>(m_grid);
365 const T& xmax = std::get<3>(m_grid);
367 std::array<T, N + 1> ret;
370 switch (m_rangeStrategyLo) {
371 case LookupTable::OutOfRangeStrategy::Constant: {
372 ret = m_structuredData.front();
376 case LookupTable::OutOfRangeStrategy::Interpolate: {
377 const T diffDx = m_structuredData[1][indVar] - m_structuredData[0][indVar];
378 const T extrapDx = m_structuredData[0][indVar] - a_x;
380 ret = m_structuredData.front();
382 for (
size_t i = 0; i < N + 1; i++) {
383 const T slope = (m_structuredData[1][i] - m_structuredData[0][i]) / diffDx;
385 ret[i] -= slope * extrapDx;
391 throw std::runtime_error(
"LookupTable1D<T, N, I>::interpolate(array) unsupported range strategy at low end");
397 else if (a_x > xmax) {
398 switch (m_rangeStrategyHi) {
399 case LookupTable::OutOfRangeStrategy::Constant: {
400 ret = m_structuredData.back();
404 case LookupTable::OutOfRangeStrategy::Interpolate: {
405 const size_t& size = m_structuredData.size();
407 const T diffDx = m_structuredData[size - 1][indVar] - m_structuredData[size - 2][indVar];
408 const T extrapDx = a_x - m_structuredData[size - 1][indVar];
410 ret = m_structuredData.back();
412 for (
size_t i = 0; i < N + 1; i++) {
413 const T slope = (m_structuredData[size - 1][i] - m_structuredData[size - 2][i]) / diffDx;
415 ret[i] += slope * extrapDx;
421 throw std::runtime_error(
"LookupTable1D<T, N, I>::interpolate(array) unsupported range strategy at low end");
428 const size_t idx = this->getIndexLo(a_x);
430 const T diffDx = m_structuredData[idx + 1][indVar] - m_structuredData[idx][indVar];
431 const T interpDx = a_x - m_structuredData[idx][indVar];
433 ret = m_structuredData[idx];
435 for (
size_t i = 0; i < N + 1; i++) {
436 const T slope = (m_structuredData[idx + 1][i] - m_structuredData[idx][i]) / diffDx;
438 ret[i] += slope * interpDx;
445template <
typename T,
size_t N,
typename I>
450 return std::get<K>(this->interpolate(a_x));
453template <
typename T,
size_t N,
typename I>
454template <
size_t M,
typename... Ts>
458 static_assert(M <= N,
"LookupTable1D<T, N,I>::slice must have have 'M <= N'");
460 std::array<size_t,
sizeof...(Ts)> arr = {(size_t)a_columns...};
464 for (
const auto& r : m_rawData) {
465 std::array<T, M + 1> slicedData;
467 for (
size_t i = 0; i < M + 1; i++) {
468 slicedData[i] = r[arr[i]];
477#include <CD_NamespaceFooter.H>
Declaration of a lookup table in one independent variable.
OutOfRangeStrategy
Strategy for obtaining data when requesting data that is out of range. We can use either constant (i....
Definition CD_LookupTable.H:33
Spacing
Enum for classifying the coordinate system.
Definition CD_LookupTable.H:42
Class for interpolation of f = f(x) data in one independent variable x.
Definition CD_LookupTable1D.H:32
std::vector< std::array< T, N+1 > > & getStructuredData() noexcept
Access function for structured data.
Definition CD_LookupTable1DImplem.H:247
T interpolate(const T &a_x) const
Interpolation function for specific dependent variable K.
Definition CD_LookupTable1DImplem.H:448
void addData(const Ts &... x) noexcept
Add entry.
Definition CD_LookupTable1DImplem.H:49
void swap(size_t a_columnOne, size_t a_columnTwo) noexcept
Utility function for swapping columns.
Definition CD_LookupTable1DImplem.H:65
void setRangeStrategyLo(const LookupTable::OutOfRangeStrategy &a_strategy) noexcept
Set the out-of-range strategy on the low end.
Definition CD_LookupTable1DImplem.H:126
void truncate(const T &a_min, const T &a_max, size_t a_column) noexcept
Utility function for truncating raw data along one of the variables (either dependent or independent)...
Definition CD_LookupTable1DImplem.H:111
size_t getIndexLo(const T &a_x) const
Get the lower index that brackets the input variable between two data points in the structured grid.
Definition CD_LookupTable1DImplem.H:328
void setRangeStrategyHi(const LookupTable::OutOfRangeStrategy &a_strategy) noexcept
Set the out-of-range strategy on the high end.
Definition CD_LookupTable1DImplem.H:133
LookupTable1D() noexcept
Default constructor. Creates a table without any entries.
Definition CD_LookupTable1DImplem.H:28
void writeRawData(const std::string &a_file) const noexcept
Dump raw table data to file.
Definition CD_LookupTable1DImplem.H:268
void outputStructuredData(std::ostream &a_ostream=std::cout) const noexcept
Dump structured table data to file.
Definition CD_LookupTable1DImplem.H:289
void reset() noexcept
Reset everything.
Definition CD_LookupTable1DImplem.H:35
void outputRawData(std::ostream &a_ostream=std::cout) const noexcept
Dump raw table data to output stream.
Definition CD_LookupTable1DImplem.H:282
void prepareTable(const size_t &a_independentVariable, const size_t &a_numPoints, const LookupTable::Spacing &a_spacing)
Turn the raw data into uniform data for fast lookup.
Definition CD_LookupTable1DImplem.H:140
void scale(const T &a_scale) noexcept
Utility function which scales one of the columns (either dependent or independent variable)
Definition CD_LookupTable1DImplem.H:94
void writeToFile(const std::string &a_file, const std::vector< std::array< T, N+1 > > &a_data) const noexcept
Utility function for outputting data to a file.
Definition CD_LookupTable1DImplem.H:309
void writeStructuredData(const std::string &a_file) const noexcept
Dump structured table data to file.
Definition CD_LookupTable1DImplem.H:275
LookupTable1D< T, M, I > slice(const Ts &... a_columns) const noexcept
Slice this table, keeping only the user-specified columns.
Definition CD_LookupTable1DImplem.H:456
void outputData(std::ostream &a_ostream, const std::vector< std::array< T, N+1 > > &a_data) const noexcept
Utility function for outputting data.
Definition CD_LookupTable1DImplem.H:296
std::vector< std::array< T, N+1 > > & getRawData() noexcept
Access function for raw data.
Definition CD_LookupTable1DImplem.H:240