chombo-discharge
CD_LookupTable1D.H
Go to the documentation of this file.
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_LookupTable1D_H
13 #define CD_LookupTable1D_H
14 
15 // Std includes
16 #include <iostream>
17 #include <vector>
18 #include <array>
19 #include <type_traits>
20 
21 // Our includes
22 #include <CD_LookupTable.H>
23 
28 template <typename T = Real, size_t N = 1, typename I = std::enable_if_t<std::is_floating_point<T>::value>>
30 {
31 public:
32  static_assert(N >= 1, "LookupTable1D<N> must have N >= 1");
33 
37  LookupTable1D() noexcept;
38 
42  virtual ~LookupTable1D() noexcept = default;
43 
47  inline void
48  reset() noexcept;
49 
55  template <size_t M, typename... Ts>
56  LookupTable1D<T, M, I>
57  slice(const Ts&... a_columns) const noexcept;
58 
63  template <typename... Ts>
64  inline void
65  addData(const Ts&... x) noexcept;
66 
71  inline void
72  addData(const std::array<T, N + 1>& x) noexcept;
73 
81  inline void
82  swap(const size_t a_columnOne, const size_t a_columnTwo) noexcept;
83 
87  template <size_t K>
88  inline void
89  scale(const T& a_scale) noexcept;
90 
98  inline void
99  truncate(const T& a_min, const T& a_max, const size_t a_column) noexcept;
100 
105  inline void
106  setRangeStrategyLo(const LookupTable::OutOfRangeStrategy& a_strategy) noexcept;
107 
112  inline void
113  setRangeStrategyHi(const LookupTable::OutOfRangeStrategy& a_strategy) noexcept;
114 
121  inline void
122  prepareTable(const size_t& a_independentVariable, const size_t& a_numPoints, const LookupTable::Spacing& a_spacing);
123 
128  template <size_t K>
129  inline T
130  interpolate(const T& a_x) const;
131 
136  inline std::array<T, N + 1>
137  interpolate(const T& x) const;
138 
143  inline std::vector<std::array<T, N + 1>>&
144  getRawData() noexcept;
145 
150  inline std::vector<std::array<T, N + 1>>&
151  getStructuredData() noexcept;
152 
157  inline const std::vector<std::array<T, N + 1>>&
158  getRawData() const noexcept;
159 
164  inline const std::vector<std::array<T, N + 1>>&
165  getStructuredData() const noexcept;
166 
171  inline void
172  writeRawData(const std::string& a_file) const noexcept;
173 
178  inline void
179  writeStructuredData(const std::string& a_file) const noexcept;
180 
185  inline void
186  outputRawData(std::ostream& a_ostream = std::cout) const noexcept;
187 
192  inline void
193  outputStructuredData(std::ostream& a_ostream = std::cout) const noexcept;
194 
195 protected:
199  bool m_isGood;
200 
204  LookupTable::OutOfRangeStrategy m_rangeStrategyLo;
205 
209  LookupTable::OutOfRangeStrategy m_rangeStrategyHi;
210 
214  std::tuple<LookupTable::Spacing, size_t, T, T, T> m_grid;
215 
219  std::vector<std::array<T, N + 1>> m_rawData;
220 
224  std::vector<std::array<T, N + 1>> m_structuredData;
225 
231  inline void
232  outputData(std::ostream& a_ostream, const std::vector<std::array<T, N + 1>>& a_data) const noexcept;
233 
239  inline void
240  writeToFile(const std::string& a_file, const std::vector<std::array<T, N + 1>>& a_data) const noexcept;
241 
246  inline size_t
247  getIndexLo(const T& a_x) const;
248 };
249 
250 #include <CD_LookupTable1DImplem.H>
251 
252 #endif
Declaration of a table for looking up coefficients etc.
Class for interpolation of f = f(x) data in one independent variable x.
Definition: CD_LookupTable1D.H:30
LookupTable::OutOfRangeStrategy m_rangeStrategyLo
Out-of-range strategy on low end.
Definition: CD_LookupTable1D.H:204
std::vector< std::array< T, N+1 > > m_rawData
Raw data.
Definition: CD_LookupTable1D.H:219
std::vector< std::array< T, N+1 > > m_structuredData
Structured data. This is populated when calling prepareTable.
Definition: CD_LookupTable1D.H:224
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
bool m_isGood
Check if data can be interpolated.
Definition: CD_LookupTable1D.H:199
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
LookupTable::OutOfRangeStrategy m_rangeStrategyHi
Out-of-range strategy on high end.
Definition: CD_LookupTable1D.H:209
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
virtual ~LookupTable1D() noexcept=default
Destructor (does nothing).
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::tuple< LookupTable::Spacing, size_t, T, T, T > m_grid
Underlying 1D grid. This is populated when calling prepareTable.
Definition: CD_LookupTable1D.H:214
std::vector< std::array< T, N+1 > > & getRawData() noexcept
Access function for raw data.
Definition: CD_LookupTable1DImplem.H:238