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.
mortar_and_criteria.h
Go to the documentation of this file.
1 // KRATOS ______ __ __ _____ __ __ __
2 // / ____/___ ____ / /_____ ______/ /_/ ___// /________ _______/ /___ ___________ _/ /
3 // / / / __ \/ __ \/ __/ __ `/ ___/ __/\__ \/ __/ ___/ / / / ___/ __/ / / / ___/ __ `/ /
4 // / /___/ /_/ / / / / /_/ /_/ / /__/ /_ ___/ / /_/ / / /_/ / /__/ /_/ /_/ / / / /_/ / /
5 // \____/\____/_/ /_/\__/\__,_/\___/\__//____/\__/_/ \__,_/\___/\__/\__,_/_/ \__,_/_/ MECHANICS
6 //
7 // License: BSD License
8 // license: ContactStructuralMechanicsApplication/license.txt
9 //
10 // Main authors: Vicente Mataix Ferrandiz
11 //
12 
13 #pragma once
14 
15 // System includes
16 
17 // External includes
18 
19 // Project includes
24 
25 namespace Kratos
26 {
27 
30 
34 
38 
42 
46 
53 template<class TSparseSpace,
54  class TDenseSpace
55  >
57  : public And_Criteria< TSparseSpace, TDenseSpace >
58 {
59 public:
62 
65 
67  KRATOS_DEFINE_LOCAL_FLAG( PRINTING_OUTPUT );
68  KRATOS_DEFINE_LOCAL_FLAG( TABLE_IS_INITIALIZED );
69  KRATOS_DEFINE_LOCAL_FLAG( CONDITION_NUMBER_IS_INITIALIZED );
70 
73 
76 
79 
82 
85 
88 
90  using TablePrinterPointerType = TableStreamUtility::Pointer;
91 
93  using IndexType = std::size_t;
94 
96  using ConditionNumberUtilityPointerType = ConditionNumberUtility::Pointer;
97 
101 
106  : BaseType()
107  {
108  }
109 
115  : BaseType()
116  {
117  // Validate and assign defaults
118  ThisParameters = this->ValidateAndAssignParameters(ThisParameters, this->GetDefaultParameters());
119  this->AssignSettings(ThisParameters);
120  }
121 
126  typename ConvergenceCriteriaBaseType::Pointer pFirstCriterion,
127  typename ConvergenceCriteriaBaseType::Pointer pSecondCriterion,
128  const bool PrintingOutput = false,
129  ConditionNumberUtilityPointerType pConditionNumberUtility = nullptr
130  )
131  :BaseType(pFirstCriterion, pSecondCriterion),
132  mpConditionNumberUtility(pConditionNumberUtility)
133  {
134  // Set local flags
135  mOptions.Set(MortarAndConvergenceCriteria::PRINTING_OUTPUT, PrintingOutput);
136  mOptions.Set(MortarAndConvergenceCriteria::TABLE_IS_INITIALIZED, false);
137  mOptions.Set(MortarAndConvergenceCriteria::CONDITION_NUMBER_IS_INITIALIZED, false);
138  }
139 
144  :BaseType(rOther)
145  ,mOptions(rOther.mOptions)
146  ,mpConditionNumberUtility(rOther.mpConditionNumberUtility)
147  {
148  }
149 
152  ~MortarAndConvergenceCriteria () override = default;
153 
157 
161 
166  typename ConvergenceCriteriaBaseType::Pointer Create(Parameters ThisParameters) const override
167  {
168  return Kratos::make_shared<ClassType>(ThisParameters);
169  }
170 
181  ModelPart& rModelPart,
182  DofsArrayType& rDofSet,
183  const TSystemMatrixType& rA,
184  const TSystemVectorType& rDx,
185  const TSystemVectorType& rb
186  ) override
187  {
188  // The process info
189  ProcessInfo& r_process_info = rModelPart.GetProcessInfo();
190 
191  if (rModelPart.GetCommunicator().MyPID() == 0 && this->GetEchoLevel() > 0) {
192  if (r_process_info.Has(TABLE_UTILITY)) {
193  TablePrinterPointerType p_table = r_process_info[TABLE_UTILITY];
194  p_table->AddToRow<IndexType>(rModelPart.GetProcessInfo()[NL_ITERATION_NUMBER]);
195  }
196  }
197 
198  bool criterion_result = BaseType::PostCriteria(rModelPart, rDofSet, rA, rDx, rb);
199 
200  if (mpConditionNumberUtility != nullptr) {
201  TSystemMatrixType copy_A(rA); // NOTE: Can not be const, TODO: Change the solvers to const
202  const double condition_number = mpConditionNumberUtility->GetConditionNumber(copy_A);
203 
204  if (r_process_info.Has(TABLE_UTILITY)) {
205  std::cout.precision(4);
206  TablePrinterPointerType p_table = r_process_info[TABLE_UTILITY];
207  auto& r_table = p_table->GetTable();
208  r_table << condition_number;
209  } else {
210  if (mOptions.IsNot(MortarAndConvergenceCriteria::PRINTING_OUTPUT))
211  KRATOS_INFO("MortarAndConvergenceCriteria") << "\n" << BOLDFONT("CONDITION NUMBER:") << "\t " << std::scientific << condition_number << std::endl;
212  else
213  KRATOS_INFO("MortarAndConvergenceCriteria") << "\n" << "CONDITION NUMBER:" << "\t" << std::scientific << condition_number << std::endl;
214  }
215  }
216 
217  if (criterion_result == true && rModelPart.GetCommunicator().MyPID() == 0 && this->GetEchoLevel() > 0)
218  if (r_process_info.Has(TABLE_UTILITY)) {
219  TablePrinterPointerType p_table = r_process_info[TABLE_UTILITY];
220  p_table->PrintFooter();
221  }
222 
223  return criterion_result;
224  }
225 
230  void Initialize(ModelPart& rModelPart) override
231  {
232  // The process info
233  ProcessInfo& r_process_info = rModelPart.GetProcessInfo();
234 
235  if (r_process_info.Has(TABLE_UTILITY) && mOptions.IsNot(MortarAndConvergenceCriteria::TABLE_IS_INITIALIZED)) {
236  TablePrinterPointerType p_table = r_process_info[TABLE_UTILITY];
237  (p_table->GetTable()).SetBold(mOptions.IsNot(MortarAndConvergenceCriteria::PRINTING_OUTPUT));
238  (p_table->GetTable()).AddColumn("ITER", 4);
239  }
240 
241  mOptions.Set(MortarAndConvergenceCriteria::TABLE_IS_INITIALIZED, true);
242  BaseType::Initialize(rModelPart);
243 
244  if (r_process_info.Has(TABLE_UTILITY) && mpConditionNumberUtility != nullptr
245  && mOptions.IsNot(MortarAndConvergenceCriteria::CONDITION_NUMBER_IS_INITIALIZED)) {
246  TablePrinterPointerType p_table = r_process_info[TABLE_UTILITY];
247  (p_table->GetTable()).AddColumn("COND.NUM.", 10);
248  mOptions.Set(MortarAndConvergenceCriteria::CONDITION_NUMBER_IS_INITIALIZED, true);
249  }
250  }
251 
261  ModelPart& rModelPart,
262  DofsArrayType& rDofSet,
263  const TSystemMatrixType& rA,
264  const TSystemVectorType& rDx,
265  const TSystemVectorType& rb
266  ) override
267  {
268  // The process info
269  ProcessInfo& r_process_info = rModelPart.GetProcessInfo();
270 
271  if (rModelPart.GetCommunicator().MyPID() == 0 && this->GetEchoLevel() > 0) {
272  std::cout.precision(4);
273  if (mOptions.IsNot(MortarAndConvergenceCriteria::PRINTING_OUTPUT))
274  std::cout << "\n\n" << BOLDFONT("CONVERGENCE CHECK") << "\tSTEP: " << rModelPart.GetProcessInfo()[STEP] << "\tTIME: " << std::scientific << rModelPart.GetProcessInfo()[TIME] << "\tDELTA TIME: " << std::scientific << rModelPart.GetProcessInfo()[DELTA_TIME] << std::endl;
275  else
276  std::cout << "\n\n" << "CONVERGENCE CHECK" << "\tSTEP: " << rModelPart.GetProcessInfo()[STEP] << "\tTIME: " << std::scientific << rModelPart.GetProcessInfo()[TIME] << "\tDELTA TIME: " << std::scientific << rModelPart.GetProcessInfo()[DELTA_TIME] << std::endl;
277 
278  if (r_process_info.Has(TABLE_UTILITY)) {
279  TablePrinterPointerType p_table = r_process_info[TABLE_UTILITY];
280  p_table->PrintHeader();
281  }
282  }
283 
284  BaseType::InitializeSolutionStep(rModelPart, rDofSet, rA, rDx, rb);
285  }
286 
296  ModelPart& rModelPart,
297  DofsArrayType& rDofSet,
298  const TSystemMatrixType& rA,
299  const TSystemVectorType& rDx,
300  const TSystemVectorType& rb
301  ) override
302  {
303  BaseType::FinalizeSolutionStep(rModelPart,rDofSet, rA, rDx, rb);
304  }
305 
311  {
312  Parameters default_parameters = Parameters(R"(
313  {
314  "name" : "mortar_and_criteria",
315  "print_convergence_criterion" : false
316  })" );
317 
318  // Getting base class default parameters
319  const Parameters base_default_parameters = BaseType::GetDefaultParameters();
320  default_parameters.RecursivelyAddMissingParameters(base_default_parameters);
321  return default_parameters;
322  }
323 
328  static std::string Name()
329  {
330  return "mortar_and_criteria";
331  }
332 
336 
340 
344 
346  std::string Info() const override
347  {
348  return "MortarAndConvergenceCriteria";
349  }
350 
352  void PrintInfo(std::ostream& rOStream) const override
353  {
354  rOStream << Info();
355  }
356 
358  void PrintData(std::ostream& rOStream) const override
359  {
360  rOStream << Info();
361  }
362 
364 protected:
367 
371 
375 
379 
384  void AssignSettings(const Parameters ThisParameters) override
385  {
386  BaseType::AssignSettings(ThisParameters);
387 
388  // Defining condition number utility
389  mpConditionNumberUtility = nullptr; // TODO: Define a factory
390 
391  // Set local flags
392  mOptions.Set(MortarAndConvergenceCriteria::PRINTING_OUTPUT, ThisParameters["print_convergence_criterion"].GetBool());
393  mOptions.Set(MortarAndConvergenceCriteria::TABLE_IS_INITIALIZED, false);
394  mOptions.Set(MortarAndConvergenceCriteria::CONDITION_NUMBER_IS_INITIALIZED, false);
395  }
396 
398 private:
401 
405 
406  Flags mOptions;
407 
408  ConditionNumberUtilityPointerType mpConditionNumberUtility;
409 
411 }; // Kratos MortarAndConvergenceCriteria
412 
415 
417 template<class TSparseSpace, class TDenseSpace>
418 const Kratos::Flags MortarAndConvergenceCriteria<TSparseSpace, TDenseSpace>::PRINTING_OUTPUT(Kratos::Flags::Create(0));
419 template<class TSparseSpace, class TDenseSpace>
420 const Kratos::Flags MortarAndConvergenceCriteria<TSparseSpace, TDenseSpace>::TABLE_IS_INITIALIZED(Kratos::Flags::Create(1));
421 template<class TSparseSpace, class TDenseSpace>
422 const Kratos::Flags MortarAndConvergenceCriteria<TSparseSpace, TDenseSpace>::CONDITION_NUMBER_IS_INITIALIZED(Kratos::Flags::Create(2));
423 
424 } /* namespace Kratos.*/
This convergence criteria checks simultaneously two convergence criteria (both must be satisfied)
Definition: and_criteria.h:60
BaseType::TSystemMatrixType TSystemMatrixType
Definition: and_criteria.h:79
BaseType::DofsArrayType DofsArrayType
Definition: and_criteria.h:77
void Initialize(ModelPart &rModelPart) override
This function initialize the convergence criteria.
Definition: and_criteria.h:228
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: and_criteria.h:337
void FinalizeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &A, const TSystemVectorType &Dx, const TSystemVectorType &b) override
This function finalizes the solution step.
Definition: and_criteria.h:282
BaseType::TSystemVectorType TSystemVectorType
Definition: and_criteria.h:81
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &A, const TSystemVectorType &Dx, const TSystemVectorType &b) override
Criteria that need to be called after getting the solution.
Definition: and_criteria.h:210
void InitializeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &A, const TSystemVectorType &Dx, const TSystemVectorType &b) override
This function initializes the solution step.
Definition: and_criteria.h:242
virtual int MyPID() const
Definition: communicator.cpp:91
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
virtual Parameters ValidateAndAssignParameters(Parameters ThisParameters, const Parameters DefaultParameters) const
This method validate and assign default parameters.
Definition: convergence_criteria.h:466
virtual void AssignSettings(const Parameters ThisParameters)
This method assigns settings to member variables.
Definition: convergence_criteria.h:479
bool Has(const Variable< TDataType > &rThisVariable) const
Checks if the data container has a value associated with a given variable.
Definition: data_value_container.h:382
Definition: flags.h:58
void Set(const Flags ThisFlag)
Definition: flags.cpp:33
static Flags Create(IndexType ThisPosition, bool Value=true)
Definition: flags.h:138
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
Custom AND convergence criteria for the mortar condition.
Definition: mortar_and_criteria.h:58
void InitializeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
This function initializes the solution step.
Definition: mortar_and_criteria.h:260
ConditionNumberUtility::Pointer ConditionNumberUtilityPointerType
The condition number utility pointer definition.
Definition: mortar_and_criteria.h:96
void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: mortar_and_criteria.h:384
void Initialize(ModelPart &rModelPart) override
This function initialize the convergence criteria.
Definition: mortar_and_criteria.h:230
KRATOS_DEFINE_LOCAL_FLAG(PRINTING_OUTPUT)
Local Flags.
MortarAndConvergenceCriteria(MortarAndConvergenceCriteria const &rOther)
Definition: mortar_and_criteria.h:143
KRATOS_DEFINE_LOCAL_FLAG(CONDITION_NUMBER_IS_INITIALIZED)
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: mortar_and_criteria.h:352
MortarAndConvergenceCriteria(Kratos::Parameters ThisParameters)
Default constructor. (with parameters)
Definition: mortar_and_criteria.h:114
std::size_t IndexType
The index type definition.
Definition: mortar_and_criteria.h:93
KRATOS_CLASS_POINTER_DEFINITION(MortarAndConvergenceCriteria)
Pointer definition of MortarAndConvergenceCriteria.
std::string Info() const override
Turn back information as a string.
Definition: mortar_and_criteria.h:346
void FinalizeSolutionStep(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
This function finalizes the solution step.
Definition: mortar_and_criteria.h:295
static std::string Name()
Returns the name of the class as used in the settings (snake_case format)
Definition: mortar_and_criteria.h:328
TableStreamUtility::Pointer TablePrinterPointerType
The table stream definition TODO: Replace by logger.
Definition: mortar_and_criteria.h:90
MortarAndConvergenceCriteria()
Default constructor.
Definition: mortar_and_criteria.h:105
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: mortar_and_criteria.h:358
ConvergenceCriteriaBaseType::Pointer Create(Parameters ThisParameters) const override
Create method.
Definition: mortar_and_criteria.h:166
~MortarAndConvergenceCriteria() override=default
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &rA, const TSystemVectorType &rDx, const TSystemVectorType &rb) override
Criteria that need to be called after getting the solution.
Definition: mortar_and_criteria.h:180
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: mortar_and_criteria.h:310
KRATOS_DEFINE_LOCAL_FLAG(TABLE_IS_INITIALIZED)
MortarAndConvergenceCriteria(typename ConvergenceCriteriaBaseType::Pointer pFirstCriterion, typename ConvergenceCriteriaBaseType::Pointer pSecondCriterion, const bool PrintingOutput=false, ConditionNumberUtilityPointerType pConditionNumberUtility=nullptr)
Definition: mortar_and_criteria.h:125
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
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
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
#define BOLDFONT(x)
Definition: color_utilities.h:34
#define KRATOS_INFO(label)
Definition: logger.h:250
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21