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.
solution_builder_and_solver.hpp
Go to the documentation of this file.
1 //
2 // Project Name: KratosSolidMechanicsApplication $
3 // Created by: $Author: JMCarbonell $
4 // Last modified by: $Co-Author: $
5 // Date: $Date: March 2018 $
6 // Revision: $Revision: 0.0 $
7 //
8 //
9 
10 #if !defined(KRATOS_SOLUTION_BUILDER_AND_SOLVER_H_INCLUDED)
11 #define KRATOS_SOLUTION_BUILDER_AND_SOLVER_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 
17 // Project includes
19 
20 namespace Kratos
21 {
22 
25 
29 
33 
37 
41 
42 
58 template<class TSparseSpace,
59  class TDenseSpace, // = DenseSpace<double>,
60  class TLinearSolver //= LinearSolver<TSparseSpace,TDenseSpace>
61  >
63 {
64 public:
65 
68 
71 
73 
77 
78  typedef typename TSparseSpace::MatrixPointerType SystemMatrixPointerType;
79  typedef typename TSparseSpace::VectorPointerType SystemVectorPointerType;
80 
83 
85  typedef typename SchemeType::Pointer SchemePointerType;
86 
87  typedef typename TLinearSolver::Pointer LinearSolverPointerType;
88 
92 
95  {
96  this->Set(LocalFlagType::DOFS_INITIALIZED, false);
97 
98  mpLinearSystemSolver = nullptr;
99  mEchoLevel = 0;
100  }
101 
104  {
105  this->Set(LocalFlagType::DOFS_INITIALIZED, false);
106 
107  mpLinearSystemSolver = pLinearSystemSolver;
108  mEchoLevel = 0;
109  }
110 
113 
117 
121 
122 
128  virtual void BuildLHS(SchemePointerType pScheme,
129  ModelPart& rModelPart,
130  SystemMatrixType& rA)
131  {
132  }
133 
138  virtual void BuildRHS(SchemePointerType pScheme,
139  ModelPart& rModelPart,
140  SystemVectorType& rb)
141  {
142  }
143 
148  virtual void Build(SchemePointerType pScheme,
149  ModelPart& rModelPart,
150  SystemMatrixType& rA,
151  SystemVectorType& rb)
152  {
153  }
154 
158  virtual void SystemSolve(SystemMatrixType& rA,
159  SystemVectorType& rDx,
160  SystemVectorType& rb
161  )
162  {
163  }
164 
169  virtual void BuildAndSolve(SchemePointerType pScheme,
170  ModelPart& rModelPart,
171  SystemMatrixType& rA,
172  SystemVectorType& rDx,
173  SystemVectorType& rb)
174  {
175  }
176 
181  virtual void BuildRHSAndSolve(SchemePointerType pScheme,
182  ModelPart& rModelPart,
183  SystemMatrixType& rA,
184  SystemVectorType& rDx,
185  SystemVectorType& rb)
186  {
187  }
188 
197  ModelPart& rModelPart,
198  SystemMatrixType& rA,
199  SystemVectorType& rDx,
200  SystemVectorType& rb)
201  {
202  }
203 
204 
205  // /**
206  // * @brief Performs all the required operations to reform dofs
207  // */
208  // virtual void SetSystemDofs(SchemePointerType pScheme,
209  // ModelPart& rModelPart)
210  // {
211  // KRATOS_TRY
212 
213  // if (this->mEchoLevel >= 2)
214  // KRATOS_INFO(" Reform Dofs ") << " Flag = " <<this->mOptions.Is(LocalFlagType::REFORM_DOFS) << std::endl;
215 
216  // //set up the system, operation performed just once unless it is required to reform the dof set at each iteration
217 
218  // //setting up the list of the DOFs to be solved
219  // double begin_time = OpenMPUtils::GetCurrentTime();
220  // this->SetUpDofSet(rModelPart);
221  // double end_time = OpenMPUtils::GetCurrentTime();
222  // if (this->mEchoLevel >= 2)
223  // KRATOS_INFO("setup_dofs_time") << "setup_dofs_time : " << end_time - begin_time << "\n" << LoggerMessage::Category::STATISTICS;
224 
225  // //shaping correctly the system
226  // begin_time = OpenMPUtils::GetCurrentTime();
227  // this->SetUpSystem();
228  // end_time = OpenMPUtils::GetCurrentTime();
229  // if (this->mEchoLevel >= 2)
230  // KRATOS_INFO("setup_system_time") << ": setup_system_time : " << end_time - begin_time << "\n" << LoggerMessage::Category::STATISTICS;
231 
232  // KRATOS_CATCH("")
233  // }
234 
235 
240  virtual void SetUpDofSet(SchemePointerType pScheme,
241  ModelPart& rModelPart)
242  {
243  }
244 
248  virtual void SetUpSystem()
249  {
250  }
251 
252 
257  ModelPart& rModelPart,
261  {
262  }
263 
264 
270  ModelPart& rModelPart,
274  {
275  }
276 
282  ModelPart& rModelPart,
286  {
287  }
288 
294  virtual void CalculateReactions(SchemePointerType pScheme,
295  ModelPart& rModelPart,
296  SystemMatrixType& rA,
297  SystemVectorType& rDx,
298  SystemVectorType& rb)
299  {
300  }
301 
305  virtual void Clear()
306  {
307  this->mDofSet.clear();
308 
309  if(this->mpReactionsVector != nullptr)
310  TSparseSpace::Clear(this->mpReactionsVector);
311 
312  if(this->mpLinearSystemSolver != nullptr)
313  this->mpLinearSystemSolver->Clear();
314 
315  if (this->mEchoLevel > 0)
316  {
317  KRATOS_INFO("Builder and Solver Cleared") << "Clear Function called" << std::endl;
318  }
319  }
320 
321 
329  virtual int Check(ModelPart& rModelPart)
330  {
331  KRATOS_TRY
332 
333  return 0;
334 
335  KRATOS_CATCH("")
336  }
337 
338 
342 
346  unsigned int GetEquationSystemSize()
347  {
348  return mEquationSystemSize;
349  }
350 
351 
356  {
357  return mpLinearSystemSolver;
358  }
359 
360 
364  void SetOptions(Flags& rOptions)
365  {
366  mOptions = rOptions;
367  }
368 
374  {
375  return mOptions;
376  }
377 
378 
390  virtual void SetEchoLevel(const int Level)
391  {
392  mEchoLevel = Level;
393  }
394 
395  virtual int GetEchoLevel()
396  {
397  return mEchoLevel;
398  }
399 
400 
404  virtual void EchoInfo(ModelPart& rModelPart,
405  SystemMatrixType& rA,
406  SystemVectorType& rDx,
407  SystemVectorType& rb)
408  {
409  KRATOS_TRY
410 
411  if (this->mEchoLevel == 2) //if it is needed to print the debug info
412  {
413  KRATOS_INFO("Dx") << "Solution = " << rDx << std::endl;
414  KRATOS_INFO("RHS") << "Vector = " << rb << std::endl;
415  }
416  else if (this->mEchoLevel == 3) //if it is needed to print the debug info
417  {
418  KRATOS_INFO("LHS") << "Matrix = " << rA << std::endl;
419  KRATOS_INFO("Dx") << "Solution = " << rDx << std::endl;
420  KRATOS_INFO("RHS") << "Vector = " << rb << std::endl;
421 
422  }
423  else if (this->mEchoLevel == 4) //print to matrix market file
424  {
425  unsigned int iteration_number = 0;
426  if( rModelPart.GetProcessInfo().Has(NL_ITERATION_NUMBER) )
427  iteration_number = rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER];
428 
429  std::stringstream matrix_market_name;
430  matrix_market_name << "A_" << rModelPart.GetProcessInfo()[TIME] << "_" << iteration_number << ".mm";
431  TSparseSpace::WriteMatrixMarketMatrix((char *)(matrix_market_name.str()).c_str(), rA, false);
432 
433  std::stringstream matrix_market_vectname;
434  matrix_market_vectname << "b_" << rModelPart.GetProcessInfo()[TIME] << "_" << iteration_number << ".mm.rhs";
435  TSparseSpace::WriteMatrixMarketVector((char *)(matrix_market_vectname.str()).c_str(), rb);
436  }
437 
438  KRATOS_CATCH("")
439 
440  }
441 
442 
447  {
448  return mDofSet;
449  }
450 
454 
458 
460 
461 protected:
464 
468 
469  // Pointer to the LinearSolver.
471 
472  // Container for de system dofs set
474 
475  // Flags to set options
477 
478  // Number of degrees of freedom of the problem to be solved
479  unsigned int mEquationSystemSize;
480 
481  // Level of echo for the builder and solver
483 
484  // Reactions vector
486 
502 
503 private:
504 
527 
528 };
529 
531 
534 
538 
540 
542 
543 } // namespace Kratos.
544 
545 #endif // KRATOS_SOLUTION_BUILDER_AND_SOLVER_H_INCLUDED defined
546 
bool Has(const Variable< TDataType > &rThisVariable) const
Checks if the data container has a value associated with a given variable.
Definition: data_value_container.h:382
Definition: flags.h:58
void Set(const Flags ThisFlag)
Definition: flags.cpp:33
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
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
void clear()
Clear the set, removing all elements.
Definition: pointer_vector_set.h:663
Solution Buider and Solver base class.
Definition: solution_builder_and_solver.hpp:63
unsigned int mEquationSystemSize
Definition: solution_builder_and_solver.hpp:479
virtual void Clear()
This function is intended to be called at the end of the solution step to clean up memory storage not...
Definition: solution_builder_and_solver.hpp:305
virtual void SetUpDofSet(SchemePointerType pScheme, ModelPart &rModelPart)
Builds the list of the DofSets involved in the problem by "asking" to each element and condition its ...
Definition: solution_builder_and_solver.hpp:240
TSparseSpace::MatrixPointerType SystemMatrixPointerType
Definition: solution_builder_and_solver.hpp:78
virtual void BuildRHS(SchemePointerType pScheme, ModelPart &rModelPart, SystemVectorType &rb)
Function to perform the build of the RHS.
Definition: solution_builder_and_solver.hpp:138
SolutionScheme< TSparseSpace, TDenseSpace > SchemeType
Definition: solution_builder_and_solver.hpp:84
SystemVectorPointerType mpReactionsVector
Definition: solution_builder_and_solver.hpp:485
Flags & GetOptions()
Get strategy options.
Definition: solution_builder_and_solver.hpp:373
SolverLocalFlags LocalFlagType
Definition: solution_builder_and_solver.hpp:72
void SetOptions(Flags &rOptions)
Sets strategy options.
Definition: solution_builder_and_solver.hpp:364
SolutionBuilderAndSolver()
Default Constructor.
Definition: solution_builder_and_solver.hpp:94
virtual void CalculateReactions(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb)
Calculates system reactions.
Definition: solution_builder_and_solver.hpp:294
unsigned int GetEquationSystemSize()
Get size of the system.
Definition: solution_builder_and_solver.hpp:346
LinearSolverPointerType GetLinearSystemSolver()
Get linear solver.
Definition: solution_builder_and_solver.hpp:355
virtual DofsArrayType & GetDofSet()
Allows to get the list of system Dofs.
Definition: solution_builder_and_solver.hpp:446
TSparseSpace::VectorType SystemVectorType
Definition: solution_builder_and_solver.hpp:76
TDenseSpace::VectorType LocalSystemVectorType
Definition: solution_builder_and_solver.hpp:82
TSparseSpace::MatrixType SystemMatrixType
Definition: solution_builder_and_solver.hpp:75
ModelPart::DofsArrayType DofsArrayType
Definition: solution_builder_and_solver.hpp:74
Flags mOptions
Definition: solution_builder_and_solver.hpp:476
virtual void FinalizeSolutionStep(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixPointerType &pA, SystemVectorPointerType &pDx, SystemVectorPointerType &pb)
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: solution_builder_and_solver.hpp:281
virtual void InitializeSolutionStep(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixPointerType &pA, SystemVectorPointerType &pDx, SystemVectorPointerType &pb)
Performs all the required operations that should be done (for each step) before solving the solution ...
Definition: solution_builder_and_solver.hpp:269
SolutionBuilderAndSolver(LinearSolverPointerType pLinearSystemSolver)
Constructor.
Definition: solution_builder_and_solver.hpp:103
virtual void SetEchoLevel(const int Level)
This sets the level of echo for the builder and solver.
Definition: solution_builder_and_solver.hpp:390
virtual void SystemSolve(SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb)
This is a call to the linear system solver.
Definition: solution_builder_and_solver.hpp:158
virtual void Build(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixType &rA, SystemVectorType &rb)
Function to perform the building of the LHS and RHS.
Definition: solution_builder_and_solver.hpp:148
virtual void SetUpSystemMatrices(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixPointerType &pA, SystemVectorPointerType &pDx, SystemVectorPointerType &pb)
Resizes and Initializes the system vectors and matrices after SetUpDofSet and SetUpSytem has been cal...
Definition: solution_builder_and_solver.hpp:256
DofsArrayType mDofSet
Definition: solution_builder_and_solver.hpp:473
TDenseSpace::MatrixType LocalSystemMatrixType
Definition: solution_builder_and_solver.hpp:81
TLinearSolver::Pointer LinearSolverPointerType
Definition: solution_builder_and_solver.hpp:87
virtual void ApplyDirichletConditions(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb)
applies the dirichlet conditions.
Definition: solution_builder_and_solver.hpp:196
virtual int GetEchoLevel()
Definition: solution_builder_and_solver.hpp:395
virtual void EchoInfo(ModelPart &rModelPart, SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb)
This method returns the components of the system of equations depending of the echo level.
Definition: solution_builder_and_solver.hpp:404
~SolutionBuilderAndSolver() override
Destructor.
Definition: solution_builder_and_solver.hpp:112
TSparseSpace::VectorPointerType SystemVectorPointerType
Definition: solution_builder_and_solver.hpp:79
LinearSolverPointerType mpLinearSystemSolver
Definition: solution_builder_and_solver.hpp:470
virtual void BuildLHS(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixType &rA)
Function to perform the building of the LHS,.
Definition: solution_builder_and_solver.hpp:128
int mEchoLevel
Definition: solution_builder_and_solver.hpp:482
virtual void BuildRHSAndSolve(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb)
Function to perform the building of the RHS and solving phase at the same time.
Definition: solution_builder_and_solver.hpp:181
virtual void BuildAndSolve(SchemePointerType pScheme, ModelPart &rModelPart, SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb)
Function to perform the building and solving phase at the same time.
Definition: solution_builder_and_solver.hpp:169
virtual int Check(ModelPart &rModelPart)
Definition: solution_builder_and_solver.hpp:329
KRATOS_CLASS_POINTER_DEFINITION(SolutionBuilderAndSolver)
Pointer definition of SolutionBuilderAndSolver.
SchemeType::Pointer SchemePointerType
Definition: solution_builder_and_solver.hpp:85
virtual void SetUpSystem()
organises the dofset in order to speed up the building phase
Definition: solution_builder_and_solver.hpp:248
Solution scheme base class.
Definition: solution_scheme.hpp:54
Solver local flags class definition.
Definition: solution_local_flags.hpp:48
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_INFO(label)
Definition: logger.h:250
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
bool WriteMatrixMarketVector(const char *FileName, VectorType &V)
Definition: matrix_market_interface.h:539
bool WriteMatrixMarketMatrix(const char *FileName, CompressedMatrixType &M, bool Symmetric)
Definition: matrix_market_interface.h:308