From 59f2b0da88ffa6fa58306a62af9fa49096a024fb Mon Sep 17 00:00:00 2001 From: Ian Bell Date: Fri, 26 Jun 2015 22:07:27 -0600 Subject: [PATCH] Add code for tabular data management --- src/Backends/Tabular/TabularBackends.cpp | 44 ++++++++++++++++++++++++ src/Backends/Tabular/TabularBackends.h | 33 ++++++++++++++++-- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/Backends/Tabular/TabularBackends.cpp b/src/Backends/Tabular/TabularBackends.cpp index 8108b3db..a394863e 100644 --- a/src/Backends/Tabular/TabularBackends.cpp +++ b/src/Backends/Tabular/TabularBackends.cpp @@ -8,6 +8,8 @@ #include "miniz.h" namespace CoolProp{ + + /** * @brief * @param table @@ -316,6 +318,48 @@ void CoolProp::TabularBackend::load_tables(){ if (get_debug_level() > 0){ std::cout << "Tables loaded" << std::endl; } } +void CoolProp::TabularDataSet::write_tables(const std::string &path_to_tables) +{ + make_dirs(path_to_tables); + write_table(single_phase_logph, path_to_tables, "single_phase_logph"); + write_table(single_phase_logpT, path_to_tables, "single_phase_logpT"); + write_table(pure_saturation, path_to_tables, "pure_saturation"); + write_table(phase_envelope, path_to_tables, "phase_envelope"); +} + +void CoolProp::TabularDataSet::load_tables(const std::string &path_to_tables, shared_ptr &AS) +{ + single_phase_logph.AS = AS; + single_phase_logpT.AS = AS; + pure_saturation.AS = AS; + single_phase_logph.set_limits(); + single_phase_logpT.set_limits(); + load_table(single_phase_logph, path_to_tables, "single_phase_logph.bin.z"); + load_table(single_phase_logpT, path_to_tables, "single_phase_logpT.bin.z"); + load_table(pure_saturation, path_to_tables, "pure_saturation.bin.z"); + load_table(phase_envelope, path_to_tables, "phase_envelope.bin.z"); + if (get_debug_level() > 0){ std::cout << "Tables loaded" << std::endl; } +}; + +/// Return the set of tabular datasets +CoolProp::TabularDataSet const * CoolProp::TabularDataLibrary::get_set(shared_ptr &AS) +{ + const std::string path = path_to_tables(AS); + // Try to find tabular set if it is already loaded + std::map::iterator it = data.find(path); + // It is already in the map, return it + if (it != data.end()){ + return &(it->second); + } + // It is not in the map, build it + else{ + TabularDataSet set; + data.insert(std::pair(path, set)); + set.load_tables(path, AS); + return &(data[path]); + } +} + #if defined(ENABLE_CATCH) #include "catch.hpp" diff --git a/src/Backends/Tabular/TabularBackends.h b/src/Backends/Tabular/TabularBackends.h index fb717ae4..9e64837e 100644 --- a/src/Backends/Tabular/TabularBackends.h +++ b/src/Backends/Tabular/TabularBackends.h @@ -568,7 +568,6 @@ class TabularBackend : public AbstractState { protected: bool tables_loaded, using_single_phase_table, is_mixture; - shared_ptr AS; enum selected_table_options{SELECTED_NO_TABLE=0, SELECTED_PH_TABLE, SELECTED_PT_TABLE}; selected_table_options selected_table; std::size_t cached_single_phase_i, cached_single_phase_j; @@ -576,7 +575,7 @@ class TabularBackend : public AbstractState std::vector > *z, *dzdx, *dzdy, *d2zdx2, *d2zdxdy, *d2zdy2; std::vector mole_fractions; public: - + shared_ptr AS; TabularBackend(shared_ptr AS) : tables_loaded(false), using_single_phase_table(false), AS(AS), is_mixture(false) { selected_table = SELECTED_NO_TABLE; // Flush the cached indices (set to large number) @@ -937,6 +936,36 @@ class TabularBackend : public AbstractState } } }; +/// This class contains the data for one set of Tabular data including single-phase and two-phase data +class TabularDataSet +{ +public: + LogPHTable single_phase_logph; + LogPTTable single_phase_logpT; + PureFluidSaturationTableData pure_saturation; + PhaseEnvelopeData phase_envelope; + void write_tables(const std::string &path_to_tables); + void load_tables(const std::string &path_to_tables, shared_ptr &AS); +}; + +class TabularDataLibrary +{ +private: + std::map data; +public: + TabularDataLibrary(){}; + std::string path_to_tables(shared_ptr &AS){ + std::vector fluids = AS->fluid_names(); + std::vector fractions = AS->get_mole_fractions(); + std::vector components; + for (std::size_t i = 0; i < fluids.size(); ++i){ + components.push_back(format("%s[%0.10Lf]", fluids[i].c_str(), fractions[i])); + } + return get_home_dir() + "/.CoolProp/Tables/" + AS->backend_name() + "(" + strjoin(components, "&") + ")"; + } + /// Return the set of tabular datasets + TabularDataSet const * get_set(shared_ptr &AS); +}; } /* namespace CoolProp*/