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.
newton_raphson_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_NEWTON_RAPHSON_STRATEGY_H_INCLUDED)
11 #define KRATOS_NEWTON_RAPHSON_STRATEGY_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 
17 // Project includes
19 
20 //default convergence criterion
22 
24 
25 namespace Kratos
26 {
27 
30 
34 
38 
42 
46 
53 template <class TSparseSpace,
54  class TDenseSpace, // = DenseSpace<double>,
55  class TLinearSolver // = LinearSolver<TSparseSpace,TDenseSpace>
56  >
57 class NewtonRaphsonStrategy : public LinearStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
58 {
59  public:
62 
63  // Counted pointer of ClassName
65 
67 
69 
71 
73 
74  typedef typename BaseType::SchemeType SchemeType;
75 
76  typedef TLinearSolver LinearSolverType;
77 
78  typedef TSparseSpace SparseSpaceType;
79 
81 
83 
85 
87 
89 
92 
94 
105  typename SchemeType::Pointer pScheme,
106  typename BuilderAndSolverType::Pointer pBuilderAndSolver,
107  typename ConvergenceCriterionType::Pointer pConvergenceCriterion,
108  Flags& rOptions,
109  unsigned int MaxIterations = 30)
110  : LinearStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(rModelPart, pScheme, pBuilderAndSolver, rOptions)
111  {
112  KRATOS_TRY
113 
114  // Saving the convergence criteria to be used
115  mpConvergenceCriteria = pConvergenceCriterion;
116 
117  // Maximum iterations allowed
118  mMaxIterationNumber = MaxIterations;
119 
120  KRATOS_CATCH("")
121  }
122 
123 
134  typename SchemeType::Pointer pScheme,
135  typename LinearSolverType::Pointer pLinearSolver,
136  typename ConvergenceCriterionType::Pointer pConvergenceCriterion,
137  Flags& rOptions,
138  unsigned int MaxIterations = 30)
139  : NewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(rModelPart, pScheme, Kratos::make_shared<BlockBuilderAndSolver<TSparseSpace, TDenseSpace, TLinearSolver> >(pLinearSolver), pConvergenceCriterion, rOptions, MaxIterations)
140  {}
141 
142 
148  {
149  Clear();
150  }
151 
154 
156 
160 
164  void InitializeSolutionStep() override
165  {
166  KRATOS_TRY
167 
168  //set implex
169  if(this->mOptions.Is(LocalFlagType::IMPLEX))
170  this->GetModelPart().GetProcessInfo().SetValue(IMPLEX, true);
171 
173 
174  KRATOS_CATCH("")
175  }
176 
180  void FinalizeSolutionStep() override
181  {
182  KRATOS_TRY
183 
184  //Finalization of the solution step, operations to be done after achieving convergence
185 
186  //set implex calculation
187  if(this->mOptions.Is(LocalFlagType::IMPLEX)){
188  this->GetModelPart().GetProcessInfo().SetValue(IMPLEX, false);
189  if(this->mOptions.IsNot(LocalFlagType::COMPUTE_REACTIONS))
190  this->mpBuilderAndSolver->BuildRHS(this->mpScheme, this->GetModelPart(), (*this->mpb));
191  }
192 
194 
195  KRATOS_CATCH("")
196  }
197 
198 
202  bool SolveSolutionStep() override
203  {
204  KRATOS_TRY
205 
206  //initializing the parameters of the Newton-Raphson cicle
207  unsigned int iteration_number = 1;
208 
209  //setting the iteration number
210  this->GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
211 
212  this->Set(LocalFlagType::CONVERGED, this->SolveIteration());
213 
214  //iteration cycle... performed only for NonLinearProblems
215  while( this->IsNot(LocalFlagType::CONVERGED) && iteration_number++ < mMaxIterationNumber)
216  {
217  //setting the iteration number
218  this->GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
219 
220  this->Set(LocalFlagType::CONVERGED, this->SolveIteration());
221  }
222 
223  //plots a warning if the maximum number of iterations is exceeded
224  if(iteration_number >= mMaxIterationNumber)
225  {
226  if( this->GetEchoLevel() >= 0 )
227  KRATOS_INFO(" [Iterative loop interrupted] ") << "[" << iteration_number << " iterations performed] \n";
228  }
229 
230  return (this->Is(LocalFlagType::CONVERGED));
231 
232  KRATOS_CATCH("")
233 
234  }
235 
236 
240  bool SolveIteration() override
241  {
242  KRATOS_TRY
243 
244  bool is_converged = false;
245 
246  // Warning info
247  if(!SparseSpaceType::Size((*this->mpDx)))
248  KRATOS_WARNING("DOFS") << "solution has zero size, no free DOFs" << std::endl;
249 
250  // Initialize Iteration
251  this->mpScheme->InitializeNonLinearIteration(this->GetModelPart());
252 
253  is_converged = mpConvergenceCriteria->PreCriteria(this->GetModelPart(), this->mpBuilderAndSolver->GetDofSet(), (*this->mpA), (*this->mpDx), (*this->mpb));
254 
255  // Function to perform the building and the solving phase.
256  if(this->mOptions.IsNot(LocalFlagType::CONSTANT_SYSTEM_MATRIX)){
257 
258  TSparseSpace::SetToZero((*this->mpA));
259  TSparseSpace::SetToZero((*this->mpDx));
260  TSparseSpace::SetToZero((*this->mpb));
261 
262  this->mpBuilderAndSolver->BuildAndSolve(this->mpScheme, this->GetModelPart(), (*this->mpA), (*this->mpDx), (*this->mpb));
263  }
264  else{
265 
266  TSparseSpace::SetToZero((*this->mpDx));
267  TSparseSpace::SetToZero((*this->mpb));
268 
269  this->mpBuilderAndSolver->BuildRHSAndSolve(this->mpScheme, this->GetModelPart(), (*this->mpA), (*this->mpDx), (*this->mpb));
270  }
271 
272  // EchoInfo
273  this->mpBuilderAndSolver->EchoInfo(this->GetModelPart(), (*this->mpA), (*this->mpDx), (*this->mpb));
274 
275  // Updating the results
276  this->Update();
277 
278  // Finalize Iteration
279  this->mpScheme->FinalizeNonLinearIteration(this->GetModelPart());
280 
281  if(is_converged == true)
282  {
283  //initialisation of the convergence criteria (after first calculation only)
284  if( this->GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] == 1 ){
285  mpConvergenceCriteria->InitializeSolutionStep(this->GetModelPart(), this->mpBuilderAndSolver->GetDofSet(), (*this->mpA), (*this->mpDx), (*this->mpb));
286  }
287 
288  if(mpConvergenceCriteria->Is(CriterionLocalFlags::UPDATE_RHS))
289  {
290  TSparseSpace::SetToZero((*this->mpb));
291 
292  this->mpBuilderAndSolver->BuildRHS(this->mpScheme, this->GetModelPart(), (*this->mpb));
293  }
294 
295  is_converged = mpConvergenceCriteria->PostCriteria(this->GetModelPart(), this->mpBuilderAndSolver->GetDofSet(), (*this->mpA), (*this->mpDx), (*this->mpb));
296  }
297 
298  return is_converged;
299 
300  KRATOS_CATCH("")
301  }
302 
303 
307  void Clear() override
308  {
309  KRATOS_TRY
310 
311  BaseType::Clear();
312 
313  KRATOS_CATCH("")
314  }
315 
320  int Check() override
321  {
322  KRATOS_TRY
323 
324  //check linear strategy
325  BaseType::Check();
326 
327  //check the convergence criterion
328  mpConvergenceCriteria->Check(this->GetModelPart());
329 
330  return 0;
331 
332  KRATOS_CATCH("")
333  }
334 
335 
339 
351  void SetEchoLevel(const int Level) override
352  {
353  BaseType::SetEchoLevel(Level);
354  }
355 
360  void SetMaxIterationNumber(unsigned int MaxIterationNumber)
361  {
362  mMaxIterationNumber = MaxIterationNumber;
363  }
364 
369  unsigned int GetMaxIterationNumber() override
370  {
371  return mMaxIterationNumber;
372  }
373 
377 
381 
383 
384  protected:
387 
391 
392  typename ConvergenceCriterionType::Pointer mpConvergenceCriteria;
393 
394  unsigned int mMaxIterationNumber;
395 
402 
406  void Initialize() override
407  {
408  KRATOS_TRY
409 
411 
412  KRATOS_CATCH("")
413  }
414 
425 
426  private:
447 
450 
452 
453 };
454 
456 
463 
465 
466 } // namespace Kratos.
467 #endif // KRATOS_NEWTON_RAPHSON_STRATEGY_H_INCLUDED defined
Solution Buider and Solver based on block matrix.
Definition: block_builder_and_solver.hpp:58
Convergence Criterion base class.
Definition: convergence_criterion.hpp:52
void SetValue(const Variable< TDataType > &rThisVariable, TDataType const &rValue)
Sets the value for a given variable.
Definition: data_value_container.h:320
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
SchemeType::Pointer mpScheme
Definition: linear_strategy.hpp:427
SystemVectorPointerType mpDx
The pointer to the builder and solver employed.
Definition: linear_strategy.hpp:430
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 FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: linear_strategy.hpp:205
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
SystemVectorPointerType mpb
The incremement in the solution.
Definition: linear_strategy.hpp:431
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
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
This is the base Newton Raphson strategy.
Definition: newton_raphson_strategy.hpp:58
BaseType::SystemMatrixPointerType SystemMatrixPointerType
Definition: newton_raphson_strategy.hpp:86
unsigned int GetMaxIterationNumber() override
This method gets the flag mMaxIterationNumber.
Definition: newton_raphson_strategy.hpp:369
BaseType::SystemVectorType SystemVectorType
Definition: newton_raphson_strategy.hpp:84
TLinearSolver LinearSolverType
Definition: newton_raphson_strategy.hpp:76
NewtonRaphsonStrategy(ModelPart &rModelPart, typename SchemeType::Pointer pScheme, typename BuilderAndSolverType::Pointer pBuilderAndSolver, typename ConvergenceCriterionType::Pointer pConvergenceCriterion, Flags &rOptions, unsigned int MaxIterations=30)
Definition: newton_raphson_strategy.hpp:104
LinearStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: newton_raphson_strategy.hpp:66
TSparseSpace SparseSpaceType
Definition: newton_raphson_strategy.hpp:78
BaseType::BuilderAndSolverType BuilderAndSolverType
Definition: newton_raphson_strategy.hpp:72
bool SolveIteration() override
Solves the iteration. This function returns true if a solution has been found, false otherwise.
Definition: newton_raphson_strategy.hpp:240
unsigned int mMaxIterationNumber
The pointer to the convergence criteria employed.
Definition: newton_raphson_strategy.hpp:394
~NewtonRaphsonStrategy() override
Destructor.
Definition: newton_raphson_strategy.hpp:147
BaseType::SchemeType SchemeType
Definition: newton_raphson_strategy.hpp:74
BaseType::DofsArrayType DofsArrayType
Definition: newton_raphson_strategy.hpp:80
void Initialize() override
Initialization of member variables and prior operations.
Definition: newton_raphson_strategy.hpp:406
BaseType::LocalFlagType LocalFlagType
Definition: newton_raphson_strategy.hpp:68
int Check() override
Function to perform expensive checks.
Definition: newton_raphson_strategy.hpp:320
void InitializeSolutionStep() override
Performs all the required operations that should be done (for each step) before solving the solution ...
Definition: newton_raphson_strategy.hpp:164
NewtonRaphsonStrategy(ModelPart &rModelPart, typename SchemeType::Pointer pScheme, typename LinearSolverType::Pointer pLinearSolver, typename ConvergenceCriterionType::Pointer pConvergenceCriterion, Flags &rOptions, unsigned int MaxIterations=30)
Definition: newton_raphson_strategy.hpp:133
ConvergenceCriterion< TSparseSpace, TDenseSpace > ConvergenceCriterionType
Definition: newton_raphson_strategy.hpp:70
void SetEchoLevel(const int Level) override
This sets the level of echo for the solving strategy.
Definition: newton_raphson_strategy.hpp:351
bool SolveSolutionStep() override
Solves the current step. This function returns true if a solution has been found, false otherwise.
Definition: newton_raphson_strategy.hpp:202
void Clear() override
Clears the internal storage.
Definition: newton_raphson_strategy.hpp:307
BaseType::SystemMatrixType SystemMatrixType
Definition: newton_raphson_strategy.hpp:82
ConvergenceCriterionType::Pointer mpConvergenceCriteria
Definition: newton_raphson_strategy.hpp:392
BaseType::SystemVectorPointerType SystemVectorPointerType
Definition: newton_raphson_strategy.hpp:88
void SetMaxIterationNumber(unsigned int MaxIterationNumber)
This method sets the flag mMaxIterationNumber.
Definition: newton_raphson_strategy.hpp:360
void FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: newton_raphson_strategy.hpp:180
KRATOS_CLASS_POINTER_DEFINITION(NewtonRaphsonStrategy)
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
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
TSparseSpace::VectorType SystemVectorType
Definition: solution_strategy.hpp:61
virtual int GetEchoLevel()
This returns the level of echo for the solution strategy.
Definition: solution_strategy.hpp:206
Solver local flags class definition.
Definition: solution_local_flags.hpp:48
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
ProcessInfo & GetProcessInfo(ModelPart &rModelPart)
Definition: add_model_part_to_python.cpp:54
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