From b493de51a44078e4f5bbb8712e89305fd91f8b85 Mon Sep 17 00:00:00 2001 From: Jeff Henning Date: Sun, 10 Feb 2019 01:09:51 -0500 Subject: [PATCH] Set _phase in REFPROP backend and return it in calc_phase() (#1797) * Set _phase in REFPROP backend and return it in calc_phase() * Comment touch-up * Throw error from REFPROPMixtureBackend::calc_phase() for mixtures * Remove redundant qualifier on GetRPphase() in REFPROPMixtureBackend.h --- .../REFPROP/REFPROPMixtureBackend.cpp | 43 +++++++++++++++++++ src/Backends/REFPROP/REFPROPMixtureBackend.h | 13 ++++++ src/CoolProp.cpp | 36 ++++++++-------- 3 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/Backends/REFPROP/REFPROPMixtureBackend.cpp b/src/Backends/REFPROP/REFPROPMixtureBackend.cpp index 91e9bdf8..5456dff1 100644 --- a/src/Backends/REFPROP/REFPROPMixtureBackend.cpp +++ b/src/Backends/REFPROP/REFPROPMixtureBackend.cpp @@ -1156,6 +1156,44 @@ CoolPropDbl REFPROPMixtureBackend::calc_cpmolar_idealgas(void) return static_cast(cp0); } +phases REFPROPMixtureBackend::GetRPphase() +{ + phases RPphase = iphase_unknown; + if (ValidNumber(_Q)) + { + if ((_Q >= 0.00) && (_Q <= 1.00)) { // CoolProp includes Q = 1 or 0 in the two phase region, + RPphase = iphase_twophase; // whereas RefProp designates saturated liquid and saturated vapor. + } + else if (_Q > 1.00) { // Above saturation curve + RPphase = iphase_gas; + if (_T >= calc_T_critical()) { // ....AND T >= Tcrit + RPphase = iphase_supercritical_gas; + } + } + else if (_Q < 0.00) { // Below saturation curve + RPphase = iphase_liquid; + if (_p >= calc_p_critical()) { // ....AND P >= Pcrit + RPphase = iphase_supercritical_liquid; + } + } + else { // RefProp might return Q = 920 for Metastable + RPphase = iphase_unknown; // but CoolProp doesn't have an enumerator for this state, + } // so it's unknown as well. + + if ((_Q == 999) || (_Q == -997)) { // One last check for _Q == 999||-997 (Supercritical) + RPphase = iphase_supercritical; // T >= Tcrit AND P >= Pcrit + if ((std::abs(_T - calc_T_critical()) < 10 * DBL_EPSILON) && // IF (T == Tcrit) AND + (std::abs(_p - calc_p_critical()) < 10 * DBL_EPSILON)) { // (P == Pcrit) THEN + RPphase = iphase_critical_point; // at critical point. + }; + } + } + else { + RPphase = iphase_unknown; + } + return RPphase; +} + void REFPROPMixtureBackend::update(CoolProp::input_pairs input_pair, double value1, double value2) { this->check_loaded_fluid(); @@ -1795,6 +1833,11 @@ void REFPROPMixtureBackend::update(CoolProp::input_pairs input_pair, double valu _delta = _rhomolar/calc_rhomolar_reducing(); _gibbsmolar = hmol-_T*smol; _Q = q; + if (imposed_phase_index == iphase_not_imposed) { // If phase is imposed, _phase will already be set. + if (Ncomp == 1) { // Only set _phase for pure fluids + _phase = GetRPphase(); // Set the CoolProp _phase variable based on RefProp's quality value (q) + } + } } void REFPROPMixtureBackend::update_with_guesses(CoolProp::input_pairs input_pair, diff --git a/src/Backends/REFPROP/REFPROPMixtureBackend.h b/src/Backends/REFPROP/REFPROPMixtureBackend.h index c45b12f7..ddf3176e 100644 --- a/src/Backends/REFPROP/REFPROPMixtureBackend.h +++ b/src/Backends/REFPROP/REFPROPMixtureBackend.h @@ -77,6 +77,19 @@ public: bool using_mass_fractions(){return false;} bool using_volu_fractions(){return false;} + // Get _phase for pure fluids only + phases calc_phase(void) { + if (this->Ncomp > 1) { + throw NotImplementedError("The REFPROP backend does not implement calc_phase function for mixtures."); + } + else { + return _phase; + } + }; + + // Utility function to determine the phase from quality value return from REFPROP + phases GetRPphase(); + /** \brief Specify the phase - this phase will always be used in calculations * * @param phase_index The index from CoolProp::phases diff --git a/src/CoolProp.cpp b/src/CoolProp.cpp index 32a459f0..9e20a141 100644 --- a/src/CoolProp.cpp +++ b/src/CoolProp.cpp @@ -1048,24 +1048,24 @@ std::string phase_lookup_string(phases Phase) { switch (Phase) { - case iphase_liquid: ///< Liquid - return "liquid"; - case iphase_supercritical: ///< Supercritical (p > pc, T > Tc) - return "supercritical"; - case iphase_supercritical_gas: ///< Supercritical gas (p < pc, T > Tc) - return "supercritical_gas"; - case iphase_supercritical_liquid: ///< Supercritical liquid (p > pc, T < Tc) - return "supercritical_liquid"; - case iphase_critical_point: ///< At the critical point - return "critical_point"; - case iphase_gas: ///< Subcritical gas - return "gas"; - case iphase_twophase: ///< Twophase - return "twophase"; - case iphase_unknown: ///< Unknown phase - return "unknown"; - case iphase_not_imposed: - return "not_imposed"; + case iphase_liquid: ///< Liquid + return "liquid"; + case iphase_supercritical: ///< Supercritical (p > pc, T > Tc) + return "supercritical"; + case iphase_supercritical_gas: ///< Supercritical gas (p < pc, T > Tc) + return "supercritical_gas"; + case iphase_supercritical_liquid: ///< Supercritical liquid (p > pc, T < Tc) + return "supercritical_liquid"; + case iphase_critical_point: ///< At the critical point + return "critical_point"; + case iphase_gas: ///< Subcritical gas + return "gas"; + case iphase_twophase: ///< Twophase (between saturation curves - inclusive) + return "twophase"; + case iphase_unknown: ///< Unknown phase + return "unknown"; + case iphase_not_imposed: + return "not_imposed"; } throw ValueError("I should never be thrown"); }