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.
mpm_residual_based_newton_raphson_strategy.hpp
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: Ilaria Iaconeta, Bodhinanda Chandra
11 //
12 //
13 
14 
15 #if !defined(KRATOS_MPM_RESIDUAL_BASED_NEWTON_RAPHSON_STRATEGY )
16 #define KRATOS_MPM_RESIDUAL_BASED_NEWTON_RAPHSON_STRATEGY
17 
18 /* System includes */
19 
20 /* External includes */
22 
23 // Application includes
25 
26 namespace Kratos
27 {
28 
55 
62 template<class TSparseSpace,
63  class TDenseSpace,
64  class TLinearSolver
65  >
67  : public ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
68 {
69 public:
73 
76 
78 
80 
81  typedef typename BaseType::TDataType TDataType;
82 
83  typedef TSparseSpace SparseSpaceType;
84 
86 
88 
90 
92 
94 
96 
98 
100 
101 
111  ModelPart& rModelPart,
112  bool MoveMeshFlag = false
113  ) : ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(
114  rModelPart, MoveMeshFlag)
115  {
116  }
117 
119  ModelPart& rModelPart,
120  typename TSchemeType::Pointer pScheme,
121  typename TLinearSolver::Pointer pNewLinearSolver,
122  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
123  int MaxIterations = 30,
124  bool CalculateReactions = false,
125  bool ReformDofSetAtEachStep = false,
126  bool MoveMeshFlag = false
127  ) : ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(
128  rModelPart, pScheme, pNewLinearSolver, pNewConvergenceCriteria,
129  MaxIterations, CalculateReactions, ReformDofSetAtEachStep, MoveMeshFlag)
130  {
131  }
132 
134  ModelPart& rModelPart,
135  typename TSchemeType::Pointer pScheme,
136  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
137  typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver,
138  int MaxIterations = 30,
139  bool CalculateReactions = false,
140  bool ReformDofSetAtEachStep = false,
141  bool MoveMeshFlag = false
142  ) : ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(
143  rModelPart, pScheme, pNewConvergenceCriteria, pNewBuilderAndSolver,
144  MaxIterations, CalculateReactions, ReformDofSetAtEachStep, MoveMeshFlag)
145  {
146  }
147 
151  {
152  }
153 
157  bool SolveSolutionStep() override
158  {
159  typename TSchemeType::Pointer p_scheme = this->GetScheme();
160  typename TBuilderAndSolverType::Pointer p_builder_and_solver = this->GetBuilderAndSolver();
161 
162  TSystemMatrixType& rA = *(this->mpA);
163  TSystemVectorType& rDx = *(this->mpDx);
164  TSystemVectorType& rb = *(this->mpb);
165  DofsArrayType& r_dof_set = p_builder_and_solver->GetDofSet();
166 
167  // Initializing the parameters of the Newton-Raphson cycle
168  unsigned int iteration_number = 1;
169  BaseType::GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
170  bool is_converged = false;
171 
172  p_scheme->InitializeNonLinIteration(BaseType::GetModelPart(), rA, rDx, rb);
173  is_converged = this->mpConvergenceCriteria->PreCriteria(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
174 
175  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3) << "PreCriteria:"
176  << "\tIs_converged: " << is_converged << "\tmRebuildLevel: " << BaseType::mRebuildLevel
177  << "\tmStiffnessMatrixIsBuilt: " << BaseType::mStiffnessMatrixIsBuilt << std::endl;
178 
180  {
181  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3) << "SetToZero the matrix and vectors of the system" << std::endl;
182 
183  TSparseSpace::SetToZero(rA);
184  TSparseSpace::SetToZero(rDx);
185  TSparseSpace::SetToZero(rb);
186 
187  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3) << "Build and Solve" << std::endl;
188 
189  p_builder_and_solver->BuildAndSolve(p_scheme, BaseType::GetModelPart(), rA, rDx, rb);
190  }
191  else
192  {
193  TSparseSpace::SetToZero(rDx); // rDx=0.00;
194  TSparseSpace::SetToZero(rb);
195 
196  p_builder_and_solver->BuildRHSAndSolve(p_scheme, BaseType::GetModelPart(), rA, rDx, rb);
197  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3) << "BuildRHSAndSolve" << std::endl;
198  }
199 
200 
201  if (this->GetEchoLevel() == 3) // If it is needed to print the debug info
202  {
203  KRATOS_INFO("MPMNewtonRaphsonStrategy") << "SystemMatrix = " << rA << std::endl;
204  KRATOS_INFO("MPMNewtonRaphsonStrategy") << "solution obtained = " << rDx << std::endl;
205  KRATOS_INFO("MPMNewtonRaphsonStrategy") << "RHS = " << rb << std::endl;
206  }
207  else if (this->GetEchoLevel() == 4) // Print to matrix market file
208  {
209  std::stringstream matrix_market_name;
210  matrix_market_name << "A_" << BaseType::GetModelPart().GetProcessInfo()[TIME] << "_" << iteration_number << ".mm";
211  TSparseSpace::WriteMatrixMarketMatrix((char*)(matrix_market_name.str()).c_str(), rA, false);
212 
213  std::stringstream matrix_market_vectname;
214  matrix_market_vectname << "b_" << BaseType::GetModelPart().GetProcessInfo()[TIME] << "_" << iteration_number << ".mm.rhs";
215  TSparseSpace::WriteMatrixMarketVector((char*)(matrix_market_vectname.str()).c_str(), rb);
216  }
217 
218  // Update results
219  r_dof_set = p_builder_and_solver->GetDofSet();
220  p_scheme->Update(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
221  p_scheme->FinalizeNonLinIteration(BaseType::GetModelPart(), rA, rDx, rb);
222 
223  // Move the mesh if needed
225 
226  if (is_converged == true)
227  {
228  // Initialisation of the convergence criteria
229  this->mpConvergenceCriteria->InitializeSolutionStep(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
230 
231  if (this->mpConvergenceCriteria->GetActualizeRHSflag() == true)
232  {
233  TSparseSpace::SetToZero(rb);
234  p_builder_and_solver->BuildRHS(p_scheme, BaseType::GetModelPart(), rb);
235  }
236 
237  is_converged = this->mpConvergenceCriteria->PostCriteria(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
238  }
239  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3 && !is_converged) << "Starting Nonlinear iteration" << std::endl;
240 
241  // Iteration Loop
242  while (is_converged == false &&
243  iteration_number++ < this->mMaxIterationNumber)
244  {
245  // Setting the number of iteration
246  BaseType::GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
247  p_scheme->InitializeNonLinIteration(BaseType::GetModelPart(), rA, rDx, rb);
248  is_converged = this->mpConvergenceCriteria->PreCriteria(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
249 
250  // Call the linear system solver to find the correction rDx. It is not called if there is no system to solve
251  if (SparseSpaceType::Size(rDx) != 0)
252  {
254  {
255  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3) << "Iteration Number: " << iteration_number << std::endl;
256 
257  if (this->GetKeepSystemConstantDuringIterations() == false)
258  {
259  TSparseSpace::SetToZero(rA);
260  TSparseSpace::SetToZero(rDx);
261  TSparseSpace::SetToZero(rb);
262 
263  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3) << "Build and Solve" << std::endl;
264  p_builder_and_solver->BuildAndSolve(p_scheme, BaseType::GetModelPart(), rA, rDx, rb);
265  }
266  else
267  {
268  TSparseSpace::SetToZero(rDx);
269  TSparseSpace::SetToZero(rb);
270 
271  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3) << "Build RHS and Solve" << std::endl;
272  p_builder_and_solver->BuildRHSAndSolve(p_scheme, BaseType::GetModelPart(), rA, rDx, rb);
273  }
274  }
275  else
276  {
277  TSparseSpace::SetToZero(rDx);
278  TSparseSpace::SetToZero(rb);
279 
280  KRATOS_INFO_IF("MPMNewtonRaphsonStrategy", this->GetEchoLevel() >= 3) << "Build RHS and Solve" << std::endl;
281  p_builder_and_solver->BuildRHSAndSolve(p_scheme, BaseType::GetModelPart(), rA, rDx, rb);
282  }
283  }
284  else
285  {
286  KRATOS_WARNING("MPMNewtonRaphsonStrategy") << "ATTENTION: no free DOFs!! " << std::endl;
287  }
288 
289  // Updating the results stored in the database
290  r_dof_set = p_builder_and_solver->GetDofSet();
291 
292  p_scheme->Update(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
293  p_scheme->FinalizeNonLinIteration(BaseType::GetModelPart(), rA, rDx, rb);
294 
295  // Move the mesh if needed
297 
298  // If converged
299  if (is_converged == true)
300  {
301  if (this->mpConvergenceCriteria->GetActualizeRHSflag() == true)
302  {
303  TSparseSpace::SetToZero(rb);
304 
305  p_builder_and_solver->BuildRHS(p_scheme, BaseType::GetModelPart(), rb);
306 
307  }
308 
309  is_converged = this->mpConvergenceCriteria->PostCriteria(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
310  }
311  }
312 
313  // Plot a warning if the maximum number of iterations is exceeded
314  if (iteration_number >= this->mMaxIterationNumber && BaseType::GetModelPart().GetCommunicator().MyPID() == 0)
315  {
316  if (this->GetEchoLevel() > 1) this->MaxIterationsExceeded();
317  }
318 
319  return is_converged;
320  }
321 
322 }; /* Class MPMResidualBasedNewtonRaphsonStrategy */
323 
324 } /* namespace Kratos.*/
325 
326 #endif /* KRATOS_MPM_RESIDUAL_BASED_NEWTON_RAPHSON_STRATEGY defined */
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
Implicit solving strategy base class This is the base class from which we will derive all the implici...
Definition: implicit_solving_strategy.h:61
Scheme< TSparseSpace, TDenseSpace > TSchemeType
Definition: implicit_solving_strategy.h:82
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
Short class definition.
Definition: mpm_residual_based_newton_raphson_strategy.hpp:68
BaseType::TSystemVectorType TSystemVectorType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:91
BaseType::DofsArrayType DofsArrayType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:87
KRATOS_CLASS_POINTER_DEFINITION(MPMResidualBasedNewtonRaphsonStrategy)
BaseType::TDataType TDataType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:81
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:79
BaseType::TSystemMatrixType TSystemMatrixType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:89
MPMResidualBasedNewtonRaphsonStrategy(ModelPart &rModelPart, typename TSchemeType::Pointer pScheme, typename TLinearSolver::Pointer pNewLinearSolver, typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria, int MaxIterations=30, bool CalculateReactions=false, bool ReformDofSetAtEachStep=false, bool MoveMeshFlag=false)
Definition: mpm_residual_based_newton_raphson_strategy.hpp:118
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:95
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:93
BaseType::TSystemMatrixPointerType TSystemMatrixPointerType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:97
BaseType::TSystemVectorPointerType TSystemVectorPointerType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:99
BaseType::TSchemeType TSchemeType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:85
MPMResidualBasedNewtonRaphsonStrategy(ModelPart &rModelPart, bool MoveMeshFlag=false)
Definition: mpm_residual_based_newton_raphson_strategy.hpp:110
virtual ~MPMResidualBasedNewtonRaphsonStrategy()
Definition: mpm_residual_based_newton_raphson_strategy.hpp:150
ConvergenceCriteria< TSparseSpace, TDenseSpace > TConvergenceCriteriaType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:72
MPMResidualBasedNewtonRaphsonStrategy(ModelPart &rModelPart, typename TSchemeType::Pointer pScheme, typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria, typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver, int MaxIterations=30, bool CalculateReactions=false, bool ReformDofSetAtEachStep=false, bool MoveMeshFlag=false)
Definition: mpm_residual_based_newton_raphson_strategy.hpp:133
TSparseSpace SparseSpaceType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:83
bool SolveSolutionStep() override
Solves the current step. This function returns true if a solution has been found, false otherwise.
Definition: mpm_residual_based_newton_raphson_strategy.hpp:157
ImplicitSolvingStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: mpm_residual_based_newton_raphson_strategy.hpp:77
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
This is the base Newton Raphson strategy.
Definition: residualbased_newton_raphson_strategy.h:66
TBuilderAndSolverType::Pointer GetBuilderAndSolver()
Get method for the builder and solver.
Definition: residualbased_newton_raphson_strategy.h:493
unsigned int mMaxIterationNumber
Definition: residualbased_newton_raphson_strategy.h:1261
TSchemeType::Pointer GetScheme()
Get method for the time scheme.
Definition: residualbased_newton_raphson_strategy.h:475
TSystemVectorPointerType mpb
The increment in the solution.
Definition: residualbased_newton_raphson_strategy.h:1237
TSystemMatrixPointerType mpA
The RHS vector of the system of equations.
Definition: residualbased_newton_raphson_strategy.h:1238
virtual void MaxIterationsExceeded()
This method prints information after reach the max number of iterations.
Definition: residualbased_newton_raphson_strategy.h:1340
TSystemVectorPointerType mpDx
The pointer to the convergence criteria employed.
Definition: residualbased_newton_raphson_strategy.h:1236
bool GetKeepSystemConstantDuringIterations()
Get method for the flag mKeepSystemConstantDuringIterations.
Definition: residualbased_newton_raphson_strategy.h:1158
TConvergenceCriteriaType::Pointer mpConvergenceCriteria
The pointer to the builder and solver employed.
Definition: residualbased_newton_raphson_strategy.h:1234
TSparseSpace::VectorPointerType TSystemVectorPointerType
Definition: solving_strategy.h:77
TDenseSpace::MatrixType LocalSystemMatrixType
Definition: solving_strategy.h:79
TSparseSpace::DataType TDataType
Definition: solving_strategy.h:69
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
TSparseSpace::MatrixPointerType TSystemMatrixPointerType
Definition: solving_strategy.h:75
bool MoveMeshFlag()
This function returns the flag that says if the mesh is moved.
Definition: solving_strategy.h:290
TSparseSpace::VectorType TSystemVectorType
Definition: solving_strategy.h:73
virtual void MoveMesh()
This function is designed to move the mesh.
Definition: solving_strategy.h:330
TDenseSpace::VectorType LocalSystemVectorType
Definition: solving_strategy.h:81
static IndexType Size(VectorType const &rV)
return size of vector rV
Definition: ublas_space.h:190
#define KRATOS_INFO(label)
Definition: logger.h:250
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
#define KRATOS_WARNING(label)
Definition: logger.h:265
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