diff --git a/REGISTER.md b/REGISTER.md index 0145c336..333882a0 100644 --- a/REGISTER.md +++ b/REGISTER.md @@ -29636,3 +29636,45 @@ tabulate (the table model clamps outside its range, so only in-range points coun - **Status:** decided (2026-11-12). Open: the 6-9e-4 offset from Eimerl's indices; Chen et al.'s tabulation and Zhang et al. (2000) would settle it, and neither is in the library. + +## TC-1096 CoolProp without superancillaries: the same densities, a seventh of the load (2026-11-16) + +- **Where:** `optmat.gas.eos` (`_coolprop`, `_quiet_stdout`, `_update_gas`, + `EVALUATION_VERSION`), `tests/test_coolprop_loading.py`, + `tests/data/golden_models.json`. +- **Claim:** CoolProp 8.0.0 builds the superancillaries of all of its fluids (about 120) + when it first loads, which is 0.43 s of its 0.46 s load (measured on an Apple M-series + laptop; `COOLPROP_DISABLE_SUPERANCILLARIES_ENTIRELY`, read in `FluidLibrary.h` as the + fluids are parsed, skips them: 0.067 s). optmat sets that variable for its import of + CoolProp, unless it is already set, removes it again so that child processes do not + take it for the user's, and hides CoolProp's notice of it on standard output. Without + superancillaries CoolProp finds a pressure-temperature state's phase with a + saturation solve that refuses pressures under its triple point's, which the phase + domain reaches just above the triple point (every gas's + `test_the_inverse_round_trips_across_the_domain` failed there); below the critical + temperature, where every caller has already checked that the state is the vapour, + optmat now tells CoolProp so (`iphase_gas`). +- **Checked:** against optmat 0.9.1 (superancillaries, phase found by CoolProp). + - Densities: every gas with an equation, 12 temperatures from its triple point to + 600 K and a fixed grid of 40 pressures from 100 Pa to 200 bar within the domain + (5924 states): every density bitwise the same; the imposed phase alone changes + nothing. + - The critical temperature: CoolProp reports the equation's stated critical + temperature instead of the critical point of its superancillary, which is where + the equation's coexistence actually ends. Oxygen's moves from 154.599 K to the + published 154.581 K (1.2e-4), hydrogen's by 2.0e-5, the others' by at most 2.1e-6. + Oxygen between the two is now supercritical: its domain there ends at the + equation's 800 bar, not at the vapour pressure (about 50 bar). + - Saturation pressures, and so the domain's top: 300 temperatures per gas, denser + towards the critical point. More than 1 K below it they move by at most 7.1e-11 + (oxygen's at its triple point, 146 Pa, by 2.0e-9); within 1 K by at most 6.5e-8 + (hydrogen, 0.7 mK below it), the tolerance of CoolProp's saturation solve, which + they now come from. Within 60 µK of helium's critical point and 4.4 µK of SF6's the + solve is worse than 1e-6 (helium's 1 % at 2 µK, SF6's 2.2e-4) or, for helium within + 40 µK, raises CoolProp's own `ValueError` rather than a `StateError`. +- **Decided:** `EVALUATION_VERSION` of `optmat.gas.eos` is raised to 2 (the maintainer, + 2026-11-17): the critical temperature is a field of every equation's identity, so the + identities change anyway, and the domain moves near the critical points. 57 golden + identities were re-recorded (equations of state, their mixtures, transport and air + models); no golden value changed. +- **Status:** decided (2026-11-17). diff --git a/changelog.d/coolprop-no-superancillaries.changed.md b/changelog.d/coolprop-no-superancillaries.changed.md new file mode 100644 index 00000000..568c0d7f --- /dev/null +++ b/changelog.d/coolprop-no-superancillaries.changed.md @@ -0,0 +1,9 @@ +CoolProp is loaded without the superancillaries it builds for all of its fluids, unless +`COOLPROP_DISABLE_SUPERANCILLARIES_ENTIRELY` is already set or CoolProp was imported +first: the first real-gas density takes 0.07 s instead of 0.46 s. No density at a given +state changed and no recommended value moved, but the identity of every equation of +state, and of the mixture, transport and air models built on one, changes: the +equation-of-state `EVALUATION_VERSION` is now 2. A gas's critical temperature is now its +equation's stated value (oxygen 154.581 K instead of 154.599 K; others within 2e-5), and +saturation pressures, so where a gas's domain ends, move by at most 7e-11 more than 1 K +below the critical point and by up to 7e-8 within it (TC-1096). diff --git a/docs/conventions.md b/docs/conventions.md index a3dec289..80b60dae 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -33,6 +33,17 @@ it with its identity (CoolProp's version, the fluid and the equation's reference CODATA's $k_B$, not the equation's mass density, which carries the gas constant of its publication (argon's, 5.7 ppm from CODATA's). +**Loading CoolProp.** optmat imports CoolProp on the first real-gas density, without the +superancillaries CoolProp would otherwise build for every one of its fluids: they are +most of its load time (0.46 s against 0.07 s), and no density at a given state changes +without them. A gas's critical temperature is then its equation's stated value (oxygen's +154.581 K, not the 154.599 K where its coexistence ends), and saturation pressures, so +where a gas's domain ends, come from CoolProp's saturation solve: within 7 × 10⁻¹¹ of the +superancillaries' more than 1 K below the critical point, and to 7 × 10⁻⁸ within it, but +not within some tens of microkelvin of it (TC-1096). Set +`COOLPROP_DISABLE_SUPERANCILLARIES_ENTIRELY` yourself, or import CoolProp before optmat +does, to choose otherwise; the variable is read once, when CoolProp loads. + **The molar mass.** `molar_mass` is the molar mass the backend's equation uses, kg/mol: the one its mass density and its speed of sound are formed with, for a propagation code's mass terms. The number densities do not use it. For the pseudo-pure dry air of diff --git a/src/optmat/gas/eos.py b/src/optmat/gas/eos.py index d9176055..0f816548 100644 --- a/src/optmat/gas/eos.py +++ b/src/optmat/gas/eos.py @@ -10,7 +10,12 @@ quantity, which needs the number density the author actually meant. Real-gas densities come from CoolProp's Helmholtz-energy equations of state, imported -lazily because CoolProp takes about a second to load. :func:`equation_of_state` returns +lazily, and without its superancillaries unless they were asked for (TC-1096): building +them for every one of its fluids is most of CoolProp's load time (0.43 s of 0.46 s), and +no density optmat reads changes without them; the critical temperature is the +equation's stated one, and saturation pressures agree to rounding except close to it. +Set ``COOLPROP_DISABLE_SUPERANCILLARIES_ENTIRELY`` yourself, or import +CoolProp before optmat loads it, to decide otherwise. :func:`equation_of_state` returns a gas's equation as an object with an identity: its forward law ``N = p / (Z k_B T)``, its inverse (the pressure at a number density and temperature), its speed of sound, its molar mass and its phase domain (OEP 1, section 4.2). @@ -33,8 +38,14 @@ """ import atexit +import contextlib +import ctypes import functools +import os +import sys +from collections.abc import Iterator from dataclasses import dataclass +from pathlib import Path from typing import Any, ClassVar, Final import numpy as np @@ -60,7 +71,7 @@ "pressure", ] -EVALUATION_VERSION: Final = 1 +EVALUATION_VERSION: Final = 2 """The version of this module's evaluation code, in the identity of every model that evaluates through it; raised whenever the code changes its output (OEP 1, section 4.8). """ @@ -106,9 +117,55 @@ def ideal_number_density(state: State) -> float: return state.pressure / (K_B * state.temperature) +_NO_SUPERANCILLARIES: Final = "COOLPROP_DISABLE_SUPERANCILLARIES_ENTIRELY" +"""CoolProp's switch, read once as it loads its fluids, that skips superancillaries.""" + + +@contextlib.contextmanager +def _quiet_stdout() -> Iterator[None]: + """Send what native code writes to standard output nowhere for the block. + + CoolProp announces on standard output that superancillaries are disabled. The + file descriptor itself is redirected, since the line comes from C++. C's buffers are + flushed before it is redirected, so that earlier output still reaches it, and again + before it is restored. Without a usable descriptor 1 the block runs as it is. + + - Yields: nothing. + """ + try: + sys.stdout.flush() + _flush_c_streams() + saved = os.dup(1) + except (AttributeError, OSError, ValueError): + yield + return + try: + with Path(os.devnull).open("wb") as sink: + os.dup2(sink.fileno(), 1) + try: + yield + finally: + _flush_c_streams() + finally: + os.dup2(saved, 1) + os.close(saved) + + +def _flush_c_streams() -> None: + """Flush C's output buffers (``fflush(NULL)``), where the C library can be found.""" + with contextlib.suppress(AttributeError, OSError): + library = ctypes.CDLL(None) if os.name == "posix" else ctypes.cdll.ucrtbase + library.fflush(None) + + @functools.cache def _coolprop() -> Any: - """Return CoolProp's module, imported on first use. + """Return CoolProp's module, imported on first use, without superancillaries. + + Unless :data:`_NO_SUPERANCILLARIES` is already set, it is set for the import alone, + which loads CoolProp's fluids, and CoolProp's notice of it is not printed; it is + removed again so that child processes do not inherit it. CoolProp imported earlier + keeps the fluids it loaded. Returns ------- @@ -120,14 +177,30 @@ def _coolprop() -> Any: ImportError Without the ``gas`` extra. """ + asked = _NO_SUPERANCILLARIES in os.environ try: - import CoolProp.CoolProp as coolprop # noqa: N813, PLC0415 - import cost + with contextlib.nullcontext() if asked else _without_superancillaries(): + import CoolProp.CoolProp as coolprop # noqa: N813, PLC0415 - import cost except ImportError as exc: msg = "real-gas densities need CoolProp: install optmat[gas]" raise ImportError(msg) from exc return coolprop +@contextlib.contextmanager +def _without_superancillaries() -> Iterator[None]: + """Set :data:`_NO_SUPERANCILLARIES` for the block, with standard output quiet. + + - Yields: nothing. + """ + os.environ[_NO_SUPERANCILLARIES] = "1" + try: + with _quiet_stdout(): + yield + finally: + del os.environ[_NO_SUPERANCILLARIES] + + @functools.cache def _abstract_state(fluid: str) -> Any: """Return CoolProp's Helmholtz-energy state of a fluid (one per fluid, reused). @@ -158,6 +231,36 @@ def _release() -> None: atexit.register(_release) +def _update_gas(backend: Any, pressure: float, temperature: float) -> None: + """Set a CoolProp state to a pressure and temperature of the gas. + + Below the critical temperature the state is the vapour: every caller has checked it + against the phase domain, or keeps within it. CoolProp is told so, rather than + finding the phase itself; without superancillaries it would find it with a + saturation solve that refuses pressures under its triple point's, which the domain + reaches just above the triple point (TC-1096). Above the critical temperature there + is no saturation to find, and CoolProp solves as it always has. + + Parameters + ---------- + backend : CoolProp.AbstractState + The fluid's state, updated in place. + pressure : float + Pa. + temperature : float + K. + """ + coolprop = _coolprop() + if temperature >= backend.T_critical(): + backend.update(coolprop.PT_INPUTS, pressure, temperature) + return + backend.specify_phase(coolprop.iphase_gas) + try: + backend.update(coolprop.PT_INPUTS, pressure, temperature) + finally: + backend.unspecify_phase() + + @functools.cache def _compressibility(fluid: str, pressure: float, temperature: float) -> float: """Return the compressibility factor at a state, unchecked (cached). @@ -178,7 +281,7 @@ def _compressibility(fluid: str, pressure: float, temperature: float) -> float: """ backend = _abstract_state(fluid) try: - backend.update(_coolprop().PT_INPUTS, pressure, temperature) + _update_gas(backend, pressure, temperature) except ValueError as exc: msg = ( f"no equation-of-state solution for {fluid} at {pressure} Pa and " @@ -476,7 +579,7 @@ def speed_of_sound(self, state: State) -> float: """ self._check(state) backend = _abstract_state(self.fluid) - backend.update(_coolprop().PT_INPUTS, state.pressure, state.temperature) + _update_gas(backend, state.pressure, state.temperature) return float(backend.speed_sound()) def _density_at(self, pressure_value: float, temperature: float) -> float: diff --git a/tests/data/golden_models.json b/tests/data/golden_models.json index 17565131..fd594d6d 100644 --- a/tests/data/golden_models.json +++ b/tests/data/golden_models.json @@ -8243,7 +8243,7 @@ 0.0 ] ], - "digest": "77be0c7bc2397b822fd3f5db8c12a75d7286cf979b101dbfdfde9417e4137e7f" + "digest": "1f589cb656b4839a4402e4e9299467d798137b473684dca981ab88bdf58d5103" }, { "id": "air/dry air (N2, O2, Ar)", @@ -8275,7 +8275,7 @@ 0.0 ] ], - "digest": "77be0c7bc2397b822fd3f5db8c12a75d7286cf979b101dbfdfde9417e4137e7f" + "digest": "1f589cb656b4839a4402e4e9299467d798137b473684dca981ab88bdf58d5103" }, { "id": "air/dry air (N2, O2)", @@ -8307,7 +8307,7 @@ 0.0 ] ], - "digest": "8b55b584e2f8763c2e3f6f83947bcc0a2c058164c7188a81c7bbc27ad2cc8cb1" + "digest": "b28b5de13ca6b45b9d446a9982f9da4d7bf8013ef8dd0f8c6c36a418497f1a0f" }, { "id": "air/dry air (N2, O2)", @@ -8339,7 +8339,7 @@ 0.0 ] ], - "digest": "8b55b584e2f8763c2e3f6f83947bcc0a2c058164c7188a81c7bbc27ad2cc8cb1" + "digest": "b28b5de13ca6b45b9d446a9982f9da4d7bf8013ef8dd0f8c6c36a418497f1a0f" }, { "id": "air/dry air (CIPM-2007)", @@ -8371,7 +8371,7 @@ 0.0 ] ], - "digest": "b350c540b0aba841a31a09ba1b3aec02c8f52bf8d3f9dd41ea7522e2eb8ede21" + "digest": "5b12f10c32db7f763adbaf9ba90e759d00196c22788b812d9a5738eba89d77c8" }, { "id": "air/dry air (CIPM-2007)", @@ -8403,7 +8403,7 @@ 0.0 ] ], - "digest": "b350c540b0aba841a31a09ba1b3aec02c8f52bf8d3f9dd41ea7522e2eb8ede21" + "digest": "5b12f10c32db7f763adbaf9ba90e759d00196c22788b812d9a5738eba89d77c8" }, { "id": "eos/He", @@ -8411,7 +8411,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.004002602, - "digest": "3ef5e835bc01cf3cd96cbf1e4c7abb6cdeb437abcda5c9b3edae77253daa5303" + "digest": "ef5a2e1afe7d01570b0b9670fbd3e98f17cbb6dc45a355594569c41931c89d33" }, { "id": "eos/Ne", @@ -8419,7 +8419,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.020179, - "digest": "bb2dc33ae341703986c7322d947f3d18b04702cd82103c43fd5efc7d2a6f9cef" + "digest": "d675f193dfb1db0503b0162b8b7b2062310afa95660fd5232c30cf0f06a39d94" }, { "id": "eos/Ar", @@ -8427,7 +8427,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.039948, - "digest": "85ee5c2688ac8be4e160b49e1864a82fc4e20ae659fd5f9be230e683d4caf219" + "digest": "15a9b124e6eec19a08168dcbd2ba7a69cf61d26bab6398843257b97c5d66a94a" }, { "id": "eos/Kr", @@ -8435,7 +8435,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.083798, - "digest": "125d26a80c1a32acb28a32f15c521cab1c519cfe0cc31136dec7a4e039541f65" + "digest": "e00353a0f1ba9c5a5c9d091040d69ae2631d86c8aa5b2efd36c34ede5c7e4531" }, { "id": "eos/Xe", @@ -8443,7 +8443,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.131293, - "digest": "59966dc8187d83b1a2612e39845fb24565f9edf994d012f710d6bf8b1733a27c" + "digest": "78313d470aa40eda5d5f44c80c632779f8244cd52f678317bd7027e6e950a3a1" }, { "id": "eos/H2", @@ -8451,7 +8451,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.00201588, - "digest": "b0498bbd8ed999af8383b60036b0008bae9aa871f03e9cad89bad8fef5b91881" + "digest": "5e8a74915ab20d9bf50ededaffb00ebc59fd7f5a017cfd530bd098327aecb86a" }, { "id": "eos/D2", @@ -8459,7 +8459,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.0040282, - "digest": "d803881e1c53e0d82440c8955428b1401b18baddb4ba7c8a00065b942dbb200e" + "digest": "fe408b938545c326f67959c15c41a7fd0b29cf80daac8a0faa606d918edae5f1" }, { "id": "eos/N2", @@ -8467,7 +8467,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.02801348, - "digest": "fda8fdb5206b7caafb85c19e945e7ec6a7ed2ffeafec9657ac301798eff1f48e" + "digest": "ef40cd31a4ebe10bfb9cc8530cfc87f015a8c18cfbe18c8439b6b2a17cb1be8c" }, { "id": "eos/O2", @@ -8475,7 +8475,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.0319988, - "digest": "6d873cc8706ce2ba16077b74310d1edc71019cec779c6b8a5d9ed27752f4cb14" + "digest": "8cab529fc668009c539f22a88e4a5e16d44f99ee25facf86934b1fb76dc8467f" }, { "id": "eos/CH4", @@ -8483,7 +8483,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.0160428, - "digest": "1f9c7c1e432eb1b13bc545a3baef359c56399488a9f10d1327a7306772e4feea" + "digest": "3048c2a9c7efeb5ce0b9b6f90d24a6a308e39a2aa43186a6c520e08439d20876" }, { "id": "eos/SF6", @@ -8491,7 +8491,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.1460554192, - "digest": "c53e6f9688a8e6b5792b9fdf7ae3080c0a93a141379f9af91102f178a8ffa090" + "digest": "66cbc9649889c977565582babdae9e7ef605f0fb442013fd2017acabb733400b" }, { "id": "eos/N2O", @@ -8499,7 +8499,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.0440128, - "digest": "f0cc1b2ce0585faec60ed296c113b3fa3d0bebd01e5712c615308b46fb0394ee" + "digest": "6c8d1fba19a813cdf4aa383e4c52b7674743b6d62a0c48327c4d3a0a84517b2f" }, { "id": "eos/CO2", @@ -8507,7 +8507,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.0440098, - "digest": "7eec0bcbef23f944c576d039b06ec22cadc36471e44cb9b338fec227b60bc3a8" + "digest": "2c56f87b1eb479fd0d01828c43aeefeff5deb6724ecbd71a475e893dd78c098a" }, { "id": "eos/CO", @@ -8515,7 +8515,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.0280101, - "digest": "f03ec881815f241028e3c8ac2e42e4c0e05dff418739fcfc6e89a13d3db89b82" + "digest": "ad98b3d1c2bdaa55ced25d860982ed050c23ae7d8c4e2e3a2d5376b4b27ad4a2" }, { "id": "eos/dry air (Lemmon 2000)", @@ -8523,7 +8523,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.02896546, - "digest": "9d30082a35f2cc45877f67b6854b7c120e65944b3eb78ddffb49fd75f273de9c" + "digest": "2cbd1887d92db515baf3116f7430e7439bfc01c9edd5764348fcde09ee716055" }, { "id": "mixture/Ar+He", @@ -8806,7 +8806,7 @@ 2.4555894952236615e+26, 4.895137230339126e+26 ], - "digest": "cbde31e27e816135f9b06dd6b64637ef8b6d89aef509657d16916ca53f407507" + "digest": "56388c9fc62cafe4ddc4be5b08ff4c4785d83c20e5505c0530c6fd038d42fadb" }, { "id": "mixture_eos/He+Ar", @@ -8814,7 +8814,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.021975301, - "digest": "cbde31e27e816135f9b06dd6b64637ef8b6d89aef509657d16916ca53f407507" + "digest": "56388c9fc62cafe4ddc4be5b08ff4c4785d83c20e5505c0530c6fd038d42fadb" }, { "id": "mixture_eos/He+Ne", @@ -8833,7 +8833,7 @@ 2.4515697973718496e+26, 4.879489213324201e+26 ], - "digest": "9b696da38af8b4132e098bf3628021381f942a627ab9b7b5d6b59f2a87dab15e" + "digest": "cc0cb0a6e52821a4fe7afee60767a427cfcab87bc4c9a3a61b44e0a9424fad2a" }, { "id": "mixture_eos/He+Ne", @@ -8841,7 +8841,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.012090801, - "digest": "9b696da38af8b4132e098bf3628021381f942a627ab9b7b5d6b59f2a87dab15e" + "digest": "cc0cb0a6e52821a4fe7afee60767a427cfcab87bc4c9a3a61b44e0a9424fad2a" }, { "id": "mixture_eos/Ne+Ar", @@ -8860,7 +8860,7 @@ 2.4593991942650856e+26, 4.9102035424782886e+26 ], - "digest": "51f40c8efe96add3cdb56afa34a1af8cc340b212491b150524dd3c7e2605a1c0" + "digest": "f6383275dbb11c0c63c02cbfed532934af6b101564acb492b78534f9d20e77ed" }, { "id": "mixture_eos/Ne+Ar", @@ -8868,7 +8868,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.0300635, - "digest": "51f40c8efe96add3cdb56afa34a1af8cc340b212491b150524dd3c7e2605a1c0" + "digest": "f6383275dbb11c0c63c02cbfed532934af6b101564acb492b78534f9d20e77ed" }, { "id": "mixture_eos/dry air", @@ -8887,7 +8887,7 @@ 2.4714505413942523e+26, 4.95657742538097e+26 ], - "digest": "30e94261c3f3bb42ee87197de43faf91574c1a2a34a69fb7c71b978e70acb768" + "digest": "6663762c50a7182ac41ea68f8b7c1b046eed25dcc6707f20f12b778affaf584d" }, { "id": "mixture_eos/dry air", @@ -8895,7 +8895,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.028959745989815624, - "digest": "30e94261c3f3bb42ee87197de43faf91574c1a2a34a69fb7c71b978e70acb768" + "digest": "6663762c50a7182ac41ea68f8b7c1b046eed25dcc6707f20f12b778affaf584d" }, { "id": "mixture_eos/CH4+N2", @@ -8914,7 +8914,7 @@ 2.4855667021998117e+26, 5.013435195413183e+26 ], - "digest": "13d8751b983b0df126d1fdf649f019ae437035c844e1061e1a56545172852a21" + "digest": "6603bd296490111cbe122f5958c8ab1530f7fdbd08f3d1b0c18811de8d9fafd6" }, { "id": "mixture_eos/CH4+N2", @@ -8922,7 +8922,7 @@ "quantity": "molar_mass", "arguments": {}, "values": 0.02202814, - "digest": "13d8751b983b0df126d1fdf649f019ae437035c844e1061e1a56545172852a21" + "digest": "6603bd296490111cbe122f5958c8ab1530f7fdbd08f3d1b0c18811de8d9fafd6" }, { "id": "transport/He", @@ -8942,7 +8942,7 @@ 2.2150723773768613e-05, 2.836096307710286e-05 ], - "digest": "d1f63e3850e68a95ac4d5937a9d436da5467a50c794d0b3ec57c225e29398ad5" + "digest": "85daa6c98ba8cfb7616f2ea854c91ef62939981d68e77fcd07a9af7ba0d81462" }, { "id": "transport/He", @@ -8962,7 +8962,7 @@ 20.7861565453831, 20.7861565453831 ], - "digest": "d1f63e3850e68a95ac4d5937a9d436da5467a50c794d0b3ec57c225e29398ad5" + "digest": "85daa6c98ba8cfb7616f2ea854c91ef62939981d68e77fcd07a9af7ba0d81462" }, { "id": "transport/Ne", @@ -8982,7 +8982,7 @@ 3.526149593637579e-05, 4.4507649295825845e-05 ], - "digest": "18d1030864a40ef60403d32e25dc87df880f07d2cc66ba5257f736cdc0bba3f3" + "digest": "5c31661dedffa593f4cbaf8cf42f229ec9c3169609a0743ebc8d3692bb4dc5c2" }, { "id": "transport/Ne", @@ -9002,7 +9002,7 @@ 20.786156545383104, 20.7861565453831 ], - "digest": "18d1030864a40ef60403d32e25dc87df880f07d2cc66ba5257f736cdc0bba3f3" + "digest": "5c31661dedffa593f4cbaf8cf42f229ec9c3169609a0743ebc8d3692bb4dc5c2" }, { "id": "transport/Ar", @@ -9022,7 +9022,7 @@ 2.5792183367556897e-05, 3.406828938165415e-05 ], - "digest": "1054ed95ba27f93ac5e991c005300493bf35e079327917f1979b61dbbb259382" + "digest": "314ba33bd21751cc900a0ae9021866a75b93d251da98c7f8252f05279053bcad" }, { "id": "transport/Ar", @@ -9042,7 +9042,7 @@ 20.7861565453831, 20.786156545383093 ], - "digest": "1054ed95ba27f93ac5e991c005300493bf35e079327917f1979b61dbbb259382" + "digest": "314ba33bd21751cc900a0ae9021866a75b93d251da98c7f8252f05279053bcad" }, { "id": "transport/Kr", @@ -9062,7 +9062,7 @@ 2.911325393282991e-05, 3.891635140064969e-05 ], - "digest": "500dc7f588d77bbdfdf84ef2609866cbac185b0dfa6004a2ad99c821440a0b4f" + "digest": "887e794bc645c916ba5167c2f8e3049beb7aa8afa0a230c93542c7673af8db59" }, { "id": "transport/Kr", @@ -9082,7 +9082,7 @@ 20.7861565453831, 20.7861565453831 ], - "digest": "500dc7f588d77bbdfdf84ef2609866cbac185b0dfa6004a2ad99c821440a0b4f" + "digest": "887e794bc645c916ba5167c2f8e3049beb7aa8afa0a230c93542c7673af8db59" }, { "id": "transport/Xe", @@ -9102,7 +9102,7 @@ 2.689206421845667e-05, 3.715314753327817e-05 ], - "digest": "538ffc501f28c09e769057dd870501f524a3f4ab626dc0c041b5c4a0d661d445" + "digest": "b687b3a2e3491de3fd8e78f3c989d524ab5385db488d06560b736afc5ad7328a" }, { "id": "transport/Xe", @@ -9122,7 +9122,7 @@ 20.7861565453831, 20.7861565453831 ], - "digest": "538ffc501f28c09e769057dd870501f524a3f4ab626dc0c041b5c4a0d661d445" + "digest": "b687b3a2e3491de3fd8e78f3c989d524ab5385db488d06560b736afc5ad7328a" }, { "id": "transport/H2", @@ -9142,7 +9142,7 @@ 9.944362180694878e-06, 1.2738454389102773e-05 ], - "digest": "401533e65cb4db0e70e11e1f5dade7c24664cc317f7fdfa7cc3b8ea1a4391653" + "digest": "32b3349d192e73878cd693476f51762e9dba651118d028781b03fe27cdaf0ad9" }, { "id": "transport/H2", @@ -9162,7 +9162,7 @@ 29.08641473713968, 29.254828306720842 ], - "digest": "401533e65cb4db0e70e11e1f5dade7c24664cc317f7fdfa7cc3b8ea1a4391653" + "digest": "32b3349d192e73878cd693476f51762e9dba651118d028781b03fe27cdaf0ad9" }, { "id": "transport/D2", @@ -9182,7 +9182,7 @@ 1.4042595663466066e-05, 1.79217525041969e-05 ], - "digest": "4dc938036ea123717808fbc87b1859ee2f12a375561a354655e102ed28e06595" + "digest": "eb7ffb7b224e8f44e5b80b41ac5e84f9ed4785ec951f1832b76e992866768434" }, { "id": "transport/D2", @@ -9202,7 +9202,7 @@ 29.20818992694547, 29.375110863905064 ], - "digest": "4dc938036ea123717808fbc87b1859ee2f12a375561a354655e102ed28e06595" + "digest": "eb7ffb7b224e8f44e5b80b41ac5e84f9ed4785ec951f1832b76e992866768434" }, { "id": "transport/N2", @@ -9222,7 +9222,7 @@ 2.0106711449660757e-05, 2.605623721593737e-05 ], - "digest": "254877fae43535dcfcb4c286ef532dabc7cb9a5dd18e2094023368b0f34db8e9" + "digest": "db8dff51f7e1940898aa5aeac8dcf6c3e0e44e9ddc36255249e7efa4bf2adaba" }, { "id": "transport/N2", @@ -9242,7 +9242,7 @@ 29.166290541105443, 29.580118867304147 ], - "digest": "254877fae43535dcfcb4c286ef532dabc7cb9a5dd18e2094023368b0f34db8e9" + "digest": "db8dff51f7e1940898aa5aeac8dcf6c3e0e44e9ddc36255249e7efa4bf2adaba" }, { "id": "transport/O2", @@ -9262,7 +9262,7 @@ 2.330424472076908e-05, 3.0469944778853437e-05 ], - "digest": "14ae7c86c0b0f3aa8c0c1afdb005f54a3d4330cdad2b47bd0dfc570bc34bb892" + "digest": "1d618eba4084608bebe59c3a4f27d5aa52a76b5e16b215455624ad0be4d18886" }, { "id": "transport/O2", @@ -9282,7 +9282,7 @@ 29.69454821127823, 31.09333622426335 ], - "digest": "14ae7c86c0b0f3aa8c0c1afdb005f54a3d4330cdad2b47bd0dfc570bc34bb892" + "digest": "1d618eba4084608bebe59c3a4f27d5aa52a76b5e16b215455624ad0be4d18886" }, { "id": "transport/CH4", @@ -9302,7 +9302,7 @@ 1.2781634821863146e-05, 1.6967220704330895e-05 ], - "digest": "105934a9e13878ea32823bda81b51a937a89d8bcd3eb7152383e2ce4711d3e3b" + "digest": "156065de7e91132fa1e6b7e5f4edb3b472d75a84fa1944fd0c6a58208d245a4d" }, { "id": "transport/CH4", @@ -9322,7 +9322,7 @@ 37.962612926287086, 46.506575042579776 ], - "digest": "105934a9e13878ea32823bda81b51a937a89d8bcd3eb7152383e2ce4711d3e3b" + "digest": "156065de7e91132fa1e6b7e5f4edb3b472d75a84fa1944fd0c6a58208d245a4d" }, { "id": "transport/SF6", @@ -9342,7 +9342,7 @@ 1.7516372861332013e-05, 2.3776728640450104e-05 ], - "digest": "3f02105cc56f0a64765b5a5795c905baae5d9cefa520871135324b8411ca955b" + "digest": "26a73f291360a001ee61ca6eea4adeb350f9e4c708955cd2cf8471cc0ffcc674" }, { "id": "transport/SF6", @@ -9362,7 +9362,7 @@ 108.05756089189755, 128.4020571866504 ], - "digest": "3f02105cc56f0a64765b5a5795c905baae5d9cefa520871135324b8411ca955b" + "digest": "26a73f291360a001ee61ca6eea4adeb350f9e4c708955cd2cf8471cc0ffcc674" }, { "id": "transport/N2O", @@ -9382,7 +9382,7 @@ 1.7236605600836998e-05, 2.3554284470030104e-05 ], - "digest": "af7840a756785463eec54ea043a756cb858c32630ce25cc6b1db185b8e9fd478" + "digest": "da83835cf73be45fdf59854bad404263bcc7daa1b1757af95059db6a1b6ab03f" }, { "id": "transport/N2O", @@ -9402,7 +9402,7 @@ 40.77801976579624, 45.86422912019624 ], - "digest": "af7840a756785463eec54ea043a756cb858c32630ce25cc6b1db185b8e9fd478" + "digest": "da83835cf73be45fdf59854bad404263bcc7daa1b1757af95059db6a1b6ab03f" }, { "id": "transport/CO2", @@ -9422,7 +9422,7 @@ 1.7346804574654503e-05, 2.3910303022709985e-05 ], - "digest": "2f154a0a246d3eacc18c93f49b3994325c4769e44b462f9518544c447dfd1442" + "digest": "b5fc9c356b12975959cc39499735e3772a5c808f2115daa756b67f15764e62f8" }, { "id": "transport/CO2", @@ -9442,7 +9442,7 @@ 39.39385874121629, 44.63187230695242 ], - "digest": "2f154a0a246d3eacc18c93f49b3994325c4769e44b462f9518544c447dfd1442" + "digest": "b5fc9c356b12975959cc39499735e3772a5c808f2115daa756b67f15764e62f8" }, { "id": "silica_bundle/blow_1989", diff --git a/tests/test_coolprop_loading.py b/tests/test_coolprop_loading.py new file mode 100644 index 00000000..10cc561a --- /dev/null +++ b/tests/test_coolprop_loading.py @@ -0,0 +1,150 @@ +# SPDX-FileCopyrightText: 2026 John C. Travers and contributors +# +# SPDX-License-Identifier: MIT + +"""CoolProp loaded without superancillaries: the same values, quietly (TC-1096). + +optmat loads CoolProp without the superancillaries it would build for every fluid. The +densities and saturation pressures optmat reads must agree with those of a CoolProp +loaded with them, over every gas's phase domain from its triple point to 600 K and 200 +bar, and CoolProp's notice of the switch must not reach standard output. Each +loading needs a process of its own: CoolProp reads the switch once, when it loads. +""" + +import json +import os +import subprocess +import sys + +import pytest + +from optmat.gas import eos + +_SWITCH = "COOLPROP_DISABLE_SUPERANCILLARIES_ENTIRELY" + +_SAMPLE = """ +import json +import numpy as np +from optmat.conditions import State +from optmat.gas.eos import equation_of_state +from optmat.gas.gases import Gas, info + +values = {} +for gas in (g for g in Gas if info(g).fluid is not None): + e = equation_of_state(gas) + low, high = e.temperature_range + for t in np.geomspace(low * (1 + 1e-9), min(high, 600.0), 12): + bottom, top = e.phase_domain(float(t)) + # The same states in both runs: pressures on a fixed grid, clear of the top, + # which moves with the saturation pressure. + for p in np.geomspace(1e2, 2e7, 40): + if bottom <= p < top * (1 - 1e-6): + state = State(float(p), float(t)) + values[f"{gas.value} {state!r}"] = e.number_density(state) + saturation = e.saturation_pressure(float(t)) + if saturation is not None: + values[f"{gas.value} {float(t)!r} K saturation"] = saturation +print(json.dumps(values)) +""" + + +def _run(code: str, *, superancillaries: bool) -> str: + """Return the standard output of code run in a fresh interpreter. + + - ``superancillaries``: whether CoolProp loads them; it is imported first, before + optmat would load it without them, to keep them. + """ + environment = {k: v for k, v in os.environ.items() if k != _SWITCH} + first = "import CoolProp.CoolProp\n" if superancillaries else "" + result = subprocess.run( + [sys.executable, "-c", first + code], + capture_output=True, + text=True, + check=True, + env=environment, + ) + return result.stdout + + +def test_the_values_read_agree_with_and_without_superancillaries(): + without = json.loads(_run(_SAMPLE, superancillaries=False)) + with_them = json.loads(_run(_SAMPLE, superancillaries=True)) + assert without.keys() == with_them.keys() + assert sum(key.endswith("saturation") for key in without) > 50 + for key, value in with_them.items(): + # Measured 2026-11-16: every density bitwise the same; the saturation pressures + # within 3e-11, but oxygen's at its triple point (146 Pa) within 2.0e-9, the + # tolerance of CoolProp's saturation solve, which they then come from. + bound = 5e-9 if key.endswith("saturation") else 1e-12 + assert without[key] == pytest.approx(value, rel=bound, abs=0.0), key + + +_PROBE = """ +from optmat.gas.eos import _abstract_state +state = _abstract_state("Helium") +try: + state.update_QT_pure_superanc(1.0, 4.0) +except ValueError: + print("without") +else: + print("with") +""" + + +def test_coolprop_is_loaded_without_superancillaries_and_says_nothing(): + # Standard output holds the probe's word alone: CoolProp's notice is not there. + assert _run(_PROBE, superancillaries=False).split() == ["without"] + + +def test_child_processes_do_not_inherit_the_switch(): + # A child would take an inherited switch for the user's and print CoolProp's notice. + code = _PROBE + "import os; print(os.environ.get('" + _SWITCH + "'))\n" + assert _run(code, superancillaries=False).split() == ["without", "None"] + + +_EARLIER_OUTPUT = """ +import ctypes +from optmat.gas import eos +libc = ctypes.CDLL(None) +libc.printf(b"before\\n") +with eos._quiet_stdout(): + libc.printf(b"hidden\\n") +libc.printf(b"after\\n") +libc.fflush(None) +""" + + +@pytest.mark.skipif(os.name != "posix", reason="libc's printf is reached through POSIX") +def test_native_output_written_before_the_block_is_kept(): + # Buffered in C (standard output is a pipe) until the block flushes it first. + assert _run(_EARLIER_OUTPUT, superancillaries=False).split() == ["before", "after"] + + +def test_a_coolprop_already_loaded_keeps_its_superancillaries(): + assert _run(_PROBE, superancillaries=True).split() == ["with"] + + +def test_a_switch_set_by_the_user_is_kept_and_coolprop_says_so(): + environment = {**os.environ, _SWITCH: "yes"} + code = _PROBE + "import os; print(os.environ['" + _SWITCH + "'])\n" + result = subprocess.run( + [sys.executable, "-c", code], + capture_output=True, + text=True, + check=True, + env=environment, + ) + # The user's value stands, and CoolProp's own notice of it is not hidden. + assert result.stdout.split()[:2] == ["without", "yes"] + assert "superancillaries have been disabled" in result.stdout + + +def test_without_a_standard_output_descriptor_the_block_still_runs(monkeypatch): + def refused(fd: int) -> int: + raise OSError(fd) + + monkeypatch.setattr(eos.os, "dup", refused) + ran = [] + with eos._quiet_stdout(): + ran.append(True) + assert ran == [True]