GCC Code Coverage Report


Directory: ./
File: Tools/table_interpolation.hpp
Date: 2024-04-14 07:32:34
Exec Total Coverage
Lines: 22 28 78.6%
Branches: 5 12 41.7%

Line Branch Exec Source
1 // ______ ______ _ _ _____ ______
2 // | ____| ____| | (_)/ ____| | ____|
3 // | |__ | |__ | | _| (___ ___| |__
4 // | __| | __| | | | |\___ \ / __| __|
5 // | | | |____| |____| |____) | (__| |____
6 // |_| |______|______|_|_____/ \___|______|
7 // Finite Elements for Life Sciences and Engineering
8 //
9 // License: LGL2.1 License
10 // FELiScE default license: LICENSE in root folder
11 //
12 // Main authors: Vicente Mataix Ferrandiz
13 //
14
15 #if !defined(FELISCE_TABLE_HPP_INCLUDED )
16 #define FELISCE_TABLE_HPP_INCLUDED
17
18 // System includes
19 #include <string>
20 #include <iostream>
21 #include <array>
22 #include <vector>
23
24 // External includes
25
26 // Project includes
27 #include "Core/shared_pointers.hpp"
28
29 namespace felisce
30 {
31 ///@name felisce Globals
32 ///@{
33
34 ///@}
35 ///@name Type Definitions
36 ///@{
37
38 ///@}
39 ///@name Enum's
40 ///@{
41
42 ///@}
43 ///@name Functions
44 ///@{
45
46 ///@}
47 ///@name felisce Classes
48 ///@{
49
50 /**
51 * @class TableInterpolation
52 * @brief This class represents the value of its variable depending to other variable.
53 * @details TableInterpolation class stores the value of its second variable respect to the value of its first variable.
54 * It also provides a double to double table with piecewise linear interpolator/extrapolator for getting intermediate values.
55 * @author Vicente Mataix Ferrandoz
56 */
57 class TableInterpolation
58 {
59 public:
60 ///@name Type Definitions
61 ///@{
62
63 /// Pointer definition of TableInterpolation
64 FELISCE_CLASS_POINTER_DEFINITION(TableInterpolation);
65
66 typedef std::array<double, 1> result_row_type;
67
68 typedef std::pair<double, result_row_type> RecordType;
69
70 typedef std::vector<RecordType> TableInterpolationContainerType;
71
72 ///@}
73 ///@name Life Cycle
74 ///@{
75
76 /// Default constructor.
77
3/6
✓ Branch 3 taken 41 times.
✗ Branch 4 not taken.
✓ Branch 8 taken 41 times.
✗ Branch 9 not taken.
✓ Branch 12 taken 41 times.
✗ Branch 13 not taken.
41 TableInterpolation() : mData()
78 {
79 41 }
80
81 /// Copy constructor.
82 72 TableInterpolation(TableInterpolation const& rOther)
83 72 : mData(rOther.mData),
84
1/2
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
72 mName(rOther.mName),
85
1/2
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
144 mVariablesNames(rOther.mVariablesNames)
86 {
87
88 72 }
89
90 /// Matrix constructor. the template parameter must have (i,j) access operator and size1 methods defined.
91 template<class TMatrixType>
92 TableInterpolation(TMatrixType const& rThisMatrix): mData()
93 {
94 for(std::size_t i = 0 ; i < rThisMatrix.size1() ; i++) {
95 PushBack(rThisMatrix(i,0), rThisMatrix(i,1));
96 }
97 }
98
99 /// Vector constructor
100 template<class TVectorType>
101 TableInterpolation(TVectorType const& rThisVector1, TVectorType const& rThisVector2) : mData()
102 {
103 for(std::size_t i = 0 ; i < rThisVector1.size() ; i++) {
104 PushBack(rThisVector1[i], rThisVector2[i]);
105 }
106 }
107
108 /// Destructor.
109 226 virtual ~TableInterpolation()
110 {
111 }
112
113 /// Assignment operator.
114 TableInterpolation& operator=(TableInterpolation const& rOther)
115 {
116 mData = rOther.mData;
117 mName = rOther.mName;
118 mVariablesNames = rOther.mVariablesNames;
119 return *this;
120 }
121
122 ///@}
123 ///@name Operators
124 ///@{
125
126 // I want to put operator(i,j) for accessing, operator(i) for first column and operator[i] for getting the complete row
127
128 // This operator calculates the piecewise linear interpolation for
129 // given argument
130 1320 double operator()(const double X) const
131 {
132 1320 return GetValue(X);
133 }
134
135 // This operator gives the result for the nearest value to argument found in table
136 const double & operator[](const double X) const
137 {
138 return GetNearestValue(X);
139 }
140
141 // This operator gives the result for the nearest value to argument found in table
142 double & operator[](double& X)
143 {
144 return GetNearestValue(X);
145 }
146
147 ///@}
148 ///@name Operations
149 ///@{
150
151 // Get the value for the given argument using piecewise linear
152 double GetValue(const double X) const;
153
154 // Get the nesrest value for the given argument
155 result_row_type& GetNearestRow(const double X);
156
157 // Get the nesrest value for the given argument
158 const double& GetNearestValue(const double X) const;
159
160 // Get the nesrest value for the given argument
161 double& GetNearestValue(const double X);
162
163 // Interpolate the value
164 double& Interpolate(const double X, const double X1, const double Y1, const double X2, const double Y2, double& Result) const;
165
166 // inserts a row in a sorted position where Xi-1 < X < Xi+1 and fills the first column with Y
167 void insert(const double X, const double Y);
168
169 // inserts a row in a sorted position where Xi-1 < X < Xi+1
170 void insert(const double X, const result_row_type& Y);
171
172 // assumes that the X is the greater than the last argument and put the row at the end.
173 // faster than insert.
174 void PushBack(const double X, const double Y);
175
176 // Get the derivative for the given argument using piecewise linear
177 double GetDerivative(const double X) const;
178
179 // This interpolates the derivative
180 double& InterpolateDerivative( const double X1, const double Y1, const double X2, const double Y2, double& Result) const;
181
182 /**
183 * @brief This returns the table internal data
184 * @return The internal data
185 */
186 const std::array<std::vector<double>, 2> GetTableAsVectors() const
187 {
188 const std::size_t size = mData.size();
189 std::vector<double> vector_x(size);
190 std::vector<double> vector_y(size);
191 for(std::size_t i = 0 ; i < size ; i++) {
192 vector_x[i] = mData[i].first;
193 vector_y[i] = mData[i].second[0];
194 }
195
196 const std::array<std::vector<double>, 2> aux_vectors({vector_x, vector_y});
197 return aux_vectors;
198 }
199
200 /**
201 * @brief This method clears databse
202 */
203 1 void Clear()
204 {
205 1 mData.clear();
206 1 }
207
208 ///@}
209 ///@name Access
210 ///@{
211
212 /**
213 * @brief This returns the table internal data
214 * @return The internal data
215 */
216 1 TableInterpolationContainerType& Data()
217 {
218 1 return mData;
219 }
220
221 /**
222 * @brief This returns the table internal data (constant version)
223 * @return The internal data
224 */
225 TableInterpolationContainerType const& Data() const
226 {
227 return mData;
228 }
229
230 /**
231 * @brief This method sets the variables names
232 * @param rName1 The name of the first variable
233 * @param rName2 The name of the second variable
234 */
235 24 void SetVariablesNames(
236 const std::string& rName1,
237 const std::string& rName2
238 )
239 {
240 24 mVariablesNames[0] = rName1;
241 24 mVariablesNames[1] = rName2;
242 24 }
243
244 /**
245 * @brief This method retrieves the names of the variables in the table
246 */
247 std::array<std::string, 2>& GetVariablesNames()
248 {
249 return mVariablesNames;
250 }
251
252 /**
253 * @brief This method retrieves the names of the variables in the table (constant version)
254 */
255 const std::array<std::string, 2>& GetVariablesNames() const
256 {
257 return mVariablesNames;
258 }
259
260 /**
261 * @brief This method sets the variables names
262 * @param rName The name of the table name
263 */
264 24 void SetTableName(const std::string& rName)
265 {
266 24 mName = rName;
267 24 }
268
269 /**
270 * @brief This method retrieves the name of the table
271 */
272 std::string& GetTableName()
273 {
274 return mName;
275 }
276
277 /**
278 * @brief This method retrieves the name of the table (constant version)
279 */
280 const std::string& GetTableName() const
281 {
282 return mName;
283 }
284
285 ///@}
286 ///@name Inquiry
287 ///@{
288
289
290 ///@}
291 ///@name Input and output
292 ///@{
293
294 /// Turn back information as a string.
295 virtual std::string Info() const
296 {
297 return "Piecewise Linear TableInterpolation: " + mName;
298 }
299
300 /// Print information about this object.
301 virtual void PrintInfo(std::ostream& rOStream) const
302 {
303 rOStream << Info();
304 }
305
306 /// Print object's data.
307 virtual void PrintData(std::ostream& rOStream) const;
308
309 ///@}
310 ///@name Friends
311 ///@{
312
313 ///@}
314 private:
315 ///@name Static Member Variables
316 ///@{
317
318 ///@}
319 ///@name Member Variables
320 ///@{
321
322 TableInterpolationContainerType mData; /// This stores the results
323
324 std::string mName = "Table"; /// The name of the table
325
326 std::array<std::string, 2> mVariablesNames = std::array<std::string, 2>({"Variable1", "Variable2"}); /// This stores the variables names if any
327
328 ///@}
329 ///@name Private Operators
330 ///@{
331
332 ///@}
333 ///@name Private Operations
334 ///@{
335
336 ///@}
337 ///@name Serialization
338 ///@{
339
340 ///@}
341 ///@name Private Access
342 ///@{
343
344 ///@}
345 ///@name Private Inquiry
346 ///@{
347
348 ///@}
349 ///@name Un accessible methods
350 ///@{
351
352 ///@}
353 }; // Class TableInterpolation
354
355 ///@}
356
357 ///@name Type Definitions
358 ///@{
359
360 ///@}
361 ///@name Input and output
362 ///@{
363
364 /// input stream function
365 inline std::istream& operator >> (std::istream& rIStream,
366 TableInterpolation& rThis);
367
368 /// output stream function
369 inline std::ostream& operator << (std::ostream& rOStream,
370 const TableInterpolation& rThis)
371 {
372 rThis.PrintInfo(rOStream);
373 rOStream << std::endl;
374 rThis.PrintData(rOStream);
375
376 return rOStream;
377 }
378
379 ///@}
380
381 } // namespace felisce.
382
383 #endif // FELISCE_TABLE_HPP_INCLUDED defined
384
385
386