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.
poromechanics_newton_raphson_strategy.hpp
Go to the documentation of this file.
1 
2 // | / |
3 // ' / __| _` | __| _ \ __|
4 // . \ | ( | | ( |\__ `
5 // _|\_\_| \__,_|\__|\___/ ____/
6 // Multi-Physics
7 //
8 // License: BSD License
9 // Kratos default license: kratos/license.txt
10 //
11 // Main authors: Ignasi de Pouplana
12 //
13 
14 
15 #if !defined(KRATOS_POROMECHANICS_NEWTON_RAPHSON_STRATEGY)
16 #define KRATOS_POROMECHANICS_NEWTON_RAPHSON_STRATEGY
17 
18 // Project includes
19 #include "includes/define.h"
20 #include "includes/model_part.h"
21 #include "includes/checks.h"
25 
26 // Application includes
28 
29 namespace Kratos
30 {
31 
32 template<class TSparseSpace,class TDenseSpace,class TLinearSolver>
33 
34 class PoromechanicsNewtonRaphsonStrategy : public ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
35 {
36 
37 public:
38 
40 
51  using MotherType::mpA; //Tangent matrix
52  using MotherType::mpb; //Residual vector of iteration i
53  using MotherType::mpDx; //Delta x of iteration i
56 
57 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
58 
62  typename TSchemeType::Pointer pScheme,
63  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
64  typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver,
65  Parameters& rParameters,
66  int MaxIterations = 30,
67  bool CalculateReactions = false,
68  bool ReformDofSetAtEachStep = false,
69  bool MoveMeshFlag = false
70  ) : ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(model_part, pScheme,
71  pNewConvergenceCriteria, pNewBuilderAndSolver, MaxIterations, CalculateReactions, ReformDofSetAtEachStep, MoveMeshFlag)
72  {
73  //only include validation with c++11 since raw_literals do not exist in c++03
74  Parameters default_parameters( R"(
75  {
76  "desired_iterations": 4,
77  "max_radius_factor": 20.0,
78  "min_radius_factor": 0.5,
79  "characteristic_length": 0.05,
80  "search_neighbours_step": false,
81  "body_domain_sub_model_part_list": [],
82  "loads_sub_model_part_list": [],
83  "loads_variable_list" : []
84  } )" );
85 
86  // Validate agains defaults -- this also ensures no type mismatch
87  rParameters.ValidateAndAssignDefaults(default_parameters);
88 
89  mpParameters = &rParameters;
90 
91  // Set Load SubModelParts and Variable names
92  if(rParameters["loads_sub_model_part_list"].size() > 0)
93  {
94  mSubModelPartList.resize(rParameters["loads_sub_model_part_list"].size());
95  mVariableNames.resize(rParameters["loads_variable_list"].size());
96 
97  if( mSubModelPartList.size() != mVariableNames.size() )
98  KRATOS_THROW_ERROR( std::logic_error, "For each SubModelPart there must be a corresponding nodal Variable", "" )
99 
100  for(unsigned int i = 0; i < mVariableNames.size(); i++)
101  {
102  mSubModelPartList[i] = &( model_part.GetSubModelPart(rParameters["loads_sub_model_part_list"][i].GetString()) );
103  mVariableNames[i] = rParameters["loads_variable_list"][i].GetString();
104  }
105  }
106  }
107 
108  //------------------------------------------------------------------------------------
109 
112 
113 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
114 
115  void Initialize() override
116  {
117  KRATOS_TRY
118 
119  if (mInitializeWasPerformed == false)
120  {
122 
123  //Initialize ProcessInfo variables
124  BaseType::GetModelPart().GetProcessInfo()[IS_CONVERGED] = true;
125  }
126 
127  KRATOS_CATCH( "" )
128  }
129 
130 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
131 
132  bool IsConverged() override
133  {
134  KRATOS_TRY
135 
136  bool IsConverged = true;
137 
138  // Set the loads to 0.0
139  this->SetLoadsToZero();
140 
141  // Note: Initialize() needs to be called beforehand
142 
143  this->InitializeSolutionStep();
144 
145  this->Predict();
146 
147  // Solve the problem with load = 0.0
148  IsConverged = this->CheckConvergence();
149 
150  this->FinalizeSolutionStep();
151 
152  // Set the loads to its original value
153  this->RestoreLoadsValue();
154 
155  return IsConverged;
156 
157  KRATOS_CATCH("")
158  }
159 
160 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
161 
162 protected:
163 
166  std::vector<ModelPart*> mSubModelPartList;
167  std::vector<std::string> mVariableNames;
168 
169 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
170 
171  virtual bool CheckConvergence()
172  {
173  // ********** Prediction phase **********
174 
175  // Initialize variables
176  DofsArrayType& rDofSet = mpBuilderAndSolver->GetDofSet();
177  TSystemMatrixType& mA = *mpA;
178  TSystemVectorType& mDx = *mpDx;
180 
181  mpScheme->InitializeNonLinIteration(BaseType::GetModelPart(), mA, mDx, mb);
182 
183  TSparseSpace::SetToZero(mA);
184  TSparseSpace::SetToZero(mb);
185  TSparseSpace::SetToZero(mDx);
186 
187  mpBuilderAndSolver->BuildAndSolve(mpScheme, BaseType::GetModelPart(), mA, mDx, mb);
188 
189  mpScheme->Update(BaseType::GetModelPart(), rDofSet, mA, mDx, mb);
190 
191  //move the mesh if needed
193 
194  mpScheme->FinalizeNonLinIteration(BaseType::GetModelPart(), mA, mDx, mb);
195 
196  unsigned int iteration_number = 0;
197  bool is_converged = false;
198  double dofs_ratio = 1000.0;
199  double ReferenceDofsNorm;
200  double NormDx;
201 
202  // ********** Correction phase (iteration cicle) **********
203 
204  while (is_converged == false && iteration_number < mMaxIterationNumber)
205  {
206  //setting the number of iteration
207  iteration_number += 1;
208  BaseType::GetModelPart().GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
209 
210  mpScheme->InitializeNonLinIteration(BaseType::GetModelPart(), mA, mDx, mb);
211 
212  TSparseSpace::SetToZero(mA);
213  TSparseSpace::SetToZero(mb);
214  TSparseSpace::SetToZero(mDx);
215 
216  mpBuilderAndSolver->BuildAndSolve(mpScheme, BaseType::GetModelPart(), mA, mDx, mb);
217 
218  mpScheme->Update(BaseType::GetModelPart(), rDofSet, mA, mDx, mb);
219 
220  //move the mesh if needed
222 
223  mpScheme->FinalizeNonLinIteration(BaseType::GetModelPart(), mA, mDx, mb);
224 
225  NormDx = TSparseSpace::TwoNorm(mDx);
226  ReferenceDofsNorm = this->CalculateReferenceDofsNorm(rDofSet);
227  dofs_ratio = NormDx/ReferenceDofsNorm;
228  KRATOS_INFO("Newton Raphson Strategy") << "TEST ITERATION: " << iteration_number << std::endl;
229  KRATOS_INFO("Newton Raphson Strategy") << " Dofs Ratio = " << dofs_ratio << std::endl;
230 
231  if(dofs_ratio <= 1.0e-3)
232  is_converged = true;
233  }
234 
235  return is_converged;
236  }
237 
238 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
239 
241  {
242  double ReferenceDofsNorm = 0.0;
243 
244  int NumThreads = ParallelUtilities::GetNumThreads();
245  OpenMPUtils::PartitionVector DofSetPartition;
246  OpenMPUtils::DivideInPartitions(rDofSet.size(), NumThreads, DofSetPartition);
247 
248  #pragma omp parallel reduction(+:ReferenceDofsNorm)
249  {
250  int k = OpenMPUtils::ThisThread();
251 
252  typename DofsArrayType::iterator DofsBegin = rDofSet.begin() + DofSetPartition[k];
253  typename DofsArrayType::iterator DofsEnd = rDofSet.begin() + DofSetPartition[k+1];
254 
255  for (typename DofsArrayType::iterator itDof = DofsBegin; itDof != DofsEnd; ++itDof)
256  {
257  if (itDof->IsFree())
258  {
259  const double& temp = itDof->GetSolutionStepValue();
260  ReferenceDofsNorm += temp*temp;
261  }
262  }
263  }
264 
265  return sqrt(ReferenceDofsNorm);
266  }
267 
268 private:
269 
270 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
271 
272  void SetLoadsToZero()
273  {
274  for(unsigned int i = 0; i < mVariableNames.size(); i++)
275  {
276  ModelPart& rSubModelPart = *(mSubModelPartList[i]);
277  const std::string& VariableName = mVariableNames[i];
278 
279  if( KratosComponents< Variable<double> >::Has( VariableName ) )
280  {
281  const Variable<double>& var = KratosComponents< Variable<double> >::Get( VariableName );
282 
283  #pragma omp parallel
284  {
285  ModelPart::NodeIterator NodesBegin;
286  ModelPart::NodeIterator NodesEnd;
287  OpenMPUtils::PartitionedIterators(rSubModelPart.Nodes(),NodesBegin,NodesEnd);
288 
289  for (ModelPart::NodeIterator itNode = NodesBegin; itNode != NodesEnd; ++itNode)
290  {
291  double& rvalue = itNode->FastGetSolutionStepValue(var);
292  itNode->FastGetSolutionStepValue(var,1) = rvalue;
293  rvalue = 0.0;
294  }
295  }
296  }
297  else if( KratosComponents< Variable<array_1d<double,3> > >::Has(VariableName) )
298  {
299  typedef Variable<double> component_type;
300  const component_type& varx = KratosComponents< component_type >::Get(VariableName+std::string("_X"));
301  const component_type& vary = KratosComponents< component_type >::Get(VariableName+std::string("_Y"));
302  const component_type& varz = KratosComponents< component_type >::Get(VariableName+std::string("_Z"));
303 
304  #pragma omp parallel
305  {
306  ModelPart::NodeIterator NodesBegin;
307  ModelPart::NodeIterator NodesEnd;
308  OpenMPUtils::PartitionedIterators(rSubModelPart.Nodes(),NodesBegin,NodesEnd);
309 
310  for (ModelPart::NodeIterator itNode = NodesBegin; itNode != NodesEnd; ++itNode)
311  {
312  double& rvaluex = itNode->FastGetSolutionStepValue(varx);
313  itNode->FastGetSolutionStepValue(varx,1) = rvaluex;
314  rvaluex = 0.0;
315 
316  double& rvaluey = itNode->FastGetSolutionStepValue(vary);
317  itNode->FastGetSolutionStepValue(vary,1) = rvaluey;
318  rvaluey = 0.0;
319 
320  double& rvaluez = itNode->FastGetSolutionStepValue(varz);
321  itNode->FastGetSolutionStepValue(varz,1) = rvaluez;
322  rvaluez = 0.0;
323  }
324  }
325  }
326  else
327  {
328  KRATOS_THROW_ERROR( std::logic_error, "One variable of the applied loads has a non supported type. Variable: ", VariableName )
329  }
330  }
331  }
332 
333 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
334 
335  void RestoreLoadsValue()
336  {
337  for(unsigned int i = 0; i < mVariableNames.size(); i++)
338  {
339  ModelPart& rSubModelPart = *(mSubModelPartList[i]);
340  const std::string& VariableName = mVariableNames[i];
341 
342  if( KratosComponents< Variable<double> >::Has( VariableName ) )
343  {
344  const Variable<double>& var = KratosComponents< Variable<double> >::Get( VariableName );
345 
346  #pragma omp parallel
347  {
348  ModelPart::NodeIterator NodesBegin;
349  ModelPart::NodeIterator NodesEnd;
350  OpenMPUtils::PartitionedIterators(rSubModelPart.Nodes(),NodesBegin,NodesEnd);
351 
352  for (ModelPart::NodeIterator itNode = NodesBegin; itNode != NodesEnd; ++itNode)
353  {
354  itNode->FastGetSolutionStepValue(var) = itNode->FastGetSolutionStepValue(var,1);
355  }
356  }
357  }
358  else if( KratosComponents< Variable<array_1d<double,3> > >::Has(VariableName) )
359  {
360  typedef Variable<double> component_type;
361  const component_type& varx = KratosComponents< component_type >::Get(VariableName+std::string("_X"));
362  const component_type& vary = KratosComponents< component_type >::Get(VariableName+std::string("_Y"));
363  const component_type& varz = KratosComponents< component_type >::Get(VariableName+std::string("_Z"));
364 
365  #pragma omp parallel
366  {
367  ModelPart::NodeIterator NodesBegin;
368  ModelPart::NodeIterator NodesEnd;
369  OpenMPUtils::PartitionedIterators(rSubModelPart.Nodes(),NodesBegin,NodesEnd);
370 
371  for (ModelPart::NodeIterator itNode = NodesBegin; itNode != NodesEnd; ++itNode)
372  {
373  itNode->FastGetSolutionStepValue(varx) = itNode->FastGetSolutionStepValue(varx,1);
374 
375  itNode->FastGetSolutionStepValue(vary) = itNode->FastGetSolutionStepValue(vary,1);
376 
377  itNode->FastGetSolutionStepValue(varz) = itNode->FastGetSolutionStepValue(varz,1);
378  }
379  }
380  }
381  else
382  {
383  KRATOS_THROW_ERROR( std::logic_error, "One variable of the applied loads has a non supported type. Variable: ", VariableName )
384  }
385  }
386  }
387 
388 }; // Class PoromechanicsNewtonRaphsonStrategy
389 
390 } // namespace Kratos
391 
392 #endif // KRATOS_POROMECHANICS_NEWTON_RAPHSON_STRATEGY defined
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
Implicit solving strategy base class This is the base class from which we will derive all the implici...
Definition: implicit_solving_strategy.h:61
BaseType::TSystemVectorType TSystemVectorType
Definition: implicit_solving_strategy.h:72
Scheme< TSparseSpace, TDenseSpace > TSchemeType
Definition: implicit_solving_strategy.h:82
BuilderAndSolver< TSparseSpace, TDenseSpace, TLinearSolver > TBuilderAndSolverType
Definition: implicit_solving_strategy.h:84
BaseType::TSystemMatrixType TSystemMatrixType
Definition: implicit_solving_strategy.h:70
KratosComponents class encapsulates a lookup table for a family of classes in a generic way.
Definition: kratos_components.h:49
static const TComponentType & Get(const std::string &rName)
Retrieves a component with the specified name.
Definition: kratos_components.h:114
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
MeshType::NodeIterator NodeIterator
Definition: model_part.h:134
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
static void DivideInPartitions(const int NumTerms, const int NumThreads, PartitionVector &Partitions)
Divide an array of length NumTerms between NumThreads threads.
Definition: openmp_utils.h:158
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
static void PartitionedIterators(TVector &rVector, typename TVector::iterator &rBegin, typename TVector::iterator &rEnd)
Generate a partition for an std::vector-like array, providing iterators to the begin and end position...
Definition: openmp_utils.h:179
std::vector< int > PartitionVector
Vector type for the output of DivideInPartitions method.
Definition: openmp_utils.h:53
static int GetNumThreads()
Returns the current number of threads.
Definition: parallel_utilities.cpp:34
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
void ValidateAndAssignDefaults(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing match the form prescribed by th...
Definition: kratos_parameters.cpp:1306
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
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
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
Definition: poromechanics_newton_raphson_strategy.hpp:35
BaseType::TSchemeType TSchemeType
Definition: poromechanics_newton_raphson_strategy.hpp:45
bool IsConverged() override
This should be considered as a "post solution" convergence check which is useful for coupled analysis...
Definition: poromechanics_newton_raphson_strategy.hpp:132
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: poromechanics_newton_raphson_strategy.hpp:44
ResidualBasedNewtonRaphsonStrategy< TSparseSpace, TDenseSpace, TLinearSolver > MotherType
Definition: poromechanics_newton_raphson_strategy.hpp:42
unsigned int mMaxIterationNumber
Definition: residualbased_newton_raphson_strategy.h:1261
ConvergenceCriteria< TSparseSpace, TDenseSpace > TConvergenceCriteriaType
Definition: poromechanics_newton_raphson_strategy.hpp:43
std::vector< ModelPart * > mSubModelPartList
Definition: poromechanics_newton_raphson_strategy.hpp:166
TSystemVectorPointerType mpb
The increment in the solution.
Definition: residualbased_newton_raphson_strategy.h:1237
KRATOS_CLASS_POINTER_DEFINITION(PoromechanicsNewtonRaphsonStrategy)
PoromechanicsNewtonRaphsonStrategy(ModelPart &model_part, typename TSchemeType::Pointer pScheme, typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria, typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver, Parameters &rParameters, int MaxIterations=30, bool CalculateReactions=false, bool ReformDofSetAtEachStep=false, bool MoveMeshFlag=false)
Constructor.
Definition: poromechanics_newton_raphson_strategy.hpp:60
ImplicitSolvingStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: poromechanics_newton_raphson_strategy.hpp:41
BaseType::TSystemMatrixType TSystemMatrixType
Definition: poromechanics_newton_raphson_strategy.hpp:47
TSystemMatrixPointerType mpA
The RHS vector of the system of equations.
Definition: residualbased_newton_raphson_strategy.h:1238
Parameters * mpParameters
Member Variables.
Definition: poromechanics_newton_raphson_strategy.hpp:165
TSystemVectorPointerType mpDx
The pointer to the convergence criteria employed.
Definition: residualbased_newton_raphson_strategy.h:1236
TBuilderAndSolverType::Pointer mpBuilderAndSolver
The pointer to the time scheme employed.
Definition: residualbased_newton_raphson_strategy.h:1233
void Initialize() override
Initialization of member variables and prior operations.
Definition: poromechanics_newton_raphson_strategy.hpp:115
TSchemeType::Pointer mpScheme
Definition: residualbased_newton_raphson_strategy.h:1232
BaseType::TSystemVectorType TSystemVectorType
Definition: poromechanics_newton_raphson_strategy.hpp:48
~PoromechanicsNewtonRaphsonStrategy() override
Destructor.
Definition: poromechanics_newton_raphson_strategy.hpp:111
std::vector< std::string > mVariableNames
List of every SubModelPart associated to an external load.
Definition: poromechanics_newton_raphson_strategy.hpp:167
bool mInitializeWasPerformed
The maximum number of iterations, 30 by default.
Definition: residualbased_newton_raphson_strategy.h:1263
virtual bool CheckConvergence()
Name of the nodal variable associated to every SubModelPart.
Definition: poromechanics_newton_raphson_strategy.hpp:171
BaseType::DofsArrayType DofsArrayType
Definition: poromechanics_newton_raphson_strategy.hpp:46
double CalculateReferenceDofsNorm(DofsArrayType &rDofSet)
Definition: poromechanics_newton_raphson_strategy.hpp:240
This is the base Newton Raphson strategy.
Definition: residualbased_newton_raphson_strategy.h:66
unsigned int mMaxIterationNumber
Definition: residualbased_newton_raphson_strategy.h:1261
TSystemVectorPointerType mpb
The increment in the solution.
Definition: residualbased_newton_raphson_strategy.h:1237
TSystemMatrixPointerType mpA
The RHS vector of the system of equations.
Definition: residualbased_newton_raphson_strategy.h:1238
TSystemVectorPointerType mpDx
The pointer to the convergence criteria employed.
Definition: residualbased_newton_raphson_strategy.h:1236
TBuilderAndSolverType::Pointer mpBuilderAndSolver
The pointer to the time scheme employed.
Definition: residualbased_newton_raphson_strategy.h:1233
void Initialize() override
Initialization of member variables and prior operations.
Definition: residualbased_newton_raphson_strategy.h:672
TSchemeType::Pointer mpScheme
Definition: residualbased_newton_raphson_strategy.h:1232
void Predict() override
Operation to predict the solution ... if it is not called a trivial predictor is used in which the va...
Definition: residualbased_newton_raphson_strategy.h:625
void InitializeSolutionStep() override
Performs all the required operations that should be done (for each step) before solving the solution ...
Definition: residualbased_newton_raphson_strategy.h:781
bool mInitializeWasPerformed
The maximum number of iterations, 30 by default.
Definition: residualbased_newton_raphson_strategy.h:1263
void FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: residualbased_newton_raphson_strategy.h:848
ModelPart & GetModelPart()
Operations to get the pointer to the model.
Definition: solving_strategy.h:350
TSparseSpace::MatrixType TSystemMatrixType
Definition: solving_strategy.h:71
bool MoveMeshFlag()
This function returns the flag that says if the mesh is moved.
Definition: solving_strategy.h:290
TSparseSpace::VectorType TSystemVectorType
Definition: solving_strategy.h:73
virtual void MoveMesh()
This function is designed to move the mesh.
Definition: solving_strategy.h:330
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_INFO(label)
Definition: logger.h:250
Kratos::ModelPart ModelPart
Definition: kratos_wrapper.h:31
bool Has(const std::string &ModelerName)
Checks if the modeler is registered.
Definition: modeler_factory.cpp:24
double TwoNorm(SparseSpaceType &dummy, SparseSpaceType::VectorType &x)
Definition: add_strategies_to_python.cpp:164
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
model_part
Definition: face_heat.py:14
int k
Definition: quadrature.py:595
float temp
Definition: rotating_cone.py:85
integer i
Definition: TensorModule.f:17
double precision function mb(a)
Definition: TensorModule.f:849