LCOV - code coverage report
Current view: top level - src/linearfvkernels - LinearFVTKESourceSink.C (source / functions) Hit Total Coverage
Test: idaholab/moose navier_stokes: 2bd28b Lines: 101 107 94.4 %
Date: 2025-10-23 22:11:45 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://www.mooseframework.org
       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 "LinearFVTKESourceSink.h"
      11             : #include "Assembly.h"
      12             : #include "SubProblem.h"
      13             : #include "NavierStokesMethods.h"
      14             : 
      15             : registerMooseObject("NavierStokesApp", LinearFVTKESourceSink);
      16             : 
      17             : InputParameters
      18         106 : LinearFVTKESourceSink::validParams()
      19             : {
      20         106 :   InputParameters params = LinearFVElementalKernel::validParams();
      21         106 :   params.addClassDescription("Elemental kernel to compute the production and destruction "
      22             :                              " terms of turbulent kinetic energy (TKE).");
      23         212 :   params.addRequiredParam<MooseFunctorName>("u", "The velocity in the x direction.");
      24         212 :   params.addParam<MooseFunctorName>("v", "The velocity in the y direction.");
      25         212 :   params.addParam<MooseFunctorName>("w", "The velocity in the z direction.");
      26         106 :   params.addRequiredParam<MooseFunctorName>(NS::TKED,
      27             :                                             "Coupled turbulent kinetic energy dissipation rate.");
      28         106 :   params.addRequiredParam<MooseFunctorName>(NS::density, "Fluid density");
      29         106 :   params.addRequiredParam<MooseFunctorName>(NS::mu, "Dynamic viscosity.");
      30         106 :   params.addRequiredParam<MooseFunctorName>(NS::mu_t, "Turbulent viscosity.");
      31             : 
      32         212 :   params.addParam<std::vector<BoundaryName>>(
      33             :       "walls", {}, "Boundaries that correspond to solid walls.");
      34         212 :   params.addParam<bool>(
      35             :       "linearized_model",
      36         212 :       true,
      37             :       "Boolean to determine if the problem should be use in a linear or nonlinear solve.");
      38         212 :   MooseEnum wall_treatment("eq_newton eq_incremental eq_linearized neq", "neq");
      39             : 
      40         212 :   params.addParam<MooseEnum>("wall_treatment",
      41             :                              wall_treatment,
      42             :                              "The method used for computing the wall functions "
      43             :                              "'eq_newton', 'eq_incremental', 'eq_linearized', 'neq'");
      44         212 :   params.addParam<Real>("C_mu", 0.09, "Coupled turbulent kinetic energy closure.");
      45         212 :   params.addParam<Real>("C_pl", 10.0, "Production Limiter Constant Multiplier.");
      46             : 
      47         106 :   return params;
      48         106 : }
      49             : 
      50          53 : LinearFVTKESourceSink::LinearFVTKESourceSink(const InputParameters & params)
      51             :   : LinearFVElementalKernel(params),
      52          53 :     _dim(_subproblem.mesh().dimension()),
      53         106 :     _u_var(getFunctor<Real>("u")),
      54         159 :     _v_var(params.isParamValid("v") ? &(getFunctor<Real>("v")) : nullptr),
      55          53 :     _w_var(params.isParamValid("w") ? &(getFunctor<Real>("w")) : nullptr),
      56          53 :     _epsilon(getFunctor<Real>(NS::TKED)),
      57          53 :     _rho(getFunctor<Real>(NS::density)),
      58          53 :     _mu(getFunctor<Real>(NS::mu)),
      59          53 :     _mu_t(getFunctor<Real>(NS::mu_t)),
      60         106 :     _wall_boundary_names(getParam<std::vector<BoundaryName>>("walls")),
      61         106 :     _linearized_model(getParam<bool>("linearized_model")),
      62         106 :     _wall_treatment(getParam<MooseEnum>("wall_treatment").getEnum<NS::WallTreatmentEnum>()),
      63         106 :     _C_mu(getParam<Real>("C_mu")),
      64         159 :     _C_pl(getParam<Real>("C_pl"))
      65             : {
      66          53 :   if (_dim >= 2 && !_v_var)
      67           0 :     paramError("v", "In two or more dimensions, the v velocity must be supplied!");
      68             : 
      69          53 :   if (_dim >= 3 && !_w_var)
      70           0 :     paramError("w", "In three or more dimensions, the w velocity must be supplied!");
      71             : 
      72             :   // // Strain tensor term requires velocity gradients;
      73          53 :   if (dynamic_cast<const MooseLinearVariableFV<Real> *>(&_u_var))
      74           0 :     requestVariableCellGradient(getParam<MooseFunctorName>("u"));
      75          53 :   if (dynamic_cast<const MooseLinearVariableFV<Real> *>(_v_var))
      76           0 :     requestVariableCellGradient(getParam<MooseFunctorName>("v"));
      77          53 :   if (dynamic_cast<const MooseLinearVariableFV<Real> *>(_w_var))
      78           0 :     requestVariableCellGradient(getParam<MooseFunctorName>("w"));
      79          53 : }
      80             : 
      81             : void
      82         265 : LinearFVTKESourceSink::initialSetup()
      83             : {
      84         265 :   LinearFVElementalKernel::initialSetup();
      85         530 :   NS::getWallBoundedElements(
      86         265 :       _wall_boundary_names, _fe_problem, _subproblem, blockIDs(), _wall_bounded);
      87         265 :   NS::getWallDistance(_wall_boundary_names, _fe_problem, _subproblem, blockIDs(), _dist);
      88         265 :   NS::getElementFaceArgs(_wall_boundary_names, _fe_problem, _subproblem, blockIDs(), _face_infos);
      89         265 : }
      90             : 
      91             : Real
      92      613088 : LinearFVTKESourceSink::computeMatrixContribution()
      93             : {
      94             :   /*
      95             :   Matrix contribution:
      96             :   - Computes near-wall TKE destruction
      97             :   - Computes bulk TKE destruction
      98             :   */
      99             : 
     100             :   // Useful variables
     101      613088 :   const auto state = determineState();
     102      613088 :   const auto elem_arg = makeElemArg(_current_elem_info->elem());
     103      613088 :   const Real rho = _rho(elem_arg, state);
     104             : 
     105      613088 :   if (_wall_bounded.find(_current_elem_info->elem()) != _wall_bounded.end())
     106             :   {
     107             : 
     108             :     // Useful variables
     109      161076 :     const Real mu = _mu(elem_arg, state);
     110      161076 :     const Real TKE = _var.getElemValue(*_current_elem_info, state);
     111             : 
     112             :     // Assembly variables
     113             :     Real production = 0.0;
     114             :     Real destruction = 0.0;
     115             :     Real tot_weight = 0.0;
     116             :     std::vector<Real> y_plus_vec, velocity_grad_norm_vec;
     117             : 
     118             :     // Get near-wall element velocity vector
     119      161076 :     RealVectorValue velocity(_u_var(elem_arg, state));
     120      161076 :     if (_v_var)
     121      161076 :       velocity(1) = (*_v_var)(elem_arg, state);
     122      161076 :     if (_w_var)
     123           0 :       velocity(2) = (*_w_var)(elem_arg, state);
     124             : 
     125             :     // Get faceInfo and distance vector for between all walls and elements
     126      161076 :     const auto & face_info_vec = libmesh_map_find(_face_infos, _current_elem_info->elem());
     127      161076 :     const auto & distance_vec = libmesh_map_find(_dist, _current_elem_info->elem());
     128             :     mooseAssert(distance_vec.size(), "Should have found a distance vector");
     129             :     mooseAssert(distance_vec.size() == face_info_vec.size(),
     130             :                 "Should be as many distance vectors as face info vectors");
     131             : 
     132             :     // Populate y+, near wall velocity gradient, and update weights
     133      323212 :     for (unsigned int i = 0; i < distance_vec.size(); i++)
     134             :     {
     135      162136 :       const auto parallel_speed = NS::computeSpeed<Real>(
     136      162136 :           velocity - velocity * face_info_vec[i]->normal() * face_info_vec[i]->normal());
     137      162136 :       const auto distance = distance_vec[i];
     138             : 
     139             :       Real y_plus;
     140      162136 :       if (_wall_treatment == NS::WallTreatmentEnum::NEQ) // Non-equilibrium --> Non-iterative
     141       50000 :         y_plus = distance * std::sqrt(std::sqrt(_C_mu) * TKE) * rho / mu;
     142             :       else // Equilibrium --> Iterative
     143      112136 :         y_plus = NS::findyPlus<Real>(mu, rho, std::max(parallel_speed, 1e-10), distance);
     144             : 
     145      162136 :       y_plus_vec.push_back(y_plus);
     146      162136 :       const Real velocity_grad_norm = parallel_speed / distance;
     147      162136 :       velocity_grad_norm_vec.push_back(velocity_grad_norm);
     148      162136 :       tot_weight += 1.0;
     149             :     }
     150             : 
     151             :     // Compute near-wall production and destruction
     152      323212 :     for (unsigned int i = 0; i < y_plus_vec.size(); i++)
     153             :     {
     154      162136 :       const auto y_plus = y_plus_vec[i];
     155             : 
     156      162136 :       const auto fi = face_info_vec[i];
     157      162136 :       const bool defined_on_elem_side = _var.hasFaceSide(*fi, true);
     158      162136 :       const Elem * const loc_elem = defined_on_elem_side ? &fi->elem() : fi->neighborPtr();
     159      162136 :       const Moose::FaceArg facearg = {
     160      162136 :           fi, Moose::FV::LimiterType::CentralDifference, false, false, loc_elem, nullptr};
     161             : 
     162      162136 :       const Real wall_mut = _mu_t(facearg, state);
     163      162136 :       const Real wall_mu = _mu(facearg, state);
     164      162136 :       const auto tau_w = (wall_mut + wall_mu) * velocity_grad_norm_vec[i];
     165      162136 :       const auto destruction_visc = 2.0 * wall_mu / Utility::pow<2>(distance_vec[i]) / tot_weight;
     166      162136 :       const auto destruction_log = std::pow(_C_mu, 0.75) * rho * std::pow(TKE, 0.5) /
     167      162136 :                                    (NS::von_karman_constant * distance_vec[i]) / tot_weight;
     168             : 
     169      162136 :       if (y_plus < 11.25)
     170       32459 :         destruction += destruction_visc;
     171             :       else
     172             :       {
     173      129677 :         destruction += destruction_log;
     174      129677 :         production += tau_w * std::pow(_C_mu, 0.25) / std::sqrt(TKE) /
     175      129677 :                       (NS::von_karman_constant * distance_vec[i]) / tot_weight;
     176             :       }
     177             :     }
     178             : 
     179             :     // Assign terms to matrix to solve implicitly (they get multiplied by TKE)
     180      161076 :     return (destruction - production) * _current_elem_volume;
     181      161076 :   }
     182             :   else
     183             :   {
     184             :     // Compute destruction
     185             :     const auto destruction =
     186      452012 :         rho * _epsilon(elem_arg, state) / _var.getElemValue(*_current_elem_info, state);
     187             : 
     188             :     // Assign to matrix to solve implicitly
     189      452012 :     return destruction * _current_elem_volume;
     190             :   }
     191             : }
     192             : 
     193             : Real
     194      613088 : LinearFVTKESourceSink::computeRightHandSideContribution()
     195             : {
     196             : 
     197             :   // Useful variables
     198      613088 :   const auto state = determineState();
     199      613088 :   const auto elem_arg = makeElemArg(_current_elem_info->elem());
     200             : 
     201      613088 :   if (_wall_bounded.find(_current_elem_info->elem()) != _wall_bounded.end()) // Wall bounded
     202             :     return 0.0;                                                              // Do nothing
     203             :   else                                                                       // Not wall bounded
     204             :   {
     205             :     // Compute TKE production
     206             :     const auto symmetric_strain_tensor_sq_norm =
     207      452012 :         NS::computeShearStrainRateNormSquared<Real>(_u_var, _v_var, _w_var, elem_arg, state);
     208             : 
     209      452012 :     auto production = _mu_t(elem_arg, state) * symmetric_strain_tensor_sq_norm;
     210             : 
     211             :     // k-Production limiter (needed for flows with stagnation zones)
     212      452012 :     const Real production_limit = _C_pl * _rho(elem_arg, state) * _epsilon(elem_arg, state);
     213             : 
     214             :     // Apply production limiter
     215      452012 :     production = std::min(production, production_limit);
     216             : 
     217             :     // Assign production to RHS
     218      452012 :     return production * _current_elem_volume;
     219             :   }
     220             : }

Generated by: LCOV version 1.14