chombo-discharge
Loading...
Searching...
No Matches
CD_LookupTable1DImplem.H
1/* chombo-discharge
2 * Copyright © 2021 SINTEF Energy Research.
3 * Please refer to Copyright.txt and LICENSE in the chombo-discharge root directory.
4 */
5
12#ifndef CD_LookupTableImplem_H
13#define CD_LookupTableImplem_H
14
15// Std includes
16#include <algorithm>
17#include <math.h>
18#include <iomanip>
19#include <fstream>
20#include <limits>
21
22// Our includes
23#include <CD_LookupTable1D.H>
24#include <CD_NamespaceHeader.H>
25
26template <typename T, size_t N, typename I>
31
32template <typename T, size_t N, typename I>
33inline void
35{
36 m_isGood = false;
37 m_grid = std::make_tuple(LookupTable::Spacing::Uniform, 0, -1.0, -1.0, -1.0);
38 m_rangeStrategyLo = LookupTable::OutOfRangeStrategy::Constant;
39 m_rangeStrategyHi = LookupTable::OutOfRangeStrategy::Constant;
40
41 m_rawData.clear();
42 m_structuredData.clear();
43}
44
45template <typename T, size_t N, typename I>
46template <typename... Ts>
47inline void
50 std::array<T, sizeof...(Ts)> arr = {(T)x...};
51
52 m_rawData.emplace_back(arr);
53}
54
55template <typename T, size_t N, typename I>
56inline void
59 m_rawData.emplace_back(x);
60}
61
62template <typename T, size_t N, typename I>
63inline void
64LookupTable1D<T, N, I>::swap(const size_t a_columnOne, const size_t a_columnTwo) noexcept
65{
66 for (auto& r : m_rawData) {
67 const auto tmp = r[a_columnOne];
68
70 r[a_columnTwo] = tmp;
71 }
72
73 for (auto& r : m_structuredData) {
74 const auto tmp = r[a_columnOne];
75
77 r[a_columnTwo] = tmp;
78 }
79
80 if (m_isGood) {
81 if (a_columnOne == std::get<4>(m_grid)) {
82 std::get<4>(m_grid) = a_columnOne;
83 }
84 else if (a_columnTwo == std::get<4>(m_grid)) {
85 std::get<4>(m_grid) = a_columnTwo;
86 }
87 }
88}
89
90template <typename T, size_t N, typename I>
91template <size_t K>
92inline void
94{
95 for (auto& r : m_rawData) {
96 r[K] *= a_scale;
97 }
98
99 for (auto& r : m_structuredData) {
100 r[K] *= a_scale;
102
103 if (m_isGood && K == std::get<2>(m_grid)) {
104 std::get<4>(m_grid) *= a_scale;
105 }
106}
107
108template <typename T, size_t N, typename I>
109inline void
110LookupTable1D<T, N, I>::truncate(const T& a_min, const T& a_max, const size_t a_column) noexcept
111{
113
114 for (const auto& xy : m_rawData) {
115 if (xy[a_column] >= a_min && xy[a_column] <= a_max) {
116 truncatedData.emplace_back(xy);
117 }
118 }
119
120 m_rawData = truncatedData;
121}
122
123template <typename T, size_t N, typename I>
124inline void
129
130template <typename T, size_t N, typename I>
131inline void
133{
134 m_rangeStrategyHi = a_strategy;
135}
136
137template <typename T, size_t N, typename I>
138inline void
140 const size_t& a_numPoints,
142{
143
144 const std::string baseError = "LookupTable1D<T,N,I>::prepareTable";
145
146 if (a_numPoints <= 1) {
147 throw std::runtime_error(baseError + " - must have 'a_numPoints > 1'");
148 }
149
150 // Sort the raw data along the specified column.
154 std::sort(sortedData.begin(),
155 sortedData.end(),
156 [i = a_independentVariable](const Values& X1, const Values& X2) -> bool {
157 return X1[i] < X2[i];
158 });
159
160 // Set up the 1D grid
161 T xmin;
162 T xmax;
163 T delta;
164
169
170 switch (a_spacing) {
171 case LookupTable::Spacing::Uniform: {
172 delta = (xmax - xmin) / (a_numPoints - 1);
173
174 for (size_t i = 0; i < a_numPoints; i++) {
175 coords[i] = xmin + i * delta;
176 }
177
178 break;
179 }
180 case LookupTable::Spacing::Exponential: {
181 if (xmin <= std::numeric_limits<T>::min()) {
182 throw std::runtime_error(baseError + " - but must have all 'x > 0.0' for logarithmic grid");
183 }
184
185 else {
186 delta = log10(xmax / xmin) / (a_numPoints - 1);
187
188 for (int i = 0; i < a_numPoints; i++) {
189 coords[i] = xmin * std::pow(10.0, i * delta);
190 }
191
192 break;
193 }
194 }
195 default: {
196 throw std::runtime_error(baseError + " - logic bust (unsupported table spacing system)");
197
198 break;
199 }
200 }
201
202 m_structuredData.clear();
203
204 size_t curRow = 0;
205
206 for (const auto& x : coords) {
207 while (curRow < m_rawData.size() - 1) {
208 const auto& row1 = m_rawData[curRow];
209 const auto& row2 = m_rawData[curRow + 1];
210
211 const auto x1 = row1[a_independentVariable];
212 const auto x2 = row2[a_independentVariable];
213
214 if (x >= x1 && x <= x2) {
215 const auto t = (x - x1) / (x2 - x1);
216
218
219 for (size_t i = 0; i <= N; i++) {
220 interpRow[i] = row1[i] + t * (row2[i] - row1[i]);
221 }
222
223 m_structuredData.emplace_back(interpRow);
224
225 break;
226 }
227
228 curRow++;
229 }
230 }
231
233
234 m_isGood = true;
235}
236
237template <typename T, size_t N, typename I>
240{
241 return (m_rawData);
243
244template <typename T, size_t N, typename I>
247{
248 return (m_structuredData);
250
251template <typename T, size_t N, typename I>
254{
255 return (m_rawData);
256}
257
258template <typename T, size_t N, typename I>
261{
262 return (m_structuredData);
263}
264
265template <typename T, size_t N, typename I>
266inline void
268{
269 this->writeToFile(a_file, m_rawData);
270}
271
272template <typename T, size_t N, typename I>
273inline void
275{
276 this->writeToFile(a_file, m_structuredData);
277}
278
279template <typename T, size_t N, typename I>
280inline void
282{
283 this->outputData(a_ostream, m_rawData);
284}
285
286template <typename T, size_t N, typename I>
287inline void
289{
290 this->outputData(a_ostream, m_structuredData);
291}
292
293template <typename T, size_t N, typename I>
294inline void
296 const std::vector<std::array<T, N + 1>>& a_data) const noexcept
297{
298 for (const auto& r : a_data) {
299 for (const auto& c : r) {
300 a_ostream << std::left << std::setw(14) << c;
301 }
302 a_ostream << "\n";
303 }
304}
305
306template <typename T, size_t N, typename I>
307inline void
309 const std::vector<std::array<T, N + 1>>& a_data) const noexcept
310{
311
312#ifdef CH_MPI
313 if (procID() == 0) {
314#endif
316
317 file.open(a_file);
318 this->outputData(file, a_data);
319 file.close();
320#ifdef CH_MPI
321 }
322#endif
323}
324
325template <typename T, size_t N, typename I>
326inline size_t
328{
330 const T& x0 = std::get<2>(m_grid);
331 const T& delta = std::get<4>(m_grid);
332
333 size_t idxLo;
334
335 switch (spacing) {
336 case LookupTable::Spacing::Uniform: {
337 idxLo = std::floor((a_x - x0) / delta);
338
339 break;
340 }
341 case LookupTable::Spacing::Exponential: {
342 idxLo = std::floor(log10(a_x / x0) / delta);
343
344 break;
345 }
346 default: {
347 throw std::runtime_error("LookupTable1D<T,N,I>::getIndexLo - logic bust (unsupported table spacing system)");
348 }
349 }
350
351 return idxLo;
352}
353
354template <typename T, size_t N, typename I>
357{
358 if (!m_isGood) {
359 throw std::runtime_error("LookupTable1D<T, N, I>::interpolate(array) but need to call 'prepareTable first'");
360 }
361
362 const size_t& indVar = std::get<1>(m_grid);
363 const T& xmin = std::get<2>(m_grid);
364 const T& xmax = std::get<3>(m_grid);
365
367
368 if (a_x < xmin) {
369 switch (m_rangeStrategyLo) {
370 case LookupTable::OutOfRangeStrategy::Constant: {
371 ret = m_structuredData.front();
372
373 break;
374 }
375 case LookupTable::OutOfRangeStrategy::Interpolate: {
376 const T diffDx = m_structuredData[1][indVar] - m_structuredData[0][indVar];
377 const T extrapDx = m_structuredData[0][indVar] - a_x;
378
379 ret = m_structuredData.front();
380
381 for (size_t i = 0; i < N + 1; i++) {
382 const T slope = (m_structuredData[1][i] - m_structuredData[0][i]) / diffDx;
383
384 ret[i] -= slope * extrapDx;
385 }
386
387 break;
388 }
389 default: {
390 throw std::runtime_error("LookupTable1D<T, N, I>::interpolate(array) unsupported range strategy at low end");
391
392 break;
393 }
394 }
395 }
396 else if (a_x > xmax) {
397 switch (m_rangeStrategyHi) {
398 case LookupTable::OutOfRangeStrategy::Constant: {
399 ret = m_structuredData.back();
400
401 break;
402 }
403 case LookupTable::OutOfRangeStrategy::Interpolate: {
404 const size_t& size = m_structuredData.size();
405
406 const T diffDx = m_structuredData[size - 1][indVar] - m_structuredData[size - 2][indVar];
407 const T extrapDx = a_x - m_structuredData[size - 1][indVar];
408
409 ret = m_structuredData.back();
410
411 for (size_t i = 0; i < N + 1; i++) {
412 const T slope = (m_structuredData[size - 1][i] - m_structuredData[size - 2][i]) / diffDx;
413
414 ret[i] += slope * extrapDx;
415 }
416
417 break;
418 }
419 default: {
420 throw std::runtime_error("LookupTable1D<T, N, I>::interpolate(array) unsupported range strategy at low end");
421
422 break;
423 }
424 }
425 }
426 else {
427 const size_t idx = this->getIndexLo(a_x);
428
429 const T diffDx = m_structuredData[idx + 1][indVar] - m_structuredData[idx][indVar];
430 const T interpDx = a_x - m_structuredData[idx][indVar];
431
432 ret = m_structuredData[idx];
433
434 for (size_t i = 0; i < N + 1; i++) {
435 const T slope = (m_structuredData[idx + 1][i] - m_structuredData[idx][i]) / diffDx;
436
437 ret[i] += slope * interpDx;
438 }
439 }
440
441 return ret;
442}
443
444template <typename T, size_t N, typename I>
445template <size_t K>
446inline T
448{
449 return std::get<K>(this->interpolate(a_x));
450}
451
452template <typename T, size_t N, typename I>
453template <size_t M, typename... Ts>
456{
457 static_assert(M <= N, "LookupTable1D<T, N,I>::slice must have have 'M <= N'");
458
459 std::array<size_t, sizeof...(Ts)> arr = {(size_t)a_columns...};
460
462
463 for (const auto& r : m_rawData) {
465
466 for (size_t i = 0; i < M + 1; i++) {
467 slicedData[i] = r[arr[i]];
468 }
469
470 ret.addData(slicedData);
471 }
472
473 return ret;
474}
475
476#include <CD_NamespaceFooter.H>
477
478#endif
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:32
Spacing
Enum for classifying the coordinate system.
Definition CD_LookupTable.H:41
std::vector< std::array< T, N+1 > > & getStructuredData() noexcept
Access function for structured data.
Definition CD_LookupTable1DImplem.H:246
T interpolate(const T &a_x) const
Interpolation function for specific dependent variable K.
Definition CD_LookupTable1DImplem.H:447
void addData(const Ts &... x) noexcept
Add entry.
Definition CD_LookupTable1DImplem.H:48
void setRangeStrategyLo(const LookupTable::OutOfRangeStrategy &a_strategy) noexcept
Set the out-of-range strategy on the low end.
Definition CD_LookupTable1DImplem.H:125
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:327
void swap(const size_t a_columnOne, const size_t a_columnTwo) noexcept
Utility function for swapping columns.
Definition CD_LookupTable1DImplem.H:64
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:110
void setRangeStrategyHi(const LookupTable::OutOfRangeStrategy &a_strategy) noexcept
Set the out-of-range strategy on the high end.
Definition CD_LookupTable1DImplem.H:132
LookupTable1D() noexcept
Default constructor. Creates a table without any entries.
Definition CD_LookupTable1DImplem.H:27
void writeRawData(const std::string &a_file) const noexcept
Dump raw table data to file.
Definition CD_LookupTable1DImplem.H:267
void outputStructuredData(std::ostream &a_ostream=std::cout) const noexcept
Dump structured table data to file.
Definition CD_LookupTable1DImplem.H:288
void reset() noexcept
Reset everything.
Definition CD_LookupTable1DImplem.H:34
void outputRawData(std::ostream &a_ostream=std::cout) const noexcept
Dump raw table data to output stream.
Definition CD_LookupTable1DImplem.H:281
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:139
void scale(const T &a_scale) noexcept
Utility function which scales one of the columns (either dependent or independent variable)
Definition CD_LookupTable1DImplem.H:93
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:308
void writeStructuredData(const std::string &a_file) const noexcept
Dump structured table data to file.
Definition CD_LookupTable1DImplem.H:274
LookupTable1D< T, M, I > slice(const Ts &... a_columns) const noexcept
Slice this table, keeping only the user-specified columns.
Definition CD_LookupTable1DImplem.H:455
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:295
std::vector< std::array< T, N+1 > > & getRawData() noexcept
Access function for raw data.
Definition CD_LookupTable1DImplem.H:239
Base class for a tracer particle solver. This solver can advance particles in a pre-defined velocity ...
Definition CD_TracerParticleSolver.H:37
TracerParticleSolver()
Default constructor.
Definition CD_TracerParticleSolverImplem.H:25