KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
piecewize_linear_table.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 // Riccardo Rossi
12 //
13 //
14 
15 #if !defined(KRATOS_PIECEWIZE_LINEAR_TABLE_H_INCLUDED )
16 #define KRATOS_PIECEWIZE_LINEAR_TABLE_H_INCLUDED
17 
18 
19 
20 // System includes
21 #include <string>
22 #include <iostream>
23 
24 
25 // External includes
26 
27 
28 // Project includes
29 #include "includes/define.h"
30 #include "containers/variable.h"
31 
32 
33 namespace Kratos
34 {
37 
40 
44 
48 
52 
56 
58 
61 template<class TArgumentType, class TResultType = TArgumentType, std::size_t TResultsColumns = 1>
62 class Table
63 {
64 public:
67 
70 
71  typedef std::array<TResultType, TResultsColumns> result_array_type;
72 
73  typedef std::pair<TArgumentType, result_array_type> RecordType;
74 
75  typedef std::vector<RecordType> TableContainerType;
76 
79 
83 
85  Table() : mData(), mpXVariable(NULL) , mpYVariable(NULL)
86  {
87  }
88 
90  Table(XVariableType const& XVariable, YVariableType const& YVariable) : mData(), mpXVariable(&XVariable) , mpYVariable(&YVariable)
91  {
92  }
93 
95  virtual ~Table()
96  {
97  }
98 
99 
101  Table& operator=(Table const& rOther)
102  {
103  mData = rOther.mData;
104  return *this;
105  }
106 
107 
111 
112  // I want to put operator(i,j) for accessing, operator(i) for first column and operator[i] for getting the complete row
113 
114  // This operator calculates the piecewise linear interpolation for
115  // given argument
116  TResultType operator()(TArgumentType const& X) const
117  {
118  return GetValue(X);
119  }
120 
121  // This operator gives the result for the nearest value to argument found in table
122  TResultType const & operator[](TArgumentType const& X) const
123  {
125  }
126 
127  // This operator gives the result for the nearest value to argument found in table
128  TResultType & operator[](TArgumentType& X)
129  {
131  }
132 
136 
137  // Get the value for the given argument using piecewise linear
138  TResultType GetValue(TArgumentType const& X) const
139  {
140  std::size_t size = mData.size();
141 
142  if(size == 0)
143  KRATOS_THROW_ERROR(std::invalid_argument, "Get value from empty table", "");
144 
145  if(size==1) // constant table. Returning the only value we have.
146  return mData.begin()->second;
147 
148  TResultType result;
149  if(X <= mData[0].first)
150  return Interpolate(X, mData[0].first, mData[0].second, mData[1].first, mData[1].second, result);
151 
152  for(std::size_t i = 1 ; i < size ; i++)
153  if(X <= mData[i].first)
154  return Interpolate(X, mData[i-1].first, mData[i-1].second, mData[i].first, mData[i].second, result);
155 
156  // now the x is outside the table and we have to extrapolate it using last two records of table.
157  return Interpolate(X, mData[size-2].first, mData[size-2].second, mData[size-1].first, mData[size-1].second, result);
158  }
159 
160  // Get the nesrest value for the given argument
161  result_array_type& GetNearestRow(TArgumentType const& X)
162  {
163  std::size_t size = mData.size();
164 
165  if(size == 0)
166  KRATOS_THROW_ERROR(std::invalid_argument, "Get value from empty table", "");
167 
168  if(size==1) // constant table. Returning the only value we have.
169  return mData.begin()->second;
170 
171  if(X <= mData[0].first)
172  return mData[0].second;
173 
174  for(std::size_t i = 1 ; i < size ; i++)
175  if(X <= mData[i].first)
176  return ((X - mData[i-1].first) < (mData[i].first - X)) ? mData[i-1].second : mData[i].second;
177 
178  // now the x is outside the table and we have to extrapolate it using last two records of table.
179  return mData[size-1].second;
180  }
181 
182  // Get the nesrest value for the given argument
183  TResultType const& GetNearestValue(TArgumentType const& X) const
184  {
185  std::size_t size = mData.size();
186 
187  if(size == 0)
188  KRATOS_THROW_ERROR(std::invalid_argument, "Get value from empty table", "");
189 
190  if(size==1) // constant table. Returning the only value we have.
191  return mData.begin()->second;
192 
193  if(X <= mData[0].first)
194  return mData[0].second;
195 
196  for(std::size_t i = 1 ; i < size ; i++)
197  if(X <= mData[i].first)
198  return ((X - mData[i-1].first) < (mData[i].first - X)) ? mData[i-1].second : mData[i].second;
199 
200  // now the x is outside the table and we have to extrapolate it using last two records of table.
201  return mData[size-1].second;
202  }
203 
204  TResultType& Interpolate(TArgumentType const& X, TArgumentType const& X1, TResultType const& Y1, TArgumentType const& X2, TResultType const& Y2, TResultType& Result)
205  {
206  double epsilon = 1e-12;
207 
208  double dx = X2 - X1;
209  TResultType dy = Y2 - Y1;
210 
211  double scale = 0.00;
212 
213  if (dx > epsilon)
214  scale = (X - X1) / dx_norm;
215 
216  Result = Y1 + dy * scale;
217 
218  return Result;
219 
220  }
221 
222  void PushBack(TArgumentType const& X, TResultType const& Y)
223  {
224  result_array_type a = {{Y}};
225  mData.push_back(RecordType(X,a));
226  }
227 
231 
232 
234  {
235  return mData;
236  }
237 
238  TableContainerType const& Data() const
239  {
240  return mData;
241  }
242 
244  {
245  return *mpXVariable;
246  }
247 
249  {
250  return *mpYVariable;
251  }
252 
253 
257 
258 
262 
264  virtual std::string Info() const
265  {
266  return "Table";
267  }
268 
270  virtual void PrintInfo(std::ostream& rOStream) const
271  {
272  rOStream << Info();
273  }
274 
276  virtual void PrintData(std::ostream& rOStream) const
277  {
278  }
279 
283 
284 
286 
287 protected:
290 
291 
295 
296 
300 
301 
305 
306 
310 
311 
315 
316 
320 
321 
323 
324 private:
327 
328 
332 
333  TableContainerType mData;
334  const XVariableType* mpXVariable;
335  const YVariableType* mpYVariable;
336 
340 
341 
342 
346 
347 
351 
352 
356 
357 
361 
363  Table(Table const& rOther);
364 
365 
367 
368 }; // Class Table
369 
371 
374 
375 
379 
380 
382 template<class TArgumentType, class TResultType>
383 inline std::istream& operator >> (std::istream& rIStream,
385 
387 template<class TArgumentType, class TResultType>
388 inline std::ostream& operator << (std::ostream& rOStream,
390 {
391  rThis.PrintInfo(rOStream);
392  rOStream << std::endl;
393  rThis.PrintData(rOStream);
394 
395  return rOStream;
396 }
397 
399 
401 
402 } // namespace Kratos.
403 
404 #endif // KRATOS_PIECEWIZE_LINEAR_TABLE_H_INCLUDED defined
405 
406 
This class represents the value of its variable depending to other variable.
Definition: piecewize_linear_table.h:63
YVariableType & GetYVariable()
Definition: piecewize_linear_table.h:248
std::vector< RecordType > TableContainerType
Definition: piecewize_linear_table.h:75
std::array< TResultType, TResultsColumns > result_array_type
Definition: piecewize_linear_table.h:71
TResultType GetValue(TArgumentType const &X) const
Definition: piecewize_linear_table.h:138
TResultType const & GetNearestValue(TArgumentType const &X) const
Definition: piecewize_linear_table.h:183
result_array_type & GetNearestRow(TArgumentType const &X)
Definition: piecewize_linear_table.h:161
TResultType & Interpolate(TArgumentType const &X, TArgumentType const &X1, TResultType const &Y1, TArgumentType const &X2, TResultType const &Y2, TResultType &Result)
Definition: piecewize_linear_table.h:204
Variable< TArgumentType > XVariableType
Definition: piecewize_linear_table.h:77
virtual std::string Info() const
Turn back information as a string.
Definition: piecewize_linear_table.h:264
TResultType & operator[](TArgumentType &X)
Definition: piecewize_linear_table.h:128
std::pair< TArgumentType, result_array_type > RecordType
Definition: piecewize_linear_table.h:73
Variable< TResultType > YVariableType
Definition: piecewize_linear_table.h:78
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: piecewize_linear_table.h:276
KRATOS_CLASS_POINTER_DEFINITION(Table)
Pointer definition of Table.
TResultType const & operator[](TArgumentType const &X) const
Definition: piecewize_linear_table.h:122
XVariableType & GetXVariable()
Definition: piecewize_linear_table.h:243
void PushBack(TArgumentType const &X, TResultType const &Y)
Definition: piecewize_linear_table.h:222
virtual ~Table()
Destructor.
Definition: piecewize_linear_table.h:95
TResultType operator()(TArgumentType const &X) const
Definition: piecewize_linear_table.h:116
Table & operator=(Table const &rOther)
Assignment operator.
Definition: piecewize_linear_table.h:101
Table(XVariableType const &XVariable, YVariableType const &YVariable)
Default constructor.
Definition: piecewize_linear_table.h:90
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: piecewize_linear_table.h:270
TableContainerType const & Data() const
Definition: piecewize_linear_table.h:238
Table()
Default constructor.
Definition: piecewize_linear_table.h:85
TableContainerType & Data()
Definition: piecewize_linear_table.h:233
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
X2
Definition: generate_frictional_mortar_condition.py:120
X1
Definition: generate_frictional_mortar_condition.py:119
a
Definition: generate_stokes_twofluid_element.py:77
integer i
Definition: TensorModule.f:17
e
Definition: run_cpp_mpi_tests.py:31