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.
segregated_strategy.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: March 2018 $
6 // Revision: $Revision: 0.0 $
7 //
8 //
9 
10 #if !defined(KRATOS_SEGREGATED_STRATEGY_H_INCLUDED)
11 #define KRATOS_SEGREGATED_STRATEGY_H_INCLUDED
12 
13 // System includes
14 
15 // External includes
16 
17 // Project includes
19 
20 //default builder and solver
22 //default convergence criterion
24 
26 
27 namespace Kratos
28 {
29 
32 
36 
40 
44 
48 
54 template <class TSparseSpace,
55  class TDenseSpace, // = DenseSpace<double>,
56  class TLinearSolver // = LinearSolver<TSparseSpace,TDenseSpace>
57  >
58 class SegregatedStrategy : public SolutionStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
59 {
60  public:
63 
64  // Counted pointer of ClassName
66 
68  typedef typename BaseType::Pointer BasePointerType;
70 
71  typedef typename std::vector<BasePointerType> StrategiesContainerType;
72  typedef typename StrategiesContainerType::iterator StrategiesContainerIteratorType;
73 
76  typedef typename BaseType::SchemeType SchemeType;
77 
78  typedef TLinearSolver LinearSolverType;
79  typedef TSparseSpace SparseSpaceType;
80 
86 
89 
91 
98  Flags& rOptions)
99  : SolutionStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(rModelPart, rOptions)
100  {
101  KRATOS_TRY
102 
103  KRATOS_CATCH("")
104  }
105 
113  Flags& rOptions,
114  StrategiesContainerType& rStrategies)
115  : SolutionStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(rModelPart, rOptions)
116  {
117  KRATOS_TRY
118 
119  mStrategies = rStrategies;
120 
121  KRATOS_CATCH("")
122  }
123 
128  {
129  Clear();
130  }
131 
134 
136 
140 
144  void InitializeSolutionStep() override
145  {
146  KRATOS_TRY
147 
148  //set implex
149  if(this->mOptions.Is(LocalFlagType::IMPLEX))
150  this->GetModelPart().GetProcessInfo().SetValue(IMPLEX, true);
151 
152  //prints informations about the current time
153  //KRATOS_INFO("") << " [STEP:" << this->GetModelPart().GetProcessInfo()[STEP] << " TIME: "<< this->GetModelPart().GetProcessInfo()[TIME]<< "]\n" << LoggerMessage::Category::STATUS;
154 
155  int counter = 0;
156  for(StrategiesContainerIteratorType it= mStrategies.begin(); it!= mStrategies.end(); ++it)
157  {
158  this->GetModelPart().GetProcessInfo().SetValue(SEGREGATED_STEP, counter);
159  (*it)->InitializeSolutionStep();
160  ++counter;
161  }
162 
163  KRATOS_CATCH("")
164  }
165 
169  void FinalizeSolutionStep() override
170  {
171  KRATOS_TRY
172 
173  //Finalization of the solution step, operations to be done after achieving convergence
174 
175  //set implex calculation
176  if(this->mOptions.Is(LocalFlagType::IMPLEX))
177  this->GetModelPart().GetProcessInfo().SetValue(IMPLEX, false);
178 
179  int counter = 0;
180  for(StrategiesContainerIteratorType it= mStrategies.begin(); it!= mStrategies.end(); ++it)
181  {
182  this->GetModelPart().GetProcessInfo().SetValue(SEGREGATED_STEP, counter);
183  (*it)->FinalizeSolutionStep();
184  ++counter;
185  }
186 
187  //this->Finalize();
188 
189  KRATOS_CATCH("")
190  }
191 
192 
196  bool SolveSolutionStep() override
197  {
198  KRATOS_TRY
199 
200  //initializing the parameters of the Newton-Raphson cicle
201  unsigned int iteration_number = 1;
202 
203  //setting the iteration number
204  this->GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
205 
206  //maximum number of iterations
207  unsigned int MaxIterationNumber = 1;
208  for(StrategiesContainerIteratorType it= mStrategies.begin(); it!= mStrategies.end(); ++it)
209  {
210  if(MaxIterationNumber < (*it)->GetMaxIterationNumber())
211  MaxIterationNumber = (*it)->GetMaxIterationNumber();
212  }
213 
214  //setting the iteration number
215  this->GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
216 
217  this->Set(LocalFlagType::CONVERGED, this->SolveIteration());
218 
219  //iteration cycle... performed only for NonLinearProblems
220  while( this->IsNot(LocalFlagType::CONVERGED) && ++iteration_number < MaxIterationNumber)
221  {
222  //setting the iteration number
223  this->GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
224 
225  this->Set(LocalFlagType::CONVERGED, this->SolveIteration());
226  }
227 
228  //plots a warning if the maximum number of iterations is exceeded
229  if(iteration_number >= MaxIterationNumber)
230  {
231  if( this->GetEchoLevel() >= 0 )
232  KRATOS_INFO(" [Iterative loop interrupted] ") << "[" << iteration_number << " iterations performed] \n";
233  }
234 
235  return (this->Is(LocalFlagType::CONVERGED));
236 
237  KRATOS_CATCH("")
238 
239  }
240 
241 
245  bool SolveIteration() override
246  {
247  KRATOS_TRY
248 
249  //convergence vector
250  std::vector<bool> convergences(mStrategies.size());
251  std::fill(convergences.begin(), convergences.end(), false);
252 
253  int counter = 0;
254  for(StrategiesContainerIteratorType it= mStrategies.begin(); it!= mStrategies.end(); ++it)
255  {
256  this->GetModelPart().GetProcessInfo().SetValue(SEGREGATED_STEP, counter);
257  convergences[counter] = (*it)->SolveIteration();
258  ++counter;
259  }
260 
261  bool convergence = std::all_of(convergences.begin(), convergences.end(), [](bool const n){return n == true;});
262  if(convergence == true){
263  if( this->GetEchoLevel() >= 0 )
264  KRATOS_INFO(" [Convergence Achieved] ") << "[" << this->GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] << " iterations performed]\n";
265  }
266 
267  return (convergence);
268 
269  KRATOS_CATCH("")
270  }
271 
272 
276  void Clear() override
277  {
278  KRATOS_TRY
279 
280  int counter = 0;
281  for(StrategiesContainerIteratorType it= mStrategies.begin(); it!= mStrategies.end(); ++it)
282  {
283  this->GetModelPart().GetProcessInfo().SetValue(SEGREGATED_STEP, counter);
284  (*it)->Clear();
285  ++counter;
286  }
287 
288  KRATOS_CATCH("")
289  }
290 
295  int Check() override
296  {
297  KRATOS_TRY
298 
299  int counter = 0;
300  for(StrategiesContainerIteratorType it= mStrategies.begin(); it!= mStrategies.end(); ++it)
301  {
302  this->GetModelPart().GetProcessInfo().SetValue(SEGREGATED_STEP, counter);
303  (*it)->Check();
304  ++counter;
305  }
306 
307  return 0;
308 
309  KRATOS_CATCH("")
310  }
311 
312 
316 
328  void SetEchoLevel(const int Level) override
329  {
330  BaseType::SetEchoLevel(Level);
331  for(StrategiesContainerIteratorType it= mStrategies.begin(); it!= mStrategies.end(); ++it)
332  {
333  (*it)->SetEchoLevel(Level);
334  }
335 
336  }
337 
342  void AddStrategy(const BasePointerType pStrategy)
343  {
344  mStrategies.push_back(pStrategy);
345  };
346 
351  BasePointerType GetStrategy(unsigned int Label)
352  {
353  return mStrategies[Label];
354  };
355 
356 
360 
364 
366 
367  protected:
370 
374 
376 
393 
394  private:
415 
417  SegregatedStrategy(const SegregatedStrategy &Other){};
418 
420 
421 };
422 
424 
431 
433 
434 } // namespace Kratos.
435 #endif // KRATOS_SEGREGATED_STRATEGY_H_INCLUDED defined
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
void SetValue(const Variable< TDataType > &rThisVariable, TDataType const &rValue)
Sets the value for a given variable.
Definition: data_value_container.h:320
Definition: flags.h:58
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
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
This is the base class for a segregated strategy for the same model part.
Definition: segregated_strategy.hpp:59
BaseType::LocalFlagType LocalFlagType
Definition: segregated_strategy.hpp:69
void Clear() override
Clears the internal storage.
Definition: segregated_strategy.hpp:276
void InitializeSolutionStep() override
Performs all the required operations that should be done (for each step) before solving the solution ...
Definition: segregated_strategy.hpp:144
std::vector< BasePointerType > StrategiesContainerType
Definition: segregated_strategy.hpp:71
KRATOS_CLASS_POINTER_DEFINITION(SegregatedStrategy)
BaseType::SystemMatrixPointerType SystemMatrixPointerType
Definition: segregated_strategy.hpp:84
StrategiesContainerType mStrategies
Definition: segregated_strategy.hpp:375
~SegregatedStrategy() override
Destructor.
Definition: segregated_strategy.hpp:127
BaseType::SystemMatrixType SystemMatrixType
Definition: segregated_strategy.hpp:82
void SetEchoLevel(const int Level) override
This sets the level of echo for the solving strategy.
Definition: segregated_strategy.hpp:328
TLinearSolver LinearSolverType
Definition: segregated_strategy.hpp:78
BaseType::SystemVectorType SystemVectorType
Definition: segregated_strategy.hpp:83
StrategiesContainerType::iterator StrategiesContainerIteratorType
Definition: segregated_strategy.hpp:72
SolutionStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: segregated_strategy.hpp:67
SegregatedStrategy(ModelPart &rModelPart, Flags &rOptions)
Definition: segregated_strategy.hpp:97
BasePointerType GetStrategy(unsigned int Label)
Get method for the time scheme.
Definition: segregated_strategy.hpp:351
int Check() override
Function to perform expensive checks.
Definition: segregated_strategy.hpp:295
BaseType::BuilderAndSolverType BuilderAndSolverType
Definition: segregated_strategy.hpp:75
BaseType::Pointer BasePointerType
Definition: segregated_strategy.hpp:68
void AddStrategy(const BasePointerType pStrategy)
Set method for the time scheme.
Definition: segregated_strategy.hpp:342
bool SolveSolutionStep() override
Solves the current step. This function returns true if a solution has been found, false otherwise.
Definition: segregated_strategy.hpp:196
BaseType::SystemVectorPointerType SystemVectorPointerType
Definition: segregated_strategy.hpp:85
void FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: segregated_strategy.hpp:169
ConvergenceCriteria< TSparseSpace, TDenseSpace > ConvergenceCriterionType
Definition: segregated_strategy.hpp:74
bool SolveIteration() override
Solves the iteration. This function returns true if a solution has been found, false otherwise.
Definition: segregated_strategy.hpp:245
BaseType::DofsArrayType DofsArrayType
Definition: segregated_strategy.hpp:81
TSparseSpace SparseSpaceType
Definition: segregated_strategy.hpp:79
SegregatedStrategy(ModelPart &rModelPart, Flags &rOptions, StrategiesContainerType &rStrategies)
Definition: segregated_strategy.hpp:112
BaseType::SchemeType SchemeType
Definition: segregated_strategy.hpp:76
Solution Buider and Solver base class.
Definition: solution_builder_and_solver.hpp:63
Solution scheme base class.
Definition: solution_scheme.hpp:54
Solution strategy base class.
Definition: solution_strategy.hpp:52
TSparseSpace::VectorPointerType SystemVectorPointerType
Definition: solution_strategy.hpp:63
ModelPart & GetModelPart()
Operations to get the pointer to the model.
Definition: solution_strategy.hpp:243
Flags mOptions
Definition: solution_strategy.hpp:267
TSparseSpace::MatrixPointerType SystemMatrixPointerType
Definition: solution_strategy.hpp:62
TSparseSpace::MatrixType SystemMatrixType
Definition: solution_strategy.hpp:60
TSparseSpace::VectorType SystemVectorType
Definition: solution_strategy.hpp:61
virtual void SetEchoLevel(const int Level)
This sets the level of echo for the solution strategy.
Definition: solution_strategy.hpp:190
virtual int GetEchoLevel()
This returns the level of echo for the solution strategy.
Definition: solution_strategy.hpp:206
Solver local flags class definition.
Definition: solution_local_flags.hpp:48
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_INFO(label)
Definition: logger.h:250
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
int n
manufactured solution and derivatives (u=0 at z=0 dudz=0 at z=domain_height)
Definition: ode_solve.py:402
int counter
Definition: script_THERMAL_CORRECT.py:218