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_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_RESIDUAL_CRITERION_H_INCLUDED )
11 #define KRATOS_RESIDUAL_CRITERION_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 
17 // Project includes
19 
20 namespace Kratos
21 {
22 
25 
28 
32 
36 
40 
44 
49 template<class TSparseSpace, class TDenseSpace>
50 class ResidualCriterion : public ConvergenceCriterion< TSparseSpace, TDenseSpace >
51 {
52  public:
53 
56 
59  typedef typename BaseType::DataType DataType;
63 
70 
71  typedef std::vector<Variable<double> > ComponentVariableVector;
72 
75 
79 
81  ResidualCriterion(DataType RatioTolerance,
82  DataType AbsoluteTolerance)
83  : BaseType(), mRatioTolerance(RatioTolerance), mAbsoluteTolerance(AbsoluteTolerance)
84  {
85  mpScalarVariable = nullptr;
86  mpVectorVariable = nullptr;
87  this->Set(LocalFlagType::SUPPLIED_DOF, false);
88  }
89 
91  ResidualCriterion(const VariableScalarType& rScalarVariable,
92  DataType RatioTolerance,
93  DataType AbsoluteTolerance)
94  : BaseType(), mRatioTolerance(RatioTolerance), mAbsoluteTolerance(AbsoluteTolerance)
95  {
96  mpScalarVariable = &rScalarVariable;
97  mpVectorVariable = nullptr;
98  this->Set(LocalFlagType::SUPPLIED_DOF, true);
99  }
100 
102  ResidualCriterion(const VariableVectorType& rVectorVariable,
103  DataType RatioTolerance,
104  DataType AbsoluteTolerance)
105  :BaseType(), mRatioTolerance(RatioTolerance), mAbsoluteTolerance(AbsoluteTolerance)
106  {
107  mpVectorVariable = &rVectorVariable;
108  mpScalarVariable = nullptr;
109  this->Set(LocalFlagType::SUPPLIED_DOF, true);
110  }
111 
114  :BaseType(rOther)
115  ,mRatioTolerance(rOther.mRatioTolerance)
116  ,mAbsoluteTolerance(rOther.mAbsoluteTolerance)
117  ,mInitialResidualNorm(rOther.mInitialResidualNorm)
118  ,mpScalarVariable(rOther.mpScalarVariable)
119  ,mpVectorVariable(rOther.mpVectorVariable)
120  {
121  }
122 
124  ~ResidualCriterion() override {}
125 
126 
130 
131  //Criterion that needs to be called after getting the solution
132  bool PostCriteria(ModelPart& rModelPart,
133  DofsArrayType& rDofSet,
134  const SystemMatrixType& rA,
135  const SystemVectorType& rDx,
136  const SystemVectorType& rb
137  ) override
138  {
139  if (TSparseSpace::Size(rb) != 0) //if we are solving for something
140  {
141 
142  std::size_t size = 0;
143 
144  if( this->IsNot(LocalFlagType::INITIALIZED) )
145  {
146  size = CalculateResidualNorm(rDofSet,rb,mInitialResidualNorm);
147  this->Set(LocalFlagType::INITIALIZED,true);
148  }
149 
150  DataType ratio;
151  DataType CurrentResidualNorm;
152  size = CalculateResidualNorm(rDofSet,rb,CurrentResidualNorm);
153 
154  if( size == 0 ){
155  KRATOS_WARNING("") << GetDofName() << " Residual Dofs vector has size: " << size << std::endl;
156  return true;
157  }
158 
159  if(mInitialResidualNorm == 0.00)
160  {
161  ratio = 0.00;
162  }
163  else
164  {
165  ratio = CurrentResidualNorm/mInitialResidualNorm;
166  }
167 
168  const DataType absolute_norm = (CurrentResidualNorm/static_cast<DataType>(size));
169 
170  if (rModelPart.GetCommunicator().MyPID() == 0)
171  {
172  if (this->GetEchoLevel() >= 1)
173  {
174  std::cout << "RESIDUAL (" << GetDofName() << ") ["<<rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER]<<"] :: Ratio = "<< ratio << "; Norm = " << absolute_norm <<std::endl;
175  }
176  }
177 
178  rModelPart.GetProcessInfo()[CONVERGENCE_RATIO] = ratio;
179  rModelPart.GetProcessInfo()[RESIDUAL_NORM] = absolute_norm;
180 
181  if (ratio <= mRatioTolerance || absolute_norm < mAbsoluteTolerance)
182  {
183  if (rModelPart.GetCommunicator().MyPID() == 0)
184  {
185  if (this->GetEchoLevel() >= 1)
186  {
187  std::cout << "Convergence is achieved" << std::endl;
188  }
189  }
190  return true;
191  }
192  else
193  {
194  return false;
195  }
196  }
197  else
198  {
199  return true;
200  }
201  }
202 
203 
205  DofsArrayType& rDofSet,
206  const SystemMatrixType& rA,
207  const SystemVectorType& rDx,
208  const SystemVectorType& rb) override
209  {
210  this->Set(LocalFlagType::INITIALIZED,false);
211  }
212 
213 
217 
221 
225 
229 
231 
232  protected:
233 
236 
240 
244 
248 
252 
256 
260 
262 
263  private:
266 
270 
271  DataType mRatioTolerance;
272 
273  DataType mAbsoluteTolerance;
274 
275  DataType mInitialResidualNorm;
276 
277  VariableScalarPointer mpScalarVariable;
278 
279  VariableVectorPointer mpVectorVariable;
280 
284 
288 
289  std::size_t CalculateResidualNorm(DofsArrayType& rDofSet, const SystemVectorType& rb, DataType& rResidualNorm)
290  {
291  KRATOS_TRY
292 
293  std::size_t size = 0;
294  rResidualNorm = 0.0;
295 
296  if( this->Is(CriterionLocalFlags::SUPPLIED_DOF) ){
297 
298  if( mpVectorVariable != nullptr ){
299 
300  DataType temp;
301  for(typename DofsArrayType::iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
302  {
303  if(i_dof->IsFree())
304  {
305  if(CheckVectorDof(i_dof))
306  {
307  temp = rb[i_dof->EquationId()];
308  rResidualNorm += temp*temp;
309  ++size;
310  }
311  }
312  }
313  }
314  else if( mpScalarVariable != nullptr ){
315 
316  DataType temp;
317 
318  for(typename DofsArrayType::iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
319  {
320  if(i_dof->IsFree())
321  {
322  if(i_dof->GetVariable() == *mpScalarVariable)
323  {
324  temp = rb[i_dof->EquationId()];
325  rResidualNorm += temp*temp;
326  ++size;
327  }
328  }
329  }
330 
331  }
332  else{
333  KRATOS_ERROR << " No variable dof supplied to the convergence criterion " << std::endl;
334  }
335 
336  rResidualNorm = std::sqrt(rResidualNorm);
337  }
338  else{
339  rResidualNorm = TSparseSpace::TwoNorm(rb);
340  size = TSparseSpace::Size(rb);
341  }
342 
343  return size;
344 
345  KRATOS_CATCH("")
346  }
347 
348  bool CheckVectorDof(typename DofsArrayType::iterator& rDofIter)
349  {
350  KRATOS_TRY
351 
352  const std::string& variable_name = mpVectorVariable->Name();
353  const Variable<double>& var_x = KratosComponents<Variable<double> >::Get(variable_name+"_X");
354  const Variable<double>& var_y = KratosComponents<Variable<double> >::Get(variable_name+"_Y");
355  const Variable<double>& var_z = KratosComponents<Variable<double> >::Get(variable_name+"_Z");
356 
357  if( rDofIter->GetVariable() == var_x || rDofIter->GetVariable() == var_y || rDofIter->GetVariable() == var_z )
358  return true;
359  else
360  return false;
361 
362  KRATOS_CATCH("")
363  }
364 
365  std::string GetDofName()
366  {
367  std::string name = "DOFS";
368 
369  if( this->Is(CriterionLocalFlags::SUPPLIED_DOF) ){
370 
371  if( mpVectorVariable != nullptr ){
372  name = mpVectorVariable->Name();
373  }
374  else if( mpScalarVariable != nullptr ){
375  name = mpScalarVariable->Name();
376  }
377  }
378 
379  return name;
380  }
381 
385 
389 
393 
395 
396 }; // Class ResidualCriterion
397 
399 
402 
403 
407 
409 
411 
412 } // namespace Kratos.
413 
414 #endif // KRATOS_RESIDUAL_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
void Set(const Flags ThisFlag)
Definition: flags.cpp:33
bool Is(Flags const &rOther) const
Definition: flags.h:274
bool IsNot(Flags const &rOther) const
Definition: flags.h:291
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
This convergence criteria checks the residual.
Definition: residual_criterion.hpp:51
BaseType::SystemMatrixType SystemMatrixType
Definition: residual_criterion.hpp:61
BaseType::DofsArrayType DofsArrayType
Definition: residual_criterion.hpp:60
BaseType::SystemVectorType SystemVectorType
Definition: residual_criterion.hpp:62
Variable< double > VectorComponentType
Definition: residual_criterion.hpp:65
BaseType::DataType DataType
Definition: residual_criterion.hpp:59
ResidualCriterion(ResidualCriterion const &rOther)
Copy constructor.
Definition: residual_criterion.hpp:113
BaseType::LocalFlagType LocalFlagType
Definition: residual_criterion.hpp:58
const VariableVectorType * VariableVectorPointer
Definition: residual_criterion.hpp:68
ResidualCriterion(const VariableVectorType &rVectorVariable, DataType RatioTolerance, DataType AbsoluteTolerance)
Constructor.
Definition: residual_criterion.hpp:102
ResidualCriterion(const VariableScalarType &rScalarVariable, DataType RatioTolerance, DataType AbsoluteTolerance)
Constructor.
Definition: residual_criterion.hpp:91
Variable< double > VariableScalarType
Definition: residual_criterion.hpp:67
ConvergenceCriterion< TSparseSpace, TDenseSpace > BaseType
Definition: residual_criterion.hpp:57
KRATOS_CLASS_POINTER_DEFINITION(ResidualCriterion)
Pointer definition of ResidualCriterion.
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const SystemMatrixType &rA, const SystemVectorType &rDx, const SystemVectorType &rb) override
Definition: residual_criterion.hpp:132
ResidualCriterion(DataType RatioTolerance, DataType AbsoluteTolerance)
Constructor.
Definition: residual_criterion.hpp:81
const VariableScalarType * VariableScalarPointer
Definition: residual_criterion.hpp:69
void InitializeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const SystemMatrixType &rA, const SystemVectorType &rDx, const SystemVectorType &rb) override
Definition: residual_criterion.hpp:204
std::vector< Variable< double > > ComponentVariableVector
Definition: residual_criterion.hpp:71
Variable< VectorType > VariableVectorType
Definition: residual_criterion.hpp:66
~ResidualCriterion() override
Destructor.
Definition: residual_criterion.hpp:124
array_1d< double, 3 > VectorType
Definition: residual_criterion.hpp:64
#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