From a707ac744f5552c5efc49004185eb073343530cc Mon Sep 17 00:00:00 2001 From: Jorrit Wronski Date: Mon, 1 Dec 2014 19:12:18 +0100 Subject: [PATCH] REFPROP: Added some more debugging output and changed the fluid detection. Before, setup was called like crazy... --- .../REFPROP/REFPROPMixtureBackend.cpp | 67 +++++++++++++++---- src/Backends/REFPROP/REFPROPMixtureBackend.h | 8 +-- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/Backends/REFPROP/REFPROPMixtureBackend.cpp b/src/Backends/REFPROP/REFPROPMixtureBackend.cpp index 58493114..82241d1f 100644 --- a/src/Backends/REFPROP/REFPROPMixtureBackend.cpp +++ b/src/Backends/REFPROP/REFPROPMixtureBackend.cpp @@ -104,7 +104,9 @@ std::string LoadedREFPROPRef; #endif #endif -std::string endings[] = {"", ".fld", ".ppf"}; +static bool dbg_refprop = false; + +std::string endings[] = {"", ".FLD", ".PPF"}; static char rel_path_HMC_BNC[] = "HMX.BNC"; static char default_reference_state[] = "DEF"; @@ -382,9 +384,9 @@ bool load_REFPROP() } #elif defined(__ISLINUX__) - RefpropdllInstance = dlopen ("librefprop.so", RTLD_LAZY); + RefpropdllInstance = dlopen ("librefprop.so", RTLD_NOW); #elif defined(__ISAPPLE__) - RefpropdllInstance = dlopen ("librefprop.dylib", RTLD_LAZY); + RefpropdllInstance = dlopen ("librefprop.dylib", RTLD_NOW); #else throw CoolProp::NotImplementedError("We should not reach this point."); RefpropdllInstance = NULL; @@ -450,7 +452,27 @@ REFPROPMixtureBackend::REFPROPMixtureBackend(const std::vector& flu } REFPROPMixtureBackend::~REFPROPMixtureBackend() { - // TODO Auto-generated destructor stub + // TODO: Remove this automatic unloading as soon as the bugs are fixed + if (RefpropdllInstance!=NULL) { + // Unload it + #if defined(__ISWINDOWS__) + FreeLibrary(RefpropdllInstance); + //delete RefpropdllInstance; + RefpropdllInstance = NULL; + #elif defined(__ISLINUX__) + dlclose (RefpropdllInstance); + //delete RefpropdllInstance; + RefpropdllInstance = NULL; + #elif defined(__ISAPPLE__) + dlclose (RefpropdllInstance); + //delete RefpropdllInstance; + RefpropdllInstance = NULL; + #else + throw CoolProp::NotImplementedError("We should not reach this point."); + //delete RefpropdllInstance; + RefpropdllInstance = NULL; + #endif + } } bool REFPROPMixtureBackend::_REFPROP_supported = true; // initialise with true @@ -464,6 +486,18 @@ bool REFPROPMixtureBackend::REFPROP_supported () { // Abort check if Refprop has been loaded. if (RefpropdllInstance!=NULL) return true; +// This unloading does not make a difference +// // TODO: Remove this automatic unloading as soon as the bugs are fixed +// if (RefpropdllInstance!=NULL) { +// // Unload it on Linux and Mac, no problems on Windows +// #if defined(__ISLINUX__) +// dlclose (RefpropdllInstance); +// RefpropdllInstance = NULL; +// #elif defined(__ISAPPLE__) +// dlclose (RefpropdllInstance); +// RefpropdllInstance = NULL; +// #endif +// } // Store result of previous check. if (_REFPROP_supported) { @@ -503,6 +537,7 @@ void REFPROPMixtureBackend::set_REFPROP_fluids(const std::vector &f long ierr=0; char component_string[10000], herr[errormessagelength]; std::string components_joined = strjoin(fluid_names,"|"); + std::string components_joined_raw = strjoin(fluid_names,"|"); std::string fdPath = get_REFPROP_fluid_path(); long N = static_cast(fluid_names.size()); @@ -529,12 +564,18 @@ void REFPROPMixtureBackend::set_REFPROP_fluids(const std::vector &f } // Load REFPROP if it isn't loaded yet - load_REFPROP(); + //load_REFPROP(); // This should not be needed. // If the name of the refrigerant doesn't match // that of the currently loaded refrigerant - if (LoadedREFPROPRef.compare(components_joined)) + if (!LoadedREFPROPRef.compare(components_joined_raw)) { + if (dbg_refprop) std::cout << format("%s:%d: The current fluid can be reused %s and %s match \n",__FILE__,__LINE__,components_joined.c_str(),LoadedREFPROPRef.c_str()); + return; + } + else + { + if (dbg_refprop) std::cout << format("%s:%d: The fluid %s has not been loaded before, current value is %s \n",__FILE__,__LINE__,components_joined_raw.c_str(),LoadedREFPROPRef.c_str()); char path_HMX_BNC[refpropcharlength+1]; strcpy(path_HMX_BNC, fdPath.c_str()); strcat(path_HMX_BNC, rel_path_HMC_BNC); @@ -551,11 +592,12 @@ void REFPROPMixtureBackend::set_REFPROP_fluids(const std::vector &f if (ierr == 0) // Success { - Ncomp = N; + this->Ncomp = N; mole_fractions.resize(N); mole_fractions_liq.resize(N); mole_fractions_vap.resize(N); - LoadedREFPROPRef = components_joined; + LoadedREFPROPRef = components_joined_raw; + if (dbg_refprop) std::cout << format("%s:%d: Successfully loaded REFPROP fluid: %s\n",__FILE__,__LINE__, components_joined.c_str()); return; } else if (ierr > 0) // Error @@ -574,9 +616,9 @@ void REFPROPMixtureBackend::set_REFPROP_fluids(const std::vector &f } void REFPROPMixtureBackend::set_mole_fractions(const std::vector &mole_fractions) { - if (mole_fractions.size() != Ncomp) + if (mole_fractions.size() != this->Ncomp) { - throw ValueError(format("size of mole fraction vector [%d] does not equal that of component vector [%d]",mole_fractions.size(), Ncomp)); + throw ValueError(format("size of mole fraction vector [%d] does not equal that of component vector [%d]",mole_fractions.size(), this->Ncomp)); } this->mole_fractions.resize(mole_fractions.size()); for (std::size_t i = 0; i < mole_fractions.size(); ++i) @@ -1436,10 +1478,11 @@ TEST_CASE("Check REFPROP and CoolProp values agree","[REFPROP]") } } -TEST_CASE("Check some non-state-dependent inputs for REFPROP work","[REFPROP]") +TEST_CASE("Check some non-state-dependent inputs for REFPROP work","[REFPROPS]") { + CoolProp::set_debug_level(1000); const int num_inputs = 4; - std::string inputs[num_inputs] = {"PCRIT", "TCRIT", "MOLEMASS", "RHOCRIT"}; + std::string inputs[num_inputs] = {"TCRIT", "PCRIT", "MOLEMASS", "RHOCRIT"}; for (int i = 0; i < num_inputs; ++i){ std::ostringstream ss; ss << "Check " << inputs[i]; diff --git a/src/Backends/REFPROP/REFPROPMixtureBackend.h b/src/Backends/REFPROP/REFPROPMixtureBackend.h index 89466b11..602451c6 100644 --- a/src/Backends/REFPROP/REFPROPMixtureBackend.h +++ b/src/Backends/REFPROP/REFPROPMixtureBackend.h @@ -55,9 +55,9 @@ public: /// Returns true if REFPROP is supported on this platform bool REFPROP_supported(void); - + long double calc_cpmolar_idealgas(void); - + long double calc_first_partial_deriv(parameters Of, parameters Wrt, parameters Constant); /// Set the fluids in REFPROP DLL by calling the SETUPdll function @@ -79,7 +79,7 @@ public: void set_mass_fractions(const std::vector &mass_fractions); void calc_phase_envelope(const std::string &type); - + std::vector calc_mole_fractions_liquid(void){return std::vector(mole_fractions_liq.begin(), mole_fractions_liq.end());} std::vector calc_mole_fractions_vapor(void){return std::vector(mole_fractions_vap.begin(), mole_fractions_vap.end());} @@ -101,7 +101,7 @@ public: long double calc_p_critical(void); long double calc_rhomolar_critical(void); long double calc_Ttriple(void); - + /// A wrapper function to calculate the limits for the EOS void limits(double &Tmin, double &Tmax, double &rhomolarmax, double &pmax); /// Calculate the maximum pressure