Esempio n. 1
0
 def from_abstract_state(cls, state: CoolProp.AbstractState):
     new_state = cls(state.backend_name(), '&'.join(state.fluid_names()))
     # Uses mass fraction to work with incompressible fluids
     masses = state.get_mass_fractions()
     if len(masses) > 1:
         new_state.set_mass_fractions(masses)
     try:
         rho = state.rhomolar()
         T = state.T()
         new_state.update(CoolProp.DmolarT_INPUTS, rho, T)
     except:
         pass
     return new_state
Esempio n. 2
0
def get_critical_state(
        state: CoolProp.AbstractState) -> CoolProp.AbstractState:
    """Create a new state instance and update it with the critical point data

    Parameters
    ----------
        state : CoolProp.AbstractState

    Returns
    -------
        CoolProp.AbstractState
    """
    crit_state = CP.PyCriticalState()
    crit_state.T = np.nan
    crit_state.p = np.nan
    crit_state.rhomolar = np.nan
    crit_state.stable = False
    try:
        crit_state.T = state.T_critical()
        crit_state.p = state.p_critical()
        crit_state.rhomolar = state.rhomolar_critical()
        crit_state.stable = True
    except:
        try:
            for crit_state_tmp in state.all_critical_points():
                if crit_state_tmp.stable and (crit_state_tmp.T > crit_state.T
                                              or
                                              not np.isfinite(crit_state.T)):
                    crit_state.T = crit_state_tmp.T
                    crit_state.p = crit_state_tmp.p
                    crit_state.rhomolar = crit_state_tmp.rhomolar
                    crit_state.stable = crit_state_tmp.stable
        except:
            raise ValueError("Could not calculate the critical point data.")
    new_state = CoolProp.AbstractState(state.backend_name(),
                                       '&'.join(state.fluid_names()))
    masses = state.get_mass_fractions()
    if len(masses) > 1:
        new_state.set_mass_fractions(masses)
        # Uses mass fraction to work with incompressible fluids
        # try: new_state.build_phase_envelope("dummy")
        # except: pass
    # TODO: Remove this hack ASAP
    # Avoid problems with https://github.com/CoolProp/CoolProp/issues/1962
    BE = state.backend_name()
    FL = '&'.join(state.fluid_names())
    if BE == "HelmholtzEOSBackend" and FL == "Ammonia":
        crit_state.T *= 1.001
    msg = ""
    if np.isfinite(crit_state.rhomolar) and np.isfinite(crit_state.T):
        try:
            new_state.specify_phase(CoolProp.iphase_critical_point)
            new_state.update(CoolProp.DmolarT_INPUTS, crit_state.rhomolar,
                             crit_state.T)
            return new_state
        except Exception as e:
            msg += str(e) + " - "
            pass
        try:
            new_state.update(CoolProp.DmolarT_INPUTS, crit_state.rhomolar,
                             crit_state.T)
            return new_state
        except Exception as e:
            msg += str(e) + " - "
            pass
    if np.isfinite(crit_state.p) and np.isfinite(crit_state.T):
        try:
            new_state.specify_phase(CoolProp.iphase_critical_point)
            new_state.update(CoolProp.PT_INPUTS, crit_state.p, crit_state.T)
            return new_state
        except Exception as e:
            msg += str(e) + " - "
            pass
        try:
            new_state.update(CoolProp.PT_INPUTS, crit_state.p, crit_state.T)
            return new_state
        except Exception as e:
            msg += str(e) + " - "
            pass
    raise ValueError("Could not calculate the critical point data. " + msg)