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.
variable.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 // Collaborator: Vicente Mataix Ferrandiz
13 //
14 //
15 
16 #if !defined(KRATOS_VARIABLE_H_INCLUDED )
17 #define KRATOS_VARIABLE_H_INCLUDED
18 
19 // System includes
20 #include <string>
21 #include <iostream>
22 
23 // External includes
24 
25 // Project includes
26 #include "includes/define.h"
27 #include "includes/registry.h"
28 #include "variable_data.h"
30 
31 namespace Kratos
32 {
33 
36 
40 
44 
48 
52 
61 template<class TDataType>
62 class Variable : public VariableData
63 {
64 public:
67 
70 
72  typedef TDataType Type;
73 
74  // Type used for key values which defined in VariableData
76 
77  // Type of this varible with given TDataType
79 
83 
90  explicit Variable(
91  const std::string& NewName,
92  const TDataType Zero = TDataType(),
93  const VariableType* pTimeDerivativeVariable = nullptr
94  )
95  : VariableData(NewName, sizeof(TDataType)),
96  mZero(Zero),
97  mpTimeDerivativeVariable(pTimeDerivativeVariable)
98  {
99  RegisterThisVariable();
100  }
106  explicit Variable(
107  const std::string& NewName,
108  const VariableType* pTimeDerivativeVariable
109  )
110  : VariableData(NewName, sizeof(TDataType)),
111  mZero(TDataType()),
112  mpTimeDerivativeVariable(pTimeDerivativeVariable)
113  {
114  RegisterThisVariable();
115  }
116 
122  template<typename TSourceVariableType>
123  explicit Variable(
124  const std::string& rNewName,
125  TSourceVariableType* pSourceVariable,
126  char ComponentIndex,
127  const TDataType Zero = TDataType()
128  )
129  : VariableData(rNewName, sizeof(TDataType), pSourceVariable, ComponentIndex),
130  mZero(Zero)
131  {
132  RegisterThisVariable();
133  }
134 
141  template<typename TSourceVariableType>
142  explicit Variable(
143  const std::string& rNewName,
144  TSourceVariableType* pSourceVariable,
145  char ComponentIndex,
146  const VariableType* pTimeDerivativeVariable,
147  const TDataType Zero = TDataType()
148  )
149  : VariableData(rNewName, sizeof(TDataType), pSourceVariable, ComponentIndex),
150  mZero(Zero),
151  mpTimeDerivativeVariable(pTimeDerivativeVariable)
152  {
153  RegisterThisVariable();
154  }
155 
161  explicit Variable(const VariableType& rOtherVariable) :
162  VariableData(rOtherVariable),
163  mZero(rOtherVariable.mZero),
164  mpTimeDerivativeVariable(rOtherVariable.mpTimeDerivativeVariable)
165  {
166  // Here we don't register as we asume that the origin is already registered
167  }
168 
170  ~Variable() override {}
171 
175 
179  VariableType& operator=(const VariableType& rOtherVariable) = delete;
180 
184 
191  void* Clone(const void* pSource) const override
192  {
193  return new TDataType(*static_cast<const TDataType* >(pSource) );
194  }
195 
203  void* Copy(const void* pSource, void* pDestination) const override
204  {
205  return new(pDestination) TDataType(*static_cast<const TDataType* >(pSource) );
206  }
207 
214  void Assign(const void* pSource, void* pDestination) const override
215  {
216  (*static_cast<TDataType* >(pDestination) ) = (*static_cast<const TDataType* >(pSource) );
217  }
218 
224  void AssignZero(void* pDestination) const override
225  {
226  //(*static_cast<TDataType* >(pDestination) ) = mZero;
227  new (pDestination) TDataType(mZero);
228  }
229 
235  void Delete(void* pSource) const override
236  {
237  delete static_cast<TDataType* >(pSource);
238  }
239 
246  void Destruct(void* pSource) const override
247  {
248  static_cast<TDataType* >(pSource)->~TDataType();
249  }
250 
257  void Print(const void* pSource, std::ostream& rOStream) const override
258  {
259  if(IsComponent()) {
260  rOStream << Name() << " component of " << GetSourceVariable().Name() << " variable : " << *static_cast<const TDataType* >(pSource) ;
261  }
262  else {
263  rOStream << Name() << " : " << *static_cast<const TDataType* >(pSource) ;
264  }
265  }
266 
273  void PrintData(const void* pSource, std::ostream& rOStream) const override
274  {
275  rOStream << *static_cast<const TDataType* >(pSource) ;
276  }
277 
283  void Save(Serializer& rSerializer, void* pData) const override
284  {
285  // I'm saving by the value, it can be done by the pointer to detect shared data. Pooyan.
286  rSerializer.save("Data",*static_cast<TDataType* >(pData));
287  }
288 
293  void Allocate(void** pData) const override
294  {
295  *pData = new TDataType;
296  }
297 
303  void Load(Serializer& rSerializer, void* pData) const override
304  {
305  rSerializer.load("Data",*static_cast<TDataType* >(pData));
306  }
307 
312  static const VariableType& StaticObject()
313  {
314  const static Variable<TDataType> static_object("NONE");
315  return static_object;
316  }
317 
318  TDataType& GetValue(void* pSource) const
319  {
320  return GetValueByIndex(static_cast<TDataType*>(pSource),GetComponentIndex());
321  }
322 
323  const TDataType& GetValue(const void* pSource) const
324  {
325  return GetValueByIndex(static_cast<TDataType*>(pSource),GetComponentIndex());
326  }
327 
331 
337  {
338  KRATOS_DEBUG_ERROR_IF(mpTimeDerivativeVariable == nullptr) << "Time derivative for Variable \"" << Name() << "\" was not assigned" << std::endl;
339  return *mpTimeDerivativeVariable;
340  }
341 
346  const TDataType& Zero() const
347  {
348  return mZero;
349  }
350 
351  const void* pZero() const override {
352  return &mZero;
353  }
354 
358 
359 
363 
367  std::string Info() const override
368  {
369  std::stringstream buffer;
370  buffer << Name() << " variable" <<" #" << static_cast<unsigned int>(Key());
371  if(IsComponent()){
372  buffer << Name() << " variable #" << static_cast<unsigned int>(Key()) << " component " << GetComponentIndex() << " of " << GetSourceVariable().Name();
373  }
374  else {
375  buffer << Name() << " variable #" << static_cast<unsigned int>(Key());
376  }
377  return buffer.str();
378  }
379 
384  void PrintInfo(std::ostream& rOStream) const override
385  {
386  rOStream << Info();
387  }
388 
390  void PrintData(std::ostream& rOStream) const override{
391  VariableData::PrintData(rOStream);
392  }
393 
397 
398 
400 
401 protected:
404 
405 
409 
410 
414 
415 
419 
420 
424 
425 
429 
430 
434 
436 private:
439 
440  static const VariableType msStaticObject;
441 
445 
446  TDataType mZero;
447 
448  const VariableType* mpTimeDerivativeVariable = nullptr;
449 
453 
454 
458 
460 
462  template<typename TValueType>
463  TDataType& GetValueByIndex(TValueType* pValue, std::size_t index) const
464  {
465  return *static_cast<TDataType*>(pValue + index);
466  }
467 
468  template<typename TValueType>
469  const TDataType& GetValueByIndex(const TValueType* pValue, std::size_t index) const
470  {
471  return *static_cast<const TDataType*>(pValue + index);
472  }
473 
474  void RegisterThisVariable(){
475  std::string variable_path = "variables.all." + Name();
476  if(!Registry::HasItem(variable_path)){
477  Registry::AddItem<VariableType>(variable_path, *this);
478  }
479  }
480 
484 
485  friend class Serializer;
486 
491  void save(Serializer& rSerializer) const override
492  {
494  rSerializer.save("Zero",mZero);
495  rSerializer.save("TimeDerivativeVariable",mpTimeDerivativeVariable);
496  }
497 
502  void load(Serializer& rSerializer) override
503  {
505  rSerializer.load("Zero",mZero);
506  rSerializer.load("TimeDerivativeVariable",mpTimeDerivativeVariable);
507  }
508 
512 
513 
517 
518 
522 
525  Variable() {}
526 
528 
529 }; // Class Variable
530 
532 
535 
536 
540 
541 
543 template<class TDataType>
544 inline std::istream& operator >> (std::istream& rIStream,
545  Variable<TDataType>& rThis);
546 
548 template<class TDataType>
549 inline std::ostream& operator << (std::ostream& rOStream,
550  const Variable<TDataType>& rThis)
551 {
552  rThis.PrintInfo(rOStream);
553  rThis.PrintData(rOStream);
554 
555  return rOStream;
556 }
558 
559 } // namespace Kratos.
560 
561 #endif // KRATOS_VARIABLE_H_INCLUDED defined
static bool HasItem(std::string const &rItemFullName)
Definition: registry.cpp:88
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
void load(std::string const &rTag, TDataType &rObject)
Definition: serializer.h:207
void save(std::string const &rTag, std::array< TDataType, TDataSize > const &rObject)
Definition: serializer.h:545
This class is the base of variables and variable's components which contains their common data.
Definition: variable_data.h:49
virtual void PrintData(const void *pSource, std::ostream &rOStream) const
Definition: variable_data.cpp:80
KeyType GetComponentIndex() const
Returns the component index.
Definition: variable_data.h:227
const VariableData & GetSourceVariable() const
Definition: variable_data.h:232
bool IsComponent() const
Definition: variable_data.h:211
const std::string & Name() const
Definition: variable_data.h:201
KeyType Key() const
Definition: variable_data.h:187
std::size_t KeyType
Definition: variable_data.h:57
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
void Allocate(void **pData) const override
This method allocates the data of the variable.
Definition: variable.h:293
void Assign(const void *pSource, void *pDestination) const override
Assign is very similar to Copy. It just differs in using an assignment operator besides the copy cons...
Definition: variable.h:214
Variable(const std::string &rNewName, TSourceVariableType *pSourceVariable, char ComponentIndex, const VariableType *pTimeDerivativeVariable, const TDataType Zero=TDataType())
Constructor for creating a component of other variable.
Definition: variable.h:142
void Load(Serializer &rSerializer, void *pData) const override
The load operation which restores the data of the class.
Definition: variable.h:303
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: variable.h:390
TDataType & GetValue(void *pSource) const
Definition: variable.h:318
KRATOS_CLASS_POINTER_DEFINITION(Variable)
Pointer definition of Variable.
void Save(Serializer &rSerializer, void *pData) const override
The save operation which backups the data of the class.
Definition: variable.h:283
static const VariableType & StaticObject()
This method returns the variable type.
Definition: variable.h:312
VariableType & operator=(const VariableType &rOtherVariable)=delete
Assignment operator, deleted to avoid misuse which can lead to memory problems.
Variable(const std::string &NewName, const VariableType *pTimeDerivativeVariable)
Constructor with specific name and zero value.
Definition: variable.h:106
const void * pZero() const override
Definition: variable.h:351
const TDataType & Zero() const
This method returns the zero value of the variable type.
Definition: variable.h:346
void Delete(void *pSource) const override
Delete removes an object of variable type from memory.
Definition: variable.h:235
void PrintData(const void *pSource, std::ostream &rOStream) const override
PrintData is an auxiliary method to produce output only the value of given variable knowing its addre...
Definition: variable.h:273
std::string Info() const override
Definition: variable.h:367
void * Copy(const void *pSource, void *pDestination) const override
Copy is very similar to Clone except that it also the destination pointer also passed to it.
Definition: variable.h:203
const TDataType & GetValue(const void *pSource) const
Definition: variable.h:323
const VariableType & GetTimeDerivative() const
This method returns the time derivative variable.
Definition: variable.h:336
Variable(const std::string &NewName, const TDataType Zero=TDataType(), const VariableType *pTimeDerivativeVariable=nullptr)
Constructor with specific name and zero value.
Definition: variable.h:90
VariableData::KeyType KeyType
Definition: variable.h:75
void Print(const void *pSource, std::ostream &rOStream) const override
Print is an auxiliary method to produce output of given variable knowing its address.
Definition: variable.h:257
Variable(const VariableType &rOtherVariable)
Copy constructor.
Definition: variable.h:161
void * Clone(const void *pSource) const override
Clone creates a copy of the object using a copy constructor of the class.
Definition: variable.h:191
void Destruct(void *pSource) const override
Destruct eliminates an object maintaining the memory it is using.
Definition: variable.h:246
void PrintInfo(std::ostream &rOStream) const override
Definition: variable.h:384
Variable< TDataType > VariableType
Definition: variable.h:78
void AssignZero(void *pDestination) const override
AssignZero is a special case of Assign for which variable zero value used as source.
Definition: variable.h:224
~Variable() override
Destructor.
Definition: variable.h:170
TDataType Type
type of this variable
Definition: variable.h:72
Variable(const std::string &rNewName, TSourceVariableType *pSourceVariable, char ComponentIndex, const TDataType Zero=TDataType())
Constructor for creating a component of other variable.
Definition: variable.h:123
#define KRATOS_SERIALIZE_SAVE_BASE_CLASS(Serializer, BaseType)
Definition: define.h:812
#define KRATOS_SERIALIZE_LOAD_BASE_CLASS(Serializer, BaseType)
Definition: define.h:815
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
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
def load(f)
Definition: ode_solve.py:307