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_explicit_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_EXPLICIT_STRATEGY)
16 #define KRATOS_POROMECHANICS_EXPLICIT_STRATEGY
17 
18 /* System includes */
19 // #include <fstream>
20 
21 // Project includes
23 
24 // Application includes
26 
27 namespace Kratos {
28 
29 template <class TSparseSpace,
30  class TDenseSpace,
31  class TLinearSolver
32  >
34  : public MechanicalExplicitStrategy<TSparseSpace, TDenseSpace, TLinearSolver> {
35 public:
36 
38 
51 
53  typedef typename Node::DofType DofType;
54  typedef typename DofType::Pointer DofPointerType;
55 
59 
60 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
61 
65  typename TSchemeType::Pointer pScheme,
66  Parameters& rParameters,
67  bool CalculateReactions = false,
68  bool ReformDofSetAtEachStep = false,
69  bool MoveMeshFlag = false
70  ) : MechanicalExplicitStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(model_part, pScheme,
71  CalculateReactions, ReformDofSetAtEachStep, MoveMeshFlag)
72  {
73  Parameters default_parameters( R"(
74  {
75  "rebuild_level": 0,
76  "max_radius_factor": 20.0,
77  "min_radius_factor": 0.5,
78  "initial_radius": 1.0e-12,
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  BaseType::SetRebuildLevel(rParameters["rebuild_level"].GetInt());
108  }
109 
110  //------------------------------------------------------------------------------------
111 
114 
115 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
116 
120  void Initialize() override
121  {
122  KRATOS_TRY
123 
124  if (mInitializeWasPerformed == false) {
125 
126  ModelPart& r_model_part = BaseType::GetModelPart();
127 
128  TSystemMatrixType matrix_a_dummy = TSystemMatrixType();
129 
130  // Initialize The Scheme - OPERATIONS TO BE DONE ONCE
131  if (!mpScheme->SchemeIsInitialized())mpScheme->Initialize(r_model_part);
132 
133  // Initialize The Elements - OPERATIONS TO BE DONE ONCE
134  if (!mpScheme->ElementsAreInitialized())mpScheme->InitializeElements(r_model_part);
135 
136  // Initialize The Conditions- OPERATIONS TO BE DONE ONCE
137  if (!mpScheme->ConditionsAreInitialized())mpScheme->InitializeConditions(r_model_part);
138 
139  // Set Nodal Mass to zero
140  NodesArrayType& r_nodes = r_model_part.Nodes();
141  VariableUtils().SetNonHistoricalVariable(NODAL_MASS, 0.0, r_nodes);
142  // TODO: Set Nodal AntiCompressibility to zero for mass-balance equation (C=1/Q, with Q being the compressibility coeff.)
143 
144  // Iterate over the elements
145  ElementsArrayType& r_elements = r_model_part.Elements();
146  const auto it_elem_begin = r_elements.begin();
147  ProcessInfo& r_current_process_info = r_model_part.GetProcessInfo();
148 
149  Vector dummy_vector;
150  #pragma omp parallel for firstprivate(dummy_vector), schedule(guided,512)
151  for (int i = 0; i < static_cast<int>(r_elements.size()); ++i) {
152  // Getting nodal mass and inertia from element
153  // this function needs to be implemented in the respective
154  // element to provide nodal masses
155  auto it_elem = it_elem_begin + i;
156  it_elem->AddExplicitContribution(dummy_vector, RESIDUAL_VECTOR, NODAL_MASS, r_current_process_info);
157  }
158 
159  //Initialize ProcessInfo variables
160  r_current_process_info[IS_CONVERGED] = true;
161 
163  }
164 
165  KRATOS_CATCH( "" )
166  }
167 
168 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
169 
174  void InitializeSolutionStep() override {
175  KRATOS_TRY
176 
177  ModelPart& r_model_part = BaseType::GetModelPart();
178 
179  TSystemMatrixType matrix_a_dummy = TSystemMatrixType();
182 
183  // Initial operations ... things that are constant over the Solution Step
184  mpScheme->InitializeSolutionStep(r_model_part, matrix_a_dummy, rDx, rb);
185 
186  if (BaseType::mRebuildLevel > 0) {
187  ProcessInfo& r_current_process_info = r_model_part.GetProcessInfo();
188  ElementsArrayType& r_elements = r_model_part.Elements();
189  const auto it_elem_begin = r_elements.begin();
190 
191  // Set Nodal Mass and Damping to zero
192  NodesArrayType& r_nodes = r_model_part.Nodes();
193  VariableUtils().SetNonHistoricalVariable(NODAL_MASS, 0.0, r_nodes);
194 
195  Vector dummy_vector;
196  #pragma omp parallel for firstprivate(dummy_vector), schedule(guided,512)
197  for (int i = 0; i < static_cast<int>(r_elements.size()); ++i) {
198  // Getting nodal mass and inertia from element
199  // this function needs to be implemented in the respective
200  // element to provide nodal masses
201  auto it_elem = it_elem_begin + i;
202  it_elem->AddExplicitContribution(dummy_vector, RESIDUAL_VECTOR, NODAL_MASS, r_current_process_info);
203  }
204  }
205 
206  KRATOS_CATCH("")
207  }
208 
209 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
210 
214  bool SolveSolutionStep() override
215  {
216  ModelPart& r_model_part = BaseType::GetModelPart();
217 
218  // Some dummy sets and matrices
219  DofsArrayType dof_set_dummy;
223 
224  // Initialize the non linear iteration
225  mpScheme->InitializeNonLinIteration(BaseType::GetModelPart(), rA, rDx, rb);
226 
227  mpScheme->Predict(r_model_part, dof_set_dummy, rA, rDx, rb);
228 
229  // Move the mesh if needed
232 
233  // Explicitly integrates the equation of motion.
234  mpScheme->Update(r_model_part, dof_set_dummy, rA, rDx, rb);
235 
236  // CONVERGENCE CHECK
237  this->CheckConvergence(r_model_part);
238 
239  // Move the mesh if needed
242 
243  // Finalize the non linear iteration
244  mpScheme->FinalizeNonLinIteration(BaseType::GetModelPart(), rA, rDx, rb);
245 
246  // Calculate reactions if required
248  this->CalculateReactions(mpScheme, r_model_part);
249  }
250 
251  return true;
252  }
253 
254 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
255 
260  void FinalizeSolutionStep() override
261  {
262  ModelPart& r_model_part = BaseType::GetModelPart();
266  // Finalisation of the solution step,
267  // operations to be done after achieving convergence, for example the
268  // Final Residual Vector (rb) has to be saved in there
269  // to avoid error accumulation
270  mpScheme->FinalizeSolutionStep(r_model_part, rA, rDx, rb);
271 
272  // Cleaning memory after the solution
273  mpScheme->Clean();
274  }
275 
276 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
277 
278 protected:
279 
282  std::vector<ModelPart*> mSubModelPartList;
283  std::vector<std::string> mVariableNames;
284 
285 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
286 
295  virtual void CalculateReactions(
296  typename TSchemeType::Pointer pScheme,
297  ModelPart& rModelPart
298  )
299  {
300  // We iterate over the nodes
301  auto& r_nodes = rModelPart.Nodes();
302 
303  // Auxiliar values
304  array_1d<double, 3> force_residual = ZeroVector(3);
305  double flux_residual = 0.0;
306 
307  const ProcessInfo& r_current_process_info = rModelPart.GetProcessInfo();
308  const unsigned int dim = r_current_process_info[DOMAIN_SIZE];
309 
310  // Getting
311  const auto it_node_begin = r_nodes.begin();
312 
313  // Iterating nodes
314  #pragma omp parallel for firstprivate(force_residual,flux_residual), schedule(guided,512)
315  for(int i=0; i<static_cast<int>(r_nodes.size()); ++i) {
316  auto it_node = it_node_begin + i;
317 
318  noalias(force_residual) = it_node->FastGetSolutionStepValue(FORCE_RESIDUAL);
319  flux_residual = it_node->FastGetSolutionStepValue(FLUX_RESIDUAL);
320 
321  if( it_node->IsFixed(DISPLACEMENT_X) == true ) {
322  double& r_reaction = it_node->FastGetSolutionStepValue(REACTION_X);
323  r_reaction = -force_residual[0];
324  }
325  if( it_node->IsFixed(DISPLACEMENT_Y) == true ) {
326  double& r_reaction = it_node->FastGetSolutionStepValue(REACTION_Y);
327  r_reaction = -force_residual[1];
328  }
329  if( it_node->IsFixed(WATER_PRESSURE) == true ) {
330  double& r_reaction = it_node->FastGetSolutionStepValue(REACTION_WATER_PRESSURE);
331  r_reaction = -flux_residual;
332  }
333  if(dim==3) {
334  if( it_node->IsFixed(DISPLACEMENT_Z) == true ) {
335  double& r_reaction = it_node->FastGetSolutionStepValue(REACTION_Z);
336  r_reaction = -force_residual[2];
337  }
338  }
339  }
340  }
341 
342  virtual void CheckConvergence(ModelPart& rModelPart)
343  {
344  const ProcessInfo& r_current_process_info = rModelPart.GetProcessInfo();
345  NodesArrayType& r_nodes = rModelPart.Nodes();
346  const auto it_node_begin = rModelPart.NodesBegin();
347 
348  double l2_numerator = 0.0;
349  double l2_denominator = 0.0;
350  #pragma omp parallel for reduction(+:l2_numerator,l2_denominator)
351  for (int i = 0; i < static_cast<int>(r_nodes.size()); ++i) {
352  auto itCurrentNode = it_node_begin + i;
353  const array_1d<double, 3>& r_current_displacement = itCurrentNode->FastGetSolutionStepValue(DISPLACEMENT);
354  const array_1d<double, 3>& r_previous_displacement = itCurrentNode->FastGetSolutionStepValue(DISPLACEMENT,1);
355  const double& r_current_water_pressure = itCurrentNode->FastGetSolutionStepValue(WATER_PRESSURE);
356  const double& r_previous_water_pressure = itCurrentNode->FastGetSolutionStepValue(WATER_PRESSURE,1);
357  array_1d<double, 3> delta_displacement;
358  noalias(delta_displacement) = r_current_displacement - r_previous_displacement;
359  const double delta_water_pressure = r_current_water_pressure - r_previous_water_pressure;
360  const double norm_2_du = inner_prod(delta_displacement,delta_displacement) + delta_water_pressure*delta_water_pressure;
361  const double norm_2_u_old = inner_prod(r_previous_displacement,r_previous_displacement) + r_previous_water_pressure*r_previous_water_pressure;
362 
363  l2_numerator += norm_2_du;
364  l2_denominator += norm_2_u_old;
365  }
366  if (l2_denominator > 1.0e-12) {
367  double l2_abs_error = std::sqrt(l2_numerator);
368  double l2_rel_error = l2_abs_error/std::sqrt(l2_denominator);
369 
370  // std::fstream l2_error_file;
371  // l2_error_file.open ("l2_error_time.txt", std::fstream::out | std::fstream::app);
372  // l2_error_file.precision(12);
373  // l2_error_file << r_current_process_info[TIME] << " " << l2_rel_error << std::endl;
374  // l2_error_file.close();
375 
376  if (l2_rel_error <= r_current_process_info[ERROR_RATIO] && l2_abs_error <= r_current_process_info[ERROR_INTEGRATION_POINT]) {
377  KRATOS_INFO("EXPLICIT CONVERGENCE CHECK") << "The simulation is converging at step: " << r_current_process_info[STEP] << std::endl;
378  KRATOS_INFO("EXPLICIT CONVERGENCE CHECK") << "L2 Relative Error is: " << l2_rel_error << std::endl;
379  KRATOS_INFO("EXPLICIT CONVERGENCE CHECK") << "L2 Absolute Error is: " << l2_abs_error << std::endl;
380  }
381  }
382  }
383 
384 }; // Class PoromechanicsExplicitStrategy
385 
386 } // namespace Kratos
387 
388 #endif // KRATOS_POROMECHANICS_EXPLICIT_STRATEGY defined
iterator begin()
Iterator pointing to the beginning of the container.
Definition: data_value_container.h:209
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
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
void SetRebuildLevel(int Level) override
This sets the build level.
Definition: implicit_solving_strategy.h:207
BaseType::NodesArrayType NodesArrayType
Definition: implicit_solving_strategy.h:92
int mRebuildLevel
Definition: implicit_solving_strategy.h:263
BaseType::TSystemMatrixType TSystemMatrixType
Definition: implicit_solving_strategy.h:70
BaseType::ElementsArrayType ElementsArrayType
Definition: implicit_solving_strategy.h:94
This strategy is used for the explicit time integration.
Definition: mechanical_explicit_strategy.hpp:54
TSchemeType::Pointer mpScheme
Definition: mechanical_explicit_strategy.hpp:587
bool mInitializeWasPerformed
Flag telling if the initialize was performed.
Definition: mechanical_explicit_strategy.hpp:608
bool mCalculateReactionsFlag
Flag telling if it is needed or not to compute the reactions.
Definition: mechanical_explicit_strategy.hpp:602
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
NodeIterator NodesBegin(IndexType ThisIndex=0)
Definition: model_part.h:487
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
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
Definition: poromechanics_explicit_strategy.hpp:34
TSchemeType::Pointer mpScheme
Definition: mechanical_explicit_strategy.hpp:587
void InitializeSolutionStep() override
Performs all the required operations that should be done (for each step) before solving the solution ...
Definition: poromechanics_explicit_strategy.hpp:174
virtual void CheckConvergence(ModelPart &rModelPart)
Definition: poromechanics_explicit_strategy.hpp:342
void Initialize() override
Initialization of member variables and prior operations.
Definition: poromechanics_explicit_strategy.hpp:120
PoromechanicsExplicitStrategy(ModelPart &model_part, typename TSchemeType::Pointer pScheme, Parameters &rParameters, bool CalculateReactions=false, bool ReformDofSetAtEachStep=false, bool MoveMeshFlag=false)
Constructor.
Definition: poromechanics_explicit_strategy.hpp:63
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: poromechanics_explicit_strategy.hpp:50
bool SolveSolutionStep() override
Solves the current step. This function returns true if a solution has been found, false otherwise.
Definition: poromechanics_explicit_strategy.hpp:214
std::vector< std::string > mVariableNames
List of every SubModelPart associated to an external load.
Definition: poromechanics_explicit_strategy.hpp:283
KRATOS_CLASS_POINTER_DEFINITION(PoromechanicsExplicitStrategy)
MechanicalExplicitStrategy< TSparseSpace, TDenseSpace, TLinearSolver > MotherType
Definition: poromechanics_explicit_strategy.hpp:40
BaseType::ElementsArrayType ElementsArrayType
Definition: poromechanics_explicit_strategy.hpp:48
~PoromechanicsExplicitStrategy() override
Destructor.
Definition: poromechanics_explicit_strategy.hpp:113
std::vector< ModelPart * > mSubModelPartList
Definition: poromechanics_explicit_strategy.hpp:282
BaseType::ConditionsArrayType ConditionsArrayType
Definition: poromechanics_explicit_strategy.hpp:49
BaseType::TSystemMatrixPointerType TSystemMatrixPointerType
Definition: poromechanics_explicit_strategy.hpp:45
BaseType::TSystemVectorPointerType TSystemVectorPointerType
Definition: poromechanics_explicit_strategy.hpp:46
bool mInitializeWasPerformed
Flag telling if the initialize was performed.
Definition: mechanical_explicit_strategy.hpp:608
bool mCalculateReactionsFlag
Flag telling if it is needed or not to compute the reactions.
Definition: mechanical_explicit_strategy.hpp:602
ImplicitSolvingStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: poromechanics_explicit_strategy.hpp:39
Node::DofType DofType
DoF types definition.
Definition: poromechanics_explicit_strategy.hpp:53
DofType::Pointer DofPointerType
Definition: poromechanics_explicit_strategy.hpp:54
BaseType::DofsArrayType DofsArrayType
Definition: poromechanics_explicit_strategy.hpp:42
virtual void CalculateReactions(typename TSchemeType::Pointer pScheme, ModelPart &rModelPart)
Name of the nodal variable associated to every SubModelPart.
Definition: poromechanics_explicit_strategy.hpp:295
Parameters * mpParameters
Member Variables.
Definition: poromechanics_explicit_strategy.hpp:281
BaseType::TSchemeType TSchemeType
Definition: poromechanics_explicit_strategy.hpp:41
BaseType::TSystemMatrixType TSystemMatrixType
Definition: poromechanics_explicit_strategy.hpp:43
BaseType::TSystemVectorType TSystemVectorType
Definition: poromechanics_explicit_strategy.hpp:44
void FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: poromechanics_explicit_strategy.hpp:260
BaseType::NodesArrayType NodesArrayType
Definition: poromechanics_explicit_strategy.hpp:47
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
TSparseSpace::VectorPointerType TSystemVectorPointerType
Definition: solving_strategy.h:77
ModelPart::NodesContainerType NodesArrayType
Definition: solving_strategy.h:89
ModelPart & GetModelPart()
Operations to get the pointer to the model.
Definition: solving_strategy.h:350
ModelPart::ElementsContainerType ElementsArrayType
Definition: solving_strategy.h:91
TSparseSpace::MatrixType TSystemMatrixType
Definition: solving_strategy.h:71
TSparseSpace::MatrixPointerType TSystemMatrixPointerType
Definition: solving_strategy.h:75
ModelPart::ConditionsContainerType ConditionsArrayType
Definition: solving_strategy.h:93
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
TDenseSpace::VectorType LocalSystemVectorType
Definition: solving_strategy.h:81
This class implements a set of auxiliar, already parallelized, methods to perform some common tasks r...
Definition: variable_utils.h:63
void SetNonHistoricalVariable(const TVarType &rVariable, const TType &Value, TContainerType &rContainer)
Sets the container value of any type of non historical variable.
Definition: variable_utils.h:790
#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
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
KratosZeroVector< double > ZeroVector
Definition: amatrix_interface.h:561
TExpression1Type::data_type inner_prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:592
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
model_part
Definition: face_heat.py:14
int dim
Definition: sensitivityMatrix.py:25
integer i
Definition: TensorModule.f:17