DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
IdealGasPhysics.hpp
Go to the documentation of this file.
1#pragma once
2/**
3 * @file IdealGasPhysics.hpp
4 * @brief Shared ideal-gas thermodynamics and Roe-flux primitives.
5 *
6 * All functions are DNDS_DEVICE_CALLABLE (host + device) and operate on
7 * scalar arguments only, so they can be called from both the Eigen-based
8 * Euler module and the scalar-loop EulerP module.
9 *
10 * Primitive variable convention is parameterized via PrimVariable:
11 * - Pressure: prim[I4] stores pressure p (used by Euler)
12 * - InternalEnergy: prim[I4] stores rho * e_internal (used by EulerP)
13 */
14
15#include "DNDS/Defines.hpp"
16#include <cmath>
17
19{
20
21 /// Which thermodynamic variable is stored at the energy index of the
22 /// primitive state vector.
23 enum class PrimVariable
24 {
25 Pressure, ///< prim[I4] = p (Euler convention)
26 InternalEnergy, ///< prim[I4] = rho * e_internal (EulerP convention)
27 };
28
29 // -----------------------------------------------------------------
30 // Core thermodynamic relations (scalar, device-callable)
31 // -----------------------------------------------------------------
32
33 /**
34 * @brief Compute pressure, speed-of-sound squared, and specific enthalpy
35 * from total energy, density, and velocity squared.
36 */
37 DNDS_DEVICE_CALLABLE inline void
38 IdealGasThermal(real E, real rho, real vSqr, real gamma,
39 real &p, real &asqr, real &H)
40 {
41 p = (gamma - 1) * (E - rho * 0.5 * vSqr);
42 asqr = gamma * p / rho;
43 H = (E + p) / rho;
44 }
45
46 /// Pressure from internal energy: p = (gamma - 1) * e
49 {
50 return (gamma - 1) * e;
51 }
52
53 /// Internal energy from pressure: e = p / (gamma - 1)
56 {
57 return p / (gamma - 1);
58 }
59
60 /// Specific enthalpy from conservative state: H = (E + p) / rho
63 {
64 return (E + p) / rho;
65 }
66
67 /// Speed of sound squared: a^2 = gamma * p / rho
70 {
71 return gamma * p / rho;
72 }
73
74 // -----------------------------------------------------------------
75 // Conservative <-> Primitive conversion (scalar, per-component)
76 // -----------------------------------------------------------------
77
78 /**
79 * @brief Convert conservative energy to primitive energy-index value.
80 *
81 * @tparam prim Whether to store pressure or internal energy.
82 * @param E Total energy (conservative).
83 * @param rho Density.
84 * @param vSqr Velocity squared.
85 * @param gamma Ratio of specific heats.
86 * @return The value to store at prim[I4].
87 */
88 template <PrimVariable prim>
90 Cons2PrimEnergy(real E, real rho, real vSqr, real gamma)
91 {
92 real e = E - rho * 0.5 * vSqr; // rho * e_internal
93 if constexpr (prim == PrimVariable::Pressure)
94 return (gamma - 1) * e; // p
95 else
96 return e; // rho * e_internal
97 }
98
99 /**
100 * @brief Convert primitive energy-index value to conservative total energy.
101 *
102 * @tparam prim Whether prim[I4] stores pressure or internal energy.
103 * @param primE The primitive energy-index value (p or e).
104 * @param rho Density.
105 * @param vSqr Velocity squared.
106 * @param gamma Ratio of specific heats.
107 * @return Total energy E.
108 */
109 template <PrimVariable prim>
111 Prim2ConsEnergy(real primE, real rho, real vSqr, real gamma)
112 {
113 if constexpr (prim == PrimVariable::Pressure)
114 return primE / (gamma - 1) + rho * 0.5 * vSqr; // E = p/(gamma-1) + 0.5*rho*v^2
115 else
116 return primE + rho * 0.5 * vSqr; // E = e + 0.5*rho*v^2
117 }
118
119 /**
120 * @brief Get pressure from the primitive energy-index value.
121 *
122 * @tparam prim Whether prim[I4] stores pressure or internal energy.
123 * @param primE The primitive energy-index value.
124 * @param gamma Ratio of specific heats.
125 */
126 template <PrimVariable prim>
129 {
130 if constexpr (prim == PrimVariable::Pressure)
131 return primE;
132 else
133 return (gamma - 1) * primE;
134 }
135
136 // -----------------------------------------------------------------
137 // Roe decomposition primitives (scalar, device-callable)
138 // -----------------------------------------------------------------
139
140 /**
141 * @brief Roe-averaged speed of sound squared: a^2 = (gamma-1)(H - 0.5*v^2).
142 */
144 RoeSpeedOfSoundSqr(real gamma, real HRoe, real vsqrRoe)
145 {
146 return (gamma - 1) * (HRoe - 0.5 * vsqrRoe);
147 }
148
149 /**
150 * @brief Roe alpha-decomposition coefficients for the 1D wave structure.
151 *
152 * Computes the wave strengths alpha0, alpha1, alpha4 from the
153 * jump across the interface.
154 *
155 * @param incU0 Jump in density (UR(0) - UL(0)).
156 * @param incU123N Jump in normal momentum.
157 * @param incU4b Jump in (E - alpha23VT . veloRoe).
158 * @param veloRoeN Roe-averaged normal velocity.
159 * @param HRoe Roe-averaged enthalpy.
160 * @param asqrRoe Roe-averaged speed of sound squared.
161 * @param aRoe Roe-averaged speed of sound.
162 * @param gamma Ratio of specific heats.
163 * @param[out] alpha0 Left-going acoustic wave strength.
164 * @param[out] alpha1 Entropy wave strength.
165 * @param[out] alpha4 Right-going acoustic wave strength.
166 */
167 DNDS_DEVICE_CALLABLE inline void
168 RoeAlphaDecomposition(real incU0, real incU123N, real incU4b,
169 real veloRoeN, real HRoe,
170 real asqrRoe, real aRoe, real gamma,
171 real &alpha0, real &alpha1, real &alpha4)
172 {
173 alpha1 = (gamma - 1) / asqrRoe *
174 (incU0 * (HRoe - veloRoeN * veloRoeN) +
175 veloRoeN * incU123N - incU4b);
176 alpha0 = (incU0 * (veloRoeN + aRoe) - incU123N - aRoe * alpha1) / (2 * aRoe);
177 alpha4 = incU0 - (alpha0 + alpha1);
178 }
179
180 // -----------------------------------------------------------------
181 // Entropy fix constants (shared between Euler and EulerP)
182 // -----------------------------------------------------------------
183
184 static constexpr real kScaleHartenYee = 0.05;
185 static constexpr real kScaleLD = 0.2;
186 static constexpr real kScaleHFix = 0.25;
187
188 /**
189 * @brief H-correction + Harten-Yee entropy fix (scheme 8 in Euler module).
190 *
191 * This is the entropy fix scheme used by both Euler and EulerP.
192 */
193 DNDS_DEVICE_CALLABLE inline void
195 real dLambda, real fixScale,
196 real &lam0, real &lam123, real &lam4)
197 {
198 const real scaleHartenYee = kScaleHartenYee * fixScale;
199 const real scaleHFix = kScaleHFix * fixScale;
200
201 real aAve = 0.5 * (aL + aR);
202 real VAve = 0.5 * (vnL + vnR);
203
204 lam0 = std::max(lam0, dLambda * scaleHFix);
205 lam4 = std::max(lam4, dLambda * scaleHFix);
206 lam123 = std::max(lam123, dLambda * scaleHFix);
207
208 real thresholdHartenYee = scaleHartenYee * (VAve + aAve);
209 real thresholdHartenYeeS = thresholdHartenYee * thresholdHartenYee;
210 if (lam0 < thresholdHartenYee)
211 lam0 = (lam0 * lam0 + thresholdHartenYeeS) / (2 * thresholdHartenYee);
212 if (lam4 < thresholdHartenYee)
213 lam4 = (lam4 * lam4 + thresholdHartenYeeS) / (2 * thresholdHartenYee);
214 if (lam123 < thresholdHartenYee)
215 lam123 = (lam123 * lam123 + thresholdHartenYeeS) / (2 * thresholdHartenYee);
216 }
217
218} // namespace DNDS::IdealGas
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
#define DNDS_DEVICE_CALLABLE
Definition Defines.hpp:76
DNDS_DEVICE_CALLABLE real Cons2PrimEnergy(real E, real rho, real vSqr, real gamma)
Convert conservative energy to primitive energy-index value.
@ InternalEnergy
prim[I4] = rho * e_internal (EulerP convention)
@ Pressure
prim[I4] = p (Euler convention)
DNDS_DEVICE_CALLABLE real RoeSpeedOfSoundSqr(real gamma, real HRoe, real vsqrRoe)
Roe-averaged speed of sound squared: a^2 = (gamma-1)(H - 0.5*v^2).
DNDS_DEVICE_CALLABLE real PrimE2Pressure(real primE, real gamma)
Get pressure from the primitive energy-index value.
DNDS_DEVICE_CALLABLE void IdealGasThermal(real E, real rho, real vSqr, real gamma, real &p, real &asqr, real &H)
Compute pressure, speed-of-sound squared, and specific enthalpy from total energy,...
DNDS_DEVICE_CALLABLE void EntropyFix_HCorrHY(real aL, real aR, real vnL, real vnR, real dLambda, real fixScale, real &lam0, real &lam123, real &lam4)
H-correction + Harten-Yee entropy fix (scheme 8 in Euler module).
DNDS_DEVICE_CALLABLE real Pressure_From_InternalEnergy(real e, real gamma)
Pressure from internal energy: p = (gamma - 1) * e.
DNDS_DEVICE_CALLABLE real Enthalpy(real E, real rho, real p)
Specific enthalpy from conservative state: H = (E + p) / rho.
DNDS_DEVICE_CALLABLE real SpeedOfSoundSqr(real gamma, real p, real rho)
Speed of sound squared: a^2 = gamma * p / rho.
DNDS_DEVICE_CALLABLE void RoeAlphaDecomposition(real incU0, real incU123N, real incU4b, real veloRoeN, real HRoe, real asqrRoe, real aRoe, real gamma, real &alpha0, real &alpha1, real &alpha4)
Roe alpha-decomposition coefficients for the 1D wave structure.
DNDS_DEVICE_CALLABLE real InternalEnergy_From_Pressure(real p, real gamma)
Internal energy from pressure: e = p / (gamma - 1)
DNDS_DEVICE_CALLABLE real Prim2ConsEnergy(real primE, real rho, real vSqr, real gamma)
Convert primitive energy-index value to conservative total energy.
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:105