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.
gauss_seidel_linear_strategy.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 #if !defined(KRATOS_GAUSS_SEIDEL_LINEAR_STRATEGY)
15 #define KRATOS_GAUSS_SEIDEL_LINEAR_STRATEGY
16 
17 /* System includes */
18 
19 /* External includes */
20 #include "boost/smart_ptr.hpp"
21 //#include "boost/timer.hpp"
22 
23 /* Project includes */
24 #include "includes/define.h"
26 
27 //default builder and solver
30 
31 namespace Kratos
32 {
33 
55 
77  template <class TSparseSpace,
78  class TDenseSpace, //= DenseSpace<double>,
79  class TLinearSolver //= LinearSolver<TSparseSpace,TDenseSpace>
80  >
82  : public ImplicitSolvingStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
83  {
84  public:
90 
92 
93  typedef typename BaseType::TDataType TDataType;
94 
95  typedef TSparseSpace SparseSpaceType;
96 
99 
101 
103 
105 
107 
109 
112 
121  //constructor specifying the builder and solver
122 
125  typename TSchemeType::Pointer pScheme,
126  typename TLinearSolver::Pointer pNewLinearSolver,
127  typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver,
128  bool ReformDofSetAtEachStep = true,
129  bool CalculateNormDxFlag = false)
130  : ImplicitSolvingStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(model_part)
131  {
132  KRATOS_TRY
133 
134  /* std::cout<<" GaussSeidelLinearStrategy"<<std::endl; */
135 
136  mReformDofSetAtEachStep = ReformDofSetAtEachStep;
137  mCalculateNormDxFlag = CalculateNormDxFlag;
138 
139  //saving the scheme
140  mpScheme = pScheme;
141 
142  //saving the linear solver
143  mpLinearSolver = pNewLinearSolver;
144 
145  //setting up the builder and solver
146  mpBuilderAndSolver = pNewBuilderAndSolver;
147 
148  //set flag to start correcty the calculations
149  mInitializeWasPerformed = false;
150 
151  //tells to the Builder And Solver if the system matrix and vectors need to
152  //be reshaped at each step or not
153  mReformDofSetAtEachStep = true;
154  GetBuilderAndSolver()->SetReshapeMatrixFlag(mReformDofSetAtEachStep);
155 
156  //set EchoLevel to the default value (only time is displayed)
157  this->SetEchoLevel(1);
158 
159  //by default the matrices are rebuilt at each solution step
161 
162  KRATOS_CATCH("")
163  }
164 
168  {
169  // in trilinos third party library, the linear solver's
170  // preconditioner should be freed before the system matrix.
171  // we control the deallocation order with Clear().
172  this->Clear();
173  }
174 
178  //Set and Get Scheme ... containing Builder, Update and other
179 
180  void SetScheme(typename TSchemeType::Pointer pScheme)
181  {
182  mpScheme = pScheme;
183  };
184 
185  typename TSchemeType::Pointer GetScheme()
186  {
187  return mpScheme;
188  };
189 
190  //Set and Get the BuilderAndSolver
191 
192  void SetBuilderAndSolver(typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver)
193  {
194  mpBuilderAndSolver = pNewBuilderAndSolver;
195  };
196 
197  typename TBuilderAndSolverType::Pointer GetBuilderAndSolver()
198  {
199  return mpBuilderAndSolver;
200  };
201 
203  {
204  mReformDofSetAtEachStep = flag;
205  mReformDofSetAtEachStep = true;
206  GetBuilderAndSolver()->SetReshapeMatrixFlag(mReformDofSetAtEachStep);
207  }
208 
210  {
211  return mReformDofSetAtEachStep;
212  }
213 
214  //level of echo for the solving strategy
215  // 0 -> mute... no echo at all
216  // 1 -> printing time and basic informations
217  // 2 -> printing linear solver data
218  // 3 -> Print of debug informations:
219  // Echo of stiffness matrix, Dx, b...
220 
221  void SetEchoLevel(int Level) override
222  {
223  BaseType::SetEchoLevel(Level);
224  GetBuilderAndSolver()->SetEchoLevel(Level);
225  }
226 
227  //*********************************************************************************
234  /* void Predict() */
235  /* { */
236  /* KRATOS_TRY */
237  /* //OPERATIONS THAT SHOULD BE DONE ONCE - internal check to avoid repetitions */
238  /* //if the operations needed were already performed this does nothing */
239  /* //if(mInitializeWasPerformed == false) */
240  /* //{ */
241  /* // Initialize(); */
242  /* // mInitializeWasPerformed = true; */
243  /* //} */
244 
245  /* ////initialize solution step */
246  /* //if (mSolutionStepIsInitialized == false) */
247  /* // InitializeSolutionStep(); */
248 
249  /* TSystemMatrixType& mA = *mpA; */
250  /* TSystemVectorType& mDx = *mpDx; */
251  /* TSystemVectorType& mb = *mpb; */
252 
253  /* DofsArrayType& rDofSet = GetBuilderAndSolver()->GetDofSet(); */
254 
255  /* this->GetScheme()->Predict(BaseType::GetModelPart(), rDofSet, mA, mDx, mb); */
256 
257  /* KRATOS_CATCH("") */
258  /* } */
259 
260  //*********************************************************************************
265  //**********************************************************************
266 
267  double Solve() override
268  {
269  KRATOS_TRY
270 
271  //pointers needed in the solution
272  typename TSchemeType::Pointer pScheme = GetScheme();
273  typename TBuilderAndSolverType::Pointer pBuilderAndSolver = GetBuilderAndSolver();
274 
275  TSystemMatrixType &mA = *mpA;
276  TSystemVectorType &mDx = *mpDx;
277  TSystemVectorType &mb = *mpb;
278 
280  {
281  TSparseSpace::SetToZero(mA);
282  TSparseSpace::SetToZero(mDx);
283  TSparseSpace::SetToZero(mb);
284  // passing smart pointers instead of references here
285  // to prevent dangling pointer to system matrix when
286  // reusing ml preconditioners in the trilinos tpl
287  pBuilderAndSolver->BuildAndSolve(pScheme, BaseType::GetModelPart(), mA, mDx, mb);
289  }
290  else
291  {
292  TSparseSpace::SetToZero(mDx);
293  TSparseSpace::SetToZero(mb);
294  pBuilderAndSolver->BuildRHSAndSolve(pScheme, BaseType::GetModelPart(), mA, mDx, mb);
295  }
296 
297  if (BaseType::GetEchoLevel() == 3) //if it is needed to print the debug info
298  {
299  std::cout << "SystemMatrix = " << mA << std::endl;
300  std::cout << "solution obtained = " << mDx << std::endl;
301  std::cout << "RHS = " << mb << std::endl;
302  }
303 
304  if (this->GetEchoLevel() == 4) //print to matrix market file
305  {
306  std::stringstream matrix_market_name;
307  matrix_market_name << "A_" << BaseType::GetModelPart().GetProcessInfo()[TIME] << ".mm";
308  TSparseSpace::WriteMatrixMarketMatrix((char *)(matrix_market_name.str()).c_str(), mA, false);
309 
310  std::stringstream matrix_market_vectname;
311  matrix_market_vectname << "b_" << BaseType::GetModelPart().GetProcessInfo()[TIME] << ".mm.rhs";
312  TSparseSpace::WriteMatrixMarketVector((char *)(matrix_market_vectname.str()).c_str(), mb);
313  }
314 
315  //update results
316  DofsArrayType &rDofSet = pBuilderAndSolver->GetDofSet();
317  pScheme->Update(BaseType::GetModelPart(), rDofSet, mA, mDx, mb);
318 
319  //move the mesh if needed
320  /* BaseType::MoveMesh(); */
321 
322  //calculate if needed the norm of Dx
323  double normDx = 0.00;
324  if (mCalculateNormDxFlag == true)
325  {
326  normDx = TSparseSpace::TwoNorm(mDx);
327  }
328 
329  //Finalisation of the solution step,
330  //operations to be done after achieving convergence, for example the
331  //Final Residual Vector (mb) has to be saved in there
332  //to avoid error accumulation
333  pScheme->FinalizeSolutionStep(BaseType::GetModelPart(), mA, mDx, mb);
334  pBuilderAndSolver->FinalizeSolutionStep(BaseType::GetModelPart(), mA, mDx, mb);
335 
336  //Cleaning memory after the solution
337  pScheme->Clean();
338 
339  return normDx;
340 
341  KRATOS_CATCH("")
342  }
343 
345  {
346  TSystemMatrixType &mA = *mpA;
347 
348  return mA;
349  }
350  //*********************************************************************************
351 
352  double GetResidualNorm() override
353  {
354  if (TSparseSpace::Size(*mpb) != 0)
355  return TSparseSpace::TwoNorm(*mpb);
356  else
357  return 0.0;
358  }
359 
360  //*********************************************************************************
361 
368  void CalculateOutputData() override
369  {
370  TSystemMatrixType &mA = *mpA;
371  TSystemVectorType &mDx = *mpDx;
372  TSystemVectorType &mb = *mpb;
373 
374  DofsArrayType &rDofSet = GetBuilderAndSolver()->GetDofSet();
375  GetScheme()->CalculateOutputData(BaseType::GetModelPart(), rDofSet, mA, mDx, mb);
376  }
377 
378  //*********************************************************************************
379 
403  protected:
433  private:
441  typename TLinearSolver::Pointer mpLinearSolver;
442 
443  typename TSchemeType::Pointer mpScheme;
444 
445  typename TBuilderAndSolverType::Pointer mpBuilderAndSolver;
446 
450 
459  bool mReformDofSetAtEachStep;
460 
461  //calculates if required the norm of the correction term Dx
462  bool mCalculateNormDxFlag;
463 
469  bool mInitializeWasPerformed;
470 
471  unsigned int mMaxIterationNumber;
472 
476  //**********************************************************************
477  //**********************************************************************
478 
479  void Initialize() override
480  {
481  KRATOS_TRY
482 
483  if (BaseType::GetEchoLevel() > 2)
484  std::cout << "entering in the Initialize of the GaussSeidelLinearStrategy" << std::endl;
485 
486  //pointers needed in the solution
487  typename TSchemeType::Pointer pScheme = GetScheme();
488 
489  //Initialize The Scheme - OPERATIONS TO BE DONE ONCE
490  if (pScheme->SchemeIsInitialized() == false)
491  pScheme->Initialize(BaseType::GetModelPart());
492 
493  //Initialize The Elements - OPERATIONS TO BE DONE ONCE
494  if (pScheme->ElementsAreInitialized() == false)
495  pScheme->InitializeElements(BaseType::GetModelPart());
496 
497  //Initialize The Conditions - OPERATIONS TO BE DONE ONCE
498  if (pScheme->ConditionsAreInitialized() == false)
499  pScheme->InitializeConditions(BaseType::GetModelPart());
500 
501  if (BaseType::GetEchoLevel() > 2)
502  std::cout << "exiting the Initialize of the GaussSeidelLinearStrategy" << std::endl;
503 
504  KRATOS_CATCH("")
505  }
506 
507  //**********************************************************************
508  //**********************************************************************
509 
510  void InitializeSolutionStep() override
511  {
512  KRATOS_TRY
513 
514  typename TBuilderAndSolverType::Pointer pBuilderAndSolver = GetBuilderAndSolver();
515  typename TSchemeType::Pointer pScheme = GetScheme();
516  //int rank = BaseType::GetModelPart().GetCommunicator().MyPID();
517 
518  /* ProcessInfo& pCurrentProcessInfo = BaseType::GetModelPart().GetProcessInfo(); */
519 
520  //OPERATIONS THAT SHOULD BE DONE ONCE - internal check to avoid repetitions
521  //if the operations needed were already performed this does nothing
522  if (mInitializeWasPerformed == false)
523  {
524  Initialize();
525  mInitializeWasPerformed = true;
526  }
527 
528  /* std::cout << "Gauss-Seidel VP Strategy, CurrentTime: " << pCurrentProcessInfo[TIME] << std::endl; */
529 
530  //loop to reform the dofset
531  // boost::timer system_construction_time;
532 
533  //setting up the list of the DOFs to be solved
534  pBuilderAndSolver->SetUpDofSet(pScheme, BaseType::GetModelPart());
535 
536  //shaping correctly the system
537  pBuilderAndSolver->SetUpSystem(BaseType::GetModelPart());
538 
539  //setting up the Vectors involved to the correct size
540  pBuilderAndSolver->ResizeAndInitializeVectors(pScheme, mpA, mpDx, mpb, BaseType::GetModelPart());
541  //if (BaseType::GetEchoLevel() > 1 && rank == 0)
542  // std::cout << "System Construction Time : " << system_construction_time.elapsed() << std::endl;
543 
544  TSystemMatrixType &mA = *mpA;
545  TSystemVectorType &mDx = *mpDx;
546  TSystemVectorType &mb = *mpb;
547 
548  //initial operations ... things that are constant over the Solution Step
549  pBuilderAndSolver->InitializeSolutionStep(BaseType::GetModelPart(), mA, mDx, mb);
550 
551  //initial operations ... things that are constant over the Solution Step
552  /* boost::timer scheme_initialize_solution_step; */
553  /* pScheme->InitializeSolutionStep(BaseType::GetModelPart(), mA, mDx, mb); */
554 
555  KRATOS_CATCH("")
556  }
557 
558  //**********************************************************************
559  //**********************************************************************
560 
561  void MaxIterationsExceeded()
562  {
563  std::cout << "***************************************************" << std::endl;
564  std::cout << "******* ATTENTION: max iterations exceeded ********" << std::endl;
565  std::cout << "***************************************************" << std::endl;
566  }
567 
568  //**********************************************************************
569  //**********************************************************************
570 
571  void Clear() override
572  {
573  KRATOS_TRY;
574  // if the preconditioner is saved between solves, it
575  // should be cleared here.
576  GetBuilderAndSolver()->GetLinearSystemSolver()->Clear();
577 
578  if (mpA != NULL)
580  if (mpDx != NULL)
582  if (mpb != NULL)
584 
585  //setting to zero the internal flag to ensure that the dof sets are recalculated
586  GetBuilderAndSolver()->SetDofSetIsInitializedFlag(false);
587  GetBuilderAndSolver()->Clear();
588  GetScheme()->Clear();
589 
590  KRATOS_CATCH("");
591  }
592 
597  int Check() override
598  {
599  KRATOS_TRY
600 
601  BaseType::Check();
602 
604 
605  GetScheme()->Check(BaseType::GetModelPart());
606 
607  return 0;
608 
609  KRATOS_CATCH("")
610  }
611 
631 
634  }; /* Class GaussSeidelLinearStrategy */
635 
643 } /* namespace Kratos.*/
644 
645 #endif /* KRATOS_GAUSS_SEIDEL_LINEAR_STRATEGY defined */
Short class definition.
Definition: gauss_seidel_linear_strategy.h:83
BaseType::DofsArrayType DofsArrayType
Definition: gauss_seidel_linear_strategy.h:100
void SetScheme(typename TSchemeType::Pointer pScheme)
Definition: gauss_seidel_linear_strategy.h:180
BaseType::TSystemMatrixPointerType TSystemMatrixPointerType
Definition: gauss_seidel_linear_strategy.h:110
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: gauss_seidel_linear_strategy.h:98
TSparseSpace SparseSpaceType
Definition: gauss_seidel_linear_strategy.h:95
TSystemMatrixType & GetSystemMatrix() override
This method returns the LHS matrix.
Definition: gauss_seidel_linear_strategy.h:344
BaseType::TSchemeType TSchemeType
Definition: gauss_seidel_linear_strategy.h:97
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: gauss_seidel_linear_strategy.h:108
BaseType::TSystemMatrixType TSystemMatrixType
Definition: gauss_seidel_linear_strategy.h:102
void CalculateOutputData() override
Definition: gauss_seidel_linear_strategy.h:368
KRATOS_CLASS_POINTER_DEFINITION(GaussSeidelLinearStrategy)
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: gauss_seidel_linear_strategy.h:106
BaseType::TDataType TDataType
Definition: gauss_seidel_linear_strategy.h:93
virtual ~GaussSeidelLinearStrategy()
Definition: gauss_seidel_linear_strategy.h:167
GaussSeidelLinearStrategy(ModelPart &model_part, typename TSchemeType::Pointer pScheme, typename TLinearSolver::Pointer pNewLinearSolver, typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver, bool ReformDofSetAtEachStep=true, bool CalculateNormDxFlag=false)
Definition: gauss_seidel_linear_strategy.h:123
bool GetReformDofSetAtEachStepFlag()
Definition: gauss_seidel_linear_strategy.h:209
BaseType::TSystemVectorType TSystemVectorType
Definition: gauss_seidel_linear_strategy.h:104
ImplicitSolvingStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: gauss_seidel_linear_strategy.h:91
void SetEchoLevel(int Level) override
This sets the level of echo for the solving strategy.
Definition: gauss_seidel_linear_strategy.h:221
void SetReformDofSetAtEachStepFlag(bool flag)
Definition: gauss_seidel_linear_strategy.h:202
void SetBuilderAndSolver(typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver)
Definition: gauss_seidel_linear_strategy.h:192
double GetResidualNorm() override
Operations to get the residual norm.
Definition: gauss_seidel_linear_strategy.h:352
TSchemeType::Pointer GetScheme()
Definition: gauss_seidel_linear_strategy.h:185
double Solve() override
Definition: gauss_seidel_linear_strategy.h:267
BaseType::TSystemVectorPointerType TSystemVectorPointerType
Definition: gauss_seidel_linear_strategy.h:111
TBuilderAndSolverType::Pointer GetBuilderAndSolver()
Definition: gauss_seidel_linear_strategy.h:197
Implicit solving strategy base class This is the base class from which we will derive all the implici...
Definition: implicit_solving_strategy.h:61
BaseType::TSystemVectorType TSystemVectorType
Definition: implicit_solving_strategy.h:72
Scheme< TSparseSpace, TDenseSpace > TSchemeType
Definition: implicit_solving_strategy.h:82
void SetRebuildLevel(int Level) override
This sets the build level.
Definition: implicit_solving_strategy.h:207
int mRebuildLevel
Definition: implicit_solving_strategy.h:263
bool mStiffnessMatrixIsBuilt
The current rebuild level.
Definition: implicit_solving_strategy.h:264
BuilderAndSolver< TSparseSpace, TDenseSpace, TLinearSolver > TBuilderAndSolverType
Definition: implicit_solving_strategy.h:84
BaseType::TSystemMatrixType TSystemMatrixType
Definition: implicit_solving_strategy.h:70
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
TSparseSpace::VectorPointerType TSystemVectorPointerType
Definition: solving_strategy.h:77
TDenseSpace::MatrixType LocalSystemMatrixType
Definition: solving_strategy.h:79
TSparseSpace::DataType TDataType
Definition: solving_strategy.h:69
virtual void SetEchoLevel(const int Level)
This sets the level of echo for the solving strategy.
Definition: solving_strategy.h:255
ModelPart & GetModelPart()
Operations to get the pointer to the model.
Definition: solving_strategy.h:350
int GetEchoLevel()
This returns the level of echo for the solving strategy.
Definition: solving_strategy.h:271
TSparseSpace::MatrixType TSystemMatrixType
Definition: solving_strategy.h:71
virtual int Check()
Function to perform expensive checks.
Definition: solving_strategy.h:377
TSparseSpace::MatrixPointerType TSystemMatrixPointerType
Definition: solving_strategy.h:75
TSparseSpace::VectorType TSystemVectorType
Definition: solving_strategy.h:73
TDenseSpace::VectorType LocalSystemVectorType
Definition: solving_strategy.h:81
static void Clear(MatrixPointerType &pA)
Definition: ublas_space.h:578
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
double TwoNorm(SparseSpaceType &dummy, SparseSpaceType::VectorType &x)
Definition: add_strategies_to_python.cpp:164
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
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
model_part
Definition: face_heat.py:14
double precision function mb(a)
Definition: TensorModule.f:849