https://mooseframework.inl.gov
INSFVTKESourceSink.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "INSFVTKESourceSink.h"
11 #include "NonlinearSystemBase.h"
12 #include "NavierStokesMethods.h"
13 #include "libmesh/nonlinear_solver.h"
14 
15 registerMooseObject("NavierStokesApp", INSFVTKESourceSink);
16 
19 {
21  params.addClassDescription("Elemental kernel to compute the production and destruction "
22  " terms of turbulent kinetic energy (TKE).");
23  params.addRequiredParam<MooseFunctorName>("u", "The velocity in the x direction.");
24  params.addParam<MooseFunctorName>("v", "The velocity in the y direction.");
25  params.addParam<MooseFunctorName>("w", "The velocity in the z direction.");
26  params.addRequiredParam<MooseFunctorName>(NS::TKED,
27  "Coupled turbulent kinetic energy dissipation rate.");
28  params.addRequiredParam<MooseFunctorName>(NS::density, "fluid density");
29  params.addRequiredParam<MooseFunctorName>(NS::mu, "Dynamic viscosity.");
30  params.addRequiredParam<MooseFunctorName>(NS::mu_t, "Turbulent viscosity.");
31  params.addParam<std::vector<BoundaryName>>(
32  "walls", {}, "Boundaries that correspond to solid walls.");
33  params.addParam<bool>(
34  "linearized_model",
35  true,
36  "Boolean to determine if the problem should be use in a linear or nonlinear solve.");
37  MooseEnum wall_treatment("eq_newton eq_incremental eq_linearized neq", "neq");
38  params.addParam<MooseEnum>("wall_treatment",
39  wall_treatment,
40  "The method used for computing the wall functions "
41  "'eq_newton', 'eq_incremental', 'eq_linearized', 'neq'");
42  params.addParam<Real>("C_mu", 0.09, "Coupled turbulent kinetic energy closure.");
43  params.addParam<Real>("C_pl", 10.0, "Production Limiter Constant Multiplier.");
44  params.set<unsigned short>("ghost_layers") = 2;
45  params.addParam<bool>("newton_solve", false, "Whether a Newton nonlinear solve is being used");
46  params.addParamNamesToGroup("newton_solve", "Advanced");
47 
48  return params;
49 }
50 
52  : FVElementalKernel(params),
53  _dim(_subproblem.mesh().dimension()),
54  _u_var(getFunctor<ADReal>("u")),
55  _v_var(params.isParamValid("v") ? &(getFunctor<ADReal>("v")) : nullptr),
56  _w_var(params.isParamValid("w") ? &(getFunctor<ADReal>("w")) : nullptr),
57  _epsilon(getFunctor<ADReal>(NS::TKED)),
58  _rho(getFunctor<ADReal>(NS::density)),
59  _mu(getFunctor<ADReal>(NS::mu)),
60  _mu_t(getFunctor<ADReal>(NS::mu_t)),
61  _wall_boundary_names(getParam<std::vector<BoundaryName>>("walls")),
62  _linearized_model(getParam<bool>("linearized_model")),
63  _wall_treatment(getParam<MooseEnum>("wall_treatment").getEnum<NS::WallTreatmentEnum>()),
64  _C_mu(getParam<Real>("C_mu")),
65  _C_pl(getParam<Real>("C_pl")),
66  _newton_solve(getParam<bool>("newton_solve"))
67 {
68  if (_dim >= 2 && !_v_var)
69  paramError("v", "In two or more dimensions, the v velocity must be supplied!");
70 
71  if (_dim >= 3 && !_w_var)
72  paramError("w", "In three or more dimensions, the w velocity must be supplied!");
73 }
74 
75 void
77 {
82 }
83 
84 ADReal
86 {
87  using std::max, std::sqrt, std::pow, std::min;
88 
89  ADReal residual = 0.0;
90  ADReal production = 0.0;
91  ADReal destruction = 0.0;
92 
93  const auto state = determineState();
94  const auto elem_arg = makeElemArg(_current_elem);
95  const auto old_state =
97  const auto rho = _rho(elem_arg, state);
98  const auto mu = _mu(elem_arg, state);
99  // To prevent negative values & preserve sparsity pattern
100  auto TKE = _newton_solve ? max(_var(elem_arg, old_state), ADReal(0) * _var(elem_arg, old_state))
101  : _var(elem_arg, old_state);
102  // Prevent computation of sqrt(0) with undefined automatic derivatives
103  // This is not needed for segregated solves, as TKE has minimum bound in the solver
104  if (_newton_solve)
105  TKE = max(TKE, 1e-10);
106 
107  if (_wall_bounded.find(_current_elem) != _wall_bounded.end())
108  {
109  std::vector<ADReal> y_plus_vec, velocity_grad_norm_vec;
110 
111  Real tot_weight = 0.0;
112 
113  ADRealVectorValue velocity(_u_var(elem_arg, state));
114  if (_v_var)
115  velocity(1) = (*_v_var)(elem_arg, state);
116  if (_w_var)
117  velocity(2) = (*_w_var)(elem_arg, state);
118 
119  const auto & face_info_vec = libmesh_map_find(_face_infos, _current_elem);
120  const auto & distance_vec = libmesh_map_find(_dist, _current_elem);
121 
122  for (unsigned int i = 0; i < distance_vec.size(); i++)
123  {
124  const auto parallel_speed = NS::computeSpeed<ADReal>(
125  velocity - velocity * face_info_vec[i]->normal() * face_info_vec[i]->normal());
126  const auto distance = distance_vec[i];
127 
128  ADReal y_plus;
129  if (_wall_treatment == NS::WallTreatmentEnum::NEQ) // Non-equilibrium / Non-iterative
130  y_plus = distance * sqrt(sqrt(_C_mu) * TKE) * rho / mu;
131  else // Equilibrium / Iterative
132  y_plus = NS::findyPlus<ADReal>(mu, rho, max(parallel_speed, 1e-10), distance);
133 
134  y_plus_vec.push_back(y_plus);
135 
136  const ADReal velocity_grad_norm = parallel_speed / distance;
137 
139  // More complete expansion for velocity gradient. Leave commented for now.
140  // Will be useful later when doing two-phase or compressible flow
141  // ADReal velocity_grad_norm_sq =
142  // Utility::pow<2>(_u_var->gradient(elem_arg, state) *
143  // _normal[_current_elem][i]);
144  // if (_dim >= 2)
145  // velocity_grad_norm_sq +=
146  // Utility::pow<2>(_v_var->gradient(elem_arg, state) *
147  // _normal[_current_elem][i]);
148  // if (_dim >= 3)
149  // velocity_grad_norm_sq +=
150  // Utility::pow<2>(_w_var->gradient(elem_arg, state) *
151  // _normal[_current_elem][i]);
152  // ADReal velocity_grad_norm = sqrt(velocity_grad_norm_sq);
153 
154  velocity_grad_norm_vec.push_back(velocity_grad_norm);
155 
156  tot_weight += 1.0;
157  }
158 
159  for (unsigned int i = 0; i < y_plus_vec.size(); i++)
160  {
161  const auto y_plus = y_plus_vec[i];
162 
163  const auto fi = face_info_vec[i];
164  const bool defined_on_elem_side = _var.hasFaceSide(*fi, true);
165  const Elem * const loc_elem = defined_on_elem_side ? &fi->elem() : fi->neighborPtr();
166  const Moose::FaceArg facearg = {
167  fi, Moose::FV::LimiterType::CentralDifference, false, false, loc_elem, nullptr};
168  const ADReal wall_mut = _mu_t(facearg, state);
169  const ADReal wall_mu = _mu(facearg, state);
170 
171  const auto destruction_visc = 2.0 * wall_mu / Utility::pow<2>(distance_vec[i]) / tot_weight;
172  const auto destruction_log = pow(_C_mu, 0.75) * rho * pow(TKE, 0.5) /
173  (NS::von_karman_constant * distance_vec[i]) / tot_weight;
174  const auto tau_w = (wall_mut + wall_mu) * velocity_grad_norm_vec[i];
175 
176  // Additional 0-value terms to make sure new derivative entries are not added during the solve
177  if (y_plus < 11.25)
178  {
179  destruction += destruction_visc;
180  if (_newton_solve)
181  destruction += 0 * destruction_log + 0 * tau_w;
182  }
183  else
184  {
185  destruction += destruction_log;
186  if (_newton_solve)
187  destruction += 0 * destruction_visc;
188  production += tau_w * pow(_C_mu, 0.25) / sqrt(TKE) /
189  (NS::von_karman_constant * distance_vec[i]) / tot_weight;
190  }
191  }
192 
193  residual = (destruction - production) * _var(elem_arg, state);
194  // Additional 0-value term to make sure new derivative entries are not added during the solve
195  if (_newton_solve)
196  residual += 0 * _epsilon(elem_arg, old_state);
197  }
198  else
199  {
200  const auto & grad_u = _u_var.gradient(elem_arg, state);
201  const auto Sij_xx = 2.0 * grad_u(0);
202  ADReal Sij_xy = 0.0;
203  ADReal Sij_xz = 0.0;
204  ADReal Sij_yy = 0.0;
205  ADReal Sij_yz = 0.0;
206  ADReal Sij_zz = 0.0;
207 
208  const auto grad_xx = grad_u(0);
209  ADReal grad_xy = 0.0;
210  ADReal grad_xz = 0.0;
211  ADReal grad_yx = 0.0;
212  ADReal grad_yy = 0.0;
213  ADReal grad_yz = 0.0;
214  ADReal grad_zx = 0.0;
215  ADReal grad_zy = 0.0;
216  ADReal grad_zz = 0.0;
217 
218  auto trace = Sij_xx / 3.0;
219 
220  if (_dim >= 2)
221  {
222  const auto & grad_v = (*_v_var).gradient(elem_arg, state);
223  Sij_xy = grad_u(1) + grad_v(0);
224  Sij_yy = 2.0 * grad_v(1);
225 
226  grad_xy = grad_u(1);
227  grad_yx = grad_v(0);
228  grad_yy = grad_v(1);
229 
230  trace += Sij_yy / 3.0;
231 
232  if (_dim >= 3)
233  {
234  const auto & grad_w = (*_w_var).gradient(elem_arg, state);
235 
236  Sij_xz = grad_u(2) + grad_w(0);
237  Sij_yz = grad_v(2) + grad_w(1);
238  Sij_zz = 2.0 * grad_w(2);
239 
240  grad_xz = grad_u(2);
241  grad_yz = grad_v(2);
242  grad_zx = grad_w(0);
243  grad_zy = grad_w(1);
244  grad_zz = grad_w(2);
245 
246  trace += Sij_zz / 3.0;
247  }
248  }
249 
250  const auto symmetric_strain_tensor_sq_norm =
251  (Sij_xx - trace) * grad_xx + Sij_xy * grad_xy + Sij_xz * grad_xz + Sij_xy * grad_yx +
252  (Sij_yy - trace) * grad_yy + Sij_yz * grad_yz + Sij_xz * grad_zx + Sij_yz * grad_zy +
253  (Sij_zz - trace) * grad_zz;
254 
255  production = _mu_t(elem_arg, state) * symmetric_strain_tensor_sq_norm;
256 
257  const auto tke_old_raw = raw_value(TKE);
258  const auto epsilon_old = _epsilon(elem_arg, old_state);
259 
260  if (MooseUtils::absoluteFuzzyEqual(tke_old_raw, 0))
261  destruction = rho * epsilon_old;
262  else
263  destruction = rho * _var(elem_arg, state) / tke_old_raw * raw_value(epsilon_old);
264 
265  // k-Production limiter (needed for flows with stagnation zones)
266  const ADReal production_limit =
267  _C_pl * rho * (_newton_solve ? max(epsilon_old, ADReal(0)) : epsilon_old);
268 
269  // Apply production limiter
270  production = min(production, production_limit);
271 
272  residual = destruction - production;
273 
274  // Additional 0-value terms to make sure new derivative entries are not added during the solve
275  if (_newton_solve)
276  residual += 0 * _epsilon(elem_arg, state);
277  }
278 
279  return residual;
280 }
const bool _newton_solve
For Newton solves we want to add extra zero-valued terms regardless of y-plus to avoid sparsity patte...
static constexpr Real von_karman_constant
Definition: NS.h:201
const bool _linearized_model
Linearized model?
static const std::string mu_t
Definition: NS.h:125
const Moose::Functor< ADReal > & _mu_t
Turbulent dynamic viscosity.
const Moose::Functor< ADReal > * _w_var
z-velocity
const Real _C_mu
C_mu constant.
void paramError(const std::string &param, Args... args) const
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
const std::vector< BoundaryName > & _wall_boundary_names
Wall boundaries.
ADReal computeQpResidual() override
Moose::StateArg determineState() const
T & set(const std::string &name, bool quiet_mode=false)
Real trace(const RealTensor &A, const unsigned int &dim)
MeshBase & mesh
static const std::string density
Definition: NS.h:33
auto raw_value(const Eigen::Map< T > &in)
static const std::string TKE
Definition: NS.h:176
WallTreatmentEnum
Wall treatment options.
Definition: NS.h:182
const Moose::Functor< ADReal > & _u_var
x-velocity
void getWallBoundedElements(const std::vector< BoundaryName > &wall_boundary_name, const FEProblemBase &fe_problem, const SubProblem &subproblem, const std::set< SubdomainID > &block_ids, std::unordered_set< const Elem *> &wall_bounded)
Map marking wall bounded elements The map passed in wall_bounded_map gets cleared and re-populated...
virtual const std::set< SubdomainID > & blockIDs() const
const Moose::Functor< ADReal > & _mu
Dynamic viscosity.
const Moose::Functor< ADReal > & _rho
Density.
DualNumber< Real, DNDerivativeType, true > ADReal
std::map< const Elem *, std::vector< Real > > _dist
Real distance(const Point &p)
void addRequiredParam(const std::string &name, const std::string &doc_string)
auto max(const L &left, const R &right)
Moose::ElemArg makeElemArg(const Elem *elem, bool correct_skewnewss=false) const
const Elem *const & _current_elem
static const std::string mu
Definition: NS.h:123
SubProblem & _subproblem
NS::WallTreatmentEnum _wall_treatment
Method used for wall treatment.
static InputParameters validParams()
FEProblemBase & _fe_problem
const Moose::Functor< ADReal > * _v_var
y-velocity
static InputParameters validParams()
void getWallDistance(const std::vector< BoundaryName > &wall_boundary_name, const FEProblemBase &fe_problem, const SubProblem &subproblem, const std::set< SubdomainID > &block_ids, std::map< const Elem *, std::vector< Real >> &dist_map)
Map storing wall ditance for near-wall marked elements The map passed in dist_map gets cleared and re...
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
const unsigned int _dim
The dimension of the domain.
std::unordered_set< const Elem * > _wall_bounded
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
template ADReal computeSpeed< ADReal >(const libMesh::VectorValue< ADReal > &velocity)
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
template ADReal findyPlus< ADReal >(const ADReal &mu, const ADReal &rho, const ADReal &u, Real dist)
static const std::string TKED
Definition: NS.h:177
registerMooseObject("NavierStokesApp", INSFVTKESourceSink)
void addClassDescription(const std::string &doc_string)
void getElementFaceArgs(const std::vector< BoundaryName > &wall_boundary_name, const FEProblemBase &fe_problem, const SubProblem &subproblem, const std::set< SubdomainID > &block_ids, std::map< const Elem *, std::vector< const FaceInfo *>> &face_info_map)
Map storing face arguments to wall bounded faces The map passed in face_info_map gets cleared and re-...
static const std::string velocity
Definition: NS.h:45
Computes source the sink terms for the turbulent kinetic energy.
virtual void initialSetup() override
const Moose::Functor< ADReal > & _epsilon
epsilon - dissipation rate of TKE
auto min(const L &left, const R &right)
std::map< const Elem *, std::vector< const FaceInfo * > > _face_infos
MooseUnits pow(const MooseUnits &, int)
MooseVariableFV< Real > & _var
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
INSFVTKESourceSink(const InputParameters &parameters)
virtual bool hasFaceSide(const FaceInfo &fi, const bool fi_elem_side) const override