https://mooseframework.inl.gov
Numerics.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 "Numerics.h"
11 #include "MooseUtils.h"
12 #include "ADReal.h"
13 
14 namespace THM
15 {
16 
17 bool
19 {
20  return MooseUtils::absoluteFuzzyEqual(a(0), b(0), tol) &&
23 }
24 
25 bool
26 areParallelVectors(const RealVectorValue & a, const RealVectorValue & b, const Real & tol)
27 {
28  const RealVectorValue c = a.cross(b);
30 }
31 
32 bool
33 haveSameDirection(const RealVectorValue & a, const RealVectorValue & b, const Real & tol)
34 {
35  return areParallelVectors(a, b, tol) && a * b > 0;
36 }
37 
38 Real
39 applyQuotientRule(const Real & num, const Real & den, const Real & dnum_dy, const Real & dden_dy)
40 {
41  return (dnum_dy * den - num * dden_dy) / (den * den);
42 }
43 
44 DenseVector<Real>
45 applyQuotientRule(const Real & num,
46  const Real & den,
47  const DenseVector<Real> & dnum_dy,
48  const DenseVector<Real> & dden_dy)
49 {
50  DenseVector<Real> d_dy = dnum_dy;
51  d_dy *= 1.0 / den;
52  d_dy.add(-num / std::pow(den, 2), dden_dy);
53 
54  return d_dy;
55 }
56 
57 void
58 vel_from_arhoA_arhouA(Real arhoA, Real arhouA, Real & vel, Real & dvel_darhoA, Real & dvel_darhouA)
59 {
60  vel = arhouA / arhoA;
61  dvel_darhoA = -arhouA / (arhoA * arhoA);
62  dvel_darhouA = 1.0 / arhoA;
63 }
64 
65 ADReal
67 {
68  return arhouA / arhoA;
69 }
70 
71 Real
72 dvel_darhoA(Real arhoA, Real arhouA)
73 {
74  return -arhouA / (arhoA * arhoA);
75 }
76 
77 Real
78 dvel_darhouA(Real arhoA)
79 {
80  return 1.0 / arhoA;
81 }
82 
83 void
85  Real arhoA, Real alpha, Real A, Real & rho, Real & drho_darhoA, Real & drho_dalpha)
86 {
87  rho = arhoA / (alpha * A);
88 
89  drho_darhoA = 1.0 / (alpha * A);
90  drho_dalpha = -arhoA / (alpha * alpha * A);
91 }
92 
93 ADReal
95 {
96  return arhoA / (alpha * A);
97 }
98 
99 void
100 v_from_rhoA_A(Real rhoA, Real A, Real & v, Real & dv_drhoA)
101 {
102  v = A / rhoA;
103  dv_drhoA = -A / (rhoA * rhoA);
104 }
105 
106 ADReal
108 {
109  return A / rhoA;
110 }
111 
112 void
113 v_from_arhoA_alpha_A(Real arhoA, Real alpha, Real A, Real & v, Real & dv_darhoA, Real & dv_dalpha)
114 {
115  v = (alpha * A) / arhoA;
116  dv_darhoA = -(alpha * A) / (arhoA * arhoA);
117  dv_dalpha = A / arhoA;
118 }
119 
120 ADReal
122 {
123  return (alpha * A) / arhoA;
124 }
125 
126 void
127 v_from_rho(Real rho, Real & v, Real & dv_drho)
128 {
129  v = 1.0 / rho;
130  dv_drho = -1.0 / (rho * rho);
131 }
132 
133 Real
134 dv_dalpha_liquid(Real area, Real arhoA, bool is_liquid)
135 {
136  const Real sign = is_liquid ? 1.0 : -1.0;
137  return sign * (area / arhoA);
138 }
139 
140 Real
141 dv_darhoA(Real area, Real arhoA)
142 {
143  return -area / arhoA / arhoA;
144 }
145 
146 void
148  Real arhouA,
149  Real arhoEA,
150  Real & e,
151  Real & de_darhoA_val,
152  Real & de_darhouA_val,
153  Real & de_darhoEA_val)
154 {
155  e = arhoEA / arhoA - 0.5 * arhouA * arhouA / (arhoA * arhoA);
156  de_darhoA_val = de_darhoA(arhoA, arhouA, arhoEA);
157  de_darhouA_val = de_darhouA(arhoA, arhouA);
158  de_darhoEA_val = de_darhoEA(arhoA);
159 }
160 
161 ADReal
163 {
164  return arhoEA / arhoA - 0.5 * arhouA * arhouA / (arhoA * arhoA);
165 }
166 
167 void
168 e_from_E_vel(Real E, Real vel, Real & e, Real & de_dE, Real & de_dvel)
169 {
170  e = E - 0.5 * vel * vel;
171  de_dE = 1;
172  de_dvel = -vel;
173 }
174 ADReal
176 {
177  return E - 0.5 * vel * vel;
178 }
179 
180 Real
181 de_darhoA(Real arhoA, Real arhouA, Real arhoEA)
182 {
183  return (-arhoEA / arhoA / arhoA + arhouA * arhouA / arhoA / arhoA / arhoA);
184 }
185 
186 Real
187 de_darhouA(Real arhoA, Real arhouA)
188 {
189  return (-arhouA / arhoA / arhoA);
190 }
191 
192 Real
193 de_darhoEA(Real arhoA)
194 {
195  return (1 / arhoA);
196 }
197 
198 void
199 E_from_arhoA_arhoEA(Real arhoA, Real arhoEA, Real & E, Real & dE_darhoA, Real & dE_darhoEA)
200 {
201  E = arhoEA / arhoA;
202  dE_darhoA = -arhoEA / (arhoA * arhoA);
203  dE_darhoEA = 1.0 / arhoA;
204 }
205 ADReal
207 {
208  return arhoEA / arhoA;
209 }
210 
211 void
212 E_from_e_vel(Real e, Real vel, Real & E, Real & dE_de, Real & dE_dvel)
213 {
214  E = e + 0.5 * vel * vel;
215  dE_de = 1.0;
216  dE_dvel = vel;
217 }
218 
219 void
220 h_from_e_p_rho(Real e, Real p, Real rho, Real & h, Real & dh_de, Real & dh_dp, Real & dh_drho)
221 {
222  h = e + p / rho;
223  dh_de = 1.0;
224  dh_dp = 1.0 / rho;
225  dh_drho = -p / (rho * rho);
226 }
227 
228 ADReal
230 {
231  return e + p / rho;
232 }
233 
234 bool
235 isInlet(Real vel, Real normal)
236 {
237  return (vel * normal) < 0;
238 }
239 
240 bool
241 isInlet(ADReal vel, Real normal)
242 {
243  return (MetaPhysicL::raw_value(vel) * normal) < 0;
244 }
245 
246 bool
247 isOutlet(Real vel, Real normal)
248 {
249  return (vel * normal) >= 0;
250 }
251 
252 bool
253 isOutlet(ADReal vel, Real normal)
254 {
255  return (MetaPhysicL::raw_value(vel) * normal) >= 0;
256 }
257 
258 } // namespace THM
void e_from_arhoA_arhouA_arhoEA(Real arhoA, Real arhouA, Real arhoEA, Real &e, Real &de_darhoA, Real &de_darhouA, Real &de_darhoEA)
Computes specific internal energy and its derivatives from alpha*rho*A, alpha*rho*u*A, and alpha*rho*E*A.
Definition: Numerics.C:147
Real de_darhoA(Real arhoA, Real arhouA, Real arhoEA)
Derivative of specific internal energy wrt density of the phase (rhoA or arhoA)
Definition: Numerics.C:181
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Real de_darhoEA(Real arhoA)
Derivative of specific internal energy wrt total energy of the phase (rhoEA or arhoEA) ...
Definition: Numerics.C:193
void v_from_rhoA_A(Real rhoA, Real A, Real &v, Real &dv_drhoA)
Computes specific volume and its derivatives from rho*A, and area.
Definition: Numerics.C:100
void rho_from_arhoA_alpha_A(Real arhoA, Real alpha, Real A, Real &rho, Real &drho_darhoA, Real &drho_dalpha)
Computes density and its derivatives from alpha*rho*A, alpha, and area.
Definition: Numerics.C:84
const double tol
auto raw_value(const Eigen::Map< T > &in)
void e_from_E_vel(Real E, Real vel, Real &e, Real &de_dE, Real &de_dvel)
Computes specific internal energy and its derivatives from specific total energy and velocity...
Definition: Numerics.C:168
Real dv_darhoA(Real area, Real arhoA)
Derivative of specific volume wrt density equation solution variable.
Definition: Numerics.C:141
bool isOutlet(Real vel, Real normal)
Determine if outlet boundary condition should be applied.
Definition: Numerics.C:247
void vel_from_arhoA_arhouA(Real arhoA, Real arhouA, Real &vel, Real &dvel_darhoA, Real &dvel_darhouA)
Computes velocity and its derivatives from alpha*rho*A and alpha*rho*u*A.
Definition: Numerics.C:58
bool areParallelVectors(const RealVectorValue &a, const RealVectorValue &b, const Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Tests if two real-valued vectors are parallel within some absolute tolerance.
Definition: Numerics.C:26
DualNumber< Real, DNDerivativeType, true > ADReal
Real de_darhouA(Real arhoA, Real arhouA)
Derivative of specific internal energy wrt momentum of the phase (rhouA or arhouA) ...
Definition: Numerics.C:187
void E_from_arhoA_arhoEA(Real arhoA, Real arhoEA, Real &E, Real &dE_darhoA, Real &dE_darhoEA)
Computes specific total energy and its derivatives from alpha*rho*A and alpha*rho*E*A.
Definition: Numerics.C:199
T sign(T x)
Real dvel_darhouA(Real arhoA)
Derivative of velocity w.r.t.
Definition: Numerics.C:78
bool haveSameDirection(const RealVectorValue &a, const RealVectorValue &b, const Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Tests if two real-valued vectors are in the same direction.
Definition: Numerics.C:33
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static const std::string v
Definition: NS.h:84
bool isInlet(Real vel, Real normal)
Determine if inlet boundary condition should be applied.
Definition: Numerics.C:235
static const std::string alpha
Definition: NS.h:134
void v_from_rho(Real rho, Real &v, Real &dv_drho)
Computes specific volume and its derivative with respect to density.
Definition: Numerics.C:127
void E_from_e_vel(Real e, Real vel, Real &E, Real &dE_de, Real &dE_dvel)
Computes specific total energy and its derivatives from specific internal energy and velocity...
Definition: Numerics.C:212
Real dv_dalpha_liquid(Real area, Real arhoA, bool is_liquid)
Derivative of specific volume wrt alpha_liquid.
Definition: Numerics.C:134
Real dvel_darhoA(Real arhoA, Real arhouA)
Derivative of velocity w.r.t.
Definition: Numerics.C:72
void h_from_e_p_rho(Real e, Real p, Real rho, Real &h, Real &dh_de, Real &dh_dp, Real &dh_drho)
Computes specific enthalpy and its derivatives from specific internal energy, pressure, and density.
Definition: Numerics.C:220
MooseUnits pow(const MooseUnits &, int)
void v_from_arhoA_alpha_A(Real arhoA, Real alpha, Real A, Real &v, Real &dv_darhoA, Real &dv_dalpha)
Computes specific volume and its derivatives from alpha*rho*A, volume fraction, and area...
Definition: Numerics.C:113
bool absoluteFuzzyEqualVectors(const RealVectorValue &a, const RealVectorValue &b, const Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Tests if two real-valued vectors are equal within some absolute tolerance.
Definition: Numerics.C:18
Real applyQuotientRule(const Real &num, const Real &den, const Real &dnum_dy, const Real &dden_dy)
Computes a derivative of a fraction using quotient rule for a derivative w.r.t.
Definition: Numerics.C:39