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.
builder_and_solver.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: Riccardo Rossi
11 //
12 //
13 
14 #pragma once
15 
16 /* System includes */
17 #include <set>
18 
19 /* External includes */
20 
21 /* Project includes */
22 #include "includes/define.h"
23 #include "includes/model_part.h"
26 
27 namespace Kratos
28 {
31 
32 
36 
40 
41 
45 
49 
59 template<class TSparseSpace,
60  class TDenseSpace, // = DenseSpace<double>,
61  class TLinearSolver //= LinearSolver<TSparseSpace,TDenseSpace>
62  >
64 {
65 public:
68 
71 
73  typedef std::size_t SizeType;
74 
76  typedef std::size_t IndexType;
77 
79  typedef typename TSparseSpace::DataType TDataType;
80 
83 
86 
88  typedef typename TSparseSpace::MatrixPointerType TSystemMatrixPointerType;
89 
91  typedef typename TSparseSpace::VectorPointerType TSystemVectorPointerType;
92 
95 
98 
101 
104 
107 
112 
115 
118 
122 
126  explicit BuilderAndSolver()
127  {
128  }
129 
136  typename TLinearSolver::Pointer pNewLinearSystemSolver,
137  Parameters ThisParameters
138  )
139  {
140  // Validate and assign defaults
141  ThisParameters = this->ValidateAndAssignParameters(ThisParameters, this->GetDefaultParameters());
142  this->AssignSettings(ThisParameters);
143 
144  // We set the other member variables
145  mpLinearSystemSolver = pNewLinearSystemSolver;
146  }
147 
152  explicit BuilderAndSolver(typename TLinearSolver::Pointer pNewLinearSystemSolver)
153  {
154  mpLinearSystemSolver = pNewLinearSystemSolver;
155  }
156 
160  {
161  }
162 
168  virtual ClassType::Pointer Create(
169  typename TLinearSolver::Pointer pNewLinearSystemSolver,
170  Parameters ThisParameters
171  ) const
172  {
173  return Kratos::make_shared<ClassType>(pNewLinearSystemSolver,ThisParameters);
174  }
175 
179 
185  {
187  }
188 
194  {
196  }
197 
203  {
204  return mDofSetIsInitialized;
205  }
206 
211  void SetDofSetIsInitializedFlag(bool DofSetIsInitialized)
212  {
213  mDofSetIsInitialized = DofSetIsInitialized;
214  }
215 
220  bool GetReshapeMatrixFlag() const
221  {
222  return mReshapeMatrixFlag;
223  }
224 
229  void SetReshapeMatrixFlag(bool ReshapeMatrixFlag)
230  {
231  mReshapeMatrixFlag = ReshapeMatrixFlag;
232  }
233 
238  unsigned int GetEquationSystemSize() const
239  {
240  return mEquationSystemSize;
241  }
242 
247  typename TLinearSolver::Pointer GetLinearSystemSolver() const
248  {
249  return mpLinearSystemSolver;
250  }
251 
256  void SetLinearSystemSolver(typename TLinearSolver::Pointer pLinearSystemSolver)
257  {
258  mpLinearSystemSolver = pLinearSystemSolver;
259  }
260 
267  virtual void BuildLHS(
268  typename TSchemeType::Pointer pScheme,
269  ModelPart& rModelPart,
271  )
272  {
273  }
274 
281  virtual void BuildRHS(
282  typename TSchemeType::Pointer pScheme,
283  ModelPart& rModelPart,
285  )
286  {
287  }
288 
296  virtual void Build(
297  typename TSchemeType::Pointer pScheme,
298  ModelPart& rModelPart,
299  TSystemMatrixType& rA,
301  )
302  {
303  }
304 
313  typename TSchemeType::Pointer pScheme,
314  ModelPart& rModelPart,
316  )
317  {
318  }
319 
327  virtual void BuildLHS_Complete(
328  typename TSchemeType::Pointer pScheme,
329  ModelPart& rModelPart,
331  )
332  {
333  }
334 
341  virtual void SystemSolve(
342  TSystemMatrixType& rA,
343  TSystemVectorType& rDx,
345  )
346  {
347  }
348 
358  virtual void BuildAndSolve(
359  typename TSchemeType::Pointer pScheme,
360  ModelPart& rModelPart,
361  TSystemMatrixType& rA,
362  TSystemVectorType& rDx,
363  TSystemVectorType& rb)
364  {
365  }
366 
378  typename TSchemeType::Pointer pScheme,
379  ModelPart& rModelPart,
380  TSystemMatrixType& rA,
381  TSystemVectorType& rDx,
382  TSystemVectorType& rb,
383  const bool MoveMesh
384  )
385  {
386  KRATOS_ERROR << "No special implementation available for "
387  << "BuildAndSolveLinearizedOnPreviousIteration "
388  << " please use UseOldStiffnessInFirstIterationFlag=false in the settings of the strategy "
389  << std::endl;
390  }
391 
400  virtual void BuildRHSAndSolve(
401  typename TSchemeType::Pointer pScheme,
402  ModelPart& rModelPart,
403  TSystemMatrixType& rA,
404  TSystemVectorType& rDx,
406  )
407  {
408  }
409 
420  typename TSchemeType::Pointer pScheme,
421  ModelPart& rModelPart,
422  TSystemMatrixType& rA,
423  TSystemVectorType& rDx,
425  )
426  {
427  }
428 
437  typename TSchemeType::Pointer pScheme,
438  ModelPart& rModelPart,
439  TSystemMatrixType& rA,
440  TSystemVectorType& rDx
441  )
442  {
443  }
444 
453  typename TSchemeType::Pointer pScheme,
454  ModelPart& rModelPart,
455  TSystemVectorType& rDx,
457  )
458  {
459  }
460 
467  virtual void ApplyRHSConstraints(
468  typename TSchemeType::Pointer pScheme,
469  ModelPart& rModelPart,
471  )
472  {
473  }
474 
482  virtual void ApplyConstraints(
483  typename TSchemeType::Pointer pScheme,
484  ModelPart& rModelPart,
485  TSystemMatrixType& rA,
487  )
488  {
489  }
490 
497  virtual void SetUpDofSet(
498  typename TSchemeType::Pointer pScheme,
499  ModelPart& rModelPart
500  )
501  {
502  }
503 
508  {
509  return mDofSet;
510  }
511 
515  virtual const DofsArrayType& GetDofSet() const
516  {
517  return mDofSet;
518  }
519 
524  virtual void SetUpSystem(ModelPart& rModelPart)
525  {
526  }
527 
537  typename TSchemeType::Pointer pScheme,
541  ModelPart& rModelPart
542  )
543  {
544  }
545 
554  ModelPart& rModelPart,
555  TSystemMatrixType& rA,
556  TSystemVectorType& rDx,
558  )
559  {
560  }
561 
569  virtual void FinalizeSolutionStep(
570  ModelPart& rModelPart,
571  TSystemMatrixType& rA,
572  TSystemVectorType& rDx,
574  )
575  {
576  }
577 
586  virtual void CalculateReactions(
587  typename TSchemeType::Pointer pScheme,
588  ModelPart& rModelPart,
589  TSystemMatrixType& rA,
590  TSystemVectorType& rDx,
592  )
593  {
594  }
595 
600  virtual void Clear()
601  {
602  this->mDofSet = DofsArrayType();
603  this->mpReactionsVector.reset();
604  if (this->mpLinearSystemSolver != nullptr) this->mpLinearSystemSolver->Clear();
605 
606  KRATOS_INFO_IF("BuilderAndSolver", this->GetEchoLevel() > 0) << "Clear Function called" << std::endl;
607  }
608 
616  virtual int Check(ModelPart& rModelPart)
617  {
618  KRATOS_TRY
619 
620  return 0;
621  KRATOS_CATCH("");
622  }
623 
629  {
630  const Parameters default_parameters = Parameters(R"(
631  {
632  "name" : "builder_and_solver",
633  "echo_level" : 1
634  })" );
635  return default_parameters;
636  }
637 
642  static std::string Name()
643  {
644  return "builder_and_solver";
645  }
646 
650 
654 
665  void SetEchoLevel(int Level)
666  {
667  mEchoLevel = Level;
668  }
669 
674  int GetEchoLevel() const
675  {
676  return mEchoLevel;
677  }
678 
684  {
685  KRATOS_ERROR << "GetConstraintRelationMatrix is not implemented in base BuilderAndSolver" << std::endl;
686  }
687 
693  {
694  KRATOS_ERROR << "GetConstraintConstantVector is not implemented in base BuilderAndSolver" << std::endl;
695  }
696 
700 
704 
706  virtual std::string Info() const
707  {
708  return "BuilderAndSolver";
709  }
710 
712  virtual void PrintInfo(std::ostream& rOStream) const
713  {
714  rOStream << Info();
715  }
716 
718  virtual void PrintData(std::ostream& rOStream) const
719  {
720  rOStream << Info();
721  }
722 
726 
728 
729 protected:
732 
736 
737  typename TLinearSolver::Pointer mpLinearSystemSolver = nullptr;
738 
740 
741  bool mReshapeMatrixFlag = false;
742 
743  bool mDofSetIsInitialized = false;
744 
745  bool mCalculateReactionsFlag = false;
746 
747  unsigned int mEquationSystemSize;
748 
749  int mEchoLevel = 0;
750 
752 
756 
760 
768  Parameters ThisParameters,
769  const Parameters DefaultParameters
770  ) const
771  {
772  ThisParameters.ValidateAndAssignDefaults(DefaultParameters);
773  return ThisParameters;
774  }
775 
780  virtual void AssignSettings(const Parameters ThisParameters)
781  {
782  mEchoLevel = ThisParameters["echo_level"].GetInt();
783  }
784 
788 
792 
796 
798 
799 private:
802 
806 
810 
814 
818 
822 
826 
827 
829 
830 }; /* Class BuilderAndSolver */
831 
833 
836 
837 
839 
840 } /* namespace Kratos.*/
Current class provides an implementation for the base builder and solving operations.
Definition: builder_and_solver.h:64
ModelPart::DofType TDofType
Definition of the DoF class.
Definition: builder_and_solver.h:103
std::size_t IndexType
Definition of the index type.
Definition: builder_and_solver.h:76
virtual std::string Info() const
Turn back information as a string.
Definition: builder_and_solver.h:706
void SetReshapeMatrixFlag(bool ReshapeMatrixFlag)
This method sets the flag mReshapeMatrixFlag.
Definition: builder_and_solver.h:229
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: builder_and_solver.h:712
bool mDofSetIsInitialized
If the matrix is reshaped each step.
Definition: builder_and_solver.h:743
PointerVectorSet< Element, IndexedObject > ElementsContainerType
The definition of the element container type.
Definition: builder_and_solver.h:114
virtual void BuildLHS_CompleteOnFreeRows(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA)
It builds a rectangular matrix of size n*N where "n" is the number of unrestrained degrees of freedom...
Definition: builder_and_solver.h:312
virtual void FinalizeSolutionStep(ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
It applies certain operations at the system of equations at the end of the solution step.
Definition: builder_and_solver.h:569
TSparseSpace::VectorType TSystemVectorType
Definition of the vector size.
Definition: builder_and_solver.h:85
virtual void InitializeSolutionStep(ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
It applies certain operations at the system of equations at the beginning of the solution step.
Definition: builder_and_solver.h:553
bool mReshapeMatrixFlag
The set containing the DoF of the system.
Definition: builder_and_solver.h:741
ModelPart::NodesContainerType NodesArrayType
The containers of the entities.
Definition: builder_and_solver.h:109
virtual void ResizeAndInitializeVectors(typename TSchemeType::Pointer pScheme, TSystemMatrixPointerType &pA, TSystemVectorPointerType &pDx, TSystemVectorPointerType &pb, ModelPart &rModelPart)
This method initializes and resizes the system of equations.
Definition: builder_and_solver.h:536
void SetLinearSystemSolver(typename TLinearSolver::Pointer pLinearSystemSolver)
This method sets the linear solver to be used.
Definition: builder_and_solver.h:256
bool GetCalculateReactionsFlag() const
This method returns the flag mCalculateReactionsFlag.
Definition: builder_and_solver.h:184
TSparseSpace::MatrixType TSystemMatrixType
Definition of the sparse matrix.
Definition: builder_and_solver.h:82
virtual void ApplyDirichletConditions_LHS(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx)
The same of the precedent but affecting only the LHS.
Definition: builder_and_solver.h:436
virtual void ApplyRHSConstraints(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemVectorType &rb)
Applies the constraints with master-slave relation matrix (RHS only)
Definition: builder_and_solver.h:467
virtual int Check(ModelPart &rModelPart)
This function is designed to be called once to perform all the checks needed on the input provided....
Definition: builder_and_solver.h:616
virtual void BuildRHS(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemVectorType &rb)
Function to perform the build of the RHS. The vector could be sized as the total number of dofs or as...
Definition: builder_and_solver.h:281
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: builder_and_solver.h:718
ModelPart::ConditionsContainerType ConditionsArrayType
Definition: builder_and_solver.h:111
virtual TSparseSpace::MatrixType & GetConstraintRelationMatrix()
This method returns constraint relation (T) matrix.
Definition: builder_and_solver.h:683
static std::string Name()
Returns the name of the class as used in the settings (snake_case format)
Definition: builder_and_solver.h:642
KRATOS_CLASS_POINTER_DEFINITION(BuilderAndSolver)
Pointer definition of BuilderAndSolver.
virtual void BuildAndSolve(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
Function to perform the building and solving phase at the same time.
Definition: builder_and_solver.h:358
virtual void AssignSettings(const Parameters ThisParameters)
This method assigns settings to member variables.
Definition: builder_and_solver.h:780
TSystemVectorPointerType mpReactionsVector
Definition: builder_and_solver.h:751
virtual void BuildRHSAndSolve(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
Corresponds to the previews, but the System's matrix is considered already built and only the RHS is ...
Definition: builder_and_solver.h:400
Scheme< TSparseSpace, TDenseSpace > TSchemeType
Definition of the scheme type.
Definition: builder_and_solver.h:100
BuilderAndSolver(typename TLinearSolver::Pointer pNewLinearSystemSolver, Parameters ThisParameters)
Default constructor with Parameters.
Definition: builder_and_solver.h:135
virtual const DofsArrayType & GetDofSet() const
It allows to get the list of Dofs from the element.
Definition: builder_and_solver.h:515
virtual void BuildAndSolveLinearizedOnPreviousIteration(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb, const bool MoveMesh)
Function to perform the building and solving phase at the same time Linearizing with the database at ...
Definition: builder_and_solver.h:377
virtual Parameters ValidateAndAssignParameters(Parameters ThisParameters, const Parameters DefaultParameters) const
This method validate and assign default parameters.
Definition: builder_and_solver.h:767
virtual TSparseSpace::VectorType & GetConstraintConstantVector()
This method returns constraint constant vector.
Definition: builder_and_solver.h:692
virtual void ApplyDirichletConditions(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
It applies the dirichlet conditions. This operation may be very heavy or completely unexpensive depen...
Definition: builder_and_solver.h:419
virtual ClassType::Pointer Create(typename TLinearSolver::Pointer pNewLinearSystemSolver, Parameters ThisParameters) const
Create method.
Definition: builder_and_solver.h:168
ModelPart::DofsArrayType DofsArrayType
Definition of the DoF array type.
Definition: builder_and_solver.h:106
virtual void Clear()
This function is intended to be called at the end of the solution step to clean up memory storage not...
Definition: builder_and_solver.h:600
virtual void SetUpSystem(ModelPart &rModelPart)
It organises the dofset in order to speed up the building phase.
Definition: builder_and_solver.h:524
bool GetDofSetIsInitializedFlag() const
This method returns the flag mDofSetIsInitialized.
Definition: builder_and_solver.h:202
bool GetReshapeMatrixFlag() const
This method returns the flag mReshapeMatrixFlag.
Definition: builder_and_solver.h:220
void SetCalculateReactionsFlag(bool flag)
This method sets the flag mCalculateReactionsFlag.
Definition: builder_and_solver.h:193
virtual void BuildLHS(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA)
Function to perform the building of the LHS, depending on the implementation chosen the size of the m...
Definition: builder_and_solver.h:267
BuilderAndSolver< TSparseSpace, TDenseSpace, TLinearSolver > ClassType
The definition of the current class.
Definition: builder_and_solver.h:70
TDenseSpace::MatrixType LocalSystemMatrixType
The local matrix definition.
Definition: builder_and_solver.h:94
TSparseSpace::VectorPointerType TSystemVectorPointerType
Definition of the pointer to the vector.
Definition: builder_and_solver.h:91
TSparseSpace::DataType TDataType
Definition of the data type.
Definition: builder_and_solver.h:79
TLinearSolver::Pointer mpLinearSystemSolver
Definition: builder_and_solver.h:737
void SetEchoLevel(int Level)
It sets the level of echo for the solving strategy.
Definition: builder_and_solver.h:665
virtual void ApplyDirichletConditions_RHS(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemVectorType &rDx, TSystemVectorType &rb)
The same of the precedent but affecting only the RHS.
Definition: builder_and_solver.h:452
int mEchoLevel
Number of degrees of freedom of the problem to be solve.
Definition: builder_and_solver.h:749
virtual void Build(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rb)
equivalent (but generally faster) then performing BuildLHS and BuildRHS
Definition: builder_and_solver.h:296
BuilderAndSolver(typename TLinearSolver::Pointer pNewLinearSystemSolver)
Default constructor.
Definition: builder_and_solver.h:152
void SetDofSetIsInitializedFlag(bool DofSetIsInitialized)
This method sets the flag mDofSetIsInitialized.
Definition: builder_and_solver.h:211
TLinearSolver::Pointer GetLinearSystemSolver() const
This method return the linear solver used.
Definition: builder_and_solver.h:247
DofsArrayType mDofSet
Pointer to the linear solver.
Definition: builder_and_solver.h:739
virtual void SystemSolve(TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
This is a call to the linear system solver.
Definition: builder_and_solver.h:341
TDenseSpace::VectorType LocalSystemVectorType
The local vector definition.
Definition: builder_and_solver.h:97
unsigned int GetEquationSystemSize() const
This method returns the value mEquationSystemSize.
Definition: builder_and_solver.h:238
virtual void ApplyConstraints(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rb)
Applies the constraints with master-slave relation matrix.
Definition: builder_and_solver.h:482
virtual ~BuilderAndSolver()
Definition: builder_and_solver.h:159
virtual void SetUpDofSet(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart)
Builds the list of the DofSets involved in the problem by "asking" to each element and condition its ...
Definition: builder_and_solver.h:497
TSparseSpace::MatrixPointerType TSystemMatrixPointerType
Definition of the pointer to the sparse matrix.
Definition: builder_and_solver.h:88
std::size_t SizeType
Definition of the size type.
Definition: builder_and_solver.h:73
bool mCalculateReactionsFlag
Flag taking care if the dof set was initialized ot not.
Definition: builder_and_solver.h:745
int GetEchoLevel() const
It returns the echo level.
Definition: builder_and_solver.h:674
BuilderAndSolver()
Default constructor.
Definition: builder_and_solver.h:126
virtual DofsArrayType & GetDofSet()
It allows to get the list of Dofs from the element.
Definition: builder_and_solver.h:507
virtual Parameters GetDefaultParameters() const
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: builder_and_solver.h:628
unsigned int mEquationSystemSize
Flag taking in account if it is needed or not to calculate the reactions.
Definition: builder_and_solver.h:747
ModelPart::ElementsContainerType ElementsArrayType
Definition: builder_and_solver.h:110
virtual void BuildLHS_Complete(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA)
It builds a matrix of size N*N where "N" is the total number of degrees of freedom involved.
Definition: builder_and_solver.h:327
virtual void CalculateReactions(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
It computes the reactions of the system.
Definition: builder_and_solver.h:586
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MeshType::ConditionsContainerType ConditionsContainerType
Condintions container. A vector set of Conditions with their Id's as key.
Definition: model_part.h:183
MeshType::ElementsContainerType ElementsContainerType
Element container. A vector set of Elements with their Id's as key.
Definition: model_part.h:168
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
int GetInt() const
This method returns the integer contained in the current Parameter.
Definition: kratos_parameters.cpp:666
void ValidateAndAssignDefaults(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing match the form prescribed by th...
Definition: kratos_parameters.cpp:1306
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
This class provides the implementation of the basic tasks that are needed by the solution strategy.
Definition: scheme.h:56
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
void MoveMesh(Scheme< SparseSpaceType, LocalSpaceType > &dummy, ModelPart::NodesContainerType &rNodes)
Definition: add_strategies_to_python.cpp:175
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21