co2-eos 0.1.0__tar.gz
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-0.1.0/LICENSE +201 -0
- co2_eos-0.1.0/PKG-INFO +193 -0
- co2_eos-0.1.0/README.md +159 -0
- co2_eos-0.1.0/co2_eos/__init__.py +265 -0
- co2_eos-0.1.0/co2_eos/data/saturation_table.npz +0 -0
- co2_eos-0.1.0/co2_eos/inversions.py +695 -0
- co2_eos-0.1.0/co2_eos/saturation.py +156 -0
- co2_eos-0.1.0/co2_eos/span_wagner.py +501 -0
- co2_eos-0.1.0/co2_eos/transport.py +286 -0
- co2_eos-0.1.0/co2_eos.egg-info/PKG-INFO +193 -0
- co2_eos-0.1.0/co2_eos.egg-info/SOURCES.txt +18 -0
- co2_eos-0.1.0/co2_eos.egg-info/dependency_links.txt +1 -0
- co2_eos-0.1.0/co2_eos.egg-info/requires.txt +10 -0
- co2_eos-0.1.0/co2_eos.egg-info/top_level.txt +1 -0
- co2_eos-0.1.0/pyproject.toml +61 -0
- co2_eos-0.1.0/setup.cfg +4 -0
- co2_eos-0.1.0/tests/test_accuracy_vs_coolprop.py +153 -0
- co2_eos-0.1.0/tests/test_dome_detection.py +205 -0
- co2_eos-0.1.0/tests/test_inversion_gradients.py +333 -0
- co2_eos-0.1.0/tests/test_no_coolprop_runtime.py +39 -0
co2_eos-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. Please also get a custom media
|
|
186
|
+
type (formerly combinator/MIME type) if one does not already exist
|
|
187
|
+
for your file format at http://www.iana.org/assignments/media-types/.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 FluxTech UG
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
co2_eos-0.1.0/PKG-INFO
ADDED
|
@@ -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
|
co2_eos-0.1.0/README.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# CO2-EOS
|
|
2
|
+
|
|
3
|
+
[](https://github.com/John-FluxTech/co2-eos/actions/workflows/test.yml)
|
|
4
|
+
[](https://colab.research.google.com/github/John-FluxTech/co2-eos/blob/main/examples/launch_demo.ipynb)
|
|
5
|
+
|
|
6
|
+
Differentiable CO₂ thermodynamic properties in JAX.
|
|
7
|
+
|
|
8
|
+
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.
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
import co2_eos as co2
|
|
12
|
+
import jax
|
|
13
|
+
|
|
14
|
+
# Properties from (T, ρ)
|
|
15
|
+
state = co2.properties(T=310.0, rho=350.0)
|
|
16
|
+
print(state['pressure'], state['cp'], state['speed_of_sound'])
|
|
17
|
+
|
|
18
|
+
# State from (P, T): phase-aware, robust near the critical point
|
|
19
|
+
state = co2.state_from_PT(P=8e6, T=310.0)
|
|
20
|
+
|
|
21
|
+
# Differentiate anything: e.g. ∂ρ/∂T at constant P
|
|
22
|
+
# (peaks near the Widom line at these conditions)
|
|
23
|
+
drho_dT = jax.grad(lambda T: co2.state_from_PT(P=8e6, T=T)['density'])(310.0)
|
|
24
|
+
|
|
25
|
+
# Vectorise across conditions
|
|
26
|
+
import jax.numpy as jnp
|
|
27
|
+
T_array = jnp.linspace(280, 340, 100)
|
|
28
|
+
states = jax.vmap(lambda T: co2.state_from_PT(P=8e6, T=T))(T_array)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Try it without cloning
|
|
32
|
+
|
|
33
|
+
[`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.
|
|
34
|
+
|
|
35
|
+
## Why this exists
|
|
36
|
+
|
|
37
|
+
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.
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
CO2-EOS solves this for CO₂ by implementing the same reference EOS (Span-Wagner 1996) directly in JAX:
|
|
42
|
+
|
|
43
|
+
- **Differentiable.** `jax.grad` through any property, any inversion, any combination. No finite differences. Exact gradients via autodiff, including second and higher derivatives for free.
|
|
44
|
+
- **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×.
|
|
45
|
+
- **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.
|
|
46
|
+
- **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.
|
|
47
|
+
|
|
48
|
+
## What's included
|
|
49
|
+
|
|
50
|
+
**Equation of state**: Full Span-Wagner (1996) formulation for CO₂. Helmholtz free energy as A(T, ρ), with all thermodynamic properties derived by autodiff:
|
|
51
|
+
|
|
52
|
+
- Pressure, internal energy, enthalpy, entropy
|
|
53
|
+
- Isochoric and isobaric heat capacities (Cv, Cp)
|
|
54
|
+
- Speed of sound
|
|
55
|
+
- Gibbs energy
|
|
56
|
+
|
|
57
|
+
**Transport properties:**
|
|
58
|
+
|
|
59
|
+
- Viscosity: Laesecke & Muzny (2017), including dilute-gas, initial-density, and residual contributions, plus critical enhancement
|
|
60
|
+
- Thermal conductivity: Huber, Sykioti, Assael & Perkins (2016), with critical enhancement via the simplified crossover model
|
|
61
|
+
|
|
62
|
+
**Saturation curve:**
|
|
63
|
+
|
|
64
|
+
- Saturation pressure, densities, enthalpies, and entropies as functions of T or P
|
|
65
|
+
- Cubic spline interpolation of a precomputed Maxwell-construction table, JIT-compilable and differentiable
|
|
66
|
+
|
|
67
|
+
**State inversions:**
|
|
68
|
+
|
|
69
|
+
- `state_from_PT(P, T)`: Halley's method with pressure-aware critical-region initial guess, step damping, and bisection safety net
|
|
70
|
+
- `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
|
|
71
|
+
- `state_from_Du(rho, u)`: for simulation codes carrying (ρ, u) as conserved variables. Uses `custom_vjp` for clean gradient propagation through the implicit solve
|
|
72
|
+
|
|
73
|
+
## Valid range
|
|
74
|
+
|
|
75
|
+
CO2-EOS covers the full fluid region of the Span-Wagner EOS:
|
|
76
|
+
|
|
77
|
+
- **Temperature:** 216.6 K (triple point) to 1100 K
|
|
78
|
+
- **Pressure:** up to 800 MPa
|
|
79
|
+
- **Saturation curve:** triple point to within 1 mK of the critical point (304.1282 K, 7.3773 MPa)
|
|
80
|
+
|
|
81
|
+
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).
|
|
82
|
+
|
|
83
|
+
## Installation
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pip install co2-eos
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Requires Python ≥ 3.10 and JAX ≥ 0.4. No other dependencies.
|
|
90
|
+
|
|
91
|
+
To run the validation tests (which compare against CoolProp):
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
git clone https://github.com/John-FluxTech/co2-eos.git
|
|
95
|
+
cd co2-eos
|
|
96
|
+
pip install -e ".[test]"
|
|
97
|
+
pytest tests/ -v
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The `[test]` extra adds CoolProp (validation only, not a runtime
|
|
101
|
+
dependency) and pytest. The `[dev]` extra additionally pulls in scipy for
|
|
102
|
+
regenerating the saturation table from scratch via
|
|
103
|
+
`scripts/generate_saturation_table.py`.
|
|
104
|
+
|
|
105
|
+
## Validation
|
|
106
|
+
|
|
107
|
+
CO2-EOS is validated against CoolProp across the full valid range. The test suite covers:
|
|
108
|
+
|
|
109
|
+
- Single-phase properties on a dense (T, ρ) grid spanning gas, liquid, and supercritical regions
|
|
110
|
+
- Saturation curve properties from triple point to near-critical
|
|
111
|
+
- Transport properties (viscosity and thermal conductivity) across all phases
|
|
112
|
+
- Inversions (P,T → ρ), (P,h → state), (ρ,u → state) including near-critical conditions
|
|
113
|
+
- Comparison of all Helmholtz derivatives against CoolProp's analytical values
|
|
114
|
+
|
|
115
|
+
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:
|
|
116
|
+
|
|
117
|
+
- **Density and viscosity:** below 10⁻¹² relative error everywhere on the grid (limited by floating-point evaluation order).
|
|
118
|
+
- **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.
|
|
119
|
+
|
|
120
|
+
Run the validation suite:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
pytest tests/ -v
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Scope and philosophy
|
|
127
|
+
|
|
128
|
+
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.
|
|
129
|
+
|
|
130
|
+
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.
|
|
131
|
+
|
|
132
|
+
Contributions that extend CO₂ capability are welcome:
|
|
133
|
+
|
|
134
|
+
- Better transport property models as new correlations are published
|
|
135
|
+
- Improved near-critical formulations (crossover EOS, scaled equations)
|
|
136
|
+
- Mixture models where CO₂ is the primary component (CO₂ + impurities for CCS, CO₂ + lubricant for heat pumps)
|
|
137
|
+
- Fitting to experimental data for conditions where Span-Wagner accuracy is insufficient
|
|
138
|
+
|
|
139
|
+
## Physical references
|
|
140
|
+
|
|
141
|
+
The implementations follow these published formulations:
|
|
142
|
+
|
|
143
|
+
- **EOS:** Span, R. and Wagner, W. (1996). "A New Equation of State for Carbon Dioxide." *J. Phys. Chem. Ref. Data*, 25(6), 1509–1596.
|
|
144
|
+
- **Viscosity:** Laesecke, A. and Muzny, C. D. (2017). "Reference Correlation for the Viscosity of Carbon Dioxide." *J. Phys. Chem. Ref. Data*, 46, 013107.
|
|
145
|
+
- **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.
|
|
146
|
+
|
|
147
|
+
Solver design is informed by:
|
|
148
|
+
|
|
149
|
+
- Bell, I. H. et al. (2014). CoolProp solver architecture: phase-aware initial guesses and fallback chains.
|
|
150
|
+
- 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.
|
|
151
|
+
- Bell, I. H. and Alpert, B. K. (2018). "Exceptionally Reliable Density-Solving Algorithms." *Fluid Phase Equilibria*, 477, 87–97.
|
|
152
|
+
|
|
153
|
+
## Acknowledgements
|
|
154
|
+
|
|
155
|
+
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.
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
Apache-2.0
|