Skip to content

Commit d0e9605

Browse files
authored
Merge pull request #52 from portyanikhin/with-state-specified-phase
The `with_state` method of the `Fluid` and `Mixture` classes no longer resets the specified phase
2 parents 0c913d5 + 3e50cf6 commit d0e9605

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

pyfluids/fluids/abstract_fluid.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(self):
4545
self.__temperature: float | None = None
4646
self.__triple_pressure: float | None = None
4747
self.__triple_temperature: float | None = None
48+
self.__specified_phase: Phases | None = None
4849
self._unit_converter: UnitConverter = UnitConverter()
4950
self._fraction_unit: str = (
5051
" %" if self.units_system == UnitsSystem.SIWithCelsiusAndPercents else ""
@@ -321,6 +322,8 @@ def with_state(self, first_input: Input, second_input: Input) -> AbstractFluid:
321322
:raises ValueError: If input is invalid.
322323
"""
323324
fluid = self.factory()
325+
if self.__specified_phase is not None:
326+
fluid.specify_phase(self.__specified_phase)
324327
fluid.update(first_input, second_input)
325328
return fluid
326329

@@ -373,6 +376,7 @@ def specify_phase(self, phase: Phases) -> AbstractFluid:
373376
:return: Current fluid instance.
374377
"""
375378
self._backend.specify_phase(phase.value)
379+
self.__specified_phase = phase
376380
return self
377381

378382
def unspecify_phase(self) -> AbstractFluid:
@@ -382,6 +386,7 @@ def unspecify_phase(self) -> AbstractFluid:
382386
:return: Current fluid instance.
383387
"""
384388
self._backend.unspecify_phase()
389+
self.__specified_phase = None
385390
return self
386391

387392
def isentropic_compression_to_pressure(self, pressure: float) -> AbstractFluid:

tests/fluids/test_fluid.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ def test_update_various_fluids_matches_with_coolprop(self, name: FluidsList):
152152
)
153153
assert all(
154154
[
155-
True
156-
if actual[i] is None and expected[i] is None
157-
else abs(actual[i] - expected[i]) < 1e-9
155+
(
156+
True
157+
if actual[i] is None and expected[i] is None
158+
else abs(actual[i] - expected[i]) < 1e-9
159+
)
158160
for i in range(len(actual))
159161
]
160162
)
@@ -170,10 +172,34 @@ def test_specify_phase_always_specifies_phase_for_all_further_calculations(self)
170172
self.fluid.specify_phase(Phases.Gas)
171173
with pytest.raises(ValueError):
172174
self.fluid.update(Input.pressure(101325), Input.temperature(20))
175+
with pytest.raises(ValueError):
176+
self.fluid.with_state(Input.pressure(101325), Input.temperature(20))
173177
self.fluid.unspecify_phase()
174178
self.fluid.update(
175179
Input.pressure(101325), Input.temperature(20)
176180
) # does not raise
181+
self.fluid.with_state(
182+
Input.pressure(101325), Input.temperature(20)
183+
) # does not raise
184+
185+
def test_specify_phase_always_with_methods_chaining_works_same_way(self):
186+
with pytest.raises(ValueError):
187+
self.fluid.specify_phase(Phases.Gas).update(
188+
Input.pressure(101325), Input.temperature(20)
189+
)
190+
self.fluid.unspecify_phase()
191+
with pytest.raises(ValueError):
192+
self.fluid.specify_phase(Phases.Gas).with_state(
193+
Input.pressure(101325), Input.temperature(20)
194+
)
195+
self.fluid.specify_phase(Phases.Gas)
196+
self.fluid.unspecify_phase().update(
197+
Input.pressure(101325), Input.temperature(20)
198+
) # does not raise
199+
self.fluid.specify_phase(Phases.Gas)
200+
self.fluid.unspecify_phase().with_state(
201+
Input.pressure(101325), Input.temperature(20)
202+
) # does not raise
177203

178204
def test_equals_same_returns_true(self):
179205
origin = self.fluid.with_state(Input.pressure(101325), Input.temperature(5))

0 commit comments

Comments
 (0)