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.
dof.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 //
12 //
13 
14 #pragma once
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 #include <sstream>
20 #include <cstddef>
21 
22 // External includes
23 
24 // Project includes
25 #include "includes/define.h"
27 #include "containers/nodal_data.h"
28 
29 namespace Kratos
30 {
31 
32 #define KRATOS_DOF_TRAITS \
33  KRATOS_MAKE_DOF_TRAIT(0) Variable<TDataType> KRATOS_END_DOF_TRAIT(0);
34 
35 template<class TDataType, class TVariableType = Variable<TDataType> >
36 struct DofTrait
37 {
38  static const int Id;
39 };
40 
41 
42 #define KRATOS_MAKE_DOF_TRAIT(id) \
43  template<class TDataType> \
44  struct DofTrait<TDataType,
45 
46 
47 #define KRATOS_END_DOF_TRAIT(id) \
48  >{\
49  static const int Id = id;\
50  }
51 
52 
54 
55 
56 #undef KRATOS_MAKE_DOF_TRAIT
57 #undef KRATOS_END_DOF_TRAIT
58 
59 
62 
66 
70 
74 
78 
80 
85 template<class TDataType>
86 class Dof{
87 public:
90 
92  //KRATOS_CLASS_POINTER_DEFINITION(Dof);
93  using Pointer=Dof*;
94 
95  typedef std::size_t IndexType;
96 
97  typedef std::size_t EquationIdType;
98 
100 
104 
118  template<class TVariableType>
119  Dof(NodalData* pThisNodalData,
120  const TVariableType& rThisVariable)
121  : mIsFixed(false),
122  mVariableType(DofTrait<TDataType, TVariableType>::Id),
123  mReactionType(DofTrait<TDataType, Variable<TDataType> >::Id),
124  mEquationId(IndexType()),
125  mpNodalData(pThisNodalData)
126  {
127  KRATOS_DEBUG_ERROR_IF_NOT(pThisNodalData->GetSolutionStepData().Has(rThisVariable))
128  << "The Dof-Variable " << rThisVariable.Name() << " is not "
129  << "in the list of variables" << std::endl;
130 
131  mIndex = mpNodalData->GetSolutionStepData().pGetVariablesList()->AddDof(&rThisVariable);
132  }
133 
155  template<class TVariableType, class TReactionType>
156  Dof(NodalData* pThisNodalData,
157  const TVariableType& rThisVariable,
158  const TReactionType& rThisReaction)
159  : mIsFixed(false),
160  mVariableType(DofTrait<TDataType, TVariableType>::Id),
161  mReactionType(DofTrait<TDataType, TReactionType>::Id),
162  mEquationId(IndexType()),
163  mpNodalData(pThisNodalData)
164  {
165  KRATOS_DEBUG_ERROR_IF_NOT(pThisNodalData->GetSolutionStepData().Has(rThisVariable))
166  << "The Dof-Variable " << rThisVariable.Name() << " is not "
167  << "in the list of variables" << std::endl;
168 
169  KRATOS_DEBUG_ERROR_IF_NOT(pThisNodalData->GetSolutionStepData().Has(rThisReaction))
170  << "The Reaction-Variable " << rThisReaction.Name() << " is not "
171  << "in the list of variables" << std::endl;
172 
173  mIndex = mpNodalData->GetSolutionStepData().pGetVariablesList()->AddDof(&rThisVariable, &rThisReaction);
174  }
175 
176  //This default constructor is needed for serializer
177  Dof()
178  : mIsFixed(false),
179  mVariableType(DofTrait<TDataType, Variable<TDataType> >::Id),
180  mReactionType(DofTrait<TDataType, Variable<TDataType> >::Id),
181  mIndex(),
182  mEquationId(IndexType()),
183  mpNodalData()
184  {
185  }
186 
188  Dof(Dof const& rOther)
189  : mIsFixed(rOther.mIsFixed),
190  mVariableType(rOther.mVariableType),
191  mReactionType(rOther.mReactionType),
192  mIndex(rOther.mIndex),
193  mEquationId(rOther.mEquationId),
194  mpNodalData(rOther.mpNodalData)
195  {
196  }
197 
198 
200  ~Dof() {}
201 
202 
206 
208  Dof& operator=(Dof const& rOther)
209  {
210  mIsFixed = rOther.mIsFixed;
211  mEquationId = rOther.mEquationId;
212  mpNodalData = rOther.mpNodalData;
213  mIndex = rOther.mIndex;
214  mVariableType = rOther.mVariableType;
215  mReactionType = rOther.mReactionType;
216 
217  return *this;
218  }
219 
220  template<class TVariableType>
221  typename TVariableType::Type& operator()(const TVariableType& rThisVariable, IndexType SolutionStepIndex = 0)
222  {
223  return GetSolutionStepValue(rThisVariable, SolutionStepIndex);
224  }
225 
226  template<class TVariableType>
227  typename TVariableType::Type const& operator()(const TVariableType& rThisVariable, IndexType SolutionStepIndex = 0) const
228  {
229  return GetSolutionStepValue(rThisVariable, SolutionStepIndex);
230  }
231 
232  TDataType& operator()(IndexType SolutionStepIndex = 0)
233  {
234  return GetSolutionStepValue(SolutionStepIndex);
235  }
236 
237  TDataType const& operator()(IndexType SolutionStepIndex = 0) const
238  {
239  return GetSolutionStepValue(SolutionStepIndex);
240  }
241 
242  TDataType& operator[](IndexType SolutionStepIndex)
243  {
244  return GetSolutionStepValue(SolutionStepIndex);
245  }
246 
247  TDataType const& operator[](IndexType SolutionStepIndex) const
248  {
249  return GetSolutionStepValue(SolutionStepIndex);
250  }
251 
255 
256  TDataType& GetSolutionStepValue(IndexType SolutionStepIndex = 0)
257  {
258  return GetReference(GetVariable(), mpNodalData->GetSolutionStepData(), SolutionStepIndex, mVariableType);
259  }
260 
261  TDataType const& GetSolutionStepValue(IndexType SolutionStepIndex = 0) const
262  {
263  return GetReference(GetVariable(), mpNodalData->GetSolutionStepData(), SolutionStepIndex, mVariableType);
264  }
265 
266  template<class TVariableType>
267  typename TVariableType::Type& GetSolutionStepValue(const TVariableType& rThisVariable, IndexType SolutionStepIndex = 0)
268  {
269  return mpNodalData->GetSolutionStepData().GetValue(rThisVariable, SolutionStepIndex);
270  }
271 
272  template<class TVariableType>
273  typename TVariableType::Type const& GetSolutionStepValue(const TVariableType& rThisVariable, IndexType SolutionStepIndex = 0) const
274  {
275  return mpNodalData->GetSolutionStepData().GetValue(rThisVariable, SolutionStepIndex);
276  }
277 
278  TDataType& GetSolutionStepReactionValue(IndexType SolutionStepIndex = 0)
279  {
280  return GetReference(GetReaction(), mpNodalData->GetSolutionStepData(), SolutionStepIndex, mReactionType);
281  }
282 
283  TDataType const& GetSolutionStepReactionValue(IndexType SolutionStepIndex = 0) const
284  {
285  return GetReference(GetReaction(), mpNodalData->GetSolutionStepData(), SolutionStepIndex, mReactionType);
286  }
287 
291 
292  IndexType Id() const
293  {
294  return mpNodalData->GetId();
295  }
296 
297  IndexType GetId() const
298  {
299  return mpNodalData->GetId();
300  }
301 
303  const VariableData& GetVariable() const
304  {
305  return mpNodalData->GetSolutionStepData().GetVariablesList().GetDofVariable(mIndex);
306  }
307 
309  const VariableData& GetReaction() const
310  {
311  auto p_reaction = mpNodalData->GetSolutionStepData().GetVariablesList().pGetDofReaction(mIndex);
312  return (p_reaction == nullptr) ? msNone : *p_reaction;
313  }
314 
315  template<class TReactionType>
316  void SetReaction(TReactionType const& rReaction)
317  {
319  mpNodalData->GetSolutionStepData().pGetVariablesList()->SetDofReaction(&rReaction, mIndex);
320  }
321 
325  {
326  return mEquationId;
327  }
328 
331  void SetEquationId(EquationIdType NewEquationId)
332  {
333  mEquationId = NewEquationId;
334  }
335 
338  void FixDof()
339  {
340  mIsFixed=true;
341  }
342 
345  void FreeDof()
346  {
347  mIsFixed=false;
348  }
349 
351  {
352  return &(mpNodalData->GetSolutionStepData());
353  }
354 
355  void SetNodalData(NodalData* pNewNodalData)
356  {
357  auto p_variable = &GetVariable();
358  auto p_reaction = mpNodalData->GetSolutionStepData().pGetVariablesList()->pGetDofReaction(mIndex);
359  mpNodalData = pNewNodalData;
360  if(p_reaction != nullptr){
361  mIndex = mpNodalData->GetSolutionStepData().pGetVariablesList()->AddDof(p_variable, p_reaction);
362  } else{
363  mIndex = mpNodalData->GetSolutionStepData().pGetVariablesList()->AddDof(p_variable);
364  }
365  }
366 
367  bool HasReaction() const
368  {
369  return (mpNodalData->GetSolutionStepData().pGetVariablesList()->pGetDofReaction(mIndex) != nullptr);
370  }
371 
375 
376  bool IsFixed() const
377  {
378  return mIsFixed;
379  }
380 
381 
382  bool IsFree() const
383  {
384  return !IsFixed();
385  }
386 
390 
391 
393  std::string Info() const
394  {
395  std::stringstream buffer;
396 
397  if(IsFixed())
398  buffer << "Fix " << GetVariable().Name() << " degree of freedom";
399  else
400  buffer << "Free " << GetVariable().Name() << " degree of freedom";
401 
402  return buffer.str();
403  }
404 
406  void PrintInfo(std::ostream& rOStream) const
407  {
408  rOStream << Info();
409  }
410 
412  void PrintData(std::ostream& rOStream) const
413  {
414  rOStream << " Variable : " << GetVariable().Name() << std::endl;
415  rOStream << " Reaction : " << GetReaction().Name() << std::endl;
416  if(IsFixed())
417  rOStream << " IsFixed : True" << std::endl;
418  else
419  rOStream << " IsFixed : False" << std::endl;
420  rOStream << " Equation Id : " << mEquationId << std::endl;
421  }
422 
426 
428 private:
431 
432  static const Variable<TDataType> msNone;
433  static constexpr int msIsFixedPosition = 63;
434 
438 
440  int mIsFixed : 1;
441 
442  int mVariableType : 4;
443 
444  int mReactionType : 4;
445 
446  int mIndex : 6;
447 
449  EquationIdType mEquationId : 48;
450 
452  NodalData* mpNodalData;
453 
457 #define KRATOS_MAKE_DOF_TRAIT(id) \
458  case id : \
459  return rData.GetValue(static_cast<
460 
461 
462 #define KRATOS_END_DOF_TRAIT(id) \
463  const&>(ThisVariable), SolutionStepIndex);
464 
465  TDataType& GetReference(VariableData const& ThisVariable, VariablesListDataValueContainer& rData, IndexType SolutionStepIndex, int ThisId)
466  {
467  switch(ThisId)
468  {
470  }
471  KRATOS_ERROR << "Not supported type for Dof" << std::endl;
472  }
473 
474  TDataType const& GetReference(VariableData const& ThisVariable, VariablesListDataValueContainer const& rData, IndexType SolutionStepIndex, int ThisId) const
475  {
476  switch(ThisId)
477  {
479  }
480  KRATOS_ERROR << "Not supported type for Dof" << std::endl;
481  }
482 
486 
487  friend class Serializer;
488 
489  void save(Serializer& rSerializer) const
490  {
491  rSerializer.save("IsFixed", static_cast<bool>(mIsFixed));
492  rSerializer.save("EquationId", static_cast<EquationIdType>(mEquationId));
493  rSerializer.save("NodalData", mpNodalData);
494  rSerializer.save("VariableType", static_cast<int>(mVariableType));
495  rSerializer.save("ReactionType", static_cast<int>(mReactionType));
496  rSerializer.save("Index", static_cast<int>(mIndex));
497 
498  }
499 
500  void load(Serializer& rSerializer)
501  {
502  std::string name;
503  bool is_fixed;
504  rSerializer.load("IsFixed", is_fixed);
505  mIsFixed=is_fixed;
506  EquationIdType equation_id;
507  rSerializer.load("EquationId", equation_id);
508  mEquationId = equation_id;
509  rSerializer.load("NodalData", mpNodalData);
510 
511  int variable_type;
512  int reaction_type;
513  rSerializer.load("VariableType", variable_type);
514  rSerializer.load("ReactionType", reaction_type);
515 
516  mVariableType = variable_type;
517  mReactionType = reaction_type;
518 
519  int index;
520  rSerializer.load("Index", index);
521  mIndex = index;
522  }
526 
530 
534 
536 
537 }; // Class Dof
538 template<class TDataType> const Variable<TDataType> Dof<TDataType>::msNone("NONE");
539 
543 
547 
549 template<class TDataType>
550 inline std::istream& operator >> (std::istream& rIStream,
551  Dof<TDataType>& rThis);
552 
553 
555 template<class TDataType>
556 inline std::ostream& operator << (std::ostream& rOStream,
557  const Dof<TDataType>& rThis)
558 {
559  rThis.PrintInfo(rOStream);
560  rOStream << std::endl;
561  rThis.PrintData(rOStream);
562 
563 
564  return rOStream;
565 }
569 
570 
572 template<class TDataType>
573 inline bool operator > ( Dof<TDataType> const& First,
574  Dof<TDataType> const& Second)
575 {
576  if(First.Id() == Second.Id())
577  return (First.GetVariable().Key() > Second.GetVariable().Key());
578 
579  return (First.Id() > Second.Id());
580 }
581 
583 template<class TDataType>
584 inline bool operator < ( Dof<TDataType> const& First,
585  Dof<TDataType> const& Second)
586 {
587  if(First.Id() == Second.Id())
588  return (First.GetVariable().Key() < Second.GetVariable().Key());
589 
590  return (First.Id() < Second.Id());
591 }
592 
594 template<class TDataType>
595 inline bool operator >= ( Dof<TDataType> const& First,
596  Dof<TDataType> const& Second)
597 {
598  if(First.Id() == Second.Id())
599  return (First.GetVariable().Key() >= Second.GetVariable().Key());
600 
601  return (First.Id() > Second.Id());
602 }
603 
605 template<class TDataType>
606 inline bool operator <= ( Dof<TDataType> const& First,
607  Dof<TDataType> const& Second)
608 {
609  if(First.Id() == Second.Id())
610  return (First.GetVariable().Key() <= Second.GetVariable().Key());
611 
612  return (First.Id() < Second.Id());
613 }
614 
616 template<class TDataType>
617 inline bool operator == ( Dof<TDataType> const& First,
618  Dof<TDataType> const& Second)
619 {
620  return ((First.Id() == Second.Id()) && (First.GetVariable().Key() == Second.GetVariable().Key()));
621 }
622 
624 
625 } // namespace Kratos.
626 
627 #undef KRATOS_DOF_TRAITS
628 #undef KRATOS_MAKE_DOF_TRAIT
629 #undef KRATOS_END_DOF_TRAIT
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
TDataType const & operator()(IndexType SolutionStepIndex=0) const
Definition: dof.h:237
std::size_t EquationIdType
Definition: dof.h:97
VariablesListDataValueContainer SolutionStepsDataContainerType
Definition: dof.h:99
Dof(Dof const &rOther)
Copy constructor.
Definition: dof.h:188
TVariableType::Type & GetSolutionStepValue(const TVariableType &rThisVariable, IndexType SolutionStepIndex=0)
Definition: dof.h:267
Dof & operator=(Dof const &rOther)
Assignment operator.
Definition: dof.h:208
const VariableData & GetVariable() const
Definition: dof.h:303
IndexType GetId() const
Definition: dof.h:297
TDataType & operator()(IndexType SolutionStepIndex=0)
Definition: dof.h:232
void SetEquationId(EquationIdType NewEquationId)
Definition: dof.h:331
void SetReaction(TReactionType const &rReaction)
Definition: dof.h:316
void FixDof()
Definition: dof.h:338
SolutionStepsDataContainerType * GetSolutionStepsData()
Definition: dof.h:350
void SetNodalData(NodalData *pNewNodalData)
Definition: dof.h:355
Dof(NodalData *pThisNodalData, const TVariableType &rThisVariable)
Definition: dof.h:119
IndexType Id() const
Definition: dof.h:292
std::string Info() const
Turn back information as a string.
Definition: dof.h:393
TDataType const & GetSolutionStepReactionValue(IndexType SolutionStepIndex=0) const
Definition: dof.h:283
bool IsFixed() const
Definition: dof.h:376
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: dof.h:406
Dof(NodalData *pThisNodalData, const TVariableType &rThisVariable, const TReactionType &rThisReaction)
Definition: dof.h:156
void FreeDof()
Definition: dof.h:345
TVariableType::Type const & GetSolutionStepValue(const TVariableType &rThisVariable, IndexType SolutionStepIndex=0) const
Definition: dof.h:273
TVariableType::Type const & operator()(const TVariableType &rThisVariable, IndexType SolutionStepIndex=0) const
Definition: dof.h:227
Dof()
Definition: dof.h:177
const VariableData & GetReaction() const
Definition: dof.h:309
bool HasReaction() const
Definition: dof.h:367
bool IsFree() const
Definition: dof.h:382
TVariableType::Type & operator()(const TVariableType &rThisVariable, IndexType SolutionStepIndex=0)
Definition: dof.h:221
TDataType const & operator[](IndexType SolutionStepIndex) const
Definition: dof.h:247
TDataType const & GetSolutionStepValue(IndexType SolutionStepIndex=0) const
Definition: dof.h:261
EquationIdType EquationId() const
Definition: dof.h:324
~Dof()
Destructor.
Definition: dof.h:200
TDataType & GetSolutionStepValue(IndexType SolutionStepIndex=0)
Definition: dof.h:256
TDataType & GetSolutionStepReactionValue(IndexType SolutionStepIndex=0)
Definition: dof.h:278
TDataType & operator[](IndexType SolutionStepIndex)
Definition: dof.h:242
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: dof.h:412
std::size_t IndexType
Definition: dof.h:95
Stores all data and dofs which are stored in each elements.
Definition: nodal_data.h:37
VariablesListDataValueContainer & GetSolutionStepData()
Definition: nodal_data.h:106
IndexType GetId() const
Returns the Id of the Node.
Definition: nodal_data.h:89
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
const std::string & Name() const
Definition: variable_data.h:201
KeyType Key() const
Definition: variable_data.h:187
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
A shared variable list gives the position of each variable in the containers sharing it.
Definition: variables_list_data_value_container.h:61
TDataType & GetValue(const Variable< TDataType > &rThisVariable)
Definition: variables_list_data_value_container.h:282
bool Has(const VariableData &rThisVariable) const
This method returns if a certain variable is stored in the data value container.
Definition: variables_list_data_value_container.h:704
VariablesList::Pointer pGetVariablesList()
Definition: variables_list_data_value_container.h:424
VariablesList & GetVariablesList()
Definition: variables_list_data_value_container.h:434
const VariableData * pGetDofReaction(int DofIndex) const
Definition: variables_list.h:327
const VariableData & GetDofVariable(int DofIndex) const
Definition: variables_list.h:323
#define KRATOS_DOF_TRAITS
Definition: dof.h:32
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_DEBUG_ERROR_IF_NOT(conditional)
Definition: exception.h:172
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
bool operator<=(Dof< TDataType > const &First, Dof< TDataType > const &Second)
Less equal operator.
Definition: dof.h:606
bool operator<(Dof< TDataType > const &First, Dof< TDataType > const &Second)
Less than operator.
Definition: dof.h:584
bool operator==(const Flags &Left, const Flags &Right)
Definition: flags.cpp:45
bool operator>(Dof< TDataType > const &First, Dof< TDataType > const &Second)
Greater than operator.
Definition: dof.h:573
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
bool operator>=(Dof< TDataType > const &First, Dof< TDataType > const &Second)
Greater equal operator.
Definition: dof.h:595
def load(f)
Definition: ode_solve.py:307
Definition: dof.h:37
static const int Id
Definition: dof.h:38