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.
displacement_criteria.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 #pragma once
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "includes/model_part.h"
25 
26 namespace Kratos
27 {
30 
34 
38 
42 
46 
54 template<class TSparseSpace,
55  class TDenseSpace
56  >
58  : public ConvergenceCriteria< TSparseSpace, TDenseSpace >
59 {
60 public:
63 
66 
69 
72 
74  using SparseSpaceType = TSparseSpace;
75 
77  using TDataType = typename BaseType::TDataType;
78 
81 
83  using DofType = typename Node::DofType;
84 
87 
90 
92  using IndexType = std::size_t;
93 
95  using SizeType = std::size_t;
96 
100 
103  : BaseType()
104  {
105  }
106 
111  explicit DisplacementCriteria(Kratos::Parameters ThisParameters)
112  : BaseType()
113  {
114  // Validate and assign defaults
115  ThisParameters = this->ValidateAndAssignParameters(ThisParameters, this->GetDefaultParameters());
116  this->AssignSettings(ThisParameters);
117  }
118 
125  TDataType NewRatioTolerance,
126  TDataType AlwaysConvergedNorm)
127  : BaseType(),
128  mRatioTolerance(NewRatioTolerance),
129  mAlwaysConvergedNorm(AlwaysConvergedNorm)
130  {
131  }
132 
137  explicit DisplacementCriteria( DisplacementCriteria const& rOther )
138  :BaseType(rOther)
142  {
143  }
144 
148  ~DisplacementCriteria() override {}
149 
153 
156 
160 
165  typename BaseType::Pointer Create(Parameters ThisParameters) const override
166  {
167  return Kratos::make_shared<ClassType>(ThisParameters);
168  }
169 
180  ModelPart& rModelPart,
181  DofsArrayType& rDofSet,
182  const TSystemMatrixType& rA,
183  const TSystemVectorType& rDx,
184  const TSystemVectorType& rb
185  ) override
186  {
187  if (SparseSpaceType::Size(rDx) != 0) { //if we are solving for something
188  // Some values
189  const int rank = rModelPart.GetCommunicator().GetDataCommunicator().Rank();
190  const TDataType approx_zero_tolerance = std::numeric_limits<TDataType>::epsilon();
191 
192  // The final correction norm calculation
193  SizeType size_solution = 0;
194  const TDataType final_correction_norm = CalculateFinalCorrectionNorm(size_solution, rDofSet, rDx, rModelPart);
195 
196  TDataType ratio = 0.0;
197 
198  CalculateReferenceNorm(rDofSet, rModelPart);
199  if (mReferenceDispNorm < approx_zero_tolerance) {
200  KRATOS_WARNING("DisplacementCriteria") << "NaN norm is detected. Setting reference to convergence criteria" << std::endl;
201  mReferenceDispNorm = final_correction_norm;
202  }
203 
204  if(final_correction_norm < approx_zero_tolerance) {
205  ratio = 0.0;
206  } else {
207  ratio = final_correction_norm/mReferenceDispNorm;
208  }
209 
210  const TDataType float_size_solution = static_cast<TDataType>(size_solution);
211 
212  const TDataType absolute_norm = (final_correction_norm/std::sqrt(float_size_solution));
213 
214  KRATOS_INFO_IF("DISPLACEMENT CRITERION", this->GetEchoLevel() > 0 && rank == 0) << " :: [ Obtained ratio = " << ratio << "; Expected ratio = " << mRatioTolerance << "; Absolute norm = " << absolute_norm << "; Expected norm = " << mAlwaysConvergedNorm << "]" << std::endl;
215 
216  rModelPart.GetProcessInfo()[CONVERGENCE_RATIO] = ratio;
217  rModelPart.GetProcessInfo()[RESIDUAL_NORM] = absolute_norm;
218 
219  const bool has_achieved_convergence = ratio <= mRatioTolerance || absolute_norm < mAlwaysConvergedNorm; // || (final_correction_norm/x.size())<=1e-7)
220  KRATOS_INFO_IF("DISPLACEMENT CRITERION", has_achieved_convergence && this->GetEchoLevel() > 0 && rank == 0) << "Convergence is achieved" << std::endl;
221  return has_achieved_convergence;
222  } else { //in this case all the displacements are imposed!
223  return true;
224  }
225  }
226 
232  ModelPart& rModelPart
233  ) override
234  {
236  }
237 
247  ModelPart& rModelPart,
248  DofsArrayType& rDofSet,
249  const TSystemMatrixType& rA,
250  const TSystemVectorType& rDx,
251  const TSystemVectorType& rb
252  ) override
253  {
254  BaseType::InitializeSolutionStep(rModelPart, rDofSet, rA, rDx, rb);
255  }
256 
266  ModelPart& rModelPart,
267  DofsArrayType& rDofSet,
268  const TSystemMatrixType& rA,
269  const TSystemVectorType& rDx,
270  const TSystemVectorType& rb
271  ) override
272  {
273  BaseType::FinalizeSolutionStep(rModelPart, rDofSet, rA, rDx, rb);
274  }
275 
280  static std::string Name()
281  {
282  return "displacement_criteria";
283  }
284 
290  {
291  Parameters default_parameters = Parameters(R"(
292  {
293  "name" : "displacement_criteria",
294  "displacement_relative_tolerance" : 1.0e-4,
295  "displacement_absolute_tolerance" : 1.0e-9
296  })");
297 
298  // Getting base class default parameters
299  const Parameters base_default_parameters = BaseType::GetDefaultParameters();
300  default_parameters.RecursivelyAddMissingParameters(base_default_parameters);
301  return default_parameters;
302  }
303 
307 
311 
315 
317  std::string Info() const override
318  {
319  return "DisplacementCriteria";
320  }
321 
323  void PrintInfo(std::ostream& rOStream) const override
324  {
325  rOStream << Info();
326  }
327 
329  void PrintData(std::ostream& rOStream) const override
330  {
331  rOStream << Info();
332  }
333 
337 
339 protected:
342 
346 
348 
350 
352 
356 
360 
365  void AssignSettings(const Parameters ThisParameters) override
366  {
367  BaseType::AssignSettings(ThisParameters);
368  mAlwaysConvergedNorm = ThisParameters["displacement_absolute_tolerance"].GetDouble();
369  mRatioTolerance = ThisParameters["displacement_relative_tolerance"].GetDouble();
370  }
371 
375 
379 
383 
385 private:
388 
392 
396 
400 
410  bool IsFreeAndLocalDof(
411  const DofType& rDof,
412  const int Rank
413  )
414  {
415  if constexpr (!TSparseSpace::IsDistributedSpace()) {
416  return rDof.IsFree();
417  } else {
418  return (rDof.IsFree() && (rDof.GetSolutionStepValue(PARTITION_INDEX) == Rank));
419  }
420  }
421 
429  void CalculateReferenceNorm(
430  DofsArrayType& rDofSet,
431  ModelPart& rModelPart
432  )
433  {
434  // Retrieve the data communicator
435  const auto& r_data_communicator = rModelPart.GetCommunicator().GetDataCommunicator();
436 
437  // The current MPI rank
438  const int rank = r_data_communicator.Rank();
439 
440  // Auxiliary struct
441  struct TLS {TDataType dof_value{};};
442 
443  const TDataType reference_disp_norm = block_for_each<SumReduction<TDataType>>(rDofSet, TLS(), [this, &rank](auto& rDof, TLS& rTLS) {
444  if (ClassType::IsFreeAndLocalDof(rDof, rank)) {
445  rTLS.dof_value = rDof.GetSolutionStepValue();
446  return std::pow(rTLS.dof_value, 2);
447  } else {
448  return TDataType();
449  }
450  });
451  mReferenceDispNorm = std::sqrt(r_data_communicator.SumAll(reference_disp_norm));
452  }
453 
462  TDataType CalculateFinalCorrectionNorm(
463  SizeType& rDofNum,
464  DofsArrayType& rDofSet,
465  const TSystemVectorType& rDx,
466  ModelPart& rModelPart
467  )
468  {
469  // Retrieve the data communicator
470  const auto& r_data_communicator = rModelPart.GetCommunicator().GetDataCommunicator();
471 
472  // The current MPI rank
473  const int rank = r_data_communicator.Rank();
474 
475  // Initialize
476  TDataType final_correction_norm = TDataType();
477  unsigned int dof_num = 0;
478 
479  // Custom reduction
480  using CustomReduction = CombinedReduction<SumReduction<TDataType>,SumReduction<unsigned int>>;
481 
482  // Auxiliary struct
483  struct TLS {TDataType dof_value{}; TDataType variation_dof_value{};};
484 
485  // Loop over Dofs
486  std::tie(final_correction_norm, dof_num) = block_for_each<CustomReduction>(rDofSet, TLS(), [this, &rDx, &rank](auto& rDof, TLS& rTLS) {
487  if (this->IsFreeAndLocalDof(rDof, rank)) {
488  rTLS.variation_dof_value = SparseSpaceType::GetValue(rDx, rDof.EquationId());
489  return std::make_tuple(std::pow(rTLS.variation_dof_value, 2), 1);
490  } else {
491  return std::make_tuple(TDataType(), 0);
492  }
493  });
494 
495  rDofNum = static_cast<SizeType>(r_data_communicator.SumAll(dof_num));
496  return std::sqrt(r_data_communicator.SumAll(final_correction_norm));
497  }
498 
500 }; /* Class DisplacementCriteria */
501 
505 
507 
508 } /* namespace Kratos.*/
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
int GetEchoLevel()
This returns the level of echo for the solving strategy.
Definition: convergence_criteria.h:209
virtual Parameters ValidateAndAssignParameters(Parameters ThisParameters, const Parameters DefaultParameters) const
This method validate and assign default parameters.
Definition: convergence_criteria.h:466
TSparseSpace::MatrixType TSystemMatrixType
Matrix type definition.
Definition: convergence_criteria.h:72
ModelPart::DofsArrayType DofsArrayType
DoF array type definition.
Definition: convergence_criteria.h:81
virtual Parameters GetDefaultParameters() const
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: convergence_criteria.h:384
TSparseSpace::DataType TDataType
Data type definition.
Definition: convergence_criteria.h:70
TSparseSpace::VectorType TSystemVectorType
Vector type definition.
Definition: convergence_criteria.h:74
virtual void FinalizeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb)
This function finalizes the solution step.
Definition: convergence_criteria.h:337
bool mConvergenceCriteriaIsInitialized
This "flag" is set in order to know if it is necessary to actualize the RHS.
Definition: convergence_criteria.h:448
virtual void InitializeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb)
This function initializes the solution step.
Definition: convergence_criteria.h:299
virtual void AssignSettings(const Parameters ThisParameters)
This method assigns settings to member variables.
Definition: convergence_criteria.h:479
virtual int Rank() const
Get the parallel rank for this DataCommunicator.
Definition: data_communicator.h:587
This is a convergence criteria that considers the increment on the solution as criteria.
Definition: displacement_criteria.h:59
TDataType mReferenceDispNorm
The absolute value threshold for the norm of the residual.
Definition: displacement_criteria.h:351
DisplacementCriteria & operator=(DisplacementCriteria const &rOther)=delete
Deleted assignment operator.
DisplacementCriteria(DisplacementCriteria const &rOther)
Copy constructor.
Definition: displacement_criteria.h:137
void InitializeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
This function initializes the solution step.
Definition: displacement_criteria.h:246
std::string Info() const override
Turn back information as a string.
Definition: displacement_criteria.h:317
typename BaseType::TSystemVectorType TSystemVectorType
The dense vector type.
Definition: displacement_criteria.h:89
KRATOS_CLASS_POINTER_DEFINITION(DisplacementCriteria)
Pointer definition of DisplacementCriteria.
static std::string Name()
Returns the name of the class as used in the settings (snake_case format)
Definition: displacement_criteria.h:280
DisplacementCriteria(Kratos::Parameters ThisParameters)
Default constructor. (with parameters)
Definition: displacement_criteria.h:111
void FinalizeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
This function finalizes the solution step.
Definition: displacement_criteria.h:265
DisplacementCriteria(TDataType NewRatioTolerance, TDataType AlwaysConvergedNorm)
Constructor 2 arguments.
Definition: displacement_criteria.h:124
TDataType mRatioTolerance
Definition: displacement_criteria.h:347
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: displacement_criteria.h:289
void Initialize(ModelPart &rModelPart) override
This function initialize the convergence criteria.
Definition: displacement_criteria.h:231
TSparseSpace SparseSpaceType
The definition of the sparse space type.
Definition: displacement_criteria.h:74
std::size_t SizeType
Definition of the size type.
Definition: displacement_criteria.h:95
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: displacement_criteria.h:329
void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: displacement_criteria.h:365
typename Node::DofType DofType
The definition of the DoF data type.
Definition: displacement_criteria.h:83
BaseType::Pointer Create(Parameters ThisParameters) const override
Create method.
Definition: displacement_criteria.h:165
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
Compute relative and absolute error.
Definition: displacement_criteria.h:179
typename BaseType::DofsArrayType DofsArrayType
The dofs array type.
Definition: displacement_criteria.h:80
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: displacement_criteria.h:323
TDataType mAlwaysConvergedNorm
The ratio threshold for the norm of the residual.
Definition: displacement_criteria.h:349
typename BaseType::TDataType TDataType
The data type.
Definition: displacement_criteria.h:77
DisplacementCriteria()
Default constructor.
Definition: displacement_criteria.h:102
~DisplacementCriteria() override
Destructor.
Definition: displacement_criteria.h:148
std::size_t IndexType
Definition of the IndexType.
Definition: displacement_criteria.h:92
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Communicator & GetCommunicator()
Definition: model_part.h:1821
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
Dof< double > DofType
Dof type.
Definition: node.h:83
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
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
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
static TDataType GetValue(const VectorType &x, std::size_t I)
Definition: ublas_space.h:896
static IndexType Size(VectorType const &rV)
return size of vector rV
Definition: ublas_space.h:190
#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