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.
residual_displacement_and_other_dof_criteria.h
Go to the documentation of this file.
1 // KRATOS ___| | | |
2 // \___ \ __| __| | | __| __| | | __| _` | |
3 // | | | | | ( | | | | ( | |
4 // _____/ \__|_| \__,_|\___|\__|\__,_|_| \__,_|_| MECHANICS
5 //
6 // License: BSD License
7 // license: StructuralMechanicsApplication/license.txt
8 //
9 // Main authors: Vicente Mataix Ferrandiz
10 //
11 
12 #pragma once
13 
14 // System includes
15 
16 // External includes
17 
18 // Project includes
19 #include "includes/model_part.h"
20 #include "includes/define.h"
22 
23 namespace Kratos
24 {
25 
28 
32 
36 
40 
44 
52 template<class TSparseSpace,
53  class TDenseSpace
54  >
56  : public ConvergenceCriteria< TSparseSpace, TDenseSpace >
57 {
58 public:
61 
63 
65 
66  typedef TSparseSpace SparseSpaceType;
67 
68  typedef typename BaseType::TDataType TDataType;
69 
71 
73 
75 
76  typedef std::size_t IndexType;
77 
78  typedef std::size_t SizeType;
79 
82 
84 
91  TDataType RatioTolerance,
92  TDataType AbsoluteTolerance,
93  const std::string& OtherDoFName = "ROTATION"
94  )
95  : ConvergenceCriteria< TSparseSpace, TDenseSpace >(),
96  mOtherDoFName(OtherDoFName),
97  mRatioTolerance(RatioTolerance),
98  mAbsoluteTolerance(AbsoluteTolerance)
99  {
100  this->mActualizeRHSIsNeeded = true;
101  }
102 
103  //* Copy constructor.
104 
106  :BaseType(rOther)
107  ,mOtherDoFName(rOther.mOtherDoFName)
108  ,mRatioTolerance(rOther.mRatioTolerance)
109  ,mAbsoluteTolerance(rOther.mAbsoluteTolerance)
110  ,mInitialResidualDispNorm(rOther.mInitialResidualDispNorm)
111  ,mCurrentResidualDispNorm(rOther.mCurrentResidualDispNorm)
112  ,mInitialResidualOtherDoFNorm(rOther.mInitialResidualOtherDoFNorm)
113  ,mCurrentResidualOtherDoFNorm(rOther.mCurrentResidualOtherDoFNorm)
114  {
115  this->mActualizeRHSIsNeeded = true;
116  }
117 
118  //* Destructor.
119 
121 
122 
125 
127 
139  ModelPart& rModelPart,
140  DofsArrayType& rDofSet,
141  const TSystemMatrixType& rA,
142  const TSystemVectorType& rDx,
143  const TSystemVectorType& rb
144  ) override
145  {
146  if (TSparseSpace::Size(rb) != 0) { //if we are solving for something
147  TDataType ratio_displacement = 0.0;
148  TDataType ratio_other_dof = 0.0;
149 
150  SizeType disp_size;
151  CalculateResidualNorm(rModelPart, mCurrentResidualDispNorm, mCurrentResidualOtherDoFNorm, disp_size, rDofSet, rb);
152 
153  if (mInitialResidualDispNorm == 0.0) {
154  ratio_displacement = 0.0;
155  } else {
156  ratio_displacement = mCurrentResidualDispNorm/mInitialResidualDispNorm;
157  }
158 
159  if (mInitialResidualOtherDoFNorm == 0.0) {
160  ratio_other_dof = 0.0;
161  } else {
162  ratio_other_dof = mCurrentResidualOtherDoFNorm/mInitialResidualOtherDoFNorm;
163  }
164 
165  const std::size_t system_size = TSparseSpace::Size(rb);
166  const TDataType absolute_norm_disp = (mCurrentResidualDispNorm/static_cast<TDataType>(disp_size));
167  const TDataType absolute_norm_other_dof = (mCurrentResidualOtherDoFNorm/static_cast<TDataType>(system_size - disp_size));
168 
169  KRATOS_INFO_IF("ResidualDisplacementAndOtherDoFCriteria", this->GetEchoLevel() > 0) << "RESIDUAL DISPLACEMENT CRITERION :: Ratio = "<< ratio_displacement << "; Norm = " << absolute_norm_disp << std::endl;
170  KRATOS_INFO_IF("ResidualDisplacementAndOtherDoFCriteria", this->GetEchoLevel() > 0) << "RESIDUAL " << mOtherDoFName << " CRITERION :: Ratio = "<< ratio_other_dof << "; Norm = " << absolute_norm_other_dof << std::endl;
171 
172  rModelPart.GetProcessInfo()[CONVERGENCE_RATIO] = ratio_displacement;
173  rModelPart.GetProcessInfo()[RESIDUAL_NORM] = absolute_norm_disp;
174 
175  if ((ratio_displacement <= mRatioTolerance || absolute_norm_disp < mAbsoluteTolerance) && (ratio_other_dof <= mRatioTolerance || absolute_norm_other_dof < mAbsoluteTolerance)) {
176  KRATOS_INFO_IF("ResidualDisplacementAndOtherDoFCriteria", this->GetEchoLevel() > 0) << "Convergence is achieved" << std::endl;
177  return true;
178  } else {
179  return false;
180  }
181  } else {
182  return true;
183  }
184  }
185 
192  ModelPart& rModelPart
193  ) override
194  {
196  }
197 
208  ModelPart& rModelPart,
209  DofsArrayType& rDofSet,
210  const TSystemMatrixType& rA,
211  const TSystemVectorType& rDx,
212  const TSystemVectorType& rb
213  ) override
214  {
215  BaseType::InitializeSolutionStep(rModelPart, rDofSet, rA, rDx, rb);
216 
217  // Filling mActiveDofs when MPC exist
218  if (rModelPart.NumberOfMasterSlaveConstraints() > 0) {
219  mActiveDofs.resize(rDofSet.size());
220 
221  #pragma omp parallel for
222  for(int i=0; i<static_cast<int>(mActiveDofs.size()); ++i) {
223  mActiveDofs[i] = true;
224  }
225 
226  #pragma omp parallel for
227  for (int i=0; i<static_cast<int>(rDofSet.size()); ++i) {
228  const auto it_dof = rDofSet.begin() + i;
229  if (it_dof->IsFixed()) {
230  mActiveDofs[it_dof->EquationId()] = false;
231  }
232  }
233 
234  for (const auto& r_mpc : rModelPart.MasterSlaveConstraints()) {
235  for (const auto& r_dof : r_mpc.GetMasterDofsVector()) {
236  mActiveDofs[r_dof->EquationId()] = false;
237  }
238  for (const auto& r_dof : r_mpc.GetSlaveDofsVector()) {
239  mActiveDofs[r_dof->EquationId()] = false;
240  }
241  }
242  }
243 
244  SizeType size_residual;
245  CalculateResidualNorm(rModelPart, mInitialResidualDispNorm, mInitialResidualOtherDoFNorm, size_residual, rDofSet, rb);
246  }
247 
251 
255 
259 
263 
265 
266 protected:
269 
273 
277 
281 
285 
289 
293 
295 
296 private:
299 
303 
304  std::string mOtherDoFName; // The name of the other DoF
305 
306  TDataType mInitialResidualDispNorm; // The initial residual norm for displacements
307  TDataType mCurrentResidualDispNorm; // The current residual norm for displacements
308  TDataType mInitialResidualOtherDoFNorm; // The initial residual norm for displacements
309  TDataType mCurrentResidualOtherDoFNorm; // The current residual norm for displacements
310 
311  TDataType mRatioTolerance; // The tolerance admited in the ratio
312  TDataType mAbsoluteTolerance; // The tolerance admited in the absolutte value
313 
314  std::vector<bool> mActiveDofs;
315 
319 
323 
333  virtual void CalculateResidualNorm(
334  ModelPart& rModelPart,
335  TDataType& rResidualSolutionNormDisp,
336  TDataType& rResidualSolutionNormOtherDof,
337  SizeType& rDofNumDisp,
338  DofsArrayType& rDofSet,
339  const TSystemVectorType& rb
340  )
341  {
342  // Initialize
343  TDataType residual_solution_norm_disp = TDataType();
344  TDataType residual_solution_norm_other_dof = TDataType();
345  SizeType disp_dof_num = 0;
346 
347  // Auxiliary values
348  TDataType residual_dof_value = 0.0;
349  const auto it_dof_begin = rDofSet.begin();
350  const int number_of_dof = static_cast<int>(rDofSet.size());
351 
352  // Loop over Dofs
353  if (rModelPart.NumberOfMasterSlaveConstraints() > 0) {
354  #pragma omp parallel for firstprivate(residual_dof_value) reduction(+:residual_solution_norm_disp, residual_solution_norm_other_dof, disp_dof_num)
355  for (int i = 0; i < number_of_dof; i++) {
356  auto it_dof = it_dof_begin + i;
357 
358  const IndexType dof_id = it_dof->EquationId();
359  residual_dof_value = TSparseSpace::GetValue(rb,dof_id);
360 
361  if (mActiveDofs[dof_id]) {
362  if (it_dof->GetVariable() == DISPLACEMENT_X || it_dof->GetVariable() == DISPLACEMENT_Y || it_dof->GetVariable() == DISPLACEMENT_Z) {
363  residual_solution_norm_disp += std::pow(residual_dof_value, 2);
364  disp_dof_num++;
365  } else {
366  residual_solution_norm_other_dof += std::pow(residual_dof_value, 2);
367  }
368  }
369  }
370  } else {
371  #pragma omp parallel for firstprivate(residual_dof_value) reduction(+:residual_solution_norm_disp, residual_solution_norm_other_dof, disp_dof_num)
372  for (int i = 0; i < number_of_dof; i++) {
373  auto it_dof = it_dof_begin + i;
374 
375  if (!it_dof->IsFixed()) {
376  const IndexType dof_id = it_dof->EquationId();
377  residual_dof_value = TSparseSpace::GetValue(rb,dof_id);
378 
379  if (it_dof->GetVariable() == DISPLACEMENT_X || it_dof->GetVariable() == DISPLACEMENT_Y || it_dof->GetVariable() == DISPLACEMENT_Z) {
380  residual_solution_norm_disp += std::pow(residual_dof_value, 2);
381  disp_dof_num++;
382  } else {
383  residual_solution_norm_other_dof += std::pow(residual_dof_value, 2);
384  }
385  }
386  }
387  }
388 
389  rDofNumDisp = disp_dof_num;
390  rResidualSolutionNormDisp = std::sqrt(residual_solution_norm_disp);
391  rResidualSolutionNormOtherDof = std::sqrt(residual_solution_norm_other_dof);
392  }
393 
397 
401 
405 
407 
408 }; // Class ClassName
409 
411 
414 
415 
417 
418 } // namespace Kratos.
419 
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
bool mActualizeRHSIsNeeded
Definition: convergence_criteria.h:447
TSparseSpace::MatrixType TSystemMatrixType
Matrix type definition.
Definition: convergence_criteria.h:72
TSparseSpace::DataType TDataType
Data type definition.
Definition: convergence_criteria.h:70
TSparseSpace::VectorType TSystemVectorType
Vector type definition.
Definition: convergence_criteria.h:74
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
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
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
This is a convergence criteria that employes the residual as criteria, it divides the problem in two ...
Definition: residual_displacement_and_other_dof_criteria.h:57
BaseType::TSystemMatrixType TSystemMatrixType
Definition: residual_displacement_and_other_dof_criteria.h:72
~ResidualDisplacementAndOtherDoFCriteria() override
Definition: residual_displacement_and_other_dof_criteria.h:120
void Initialize(ModelPart &rModelPart) override
Definition: residual_displacement_and_other_dof_criteria.h:191
ResidualDisplacementAndOtherDoFCriteria(TDataType RatioTolerance, TDataType AbsoluteTolerance, const std::string &OtherDoFName="ROTATION")
Definition: residual_displacement_and_other_dof_criteria.h:90
ResidualDisplacementAndOtherDoFCriteria(ResidualDisplacementAndOtherDoFCriteria const &rOther)
Definition: residual_displacement_and_other_dof_criteria.h:105
std::size_t SizeType
Definition: residual_displacement_and_other_dof_criteria.h:78
void InitializeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
Definition: residual_displacement_and_other_dof_criteria.h:207
BaseType::TDataType TDataType
Definition: residual_displacement_and_other_dof_criteria.h:68
KRATOS_CLASS_POINTER_DEFINITION(ResidualDisplacementAndOtherDoFCriteria)
ConvergenceCriteria< TSparseSpace, TDenseSpace > BaseType
Definition: residual_displacement_and_other_dof_criteria.h:64
BaseType::TSystemVectorType TSystemVectorType
Definition: residual_displacement_and_other_dof_criteria.h:74
TSparseSpace SparseSpaceType
Definition: residual_displacement_and_other_dof_criteria.h:66
BaseType::DofsArrayType DofsArrayType
Definition: residual_displacement_and_other_dof_criteria.h:70
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
Definition: residual_displacement_and_other_dof_criteria.h:138
std::size_t IndexType
Definition: residual_displacement_and_other_dof_criteria.h:76
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
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
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
integer i
Definition: TensorModule.f:17