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.
explicit_solving_strategy_runge_kutta.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Ruben Zorrilla
11 // Eduard Gomez
12 //
13 //
14 
15 #pragma once
16 
17 // System includes
18 #include <numeric>
19 
20 // External includes
21 
22 // Project includes
23 #include "includes/define.h"
24 #include "includes/model_part.h"
25 #include "factories/factory.h"
27 #include "butcher_tableau.h"
28 
29 namespace Kratos
30 {
31 
35 
39 
43 
64 template <class TSparseSpace, class TDenseSpace, class TButcherTableau>
65 class ExplicitSolvingStrategyRungeKutta : public ExplicitSolvingStrategy<TSparseSpace, TDenseSpace>
66 {
67 public:
70 
73 
76 
79 
82 
84  using DofType = typename BaseType::DofType;
85 
89 
92 
96 
101  : BaseType()
102  {
103  }
104 
111  ModelPart &rModelPart,
112  Parameters ThisParameters)
113  : BaseType(rModelPart)
114  {
115  // Validate and assign defaults
116  ThisParameters = this->ValidateAndAssignParameters(ThisParameters, this->GetDefaultParameters());
117  this->AssignSettings(ThisParameters);
118  }
119 
127  ModelPart &rModelPart,
128  typename ExplicitBuilderType::Pointer pExplicitBuilder,
129  bool MoveMeshFlag = false,
130  int RebuildLevel = 0)
131  : BaseType(rModelPart, pExplicitBuilder, MoveMeshFlag, RebuildLevel)
132  {
133  }
134 
141  ModelPart &rModelPart,
142  bool MoveMeshFlag = false,
143  int RebuildLevel = 0)
144  : BaseType(rModelPart, MoveMeshFlag, RebuildLevel)
145  {
146  }
147 
153  typename SolvingStrategyType::Pointer Create(
154  ModelPart& rModelPart,
155  Parameters ThisParameters
156  ) const override
157  {
158  return Kratos::make_shared<ClassType>(rModelPart, ThisParameters);
159  }
160 
164 
168 
174  {
175  Parameters default_parameters = Parameters(R"(
176  {
177  "name" : "explicit_solving_strategy_runge_kutta"
178  })");
179 
180  // Getting base class default parameters
181  const Parameters base_default_parameters = BaseType::GetDefaultParameters();
182  default_parameters.RecursivelyAddMissingParameters(base_default_parameters);
183  return default_parameters;
184  }
185 
189 
190 
194 
195  void Initialize() override
196  {
198  BaseType::GetModelPart().GetProcessInfo().SetValue(TIME_INTEGRATION_THETA, 0.0);
199  }
200 
204 
209  static std::string Name()
210  {
211  std::stringstream s;
212  s << "explicit_solving_strategy_runge_kutta_" << TButcherTableau::Name();
213  return s.str();
214  }
215 
217  std::string Info() const override
218  {
219  std::stringstream ss;
220  ss << "ExplicitSolvingStrategyRungeKutta<" << mButcherTableau.Info() << ">";
221  return ss.str();
222  }
223 
225  void PrintInfo(std::ostream &rOStream) const override
226  {
227  rOStream << Info();
228  }
229 
231  void PrintData(std::ostream &rOStream) const override
232  {
233  rOStream << Info();
234  }
235 
237 
238 protected:
241  const TButcherTableau mButcherTableau;
242 
243 
247 
251 
252 
256 
258  {
259  KRATOS_TRY
260 
261  // Get the required data from the explicit builder and solver
262  auto& r_explicit_bs = BaseType::GetExplicitBuilder();
263  auto& r_dof_set = r_explicit_bs.GetDofSet();
264  const unsigned int dof_size = r_explicit_bs.GetEquationSystemSize();
265  const auto& r_lumped_mass_vector = r_explicit_bs.GetLumpedMassMatrixVector();
266 
267  // Set the auxiliary RK vectors
268  LocalSystemVectorType u_n(dof_size); // TODO: THIS IS INEFICCIENT. CREATE A UNORDERED_SET WITH THE IDOF AND VALUE AS ENTRIES. THIS HAS TO BE OPTIONAL
269  LocalSystemMatrixType rk_K(dof_size, TButcherTableau::SubstepCount());
270 
271  // Perform the RK update
272  const double dt = BaseType::GetDeltaTime();
273  KRATOS_ERROR_IF(dt < 1.0e-12) << "ProcessInfo DELTA_TIME is close to zero." << std::endl;
274 
275  // Set the previous step solution in the current buffer position
276  // Note that we set the 0 position of the buffer to be equal to the values in step n (not n+1)
277  // Additionally, we save in an auxiliary vector the value of the fixed DOFs, which is also taken from the previous time step
278  IndexPartition<int>(r_dof_set.size()).for_each(
279  [&](int i_dof){
280  auto it_dof = r_dof_set.begin() + i_dof;
281  double& r_u_0 = it_dof->GetSolutionStepValue(0);
282  const double& r_u_1 = it_dof->GetSolutionStepValue(1);
283  if (it_dof->IsFixed()) {
284  u_n(i_dof) = r_u_0;
285  }
286  r_u_0 = r_u_1;
287  }
288  );
289 
290  // Calculate the RK intermediate sub steps
291  for(unsigned int i=1; i<TButcherTableau::SubstepCount(); ++i) {
293  }
295 
296  // Do the final solution update
297  const auto& weights = mButcherTableau.GetWeights();
298  IndexPartition<int>(r_dof_set.size()).for_each(
299  [&](int i_dof){
300  auto it_dof = r_dof_set.begin() + i_dof;
301  // Do the DOF update
302  double& r_u = it_dof->GetSolutionStepValue(0);
303  const double& r_u_old = it_dof->GetSolutionStepValue(1);
304  if (!it_dof->IsFixed()) {
305  const double mass = r_lumped_mass_vector(i_dof);
306  const MatrixRow<LocalSystemMatrixType> substeps_k = row(rk_K, i_dof);
307  r_u = r_u_old + (dt / mass) * std::inner_product(weights.begin(), weights.end(), substeps_k.begin(), 0.0);
308  } else {
309  r_u = u_n(i_dof);
310  }
311  }
312  );
313 
314  KRATOS_CATCH("");
315  }
316 
322 
328 
334 
339  virtual void FinalizeRungeKuttaLastSubStep() {};
340 
350  const IndexType SubStepIndex,
351  const LocalSystemVectorType& rFixedDofsValues,
352  LocalSystemMatrixType& rIntermediateStepResidualVectors)
353  {
354  KRATOS_TRY
355 
356  // Get the required data from the explicit builder and solver
357  auto& r_explicit_bs = BaseType::GetExplicitBuilder();
358  auto& r_dof_set = r_explicit_bs.GetDofSet();
359  const auto& r_lumped_mass_vector = r_explicit_bs.GetLumpedMassMatrixVector();
360 
361  // Get model part and information
362  const double dt = BaseType::GetDeltaTime();
363  KRATOS_ERROR_IF(dt < 1.0e-12) << "ProcessInfo DELTA_TIME is close to zero." << std::endl;
364  auto& r_model_part = BaseType::GetModelPart();
365  auto& r_process_info = r_model_part.GetProcessInfo();
366 
367  // Fetch this substeps's values from tableau
368  const double integration_theta = mButcherTableau.GetIntegrationTheta(SubStepIndex);
369  const auto alphas_begin = mButcherTableau.GetMatrixRowBegin(SubStepIndex); // Runge kutta matrix row begin
370  const auto alphas_end = mButcherTableau.GetMatrixRowEnd(SubStepIndex); // Runge kutta matrix row end
371 
372  // Set the RUNGE_KUTTA_STEP value. This has to be done prior to the InitializeRungeKuttaStep()
373  r_process_info.GetValue(RUNGE_KUTTA_STEP) = SubStepIndex;
374  r_process_info.GetValue(TIME_INTEGRATION_THETA) = integration_theta;
375 
376  // Perform the intermidate sub step update
378  r_explicit_bs.BuildRHS(r_model_part);
379 
380  IndexPartition<int>(r_dof_set.size()).for_each(
381  [&](int i_dof){
382  auto it_dof = r_dof_set.begin() + i_dof;
383  // Save current value in the corresponding vector
384  const double& r_res = it_dof->GetSolutionStepReactionValue();
385  rIntermediateStepResidualVectors(i_dof, SubStepIndex-1) = r_res;
386  // Do the DOF update
387  double& r_u = it_dof->GetSolutionStepValue(0);
388  const double& r_u_old = it_dof->GetSolutionStepValue(1);
389  if (!it_dof->IsFixed()) {
390  const double mass = r_lumped_mass_vector(i_dof);
391  const auto k = row(rIntermediateStepResidualVectors, i_dof);
392  r_u = r_u_old + (dt / mass) * std::inner_product(alphas_begin, alphas_end, k.begin(), 0.0);
393  /* ^~~~~~~~~~~~~~~~~~
394  * Using std::inner_product instead of boost's inner_prod because it allows us to
395  * chose a begin and, more importantly, an end.
396  *
397  * This is useful because alpha_ij = 0 for j >= i, hence the tail end of this scalar product
398  * can be ignored by setting the past-the-end iterator at j=i
399  */
400  } else {
401  const double delta_u = rFixedDofsValues(i_dof) - r_u_old;
402  r_u = r_u_old + integration_theta * delta_u;
403  }
404  }
405  );
406 
408 
409  KRATOS_CATCH("SubstepIndex = " + std::to_string(SubStepIndex));
410  }
411 
417  virtual void PerformRungeKuttaLastSubStep(LocalSystemMatrixType& rLastStepResidualVector)
418  {
419  KRATOS_TRY
420 
421  // Get the required data from the explicit builder and solver
422  auto& r_explicit_bs = BaseType::GetExplicitBuilder();
423  auto& r_dof_set = r_explicit_bs.GetDofSet();
424 
425  // Get model part
426  auto& r_model_part = BaseType::GetModelPart();
427  auto& r_process_info = r_model_part.GetProcessInfo();
428  constexpr unsigned int substep_index = TButcherTableau::SubstepCount();
429 
430  // Set the RUNGE_KUTTA_STEP value. This has to be done prior to the InitializeRungeKuttaStep()
431  r_process_info.GetValue(RUNGE_KUTTA_STEP) = TButcherTableau::SubstepCount();
432  r_process_info.GetValue(TIME_INTEGRATION_THETA) = mButcherTableau.GetIntegrationTheta(substep_index);
433 
434  // Perform the last sub step residual calculation
436  r_explicit_bs.BuildRHS(r_model_part);
437 
438  IndexPartition<int>(r_dof_set.size()).for_each(
439  [&](int i_dof){
440  const auto it_dof = r_dof_set.begin() + i_dof;
441  // Save current value in the corresponding vector
442  const double& r_res = it_dof->GetSolutionStepReactionValue();
443  rLastStepResidualVector(i_dof, substep_index - 1) = r_res;
444  }
445  );
446 
448 
449  KRATOS_CATCH("");
450  }
451 
455 
456 
460 
461 
465 
466 
468 private:
471 
472 
476 
477 
481 
482 
486 
487 
491 
492 
496 
497 
501 
502 
504 };
505 
507 
510 
511 template <class TSparseSpace, class TDenseSpace>
513 
514 template <class TSparseSpace, class TDenseSpace>
516 
517 template <class TSparseSpace, class TDenseSpace>
519 
520 template <class TSparseSpace, class TDenseSpace>
522 
524 
525 } /* namespace Kratos.*/
void SetValue(const Variable< TDataType > &rThisVariable, TDataType const &rValue)
Sets the value for a given variable.
Definition: data_value_container.h:320
Dof represents a degree of freedom (DoF).
Definition: dof.h:86
Current class provides an implementation for the base explicit builder and solving operations.
Definition: explicit_builder.h:68
Explicit solving strategy base class.
Definition: explicit_solving_strategy.h:58
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: explicit_solving_strategy.h:338
virtual double GetDeltaTime()
Get the Delta Time object This method returns the DELTA_TIME from the ProcessInfo container.
Definition: explicit_solving_strategy.h:410
ExplicitBuilder< TSparseSpace, TDenseSpace > ExplicitBuilderType
Definition: explicit_solving_strategy.h:64
ExplicitBuilderType::DofType DofType
Definition: explicit_solving_strategy.h:70
void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: explicit_solving_strategy.h:419
void Initialize() override
Initialization of member variables and prior operations.
Definition: explicit_solving_strategy.h:171
ExplicitBuilderType & GetExplicitBuilder()
Operations to get the explicit builder and solver.
Definition: explicit_solving_strategy.h:295
Family of explicit Runge-Kutta schemes.
Definition: explicit_solving_strategy_runge_kutta.h:66
virtual void InitializeRungeKuttaLastSubStep()
Initialize the Runge-Kutta last substep This method is intended to implement all the operations requi...
Definition: explicit_solving_strategy_runge_kutta.h:333
typename TDenseSpace::VectorType LocalSystemVectorType
The local vector definition.
Definition: explicit_solving_strategy_runge_kutta.h:87
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: explicit_solving_strategy_runge_kutta.h:225
virtual void FinalizeRungeKuttaLastSubStep()
Finalize the Runge-Kutta last substep This method is intended to implement all the operations require...
Definition: explicit_solving_strategy_runge_kutta.h:339
KRATOS_CLASS_POINTER_DEFINITION(ExplicitSolvingStrategyRungeKutta)
std::string Info() const override
Return information as a string.
Definition: explicit_solving_strategy_runge_kutta.h:217
virtual void PerformRungeKuttaIntermediateSubStep(const IndexType SubStepIndex, const LocalSystemVectorType &rFixedDofsValues, LocalSystemMatrixType &rIntermediateStepResidualVectors)
Performs an intermediate RK4 step This functions performs all the operations required in an intermedi...
Definition: explicit_solving_strategy_runge_kutta.h:349
void SolveWithLumpedMassMatrix() override
Calculate the explicit update This method is intended to implement the explicit update calculation No...
Definition: explicit_solving_strategy_runge_kutta.h:257
const TButcherTableau mButcherTableau
Definition: explicit_solving_strategy_runge_kutta.h:241
ExplicitSolvingStrategyRungeKutta()
Default constructor. (empty)
Definition: explicit_solving_strategy_runge_kutta.h:100
ExplicitSolvingStrategyRungeKutta(ModelPart &rModelPart, typename ExplicitBuilderType::Pointer pExplicitBuilder, bool MoveMeshFlag=false, int RebuildLevel=0)
Default constructor.
Definition: explicit_solving_strategy_runge_kutta.h:126
ExplicitSolvingStrategyRungeKutta(ModelPart &rModelPart, bool MoveMeshFlag=false, int RebuildLevel=0)
Default constructor.
Definition: explicit_solving_strategy_runge_kutta.h:140
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: explicit_solving_strategy_runge_kutta.h:231
virtual void FinalizeRungeKuttaIntermediateSubStep()
Finalize the Runge-Kutta intermediate substep This method is intended to implement all the operations...
Definition: explicit_solving_strategy_runge_kutta.h:327
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: explicit_solving_strategy_runge_kutta.h:173
SolvingStrategyType::Pointer Create(ModelPart &rModelPart, Parameters ThisParameters) const override
Create method.
Definition: explicit_solving_strategy_runge_kutta.h:153
~ExplicitSolvingStrategyRungeKutta() override=default
ExplicitSolvingStrategyRungeKutta(const ExplicitSolvingStrategyRungeKutta &Other)=delete
void Initialize() override
Initialization of member variables and prior operations.
Definition: explicit_solving_strategy_runge_kutta.h:195
ExplicitSolvingStrategyRungeKutta(ModelPart &rModelPart, Parameters ThisParameters)
Default constructor. (with parameters)
Definition: explicit_solving_strategy_runge_kutta.h:110
virtual void InitializeRungeKuttaIntermediateSubStep()
Initialize the Runge-Kutta intermediate substep This method is intended to implement all the operatio...
Definition: explicit_solving_strategy_runge_kutta.h:321
static std::string Name()
Returns the name of the class as used in the settings (snake_case format)
Definition: explicit_solving_strategy_runge_kutta.h:209
typename TDenseSpace::MatrixType LocalSystemMatrixType
Definition: explicit_solving_strategy_runge_kutta.h:88
virtual void PerformRungeKuttaLastSubStep(LocalSystemMatrixType &rLastStepResidualVector)
Performs the last RK4 step This functions performs all the operations required in the last RK4 sub st...
Definition: explicit_solving_strategy_runge_kutta.h:417
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
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
Solving strategy base class This is the base class from which we will derive all the strategies (impl...
Definition: solving_strategy.h:64
ModelPart & GetModelPart()
Operations to get the pointer to the model.
Definition: solving_strategy.h:350
virtual Parameters ValidateAndAssignParameters(Parameters ThisParameters, const Parameters DefaultParameters) const
This method validate and assign default parameters.
Definition: solving_strategy.h:507
bool MoveMeshFlag()
This function returns the flag that says if the mesh is moved.
Definition: solving_strategy.h:290
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
dt
Definition: DEM_benchmarks.py:173
Vector VectorType
Definition: geometrical_transformation_utilities.h:56
Matrix MatrixType
Definition: geometrical_transformation_utilities.h:55
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
AMatrix::MatrixRow< const TExpressionType > row(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t RowIndex)
Definition: amatrix_interface.h:649
int k
Definition: quadrature.py:595
integer i
Definition: TensorModule.f:17