Skip to content

Commit f5639d7

Browse files
authored
fix(neqsim): re-flash gas-only system after liquid removal (#1553)
1 parent 1a745f3 commit f5639d7

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

src/ecalc_neqsim_wrapper/thermo.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,11 @@ def _ph_flash(thermodynamic_system: ThermodynamicSystem, use_gerg: bool, enthalp
347347
return thermodynamic_system
348348

349349
@staticmethod
350-
def _remove_liquid(thermodynamic_system: ThermodynamicSystem) -> ThermodynamicSystem:
351-
"""Remove liquid part of thermodynamic_system, return new NeqsimFluid object with only gas part."""
352-
return thermodynamic_system.clone().phaseToSystem("gas")
350+
def _remove_liquid(thermodynamic_system: ThermodynamicSystem, use_gerg: bool) -> ThermodynamicSystem:
351+
"""Return a gas-only thermodynamic system, re-flashed at the current (P, T)."""
352+
gas_only_system = thermodynamic_system.clone().phaseToSystem("gas")
353+
gas_only_system = NeqsimFluid._tp_flash(thermodynamic_system=gas_only_system, use_gerg=use_gerg)
354+
return gas_only_system
353355

354356
def clone_gas_phase(self) -> NeqsimFluid:
355357
"""Clone this fluid, extracting only the gas phase.
@@ -360,7 +362,7 @@ def clone_gas_phase(self) -> NeqsimFluid:
360362
Returns:
361363
New NeqsimFluid with gas phase only.
362364
"""
363-
gas_only_system = NeqsimFluid._remove_liquid(self._thermodynamic_system)
365+
gas_only_system = NeqsimFluid._remove_liquid(self._thermodynamic_system, use_gerg=self._use_gerg)
364366
return NeqsimFluid(thermodynamic_system=gas_only_system, use_gerg=self._use_gerg)
365367

366368
def copy(self) -> NeqsimFluid:

tests/ecalc_neqsim_wrapper/integration_tests/test_remove_liquid.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,49 @@ def test_liquid_takeoff(inlet_fluid, outlet_fluid) -> None:
122122
assert math.isclose(outlet_fluid.pressure_bara, fluid_after_liquid_takeoff.pressure_bara, rel_tol=0.01)
123123
assert math.isclose(outlet_fluid.molar_mass, fluid_after_liquid_takeoff.molar_mass, rel_tol=0.01)
124124
assert math.isclose(
125-
outlet_fluid.enthalpy_joule_per_kg, fluid_after_liquid_takeoff.enthalpy_joule_per_kg, rel_tol=0.5
125+
outlet_fluid.enthalpy_joule_per_kg, fluid_after_liquid_takeoff.enthalpy_joule_per_kg, rel_tol=0.02
126126
)
127+
128+
129+
# Propane-rich composition that is ~97% liquid at the probe (P, T) below.
130+
DENSE_LIQUID_COMPOSITION = FluidComposition(
131+
water=0.003,
132+
nitrogen=2.447,
133+
CO2=0.64,
134+
methane=41.91,
135+
ethane=19.9,
136+
propane=24.29,
137+
i_butane=3.64,
138+
n_butane=5.40,
139+
i_pentane=0.83,
140+
n_pentane=0.68,
141+
n_hexane=0.26,
142+
).normalized()
143+
144+
145+
def test_clone_gas_phase_returns_initialized_properties() -> None:
146+
"""clone_gas_phase() on a two-phase fluid must return properties matching a clean PT-flash of the gas-only composition."""
147+
two_phase = NeqsimFluid.create_thermo_system(
148+
composition=DENSE_LIQUID_COMPOSITION,
149+
temperature_kelvin=301.45,
150+
pressure_bara=91.476,
151+
)
152+
assert two_phase.vapor_fraction_molar < 0.1, (
153+
f"Test precondition: expected mostly-liquid state, got vap={two_phase.vapor_fraction_molar}"
154+
)
155+
156+
gas_only = two_phase.clone_gas_phase()
157+
158+
assert math.isclose(gas_only.vapor_fraction_molar, 1.0, rel_tol=1e-3)
159+
# Uninitialized systems return the sentinel kappa==1.0.
160+
assert gas_only.kappa > 1.02, f"Suspicious kappa (uninitialized?): {gas_only.kappa}"
161+
162+
reference = NeqsimFluid.create_thermo_system(
163+
composition=gas_only.composition,
164+
temperature_kelvin=gas_only.temperature_kelvin,
165+
pressure_bara=gas_only.pressure_bara,
166+
)
167+
assert math.isclose(gas_only.kappa, reference.kappa, rel_tol=1e-3)
168+
assert math.isclose(gas_only.z, reference.z, rel_tol=1e-3)
169+
assert math.isclose(gas_only.density, reference.density, rel_tol=1e-3)
170+
assert math.isclose(gas_only.enthalpy_joule_per_kg, reference.enthalpy_joule_per_kg, rel_tol=1e-3)

0 commit comments

Comments
 (0)