https://mooseframework.inl.gov
Loading...
Searching...
No Matches
NavierStokesMethods.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 "NavierStokesMethods.h"
11#include "MooseError.h"
12#include "libmesh/vector_value.h"
13#include "NS.h"
14
15namespace NS
16{
17int
18delta(unsigned int i, unsigned int j)
19{
20 if (i == j)
21 return 1;
22 else
23 return 0;
24}
25
26int
27computeSign(const Real & a)
28{
29 return a > 0 ? 1 : (a < 0 ? -1 : 0);
30}
31
32unsigned int
33getIndex(const Real & p, const std::vector<Real> & bounds)
34{
35 if (p < bounds.front() || p > bounds.back())
36 mooseError("Point exceeds bounds of domain!");
37
38 for (unsigned int i = 1; i < bounds.size(); ++i)
39 if (p <= bounds[i])
40 return i - 1;
41
42 return bounds.size() - 2;
43}
44
45Real
47 const Real & Re, const Real & rho, const Real & mu, const Real & drho, const Real & dmu)
48{
49 return Re * (drho / std::max(rho, 1e-6) - dmu / std::max(mu, 1e-8));
50}
51
52Real
54 const Real & cp,
55 const Real & k,
56 const Real & dmu,
57 const Real & dcp,
58 const Real & dk)
59{
60 return (k * (mu * dcp + cp * dmu) - mu * cp * dk) / std::max(k * k, 1e-8);
61}
62
63template <typename T>
64T
65findUStar(const T & mu, const T & rho, const T & u, const Real dist)
66{
67 using std::log, std::sqrt, std::pow, std::max, std::abs;
68
69 // usually takes about 3-4 iterations
70 constexpr int MAX_ITERS{50};
71 constexpr Real REL_TOLERANCE{1e-6};
72
73 // Check inputs
74 mooseAssert(mu > 0, "Need a strictly positive viscosity");
75 mooseAssert(rho > 0, "Need a strictly positive density");
76 mooseAssert(u > 0, "Need a strictly positive velocity");
77 mooseAssert(dist > 0, "Need a strictly positive wall distance");
78
79 const T nu = mu / rho;
80
81 // Wall-function linearized guess
82 const Real a_c = 1 / NS::von_karman_constant;
83 const T b_c = 1.0 / NS::von_karman_constant * (log(NS::E_turb_constant * dist / mu) + 1.0);
84 const T & c_c = u;
85
87 T u_star = max(1e-20, (-b_c + sqrt(pow(b_c, 2) + 4.0 * a_c * c_c)) / (2.0 * a_c));
88
89 // Newton-Raphson method to solve for u_star (friction velocity).
90 for (int i = 0; i < MAX_ITERS; ++i)
91 {
92 T residual =
93 u_star / NS::von_karman_constant * log(NS::E_turb_constant * u_star * dist / nu) - u;
94 T deriv = (1.0 + log(NS::E_turb_constant * u_star * dist / nu)) / NS::von_karman_constant;
95 T new_u_star = max(1e-20, u_star - residual / deriv);
96
97 Real rel_err =
98 abs(MetaPhysicL::raw_value(new_u_star - u_star) / MetaPhysicL::raw_value(new_u_star));
99
100 u_star = new_u_star;
101 if (rel_err < REL_TOLERANCE)
102 return u_star;
103 }
104
105 mooseException("Could not find the wall friction velocity (mu: ",
106 mu,
107 " rho: ",
108 rho,
109 " velocity: ",
110 u,
111 " wall distance: ",
112 dist,
113 ")");
114}
115template Real findUStar<Real>(const Real & mu, const Real & rho, const Real & u, const Real dist);
116template ADReal
117findUStar<ADReal>(const ADReal & mu, const ADReal & rho, const ADReal & u, const Real dist);
118
119template <typename T>
120T
121findyPlus(const T & mu, const T & rho, const T & u, const Real dist)
122{
123 using std::max, std::log, std::abs;
124
125 // Fixed point iteration method to find y_plus
126 // It should take 3 or 4 iterations
127 constexpr int MAX_ITERS{10};
128 constexpr Real REL_TOLERANCE{1e-2};
129
130 // Check inputs
131 mooseAssert(mu > 0, "Need a strictly positive viscosity");
132 mooseAssert(u > 0, "Need a strictly positive velocity");
133 mooseAssert(rho > 0, "Need a strictly positive density");
134 mooseAssert(dist > 0, "Need a strictly positive wall distance");
135
136 Real yPlusLast = 0.0;
137 T yPlus = dist * u * rho / mu; // Assign initial value to laminar
138 const Real rev_yPlusLam = 1.0 / MetaPhysicL::raw_value(yPlus);
139 const T kappa_time_Re = NS::von_karman_constant * u * dist / (mu / rho);
140 unsigned int iters = 0;
141
142 do
143 {
144 yPlusLast = MetaPhysicL::raw_value(yPlus);
145 // Negative y plus does not make sense
146 yPlus = max(NS::min_y_plus, yPlus);
147 yPlus = (kappa_time_Re + yPlus) / (1.0 + log(NS::E_turb_constant * yPlus));
148 } while (abs(rev_yPlusLam * (MetaPhysicL::raw_value(yPlus) - yPlusLast)) > REL_TOLERANCE &&
149 ++iters < MAX_ITERS);
150
151 return max(NS::min_y_plus, yPlus);
152}
153
154template Real findyPlus<Real>(const Real & mu, const Real & rho, const Real & u, Real dist);
155template ADReal
156findyPlus<ADReal>(const ADReal & mu, const ADReal & rho, const ADReal & u, Real dist);
157
158template <typename T>
159T
161{
162 // if the velocity is zero, then the norm function call fails because AD tries to calculate the
163 // derivatives which causes a divide by zero - because d/dx(sqrt(f(x))) = 1/2/sqrt(f(x))*df/dx.
164 // So add a bit of noise (based on hitchhiker's guide to the galaxy's meaning of life number) to
165 // avoid this failure mode.
166 return isZero(velocity) ? 1e-42 : velocity.norm();
167}
170
171template <typename T>
172T
174 const Moose::Functor<T> * v,
175 const Moose::Functor<T> * w,
176 const Moose::ElemArg & elem_arg,
177 const Moose::StateArg & state,
178 const Moose::CoordinateSystemType coord_sys,
179 const unsigned int rz_radial_coord)
180{
181 const auto & grad_u = u.gradient(elem_arg, state);
182 const T Sij_xx = 2.0 * grad_u(0);
183 T Sij_xy = 0.0;
184 T Sij_xz = 0.0;
185 T Sij_yy = 0.0;
186 T Sij_yz = 0.0;
187 T Sij_zz = 0.0;
188
189 const T grad_xx = grad_u(0);
190 T grad_xy = 0.0;
191 T grad_xz = 0.0;
192 T grad_yx = 0.0;
193 T grad_yy = 0.0;
194 T grad_yz = 0.0;
195 T grad_zx = 0.0;
196 T grad_zy = 0.0;
197 T grad_zz = 0.0;
198
199 T trace = Sij_xx / 3.0;
200
201 if (v) // dim >= 2
202 {
203 const auto & grad_v = (*v).gradient(elem_arg, state);
204 Sij_xy = grad_u(1) + grad_v(0);
205 Sij_yy = 2.0 * grad_v(1);
206
207 grad_xy = grad_u(1);
208 grad_yx = grad_v(0);
209 grad_yy = grad_v(1);
210
211 trace += Sij_yy / 3.0;
212
213 if (w) // dim >= 3
214 {
215 const auto & grad_w = (*w).gradient(elem_arg, state);
216
217 Sij_xz = grad_u(2) + grad_w(0);
218 Sij_yz = grad_v(2) + grad_w(1);
219 Sij_zz = 2.0 * grad_w(2);
220
221 grad_xz = grad_u(2);
222 grad_yz = grad_v(2);
223 grad_zx = grad_w(0);
224 grad_zy = grad_w(1);
225 grad_zz = grad_w(2);
226
227 trace += Sij_zz / 3.0;
228 }
229 }
230
231 if (coord_sys == Moose::COORD_RZ)
232 {
233 mooseAssert(elem_arg.elem, "ElemArg must reference an element for cylindrical calculations.");
234 const auto radius = elem_arg.elem->vertex_average()(rz_radial_coord);
235 mooseAssert(radius > 0.0, "Axisymmetric radial coordinate should be positive.");
236
237 const Moose::Functor<T> * radial_functor = nullptr;
238 switch (rz_radial_coord)
239 {
240 case 0:
241 radial_functor = &u;
242 break;
243 case 1:
244 radial_functor = v;
245 break;
246 default:
247 mooseError("Unsupported axisymmetric radial coordinate index: ", rz_radial_coord);
248 }
249
250 mooseAssert(radial_functor,
251 "The functor corresponding to the axisymmetric radial velocity must be provided.");
252 const T radial_velocity = (*radial_functor)(elem_arg, state);
253 Sij_zz = 2.0 * radial_velocity / radius;
254 grad_zz = radial_velocity / radius;
255 trace += Sij_zz / 3.0;
256 }
257
258 // Note that in RZ we can only do X or Y axis. However, due to the hoop stress the
259 // Z direction (polar coordinate) tensors are not 0 in this case.
260 return (Sij_xx - trace) * grad_xx + Sij_xy * grad_xy + Sij_xz * grad_xz + Sij_xy * grad_yx +
261 (Sij_yy - trace) * grad_yy + Sij_yz * grad_yz + Sij_xz * grad_zx + Sij_yz * grad_zy +
262 (Sij_zz - trace) * grad_zz;
263}
265 const Moose::Functor<Real> * v,
266 const Moose::Functor<Real> * w,
267 const Moose::ElemArg & elem_arg,
268 const Moose::StateArg & state,
269 const Moose::CoordinateSystemType coord_sys,
270 const unsigned int rz_radial_coord);
271template ADReal
274 const Moose::Functor<ADReal> * w,
275 const Moose::ElemArg & elem_arg,
276 const Moose::StateArg & state,
277 const Moose::CoordinateSystemType coord_sys,
278 const unsigned int rz_radial_coord);
279
281void
282getWallBoundedElements(const std::vector<BoundaryName> & wall_boundary_names,
283 const FEProblemBase & fe_problem,
284 const SubProblem & subproblem,
285 const std::set<SubdomainID> & block_ids,
286 std::unordered_set<const Elem *> & wall_bounded)
287{
288
289 wall_bounded.clear();
290 const auto wall_boundary_ids = subproblem.mesh().getBoundaryIDs(wall_boundary_names);
291
292 // We define these lambdas so that we can fetch the bounded elements from other
293 // processors.
294 auto gather_functor = [&subproblem, &wall_bounded](const processor_id_type libmesh_dbg_var(pid),
295 const std::vector<dof_id_type> & elem_ids,
296 std::vector<unsigned char> & data_to_fill)
297 {
298 mooseAssert(pid != subproblem.processor_id(), "We shouldn't be gathering from ourselves.");
299 data_to_fill.resize(elem_ids.size());
300
301 const auto & mesh = subproblem.mesh().getMesh();
302
303 for (const auto i : index_range(elem_ids))
304 {
305 const auto elem = mesh.elem_ptr(elem_ids[i]);
306 data_to_fill[i] = wall_bounded.count(elem) != 0;
307 }
308 };
309
310 auto action_functor = [&subproblem, &wall_bounded](const processor_id_type libmesh_dbg_var(pid),
311 const std::vector<dof_id_type> & elem_ids,
312 const std::vector<unsigned char> & filled_data)
313 {
314 mooseAssert(pid != subproblem.processor_id(),
315 "The request filler shouldn't have been ourselves");
316 mooseAssert(elem_ids.size() == filled_data.size(), "I think these should be the same size");
317
318 const auto & mesh = subproblem.mesh().getMesh();
319
320 for (const auto i : index_range(elem_ids))
321 {
322 const auto elem = mesh.elem_ptr(elem_ids[i]);
323 if (filled_data[i])
324 wall_bounded.insert(elem);
325 }
326 };
327
328 // We need these elements from other processors
329 std::unordered_map<processor_id_type, std::vector<dof_id_type>> elem_ids_requested;
330
331 for (const auto & elem : fe_problem.mesh().getMesh().active_local_element_ptr_range())
332 if (block_ids.find(elem->subdomain_id()) != block_ids.end())
333 for (const auto i_side : elem->side_index_range())
334 {
335 // This is needed because in some cases the internal boundary is registered
336 // to the neighbor element
337 std::set<BoundaryID> combined_side_bds;
338 const auto & side_bnds = subproblem.mesh().getBoundaryIDs(elem, i_side);
339 combined_side_bds.insert(side_bnds.begin(), side_bnds.end());
340 if (const auto neighbor = elem->neighbor_ptr(i_side))
341 {
342 const auto neighbor_side = neighbor->which_neighbor_am_i(elem);
343 const auto & neighbor_bnds = subproblem.mesh().getBoundaryIDs(neighbor, neighbor_side);
344 combined_side_bds.insert(neighbor_bnds.begin(), neighbor_bnds.end());
345
346 // If the neighbor lives on the first layer of the ghost region then we would
347 // like to grab its value as well (if it exists)
348 if (neighbor->processor_id() != subproblem.processor_id() &&
349 block_ids.find(neighbor->subdomain_id()) != block_ids.end())
350 elem_ids_requested[neighbor->processor_id()].push_back(neighbor->id());
351 }
352
353 for (const auto wall_id : wall_boundary_ids)
354 if (combined_side_bds.count(wall_id))
355 {
356 wall_bounded.insert(elem);
357 break;
358 }
359 }
360
361 unsigned char * bool_ex = nullptr;
363 subproblem.comm(), elem_ids_requested, gather_functor, action_functor, bool_ex);
364}
365
367void
368getWallDistance(const std::vector<BoundaryName> & wall_boundary_name,
369 const FEProblemBase & fe_problem,
370 const SubProblem & subproblem,
371 const std::set<SubdomainID> & block_ids,
372 std::map<const Elem *, std::vector<Real>> & dist_map)
373{
374 dist_map.clear();
375 const auto wall_boundary_ids = subproblem.mesh().getBoundaryIDs(wall_boundary_name);
376
377 for (const auto & elem : fe_problem.mesh().getMesh().active_local_element_ptr_range())
378 if (block_ids.find(elem->subdomain_id()) != block_ids.end())
379 for (const auto i_side : elem->side_index_range())
380 {
381 // This is needed because in some cases the internal boundary is registered
382 // to the neighbor element
383 std::set<BoundaryID> combined_side_bds;
384 const auto & side_bnds = subproblem.mesh().getBoundaryIDs(elem, i_side);
385 combined_side_bds.insert(side_bnds.begin(), side_bnds.end());
386 if (const auto neighbor = elem->neighbor_ptr(i_side))
387 {
388 const auto neighbor_side = neighbor->which_neighbor_am_i(elem);
389 const std::vector<BoundaryID> & neighbor_bnds =
390 subproblem.mesh().getBoundaryIDs(neighbor, neighbor_side);
391 combined_side_bds.insert(neighbor_bnds.begin(), neighbor_bnds.end());
392 }
393
394 for (const auto wall_id : wall_boundary_ids)
395 if (combined_side_bds.count(wall_id))
396 {
397 // The list below stores the face infos with respect to their owning elements,
398 // depending on the block restriction we might encounter situations where the
399 // element outside of the block owns the face info.
400 const auto & neighbor = elem->neighbor_ptr(i_side);
401 const auto elem_has_fi = Moose::FV::elemHasFaceInfo(*elem, neighbor);
402 const auto & elem_for_fi = elem_has_fi ? elem : neighbor;
403 const auto side = elem_has_fi ? i_side : neighbor->which_neighbor_am_i(elem);
404
405 const FaceInfo * const fi = subproblem.mesh().faceInfo(elem_for_fi, side);
406 const auto & elem_centroid = elem_has_fi ? fi->elemCentroid() : fi->neighborCentroid();
407 const Real dist = std::abs((elem_centroid - fi->faceCentroid()) * fi->normal());
408 dist_map[elem].push_back(dist);
409 }
410 }
411}
412
414void
415getElementFaceArgs(const std::vector<BoundaryName> & wall_boundary_name,
416 const FEProblemBase & fe_problem,
417 const SubProblem & subproblem,
418 const std::set<SubdomainID> & block_ids,
419 std::map<const Elem *, std::vector<const FaceInfo *>> & face_info_map)
420{
421 face_info_map.clear();
422 const auto wall_boundary_ids = subproblem.mesh().getBoundaryIDs(wall_boundary_name);
423
424 for (const auto & elem : fe_problem.mesh().getMesh().active_local_element_ptr_range())
425 if (block_ids.find(elem->subdomain_id()) != block_ids.end())
426 for (const auto i_side : elem->side_index_range())
427 {
428 // This is needed because in some cases the internal boundary is registered
429 // to the neighbor element
430 std::set<BoundaryID> combined_side_bds;
431 const auto & side_bnds = subproblem.mesh().getBoundaryIDs(elem, i_side);
432 combined_side_bds.insert(side_bnds.begin(), side_bnds.end());
433 if (elem->neighbor_ptr(i_side) && !elem->neighbor_ptr(i_side)->is_remote())
434 {
435 const auto neighbor = elem->neighbor_ptr(i_side);
436 const auto neighbor_side = neighbor->which_neighbor_am_i(elem);
437 const std::vector<BoundaryID> & neighbor_bnds =
438 subproblem.mesh().getBoundaryIDs(neighbor, neighbor_side);
439 combined_side_bds.insert(neighbor_bnds.begin(), neighbor_bnds.end());
440 }
441
442 for (const auto wall_id : wall_boundary_ids)
443 if (combined_side_bds.count(wall_id))
444 {
445 // The list below stores the face infos with respect to their owning elements,
446 // depending on the block restriction we might encounter situations where the
447 // element outside of the block owns the face info.
448 const auto & neighbor = elem->neighbor_ptr(i_side);
449 const auto elem_has_fi = Moose::FV::elemHasFaceInfo(*elem, neighbor);
450 const auto & elem_for_fi = elem_has_fi ? elem : neighbor;
451 const auto side = elem_has_fi ? i_side : neighbor->which_neighbor_am_i(elem);
452
453 const FaceInfo * const fi = subproblem.mesh().faceInfo(elem_for_fi, side);
454 face_info_map[elem].push_back(fi);
455 }
456 }
457}
458}
DualNumber< Real, DNDerivativeType, true > ADReal
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
const Real p
const double Re
const double rho
const double T
void mooseError(Args &&... args)
virtual MooseMesh & mesh() override
const Point & normal() const
const Point & neighborCentroid() const
const Point & elemCentroid() const
const Point & faceCentroid() const
MeshBase & getMesh()
const std::vector< const FaceInfo * > & faceInfo() const
std::vector< BoundaryID > getBoundaryIDs(const Elem *const elem, const unsigned short int side) const
GradientType gradient(const ElemArg &elem, const StateArg &state) const
virtual MooseMesh & mesh()=0
Point vertex_average() const
processor_id_type processor_id() const
const Parallel::Communicator & comm() const
MeshBase & mesh
auto raw_value(const Eigen::Map< T > &in)
bool elemHasFaceInfo(const Elem &elem, const Elem *const neighbor)
CoordinateSystemType
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.
T findyPlus(const T &mu, const T &rho, const T &u, Real dist)
Finds the non-dimensional wall distance normalized with the friction velocity Implements a fixed-poin...
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...
int computeSign(const Real &a)
Sign function, returns $+1$ if $a$ is positive and $-1$ if $a$ is negative.
template ADReal findyPlus< ADReal >(const ADReal &mu, const ADReal &rho, const ADReal &u, Real dist)
T computeShearStrainRateNormSquared(const Moose::Functor< T > &u, const Moose::Functor< T > *v, const Moose::Functor< T > *w, const Moose::ElemArg &elem_arg, const Moose::StateArg &state, const Moose::CoordinateSystemType coord_sys=Moose::COORD_XYZ, const unsigned int rz_radial_coord=0)
Utility function to compute the shear strain rate.
template Real computeSpeed< Real >(const libMesh::VectorValue< Real > &velocity)
static constexpr Real von_karman_constant
Definition NS.h:205
static const std::string cp
Definition NS.h:125
Real reynoldsPropertyDerivative(const Real &Re, const Real &rho, const Real &mu, const Real &drho, const Real &dmu)
Computes the derivative of the Reynolds number, $Re\equiv \frac{\rho Vd}{\mu}$, with respect to an ar...
template ADReal computeSpeed< ADReal >(const libMesh::VectorValue< ADReal > &velocity)
static const std::string mu
Definition NS.h:127
static const std::string v
Definition NS.h:85
static const std::string k
Definition NS.h:134
T findUStar(const T &mu, const T &rho, const T &u, Real dist)
Finds the friction velocity using standard velocity wall functions formulation.
template ADReal computeShearStrainRateNormSquared< ADReal >(const Moose::Functor< ADReal > &u, const Moose::Functor< ADReal > *v, const Moose::Functor< ADReal > *w, const Moose::ElemArg &elem_arg, const Moose::StateArg &state, const Moose::CoordinateSystemType coord_sys, const unsigned int rz_radial_coord)
static constexpr Real E_turb_constant
Definition NS.h:206
Real prandtlPropertyDerivative(const Real &mu, const Real &cp, const Real &k, const Real &dmu, const Real &dcp, const Real &dk)
Computes the derivative of the Prandtl number, $Pr\equiv\frac{\mu C_p}{k}$, with respect to an arbitr...
unsigned int getIndex(const Real &p, const std::vector< Real > &bounds)
Determines the index $i$ in a sorted array such that the input point is within the $i$-th and $i+1$-t...
static constexpr Real min_y_plus
Definition NS.h:212
template ADReal findUStar< ADReal >(const ADReal &mu, const ADReal &rho, const ADReal &u, const Real dist)
T computeSpeed(const libMesh::VectorValue< T > &velocity)
Compute the speed (velocity norm) given the supplied velocity.
template Real findyPlus< Real >(const Real &mu, const Real &rho, const Real &u, Real dist)
int delta(unsigned int i, unsigned int j)
Delta function, which returns zero if $i\ne j$ and unity if $i=j$.
template Real computeShearStrainRateNormSquared< Real >(const Moose::Functor< Real > &u, const Moose::Functor< Real > *v, const Moose::Functor< Real > *w, const Moose::ElemArg &elem_arg, const Moose::StateArg &state, const Moose::CoordinateSystemType coord_sys, const unsigned int rz_radial_coord)
template Real findUStar< Real >(const Real &mu, const Real &rho, const Real &u, const Real dist)
static const std::string velocity
Definition NS.h:46
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-...
void pull_parallel_vector_data(const Communicator &comm, const MapToVectors &queries, GatherFunctor &gather_data, const ActionFunctor &act_on_data, const datum *example)
const libMesh::Elem * elem
const Real radius