co2-eos 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
co2_eos/saturation.py ADDED
@@ -0,0 +1,156 @@
1
+ """CO2 saturation curve lookups — pure JAX, JIT-compilable.
2
+
3
+ Loads a precomputed saturation table (cubic spline coefficients) generated
4
+ by scripts/generate_saturation_table.py from the Span-Wagner EOS.
5
+
6
+ All lookup functions use cubic spline interpolation and are fully
7
+ differentiable and JIT-compilable. They accept scalar or array inputs.
8
+
9
+ Usage:
10
+ P = saturation_pressure(280.0) # eager
11
+ P = jax.jit(saturation_pressure)(280.0) # JIT
12
+ dPdT = jax.grad(saturation_pressure)(280.0) # differentiable
13
+ """
14
+
15
+ import jax
16
+ import jax.numpy as jnp
17
+ import numpy as np
18
+ from pathlib import Path
19
+
20
+ jax.config.update("jax_enable_x64", True)
21
+
22
+ _TABLE_PATH = Path(__file__).parent / "data" / "saturation_table.npz"
23
+
24
+ # ── Table loading (lazy, once) ───────────────────────────────────────────
25
+ # Table is loaded outside JIT tracing to avoid tracer leaks.
26
+ # JAX arrays are stored as module globals; JIT captures them as constants.
27
+
28
+ _TABLE = {}
29
+
30
+
31
+ def _ensure_loaded():
32
+ """Load saturation table on first use. Must be called outside JIT."""
33
+ if _TABLE:
34
+ return
35
+ data = np.load(_TABLE_PATH)
36
+ for k in data:
37
+ _TABLE[k] = jnp.array(data[k])
38
+
39
+
40
+ # ── Cubic spline evaluation ─────────────────────────────────────────────
41
+
42
+ def _eval_spline(x, breaks, coeffs):
43
+ """Evaluate cubic spline at x.
44
+
45
+ Args:
46
+ x: evaluation point(s), scalar or array
47
+ breaks: knot positions, shape (N,), ascending
48
+ coeffs: scipy CubicSpline coefficients, shape (4, N-1)
49
+ S(x) = c[0]*(x-x_i)^3 + c[1]*(x-x_i)^2 + c[2]*(x-x_i) + c[3]
50
+
51
+ Returns:
52
+ Interpolated value(s), same shape as x
53
+ """
54
+ n = coeffs.shape[1]
55
+ # Clamp x to valid range
56
+ x_c = jnp.clip(x, breaks[0], breaks[-1])
57
+ # Find interval index
58
+ idx = jnp.clip(jnp.searchsorted(breaks, x_c, side='right') - 1, 0, n - 1)
59
+ dx = x_c - breaks[idx]
60
+ # Horner evaluation: ((c0*dx + c1)*dx + c2)*dx + c3
61
+ return ((coeffs[0, idx] * dx + coeffs[1, idx]) * dx + coeffs[2, idx]) * dx + coeffs[3, idx]
62
+
63
+
64
+ # ── T-based lookups ─────────────────────────────────────────────────────
65
+
66
+ def saturation_pressure(T):
67
+ """Saturation pressure P_sat [Pa] at temperature T [K].
68
+
69
+ Valid for T in [216.592, 304.127] K (triple point to near-critical).
70
+ JIT-compilable and differentiable.
71
+ """
72
+ _ensure_loaded()
73
+ return _eval_spline(T, _TABLE['T_breaks'], _TABLE['P_sat_c'])
74
+
75
+
76
+ def saturation_densities(T):
77
+ """Saturated liquid and vapor densities [kg/m3] at temperature T [K].
78
+
79
+ Returns:
80
+ (rho_l, rho_v) — liquid and vapor densities
81
+ """
82
+ _ensure_loaded()
83
+ rho_l = _eval_spline(T, _TABLE['T_breaks'], _TABLE['rho_l_c'])
84
+ rho_v = _eval_spline(T, _TABLE['T_breaks'], _TABLE['rho_v_c'])
85
+ return rho_l, rho_v
86
+
87
+
88
+ def saturation_enthalpies_T(T):
89
+ """Saturated liquid and vapor enthalpies [J/kg] at temperature T [K].
90
+
91
+ Returns:
92
+ (h_l, h_v)
93
+ """
94
+ _ensure_loaded()
95
+ h_l = _eval_spline(T, _TABLE['T_breaks'], _TABLE['h_l_c'])
96
+ h_v = _eval_spline(T, _TABLE['T_breaks'], _TABLE['h_v_c'])
97
+ return h_l, h_v
98
+
99
+
100
+ def saturation_entropies(T):
101
+ """Saturated liquid and vapor entropies [J/(kg*K)] at temperature T [K].
102
+
103
+ Returns:
104
+ (s_l, s_v)
105
+ """
106
+ _ensure_loaded()
107
+ s_l = _eval_spline(T, _TABLE['T_breaks'], _TABLE['s_l_c'])
108
+ s_v = _eval_spline(T, _TABLE['T_breaks'], _TABLE['s_v_c'])
109
+ return s_l, s_v
110
+
111
+
112
+ # ── P-based lookups ─────────────────────────────────────────────────────
113
+
114
+ def saturation_temperature(P):
115
+ """Saturation temperature T_sat [K] at pressure P [Pa].
116
+
117
+ Valid for P in [P_triple, P_near_crit].
118
+ """
119
+ _ensure_loaded()
120
+ return _eval_spline(P, _TABLE['P_breaks'], _TABLE['T_sat_c'])
121
+
122
+
123
+ def saturation_enthalpies(P):
124
+ """Saturated liquid and vapor enthalpies [J/kg] at pressure P [Pa].
125
+
126
+ Returns:
127
+ (h_l, h_v)
128
+ """
129
+ _ensure_loaded()
130
+ h_l = _eval_spline(P, _TABLE['P_breaks'], _TABLE['h_l_P_c'])
131
+ h_v = _eval_spline(P, _TABLE['P_breaks'], _TABLE['h_v_P_c'])
132
+ return h_l, h_v
133
+
134
+
135
+ def saturation_densities_P(P):
136
+ """Saturated liquid and vapor densities [kg/m3] at pressure P [Pa].
137
+
138
+ Returns:
139
+ (rho_l, rho_v)
140
+ """
141
+ _ensure_loaded()
142
+ rho_l = _eval_spline(P, _TABLE['P_breaks'], _TABLE['rho_l_P_c'])
143
+ rho_v = _eval_spline(P, _TABLE['P_breaks'], _TABLE['rho_v_P_c'])
144
+ return rho_l, rho_v
145
+
146
+
147
+ def saturation_entropies_P(P):
148
+ """Saturated liquid and vapor entropies [J/(kg*K)] at pressure P [Pa].
149
+
150
+ Returns:
151
+ (s_l, s_v)
152
+ """
153
+ _ensure_loaded()
154
+ s_l = _eval_spline(P, _TABLE['P_breaks'], _TABLE['s_l_P_c'])
155
+ s_v = _eval_spline(P, _TABLE['P_breaks'], _TABLE['s_v_P_c'])
156
+ return s_l, s_v
co2_eos/span_wagner.py ADDED
@@ -0,0 +1,501 @@
1
+ """Span-Wagner (1996) equation of state for CO₂ — pure JAX implementation.
2
+
3
+ Implements the complete reduced Helmholtz energy α(τ,δ) = α⁰(τ,δ) + αʳ(τ,δ)
4
+ from: Span & Wagner, J. Phys. Chem. Ref. Data 25, 1509-1596 (1996).
5
+
6
+ Coefficients sourced from the original paper and cross-referenced against
7
+ CoolProp's machine-readable fluids/CarbonDioxide.json.
8
+
9
+ All thermodynamic properties are derived via jax.grad — no hand-coded
10
+ derivative formulas. CoolProp is never imported here.
11
+ """
12
+
13
+ import jax
14
+ import jax.numpy as jnp
15
+ from functools import partial
16
+
17
+ # Enable float64
18
+ jax.config.update("jax_enable_x64", True)
19
+
20
+ # ── Critical constants ──────────────────────────────────────────────────────
21
+ TC = 304.1282 # K
22
+ RHOC_MOLAR = 10624.9063 # mol/m³
23
+ M = 0.0440098 # kg/mol
24
+ RHOC = RHOC_MOLAR * M # kg/m³ ≈ 467.60
25
+ R_MOLAR = 8.31451 # J/(mol·K) (value used in Span-Wagner)
26
+ R = R_MOLAR / M # J/(kg·K) ≈ 188.9241
27
+ PC = 7377300.0 # Pa
28
+ T_TRIPLE = 216.592 # K — CO₂ triple-point temperature
29
+
30
+
31
+ # ═══════════════════════════════════════════════════════════════════════════
32
+ # Ideal-gas Helmholtz energy α⁰(τ, δ)
33
+ # ═══════════════════════════════════════════════════════════════════════════
34
+ # α⁰ = a1 + a2·τ + (a_logtau - 1)·ln(τ) + ln(δ)
35
+ # + Σ nk · ln(1 - exp(-θk·τ))
36
+ #
37
+ # The (a_logtau - 1) form comes from CoolProp's convention where the log
38
+ # coefficient is the cp0/R ideal-gas heat capacity constant. For CO₂,
39
+ # a_logtau = 2.5 ⟹ coefficient of ln(τ) is 1.5.
40
+
41
+ # Lead terms (includes IIR enthalpy/entropy offset: a1_off=-14.4979156224319,
42
+ # a2_off=8.82013935801453 already folded into a1, a2)
43
+ _A0_A1 = 8.37304456 + (-14.4979156224319)
44
+ _A0_A2 = -3.70454304 + 8.82013935801453
45
+ _A0_LOGTAU = 2.5 # coefficient of ln(τ) — CoolProp IdealGasHelmholtzLogTau
46
+
47
+ _A0_PE_N = jnp.array([1.99427042, 0.62105248, 0.41195293,
48
+ 1.04028922, 0.08327678])
49
+ _A0_PE_T = jnp.array([3.15163, 6.1119, 6.77708, 11.32384, 27.08792])
50
+
51
+
52
+ def alpha0(tau, delta):
53
+ """Ideal-gas reduced Helmholtz energy α⁰(τ, δ).
54
+
55
+ Args:
56
+ tau: reduced inverse temperature Tc/T (scalar)
57
+ delta: reduced density ρ/ρc (scalar)
58
+
59
+ Returns:
60
+ α⁰ value (scalar)
61
+ """
62
+ result = _A0_A1 + _A0_A2 * tau + _A0_LOGTAU * jnp.log(tau) + jnp.log(delta)
63
+ # Planck-Einstein terms
64
+ result += jnp.sum(_A0_PE_N * jnp.log(1.0 - jnp.exp(-_A0_PE_T * tau)))
65
+ return result
66
+
67
+
68
+ # ═══════════════════════════════════════════════════════════════════════════
69
+ # Residual Helmholtz energy αʳ(τ, δ)
70
+ # ═══════════════════════════════════════════════════════════════════════════
71
+
72
+ # ── Polynomial + exponential terms (34 terms total) ─────────────────────
73
+ # Terms 1-7: l=0 (pure polynomial)
74
+ # Terms 8-34: l>0 (exponential: multiply by exp(-δ^l))
75
+
76
+ _AR_N = jnp.array([
77
+ 0.388568232032, 2.93854759427, -5.5867188535,
78
+ -0.767531995925, 0.317290055804, 0.548033158978,
79
+ 0.122794112203, 2.16589615432, 1.58417351097,
80
+ -0.231327054055, 0.0581169164314, -0.553691372054,
81
+ 0.489466159094, -0.0242757398435, 0.0624947905017,
82
+ -0.121758602252, -0.370556852701, -0.0167758797004,
83
+ -0.11960736638, -0.0456193625088, 0.0356127892703,
84
+ -0.00744277271321, -0.00173957049024, -0.0218101212895,
85
+ 0.0243321665592, -0.0374401334235, 0.143387157569,
86
+ -0.134919690833, -0.0231512250535, 0.0123631254929,
87
+ 0.00210583219729, -0.000339585190264, 0.00559936517716,
88
+ -0.000303351180556,
89
+ ])
90
+
91
+ _AR_D = jnp.array([
92
+ 1, 1, 1, 1, 2, 2, 3,
93
+ 1, 2, 4, 5, 5, 5, 6, 6, 6,
94
+ 1, 1, 4, 4, 4, 7, 8,
95
+ 2, 3, 3, 5, 5, 6, 7, 8, 10,
96
+ 4, 8,
97
+ ], dtype=jnp.float64)
98
+
99
+ _AR_T = jnp.array([
100
+ 0.0, 0.75, 1.0, 2.0, 0.75, 2.0, 0.75,
101
+ 1.5, 1.5, 2.5, 0.0, 1.5, 2.0, 0.0, 1.0, 2.0,
102
+ 3.0, 6.0, 3.0, 6.0, 8.0, 6.0, 0.0,
103
+ 7.0, 12.0, 16.0, 22.0, 24.0, 16.0, 24.0, 8.0, 2.0,
104
+ 28.0, 14.0,
105
+ ])
106
+
107
+ _AR_L = jnp.array([
108
+ 0, 0, 0, 0, 0, 0, 0,
109
+ 1, 1, 1, 1, 1, 1, 1, 1, 1,
110
+ 2, 2, 2, 2, 2, 2, 2,
111
+ 3, 3, 3, 4, 4, 4, 4, 4, 4,
112
+ 5, 6,
113
+ ], dtype=jnp.float64)
114
+
115
+ # ── Gaussian bell-shaped terms (5 terms, indices 35-39) ─────────────────
116
+
117
+ _AR_GAUSS_N = jnp.array([
118
+ -213.654886883, 26641.5691493, -24027.2122046,
119
+ -283.41603424, 212.472844002,
120
+ ])
121
+
122
+ _AR_GAUSS_D = jnp.array([2, 2, 2, 3, 3], dtype=jnp.float64)
123
+ _AR_GAUSS_T = jnp.array([1.0, 0.0, 1.0, 3.0, 3.0])
124
+
125
+ _AR_GAUSS_ETA = jnp.array([25.0, 25.0, 25.0, 15.0, 20.0])
126
+ _AR_GAUSS_BETA = jnp.array([325.0, 300.0, 300.0, 275.0, 275.0])
127
+ _AR_GAUSS_GAMMA = jnp.array([1.16, 1.19, 1.19, 1.25, 1.22])
128
+ _AR_GAUSS_EPS = jnp.array([1.0, 1.0, 1.0, 1.0, 1.0])
129
+
130
+ # ── Non-analytic terms (3 terms, indices 40-42) ─────────────────────────
131
+
132
+ _NA_N = jnp.array([-0.666422765408, 0.726086323499, 0.0550686686128])
133
+ _NA_A = jnp.array([3.5, 3.5, 3.0])
134
+ _NA_B = jnp.array([0.875, 0.925, 0.875])
135
+ _NA_BETA = jnp.array([0.3, 0.3, 0.3])
136
+ _NA_BIG_A = jnp.array([0.7, 0.7, 0.7])
137
+ _NA_BIG_B = jnp.array([0.3, 0.3, 1.0])
138
+ _NA_BIG_C = jnp.array([10.0, 10.0, 12.5])
139
+ _NA_BIG_D = jnp.array([275.0, 275.0, 275.0])
140
+
141
+
142
+ def alphar(tau, delta):
143
+ """Residual reduced Helmholtz energy αʳ(τ, δ).
144
+
145
+ Args:
146
+ tau: reduced inverse temperature Tc/T (scalar)
147
+ delta: reduced density ρ/ρc (scalar)
148
+
149
+ Returns:
150
+ αʳ value (scalar)
151
+ """
152
+ # ── Polynomial + exponential terms ──
153
+ # αʳ_poly = Σ nk · δ^dk · τ^tk · exp(-δ^lk)
154
+ # For lk = 0, the exp factor is 1.
155
+ powers = delta ** _AR_D * tau ** _AR_T
156
+ # exp(-δ^l) factor: when l=0, δ^0=1, exp(-1)≠1, so we need a mask
157
+ exp_factor = jnp.where(_AR_L > 0, jnp.exp(-delta ** _AR_L), 1.0)
158
+ poly_exp = jnp.sum(_AR_N * powers * exp_factor)
159
+
160
+ # ── Gaussian bell-shaped terms ──
161
+ # αʳ_gauss = Σ nk · δ^dk · τ^tk · exp(-ηk(δ-εk)² - βk(τ-γk)²)
162
+ gauss_powers = delta ** _AR_GAUSS_D * tau ** _AR_GAUSS_T
163
+ gauss_exp = jnp.exp(
164
+ -_AR_GAUSS_ETA * (delta - _AR_GAUSS_EPS) ** 2
165
+ - _AR_GAUSS_BETA * (tau - _AR_GAUSS_GAMMA) ** 2
166
+ )
167
+ gauss = jnp.sum(_AR_GAUSS_N * gauss_powers * gauss_exp)
168
+
169
+ # ── Non-analytic terms ──
170
+ # θ = (1 - τ) + A·[(δ-1)²]^(1/(2β))
171
+ # Δ = θ² + B·[(δ-1)²]^a
172
+ # Ψ = exp(-C·(δ-1)² - D·(τ-1)²)
173
+ # αʳ_na = Σ nk · Δ^bk · δ · Ψ
174
+ dm1_sq = (delta - 1.0) ** 2
175
+ theta = (1.0 - tau) + _NA_BIG_A * (dm1_sq ** (1.0 / (2.0 * _NA_BETA)))
176
+ big_delta = theta ** 2 + _NA_BIG_B * (dm1_sq ** _NA_A)
177
+ psi = jnp.exp(-_NA_BIG_C * dm1_sq - _NA_BIG_D * (tau - 1.0) ** 2)
178
+
179
+ # Δ^b: use safe power to handle Δ=0 at exact critical point
180
+ big_delta_safe = jnp.maximum(big_delta, 1e-300)
181
+ delta_b = big_delta_safe ** _NA_B
182
+
183
+ na = jnp.sum(_NA_N * delta_b * delta * psi)
184
+
185
+ return poly_exp + gauss + na
186
+
187
+
188
+ def alpha(tau, delta):
189
+ """Total reduced Helmholtz energy α(τ, δ) = α⁰ + αʳ."""
190
+ return alpha0(tau, delta) + alphar(tau, delta)
191
+
192
+
193
+ # ═══════════════════════════════════════════════════════════════════════════
194
+ # Physical property functions (all via autodiff from α)
195
+ # ═══════════════════════════════════════════════════════════════════════════
196
+ #
197
+ # Working in reduced variables throughout:
198
+ # τ = Tc/T, δ = ρ/ρc
199
+ #
200
+ # The Helmholtz free energy per unit mass is:
201
+ # A(T, ρ) = R·T·α(τ, δ)
202
+ #
203
+ # All properties below take (T, ρ) in physical units and return physical
204
+ # units. Derivatives of α w.r.t. τ and δ are obtained via jax.grad.
205
+
206
+ # ── Derivative building blocks ──────────────────────────────────────────
207
+
208
+ def _alpha_of_T_rho(T, rho):
209
+ """α(τ(T), δ(ρ)) as a function of physical T, ρ — for autodiff."""
210
+ tau = TC / T
211
+ delta = rho / RHOC
212
+ return alpha(tau, delta)
213
+
214
+
215
+ def _alphar_of_T_rho(T, rho):
216
+ """αʳ(τ(T), δ(ρ)) as a function of physical T, ρ."""
217
+ tau = TC / T
218
+ delta = rho / RHOC
219
+ return alphar(tau, delta)
220
+
221
+
222
+ # Reduced-variable derivatives of αʳ (used in property formulas)
223
+ _dalr_ddelta = jax.grad(alphar, argnums=1)
224
+ _dalr_dtau = jax.grad(alphar, argnums=0)
225
+ _d2alr_ddelta2 = jax.grad(lambda tau, delta: _dalr_ddelta(tau, delta), argnums=1)
226
+ _d2alr_dtau2 = jax.grad(lambda tau, delta: _dalr_dtau(tau, delta), argnums=0)
227
+ _d2alr_ddelta_dtau = jax.grad(lambda tau, delta: _dalr_ddelta(tau, delta), argnums=0)
228
+
229
+ # Reduced-variable derivatives of α⁰
230
+ _dal0_ddelta = jax.grad(alpha0, argnums=1)
231
+ _dal0_dtau = jax.grad(alpha0, argnums=0)
232
+ _d2al0_dtau2 = jax.grad(lambda tau, delta: jax.grad(alpha0, argnums=0)(tau, delta), argnums=0)
233
+
234
+
235
+ # ── Scalar property functions (τ, δ inputs) ────────────────────────────
236
+
237
+ def _pressure_reduced(tau, delta):
238
+ """P / (ρ·R·T) = 1 + δ·αʳ_δ."""
239
+ return 1.0 + delta * _dalr_ddelta(tau, delta)
240
+
241
+
242
+ def _cv_reduced(tau, delta):
243
+ """Cv / R = -τ²·(α⁰_ττ + αʳ_ττ)."""
244
+ return -tau ** 2 * (_d2al0_dtau2(tau, delta) + _d2alr_dtau2(tau, delta))
245
+
246
+
247
+ def _cp_reduced(tau, delta):
248
+ """Cp / R = Cv/R + (1 + δ·αʳ_δ - δ·τ·αʳ_δτ)² / (1 + 2δ·αʳ_δ + δ²·αʳ_δδ)."""
249
+ cv_r = _cv_reduced(tau, delta)
250
+ alr_d = _dalr_ddelta(tau, delta)
251
+ alr_dd = _d2alr_ddelta2(tau, delta)
252
+ alr_dt = _d2alr_ddelta_dtau(tau, delta)
253
+
254
+ num = (1.0 + delta * alr_d - delta * tau * alr_dt) ** 2
255
+ den = 1.0 + 2.0 * delta * alr_d + delta ** 2 * alr_dd
256
+ return cv_r + num / den
257
+
258
+
259
+ def _speed_of_sound_sq_reduced(tau, delta):
260
+ """w² / (R·T) = 1 + 2δ·αʳ_δ + δ²·αʳ_δδ
261
+ + (1 + δ·αʳ_δ - δ·τ·αʳ_δτ)² / (τ²·(α⁰_ττ + αʳ_ττ))
262
+ Note: the denominator in the last term is -Cv/R (positive for stable fluids).
263
+ """
264
+ alr_d = _dalr_ddelta(tau, delta)
265
+ alr_dd = _d2alr_ddelta2(tau, delta)
266
+ alr_dt = _d2alr_ddelta_dtau(tau, delta)
267
+ al0_tt = _d2al0_dtau2(tau, delta)
268
+ alr_tt = _d2alr_dtau2(tau, delta)
269
+
270
+ mechanical = 1.0 + 2.0 * delta * alr_d + delta ** 2 * alr_dd
271
+ num = (1.0 + delta * alr_d - delta * tau * alr_dt) ** 2
272
+ den = tau ** 2 * (al0_tt + alr_tt) # this is -Cv/R, negative
273
+ return mechanical - num / den # minus minus = plus
274
+
275
+
276
+ # ── Physical property functions (T, ρ inputs → physical units) ──────────
277
+
278
+ def _scalar_pressure(T, rho):
279
+ """Pressure [Pa] at scalar (T, ρ)."""
280
+ tau = TC / T
281
+ delta = rho / RHOC
282
+ return rho * R * T * _pressure_reduced(tau, delta)
283
+
284
+
285
+ def _scalar_cv(T, rho):
286
+ """Isochoric heat capacity Cv [J/(kg·K)] at scalar (T, ρ)."""
287
+ tau = TC / T
288
+ delta = rho / RHOC
289
+ return R * _cv_reduced(tau, delta)
290
+
291
+
292
+ def _scalar_cp(T, rho):
293
+ """Isobaric heat capacity Cp [J/(kg·K)] at scalar (T, ρ)."""
294
+ tau = TC / T
295
+ delta = rho / RHOC
296
+ return R * _cp_reduced(tau, delta)
297
+
298
+
299
+ def _scalar_speed_of_sound(T, rho):
300
+ """Speed of sound [m/s] at scalar (T, ρ)."""
301
+ tau = TC / T
302
+ delta = rho / RHOC
303
+ w_sq = R * T * _speed_of_sound_sq_reduced(tau, delta)
304
+ return jnp.sqrt(jnp.maximum(w_sq, 0.0))
305
+
306
+
307
+ def _scalar_enthalpy(T, rho):
308
+ """Specific enthalpy h [J/kg] at scalar (T, ρ).
309
+
310
+ h = R·T·(τ·(α⁰_τ + αʳ_τ) + δ·αʳ_δ + 1)
311
+ """
312
+ tau = TC / T
313
+ delta = rho / RHOC
314
+ al0_t = _dal0_dtau(tau, delta)
315
+ alr_t = _dalr_dtau(tau, delta)
316
+ alr_d = _dalr_ddelta(tau, delta)
317
+ return R * T * (tau * (al0_t + alr_t) + delta * alr_d + 1.0)
318
+
319
+
320
+ def _scalar_internal_energy(T, rho):
321
+ """Specific internal energy u [J/kg] at scalar (T, ρ).
322
+
323
+ u = R·T·τ·(α⁰_τ + αʳ_τ)
324
+ """
325
+ tau = TC / T
326
+ delta = rho / RHOC
327
+ al0_t = _dal0_dtau(tau, delta)
328
+ alr_t = _dalr_dtau(tau, delta)
329
+ return R * T * tau * (al0_t + alr_t)
330
+
331
+
332
+ def _scalar_entropy(T, rho):
333
+ """Specific entropy s [J/(kg·K)] at scalar (T, ρ).
334
+
335
+ s = R·(τ·(α⁰_τ + αʳ_τ) - α⁰ - αʳ)
336
+ """
337
+ tau = TC / T
338
+ delta = rho / RHOC
339
+ al0_t = _dal0_dtau(tau, delta)
340
+ alr_t = _dalr_dtau(tau, delta)
341
+ al0_val = alpha0(tau, delta)
342
+ alr_val = alphar(tau, delta)
343
+ return R * (tau * (al0_t + alr_t) - al0_val - alr_val)
344
+
345
+
346
+ # ═══════════════════════════════════════════════════════════════════════════
347
+ # Public API — batched, JIT-compiled, vmappable
348
+ # ═══════════════════════════════════════════════════════════════════════════
349
+
350
+ @jax.jit
351
+ def pressure(T, rho):
352
+ """Pressure [Pa]. Batched over leading axis."""
353
+ return jax.vmap(_scalar_pressure)(T, rho)
354
+
355
+
356
+ @jax.jit
357
+ def cv(T, rho):
358
+ """Isochoric heat capacity Cv [J/(kg·K)]. Batched."""
359
+ return jax.vmap(_scalar_cv)(T, rho)
360
+
361
+
362
+ @jax.jit
363
+ def cp(T, rho):
364
+ """Isobaric heat capacity Cp [J/(kg·K)]. Batched."""
365
+ return jax.vmap(_scalar_cp)(T, rho)
366
+
367
+
368
+ @jax.jit
369
+ def speed_of_sound(T, rho):
370
+ """Speed of sound [m/s]. Batched."""
371
+ return jax.vmap(_scalar_speed_of_sound)(T, rho)
372
+
373
+
374
+ @jax.jit
375
+ def enthalpy(T, rho):
376
+ """Specific enthalpy h [J/kg]. Batched."""
377
+ return jax.vmap(_scalar_enthalpy)(T, rho)
378
+
379
+
380
+ @jax.jit
381
+ def internal_energy(T, rho):
382
+ """Specific internal energy u [J/kg]. Batched."""
383
+ return jax.vmap(_scalar_internal_energy)(T, rho)
384
+
385
+
386
+ @jax.jit
387
+ def entropy(T, rho):
388
+ """Specific entropy s [J/(kg·K)]. Batched."""
389
+ return jax.vmap(_scalar_entropy)(T, rho)
390
+
391
+
392
+ @jax.jit
393
+ def all_properties(T, rho):
394
+ """Compute all properties at once. Returns dict.
395
+
396
+ Keys: P [Pa], Cv [J/(kg·K)], Cp [J/(kg·K)], w [m/s],
397
+ h [J/kg], u [J/kg], s [J/(kg·K)]
398
+ """
399
+ return {
400
+ "P": pressure(T, rho),
401
+ "Cv": cv(T, rho),
402
+ "Cp": cp(T, rho),
403
+ "w": speed_of_sound(T, rho),
404
+ "h": enthalpy(T, rho),
405
+ "u": internal_energy(T, rho),
406
+ "s": entropy(T, rho),
407
+ }
408
+
409
+
410
+ # ── Gibbs energy ─────────────────────────────────────────────────────────
411
+
412
+ def _scalar_gibbs_energy(T, rho):
413
+ """Specific Gibbs energy g = h - T·s [J/kg] at scalar (T, ρ)."""
414
+ tau = TC / T
415
+ delta = rho / RHOC
416
+ al0_t = _dal0_dtau(tau, delta)
417
+ alr_t = _dalr_dtau(tau, delta)
418
+ alr_d = _dalr_ddelta(tau, delta)
419
+ al0_val = alpha0(tau, delta)
420
+ alr_val = alphar(tau, delta)
421
+ # g = h - T·s = R·T·(1 + δ·αʳ_δ + α⁰ + αʳ)
422
+ return R * T * (1.0 + delta * alr_d + al0_val + alr_val)
423
+
424
+
425
+ @jax.jit
426
+ def gibbs_energy(T, rho):
427
+ """Specific Gibbs energy g [J/kg]. Batched."""
428
+ return jax.vmap(_scalar_gibbs_energy)(T, rho)
429
+
430
+
431
+ # ═══════════════════════════════════════════════════════════════════════════
432
+ # Shared-derivative property bundle (simulation hot path)
433
+ # ═══════════════════════════════════════════════════════════════════════════
434
+
435
+ def _scalar_all_properties_shared(T, rho):
436
+ """Compute all 7 properties + Gibbs energy from 5 shared reduced derivatives.
437
+
438
+ Computes αʳ_δ, αʳ_ττ, αʳ_δδ, αʳ_δτ, α⁰_ττ once, then derives everything.
439
+ """
440
+ tau = TC / T
441
+ delta = rho / RHOC
442
+
443
+ # ── Evaluate the 5 reduced derivatives (+ values needed for h, s, g) ──
444
+ alr_d = _dalr_ddelta(tau, delta)
445
+ alr_tt = _d2alr_dtau2(tau, delta)
446
+ alr_dd = _d2alr_ddelta2(tau, delta)
447
+ alr_dt = _d2alr_ddelta_dtau(tau, delta)
448
+ al0_tt = _d2al0_dtau2(tau, delta)
449
+
450
+ # For enthalpy, entropy, Gibbs: also need τ-first-derivatives and values
451
+ al0_t = _dal0_dtau(tau, delta)
452
+ alr_t = _dalr_dtau(tau, delta)
453
+ al0_val = alpha0(tau, delta)
454
+ alr_val = alphar(tau, delta)
455
+
456
+ # ── Derive all properties from shared derivatives ──
457
+
458
+ # P = ρ·R·T·(1 + δ·αʳ_δ)
459
+ P = rho * R * T * (1.0 + delta * alr_d)
460
+
461
+ # Cv = -R·τ²·(α⁰_ττ + αʳ_ττ)
462
+ cv_val = -R * tau ** 2 * (al0_tt + alr_tt)
463
+
464
+ # Cp numerator/denominator terms (reused for speed of sound)
465
+ num = (1.0 + delta * alr_d - delta * tau * alr_dt) ** 2
466
+ den = 1.0 + 2.0 * delta * alr_d + delta ** 2 * alr_dd
467
+
468
+ # Cp = Cv + R·num/den
469
+ cp_val = cv_val + R * num / den
470
+
471
+ # w² = R·T·(den - num / (τ²·(α⁰_ττ + αʳ_ττ)))
472
+ w_sq = R * T * (den - num / (tau ** 2 * (al0_tt + alr_tt)))
473
+ w_val = jnp.sqrt(jnp.maximum(w_sq, 0.0))
474
+
475
+ # h = R·T·(τ·(α⁰_τ + αʳ_τ) + δ·αʳ_δ + 1)
476
+ h_val = R * T * (tau * (al0_t + alr_t) + delta * alr_d + 1.0)
477
+
478
+ # u = R·T·τ·(α⁰_τ + αʳ_τ)
479
+ u_val = R * T * tau * (al0_t + alr_t)
480
+
481
+ # s = R·(τ·(α⁰_τ + αʳ_τ) - α⁰ - αʳ)
482
+ s_val = R * (tau * (al0_t + alr_t) - al0_val - alr_val)
483
+
484
+ # g = h - T·s = R·T·(1 + δ·αʳ_δ + α⁰ + αʳ)
485
+ g_val = R * T * (1.0 + delta * alr_d + al0_val + alr_val)
486
+
487
+ return P, cv_val, cp_val, w_val, h_val, u_val, s_val, g_val
488
+
489
+
490
+ @jax.jit
491
+ def all_properties_shared(T, rho):
492
+ """Compute all properties from shared reduced derivatives. Returns dict.
493
+
494
+ Equivalent to all_properties() but computes the 5 key reduced derivatives
495
+ once and reuses them, avoiding redundant autodiff evaluations.
496
+
497
+ Keys: P [Pa], Cv [J/(kg·K)], Cp [J/(kg·K)], w [m/s],
498
+ h [J/kg], u [J/kg], s [J/(kg·K)], g [J/kg]
499
+ """
500
+ P, Cv, Cp, w, h, u, s, g = jax.vmap(_scalar_all_properties_shared)(T, rho)
501
+ return {"P": P, "Cv": Cv, "Cp": Cp, "w": w, "h": h, "u": u, "s": s, "g": g}