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.
dofs_criterion.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: May 2018 $
6 // Revision: $Revision: 0.0 $
7 //
8 //
9 
10 #if !defined(KRATOS_DOFS_CRITERION_H_INCLUDED )
11 #define KRATOS_DOFS_CRITERION_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 
17 // Project includes
19 
20 namespace Kratos
21 {
24 
28 
32 
36 
40 
41 
46 template<class TSparseSpace, class TDenseSpace>
47 class DofsCriterion : public ConvergenceCriterion<TSparseSpace,TDenseSpace>
48 {
49  public:
50 
53 
56  typedef typename BaseType::DataType DataType;
60 
67 
70 
74 
76  DofsCriterion(DataType RatioTolerance,
77  DataType AbsoluteTolerance)
78  : BaseType(), mRatioTolerance(RatioTolerance), mAbsoluteTolerance(AbsoluteTolerance)
79  {
80  mpScalarVariable = nullptr;
81  mpVectorVariable = nullptr;
82  this->Set(LocalFlagType::SUPPLIED_DOF, false);
83  }
84 
86  DofsCriterion(const VariableScalarType& rScalarVariable,
87  DataType RatioTolerance,
88  DataType AbsoluteTolerance)
89  : BaseType(), mRatioTolerance(RatioTolerance), mAbsoluteTolerance(AbsoluteTolerance)
90  {
91  mpScalarVariable = &rScalarVariable;
92  mpVectorVariable = nullptr;
93  this->Set(LocalFlagType::SUPPLIED_DOF, true);
94  }
95 
97  DofsCriterion(const VariableVectorType& rVectorVariable,
98  DataType RatioTolerance,
99  DataType AbsoluteTolerance)
100  : BaseType(), mRatioTolerance(RatioTolerance), mAbsoluteTolerance(AbsoluteTolerance)
101  {
102  mpVectorVariable = &rVectorVariable;
103  mpScalarVariable = nullptr;
104  this->Set(LocalFlagType::SUPPLIED_DOF, true);
105  }
106 
109  :BaseType(rOther)
110  ,mRatioTolerance(rOther.mRatioTolerance)
111  ,mAbsoluteTolerance(rOther.mAbsoluteTolerance)
112  ,mpScalarVariable(rOther.mpScalarVariable)
113  ,mpVectorVariable(rOther.mpVectorVariable)
114  {
115  }
116 
118  ~DofsCriterion() override {}
119 
120 
124 
125  //Criterion that needs to be called after getting the solution
126  bool PostCriteria(ModelPart& rModelPart,
127  DofsArrayType& rDofSet,
128  const SystemMatrixType& rA,
129  const SystemVectorType& rDx,
130  const SystemVectorType& rb) override
131  {
132  if (TSparseSpace::Size(rDx) != 0) //if we are solving for something
133  {
134  DataType ratio = 0.00;
135  DataType CorrectionNorm = DataType();
136  DataType ReferenceNorm = DataType();
137 
138  std::size_t size = 0;
139 
140  if( this->Is(LocalFlagType::INCREMENTAL) ){
141  size = CalculateIncrementalNorm(rDofSet,rDx, ReferenceNorm, CorrectionNorm);
142  }
143  else{
144  size = CalculateReferenceNorm(rDofSet,rDx, ReferenceNorm, CorrectionNorm);
145  }
146 
147  if( size == 0 ){
148  KRATOS_WARNING("") << GetDofName() <<" Dofs vector has size: " << size << std::endl;
149  return true;
150  }
151 
152  if(CorrectionNorm != 0)
153  {
154  ratio = CorrectionNorm/ReferenceNorm;
155  }
156 
157  if( ratio == 0 && int(CorrectionNorm-int(ReferenceNorm))==0 )
158  {
159  ratio = 1.0;
160  }
161 
162  const DataType absolute_norm = (CorrectionNorm/static_cast<DataType>(size));
163 
164  if (rModelPart.GetCommunicator().MyPID() == 0)
165  {
166  if(this->GetEchoLevel() >= 1)
167  {
168  std::cout << "DOF (" << GetDofName() << ") ["<<rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER]<<"] :: Ratio = " << ratio << "; Norm = " << absolute_norm << std::endl;
169  std::cout << " CorrectionNorm = " << CorrectionNorm << "; ReferenceNorm = " << ReferenceNorm << std::endl;
170  }
171  }
172 
173  rModelPart.GetProcessInfo()[CONVERGENCE_RATIO] = ratio;
174  rModelPart.GetProcessInfo()[RESIDUAL_NORM] = absolute_norm;
175 
176  if ( ratio <= mRatioTolerance || absolute_norm < mAbsoluteTolerance )
177  {
178  if (rModelPart.GetCommunicator().MyPID() == 0 )
179  {
180  if( this->GetEchoLevel() >= 1 )
181  {
182  std::cout << "Convergence is achieved" << std::endl;
183  }
184  }
185  return true;
186  }
187  else
188  {
189  if( this->Is(LocalFlagType::INCREMENTAL) && ratio == 1.0 && ReferenceNorm <= mRatioTolerance * 1e-2)
190  {
191  if (rModelPart.GetCommunicator().MyPID() == 0)
192  {
193  if (this->GetEchoLevel() >= 1)
194  {
195  std::cout << "Convergence is achieved : - no movement - " << std::endl;
196  }
197  }
198  return true;
199  }
200  else
201  {
202  return false;
203  }
204  }
205  }
206  else //in this case all the dofs are imposed!
207  {
208  return true;
209  }
210  }
211 
212 
216 
220 
224 
228 
230 
231  protected:
234 
238 
242 
246 
250 
254 
258 
260 
261  private:
264 
268 
269  DataType mRatioTolerance;
270 
271  DataType mAbsoluteTolerance;
272 
273  VariableScalarPointer mpScalarVariable;
274 
275  VariableVectorPointer mpVectorVariable;
276 
280 
284 
285  std::size_t CalculateReferenceNorm(DofsArrayType& rDofSet, const SystemVectorType& rDx, DataType& rReferenceNorm, DataType& rCorrectionNorm)
286  {
287  KRATOS_TRY
288 
289  std::size_t size = 0;
290  rCorrectionNorm = 0.0;
291  rReferenceNorm = 0.0;
292 
293  if( this->Is(CriterionLocalFlags::SUPPLIED_DOF) ){
294 
295  if( mpVectorVariable != nullptr ){
296 
297  DataType temp;
298  for(typename DofsArrayType::iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
299  {
300  if(i_dof->IsFree())
301  {
302  if(CheckVectorDof(i_dof))
303  {
304  temp = rDx[i_dof->EquationId()];
305  rCorrectionNorm += temp*temp;
306  temp = i_dof->GetSolutionStepValue();
307  rReferenceNorm += temp*temp;
308  ++size;
309  }
310  }
311  }
312  }
313  else if( mpScalarVariable != nullptr ){
314 
315  DataType temp;
316 
317  for(typename DofsArrayType::iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
318  {
319  if(i_dof->IsFree())
320  {
321  if(i_dof->GetVariable() == *mpScalarVariable)
322  {
323  temp = rDx[i_dof->EquationId()];
324  rCorrectionNorm += temp*temp;
325  temp = i_dof->GetSolutionStepValue();
326  rReferenceNorm += temp*temp;
327  ++size;
328  }
329  }
330  }
331 
332  }
333  else{
334  KRATOS_ERROR << " No variable dof supplied to the convergence criterion " << std::endl;
335  }
336 
337  rCorrectionNorm = std::sqrt(rCorrectionNorm);
338  }
339  else{
340 
341  rCorrectionNorm = TSparseSpace::TwoNorm(rDx);
342  size = TSparseSpace::Size(rDx);
343 
344  DataType temp;
345 
346  for(typename DofsArrayType::iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
347  {
348  if(i_dof->IsFree())
349  {
350  temp = i_dof->GetSolutionStepValue();
351  rReferenceNorm += temp*temp;
352  }
353  }
354 
355  }
356 
357  rReferenceNorm = std::sqrt(rReferenceNorm+1e-20); //to avoid 0
358 
359  return size;
360 
361  KRATOS_CATCH("")
362  }
363 
364 
365  std::size_t CalculateIncrementalNorm(DofsArrayType& rDofSet, const SystemVectorType& rDx, DataType& rReferenceNorm, DataType& rCorrectionNorm)
366  {
367  KRATOS_TRY
368 
369  std::size_t size = 0;
370  rCorrectionNorm = 0.0;
371  rReferenceNorm = 0.0;
372 
373  if( this->Is(CriterionLocalFlags::SUPPLIED_DOF) ){
374 
375  if( mpVectorVariable != nullptr ){
376 
377  DataType temp;
378 
379  for(typename DofsArrayType::iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
380  {
381  if(i_dof->IsFree())
382  {
383  if(CheckVectorDof(i_dof))
384  {
385  temp = rDx[i_dof->EquationId()];
386  rCorrectionNorm += temp*temp;
387  temp = (i_dof->GetSolutionStepValue()-i_dof->GetSolutionStepValue(1));
388  rReferenceNorm += temp*temp;
389  ++size;
390  }
391  }
392  }
393  }
394  else if( mpScalarVariable != nullptr ){
395 
396  DataType temp;
397 
398  for(typename DofsArrayType::iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
399  {
400  if(i_dof->IsFree())
401  {
402  if(i_dof->GetVariable() == *mpScalarVariable)
403  {
404  temp = rDx[i_dof->EquationId()];
405  rCorrectionNorm += temp*temp;
406  temp = (i_dof->GetSolutionStepValue()-i_dof->GetSolutionStepValue(1));
407  rReferenceNorm += temp*temp;
408  ++size;
409  }
410  }
411  }
412 
413  }
414  else{
415  KRATOS_ERROR << " No variable dof supplied to the convergence criterion " << std::endl;
416  }
417  }
418  else{
419 
420  DataType temp;
421 
422  for(typename DofsArrayType::iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
423  {
424  if(i_dof->IsFree())
425  {
426  temp = rDx[i_dof->EquationId()];
427  rCorrectionNorm += temp*temp;
428  temp = (i_dof->GetSolutionStepValue()-i_dof->GetSolutionStepValue(1));
429  rReferenceNorm += temp*temp;
430  ++size;
431  }
432  }
433 
434  }
435 
436  rCorrectionNorm = std::sqrt(rCorrectionNorm);
437  rReferenceNorm = std::sqrt(rReferenceNorm+1e-20); //to avoid 0
438 
439  return size;
440 
441  KRATOS_CATCH("")
442  }
443 
444  bool CheckVectorDof(typename DofsArrayType::iterator& rDofIter)
445  {
446  KRATOS_TRY
447 
448  const std::string& variable_name = mpVectorVariable->Name();
449  const Variable<double>& var_x = KratosComponents<Variable<double> >::Get(variable_name+"_X");
450  const Variable<double>& var_y = KratosComponents<Variable<double> >::Get(variable_name+"_Y");
451  const Variable<double>& var_z = KratosComponents<Variable<double> >::Get(variable_name+"_Z");
452 
453  if( rDofIter->GetVariable() == var_x || rDofIter->GetVariable() == var_y || rDofIter->GetVariable() == var_z )
454  return true;
455  else
456  return false;
457 
458  KRATOS_CATCH("")
459  }
460 
461  std::string GetDofName()
462  {
463  std::string name = "DOFS";
464 
465  if( this->Is(CriterionLocalFlags::SUPPLIED_DOF) ){
466 
467  if( mpVectorVariable != nullptr ){
468  name = mpVectorVariable->Name();
469  }
470  else if( mpScalarVariable != nullptr ){
471  name = mpScalarVariable->Name();
472  }
473  }
474 
475  return name;
476  }
477 
481 
485 
489 
491 
492 }; // Class DofsCriterion
493 
495 
498 
499 
503 
505 
507 
508 } // namespace Kratos.
509 
510 #endif // KRATOS_DOFS_CRITERION_H_INCLUDED defined
virtual int MyPID() const
Definition: communicator.cpp:91
Convergence Criterion base class.
Definition: convergence_criterion.hpp:52
TSparseSpace::DataType DataType
Definition: convergence_criterion.hpp:58
int GetEchoLevel()
Definition: convergence_criterion.hpp:109
TSparseSpace::MatrixType SystemMatrixType
Definition: convergence_criterion.hpp:60
TSparseSpace::VectorType SystemVectorType
Definition: convergence_criterion.hpp:61
Solver local flags class definition.
Definition: solution_local_flags.hpp:74
This convergence criteria checks the variable dofs.
Definition: dofs_criterion.hpp:48
BaseType::SystemMatrixType SystemMatrixType
Definition: dofs_criterion.hpp:58
array_1d< double, 3 > VectorType
Definition: dofs_criterion.hpp:61
Variable< VectorType > VariableVectorType
Definition: dofs_criterion.hpp:63
Variable< double > VectorComponentType
Definition: dofs_criterion.hpp:62
const VariableVectorType * VariableVectorPointer
Definition: dofs_criterion.hpp:65
DofsCriterion(const VariableScalarType &rScalarVariable, DataType RatioTolerance, DataType AbsoluteTolerance)
Constructor.
Definition: dofs_criterion.hpp:86
~DofsCriterion() override
Destructor.
Definition: dofs_criterion.hpp:118
BaseType::SystemVectorType SystemVectorType
Definition: dofs_criterion.hpp:59
BaseType::DataType DataType
Definition: dofs_criterion.hpp:56
KRATOS_CLASS_POINTER_DEFINITION(DofsCriterion)
Pointer definition of DofsCriterion.
BaseType::DofsArrayType DofsArrayType
Definition: dofs_criterion.hpp:57
Variable< double > VariableScalarType
Definition: dofs_criterion.hpp:64
DofsCriterion(DofsCriterion const &rOther)
Copy constructor.
Definition: dofs_criterion.hpp:108
BaseType::LocalFlagType LocalFlagType
Definition: dofs_criterion.hpp:55
DofsCriterion(DataType RatioTolerance, DataType AbsoluteTolerance)
Constructor.
Definition: dofs_criterion.hpp:76
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const SystemMatrixType &rA, const SystemVectorType &rDx, const SystemVectorType &rb) override
Definition: dofs_criterion.hpp:126
DofsCriterion(const VariableVectorType &rVectorVariable, DataType RatioTolerance, DataType AbsoluteTolerance)
Constructor.
Definition: dofs_criterion.hpp:97
const VariableScalarType * VariableScalarPointer
Definition: dofs_criterion.hpp:66
ConvergenceCriterion< TSparseSpace, TDenseSpace > BaseType
Definition: dofs_criterion.hpp:54
void Set(const Flags ThisFlag)
Definition: flags.cpp:33
bool Is(Flags const &rOther) const
Definition: flags.h:274
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
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
boost::indirect_iterator< typename TContainerType::iterator > iterator
Definition: pointer_vector_set.h:95
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_WARNING(label)
Definition: logger.h:265
double TwoNorm(SparseSpaceType &dummy, SparseSpaceType::VectorType &x)
Definition: add_strategies_to_python.cpp:164
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
float temp
Definition: rotating_cone.py:85
e
Definition: run_cpp_mpi_tests.py:31