https://mooseframework.inl.gov
INSFVTKEDSourceSink.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 "INSFVTKEDSourceSink.h"
11 #include "NonlinearSystemBase.h"
12 #include "NavierStokesMethods.h"
13 #include "libmesh/nonlinear_solver.h"
14 
15 registerMooseObject("NavierStokesApp", INSFVTKEDSourceSink);
16 
19 {
21  params.addClassDescription("Elemental kernel to compute the production and destruction "
22  " terms of turbulent kinetic energy dissipation (TKED).");
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>("k", "Coupled turbulent kinetic energy.");
27  params.deprecateParam("k", NS::TKE, "01/01/2025");
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 used 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>("C1_eps", 1.44, "First epsilon coefficient");
43  params.addParam<Real>("C2_eps", 1.92, "Second epsilon coefficient");
44  params.addParam<Real>("C_mu", 0.09, "Coupled turbulent kinetic energy closure.");
45  params.addParam<Real>("C_pl", 10.0, "Production limiter constant multiplier.");
46  params.set<unsigned short>("ghost_layers") = 2;
47  params.addParam<bool>("newton_solve", false, "Whether a Newton nonlinear solve is being used");
48  params.addParamNamesToGroup("newton_solve", "Advanced");
49  return params;
50 }
51 
53  : FVElementalKernel(params),
54  _dim(_subproblem.mesh().dimension()),
55  _u_var(getFunctor<ADReal>("u")),
56  _v_var(params.isParamValid("v") ? &(getFunctor<ADReal>("v")) : nullptr),
57  _w_var(params.isParamValid("w") ? &(getFunctor<ADReal>("w")) : nullptr),
58  _k(getFunctor<ADReal>(NS::TKE)),
59  _rho(getFunctor<ADReal>(NS::density)),
60  _mu(getFunctor<ADReal>(NS::mu)),
61  _mu_t(getFunctor<ADReal>(NS::mu_t)),
62  _wall_boundary_names(getParam<std::vector<BoundaryName>>("walls")),
63  _linearized_model(getParam<bool>("linearized_model")),
64  _wall_treatment(getParam<MooseEnum>("wall_treatment").getEnum<NS::WallTreatmentEnum>()),
65  _C1_eps(getParam<Real>("C1_eps")),
66  _C2_eps(getParam<Real>("C2_eps")),
67  _C_mu(getParam<Real>("C_mu")),
68  _C_pl(getParam<Real>("C_pl")),
69  _newton_solve(getParam<bool>("newton_solve"))
70 {
71  if (_dim >= 2 && !_v_var)
72  paramError("v", "In two or more dimensions, the v velocity must be supplied!");
73 
74  if (_dim >= 3 && !_w_var)
75  paramError("w", "In three or more dimensions, the w velocity must be supplied!");
76 }
77 
78 void
80 {
85 }
86 
87 ADReal
89 {
90  ADReal residual = 0.0;
91  ADReal production = 0.0;
92  ADReal destruction = 0.0;
93  const auto elem_arg = makeElemArg(_current_elem);
94  const auto state = determineState();
95  const auto old_state =
97  const auto mu = _mu(elem_arg, state);
98  const auto rho = _rho(elem_arg, state);
99  const auto TKE_old =
100  _newton_solve ? std::max(_k(elem_arg, old_state), 1e-10) : _k(elem_arg, old_state);
101  ADReal y_plus;
102 
103  if (_wall_bounded.find(_current_elem) != _wall_bounded.end())
104  {
105  std::vector<ADReal> y_plus_vec;
106 
107  Real tot_weight = 0.0;
108 
109  ADRealVectorValue velocity(_u_var(elem_arg, state));
110  if (_v_var)
111  velocity(1) = (*_v_var)(elem_arg, state);
112  if (_w_var)
113  velocity(2) = (*_w_var)(elem_arg, state);
114 
115  const auto & face_info_vec = libmesh_map_find(_face_infos, _current_elem);
116  const auto & distance_vec = libmesh_map_find(_dist, _current_elem);
117  mooseAssert(distance_vec.size(), "Should have found a distance vector");
118  mooseAssert(distance_vec.size() == face_info_vec.size(),
119  "Should be as many distance vectors as face info vectors");
120 
121  for (unsigned int i = 0; i < distance_vec.size(); i++)
122  {
123  const auto distance = distance_vec[i];
124  mooseAssert(distance > 0, "Should be at a non-zero distance");
125 
126  if (_wall_treatment == NS::WallTreatmentEnum::NEQ) // Non-equilibrium / Non-iterative
127  y_plus = distance * std::sqrt(std::sqrt(_C_mu) * TKE_old) * rho / mu;
128  else
129  {
130  // Equilibrium / Iterative
131  const auto parallel_speed = NS::computeSpeed(
132  velocity - velocity * face_info_vec[i]->normal() * face_info_vec[i]->normal());
133 
134  y_plus = NS::findyPlus(mu, rho, std::max(parallel_speed, 1e-10), distance);
135  }
136 
137  y_plus_vec.push_back(y_plus);
138 
139  tot_weight += 1.0;
140  }
141 
142  for (const auto i : index_range(y_plus_vec))
143  {
144  const auto y_plus = y_plus_vec[i];
145 
146  if (y_plus < 11.25)
147  {
148  const auto fi = face_info_vec[i];
149  const bool defined_on_elem_side = _var.hasFaceSide(*fi, true);
150  const Elem * const loc_elem = defined_on_elem_side ? &fi->elem() : fi->neighborPtr();
151  const Moose::FaceArg facearg = {
152  fi, Moose::FV::LimiterType::CentralDifference, false, false, loc_elem, nullptr};
153  destruction += 2.0 * TKE_old * _mu(facearg, state) / rho /
154  Utility::pow<2>(distance_vec[i]) / tot_weight;
155  }
156  else
157  destruction += std::pow(_C_mu, 0.75) * std::pow(TKE_old, 1.5) /
158  (NS::von_karman_constant * distance_vec[i]) / tot_weight;
159  }
160 
161  residual = _var(makeElemArg(_current_elem), state) - destruction;
162  }
163  else
164  {
165  const auto & grad_u = _u_var.gradient(elem_arg, state);
166  const auto Sij_xx = 2.0 * grad_u(0);
167  ADReal Sij_xy = 0.0;
168  ADReal Sij_xz = 0.0;
169  ADReal Sij_yy = 0.0;
170  ADReal Sij_yz = 0.0;
171  ADReal Sij_zz = 0.0;
172 
173  const auto grad_xx = grad_u(0);
174  ADReal grad_xy = 0.0;
175  ADReal grad_xz = 0.0;
176  ADReal grad_yx = 0.0;
177  ADReal grad_yy = 0.0;
178  ADReal grad_yz = 0.0;
179  ADReal grad_zx = 0.0;
180  ADReal grad_zy = 0.0;
181  ADReal grad_zz = 0.0;
182 
183  auto trace = Sij_xx / 3.0;
184 
185  if (_dim >= 2)
186  {
187  const auto & grad_v = (*_v_var).gradient(elem_arg, state);
188  Sij_xy = grad_u(1) + grad_v(0);
189  Sij_yy = 2.0 * grad_v(1);
190 
191  grad_xy = grad_u(1);
192  grad_yx = grad_v(0);
193  grad_yy = grad_v(1);
194 
195  trace += Sij_yy / 3.0;
196 
197  if (_dim >= 3)
198  {
199  const auto & grad_w = (*_w_var).gradient(elem_arg, state);
200 
201  Sij_xz = grad_u(2) + grad_w(0);
202  Sij_yz = grad_v(2) + grad_w(1);
203  Sij_zz = 2.0 * grad_w(2);
204 
205  grad_xz = grad_u(2);
206  grad_yz = grad_v(2);
207  grad_zx = grad_w(0);
208  grad_zy = grad_w(1);
209  grad_zz = grad_w(2);
210 
211  trace += Sij_zz / 3.0;
212  }
213  }
214 
215  const auto symmetric_strain_tensor_sq_norm =
216  (Sij_xx - trace) * grad_xx + Sij_xy * grad_xy + Sij_xz * grad_xz + Sij_xy * grad_yx +
217  (Sij_yy - trace) * grad_yy + Sij_yz * grad_yz + Sij_xz * grad_zx + Sij_yz * grad_zy +
218  (Sij_zz - trace) * grad_zz;
219 
220  ADReal production_k = _mu_t(elem_arg, state) * symmetric_strain_tensor_sq_norm;
221  // Compute production limiter (needed for flows with stagnation zones)
222  const auto eps_old =
223  _newton_solve ? std::max(_var(elem_arg, old_state), 1e-10) : _var(elem_arg, old_state);
224  const ADReal production_limit = _C_pl * rho * eps_old;
225  // Apply production limiter
226  production_k = std::min(production_k, production_limit);
227 
228  const auto time_scale = raw_value(TKE_old) / raw_value(eps_old);
229  production = _C1_eps * production_k / time_scale;
230  destruction = _C2_eps * rho * _var(elem_arg, state) / time_scale;
231 
232  residual = destruction - production;
233  }
234 
235  return residual;
236 }
std::map< const Elem *, bool > _wall_bounded
Maps for wall treatment.
const Moose::Functor< ADReal > * _w_var
z-velocity
static constexpr Real von_karman_constant
Definition: NS.h:191
static const std::string mu_t
Definition: NS.h:125
const std::vector< BoundaryName > & _wall_boundary_names
Wall boundaries.
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void getWallBoundedElements(const std::vector< BoundaryName > &wall_boundary_name, const FEProblemBase &fe_problem, const SubProblem &subproblem, const std::set< SubdomainID > &block_ids, std::map< const Elem *, bool > &wall_bounded_map)
Map marking wall bounded elements The map passed in wall_bounded_map gets cleared and re-populated...
static InputParameters validParams()
Moose::StateArg determineState() const
const Moose::Functor< ADReal > & _k
Turbulent kinetic energy.
T & set(const std::string &name, bool quiet_mode=false)
NS::WallTreatmentEnum _wall_treatment
Method used for wall treatment.
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 > * _v_var
y-velocity
virtual const std::set< SubdomainID > & blockIDs() const
Computes the source and sink terms for the turbulent kinetic energy dissipation rate.
DualNumber< Real, DNDerivativeType, true > ADReal
ADReal computeQpResidual() override
Real distance(const Point &p)
void addRequiredParam(const std::string &name, const std::string &doc_string)
const Real _C2_eps
Value of the second epsilon closure coefficient.
Moose::ElemArg makeElemArg(const Elem *elem, bool correct_skewnewss=false) const
const Real _C1_eps
Value of the first epsilon closure coefficient.
const Moose::Functor< ADReal > & _rho
Density.
void deprecateParam(const std::string &old_name, const std::string &new_name, const std::string &removal_date)
registerMooseObject("NavierStokesApp", INSFVTKEDSourceSink)
const Elem *const & _current_elem
static const std::string mu
Definition: NS.h:123
SubProblem & _subproblem
INSFVTKEDSourceSink(const InputParameters &parameters)
static InputParameters validParams()
ADReal findyPlus(const ADReal &mu, const ADReal &rho, const ADReal &u, Real dist)
Finds the non-dimensional wall distance normalized with the friction velocity Implements a fixed-poin...
std::map< const Elem *, std::vector< Real > > _dist
FEProblemBase & _fe_problem
std::map< const Elem *, std::vector< const FaceInfo * > > _face_infos
void paramError(const std::string &param, Args... args) const
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...
const bool _newton_solve
Whether a nonlinear Newton-like solver is being used (as opposed to a linearized solver) ...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const unsigned int _dim
The dimension of the simulation.
const Real _C_mu
C_mu constant.
const bool _linearized_model
If the user wants to use the linearized model.
const Moose::Functor< ADReal > & _mu
Dynamic viscosity.
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-...
const Moose::Functor< ADReal > & _mu_t
Turbulent dynamic viscosity.
static const std::string velocity
Definition: NS.h:45
ADReal computeSpeed(const ADRealVectorValue &velocity)
Compute the speed (velocity norm) given the supplied velocity.
virtual void initialSetup() override
MooseUnits pow(const MooseUnits &, int)
auto index_range(const T &sizable)
const Moose::Functor< ADReal > & _u_var
x-velocity
MooseVariableFV< Real > & _var
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
virtual bool hasFaceSide(const FaceInfo &fi, const bool fi_elem_side) const override