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_bfecc.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, Eduard Gómez
11 //
12 //
13 
14 #if !defined(KRATOS_EXPLICIT_SOLVING_STRATEGY_BFECC)
15 #define KRATOS_EXPLICIT_SOLVING_STRATEGY_BFECC
16 
17 /* System includes */
18 
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 
28 namespace Kratos
29 {
30 
34 
38 
42 
89 template <class TSparseSpace, class TDenseSpace>
90 class ExplicitSolvingStrategyBFECC : public ExplicitSolvingStrategy<TSparseSpace, TDenseSpace>
91 {
92 public:
95 
97 
99 
100  // The base solving strategy class definition
102 
103  // The base class definition
105 
108 
109  // The explicit builder and solver definition
111 
113  typedef typename BaseType::DofType DofType;
114 
118 
121 
122  // Time-stepping settings
123  struct SubstepData {
124  enum Direction : int {BACK=-1, FORTH=1};
125 
126  SubstepData(const double Theta, const Direction Dir)
127  : theta(Theta), direction(Dir)
128  { }
129 
130  int TimeDirection() const noexcept { return static_cast<int>(direction); }
131 
132  double theta;
135  };
136 
140 
145  : BaseType()
146  {
147  }
148 
155  ModelPart &rModelPart,
156  Parameters ThisParameters)
157  : BaseType(rModelPart)
158  {
159  // Validate and assign defaults
160  ThisParameters = this->ValidateAndAssignParameters(ThisParameters, this->GetDefaultParameters());
161  this->AssignSettings(ThisParameters);
162  }
163 
171  ModelPart &rModelPart,
172  typename ExplicitBuilderType::Pointer pExplicitBuilder,
173  bool MoveMeshFlag = false,
174  int RebuildLevel = 0)
175  : BaseType(rModelPart, pExplicitBuilder, MoveMeshFlag, RebuildLevel)
176  {
177  }
178 
185  ModelPart &rModelPart,
186  bool MoveMeshFlag = false,
187  int RebuildLevel = 0)
188  : BaseType(rModelPart, MoveMeshFlag, RebuildLevel)
189  {
190  }
191 
197  typename SolvingStrategyType::Pointer Create(
198  ModelPart& rModelPart,
199  Parameters ThisParameters
200  ) const override
201  {
202  return Kratos::make_shared<ClassType>(rModelPart, ThisParameters);
203  }
204 
208 
211  ~ExplicitSolvingStrategyBFECC() override = default;
212 
217  virtual Parameters GetDefaultParameters() const override
218  {
219  Parameters default_parameters = Parameters(R"(
220  {
221  "name" : "explicit_solving_strategy_bfecc"
222  })");
223 
224  // Getting base class default parameters
225  const Parameters base_default_parameters = BaseType::GetDefaultParameters();
226  default_parameters.RecursivelyAddMissingParameters(base_default_parameters);
227  return default_parameters;
228  }
229 
234  virtual void AssignSettings(const Parameters ThisParameters) override
235  {
236  BaseType::AssignSettings(ThisParameters);
237  }
238 
243  static std::string Name()
244  {
245  std::stringstream s;
246  s << "explicit_solving_strategy_bfecc";
247  return s.str();
248  }
249 
253 
254 
258 
259  void Initialize() override
260  {
262  BaseType::GetModelPart().GetProcessInfo().SetValue(TIME_INTEGRATION_THETA, 0.0);
263  }
264 
268 
270  std::string Info() const override
271  {
272  std::stringstream ss;
273  ss << "ExplicitSolvingStrategyBFECC";
274  return ss.str();
275  }
276 
278  void PrintInfo(std::ostream &rOStream) const override
279  {
280  rOStream << Info();
281  }
282 
284  void PrintData(std::ostream &rOStream) const override
285  {
286  rOStream << Info();
287  }
288 
290 
291 protected:
294 
295 
299 
303 
304 
308 
310  {
311  KRATOS_TRY
312 
313  // Validate time step size
314  const double dt = BaseType::GetDeltaTime();
315  KRATOS_ERROR_IF(dt < 1.0e-12) << "ProcessInfo DELTA_TIME is close to zero." << std::endl;
316 
317  // Stash the prev step solution
318  const auto original_starting_values = ExtractSolutionStepData(1);
319 
322 
323  CorrectErrorAfterForwardsAndBackwards(original_starting_values);
325 
326  KRATOS_CATCH("");
327  }
328 
334  {
335  KRATOS_TRY
336 
337  const SizeType source = SData.direction == SubstepData::FORTH ? 1 : 0;
338  const SizeType destination = SData.direction == SubstepData::FORTH ? 0 : 1;
339 
340  // Get the required data from the explicit builder and solver
341  auto& r_explicit_bs = BaseType::GetExplicitBuilder();
342  auto& r_dof_set = r_explicit_bs.GetDofSet();
343  const SizeType dof_size = r_explicit_bs.GetEquationSystemSize();
344 
345  LocalSystemVectorType u_fixed(dof_size);
346  IndexPartition<SizeType>(r_dof_set.size()).for_each(
347  [&](const SizeType i_dof){
348  auto r_dof = *(r_dof_set.begin() + i_dof);
349  double& r_u_0 = r_dof.GetSolutionStepValue(destination);
350  if (r_dof.IsFixed()) {
351  u_fixed(i_dof) = r_u_0;
352  }
353  r_u_0 = r_dof.GetSolutionStepValue(source);
354  }
355  );
356 
357  return u_fixed;
358 
359  KRATOS_CATCH("")
360  }
361 
363  {
364  // Get the required data from the explicit builder and solver
365  auto& r_explicit_bs = BaseType::GetExplicitBuilder();
366  auto& r_dof_set = r_explicit_bs.GetDofSet();
367  const SizeType dof_size = r_explicit_bs.GetEquationSystemSize();
368 
369  LocalSystemVectorType u_copy(dof_size);
370  IndexPartition<SizeType>(r_dof_set.size()).for_each(
371  [&](const SizeType i_dof){
372  const auto it_dof = r_dof_set.cbegin() + i_dof;
373  u_copy[i_dof] = it_dof->GetSolutionStepValue(BufferPosition);
374  }
375  );
376 
377  return u_copy;
378  }
379 
384  virtual void InitializeBFECCForwardSubstep() {};
385 
390  virtual void FinalizeBFECCForwardSubstep() {};
391 
397 
402  virtual void FinalizeBFECCBackwardSubstep() {};
403 
408  virtual void InitializeBFECCFinalSubstep() {};
409 
414  virtual void FinalizeBFECCFinalSubstep() {};
415 
420  virtual void PerformSubstep(const Substep SubstepType)
421  {
422  KRATOS_TRY
423 
424  // Get the required data from the explicit builder and solver
425  auto& r_explicit_bs = BaseType::GetExplicitBuilder();
426  auto& r_dof_set = r_explicit_bs.GetDofSet();
427  const auto& r_lumped_mass_vector = r_explicit_bs.GetLumpedMassMatrixVector();
428 
429  // Get model part and information
430  const double dt = BaseType::GetDeltaTime();
431  KRATOS_ERROR_IF(dt < 1.0e-12) << "ProcessInfo DELTA_TIME is close to zero." << std::endl;
432  auto& r_model_part = BaseType::GetModelPart();
433  auto& r_process_info = r_model_part.GetProcessInfo();
434 
435  // Clone the previous step and initialize values
436  const auto substep_settings = InitializeSubstep(SubstepType);
437 
438  r_process_info.GetValue(TIME_INTEGRATION_THETA) = substep_settings.theta;
439  r_explicit_bs.BuildRHS(r_model_part);
440 
441  IndexPartition<SizeType>(r_dof_set.size()).for_each(
442  [&](SizeType i_dof){
443  auto it_dof = r_dof_set.begin() + i_dof;
444 
445  // Save current value in the corresponding vector
446  const double residual = it_dof->GetSolutionStepReactionValue();
447 
448  // Do the DOF update
449  double& r_u = it_dof->GetSolutionStepValue(0);
450  double& r_u_prev_step = it_dof->GetSolutionStepValue(1);
451 
452  if (!it_dof->IsFixed()) {
453  const double mass = r_lumped_mass_vector[i_dof];
454  r_u += substep_settings.TimeDirection() * (dt / mass) * residual;
455  } else {
456  r_u = substep_settings.theta*substep_settings.u_fixed[i_dof] + (1 - substep_settings.theta)*r_u_prev_step;
457  }
458  }
459  );
460 
461  FinalizeSubstep(SubstepType);
462 
463  KRATOS_CATCH("Substep type: " + [](Substep substep) -> std::string {
464  switch(substep) {
465  case FORWARD: return "FORWARD";
466  case BACKWARD: return "BACKWARD";
467  case FINAL: return "FINAL";
468  default: return std::to_string(static_cast<int>(substep));
469  }
470  }(SubstepType));
471  }
472 
479  {
480  auto& r_explicit_bs = BaseType::GetExplicitBuilder();
481  auto& r_dof_set = r_explicit_bs.GetDofSet();
482 
483  IndexPartition<SizeType>(r_dof_set.size()).for_each(
484  [&](const SizeType dof_index)
485  {
486  const double prev_step_solution = rPrevStepSolution[dof_index];
487  auto& r_dof = *(r_dof_set.begin() + dof_index);
488 
489  const double error = (r_dof.GetSolutionStepValue(0) - prev_step_solution)/2.0;
490 
491  r_dof.GetSolutionStepValue(1) = prev_step_solution - error;
492  // Will be copied to buffer position (0) during the last substep initialize
493 
494  }
495  );
496  }
497 
501 
502 
506 
507 
511 
512 
514 private:
517 
518 
522 
523 
527 
528 
532 
533  SubstepData InitializeSubstep(const Substep SubstepType)
534  {
535  switch(SubstepType)
536  {
537  case FORWARD:
538  {
539  SubstepData s = {0.0, SubstepData::FORTH};
540  s.u_fixed = CopySolutionStepData(s);
542  return s;
543  }
544  case BACKWARD:
545  {
546  SubstepData s = {1.0, SubstepData::BACK};
547  s.u_fixed = CopySolutionStepData(s);
549  return s;
550  }
551  case FINAL:
552  {
553  SubstepData s = {0.0, SubstepData::FORTH};
554  s.u_fixed = CopySolutionStepData(s);
556  return s;
557  }
558  default:
559  KRATOS_ERROR << "Invalid value for Substep" << std::endl;
560  }
561  }
562 
563  void FinalizeSubstep(const Substep SubstepType)
564  {
565  switch(SubstepType) {
566  case FORWARD: FinalizeBFECCForwardSubstep(); break;
567  case BACKWARD: FinalizeBFECCBackwardSubstep(); break;
568  case FINAL: FinalizeBFECCFinalSubstep(); break;
569  default: KRATOS_ERROR << "Invalid value for Substep" << std::endl;
570  }
571  }
572 
576 
577 
581 
582 
586 
587 
589 };
590 
592 
595 
597 
598 } /* namespace Kratos.*/
599 
600 #endif /* KRATOS_EXPLICIT_SOLVING_STRATEGY_BFECC defined */
void SetValue(const Variable< TDataType > &rThisVariable, TDataType const &rValue)
Sets the value for a given variable.
Definition: data_value_container.h:320
Explicit Back-and-Forth Error Compensation Correction time-integration scheme.
Definition: explicit_solving_strategy_bfecc.h:91
ExplicitSolvingStrategyBFECC(const ExplicitSolvingStrategyBFECC &Other)=delete
static std::string Name()
Returns the name of the class as used in the settings (snake_case format)
Definition: explicit_solving_strategy_bfecc.h:243
virtual void InitializeBFECCFinalSubstep()
Initialize the BFECC final substep This method is intended to implement all the operations required b...
Definition: explicit_solving_strategy_bfecc.h:408
virtual void PerformSubstep(const Substep SubstepType)
Performs a substep.
Definition: explicit_solving_strategy_bfecc.h:420
ExplicitSolvingStrategyBFECC()
Default constructor. (empty)
Definition: explicit_solving_strategy_bfecc.h:144
ExplicitSolvingStrategy< TSparseSpace, TDenseSpace > BaseType
Definition: explicit_solving_strategy_bfecc.h:104
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: explicit_solving_strategy_bfecc.h:278
std::string Info() const override
Turn back information as a string.
Definition: explicit_solving_strategy_bfecc.h:270
virtual void FinalizeBFECCBackwardSubstep()
Finalize the BFECC backward substep This method is intended to implement all the operations required ...
Definition: explicit_solving_strategy_bfecc.h:402
LocalSystemVectorType ExtractSolutionStepData(const SizeType BufferPosition) const
Definition: explicit_solving_strategy_bfecc.h:362
ExplicitSolvingStrategyBFECC(ModelPart &rModelPart, typename ExplicitBuilderType::Pointer pExplicitBuilder, bool MoveMeshFlag=false, int RebuildLevel=0)
Default constructor.
Definition: explicit_solving_strategy_bfecc.h:170
virtual void InitializeBFECCBackwardSubstep()
Initialize the BFECC backward substep This method is intended to implement all the operations require...
Definition: explicit_solving_strategy_bfecc.h:396
void Initialize() override
Initialization of member variables and prior operations.
Definition: explicit_solving_strategy_bfecc.h:259
ExplicitSolvingStrategyBFECC(ModelPart &rModelPart, Parameters ThisParameters)
Default constructor. (with parameters)
Definition: explicit_solving_strategy_bfecc.h:154
~ExplicitSolvingStrategyBFECC() override=default
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: explicit_solving_strategy_bfecc.h:284
ExplicitSolvingStrategyBFECC(ModelPart &rModelPart, bool MoveMeshFlag=false, int RebuildLevel=0)
Default constructor.
Definition: explicit_solving_strategy_bfecc.h:184
KRATOS_CLASS_POINTER_DEFINITION(ExplicitSolvingStrategyBFECC)
BaseType::ExplicitBuilderType ExplicitBuilderType
Definition: explicit_solving_strategy_bfecc.h:110
ModelPart::SizeType SizeType
Definition: explicit_solving_strategy_bfecc.h:98
LocalSystemVectorType CopySolutionStepData(const SubstepData SData)
Definition: explicit_solving_strategy_bfecc.h:333
void CorrectErrorAfterForwardsAndBackwards(const LocalSystemVectorType &rPrevStepSolution)
Definition: explicit_solving_strategy_bfecc.h:478
Substep
Definition: explicit_solving_strategy_bfecc.h:96
@ FORWARD
Definition: explicit_solving_strategy_bfecc.h:96
@ FINAL
Definition: explicit_solving_strategy_bfecc.h:96
@ BACKWARD
Definition: explicit_solving_strategy_bfecc.h:96
ExplicitSolvingStrategyBFECC< TSparseSpace, TDenseSpace > ClassType
The definition of the current class.
Definition: explicit_solving_strategy_bfecc.h:107
TDenseSpace::MatrixType LocalSystemMatrixType
Definition: explicit_solving_strategy_bfecc.h:117
void SolveWithLumpedMassMatrix() override
Calculate the explicit update This method is intended to implement the explicit update calculation No...
Definition: explicit_solving_strategy_bfecc.h:309
virtual void FinalizeBFECCFinalSubstep()
Finalize the BFECC final substep This method is intended to implement all the operations required aft...
Definition: explicit_solving_strategy_bfecc.h:414
virtual void AssignSettings(const Parameters ThisParameters) override
This method assigns settings to member variables.
Definition: explicit_solving_strategy_bfecc.h:234
virtual void FinalizeBFECCForwardSubstep()
Finalize the BFECC initial forward substep This method is intended to implement all the operations re...
Definition: explicit_solving_strategy_bfecc.h:390
virtual Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: explicit_solving_strategy_bfecc.h:217
virtual void InitializeBFECCForwardSubstep()
Initialize the BFECC initial forward substep This method is intended to implement all the operations ...
Definition: explicit_solving_strategy_bfecc.h:384
BaseType::DofType DofType
The DOF type.
Definition: explicit_solving_strategy_bfecc.h:113
TDenseSpace::VectorType LocalSystemVectorType
The local vector definition.
Definition: explicit_solving_strategy_bfecc.h:116
SolvingStrategy< TSparseSpace, TDenseSpace > SolvingStrategyType
Definition: explicit_solving_strategy_bfecc.h:101
SolvingStrategyType::Pointer Create(ModelPart &rModelPart, Parameters ThisParameters) const override
Create method.
Definition: explicit_solving_strategy_bfecc.h:197
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
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
std::size_t SizeType
Definition: model_part.h:107
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
#define KRATOS_ERROR
Definition: exception.h:161
#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
float error
Definition: PecletTest.py:102
residual
Definition: hinsberg_optimization_4.py:433
tuple const
Definition: ode_solve.py:403
Definition: explicit_solving_strategy_bfecc.h:123
LocalSystemVectorType u_fixed
Definition: explicit_solving_strategy_bfecc.h:134
double theta
Definition: explicit_solving_strategy_bfecc.h:132
SubstepData(const double Theta, const Direction Dir)
Definition: explicit_solving_strategy_bfecc.h:126
int TimeDirection() const noexcept
Definition: explicit_solving_strategy_bfecc.h:130
Direction direction
Definition: explicit_solving_strategy_bfecc.h:133
Direction
Definition: explicit_solving_strategy_bfecc.h:124
@ BACK
Definition: explicit_solving_strategy_bfecc.h:124
@ FORTH
Definition: explicit_solving_strategy_bfecc.h:124