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.
bossak_relaxation_scalar_scheme.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: Jordi Cotela
11 // Suneth Warnakulasuriya
12 //
13 
14 #if !defined(KRATOS_BOSSAK_SCALAR_TRANSPORT_SCHEME_H_INCLUDED)
15 #define KRATOS_BOSSAK_SCALAR_TRANSPORT_SCHEME_H_INCLUDED
16 
17 // System includes
18 #include <sstream>
19 #include <vector>
20 
21 // External includes
22 
23 // Project includes
25 #include "includes/define.h"
26 #include "includes/model_part.h"
30 
31 // Application includes
33 
34 namespace Kratos
35 {
38 
40 
46 template <class TSparseSpace, class TDenseSpace>
48  : public SteadyScalarScheme<TSparseSpace, TDenseSpace>
49 {
50 public:
53 
55 
57 
59 
61 
63 
65 
67 
69 
73 
75 
77  const double AlphaBossak,
78  const double RelaxationFactor,
79  const Variable<double>& rScalarVariable)
80  : BaseType(RelaxationFactor),
81  mAlphaBossak(AlphaBossak),
82  mrScalarVariable(rScalarVariable)
83  {
84  // Allocate auxiliary memory.
85  const int num_threads = OpenMPUtils::GetNumThreads();
86 
87  mMassMatrix.resize(num_threads);
88  mSecondDerivativeValuesVector.resize(num_threads);
89  mSecondDerivativeValuesVectorOld.resize(num_threads);
90  }
91 
93  ~BossakRelaxationScalarScheme() override = default;
94 
98 
100  ModelPart& rModelPart,
101  SystemMatrixType& rA,
102  SystemVectorType& rDx,
103  SystemVectorType& rb) override
104  {
105  KRATOS_TRY;
106 
107  BaseType::InitializeSolutionStep(rModelPart, rA, rDx, rb);
108 
109  const double delta_time = rModelPart.GetProcessInfo()[DELTA_TIME];
110 
111  KRATOS_ERROR_IF(delta_time < std::numeric_limits<double>::epsilon())
112  << "detected delta_time = 0 in the Bossak Scheme ... "
113  "check if the time step is created correctly for "
114  "the current model part.";
115 
116  BossakRelaxationScalarScheme::CalculateBossakConstants(
117  mBossak, mAlphaBossak, delta_time);
118 
119  rModelPart.GetProcessInfo()[BOSSAK_ALPHA] = mBossak.Alpha;
120 
121  KRATOS_CATCH("");
122  }
123 
124  void Update(
125  ModelPart& rModelPart,
126  DofsArrayType& rDofSet,
127  SystemMatrixType& rA,
128  SystemVectorType& rDx,
129  SystemVectorType& rb) override
130  {
131  KRATOS_TRY;
132 
133  BaseType::Update(rModelPart, rDofSet, rA, rDx, rb);
134 
135  this->UpdateScalarRateVariables(rModelPart);
136 
137  KRATOS_CATCH("");
138  }
139 
140  int Check(ModelPart& rModelPart) override
141  {
142  KRATOS_TRY
143 
144  int value = BaseType::Check(rModelPart);
145 
146  KRATOS_ERROR_IF(!rModelPart.HasNodalSolutionStepVariable(mrScalarVariable))
147  << mrScalarVariable.Name() << " not in nodal solution step variable list of "
148  << rModelPart.Name() << ".\n";
149  KRATOS_ERROR_IF(!rModelPart.HasNodalSolutionStepVariable(mrScalarVariable.GetTimeDerivative()))
150  << mrScalarVariable.GetTimeDerivative().Name() << " not in nodal solution step variable list of "
151  << rModelPart.Name() << ".\n";
153  << mrScalarVariable.GetTimeDerivative().GetTimeDerivative().Name() << " not in nodal solution step variable list of "
154  << rModelPart.Name() << ".\n";
155 
156  return value;
157  KRATOS_CATCH("");
158  }
159 
161  Element& rElement,
162  LocalSystemMatrixType& rLHS_Contribution,
163  LocalSystemVectorType& rRHS_Contribution,
164  Element::EquationIdVectorType& rEquationIdVector,
165  const ProcessInfo& rCurrentProcessInfo) override
166  {
167  CalculateDynamicSystem<Element>(rElement, rLHS_Contribution, rRHS_Contribution,
168  rEquationIdVector, rCurrentProcessInfo);
169  }
170 
172  Element& rElement,
173  LocalSystemVectorType& rRHS_Contribution,
174  Element::EquationIdVectorType& rEquationIdVector,
175  const ProcessInfo& rCurrentProcessInfo) override
176  {
177  CalculateDynamicRHS<Element>(rElement, rRHS_Contribution,
178  rEquationIdVector, rCurrentProcessInfo);
179  }
180 
182  Condition& rCondition,
183  LocalSystemMatrixType& rLHS_Contribution,
184  LocalSystemVectorType& rRHS_Contribution,
185  Condition::EquationIdVectorType& rEquationIdVector,
186  const ProcessInfo& rCurrentProcessInfo) override
187  {
188  CalculateDynamicSystem<Condition>(rCondition, rLHS_Contribution, rRHS_Contribution,
189  rEquationIdVector, rCurrentProcessInfo);
190  }
191 
193  Condition& rCondition,
194  LocalSystemVectorType& rRHS_Contribution,
195  Element::EquationIdVectorType& rEquationIdVector,
196  const ProcessInfo& rCurrentProcessInfo) override
197  {
198  CalculateDynamicRHS<Condition>(rCondition, rRHS_Contribution,
199  rEquationIdVector, rCurrentProcessInfo);
200  }
201 
205 
207  std::string Info() const override
208  {
209  std::stringstream msg;
210  msg << "Using generic residual based bossak scalar transport scheme "
211  "with\n"
212  << " Scalar variable : " << mrScalarVariable.Name() << "\n"
213  << " Scalar rate variable : " << mrScalarVariable.GetTimeDerivative().Name() << "\n"
214  << " Relaxed scalar rate variable: "
215  << mrScalarVariable.GetTimeDerivative().GetTimeDerivative().Name() << "\n"
216  << " Relaxation factor : " << this->mRelaxationFactor;
217 
218  return msg.str();
219  }
220 
222 
223 private:
226 
227  struct BossakConstants {
228  double Alpha;
229  double Gamma;
230  double C0;
231  double C2;
232  double C3;
233  };
234 
235  std::vector<LocalSystemVectorType> mSecondDerivativeValuesVectorOld;
236  std::vector<LocalSystemVectorType> mSecondDerivativeValuesVector;
237  std::vector<LocalSystemMatrixType> mMassMatrix;
238  std::vector<LocalSystemMatrixType> mDampingMatrix;
239 
240  const double mAlphaBossak;
241 
242  const Variable<double>& mrScalarVariable;
243 
244  BossakConstants mBossak;
245 
249 
257  static void CalculateBossakConstants(
258  BossakConstants& rBossakConstants,
259  const double Alpha,
260  const double DeltaTime)
261  {
262  TimeDiscretization::Bossak bossak(Alpha, 0.25, 0.5);
263  rBossakConstants.Alpha = bossak.GetAlphaM();
264  rBossakConstants.Gamma = bossak.GetGamma();
265 
266  rBossakConstants.C0 =
267  (1.0 - rBossakConstants.Alpha) / (rBossakConstants.Gamma * DeltaTime);
268  rBossakConstants.C2 = 1.0 / (rBossakConstants.Gamma * DeltaTime);
269  rBossakConstants.C3 = (1.0 - rBossakConstants.Gamma) / rBossakConstants.Gamma;
270  }
271 
280  template <class TItemType>
281  void AddMassMatrixToRHS(
282  TItemType& rItem,
283  LocalSystemVectorType& rRHS_Contribution,
284  const int ThreadId)
285  {
286  rItem.GetSecondDerivativesVector(mSecondDerivativeValuesVector[ThreadId], 0);
287  (mSecondDerivativeValuesVector[ThreadId]) *= (1.00 - mBossak.Alpha);
288  rItem.GetSecondDerivativesVector(mSecondDerivativeValuesVectorOld[ThreadId], 1);
289  noalias(mSecondDerivativeValuesVector[ThreadId]) +=
290  mBossak.Alpha * mSecondDerivativeValuesVectorOld[ThreadId];
291 
292  noalias(rRHS_Contribution) -=
293  prod(mMassMatrix[ThreadId], mSecondDerivativeValuesVector[ThreadId]);
294  }
295 
306  template <class TItemType>
307  void CalculateDynamicSystem(
308  TItemType& rItem,
309  LocalSystemMatrixType& rLHS_Contribution,
310  LocalSystemVectorType& rRHS_Contribution,
311  typename TItemType::EquationIdVectorType& rEquationIdVector,
312  const ProcessInfo& rCurrentProcessInfo)
313  {
314  KRATOS_TRY;
315 
316  BaseType::CalculateSystemContributions(rItem, rLHS_Contribution, rRHS_Contribution,
317  rEquationIdVector, rCurrentProcessInfo);
318 
319  const int k = OpenMPUtils::ThisThread();
320 
321  rItem.CalculateMassMatrix(mMassMatrix[k], rCurrentProcessInfo);
322  // adding mass contribution to the dynamic stiffness
323  // This if block is required since this same method is used for conditions
324  // where zero sized mass matrix is returned, so to skip adding an empty mass matrix.
325  if (mMassMatrix[k].size1() != 0) // if M matrix declared
326  {
327  AddMassMatrixToRHS<TItemType>(rItem, rRHS_Contribution, k);
328 
329  noalias(rLHS_Contribution) += mBossak.C0 * mMassMatrix[k];
330  }
331 
332  KRATOS_CATCH("");
333  }
334 
344  template <class TItemType>
345  void CalculateDynamicRHS(
346  TItemType& rItem,
347  LocalSystemVectorType& rRHS_Contribution,
348  typename TItemType::EquationIdVectorType& rEquationIdVector,
349  const ProcessInfo& rCurrentProcessInfo)
350  {
351  KRATOS_TRY;
352 
353  const int k = OpenMPUtils::ThisThread();
354 
355  this->CalculateDampingSystem(rItem, this->mDampingMatrix[k], rRHS_Contribution,
356  rEquationIdVector, rCurrentProcessInfo, k);
357 
358  rItem.CalculateMassMatrix(mMassMatrix[k], rCurrentProcessInfo);
359  // adding mass contribution to the dynamic stiffness
360  // This if block is required since this same method is used for conditions
361  // where zero sized mass matrix is returned, so to skip adding an empty mass matrix.
362  if (mMassMatrix[k].size1() != 0) // if M matrix declared
363  {
364  AddMassMatrixToRHS(rItem, rRHS_Contribution, k);
365  }
366 
367  KRATOS_CATCH("");
368  }
369 
370  void UpdateScalarRateVariables(ModelPart& rModelPart)
371  {
372  block_for_each(rModelPart.Nodes(), [&](ModelPart::NodeType& rNode) {
373  double& r_current_rate = rNode.FastGetSolutionStepValue(mrScalarVariable.GetTimeDerivative());
374  const double old_rate = rNode.FastGetSolutionStepValue(mrScalarVariable.GetTimeDerivative(), 1);
375  const double current_value = rNode.FastGetSolutionStepValue(mrScalarVariable);
376  const double old_value = rNode.FastGetSolutionStepValue(mrScalarVariable, 1);
377 
378  // update scalar rate variable
379  r_current_rate = mBossak.C2 * (current_value - old_value) - mBossak.C3 * old_rate;
380 
381  // update relaxed scalar rate variable
382  rNode.FastGetSolutionStepValue(mrScalarVariable.GetTimeDerivative().GetTimeDerivative()) =
383  this->mAlphaBossak * old_rate + (1.0 - this->mAlphaBossak) * r_current_rate;
384  });
385  }
386 
388 
389 }; /* Class BossakRelaxationScalarScheme */
390 
392 
393 } /* namespace Kratos.*/
394 
395 #endif /* KRATOS_BOSSAK_SCALAR_TRANSPORT_SCHEME_H_INCLUDED defined */
A scheme for steady and dynamic equations, using Bossak time integration.
Definition: bossak_relaxation_scalar_scheme.h:49
typename BaseType::TSystemVectorType SystemVectorType
Definition: bossak_relaxation_scalar_scheme.h:60
KRATOS_CLASS_POINTER_DEFINITION(BossakRelaxationScalarScheme)
void CalculateSystemContributions(Element &rElement, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, Element::EquationIdVectorType &rEquationIdVector, const ProcessInfo &rCurrentProcessInfo) override
Definition: bossak_relaxation_scalar_scheme.h:160
typename BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: bossak_relaxation_scalar_scheme.h:64
void CalculateRHSContribution(Condition &rCondition, LocalSystemVectorType &rRHS_Contribution, Element::EquationIdVectorType &rEquationIdVector, const ProcessInfo &rCurrentProcessInfo) override
Definition: bossak_relaxation_scalar_scheme.h:192
void CalculateSystemContributions(Condition &rCondition, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, Condition::EquationIdVectorType &rEquationIdVector, const ProcessInfo &rCurrentProcessInfo) override
Definition: bossak_relaxation_scalar_scheme.h:181
void InitializeSolutionStep(ModelPart &rModelPart, SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb) override
Definition: bossak_relaxation_scalar_scheme.h:99
std::string Info() const override
Turn back information as a string.
Definition: bossak_relaxation_scalar_scheme.h:207
void Update(ModelPart &rModelPart, DofsArrayType &rDofSet, SystemMatrixType &rA, SystemVectorType &rDx, SystemVectorType &rb) override
Definition: bossak_relaxation_scalar_scheme.h:124
typename BaseType::TSystemMatrixType SystemMatrixType
Definition: bossak_relaxation_scalar_scheme.h:58
BossakRelaxationScalarScheme(const double AlphaBossak, const double RelaxationFactor, const Variable< double > &rScalarVariable)
Constructor.
Definition: bossak_relaxation_scalar_scheme.h:76
~BossakRelaxationScalarScheme() override=default
Destructor.
typename BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: bossak_relaxation_scalar_scheme.h:62
void CalculateRHSContribution(Element &rElement, LocalSystemVectorType &rRHS_Contribution, Element::EquationIdVectorType &rEquationIdVector, const ProcessInfo &rCurrentProcessInfo) override
Definition: bossak_relaxation_scalar_scheme.h:171
int Check(ModelPart &rModelPart) override
Definition: bossak_relaxation_scalar_scheme.h:140
Base class for all Conditions.
Definition: condition.h:59
std::vector< std::size_t > EquationIdVectorType
Definition: condition.h:98
Base class for all Elements.
Definition: element.h:60
std::vector< std::size_t > EquationIdVectorType
Definition: element.h:98
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
std::string & Name()
Definition: model_part.h:1811
bool HasNodalSolutionStepVariable(VariableData const &ThisVariable) const
Definition: model_part.h:544
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
Node NodeType
Definition: model_part.h:117
This class defines the node.
Definition: node.h:65
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
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
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
typename TDenseSpace::VectorType LocalSystemVectorType
Local system vector type definition.
Definition: scheme.h:80
virtual void InitializeSolutionStep(ModelPart &rModelPart, TSystemMatrixType &A, TSystemVectorType &Dx, TSystemVectorType &b)
Function called once at the beginning of each solution step.
Definition: scheme.h:272
typename TDenseSpace::MatrixType LocalSystemMatrixType
Local system matrix type definition.
Definition: scheme.h:77
virtual int Check(const ModelPart &rModelPart) const
This function is designed to be called once to perform all the checks needed on the input provided....
Definition: scheme.h:508
Definition: steady_scalar_scheme.h:37
typename BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: steady_scalar_scheme.h:52
void CalculateSystemContributions(Element &rElement, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, Element::EquationIdVectorType &rEquationIdVector, const ProcessInfo &rCurrentProcessInfo) override
This function is designed to be called in the builder and solver to introduce the selected time integ...
Definition: steady_scalar_scheme.h:95
typename BaseType::TSystemVectorType TSystemVectorType
Definition: steady_scalar_scheme.h:50
typename BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: steady_scalar_scheme.h:54
typename BaseType::DofsArrayType DofsArrayType
Definition: steady_scalar_scheme.h:46
void Update(ModelPart &rModelPart, DofsArrayType &rDofSet, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb) override
Performing the update of the solution.
Definition: steady_scalar_scheme.h:76
void CalculateDampingSystem(TItemType &rItem, LocalSystemMatrixType &rLHS_Contribution, LocalSystemVectorType &rRHS_Contribution, typename TItemType::EquationIdVectorType &rEquationIdVector, const ProcessInfo &rCurrentProcessInfo, const int ThreadId)
Definition: steady_scalar_scheme.h:198
typename BaseType::TSystemMatrixType TSystemMatrixType
Definition: steady_scalar_scheme.h:48
double mRelaxationFactor
Definition: steady_scalar_scheme.h:191
const std::string & Name() const
Definition: variable_data.h:201
const VariableType & GetTimeDerivative() const
This method returns the time derivative variable.
Definition: variable.h:336
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void block_for_each(TIterator itBegin, TIterator itEnd, TFunction &&rFunction)
Execute a functor on all items of a range in parallel.
Definition: parallel_utilities.h:299
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
delta_time
Definition: generate_frictional_mortar_condition.py:130
def Gamma(n, j)
Definition: quadrature.py:146
def Alpha(n, j)
Definition: quadrature.py:93
int k
Definition: quadrature.py:595
def num_threads
Definition: script.py:75