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.
femdem_residual_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: Alejandro Cornejo
11 //
12 
13 #if !defined(KRATOS_FEMDEM_RESIDUAL_CRITERIA )
14 #define KRATOS_FEMDEM_RESIDUAL_CRITERIA
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "includes/model_part.h"
22 #include "includes/define.h"
24 
25 namespace Kratos
26 {
29 
33 
37 
41 
45 
53 template<class TSparseSpace,
54  class TDenseSpace
55  >
57  : public ConvergenceCriteria< TSparseSpace, TDenseSpace >
58 {
59 public:
62 
64 
67 
69  typedef typename BaseType::TDataType TDataType;
70 
73 
76 
79 
81  typedef std::size_t IndexType;
82 
84  typedef std::size_t SizeType;
85 
89 
90  //* Constructor.
92  : BaseType()
93  {
94  if (Settings.Has("residual_absolute_tolerance")) {
95  mAlwaysConvergedNorm = Settings["residual_absolute_tolerance"].GetDouble();
96  } else if (Settings.Has("absolute_tolerance")) {
97  mAlwaysConvergedNorm = Settings["absolute_tolerance"].GetDouble();
98  } else {
99  KRATOS_WARNING("FemDemResidualCriteria") << "residual_absolute_tolerance or absolute_tolerance nor defined on settings. Using default 1.0e-9" << std::endl;
100  mAlwaysConvergedNorm = 1.0e-9;
101  }
102  if (Settings.Has("residual_relative_tolerance")) {
103  mRatioTolerance = Settings["residual_relative_tolerance"].GetDouble();
104  } else if (Settings.Has("relative_tolerance")) {
105  mRatioTolerance = Settings["relative_tolerance"].GetDouble();
106  } else {
107  KRATOS_WARNING("FemDemResidualCriteria") << "residual_relative_tolerance or relative_tolerance nor defined on settings. Using default 1.0e-4" << std::endl;
108  mRatioTolerance = 1.0e-4;
109  }
110  if (Settings.Has("max_iteration")) {
111  mMaxIterations = Settings["max_iteration"].GetInt();
112  }
113 
114  this->mActualizeRHSIsNeeded = true;
115  }
116 
117  //* Constructor.
119  TDataType NewRatioTolerance,
120  TDataType AlwaysConvergedNorm)
121  : BaseType(),
122  mRatioTolerance(NewRatioTolerance),
123  mAlwaysConvergedNorm(AlwaysConvergedNorm)
124  {
125  this->mActualizeRHSIsNeeded = true;
126  }
127 
128  //* Copy constructor.
130  :BaseType(rOther)
131  ,mRatioTolerance(rOther.mRatioTolerance)
132  ,mInitialResidualNorm(rOther.mInitialResidualNorm)
133  ,mCurrentResidualNorm(rOther.mCurrentResidualNorm)
134  ,mAlwaysConvergedNorm(rOther.mAlwaysConvergedNorm)
135  ,mReferenceDispNorm(rOther.mReferenceDispNorm)
136  {
137  this->mActualizeRHSIsNeeded = true;
138  }
139 
140  //* Destructor.
142 
146 
158  ModelPart& rModelPart,
159  DofsArrayType& rDofSet,
160  const TSystemMatrixType& rA,
161  const TSystemVectorType& rDx,
162  const TSystemVectorType& rb
163  ) override
164  {
165  if (rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER] == 1) {
166  KRATOS_INFO("") << "___________________________________________________________________" << std::endl;
167  KRATOS_INFO("") << "| ITER | RATIO | ABS_NORM | CONVERGED |" << std::endl;
168  }
169  const SizeType size_b = TSparseSpace::Size(rb);
170  if (size_b != 0) { //if we are solving for something
171 
172  SizeType size_residual;
173  CalculateResidualNorm(rModelPart, mCurrentResidualNorm, size_residual, rDofSet, rb);
174 
175  TDataType ratio = 0.0;
176  if(mInitialResidualNorm < std::numeric_limits<TDataType>::epsilon()) {
177  ratio = 0.0;
178  } else {
179  ratio = mCurrentResidualNorm/mInitialResidualNorm;
180  }
181 
182  const TDataType float_size_residual = static_cast<TDataType>(size_residual);
183  const TDataType absolute_norm = (mCurrentResidualNorm / float_size_residual);
184 
185  rModelPart.GetProcessInfo()[CONVERGENCE_RATIO] = ratio;
186  rModelPart.GetProcessInfo()[RESIDUAL_NORM] = absolute_norm;
187 
188  if (ratio <= mRatioTolerance || absolute_norm < mAlwaysConvergedNorm) { // Converged
189  if (rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER] < 10) {
190  std::cout <<"| " << rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER] << " | "
191  << std::scientific << ratio << " | "
192  << absolute_norm << " |" << " TRUE |"<< std::endl;
193  } else {
194  std::cout <<"| " << rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER] << " | "
195  << std::scientific << ratio << " | "
196  << absolute_norm << " |" << " TRUE |"<< std::endl;
197  }
198  return true;
199  } else {
200  if (rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER] < 10) {
201  std::cout <<"| " << rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER] << " | "
202  << std::scientific << ratio << " | "
203  << absolute_norm << " |" << " FALSE |"<< std::endl;
204  } else {
205  std::cout <<"| " << rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER] << " | "
206  << std::scientific << ratio << " | "
207  << absolute_norm << " |" << " FALSE |"<< std::endl;
208  }
209  if (rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER] == mMaxIterations) {
210  KRATOS_INFO("") << " ATTENTION! SOLUTION STEP NOT CONVERGED AFTER " << mMaxIterations << "ITERATIONS" << std::endl;
211  }
212  return false;
213  }
214  } else {
215  return true;
216  }
217  }
218 
223  void Initialize(ModelPart& rModelPart) override
224  {
225  BaseType::Initialize(rModelPart);
226  }
227 
237  ModelPart& rModelPart,
238  DofsArrayType& rDofSet,
239  const TSystemMatrixType& rA,
240  const TSystemVectorType& rDx,
241  const TSystemVectorType& rb
242  ) override
243  {
244  BaseType::InitializeSolutionStep(rModelPart, rDofSet, rA, rDx, rb);
245 
246  // Filling mActiveDofs when MPC exist
247  if (rModelPart.NumberOfMasterSlaveConstraints() > 0) {
248  mActiveDofs.resize(rDofSet.size());
249 
250  #pragma omp parallel for
251  for(int i=0; i<static_cast<int>(mActiveDofs.size()); ++i) {
252  mActiveDofs[i] = true;
253  }
254 
255  #pragma omp parallel for
256  for (int i = 0; i<static_cast<int>(rDofSet.size()); ++i) {
257  const auto it_dof = rDofSet.begin() + i;
258  if (it_dof->IsFixed()) {
259  mActiveDofs[it_dof->EquationId()] = false;
260  }
261  }
262 
263  for (const auto& r_mpc : rModelPart.MasterSlaveConstraints()) {
264  for (const auto& r_dof : r_mpc.GetMasterDofsVector()) {
265  mActiveDofs[r_dof->EquationId()] = false;
266  }
267  for (const auto& r_dof : r_mpc.GetSlaveDofsVector()) {
268  mActiveDofs[r_dof->EquationId()] = false;
269  }
270  }
271  }
272 
273  SizeType size_residual;
274  CalculateResidualNorm(rModelPart, mInitialResidualNorm, size_residual, rDofSet, rb);
275  }
276 
286  ModelPart& rModelPart,
287  DofsArrayType& rDofSet,
288  const TSystemMatrixType& rA,
289  const TSystemVectorType& rDx,
290  const TSystemVectorType& rb
291  ) override
292  {
293  BaseType::FinalizeSolutionStep(rModelPart, rDofSet, rA, rDx, rb);
294  KRATOS_INFO("") << "|_____________|________________|________________|_________________|" << std::endl;
295  KRATOS_INFO("") << "" << std::endl;
296  }
297 
301 
305 
309 
313 
315  std::string Info() const override
316  {
317  return "FemDemResidualCriteria";
318  }
319 
321  void PrintInfo(std::ostream& rOStream) const override
322  {
323  rOStream << Info();
324  }
325 
327  void PrintData(std::ostream& rOStream) const override
328  {
329  rOStream << Info();
330  }
331 
335 
337 
338 protected:
341 
345 
349 
353 
363  virtual void CalculateResidualNorm(
364  ModelPart& rModelPart,
365  TDataType& rResidualSolutionNorm,
366  SizeType& rDofNum,
367  DofsArrayType& rDofSet,
368  const TSystemVectorType& rb
369  )
370  {
371  // Initialize
372  TDataType residual_solution_norm = TDataType();
373  SizeType dof_num = 0;
374 
375  // Auxiliar values
376  TDataType residual_dof_value = 0.0;
377  const auto it_dof_begin = rDofSet.begin();
378  const int number_of_dof = static_cast<int>(rDofSet.size());
379 
380  // Loop over Dofs
381  if (rModelPart.NumberOfMasterSlaveConstraints() > 0) {
382  #pragma omp parallel for firstprivate(residual_dof_value) reduction(+:residual_solution_norm, dof_num)
383  for (int i = 0; i < number_of_dof; i++) {
384  auto it_dof = it_dof_begin + i;
385 
386  const IndexType dof_id = it_dof->EquationId();
387 
388  if (mActiveDofs[dof_id]) {
389  residual_dof_value = TSparseSpace::GetValue(rb,dof_id);
390  residual_solution_norm += std::pow(residual_dof_value, 2);
391  dof_num++;
392  }
393  }
394  } else {
395  #pragma omp parallel for firstprivate(residual_dof_value) reduction(+:residual_solution_norm, dof_num)
396  for (int i = 0; i < number_of_dof; i++) {
397  auto it_dof = it_dof_begin + i;
398 
399  if (!it_dof->IsFixed()) {
400  const IndexType dof_id = it_dof->EquationId();
401  residual_dof_value = TSparseSpace::GetValue(rb,dof_id);
402  residual_solution_norm += std::pow(residual_dof_value, 2);
403  dof_num++;
404  }
405  }
406  }
407 
408  rDofNum = dof_num;
409  rResidualSolutionNorm = std::sqrt(residual_solution_norm);
410  }
411 
415 
419 
423 
425 
426 private:
429 
433 
434  TDataType mRatioTolerance;
435 
436  TDataType mInitialResidualNorm;
437 
438  TDataType mCurrentResidualNorm;
439 
440  TDataType mAlwaysConvergedNorm;
441 
442  TDataType mReferenceDispNorm;
443 
444  std::vector<bool> mActiveDofs;
445 
446  int mMaxIterations;
447 
451 
455 
459 
463 
467 
469 
470 }; // Class ClassName
471 
473 
476 
477 
479 
480 } // namespace Kratos.
481 
482 #endif // KRATOS_NEW_DISPLACEMENT_CRITERIA defined
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
bool mActualizeRHSIsNeeded
Definition: convergence_criteria.h:447
TSparseSpace::MatrixType TSystemMatrixType
Matrix type definition.
Definition: convergence_criteria.h:72
virtual void Initialize(ModelPart &rModelPart)
This function initialize the convergence criteria.
Definition: convergence_criteria.h:276
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
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
This is a convergence criteria that employes the residual as criteria.
Definition: femdem_residual_criteria.h:58
virtual void CalculateResidualNorm(ModelPart &rModelPart, TDataType &rResidualSolutionNorm, SizeType &rDofNum, DofsArrayType &rDofSet, const TSystemVectorType &rb)
This method computes the norm of the residual.
Definition: femdem_residual_criteria.h:363
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: femdem_residual_criteria.h:327
KRATOS_CLASS_POINTER_DEFINITION(FemDemResidualCriteria)
std::string Info() const override
Turn back information as a string.
Definition: femdem_residual_criteria.h:315
FemDemResidualCriteria(FemDemResidualCriteria const &rOther)
Definition: femdem_residual_criteria.h:129
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: femdem_residual_criteria.h:321
std::size_t SizeType
Definition of the size type.
Definition: femdem_residual_criteria.h:84
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
Criterias that need to be called after getting the solution.
Definition: femdem_residual_criteria.h:157
ConvergenceCriteria< TSparseSpace, TDenseSpace > BaseType
The definition of the base ConvergenceCriteria.
Definition: femdem_residual_criteria.h:66
BaseType::TSystemVectorType TSystemVectorType
The dense vector type.
Definition: femdem_residual_criteria.h:78
FemDemResidualCriteria(TDataType NewRatioTolerance, TDataType AlwaysConvergedNorm)
Definition: femdem_residual_criteria.h:118
void Initialize(ModelPart &rModelPart) override
This function initialize the convergence criteria.
Definition: femdem_residual_criteria.h:223
~FemDemResidualCriteria() override
Definition: femdem_residual_criteria.h:141
std::size_t IndexType
Definition of the IndexType.
Definition: femdem_residual_criteria.h:81
BaseType::DofsArrayType DofsArrayType
The dofs array type.
Definition: femdem_residual_criteria.h:72
BaseType::TDataType TDataType
The data type.
Definition: femdem_residual_criteria.h:69
void InitializeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
This function initializes the solution step.
Definition: femdem_residual_criteria.h:236
void FinalizeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
This function finalizes the solution step.
Definition: femdem_residual_criteria.h:285
BaseType::TSystemMatrixType TSystemMatrixType
The sparse matrix type.
Definition: femdem_residual_criteria.h:75
FemDemResidualCriteria(Kratos::Parameters Settings)
Definition: femdem_residual_criteria.h:91
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MasterSlaveConstraintContainerType & MasterSlaveConstraints(IndexType ThisIndex=0)
Definition: model_part.h:654
SizeType NumberOfMasterSlaveConstraints(IndexType ThisIndex=0) const
Definition: model_part.h:649
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
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
bool Has(const std::string &rEntry) const
This method checks if the Parameter contains a certain entry.
Definition: kratos_parameters.cpp:520
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
size_type size() const
Returns the number of elements in the container.
Definition: pointer_vector_set.h:502
iterator begin()
Returns an iterator pointing to the beginning of the container.
Definition: pointer_vector_set.h:278
#define KRATOS_INFO(label)
Definition: logger.h:250
#define KRATOS_WARNING(label)
Definition: logger.h:265
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
Parameters GetValue(Parameters &rParameters, const std::string &rEntry)
Definition: add_kratos_parameters_to_python.cpp:53
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
integer i
Definition: TensorModule.f:17