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.
convection_diffusion_reaction_stabilization_utilities.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: Suneth Warnakulasuriya
11 //
12 
13 #if !defined(KRATOS_CONVECTION_DIFFUSION_REACTION_STABILIZATION_UTILITIES_H_INCLUDED)
14 #define KRATOS_CONVECTION_DIFFUSION_REACTION_STABILIZATION_UTILITIES_H_INCLUDED
15 
16 // System includes
17 #include <cmath>
18 
19 // External includes
20 
21 // Project includes
23 
24 // Application includes
26 
27 namespace Kratos
28 {
31 
32 namespace ConvectionDiffusionReactionStabilizationUtilities
33 {
34 inline double CalculatePsiOne(
35  const double VelocityNorm,
36  const double Tau,
37  const double DynamicReaction)
38 {
39  return VelocityNorm + Tau * VelocityNorm * DynamicReaction;
40 }
41 
42 inline double CalculatePsiTwo(
43  const double DynamicReaction,
44  const double Tau,
45  const double ElementLength)
46 {
47  return (DynamicReaction + Tau * DynamicReaction * std::abs(DynamicReaction)) *
48  std::pow(ElementLength, 2) * (1.0 / 6.0);
49 }
50 
51 template<unsigned int TDim>
53  double& rTau,
54  double& rElementLength,
55  const array_1d<double, TDim>& rVelocity,
56  const Matrix& rContravariantMetricTensor,
57  const double Reaction,
58  const double EffectiveKinematicViscosity,
59  const double Alpha,
60  const double Gamma,
61  const double DeltaTime,
62  const double DynamicTau)
63 {
64 
65  const double velocity_norm = norm_2(rVelocity);
66 
67  if (velocity_norm > 0.0) {
68  const array_1d<double, TDim> temp = prod(rContravariantMetricTensor, rVelocity);
69  rElementLength = 2.0 * velocity_norm / std::sqrt(inner_prod(rVelocity, temp));
70  } else {
71  rElementLength = 0.0;
72  for (unsigned int i = 0; i < TDim; ++i)
73  for (unsigned int j = 0; j < TDim; ++j)
74  rElementLength += rContravariantMetricTensor(i, j);
75  rElementLength = std::sqrt(1.0 / rElementLength) * 2.0;
76  }
77 
78  const double stab_convection = std::pow(2.0 * norm_2(rVelocity) / rElementLength, 2);
79  const double stab_diffusion = std::pow(
80  12.0 * EffectiveKinematicViscosity / (rElementLength * rElementLength), 2);
81  const double stab_dynamics =
82  std::pow(DynamicTau * (1 - Alpha) / (Gamma * DeltaTime), 2);
83  const double stab_reaction = std::pow(Reaction, 2);
84 
85  rTau = 1.0 / std::sqrt(stab_dynamics + stab_convection + stab_diffusion + stab_reaction);
86 }
87 
89  const double ElementLength,
90  const double Velocity,
91  const double Reaction,
92  const double EffectiveKinematicViscosity,
93  const double Alpha,
94  const double Gamma,
95  const double DeltaTime,
96  const double DynamicTau)
97 {
98  const double stab_convection = std::pow(2.0 * Velocity / ElementLength, 2);
99  const double stab_diffusion = std::pow(
100  12.0 * EffectiveKinematicViscosity / (ElementLength * ElementLength), 2);
101  const double stab_dynamics =
102  std::pow(DynamicTau * (1 - Alpha) / (Gamma * DeltaTime), 2);
103  const double stab_reaction = std::pow(Reaction, 2);
104 
105  return 1.0 / std::sqrt(stab_dynamics + stab_convection + stab_diffusion + stab_reaction);
106 }
107 
109  double& rChi,
110  double& rStreamLineDiffusionCoeff,
111  double& rCrossWindDiffusionCoeff,
112  const double VelocityMagnitude,
113  const double Tau,
114  const double EffectiveKinematicViscosity,
115  const double Reaction,
116  const double Alpha,
117  const double Gamma,
118  const double DeltaTime,
119  const double ElementLength,
120  const double DynamicTau)
121 {
122  const double reaction_dynamics =
123  Reaction + DynamicTau * (1 - Alpha) / (Gamma * DeltaTime);
124 
125  rChi = 2.0 / (std::abs(reaction_dynamics) * ElementLength + 2.0 * VelocityMagnitude);
126 
127  const double psi_one = CalculatePsiOne(VelocityMagnitude, Tau, reaction_dynamics);
128  const double psi_two = CalculatePsiTwo(reaction_dynamics, Tau, ElementLength);
129 
130  double value =
131  0.5 * std::abs(psi_one - Tau * VelocityMagnitude * reaction_dynamics) * ElementLength;
132  value -= (EffectiveKinematicViscosity + Tau * std::pow(VelocityMagnitude, 2));
133  value += psi_two;
134 
135  rStreamLineDiffusionCoeff = RansCalculationUtilities::SoftPositive(value);
136 
137  value = 0.5 * std::abs(psi_one) * ElementLength;
138  value -= EffectiveKinematicViscosity;
139  value += psi_two;
140 
141  rCrossWindDiffusionCoeff = RansCalculationUtilities::SoftPositive(value);
142 }
143 
144 template <unsigned int TSize>
146  double& rScalarCoeff,
147  BoundedMatrix<double, TSize, TSize>& rDiffusionMatrix,
148  const BoundedMatrix<double, TSize, TSize>& rInputMatrix)
149 {
150  rDiffusionMatrix.clear();
151 
152  for (unsigned int a = 0; a < TSize; ++a) {
153  for (unsigned int b = a + 1; b < TSize; ++b) {
154  rDiffusionMatrix(a, b) =
155  -std::max(std::max(rInputMatrix(a, b), rInputMatrix(b, a)), 0.0);
156  rDiffusionMatrix(b, a) = rDiffusionMatrix(a, b);
157  }
158  }
159 
160  for (unsigned int a = 0; a < TSize; ++a) {
161  double row_sum = 0.0;
162  for (unsigned int b = 0; b < TSize; ++b) {
163  // all the diagonal terms are initialized with zero
164  row_sum += rDiffusionMatrix(a, b);
165  }
166  rDiffusionMatrix(a, a) = -row_sum;
167  }
168 
169  rScalarCoeff = norm_frobenius(rDiffusionMatrix);
170 }
171 
173  const Matrix& rInputMatrix)
174 {
175  double coefficient = 0.0;
176  for (unsigned int a = 0; a < rInputMatrix.size1(); ++a) {
177  double row_sum = 0.0;
178  for (unsigned int b = 0; b < rInputMatrix.size2(); ++b) {
179  row_sum += rInputMatrix(a, b);
180  }
181  coefficient = std::max(coefficient, -row_sum);
182  }
183  return coefficient;
184 }
185 
187  Matrix& rMassMatrix,
188  const double AbsoluteReactionTerm,
189  const double Tau,
190  const Vector& rVelocityConvectiveTerms,
191  const double GaussWeight,
192  const Vector& rGaussShapeFunctions)
193 {
194  const IndexType n = rMassMatrix.size1();
195 
196  for (IndexType i = 0; i < n; ++i) {
197  for (IndexType j = 0; j < n; ++j) {
198  rMassMatrix(i, j) += GaussWeight * Tau *
199  (rVelocityConvectiveTerms[i] +
200  AbsoluteReactionTerm * rGaussShapeFunctions[i]) *
201  rGaussShapeFunctions[j];
202  }
203  }
204 }
205 
207  Matrix& rDampingMatrix,
208  const double ReactionTerm,
209  const double Tau,
210  const Vector& rVelocityConvectiveTerms,
211  const double GaussWeight,
212  const Vector& rGaussShapeFunctions)
213 {
214  const IndexType n = rDampingMatrix.size1();
215  const double s = std::abs(ReactionTerm);
216 
217  for (IndexType a = 0; a < n; ++a) {
218  for (IndexType b = 0; b < n; ++b) {
219  double value = 0.0;
220 
221  // Adding SUPG stabilization terms
222  value += Tau * (rVelocityConvectiveTerms[a] + s * rGaussShapeFunctions[a]) *
223  rVelocityConvectiveTerms[b];
224  value += Tau * (rVelocityConvectiveTerms[a] + s * rGaussShapeFunctions[a]) *
225  ReactionTerm * rGaussShapeFunctions[b];
226 
227  rDampingMatrix(a, b) += value * GaussWeight;
228  }
229  }
230 }
231 
233  Vector& rRightHandSideVector,
234  const double SourceTerm,
235  const double AbsoluteReactionTerm,
236  const double Tau,
237  const Vector& rVelocityConvectiveTerms,
238  const double GaussWeight,
239  const Vector& rGaussShapeFunctions)
240 {
241  for (IndexType a = 0; a < rRightHandSideVector.size(); ++a) {
242  double value = 0.0;
243 
244  value += rGaussShapeFunctions[a] * SourceTerm;
245 
246  // Add supg stabilization terms
247  value += (rVelocityConvectiveTerms[a] +
248  AbsoluteReactionTerm * rGaussShapeFunctions[a]) *
249  Tau * SourceTerm;
250 
251  rRightHandSideVector[a] += GaussWeight * value;
252  }
253 }
254 
255 } // namespace ConvectionDiffusionReactionStabilizationUtilities
256 
258 
259 } // namespace Kratos
260 
261 #endif
void clear()
Definition: amatrix_interface.h:284
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
void CalculateCrossWindDiffusionParameters(double &rChi, double &rStreamLineDiffusionCoeff, double &rCrossWindDiffusionCoeff, const double VelocityMagnitude, const double Tau, const double EffectiveKinematicViscosity, const double Reaction, const double Alpha, const double Gamma, const double DeltaTime, const double ElementLength, const double DynamicTau)
Definition: convection_diffusion_reaction_stabilization_utilities.h:108
void AddMassMatrixSUPGStabilizationGaussPointContributions(Matrix &rMassMatrix, const double AbsoluteReactionTerm, const double Tau, const Vector &rVelocityConvectiveTerms, const double GaussWeight, const Vector &rGaussShapeFunctions)
Definition: convection_diffusion_reaction_stabilization_utilities.h:186
void AddSourceTermWithSUPGStabilizationGaussPointContributions(Vector &rRightHandSideVector, const double SourceTerm, const double AbsoluteReactionTerm, const double Tau, const Vector &rVelocityConvectiveTerms, const double GaussWeight, const Vector &rGaussShapeFunctions)
Definition: convection_diffusion_reaction_stabilization_utilities.h:232
void AddDampingMatrixSUPGStabilizationGaussPointContributions(Matrix &rDampingMatrix, const double ReactionTerm, const double Tau, const Vector &rVelocityConvectiveTerms, const double GaussWeight, const Vector &rGaussShapeFunctions)
Definition: convection_diffusion_reaction_stabilization_utilities.h:206
void CalculateDiscreteUpwindOperator(double &rScalarCoeff, BoundedMatrix< double, TSize, TSize > &rDiffusionMatrix, const BoundedMatrix< double, TSize, TSize > &rInputMatrix)
Definition: convection_diffusion_reaction_stabilization_utilities.h:145
void CalculateStabilizationTau(double &rTau, double &rElementLength, const array_1d< double, TDim > &rVelocity, const Matrix &rContravariantMetricTensor, const double Reaction, const double EffectiveKinematicViscosity, const double Alpha, const double Gamma, const double DeltaTime, const double DynamicTau)
Definition: convection_diffusion_reaction_stabilization_utilities.h:52
double CalculatePsiOne(const double VelocityNorm, const double Tau, const double DynamicReaction)
Definition: convection_diffusion_reaction_stabilization_utilities.h:34
double CalculatePositivityPreservingMatrix(const Matrix &rInputMatrix)
Definition: convection_diffusion_reaction_stabilization_utilities.h:172
double CalculatePsiTwo(const double DynamicReaction, const double Tau, const double ElementLength)
Definition: convection_diffusion_reaction_stabilization_utilities.h:42
static double max(double a, double b)
Definition: GeometryFunctions.h:79
long double SoftPositive(const long double value)
Definition: rans_calculation_utilities.h:50
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
TExpressionType::data_type norm_2(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:625
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
TExpression1Type::data_type inner_prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:592
TExpressionType::data_type norm_frobenius(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:687
a
Definition: generate_stokes_twofluid_element.py:77
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
int n
manufactured solution and derivatives (u=0 at z=0 dudz=0 at z=domain_height)
Definition: ode_solve.py:402
def Gamma(n, j)
Definition: quadrature.py:146
def Alpha(n, j)
Definition: quadrature.py:93
int j
Definition: quadrature.py:648
float temp
Definition: rotating_cone.py:85
integer i
Definition: TensorModule.f:17