https://mooseframework.inl.gov
Loading...
Searching...
No Matches
IncompressibleMomentumSPBase.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
11
12#include "ScalarCoupleable.h"
14#include "PhysicalConstants.h"
15
16template <bool is_ad>
19{
21 params.addClassDescription("Base class for path-integrated incompressible momentum kernels.");
22 params.addCoupledVar("temperatures",
23 {},
24 "Fluid temperature in each segment of this component. Takes a "
25 "list of scalar variable names");
26 params.addParam<bool>(
27 "is_implicit",
28 false,
29 "Whether an explicit (previous value calculation) or implicit (current value) is used");
30 params.addRequiredParam<MooseFunctorName>("reference_pressure", "system reference pressure [Pa]");
31 params.addRequiredParam<UserObjectName>("fp", "The name of the user object for fluid properties");
32 params.addParam<std::vector<MooseFunctorName>>(
33 "areas",
34 std::vector<MooseFunctorName>({}),
35 "Component flow areas per segment. Takes a vector of functors.");
36 params.addParam<std::vector<MooseFunctorName>>(
37 "perimeters",
38 std::vector<MooseFunctorName>({}),
39 "Component flow perimeters per segment. Takes a vector of functors.");
40 params.addParam<std::vector<MooseFunctorName>>(
41 "lengths",
42 std::vector<MooseFunctorName>({}),
43 "Component flow lengths per segment. Takes a vector of functors.");
44 params.addParam<std::vector<MooseFunctorName>>(
45 "alphas",
46 std::vector<MooseFunctorName>({}),
47 "Component flow angles per segment with respect to horizontal (-pi/2 downward to pi/2 "
48 "upward). Takes a vector of functors.");
49 params.addParam<std::vector<MooseFunctorName>>(
50 "forms_losses",
51 std::vector<MooseFunctorName>({}),
52 "Forms loss coefficients per segment. Takes a vector of functors.");
53 params.addParam<std::vector<MooseFunctorName>>(
54 "pump_pressures",
55 std::vector<MooseFunctorName>({}),
56 "Pump pressure gains per segment [Pa]. Takes a vector of functors.");
57 params.addParam<std::vector<MooseFunctorName>>(
58 "roughnesses",
59 std::vector<MooseFunctorName>({}),
60 "Component wall roughnesses per segment [m]. Takes a vector of functors.");
61 params.addParam<MooseFunctorName>(
62 "g", PhysicalConstants::acceleration_of_gravity, "Gravitational acceleration [m/s]");
63
64 return params;
65}
66
67template <bool is_ad>
69 const InputParameters & parameters)
70 : Base(parameters),
71 _n_temps(ScalarCoupleable::coupledScalarComponents("temperatures")),
72 _T(_n_temps),
73 _is_implicit(this->template getParam<bool>("is_implicit")),
74 _Pref(this->template getFunctor<GenericReal<is_ad>>("reference_pressure")),
75 _fp(this->template getUserObject<SinglePhaseFluidProperties>("fp")),
76 _n_segments(this->template getParam<std::vector<MooseFunctorName>>("areas").size()),
77 _areas(this->template getParam<std::vector<MooseFunctorName>>("areas").size()),
78 _perimeters(this->template getParam<std::vector<MooseFunctorName>>("perimeters").size()),
79 _lengths(this->template getParam<std::vector<MooseFunctorName>>("lengths").size()),
80 _alphas(this->template getParam<std::vector<MooseFunctorName>>("alphas").size()),
81 _forms_losses(this->template getParam<std::vector<MooseFunctorName>>("forms_losses").size()),
82 _dPps(this->template getParam<std::vector<MooseFunctorName>>("pump_pressures").size()),
83 _roughnesses(this->template getParam<std::vector<MooseFunctorName>>("roughnesses").size()),
84 _gravity(this->template getFunctor<GenericReal<is_ad>>("g"))
85{
86 const auto & area_names = MooseBase::getParam<std::vector<MooseFunctorName>>("areas");
87 const auto & perimeter_names = MooseBase::getParam<std::vector<MooseFunctorName>>("perimeters");
88 const auto & length_names = MooseBase::getParam<std::vector<MooseFunctorName>>("lengths");
89 const auto & alpha_names = MooseBase::getParam<std::vector<MooseFunctorName>>("alphas");
90 const auto & forms_loss_names =
91 MooseBase::getParam<std::vector<MooseFunctorName>>("forms_losses");
92 const auto & dPp_names = MooseBase::getParam<std::vector<MooseFunctorName>>("pump_pressures");
93 const auto & roughness_names = MooseBase::getParam<std::vector<MooseFunctorName>>("roughnesses");
94 if (_n_segments != area_names.size() || _n_segments != perimeter_names.size() ||
95 _n_segments != length_names.size() || _n_segments != alpha_names.size() ||
96 _n_segments != forms_loss_names.size() || _n_segments != dPp_names.size() ||
97 _n_segments != roughness_names.size() || _n_segments != _n_temps)
98 {
100 "Must provide consistent number of segments for each parameter! Including temperatures!");
101 }
102 for (const auto j : make_range(_n_segments))
103 {
104 _T[j] = &(ScalarCoupleable::coupledGenericScalarValue<is_ad>("temperatures", j));
105 _areas[j] = &(this->template getFunctor<GenericReal<is_ad>>(area_names[j]));
106 _perimeters[j] = &(this->template getFunctor<GenericReal<is_ad>>(perimeter_names[j]));
107 _lengths[j] = &(this->template getFunctor<GenericReal<is_ad>>(length_names[j]));
108 _alphas[j] = &(this->template getFunctor<GenericReal<is_ad>>(alpha_names[j]));
109 _forms_losses[j] = &(this->template getFunctor<GenericReal<is_ad>>(forms_loss_names[j]));
110 _dPps[j] = &(this->template getFunctor<GenericReal<is_ad>>(dPp_names[j]));
111 _roughnesses[j] = &(this->template getFunctor<GenericReal<is_ad>>(roughness_names[j]));
112 }
113}
114
115template <bool is_ad>
118{
119 GenericReal<is_ad> momentum_residual = 0;
120 const Moose::ElemArg qp = Moose::ElemArg();
121 const int i = 0;
122 const auto state = _is_implicit ? Moose::currentState() : Moose::oldState();
123 // start by getting global fluid properties
124 const auto Tave = ((*(_T[0]))[i] + (*(_T[_n_temps - 1]))[i]) / 2;
125 const auto mu = _fp.mu_from_p_T(_Pref(qp, state), Tave);
126 const auto rhog = _fp.rho_from_p_T(_Pref(qp, state), Tave);
127 // Global rescale factor so the mass flow rate's transient term has a unit coefficient,
128 // matching the coefficient of ODETimeDerivative kernel: the path's lumped L_over_A_sum is
129 // Sum_j(length_j / area_j), so the whole equation is divided through by that single sum.
130 GenericReal<is_ad> L_over_A_sum = 0;
131 for (const auto j : make_range(_n_segments))
132 L_over_A_sum += (*(_lengths[j]))(qp, state) / (*(_areas[j]))(qp, state);
133 const auto inv_L_over_A_sum = 1.0 / L_over_A_sum;
134 // loop over segments
135 for (const auto j : make_range(_n_segments))
136 {
137 // Decide flow regime for friction factor
138 const auto Dh = 4.0 * (*(_areas[j]))(qp, state) / (*(_perimeters[j]))(qp, state);
139 const auto G = massFlowRate() / (*(_areas[j]))(qp, state);
140 const auto fd = computeFrictionFactor(mu, G, Dh, j);
141 // Friction
142 momentum_residual += fd * (*(_lengths[j]))(qp, state) / Dh * G * abs(G) / 2.0 / rhog;
143 // Form losses
144 momentum_residual += (*(_forms_losses[j]))(qp, state) * G * abs(G) / 2.0 / rhog;
145 // Gravity
146 // get local density for natural circulation aspect
147 auto rhol = _fp.rho_from_p_T(_Pref(qp, state), (*(_T[j]))[i]);
148 momentum_residual +=
149 rhol * _gravity(qp, state) * (*(_lengths[j]))(qp, state) * sin((*(_alphas[j]))(qp, state));
150 // Pump pressure
151 momentum_residual -= (*(_dPps[j]))(qp, state);
152 }
153 // Pressure drop (single path-wide unknown, applied once)
154 momentum_residual += pressureDrop();
155
156 return momentum_residual * inv_L_over_A_sum;
157}
158
159template <bool is_ad>
160Real
162{
163 if constexpr (!is_ad)
164 {
165 Real momentum_jacob = 0;
166 const Moose::ElemArg qp = Moose::ElemArg();
167 const int i = 0;
168 const auto state = _is_implicit ? Moose::currentState() : Moose::oldState();
169 // start by getting global fluid properties
170 const auto Tave = ((*(_T[0]))[i] + (*(_T[_n_temps - 1]))[i]) / 2;
171 const auto mu = _fp.mu_from_p_T(_Pref(qp, state), Tave);
172 const auto rhog = _fp.rho_from_p_T(_Pref(qp, state), Tave);
173 // Global rescale factor, see computeQpResidual()
174 Real L_over_A_sum = 0;
175 for (const auto j : make_range(_n_segments))
176 L_over_A_sum += (*(_lengths[j]))(qp, state) / (*(_areas[j]))(qp, state);
177 const auto inv_L_over_A_sum = 1.0 / L_over_A_sum;
178 // loop over segments
179 for (const auto j : make_range(_n_segments))
180 {
181 // Decide flow regime for friction factor
182 const auto Dh = 4.0 * (*(_areas[j]))(qp, state) / (*(_perimeters[j]))(qp, state);
183 const auto G = massFlowRate() / (*(_areas[j]))(qp, state);
184 const auto fd = computeFrictionFactor(mu, G, Dh, j);
185 // Friction
186 momentum_jacob +=
187 fd * (*(_lengths[j]))(qp, state) / Dh * G / rhog / (*(_areas[j]))(qp, state);
188 // Form losses
189 momentum_jacob += (*(_forms_losses[j]))(qp, state) * G / rhog / (*(_areas[j]))(qp, state);
190 }
191
192 return momentum_jacob * inv_L_over_A_sum;
193 }
194 else
195 {
196 mooseError("computeQpJacobian() should not be called in AD mode");
197 return 0;
198 }
199}
200
201template <bool is_ad>
202Real
204{
205 if constexpr (!is_ad)
206 {
207 const Moose::ElemArg qp = Moose::ElemArg();
208 const auto state = _is_implicit ? Moose::currentState() : Moose::oldState();
209 // Global rescale factor, see computeQpResidual()
210 Real L_over_A_sum = 0;
211 for (const auto j : make_range(_n_segments))
212 L_over_A_sum += (*(_lengths[j]))(qp, state) / (*(_areas[j]))(qp, state);
213 const auto inv_L_over_A_sum = 1.0 / L_over_A_sum;
214 // reference pressure drop is subtracted once, scaled by inv_L_over_A_sum, so its derivative is
215 // -inv_L_over_A_sum
216 return -inv_L_over_A_sum;
217 }
218 else
219 {
220 mooseError("computeQpJacobian() should not be called in AD mode");
221 return 0;
222 }
223}
224
225template <bool is_ad>
226Real
228{
229 if constexpr (!is_ad)
230 {
231 return 0;
232 }
233 else
234 {
235 mooseError("computeQpJacobian() should not be called in AD mode");
236 return 0;
237 }
238}
239
240template <>
241Real
243{
244 mooseError("Internal error, calling computeQpJacobian in AD class.");
245 return 0.0;
246}
247
248template <bool is_ad>
251 const GenericReal<is_ad> & G,
252 const GenericReal<is_ad> & Dh,
253 const unsigned int j)
254{
255 const Moose::ElemArg qp = Moose::ElemArg();
256 const auto state = _is_implicit ? Moose::currentState() : Moose::oldState();
257 const auto Re = abs(G) * Dh / mu;
258 const auto f_lam = 64.0 / Re;
259 const auto f_turb =
260 0.25 / pow((log10((*(_roughnesses[j]))(qp, state) / (Dh * 3.7) + 5.74 / pow(Re, 0.9))), 2);
261 if (Re < 2300.0) // laminar
262 return f_lam;
263 else if (Re > 4000.0) // turbulent using Swamee-Jain approx. of Colebrook-White eq.
264 return f_turb;
265 else // transition, conservative interpolation between the two
266 return std::max((f_turb - f_lam) / 1700 * Re + f_lam, std::max(f_lam, f_turb));
267}
268
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
const double mu
const double Re
void mooseError(Args &&... args)
Moose::GenericType< Real, is_ad > GenericReal
static InputParameters validParams()
Path-integrated incompressible momentum kernel base class.
typename std::conditional< is_ad, ADScalarKernel, ScalarKernel >::type Base
std::vector< const Moose::Functor< GenericReal< is_ad > > * > _forms_losses
Forms loss coefficients of each segment.
std::vector< const Moose::Functor< GenericReal< is_ad > > * > _alphas
Angle with respect to the horizontal of each segment.
const size_t _n_temps
Number of coupled temperature variables.
IncompressibleMomentumSPBaseTempl(const InputParameters &parameters)
virtual GenericReal< is_ad > computeQpResidual() override
std::vector< const Moose::Functor< GenericReal< is_ad > > * > _roughnesses
Wall roughness of each segment.
std::vector< const GenericVariableValue< is_ad > * > _T
Coupled temperature variables.
std::vector< const Moose::Functor< GenericReal< is_ad > > * > _dPps
Pump pressure gains of each segment.
std::vector< const Moose::Functor< GenericReal< is_ad > > * > _lengths
Length of each segment.
virtual GenericReal< is_ad > computeFrictionFactor(const GenericReal< is_ad > &mu, const GenericReal< is_ad > &G, const GenericReal< is_ad > &Dh, const unsigned int j)
std::vector< const Moose::Functor< GenericReal< is_ad > > * > _perimeters
Wetted perimeter of each segment.
const size_t _n_segments
Number of geometrically/thermally unique segments.
std::vector< const Moose::Functor< GenericReal< is_ad > > * > _areas
Flow area of each segment.
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void addCoupledVar(const std::string &name, const std::string &doc_string)
static InputParameters validParams()
Common class for single phase fluid properties.
StateArg oldState()
StateArg currentState()
const auto acceleration_of_gravity