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.
linear_strategy.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_LINEAR_STRATEGY_H_INCLUDED)
11 #define KRATOS_LINEAR_STRATEGY_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 
17 // Project includes
19 
20 //default builder and solver
22 
23 namespace Kratos
24 {
25 
28 
32 
36 
40 
44 
50 template <class TSparseSpace,
51  class TDenseSpace, // = DenseSpace<double>,
52  class TLinearSolver // = LinearSolver<TSparseSpace,TDenseSpace>
53  >
54 class LinearStrategy : public SolutionStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
55 {
56  public:
59 
60  // Counted pointer of ClassName
62 
64 
66 
68 
69  typedef typename BaseType::SchemeType SchemeType;
70 
71  typedef TLinearSolver LinearSolverType;
72 
73  typedef TSparseSpace SparseSpaceType;
74 
76 
78 
80 
82 
84 
87 
89 
98  typename SchemeType::Pointer pScheme,
99  typename BuilderAndSolverType::Pointer pBuilderAndSolver,
100  Flags& rOptions)
101  : SolutionStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(rModelPart, rOptions)
102  {
103  KRATOS_TRY
104 
105  // Saving the scheme
106  mpScheme = pScheme;
107 
108  // Setting up the default builder and solver
109  mpBuilderAndSolver = pBuilderAndSolver;
110 
111  // Set Options
112  this->mOptions = rOptions;
113 
114  // Tells to the builder and solver if the reactions have to be Calculated or not
115  mpBuilderAndSolver->GetOptions().Set(LocalFlagType::COMPUTE_REACTIONS, this->mOptions.Is(LocalFlagType::COMPUTE_REACTIONS));
116 
117  // Tells to the Builder And Solver if the system matrix and vectors need to be reshaped at each step or not
118  mpBuilderAndSolver->GetOptions().Set(LocalFlagType::REFORM_DOFS, this->mOptions.Is(LocalFlagType::REFORM_DOFS));
119 
120  mpBuilderAndSolver->SetEchoLevel(this->mEchoLevel);
121 
125 
126  KRATOS_CATCH("")
127  }
128 
129 
138  typename SchemeType::Pointer pScheme,
139  typename LinearSolverType::Pointer pLinearSolver,
140  Flags& rOptions)
141  : LinearStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(rModelPart, pScheme, Kratos::make_shared<BlockBuilderAndSolver<TSparseSpace, TDenseSpace, TLinearSolver> >(pLinearSolver), rOptions)
142  {}
143 
144 
149  ~LinearStrategy() override
150  {
151  Clear();
152  }
153 
156 
158 
162 
166  void InitializeSolutionStep() override
167  {
168  KRATOS_TRY
169 
170  //initialize system elements, conditions..
171  if(this->IsNot(LocalFlagType::INITIALIZED))
172  this->Initialize();
173 
174  //prints informations about the current time
175  //KRATOS_INFO("") << " [STEP:" << this->GetModelPart().GetProcessInfo()[STEP] << " TIME: "<< this->GetModelPart().GetProcessInfo()[TIME]<< "]\n" << LoggerMessage::Category::STATUS;
176 
177  //set up the system
178  if(this->mOptions.Is(LocalFlagType::REFORM_DOFS)){
179  this->Clear();
180  this->SetSystemDofs();
181  }
182 
183  //setting up the vectors involved to the correct size
184  // // double begin_time = OpenMPUtils::GetCurrentTime();
185  // mpBuilderAndSolver->SetUpSystemMatrices(mpScheme, this->GetModelPart(), mpA, mpDx, mpb);
186  // double end_time = OpenMPUtils::GetCurrentTime();
187 
188  // if (this->mEchoLevel >= 2)
189  // KRATOS_INFO("system_resize_time") << ": system_resize_time : " << end_time - begin_time << "\n" << LoggerMessage::Category::STATISTICS;
190 
191  //initial operations ... things that are constant over the Solution Step
192  mpBuilderAndSolver->InitializeSolutionStep(mpScheme, this->GetModelPart(), mpA, mpDx, mpb);
193 
194  //initial operations ... things that are constant over the Solution Step
195  mpScheme->InitializeSolutionStep(this->GetModelPart());
196 
197  this->Predict();
198 
199  KRATOS_CATCH("")
200  }
201 
205  void FinalizeSolutionStep() override
206  {
207  KRATOS_TRY
208 
209  //Finalization of the solution step, operations to be done after achieving convergence
210 
211  //calculate reactions if required
212  if(this->mOptions.Is(LocalFlagType::COMPUTE_REACTIONS))
213  mpBuilderAndSolver->CalculateReactions(mpScheme, this->GetModelPart(), (*mpA), (*mpDx), (*mpb));
214 
215  //finalize scheme anb builder and solver
216  mpScheme->FinalizeSolutionStep(this->GetModelPart());
217  mpBuilderAndSolver->FinalizeSolutionStep(mpScheme, this->GetModelPart(), mpA, mpDx, mpb);
218 
219  KRATOS_CATCH("")
220  }
221 
222 
226  bool SolveSolutionStep() override
227  {
228  KRATOS_TRY
229 
230  return this->SolveIteration();
231 
232  KRATOS_CATCH("")
233  }
234 
235 
239  bool SolveIteration() override
240  {
241  KRATOS_TRY
242 
243  // Warning info
244  if(!SparseSpaceType::Size((*mpDx)))
245  KRATOS_WARNING("DOFS") << "solution has zero size, no free DOFs" << std::endl;
246 
247  // Initialize Iteration
248  mpScheme->InitializeNonLinearIteration(this->GetModelPart());
249 
250  //function to perform the building and the solving phase.
251  if(this->mOptions.IsNot(LocalFlagType::CONSTANT_SYSTEM_MATRIX)){
252 
253  TSparseSpace::SetToZero((*mpA));
254  TSparseSpace::SetToZero((*mpDx));
255  TSparseSpace::SetToZero((*mpb));
256 
257  mpBuilderAndSolver->BuildAndSolve(mpScheme, this->GetModelPart(), (*mpA), (*mpDx), (*mpb));
258  }
259  else{
260 
261  TSparseSpace::SetToZero((*mpDx));
262  TSparseSpace::SetToZero((*mpb));
263 
264  mpBuilderAndSolver->BuildRHSAndSolve(mpScheme, this->GetModelPart(), (*mpA), (*mpDx), (*mpb));
265  }
266 
267  // EchoInfo
268  mpBuilderAndSolver->EchoInfo(this->GetModelPart(), (*mpA), (*mpDx), (*mpb));
269 
270  // Updating the results
271  this->Update();
272 
273  // Finalize Iteration
274  mpScheme->FinalizeNonLinearIteration(this->GetModelPart());
275 
276  return true;
277 
278  KRATOS_CATCH("")
279  }
280 
281 
285  void Clear() override
286  {
287  KRATOS_TRY
288 
289  // if the preconditioner is saved between solves, it should be cleared here.
290  mpBuilderAndSolver->GetLinearSystemSolver()->Clear();
291 
292  //deallocate the systemvectors
293  if(mpA != nullptr)
295  if(mpDx != nullptr)
297  if(mpb != nullptr)
299 
300  mpBuilderAndSolver->Clear();
301  mpScheme->Clear();
302 
303  KRATOS_CATCH("")
304  }
305 
310  int Check() override
311  {
312  KRATOS_TRY
313 
314  //check the model part
315  BaseType::Check();
316 
317  //check the scheme
318  mpScheme->Check(this->GetModelPart());
319 
320  //check the builder and solver
321  mpBuilderAndSolver->Check(this->GetModelPart());
322 
323  return 0;
324 
325  KRATOS_CATCH("")
326  }
327 
328 
332 
344  void SetEchoLevel(const int Level) override
345  {
346  BaseType::SetEchoLevel(Level);
347  mpBuilderAndSolver->SetEchoLevel(Level);
348  }
349 
350 
355  void SetScheme(typename SchemeType::Pointer pScheme)
356  {
357  mpScheme = pScheme;
358  };
359 
364  typename SchemeType::Pointer GetScheme()
365  {
366  return mpScheme;
367  };
368 
373  void SetBuilderAndSolver(typename BuilderAndSolverType::Pointer pBuilderAndSolver)
374  {
375  mpBuilderAndSolver = pBuilderAndSolver;
376  };
377 
382  typename BuilderAndSolverType::Pointer GetBuilderAndSolver()
383  {
384  return mpBuilderAndSolver;
385  };
386 
387 
393  {
394  SystemMatrixType &mA = *mpA;
395 
396  return mA;
397  }
398 
404  {
405  A = *mpA;
406  }
407 
408 
412 
416 
418 
419  protected:
422 
426 
427  typename SchemeType::Pointer mpScheme;
428  typename BuilderAndSolverType::Pointer mpBuilderAndSolver;
429 
433 
437 
441 
445  void Initialize() override
446  {
447  KRATOS_TRY
448 
449  //Initialize The Scheme - OPERATIONS TO BE DONE ONCE
450  if( mpScheme->IsNot(LocalFlagType::INITIALIZED) )
451  mpScheme->Initialize(this->GetModelPart());
452 
453  //set up the system
454  this->SetSystemDofs();
455 
456  this->Set(LocalFlagType::INITIALIZED,true);
457 
458  KRATOS_CATCH("")
459  }
460 
461 
466  void Predict() override
467  {
468  KRATOS_TRY
469 
470  mpScheme->Predict(this->GetModelPart(), mpBuilderAndSolver->GetDofSet(), (*mpDx));
471 
472  KRATOS_CATCH("")
473  }
474 
475 
483  void Update() override
484  {
485  KRATOS_TRY
486 
487  mpScheme->Update(this->GetModelPart(), mpBuilderAndSolver->GetDofSet(), (*mpDx));
488 
489  KRATOS_CATCH("")
490  }
491 
492 
497  {
498  KRATOS_TRY
499 
500  if (this->mEchoLevel >= 2)
501  KRATOS_INFO(" Reform Dofs ") << " Flag = " <<this->mOptions.Is(LocalFlagType::REFORM_DOFS) << std::endl;
502 
503  //set up the system, operation performed just once unless it is required to reform the dof set at each iteration
504 
505  //setting up the list of the DOFs to be solved
506  // double begin_time = OpenMPUtils::GetCurrentTime();
507  mpBuilderAndSolver->SetUpDofSet(mpScheme, this->GetModelPart());
508  // double end_time = OpenMPUtils::GetCurrentTime();
509  // if (this->mEchoLevel >= 2)
510  // KRATOS_INFO("setup_dofs_time") << "setup_dofs_time : " << end_time - begin_time << "\n" << LoggerMessage::Category::STATISTICS;
511 
512  //shaping correctly the system
513  // begin_time = OpenMPUtils::GetCurrentTime();
514  mpBuilderAndSolver->SetUpSystem();
515  // end_time = OpenMPUtils::GetCurrentTime();
516  // if (this->mEchoLevel >= 2)
517  // KRATOS_INFO("setup_system_time") << ": setup_system_time : " << end_time - begin_time << "\n" << LoggerMessage::Category::STATISTICS; //set up the system, operation performed just once unless it is required to reform the dof set at each iteration
518 
519  KRATOS_CATCH("")
520  }
521 
522 
533 
534  private:
555 
557  LinearStrategy(const LinearStrategy &Other){};
558 
560 
561 };
562 
564 
571 
573 
574 } // namespace Kratos.
575 #endif // KRATOS_LINEAR_STRATEGY_H_INCLUDED defined
Solution Buider and Solver based on block matrix.
Definition: block_builder_and_solver.hpp:58
Definition: flags.h:58
void Set(const Flags ThisFlag)
Definition: flags.cpp:33
bool Is(Flags const &rOther) const
Definition: flags.h:274
bool IsNot(Flags const &rOther) const
Definition: flags.h:291
This is the base linear strategy jacobi / gauss-seidel linear strategies.
Definition: linear_strategy.hpp:55
BaseType::DofsArrayType DofsArrayType
Definition: linear_strategy.hpp:75
bool SolveIteration() override
Solves the current iteration. This function returns true if a solution has been found,...
Definition: linear_strategy.hpp:239
SolutionStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: linear_strategy.hpp:63
SchemeType::Pointer mpScheme
Definition: linear_strategy.hpp:427
void GetDirectSystemMatrix(SystemMatrixType &A)
This method directly sets the input as the LHS.
Definition: linear_strategy.hpp:403
BaseType::SystemVectorPointerType SystemVectorPointerType
Definition: linear_strategy.hpp:83
SystemVectorPointerType mpDx
The pointer to the builder and solver employed.
Definition: linear_strategy.hpp:430
~LinearStrategy() override
Destructor.
Definition: linear_strategy.hpp:149
BaseType::SystemMatrixType SystemMatrixType
Definition: linear_strategy.hpp:77
KRATOS_CLASS_POINTER_DEFINITION(LinearStrategy)
BaseType::SchemeType SchemeType
Definition: linear_strategy.hpp:69
void SetEchoLevel(const int Level) override
This sets the level of echo for the solving strategy.
Definition: linear_strategy.hpp:344
void Update() override
Here the database is updated.
Definition: linear_strategy.hpp:483
void SetSystemDofs()
Performs all the required operations to reform dofs.
Definition: linear_strategy.hpp:496
TLinearSolver LinearSolverType
Definition: linear_strategy.hpp:71
void FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: linear_strategy.hpp:205
LinearStrategy(ModelPart &rModelPart, typename SchemeType::Pointer pScheme, typename BuilderAndSolverType::Pointer pBuilderAndSolver, Flags &rOptions)
Definition: linear_strategy.hpp:97
BaseType::BuilderAndSolverType BuilderAndSolverType
Definition: linear_strategy.hpp:67
SchemeType::Pointer GetScheme()
Get method for the time scheme.
Definition: linear_strategy.hpp:364
LinearStrategy(ModelPart &rModelPart, typename SchemeType::Pointer pScheme, typename LinearSolverType::Pointer pLinearSolver, Flags &rOptions)
Definition: linear_strategy.hpp:137
SystemMatrixType & GetSystemMatrix()
This method returns the LHS matrix.
Definition: linear_strategy.hpp:392
void Predict() override
Operation to predict the solution ... if it is not called a trivial predictor is used in which the va...
Definition: linear_strategy.hpp:466
BaseType::SystemMatrixPointerType SystemMatrixPointerType
Definition: linear_strategy.hpp:81
TSparseSpace SparseSpaceType
Definition: linear_strategy.hpp:73
void Initialize() override
Initialization of member variables and prior operations.
Definition: linear_strategy.hpp:445
void Clear() override
Clears the internal storage.
Definition: linear_strategy.hpp:285
BuilderAndSolverType::Pointer mpBuilderAndSolver
The pointer to the time scheme employed.
Definition: linear_strategy.hpp:428
int Check() override
Function to perform expensive checks.
Definition: linear_strategy.hpp:310
BaseType::SystemVectorType SystemVectorType
Definition: linear_strategy.hpp:79
bool SolveSolutionStep() override
Solves the current step. This function returns true if a solution has been found, false otherwise.
Definition: linear_strategy.hpp:226
void SetBuilderAndSolver(typename BuilderAndSolverType::Pointer pBuilderAndSolver)
Set method for the builder and solver.
Definition: linear_strategy.hpp:373
SystemVectorPointerType mpb
The incremement in the solution.
Definition: linear_strategy.hpp:431
void SetScheme(typename SchemeType::Pointer pScheme)
Set method for the time scheme.
Definition: linear_strategy.hpp:355
SystemMatrixPointerType mpA
The RHS vector of the system of equations.
Definition: linear_strategy.hpp:432
void InitializeSolutionStep() override
Performs all the required operations that should be done (for each step) before solving the solution ...
Definition: linear_strategy.hpp:166
BuilderAndSolverType::Pointer GetBuilderAndSolver()
Get method for the builder and solver.
Definition: linear_strategy.hpp:382
BaseType::LocalFlagType LocalFlagType
Definition: linear_strategy.hpp:65
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
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
Solution Buider and Solver base class.
Definition: solution_builder_and_solver.hpp:63
Solution scheme base class.
Definition: solution_scheme.hpp:54
Solution strategy base class.
Definition: solution_strategy.hpp:52
TSparseSpace::VectorPointerType SystemVectorPointerType
Definition: solution_strategy.hpp:63
ModelPart & GetModelPart()
Operations to get the pointer to the model.
Definition: solution_strategy.hpp:243
Flags mOptions
Definition: solution_strategy.hpp:267
TSparseSpace::MatrixPointerType SystemMatrixPointerType
Definition: solution_strategy.hpp:62
TSparseSpace::MatrixType SystemMatrixType
Definition: solution_strategy.hpp:60
virtual int Check()
Function to perform expensive checks.
Definition: solution_strategy.hpp:154
TSparseSpace::VectorType SystemVectorType
Definition: solution_strategy.hpp:61
virtual void SetEchoLevel(const int Level)
This sets the level of echo for the solution strategy.
Definition: solution_strategy.hpp:190
int mEchoLevel
Definition: solution_strategy.hpp:270
Solver local flags class definition.
Definition: solution_local_flags.hpp:48
static void Clear(MatrixPointerType &pA)
Definition: ublas_space.h:578
static IndexType Size(VectorType const &rV)
return size of vector rV
Definition: ublas_space.h:190
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_INFO(label)
Definition: logger.h:250
#define KRATOS_WARNING(label)
Definition: logger.h:265
TSpaceType::VectorPointerType CreateEmptyVectorPointer(TSpaceType &dummy)
Definition: add_strategies_to_python.cpp:187
TSpaceType::MatrixPointerType CreateEmptyMatrixPointer(TSpaceType &dummy)
Definition: add_strategies_to_python.cpp:181
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
shared_ptr< C > make_shared(Args &&...args)
Definition: smart_pointers.h:40
A
Definition: sensitivityMatrix.py:70