12 #ifndef CD_LookupTableImplem_H
13 #define CD_LookupTableImplem_H
25 template <
typename T,
size_t N,
typename I>
31 template <
typename T,
size_t N,
typename I>
36 m_grid = std::make_tuple(LookupTable::Spacing::Uniform, 0, -1.0, -1.0, -1.0);
37 m_rangeStrategyLo = LookupTable::OutOfRangeStrategy::Constant;
38 m_rangeStrategyHi = LookupTable::OutOfRangeStrategy::Constant;
41 m_structuredData.clear();
44 template <
typename T,
size_t N,
typename I>
45 template <
typename... Ts>
49 std::array<T,
sizeof...(Ts)> arr = {(T)x...};
51 m_rawData.emplace_back(arr);
54 template <
typename T,
size_t N,
typename I>
58 m_rawData.emplace_back(x);
61 template <
typename T,
size_t N,
typename I>
65 for (
auto& r : m_rawData) {
66 const auto tmp = r[a_columnOne];
68 r[a_columnOne] = r[a_columnTwo];
72 for (
auto& r : m_structuredData) {
73 const auto tmp = r[a_columnOne];
75 r[a_columnOne] = r[a_columnTwo];
80 if (a_columnOne == std::get<4>(m_grid)) {
81 std::get<4>(m_grid) = a_columnOne;
83 else if (a_columnTwo == std::get<4>(m_grid)) {
84 std::get<4>(m_grid) = a_columnTwo;
89 template <
typename T,
size_t N,
typename I>
94 for (
auto& r : m_rawData) {
98 for (
auto& r : m_structuredData) {
102 if (m_isGood && K == std::get<2>(m_grid)) {
103 std::get<4>(m_grid) *= a_scale;
107 template <
typename T,
size_t N,
typename I>
111 std::vector<std::array<T, N + 1>> truncatedData;
113 for (
const auto& xy : m_rawData) {
114 if (xy[a_column] >= a_min || xy[a_column] <= a_max) {
115 truncatedData.emplace_back(xy);
119 m_rawData = truncatedData;
122 template <
typename T,
size_t N,
typename I>
126 m_rangeStrategyLo = a_strategy;
129 template <
typename T,
size_t N,
typename I>
133 m_rangeStrategyHi = a_strategy;
136 template <
typename T,
size_t N,
typename I>
139 const size_t& a_numPoints,
143 const std::string baseError =
"LookupTable1D<T,N,I>::prepareTable";
145 if (a_numPoints <= 1) {
146 throw std::runtime_error(baseError +
" - must have 'a_numPoints > 1'");
150 using Values = std::array<T, N + 1>;
151 std::vector<Values> sortedData = m_rawData;
153 std::sort(sortedData.begin(),
155 [i = a_independentVariable](
const Values& X1,
const Values& X2) ->
bool {
156 return X1[i] < X2[i];
164 xmin = sortedData.front()[a_independentVariable];
165 xmax = sortedData.back()[a_independentVariable];
167 std::vector<T> coords(a_numPoints);
170 case LookupTable::Spacing::Uniform: {
171 delta = (xmax - xmin) / (a_numPoints - 1);
173 for (
size_t i = 0; i < a_numPoints; i++) {
174 coords[i] = xmin + i * delta;
179 case LookupTable::Spacing::Exponential: {
181 throw std::runtime_error(baseError +
" - but must have all 'x > 0.0' for logarithmic grid");
185 delta = log10(xmax / xmin) / (a_numPoints - 1);
187 for (
int i = 0; i < a_numPoints; i++) {
188 coords[i] = xmin * std::pow(10.0, i * delta);
195 throw std::runtime_error(baseError +
" - logic bust (unsupported table spacing system)");
201 m_structuredData.clear();
205 for (
const auto& x : coords) {
206 while (curRow < m_rawData.size() - 1) {
207 const auto& row1 = m_rawData[curRow];
208 const auto& row2 = m_rawData[curRow + 1];
210 const auto x1 = row1[a_independentVariable];
211 const auto x2 = row2[a_independentVariable];
213 if (x >= x1 && x <= x2) {
214 const auto t = (x - x1) / (x2 - x1);
216 std::array<T, N + 1> interpRow;
218 for (
size_t i = 0; i <= N; i++) {
219 interpRow[i] = row1[i] + t * (row2[i] - row1[i]);
222 m_structuredData.emplace_back(interpRow);
231 m_grid = std::make_tuple(a_spacing, a_independentVariable, xmin, xmax, delta);
236 template <
typename T,
size_t N,
typename I>
237 inline std::vector<std::array<T, N + 1>>&
243 template <
typename T,
size_t N,
typename I>
244 inline std::vector<std::array<T, N + 1>>&
247 return (m_structuredData);
250 template <
typename T,
size_t N,
typename I>
251 inline const std::vector<std::array<T, N + 1>>&
257 template <
typename T,
size_t N,
typename I>
258 inline const std::vector<std::array<T, N + 1>>&
261 return (m_structuredData);
264 template <
typename T,
size_t N,
typename I>
268 this->writeToFile(a_file, m_rawData);
271 template <
typename T,
size_t N,
typename I>
275 this->writeToFile(a_file, m_structuredData);
278 template <
typename T,
size_t N,
typename I>
282 this->outputData(a_ostream, m_rawData);
285 template <
typename T,
size_t N,
typename I>
289 this->outputData(a_ostream, m_structuredData);
292 template <
typename T,
size_t N,
typename I>
295 const std::vector<std::array<T, N + 1>>& a_data)
const noexcept
297 for (
const auto& r : a_data) {
298 for (
const auto&
c : r) {
299 a_ostream << std::left << std::setw(14) <<
c;
305 template <
typename T,
size_t N,
typename I>
308 const std::vector<std::array<T, N + 1>>& a_data)
const noexcept
317 this->outputData(file, a_data);
324 template <
typename T,
size_t N,
typename I>
329 const T& x0 = std::get<2>(m_grid);
330 const T& delta = std::get<4>(m_grid);
335 case LookupTable::Spacing::Uniform: {
336 idxLo = std::floor((a_x - x0) / delta);
340 case LookupTable::Spacing::Exponential: {
341 idxLo = std::floor(log10(a_x / x0) / delta);
346 throw std::runtime_error(
"LookupTable1D<T,N,I>::getIndexLo - logic bust (unsupported table spacing system)");
353 template <
typename T,
size_t N,
typename I>
354 inline std::array<T, N + 1>
358 throw std::runtime_error(
"LookupTable1D<T, N, I>::interpolate(array) but need to call 'prepareTable first'");
361 const size_t& indVar = std::get<1>(m_grid);
362 const T& xmin = std::get<2>(m_grid);
363 const T& xmax = std::get<3>(m_grid);
365 std::array<T, N + 1> ret;
368 switch (m_rangeStrategyLo) {
369 case LookupTable::OutOfRangeStrategy::Constant: {
370 ret = m_structuredData.front();
374 case LookupTable::OutOfRangeStrategy::Interpolate: {
375 const T diffDx = m_structuredData[1][indVar] - m_structuredData[0][indVar];
376 const T extrapDx = m_structuredData[0][indVar] - a_x;
378 ret = m_structuredData.front();
380 for (
size_t i = 0; i < N + 1; i++) {
381 const T slope = (m_structuredData[1][i] - m_structuredData[0][i]) / diffDx;
383 ret[i] -= slope * extrapDx;
389 throw std::runtime_error(
"LookupTable1D<T, N, I>::interpolate(array) unsupported range strategy at low end");
395 else if (a_x > xmax) {
396 switch (m_rangeStrategyHi) {
397 case LookupTable::OutOfRangeStrategy::Constant: {
398 ret = m_structuredData.back();
402 case LookupTable::OutOfRangeStrategy::Interpolate: {
403 const size_t& size = m_structuredData.size();
405 const T diffDx = m_structuredData[size - 1][indVar] - m_structuredData[size - 2][indVar];
406 const T extrapDx = a_x - m_structuredData[size - 1][indVar];
408 ret = m_structuredData.back();
410 for (
size_t i = 0; i < N + 1; i++) {
411 const T slope = (m_structuredData[size - 1][i] - m_structuredData[size - 2][i]) / diffDx;
413 ret[i] += slope * extrapDx;
419 throw std::runtime_error(
"LookupTable1D<T, N, I>::interpolate(array) unsupported range strategy at low end");
426 const size_t idx = this->getIndexLo(a_x);
428 const T diffDx = m_structuredData[idx + 1][indVar] - m_structuredData[idx][indVar];
429 const T interpDx = a_x - m_structuredData[idx][indVar];
431 ret = m_structuredData[idx];
433 for (
size_t i = 0; i < N + 1; i++) {
434 const T slope = (m_structuredData[idx + 1][i] - m_structuredData[idx][i]) / diffDx;
436 ret[i] += slope * interpDx;
443 template <
typename T,
size_t N,
typename I>
448 return std::get<K>(this->interpolate(a_x));
451 template <
typename T,
size_t N,
typename I>
452 template <
size_t M,
typename... Ts>
456 static_assert(M <= N,
"LookupTable1D<T, N,I>::slice must have have 'M <= N'");
458 std::array<size_t,
sizeof...(Ts)> arr = {(size_t)a_columns...};
462 for (
const auto& r : m_rawData) {
463 std::array<T, M + 1> slicedData;
465 for (
size_t i = 0; i < M + 1; i++) {
466 slicedData[i] = r[arr[i]];
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:29
Spacing
Enum for classifying the coordinate system.
Definition: CD_LookupTable.H:38
Class for interpolation of f = f(x) data in one independent variable x.
Definition: CD_LookupTable1D.H:30
std::vector< std::array< T, N+1 > > & getStructuredData() noexcept
Access function for structured data.
Definition: CD_LookupTable1DImplem.H:245
T interpolate(const T &a_x) const
Interpolation function for specific dependent variable K.
Definition: CD_LookupTable1DImplem.H:446
void addData(const Ts &... x) noexcept
Add entry.
Definition: CD_LookupTable1DImplem.H:47
void setRangeStrategyLo(const LookupTable::OutOfRangeStrategy &a_strategy) noexcept
Set the out-of-range strategy on the low end.
Definition: CD_LookupTable1DImplem.H:124
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:294
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:326
void swap(const size_t a_columnOne, const size_t a_columnTwo) noexcept
Utility function for swapping columns.
Definition: CD_LookupTable1DImplem.H:63
void truncate(const T &a_min, const T &a_max, const size_t a_column) noexcept
Utility function for truncating raw data along one of the variables (either dependent or independent)...
Definition: CD_LookupTable1DImplem.H:109
void setRangeStrategyHi(const LookupTable::OutOfRangeStrategy &a_strategy) noexcept
Set the out-of-range strategy on the high end.
Definition: CD_LookupTable1DImplem.H:131
LookupTable1D() noexcept
Default constructor. Creates a table without any entries.
Definition: CD_LookupTable1DImplem.H:26
void writeRawData(const std::string &a_file) const noexcept
Dump raw table data to file.
Definition: CD_LookupTable1DImplem.H:266
void outputStructuredData(std::ostream &a_ostream=std::cout) const noexcept
Dump structured table data to file.
Definition: CD_LookupTable1DImplem.H:287
void reset() noexcept
Reset everything.
Definition: CD_LookupTable1DImplem.H:33
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:307
void outputRawData(std::ostream &a_ostream=std::cout) const noexcept
Dump raw table data to output stream.
Definition: CD_LookupTable1DImplem.H:280
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:138
void scale(const T &a_scale) noexcept
Utility function which scales one of the columns (either dependent or independent variable)
Definition: CD_LookupTable1DImplem.H:92
void writeStructuredData(const std::string &a_file) const noexcept
Dump structured table data to file.
Definition: CD_LookupTable1DImplem.H:273
LookupTable1D< T, M, I > slice(const Ts &... a_columns) const noexcept
Slice this table, keeping only the user-specified columns.
Definition: CD_LookupTable1DImplem.H:454
std::vector< std::array< T, N+1 > > & getRawData() noexcept
Access function for raw data.
Definition: CD_LookupTable1DImplem.H:238
Real min(const Real &a_input) noexcept
Get the minimum of the input, reduced over MPI ranks (in the Chombo communicator)
Definition: CD_ParallelOpsImplem.H:58
constexpr Real c
Speed of light.
Definition: CD_Units.H:39