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/__init__.py +265 -0
- co2_eos/data/saturation_table.npz +0 -0
- co2_eos/inversions.py +695 -0
- co2_eos/saturation.py +156 -0
- co2_eos/span_wagner.py +501 -0
- co2_eos/transport.py +286 -0
- co2_eos-0.1.0.dist-info/METADATA +193 -0
- co2_eos-0.1.0.dist-info/RECORD +11 -0
- co2_eos-0.1.0.dist-info/WHEEL +5 -0
- co2_eos-0.1.0.dist-info/licenses/LICENSE +201 -0
- co2_eos-0.1.0.dist-info/top_level.txt +1 -0
co2_eos/transport.py
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"""CO₂ transport property correlations — pure JAX implementation.
|
|
2
|
+
|
|
3
|
+
Viscosity: Laesecke & Muzny, JPCRD 46, 013107 (2017)
|
|
4
|
+
Thermal conductivity: Huber, Sykioti, Assael & Perkins, JPCRD 45, 013102 (2016)
|
|
5
|
+
|
|
6
|
+
Coefficients cross-referenced against CoolProp's CarbonDioxide.json and
|
|
7
|
+
TransportRoutines.cpp. CoolProp is never imported here.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import jax
|
|
11
|
+
import jax.numpy as jnp
|
|
12
|
+
from functools import partial
|
|
13
|
+
|
|
14
|
+
jax.config.update("jax_enable_x64", True)
|
|
15
|
+
|
|
16
|
+
# ── Shared constants ──────────────────────────────────────────────────────
|
|
17
|
+
# Use the same values as the Span-Wagner EOS module.
|
|
18
|
+
TC = 304.1282 # K
|
|
19
|
+
RHOC_MOLAR = 10624.9063 # mol/m³
|
|
20
|
+
M = 0.0440098 # kg/mol
|
|
21
|
+
R_MOLAR = 8.31451 # J/(mol·K)
|
|
22
|
+
PC = 7377300.0 # Pa
|
|
23
|
+
RHOC_MASS = RHOC_MOLAR * M # kg/m³ ≈ 467.60
|
|
24
|
+
|
|
25
|
+
# ═══════════════════════════════════════════════════════════════════════════
|
|
26
|
+
# Residual Helmholtz derivatives (local copy to keep this module standalone)
|
|
27
|
+
# ═══════════════════════════════════════════════════════════════════════════
|
|
28
|
+
# We need αʳ_δ and αʳ_δδ for the conductivity critical enhancement.
|
|
29
|
+
# Import the alphar function and build grads here rather than importing
|
|
30
|
+
# private functions from span_wagner.
|
|
31
|
+
from co2_eos.span_wagner import alpha0, alphar
|
|
32
|
+
|
|
33
|
+
_dalr_ddelta = jax.grad(alphar, argnums=1)
|
|
34
|
+
_d2alr_ddelta2 = jax.grad(lambda tau, delta: _dalr_ddelta(tau, delta), argnums=1)
|
|
35
|
+
_d2al0_dtau2 = jax.grad(jax.grad(alpha0, argnums=0), argnums=0)
|
|
36
|
+
_d2alr_dtau2 = jax.grad(jax.grad(alphar, argnums=0), argnums=0)
|
|
37
|
+
_d2alr_ddelta_dtau = jax.grad(lambda t, d: _dalr_ddelta(t, d), argnums=0)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
# ═══════════════════════════════════════════════════════════════════════════
|
|
41
|
+
# VISCOSITY — Laesecke & Muzny (2017)
|
|
42
|
+
# ═══════════════════════════════════════════════════════════════════════════
|
|
43
|
+
|
|
44
|
+
# ── Dilute gas η₀(T) — Eq. (4) ───────────────────────────────────────────
|
|
45
|
+
_ETA0_A = jnp.array([
|
|
46
|
+
1749.354893188350,
|
|
47
|
+
-369.069300007128,
|
|
48
|
+
5423856.34887691,
|
|
49
|
+
-2.21283852168356,
|
|
50
|
+
-269503.247933569,
|
|
51
|
+
73145.021531826,
|
|
52
|
+
5.34368649509278,
|
|
53
|
+
])
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _eta_dilute(T):
|
|
57
|
+
"""Dilute-gas viscosity η₀(T) [Pa·s]."""
|
|
58
|
+
a = _ETA0_A
|
|
59
|
+
T_sixth = T ** (1.0 / 6.0)
|
|
60
|
+
T_third = T ** (1.0 / 3.0)
|
|
61
|
+
den = (a[0]
|
|
62
|
+
+ a[1] * T_sixth
|
|
63
|
+
+ a[2] * jnp.exp(a[3] * T_third)
|
|
64
|
+
+ (a[4] + a[5] * T_third) / jnp.exp(T_third)
|
|
65
|
+
+ a[6] * jnp.sqrt(T))
|
|
66
|
+
return 0.0010055 * jnp.sqrt(T) / den
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# ── Initial density dependence (Rainwater-Friend) ────────────────────────
|
|
70
|
+
_EPSILON_OVER_K = 200.76 # K
|
|
71
|
+
_SIGMA = 3.78421e-10 # m
|
|
72
|
+
_NA = 6.02214129e23 # 1/mol
|
|
73
|
+
|
|
74
|
+
_RF_B = jnp.array([
|
|
75
|
+
-19.572881, 219.73999, -1015.3226, 2471.0125,
|
|
76
|
+
-3375.1717, 2491.6597, -787.26086, 14.085455, -0.34664158,
|
|
77
|
+
])
|
|
78
|
+
_RF_T = jnp.array([0.0, -0.25, -0.5, -0.75, -1.0, -1.25, -1.5, -2.5, -5.5])
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _eta_initial(T, rho_molar, eta0):
|
|
82
|
+
"""Initial-density viscosity contribution [Pa·s].
|
|
83
|
+
|
|
84
|
+
η_initial = η₀ · B_η · ρ_molar
|
|
85
|
+
"""
|
|
86
|
+
T_star = T / _EPSILON_OVER_K
|
|
87
|
+
B_eta_star = jnp.sum(_RF_B * T_star ** _RF_T)
|
|
88
|
+
B_eta = _NA * _SIGMA ** 3 * B_eta_star # m³/mol
|
|
89
|
+
return eta0 * B_eta * rho_molar
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
# ── Higher-order (residual) — Eqs. (8)-(9) ───────────────────────────────
|
|
93
|
+
_TT = 216.592 # K — triple-point temperature
|
|
94
|
+
_RHO_TL = 1178.53 # kg/m³ — triple-point saturated liquid density
|
|
95
|
+
_C1 = 0.360603235428487
|
|
96
|
+
_C2 = 0.121550806591497
|
|
97
|
+
_GAMMA_VISC = 8.06282737481277
|
|
98
|
+
|
|
99
|
+
# Reference viscosity scale η_tL — Eq. (9)
|
|
100
|
+
_ETA_TL = (_RHO_TL ** (2.0 / 3.0)
|
|
101
|
+
* (R_MOLAR * _TT) ** 0.5
|
|
102
|
+
/ (M ** (1.0 / 6.0) * 84446887.43579945))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _eta_residual(T, rho_mass):
|
|
106
|
+
"""Higher-order viscosity contribution [Pa·s]."""
|
|
107
|
+
Tr = T / _TT
|
|
108
|
+
rhor = rho_mass / _RHO_TL
|
|
109
|
+
return _ETA_TL * (_C1 * Tr * rhor ** 3
|
|
110
|
+
+ (rhor ** 2 + rhor ** _GAMMA_VISC) / (Tr - _C2))
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# ── Scalar viscosity ─────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
def _scalar_viscosity(T, rho):
|
|
116
|
+
"""Dynamic viscosity μ [Pa·s] at scalar (T, ρ [kg/m³])."""
|
|
117
|
+
rho_molar = rho / M
|
|
118
|
+
eta0 = _eta_dilute(T)
|
|
119
|
+
eta_init = _eta_initial(T, rho_molar, eta0)
|
|
120
|
+
eta_res = _eta_residual(T, rho)
|
|
121
|
+
return eta0 + eta_init + eta_res
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# ═══════════════════════════════════════════════════════════════════════════
|
|
125
|
+
# THERMAL CONDUCTIVITY — Huber et al. (2016)
|
|
126
|
+
# ═══════════════════════════════════════════════════════════════════════════
|
|
127
|
+
|
|
128
|
+
# ── Dilute gas λ₀(T) — Eq. (3) ───────────────────────────────────────────
|
|
129
|
+
_LAM0_L = jnp.array([0.0151874307, 0.0280674040, 0.0228564190, -0.00741624210])
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _lambda_dilute(T):
|
|
133
|
+
"""Dilute-gas thermal conductivity λ₀(T) [W/(m·K)]."""
|
|
134
|
+
tau = TC / T
|
|
135
|
+
poly = (_LAM0_L[0] + _LAM0_L[1] * tau
|
|
136
|
+
+ _LAM0_L[2] * tau ** 2 + _LAM0_L[3] * tau ** 3)
|
|
137
|
+
return tau ** (-0.5) / poly / 1000.0 # mW → W
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
# ── Residual λ_res(T, ρ) — polynomial ────────────────────────────────────
|
|
141
|
+
_LAM_RES_B = jnp.array([
|
|
142
|
+
0.0100128, 0.0560488, -0.081162, 0.0624337, -0.0206336, 0.00253248,
|
|
143
|
+
0.00430829, -0.0358563, 0.067148, -0.0522855, 0.0174571, -0.00196414,
|
|
144
|
+
])
|
|
145
|
+
_LAM_RES_D = jnp.array([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6], dtype=jnp.float64)
|
|
146
|
+
_LAM_RES_T = jnp.array([0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1], dtype=jnp.float64)
|
|
147
|
+
|
|
148
|
+
_RHOMASS_REDUCING = 467.6 # kg/m³ (same as RHOC_MASS)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _lambda_residual(T, rho):
|
|
152
|
+
"""Residual thermal conductivity [W/(m·K)]."""
|
|
153
|
+
tau = TC / T
|
|
154
|
+
delta = rho / _RHOMASS_REDUCING
|
|
155
|
+
return jnp.sum(_LAM_RES_B * delta ** _LAM_RES_D * tau ** _LAM_RES_T)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
# ── Critical enhancement — simplified Olchowy-Sengers ────────────────────
|
|
159
|
+
_KB = 1.3806488e-23 # J/K
|
|
160
|
+
_R0 = 1.02 # CO₂-specific amplitude ratio
|
|
161
|
+
_GAMMA_CE = 0.052 # CO₂-specific
|
|
162
|
+
_GAMMA_EXP = 1.239 # universal critical exponent γ
|
|
163
|
+
_NU = 0.63 # universal critical exponent ν
|
|
164
|
+
_ZETA0 = 1.5e-10 # m
|
|
165
|
+
_QD = 2.5e9 # 1/m
|
|
166
|
+
_T_REF = 456.19 # K
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _lambda_critical(T, rho, mu):
|
|
170
|
+
"""Critical enhancement of thermal conductivity [W/(m·K)].
|
|
171
|
+
|
|
172
|
+
Requires viscosity μ [Pa·s] as input (avoids circular dependency).
|
|
173
|
+
Uses EOS reduced Helmholtz derivatives for compressibility.
|
|
174
|
+
"""
|
|
175
|
+
rho_molar = rho / M
|
|
176
|
+
tau = TC / T
|
|
177
|
+
delta = rho_molar / RHOC_MOLAR
|
|
178
|
+
|
|
179
|
+
# ── dp/dρ at (T, ρ) ──
|
|
180
|
+
alr_d = _dalr_ddelta(tau, delta)
|
|
181
|
+
alr_dd = _d2alr_ddelta2(tau, delta)
|
|
182
|
+
dp_drho_mol = R_MOLAR * T * (1.0 + 2.0 * delta * alr_d
|
|
183
|
+
+ delta ** 2 * alr_dd)
|
|
184
|
+
|
|
185
|
+
chi = PC / RHOC_MOLAR ** 2 * rho_molar / dp_drho_mol
|
|
186
|
+
|
|
187
|
+
# ── dp/dρ at reference temperature T_ref (same δ) ──
|
|
188
|
+
tau_ref = TC / _T_REF
|
|
189
|
+
alr_d_ref = _dalr_ddelta(tau_ref, delta)
|
|
190
|
+
alr_dd_ref = _d2alr_ddelta2(tau_ref, delta)
|
|
191
|
+
dp_drho_ref = R_MOLAR * _T_REF * (1.0 + 2.0 * delta * alr_d_ref
|
|
192
|
+
+ delta ** 2 * alr_dd_ref)
|
|
193
|
+
|
|
194
|
+
chi_ref = PC / RHOC_MOLAR ** 2 * rho_molar / dp_drho_ref * _T_REF / T
|
|
195
|
+
|
|
196
|
+
diff = chi - chi_ref
|
|
197
|
+
|
|
198
|
+
# ── Correlation length ζ ──
|
|
199
|
+
# Use safe branch: if diff < 0, set zeta to 0 (no enhancement)
|
|
200
|
+
diff_safe = jnp.maximum(diff, 0.0)
|
|
201
|
+
zeta = _ZETA0 * (diff_safe / _GAMMA_CE) ** (_NU / _GAMMA_EXP)
|
|
202
|
+
|
|
203
|
+
# ── Molar heat capacities from EOS derivatives ──
|
|
204
|
+
# Cv/R = -τ²·(α⁰_ττ + αʳ_ττ)
|
|
205
|
+
# For α⁰_ττ we use the known analytical result for CO₂ ideal gas.
|
|
206
|
+
# But it's cleaner to compute Cp/Cv ratio from mechanical derivatives.
|
|
207
|
+
#
|
|
208
|
+
# Cv_molar = R · (-τ²·(α⁰_ττ + αʳ_ττ))
|
|
209
|
+
# Cp_molar = Cv_molar + R · (1 + δαʳ_δ - δταʳ_δτ)² / (1 + 2δαʳ_δ + δ²αʳ_δδ)
|
|
210
|
+
|
|
211
|
+
al0_tt = _d2al0_dtau2(tau, delta)
|
|
212
|
+
alr_tt = _d2alr_dtau2(tau, delta)
|
|
213
|
+
alr_dt = _d2alr_ddelta_dtau(tau, delta)
|
|
214
|
+
|
|
215
|
+
cv_over_R = -tau ** 2 * (al0_tt + alr_tt)
|
|
216
|
+
num_cp = (1.0 + delta * alr_d - delta * tau * alr_dt) ** 2
|
|
217
|
+
den_cp = 1.0 + 2.0 * delta * alr_d + delta ** 2 * alr_dd
|
|
218
|
+
cp_over_R = cv_over_R + num_cp / den_cp
|
|
219
|
+
|
|
220
|
+
cv_molar = R_MOLAR * cv_over_R
|
|
221
|
+
cp_molar = R_MOLAR * cp_over_R
|
|
222
|
+
|
|
223
|
+
# ── Ω functions ──
|
|
224
|
+
qd_zeta = _QD * zeta
|
|
225
|
+
inv_qd_zeta = 1.0 / jnp.maximum(qd_zeta, 1e-300)
|
|
226
|
+
|
|
227
|
+
omega = (2.0 / jnp.pi) * (
|
|
228
|
+
(cp_molar - cv_molar) / cp_molar * jnp.arctan(qd_zeta)
|
|
229
|
+
+ cv_molar / cp_molar * qd_zeta
|
|
230
|
+
)
|
|
231
|
+
omega0 = (2.0 / jnp.pi) * (
|
|
232
|
+
1.0 - jnp.exp(-1.0 / (inv_qd_zeta
|
|
233
|
+
+ (qd_zeta) ** 2 / (3.0 * delta ** 2)))
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
# ── Critical enhancement ──
|
|
237
|
+
zeta_safe = jnp.maximum(zeta, 1e-300)
|
|
238
|
+
lam_c = (rho_molar * cp_molar * _R0 * _KB * T
|
|
239
|
+
/ (6.0 * jnp.pi * mu * zeta_safe)
|
|
240
|
+
* (omega - omega0))
|
|
241
|
+
|
|
242
|
+
# Zero out when diff <= 0 (no enhancement)
|
|
243
|
+
return jnp.where(diff > 0.0, lam_c, 0.0)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
# ── Scalar thermal conductivity ──────────────────────────────────────────
|
|
247
|
+
|
|
248
|
+
def _scalar_thermal_conductivity(T, rho):
|
|
249
|
+
"""Thermal conductivity λ [W/(m·K)] at scalar (T, ρ [kg/m³])."""
|
|
250
|
+
mu = _scalar_viscosity(T, rho)
|
|
251
|
+
lam0 = _lambda_dilute(T)
|
|
252
|
+
lam_res = _lambda_residual(T, rho)
|
|
253
|
+
lam_crit = _lambda_critical(T, rho, mu)
|
|
254
|
+
return lam0 + lam_res + lam_crit
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
# ═══════════════════════════════════════════════════════════════════════════
|
|
258
|
+
# Public API — batched, JIT-compiled
|
|
259
|
+
# ═══════════════════════════════════════════════════════════════════════════
|
|
260
|
+
|
|
261
|
+
@jax.jit
|
|
262
|
+
def viscosity(T, rho):
|
|
263
|
+
"""Dynamic viscosity μ [Pa·s]. Batched over leading axis.
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
T: temperature [K], 1-D array
|
|
267
|
+
rho: mass density [kg/m³], 1-D array
|
|
268
|
+
|
|
269
|
+
Returns:
|
|
270
|
+
μ [Pa·s], same shape as inputs
|
|
271
|
+
"""
|
|
272
|
+
return jax.vmap(_scalar_viscosity)(T, rho)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
@jax.jit
|
|
276
|
+
def thermal_conductivity(T, rho):
|
|
277
|
+
"""Thermal conductivity λ [W/(m·K)]. Batched over leading axis.
|
|
278
|
+
|
|
279
|
+
Args:
|
|
280
|
+
T: temperature [K], 1-D array
|
|
281
|
+
rho: mass density [kg/m³], 1-D array
|
|
282
|
+
|
|
283
|
+
Returns:
|
|
284
|
+
λ [W/(m·K)], same shape as inputs
|
|
285
|
+
"""
|
|
286
|
+
return jax.vmap(_scalar_thermal_conductivity)(T, rho)
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: co2-eos
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Differentiable CO₂ thermodynamic properties in JAX
|
|
5
|
+
Author-email: John Patrick Therrien <john@flux-tech.de>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/John-FluxTech/co2-eos
|
|
8
|
+
Project-URL: Repository, https://github.com/John-FluxTech/co2-eos
|
|
9
|
+
Project-URL: Documentation, https://github.com/John-FluxTech/co2-eos#readme
|
|
10
|
+
Project-URL: Issues, https://github.com/John-FluxTech/co2-eos/issues
|
|
11
|
+
Keywords: CO2,equation-of-state,thermodynamics,JAX,autodiff,Span-Wagner
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Chemistry
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: jax>=0.4
|
|
26
|
+
Requires-Dist: jaxlib>=0.4
|
|
27
|
+
Provides-Extra: test
|
|
28
|
+
Requires-Dist: CoolProp>=6.6; extra == "test"
|
|
29
|
+
Requires-Dist: pytest>=7.0; extra == "test"
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: co2-eos[test]; extra == "dev"
|
|
32
|
+
Requires-Dist: scipy>=1.10; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# CO2-EOS
|
|
36
|
+
|
|
37
|
+
[](https://github.com/John-FluxTech/co2-eos/actions/workflows/test.yml)
|
|
38
|
+
[](https://colab.research.google.com/github/John-FluxTech/co2-eos/blob/main/examples/launch_demo.ipynb)
|
|
39
|
+
|
|
40
|
+
Differentiable CO₂ thermodynamic properties in JAX.
|
|
41
|
+
|
|
42
|
+
CO2-EOS is a pure-JAX implementation of the Span-Wagner equation of state for carbon dioxide. Every property evaluation is JIT-compiled, vectorisable with `vmap`, and fully differentiable with `jax.grad`, enabling gradient-based optimisation of any system that depends on CO₂ thermodynamics.
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import co2_eos as co2
|
|
46
|
+
import jax
|
|
47
|
+
|
|
48
|
+
# Properties from (T, ρ)
|
|
49
|
+
state = co2.properties(T=310.0, rho=350.0)
|
|
50
|
+
print(state['pressure'], state['cp'], state['speed_of_sound'])
|
|
51
|
+
|
|
52
|
+
# State from (P, T): phase-aware, robust near the critical point
|
|
53
|
+
state = co2.state_from_PT(P=8e6, T=310.0)
|
|
54
|
+
|
|
55
|
+
# Differentiate anything: e.g. ∂ρ/∂T at constant P
|
|
56
|
+
# (peaks near the Widom line at these conditions)
|
|
57
|
+
drho_dT = jax.grad(lambda T: co2.state_from_PT(P=8e6, T=T)['density'])(310.0)
|
|
58
|
+
|
|
59
|
+
# Vectorise across conditions
|
|
60
|
+
import jax.numpy as jnp
|
|
61
|
+
T_array = jnp.linspace(280, 340, 100)
|
|
62
|
+
states = jax.vmap(lambda T: co2.state_from_PT(P=8e6, T=T))(T_array)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Try it without cloning
|
|
66
|
+
|
|
67
|
+
[`examples/launch_demo.ipynb`](examples/launch_demo.ipynb) is the launch demo: validation against CoolProp, the CPU and GPU benchmarks cited below, and a worked example of gradient-based optimisation through `state_from_PT`. [Read it on GitHub](https://github.com/John-FluxTech/co2-eos/blob/main/examples/launch_demo.ipynb) for a static render, or [open it on Colab](https://colab.research.google.com/github/John-FluxTech/co2-eos/blob/main/examples/launch_demo.ipynb) and switch the runtime to T4 GPU to reproduce the comparison in about 15 minutes, most of that being the CPU baseline.
|
|
68
|
+
|
|
69
|
+
## Why this exists
|
|
70
|
+
|
|
71
|
+
If you're building CO₂ system models in Python and need thermodynamic properties, CoolProp is the standard choice. It's excellent: accurate, well-tested, and covers 110+ fluids.
|
|
72
|
+
|
|
73
|
+
But CoolProp is a C++ library with Python bindings. You can't `jax.grad` through it. You can't JIT-compile a simulation loop that calls it. And the per-call overhead from the Python↔C++ boundary adds up fast in tight integration loops.
|
|
74
|
+
|
|
75
|
+
CO2-EOS solves this for CO₂ by implementing the same reference EOS (Span-Wagner 1996) directly in JAX:
|
|
76
|
+
|
|
77
|
+
- **Differentiable.** `jax.grad` through any property, any inversion, any combination. No finite differences. Exact gradients via autodiff, including second and higher derivatives for free.
|
|
78
|
+
- **Fast.** JIT-compiled and vectorisable. Forward state evaluation at fixed (T, ρ) runs in roughly 1.8 μs/point under `jax.vmap`. The (P, T) workflow that mirrors `PropsSI` includes an iterative density solve and flattens to a few tens of microseconds per point at large batches, roughly 2.7× faster than `CoolProp.PropsSI` in a Python loop on the same inputs (measured at a 10,000-point batch on an Apple M2 Pro; see `examples/launch_demo.ipynb`, section 2). On a Colab T4 GPU the same `state_from_PT` workflow runs at 27,800 states/sec on a 10⁶-point batch (36 μs/pt), versus 1,600 states/sec on Colab CPU (615 μs/pt), a 17× speedup. The ceiling is set by the iterative density solve: a `jax.while_loop` with data-dependent control flow forces lockstep execution across GPU warps, so the polynomial evaluation parallelises cleanly but is not the dominant cost. A fully feed-forward kernel on the same hardware would land closer to 50 to 100×.
|
|
79
|
+
- **Composable.** `jit`, `vmap`, `grad`, `custom_vjp`: the full JAX transformation stack works. Embed property evaluations inside your own JIT-compiled simulation and differentiate end-to-end.
|
|
80
|
+
- **Phase-aware.** Robust inversions near the critical point using Halley's method with step damping and bisection fallback. Two-phase dome detection that avoids convergence to thermodynamically unstable spinodal states.
|
|
81
|
+
|
|
82
|
+
## What's included
|
|
83
|
+
|
|
84
|
+
**Equation of state**: Full Span-Wagner (1996) formulation for CO₂. Helmholtz free energy as A(T, ρ), with all thermodynamic properties derived by autodiff:
|
|
85
|
+
|
|
86
|
+
- Pressure, internal energy, enthalpy, entropy
|
|
87
|
+
- Isochoric and isobaric heat capacities (Cv, Cp)
|
|
88
|
+
- Speed of sound
|
|
89
|
+
- Gibbs energy
|
|
90
|
+
|
|
91
|
+
**Transport properties:**
|
|
92
|
+
|
|
93
|
+
- Viscosity: Laesecke & Muzny (2017), including dilute-gas, initial-density, and residual contributions, plus critical enhancement
|
|
94
|
+
- Thermal conductivity: Huber, Sykioti, Assael & Perkins (2016), with critical enhancement via the simplified crossover model
|
|
95
|
+
|
|
96
|
+
**Saturation curve:**
|
|
97
|
+
|
|
98
|
+
- Saturation pressure, densities, enthalpies, and entropies as functions of T or P
|
|
99
|
+
- Cubic spline interpolation of a precomputed Maxwell-construction table, JIT-compilable and differentiable
|
|
100
|
+
|
|
101
|
+
**State inversions:**
|
|
102
|
+
|
|
103
|
+
- `state_from_PT(P, T)`: Halley's method with pressure-aware critical-region initial guess, step damping, and bisection safety net
|
|
104
|
+
- `state_from_Ph(P, h, phase_hint=)` (phase-aware): detects the two-phase dome at subcritical pressures and returns saturation properties directly, bypassing single-phase Newton iteration inside the dome
|
|
105
|
+
- `state_from_Du(rho, u)`: for simulation codes carrying (ρ, u) as conserved variables. Uses `custom_vjp` for clean gradient propagation through the implicit solve
|
|
106
|
+
|
|
107
|
+
## Valid range
|
|
108
|
+
|
|
109
|
+
CO2-EOS covers the full fluid region of the Span-Wagner EOS:
|
|
110
|
+
|
|
111
|
+
- **Temperature:** 216.6 K (triple point) to 1100 K
|
|
112
|
+
- **Pressure:** up to 800 MPa
|
|
113
|
+
- **Saturation curve:** triple point to within 1 mK of the critical point (304.1282 K, 7.3773 MPa)
|
|
114
|
+
|
|
115
|
+
Transport properties follow the valid ranges of their respective correlations (broadly: gas and liquid up to 100 MPa, with reduced accuracy in the immediate critical region for thermal conductivity).
|
|
116
|
+
|
|
117
|
+
## Installation
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
pip install co2-eos
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Requires Python ≥ 3.10 and JAX ≥ 0.4. No other dependencies.
|
|
124
|
+
|
|
125
|
+
To run the validation tests (which compare against CoolProp):
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
git clone https://github.com/John-FluxTech/co2-eos.git
|
|
129
|
+
cd co2-eos
|
|
130
|
+
pip install -e ".[test]"
|
|
131
|
+
pytest tests/ -v
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The `[test]` extra adds CoolProp (validation only, not a runtime
|
|
135
|
+
dependency) and pytest. The `[dev]` extra additionally pulls in scipy for
|
|
136
|
+
regenerating the saturation table from scratch via
|
|
137
|
+
`scripts/generate_saturation_table.py`.
|
|
138
|
+
|
|
139
|
+
## Validation
|
|
140
|
+
|
|
141
|
+
CO2-EOS is validated against CoolProp across the full valid range. The test suite covers:
|
|
142
|
+
|
|
143
|
+
- Single-phase properties on a dense (T, ρ) grid spanning gas, liquid, and supercritical regions
|
|
144
|
+
- Saturation curve properties from triple point to near-critical
|
|
145
|
+
- Transport properties (viscosity and thermal conductivity) across all phases
|
|
146
|
+
- Inversions (P,T → ρ), (P,h → state), (ρ,u → state) including near-critical conditions
|
|
147
|
+
- Comparison of all Helmholtz derivatives against CoolProp's analytical values
|
|
148
|
+
|
|
149
|
+
Both libraries implement the same Span-Wagner polynomial and the same reference transport correlations, so empirical agreement across the validation grid is at machine precision for the primary quantities and only loosens where physics (not formulation) amplifies floating-point noise:
|
|
150
|
+
|
|
151
|
+
- **Density and viscosity:** below 10⁻¹² relative error everywhere on the grid (limited by floating-point evaluation order).
|
|
152
|
+
- **cp, speed of sound, thermal conductivity:** below 10⁻⁹ in the bulk; up to a few × 10⁻⁸ for the speed of sound near the Widom line and within ~10 % of the saturation curve, and up to a few × 10⁻⁶ for cp at the very tip of its peak just above the critical pressure. There the inversion's residual ρ-error is amplified through ∂q/∂ρ, which is locally enormous (cp climbs above 30,000 J/(kg·K) at the peak, so a ~0.1 J/(kg·K) absolute difference still rounds to ~10⁻⁶ relative); at the same (T, ρ) the underlying polynomials still agree at machine precision.
|
|
153
|
+
|
|
154
|
+
Run the validation suite:
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
pytest tests/ -v
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Scope and philosophy
|
|
161
|
+
|
|
162
|
+
CO2-EOS is a CO₂-first library. Rather than expanding to cover more fluids, we go deeper on CO₂, improving accuracy, coverage, and robustness in the regions that matter for real engineering: near-critical, transcritical, and two-phase.
|
|
163
|
+
|
|
164
|
+
There are excellent general-purpose thermodynamic libraries: CoolProp for broad fluid coverage, teqp for autodiff-native multi-model EOS evaluation, FeOs for SAFT models and density functional theory, and Clapeyron.jl in the Julia ecosystem. CO2-EOS complements these by providing a JAX-native path for the single fluid where differentiability, speed, and near-critical robustness matter most.
|
|
165
|
+
|
|
166
|
+
Contributions that extend CO₂ capability are welcome:
|
|
167
|
+
|
|
168
|
+
- Better transport property models as new correlations are published
|
|
169
|
+
- Improved near-critical formulations (crossover EOS, scaled equations)
|
|
170
|
+
- Mixture models where CO₂ is the primary component (CO₂ + impurities for CCS, CO₂ + lubricant for heat pumps)
|
|
171
|
+
- Fitting to experimental data for conditions where Span-Wagner accuracy is insufficient
|
|
172
|
+
|
|
173
|
+
## Physical references
|
|
174
|
+
|
|
175
|
+
The implementations follow these published formulations:
|
|
176
|
+
|
|
177
|
+
- **EOS:** Span, R. and Wagner, W. (1996). "A New Equation of State for Carbon Dioxide." *J. Phys. Chem. Ref. Data*, 25(6), 1509–1596.
|
|
178
|
+
- **Viscosity:** Laesecke, A. and Muzny, C. D. (2017). "Reference Correlation for the Viscosity of Carbon Dioxide." *J. Phys. Chem. Ref. Data*, 46, 013107.
|
|
179
|
+
- **Thermal conductivity:** Huber, M. L., Sykioti, E. A., Assael, M. J., and Perkins, R. A. (2016). "Reference Correlation of the Thermal Conductivity of Carbon Dioxide from the Triple Point to 1100 K and up to 200 MPa." *J. Phys. Chem. Ref. Data*, 45, 013102.
|
|
180
|
+
|
|
181
|
+
Solver design is informed by:
|
|
182
|
+
|
|
183
|
+
- Bell, I. H. et al. (2014). CoolProp solver architecture: phase-aware initial guesses and fallback chains.
|
|
184
|
+
- Bell, I. H., Deiters, U. K., and Leal, A. M. M. (2022). "Implementing an Equation of State without Derivatives: teqp." *Ind. Eng. Chem. Res.*, 61(17), 6010–6027.
|
|
185
|
+
- Bell, I. H. and Alpert, B. K. (2018). "Exceptionally Reliable Density-Solving Algorithms." *Fluid Phase Equilibria*, 477, 87–97.
|
|
186
|
+
|
|
187
|
+
## Acknowledgements
|
|
188
|
+
|
|
189
|
+
CO2-EOS was developed by Claude and John Patrick Therrien during research on transcritical CO₂ thermoacoustic engines at FluxTech UG in Berlin. The Helmholtz-first autodiff design was validated against CoolProp and informed by the solver strategies documented in teqp and the CoolProp codebase.
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
Apache-2.0
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
co2_eos/__init__.py,sha256=X1kAL39v0Mt3Oh0oxyfGOK4bNf7GxBrI0tPpWVjLR00,9948
|
|
2
|
+
co2_eos/inversions.py,sha256=sePRQ09k-fPbe9hFN0Hh1eQGUaoputADJKtpo4cjxTI,26156
|
|
3
|
+
co2_eos/saturation.py,sha256=tV9jfyOzmFHVrqAckeQZcNLvHlMCUrNtYfuM8g252GA,5082
|
|
4
|
+
co2_eos/span_wagner.py,sha256=STEKHFxfXAoUEY23uIpi-QPhLPl_8x5tSxcdcZHR5eM,18848
|
|
5
|
+
co2_eos/transport.py,sha256=0qYHP_uFwFC-slVuCtKZxPSkmTl2WuLqlFm4iT0srDg,11853
|
|
6
|
+
co2_eos/data/saturation_table.npz,sha256=QVSs3tHi0r-cNtBJfkukBhUkYbgIm89YAksvMTzkOZQ,374706
|
|
7
|
+
co2_eos-0.1.0.dist-info/licenses/LICENSE,sha256=m_1yFCehM7IGY8fYIsHEGIUyNL_L6LH1ECBku7eTTWI,11321
|
|
8
|
+
co2_eos-0.1.0.dist-info/METADATA,sha256=TqOBD9wCkjTVHua_p92bEbfM7UUT3px4Gi12Hn6iv2A,11879
|
|
9
|
+
co2_eos-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
10
|
+
co2_eos-0.1.0.dist-info/top_level.txt,sha256=jY_oWRW6jC41KmoxKCZnunQZaf9jgm9a8AwSUJLvSkM,8
|
|
11
|
+
co2_eos-0.1.0.dist-info/RECORD,,
|