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.
line_search_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_LINE_SEARCH_STRATEGY )
15 #define KRATOS_LINE_SEARCH_STRATEGY
16 
17 // System includes
18 
19 // External includes
20 
21 // Project includes
22 #include "includes/define.h"
23 #include "includes/model_part.h"
25 
26 
27 namespace Kratos
28 {
29 
32 
33 
37 
39 
40 
43 
44 
48 
49 
50 
54 
56 
58 
59 //URL[Example of use html]{ extended_documentation/no_ex_of_use.html}
60 
61 //URL[Example of use pdf]{ extended_documentation/no_ex_of_use.pdf}
62 
63 //URL[Example of use doc]{ extended_documentation/no_ex_of_use.doc}
64 
65 //URL[Example of use ps]{ extended_documentation/no_ex_of_use.ps}
66 
67 
68 //URL[Extended documentation html]{ extended_documentation/no_ext_doc.html}
69 
70 //URL[Extended documentation pdf]{ extended_documentation/no_ext_doc.pdf}
71 
72 //URL[Extended documentation doc]{ extended_documentation/no_ext_doc.doc}
73 
74 //URL[Extended documentation ps]{ extended_documentation/no_ext_doc.ps}
75 
76 
77 
78 template<class TSparseSpace,
79  class TDenseSpace, // = DenseSpace<double>,
80  class TLinearSolver //= LinearSolver<TSparseSpace,TDenseSpace>
81  >
83  : public ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
84 {
85 public:
89 
90  // Counted pointer of ClassName
92 
96 
98 
99  typedef typename BaseType::TDataType TDataType;
100 
101  typedef TSparseSpace SparseSpaceType;
102 
104 
105  //typedef typename BaseType::DofSetType DofSetType;
106 
108 
110 
112 
114 
116 
119 
120 
124 
129  {
130  }
131 
137  explicit LineSearchStrategy(ModelPart& rModelPart, Parameters ThisParameters)
138  : BaseType(rModelPart)
139  {
140  // Validate and assign defaults
141  ThisParameters = this->ValidateAndAssignParameters(ThisParameters, this->GetDefaultParameters());
142  this->AssignSettings(ThisParameters);
143  }
144 
157  ModelPart& rModelPart,
158  typename TSchemeType::Pointer pScheme,
159  typename TLinearSolver::Pointer pNewLinearSolver,
160  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
161  int MaxIterations = 30,
162  bool CalculateReactions = false,
163  bool ReformDofSetAtEachStep = false,
164  bool MoveMeshFlag = false
165  ) : BaseType(rModelPart, pScheme, pNewLinearSolver,pNewConvergenceCriteria,MaxIterations,CalculateReactions,ReformDofSetAtEachStep, MoveMeshFlag)
166  {
167  Parameters default_settings = this->GetDefaultParameters();
168  OverrideDefaultSettingsWithParameters(default_settings, MaxIterations, ReformDofSetAtEachStep, CalculateReactions);
169  this->AssignSettings(default_settings);
170  }
171 
184  ModelPart& rModelPart,
185  typename TSchemeType::Pointer pScheme,
186  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
187  typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver,
188  int MaxIterations = 30,
189  bool CalculateReactions = false,
190  bool ReformDofSetAtEachStep = false,
191  bool MoveMeshFlag = false
192  ) : BaseType(rModelPart, pScheme, pNewConvergenceCriteria, pNewBuilderAndSolver, MaxIterations, CalculateReactions, ReformDofSetAtEachStep, MoveMeshFlag)
193  {
194  Parameters default_settings = this->GetDefaultParameters();
195  OverrideDefaultSettingsWithParameters(default_settings, MaxIterations, ReformDofSetAtEachStep, CalculateReactions);
196  this->AssignSettings(default_settings);
197  }
198 
211  KRATOS_DEPRECATED_MESSAGE("Constructor deprecated, please use the constructor without linear solver")
213  ModelPart& rModelPart,
214  typename TSchemeType::Pointer pScheme,
215  typename TLinearSolver::Pointer pNewLinearSolver,
216  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
217  typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver,
218  int MaxIterations = 30,
219  bool CalculateReactions = false,
220  bool ReformDofSetAtEachStep = false,
221  bool MoveMeshFlag = false
222  ) : BaseType(rModelPart, pScheme, pNewLinearSolver,pNewConvergenceCriteria,pNewBuilderAndSolver,MaxIterations,CalculateReactions,ReformDofSetAtEachStep, MoveMeshFlag)
223  {
224  Parameters default_settings = this->GetDefaultParameters();
225  OverrideDefaultSettingsWithParameters(default_settings, MaxIterations, ReformDofSetAtEachStep, CalculateReactions);
226  this->AssignSettings(default_settings);
227  }
228 
238  ModelPart& rModelPart,
239  typename TSchemeType::Pointer pScheme,
240  typename TLinearSolver::Pointer pNewLinearSolver,
241  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
242  Parameters ThisParameters
243  ): BaseType(rModelPart, pScheme, pNewLinearSolver,pNewConvergenceCriteria, BaseType::GetDefaultParameters())
244  {
245  // Validate and assign defaults
246  ThisParameters = this->ValidateAndAssignParameters(ThisParameters, this->GetDefaultParameters());
247  this->AssignSettings(ThisParameters);
248  }
249 
259  ModelPart& rModelPart,
260  typename TSchemeType::Pointer pScheme,
261  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
262  typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver,
263  Parameters ThisParameters
264  ): BaseType(rModelPart, pScheme, pNewConvergenceCriteria, pNewBuilderAndSolver, BaseType::GetDefaultParameters())
265  {
266  // Validate and assign defaults
267  ThisParameters = this->ValidateAndAssignParameters(ThisParameters, this->GetDefaultParameters());
268  this->AssignSettings(ThisParameters);
269  }
270 
276  {
277  }
278 
279 
282 
284 
288 
294  typename SolvingStrategyType::Pointer Create(
295  ModelPart& rModelPart,
296  Parameters ThisParameters
297  ) const override
298  {
299  return Kratos::make_shared<ClassType>(rModelPart, ThisParameters);
300  }
301 
307  {
308  Parameters default_parameters = Parameters(R"(
309  {
310  "name" : "line_search_strategy",
311  "max_line_search_iterations" : 5,
312  "first_alpha_value" : 0.5,
313  "second_alpha_value" : 1.0,
314  "min_alpha" : 0.1,
315  "max_alpha" : 2.0,
316  "line_search_tolerance" : 0.5
317  })");
318 
319  // Getting base class default parameters
320  const Parameters base_default_parameters = BaseType::GetDefaultParameters();
321  default_parameters.RecursivelyAddMissingParameters(base_default_parameters);
322  return default_parameters;
323  }
324 
329  static std::string Name()
330  {
331  return "line_search_strategy";
332  }
333 
336 
338 
342 
346 
348  std::string Info() const override
349  {
350  return "LineSearchStrategy";
351  }
352 
354  void PrintInfo(std::ostream& rOStream) const override
355  {
356  rOStream << Info();
357  }
358 
360  void PrintData(std::ostream& rOStream) const override
361  {
362  rOStream << Info();
363  }
364 
368 
369 
371 
372 private:
375 
376 
380 
381  int mMaxLineSearchIterations; //Maximum number of iterations line search do
382  double mFirstAlphaValue; //First alpha guess value used for the first iteration
383  double mSecondAlphaValue; //Second alpha guess value used for the first iteration
384  double mMinAlpha; //Minimum possible alpha value at the end of the algorithm
385  double mMaxAlpha; //Maximum possible alpha value at the end of the algorithm
386  double mLineSearchTolerance; //Tolerance of the line search algorithm, defined as the ratio between
387  //maximum residual*alpha*dx and current iteration residual*alpha*dx
391 
392 
396 
397 
398 
402 
406 
407 
411 
412 
413 
415 
416 protected:
419 
420 
424 
425 
429 
439  TSystemVectorType& Dx,
441  const bool MoveMesh
442  ) override
443  {
444  typename TSchemeType::Pointer pScheme = this->GetScheme();
445  typename TBuilderAndSolverType::Pointer pBuilderAndSolver = this->GetBuilderAndSolver();
446 
447  TSystemVectorType aux(Dx);
448 
449  double x1 = mFirstAlphaValue;
450  double x2 = mSecondAlphaValue;
451 
452  bool converged = false;
453  int it = 0;
454  double xprevious = 0.0;
455 
456  //Compute residual with 1 coefficient update (x1)
457  //since no update was performed yet, this includes an increment wrt the previous
458  //solution of x1*Dx
459  TSparseSpace::Assign(aux,x1-xprevious, Dx);
460  xprevious = x1;
462  TSparseSpace::SetToZero(b);
463  pBuilderAndSolver->BuildRHS(pScheme, BaseType::GetModelPart(), b );
464  double r1 = TSparseSpace::Dot(aux,b);
465 
466  double rmax = std::abs(r1);
467  while(!converged && it < mMaxLineSearchIterations) {
468 
469  //Compute residual with 2 coefficient update (x2)
470  //since the database was initialized with x1*Dx
471  //we need to apply ONLY THE INCREMENT, that is (x2-xprevious)*Dx
472  TSparseSpace::Assign(aux,x2-xprevious, Dx);
473  xprevious = x2;
475  TSparseSpace::SetToZero(b);
476  pBuilderAndSolver->BuildRHS(pScheme, BaseType::GetModelPart(), b );
477  double r2 = TSparseSpace::Dot(aux,b);
478 
479  if(it == 0) {
480  rmax = std::max(rmax,std::abs(r2));
481  }
482  double rmin = std::min(std::abs(r1),std::abs(r2));
483 
484  //Find optimum
485  double x = 1.0;
486  if(std::abs(r1 - r2) > 1e-10)
487  x = (r1*x2 - r2*x1)/(r1 - r2);
488 
489  if(x < mMinAlpha) {
490  x = mMinAlpha;
491  } else if(x > mMaxAlpha) {
492  x = mMaxAlpha;
493  }
494 
495  //Perform final update
496  TSparseSpace::Assign(aux,x-xprevious, Dx);
497  xprevious = x;
499  if(rmin < mLineSearchTolerance*rmax) {
500  KRATOS_INFO("LineSearchStrategy") << "LINE SEARCH it " << it << " coeff = " << x << " r1 = " << r1 << " r2 = " << r2 << std::endl;
501  converged = true;
502  TSparseSpace::Assign(aux,x, Dx);
503  break;
504  }
505 
506  //note that we compute the next residual only if it is strictly needed (we break on the line before if it is not needed)
507  TSparseSpace::SetToZero(b);
508  pBuilderAndSolver->BuildRHS(pScheme, BaseType::GetModelPart(), b );
509  double rf = TSparseSpace::Dot(aux,b);
510 
511  KRATOS_INFO("LineSearchStrategy") << "LINE SEARCH it " << it << " coeff = " << x << " rf = " << rf << " r1 = " << r1 << " r2 = " << r2 << std::endl;
512 
513 
514  if(std::abs(rf) < rmax*mLineSearchTolerance) {
515  converged = true;
516  TSparseSpace::Assign(aux,x, Dx);
517  } else {
518  if(std::abs(r1)>std::abs(r2)) {
519  r1 = rf;
520  x1 = x;
521  } else {
522  r2 = r1;
523  x2 = x1;
524  r1 = rf;
525  x1 = x;
526  }
527  converged = false;
528  }
529 
530 
531  it++;
532  }
533  TSparseSpace::SetToZero(b);
534  }
535 
540  void AssignSettings(const Parameters ThisParameters) override
541  {
542  BaseType::AssignSettings(ThisParameters);
543  mMaxLineSearchIterations = ThisParameters["max_line_search_iterations"].GetInt();
544  mFirstAlphaValue = ThisParameters["first_alpha_value"].GetDouble();
545  mSecondAlphaValue = ThisParameters["second_alpha_value"].GetDouble();
546  mMinAlpha = ThisParameters["min_alpha"].GetDouble();
547  mMaxAlpha = ThisParameters["max_alpha"].GetDouble();
548  mLineSearchTolerance = ThisParameters["line_search_tolerance"].GetDouble();
549  }
550 
559  Parameters& rDefaultSettings,
560  const double MaxIterations,
561  const bool ReformDofSetAtEachStep,
562  const bool CalculateReactions
563  )
564  {
565  rDefaultSettings["max_iteration"].SetInt(MaxIterations);
566  rDefaultSettings["reform_dofs_at_each_step"].SetBool(ReformDofSetAtEachStep);
567  rDefaultSettings["compute_reactions"].SetBool(CalculateReactions);
568  }
569 
573 
574 
578 
579 
583 
584 
588 
594  {
595  };
596 
597 
599 
600 }; /* Class LineSearchStrategy */
601 
603 
606 
607 
609 
610 } /* namespace Kratos. */
611 
612 #endif /* KRATOS_LINE_SEARCH_STRATEGY defined */
Current class provides an implementation for the base builder and solving operations.
Definition: builder_and_solver.h:64
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
BaseType::TSystemVectorType TSystemVectorType
Definition: implicit_solving_strategy.h:72
BaseType::TSystemMatrixType TSystemMatrixType
Definition: implicit_solving_strategy.h:70
Short class definition.
Definition: line_search_strategy.h:84
ResidualBasedNewtonRaphsonStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: line_search_strategy.h:94
BaseType::TSystemMatrixType TSystemMatrixType
Definition: line_search_strategy.h:109
BaseType::TSystemVectorType TSystemVectorType
Definition: line_search_strategy.h:111
TSparseSpace SparseSpaceType
Definition: line_search_strategy.h:101
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: line_search_strategy.h:306
SolvingStrategy< TSparseSpace, TDenseSpace > SolvingStrategyType
Definition: line_search_strategy.h:93
BaseType::DofsArrayType DofsArrayType
Definition: line_search_strategy.h:107
std::string Info() const override
Turn back information as a string.
Definition: line_search_strategy.h:348
KRATOS_CLASS_POINTER_DEFINITION(LineSearchStrategy)
LineSearchStrategy(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)
Constructor specifying the builder and solver.
Definition: line_search_strategy.h:183
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: line_search_strategy.h:113
BaseType::TSchemeType TSchemeType
Definition: line_search_strategy.h:103
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: line_search_strategy.h:360
void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: line_search_strategy.h:540
SolvingStrategyType::Pointer Create(ModelPart &rModelPart, Parameters ThisParameters) const override
Create method.
Definition: line_search_strategy.h:294
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: line_search_strategy.h:97
LineSearchStrategy()
Default constructor.
Definition: line_search_strategy.h:128
LineSearchStrategy< TSparseSpace, TDenseSpace, TLinearSolver > ClassType
Definition: line_search_strategy.h:95
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: line_search_strategy.h:115
LineSearchStrategy(const LineSearchStrategy &Other)
Definition: line_search_strategy.h:593
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: line_search_strategy.h:354
LineSearchStrategy(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: line_search_strategy.h:156
void UpdateDatabase(TSystemMatrixType &A, TSystemVectorType &Dx, TSystemVectorType &b, const bool MoveMesh) override
Definition: line_search_strategy.h:437
void OverrideDefaultSettingsWithParameters(Parameters &rDefaultSettings, const double MaxIterations, const bool ReformDofSetAtEachStep, const bool CalculateReactions)
This method overrides the default settings with user provided parameters.
Definition: line_search_strategy.h:558
ConvergenceCriteria< TSparseSpace, TDenseSpace > TConvergenceCriteriaType
Definition: line_search_strategy.h:88
LineSearchStrategy(ModelPart &rModelPart, Parameters ThisParameters)
Default constructor. (with parameters)
Definition: line_search_strategy.h:137
BaseType::TDataType TDataType
Definition: line_search_strategy.h:99
BaseType::TSystemVectorPointerType TSystemVectorPointerType
Definition: line_search_strategy.h:118
LineSearchStrategy(ModelPart &rModelPart, typename TSchemeType::Pointer pScheme, typename TLinearSolver::Pointer pNewLinearSolver, typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria, Parameters ThisParameters)
Definition: line_search_strategy.h:237
LineSearchStrategy(ModelPart &rModelPart, typename TSchemeType::Pointer pScheme, typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria, typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver, Parameters ThisParameters)
Definition: line_search_strategy.h:258
BaseType::TSystemMatrixPointerType TSystemMatrixPointerType
Definition: line_search_strategy.h:117
static std::string Name()
Returns the name of the class as used in the settings (snake_case format)
Definition: line_search_strategy.h:329
~LineSearchStrategy() override
Definition: line_search_strategy.h:275
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
double GetDouble() const
This method returns the double contained in the current Parameter.
Definition: kratos_parameters.cpp:657
int GetInt() const
This method returns the integer contained in the current Parameter.
Definition: kratos_parameters.cpp:666
void SetBool(const bool Value)
This method sets the bool contained in the current Parameter.
Definition: kratos_parameters.cpp:803
void RecursivelyAddMissingParameters(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing contain at least all parameters...
Definition: kratos_parameters.cpp:1457
void SetInt(const int Value)
This method sets the integer contained in the current Parameter.
Definition: kratos_parameters.cpp:795
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
virtual void UpdateDatabase(TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb, const bool MoveMesh)
Here the database is updated.
Definition: residualbased_newton_raphson_strategy.h:1278
TSchemeType::Pointer GetScheme()
Get method for the time scheme.
Definition: residualbased_newton_raphson_strategy.h:475
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: residualbased_newton_raphson_strategy.h:1069
BaseType::TSchemeType TSchemeType
Definition: residualbased_newton_raphson_strategy.h:87
void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: residualbased_newton_raphson_strategy.h:1351
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: residualbased_newton_raphson_strategy.h:81
This class provides the implementation of the basic tasks that are needed by the solution strategy.
Definition: scheme.h:56
Solving strategy base class This is the base class from which we will derive all the strategies (impl...
Definition: solving_strategy.h:64
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
virtual Parameters ValidateAndAssignParameters(Parameters ThisParameters, const Parameters DefaultParameters) const
This method validate and assign default parameters.
Definition: solving_strategy.h:507
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
#define KRATOS_INFO(label)
Definition: logger.h:250
static double max(double a, double b)
Definition: GeometryFunctions.h:79
static double min(double a, double b)
Definition: GeometryFunctions.h:71
double Dot(SparseSpaceType &dummy, SparseSpaceType::VectorType &rX, SparseSpaceType::VectorType &rY)
Definition: add_strategies_to_python.cpp:85
void Assign(const Expression &rExpression, const IndexType EntityIndex, const IndexType EntityDataBeginIndex, TDataType &rValue, std::index_sequence< TIndex... >)
Definition: variable_expression_data_io.cpp:41
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
namespace KRATOS_DEPRECATED_MESSAGE("Please use std::filesystem directly") filesystem
Definition: kratos_filesystem.h:33
x2
Definition: generate_frictional_mortar_condition.py:122
x1
Definition: generate_frictional_mortar_condition.py:121
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
A
Definition: sensitivityMatrix.py:70
x
Definition: sensitivityMatrix.py:49
e
Definition: run_cpp_mpi_tests.py:31