portyanikhin / PyFluids

Simple, full-featured, lightweight CoolProp wrapper for Python
https://pypi.org/project/pyfluids
MIT License
51 stars 2 forks source link

Unable to Impose Mixture Phase During Creation #44

Closed lestephen closed 1 month ago

lestephen commented 1 month ago

While specify_phase is available to specify mixture phase after the creation of the mixture object, there does not appear to be a mechanism for specifying the phase using with_state.

This prevents the creation of complex mixture such as:

mixture = Mixture([FluidsList.Nitrogen, FluidsList.Oxygen, FluidsList.CarbonDioxide, FluidsList.Argon, FluidsList.Water],
[72.37, 11.33, 2.03, 00.87, 13.4]).with_state(
Input.pressure(101449), Input.temperature(623.706))

which yields the following error:

ValueError: One stationary point (not good) for T=623.706,p=101449,z=[ 0.689054749976, 0.0944405350566, 0.0123029388101, 0.00580880011636, 0.198392976041 ]

This mixture can be created using CoolProp's Python bindings with the following code, which imposes a gas phase constraint (note that temperature units are K):

CP.PropsSI('D','T|gas',896.856,'P',101449,'HEOS::Nitrogen[0.7237]&Oxygen[0.1133]&CarbonDioxide[0.0203]&Argon[0.0087]&Water[0.134]')

Could Input.pressure() or Input.temperature() be modified so that a second optional parameter is an imposed phase?

portyanikhin commented 1 month ago

Hi, @lestephen!

I think your solution is somewhat counterintuitive and imposes unnecessary obligations on the user if he wants to extend the Input class (I mean this example).

If we can do something like that now:

from pyfluids import Mixture, FluidsList, Input, Phases

mixture = Mixture(
    [
        FluidsList.Nitrogen,
        FluidsList.Oxygen,
        FluidsList.CarbonDioxide,
        FluidsList.Argon,
        FluidsList.Water,
    ],
    [72.37, 11.33, 2.03, 0.87, 13.4],
)
mixture.specify_phase(Phases.Gas)
mixture.update(Input.pressure(101449), Input.temperature(623.706))
print(mixture.density)

Then the best solution would be to return an instance of the Fluid or Mixture from the specify_phase / unspecify_phase method. With this, we will be able to do the following:

from pyfluids import Mixture, FluidsList, Input, Phases

mixture = (
    Mixture(
        [
            FluidsList.Nitrogen,
            FluidsList.Oxygen,
            FluidsList.CarbonDioxide,
            FluidsList.Argon,
            FluidsList.Water,
        ],
        [72.37, 11.33, 2.03, 0.87, 13.4],
    )
    .specify_phase(Phases.Gas)
    .with_state(Input.pressure(101449), Input.temperature(623.706))
)
print(mixture.density)

What do you think?

lestephen commented 1 month ago

Thanks for responding so quickly. Yes, I think this is a better solution than what I had proposed.

portyanikhin commented 1 month ago

Ok, I'll implement it soon👌