Add code for tabular data management

This commit is contained in:
Ian Bell
2015-06-26 22:07:27 -06:00
parent f1037d88c4
commit 59f2b0da88
2 changed files with 75 additions and 2 deletions

View File

@@ -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<CoolProp::AbstractState> &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<AbstractState> &AS)
{
const std::string path = path_to_tables(AS);
// Try to find tabular set if it is already loaded
std::map<std::string, TabularDataSet>::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<std::string, TabularDataSet>(path, set));
set.load_tables(path, AS);
return &(data[path]);
}
}
#if defined(ENABLE_CATCH)
#include "catch.hpp"

View File

@@ -568,7 +568,6 @@ class TabularBackend : public AbstractState
{
protected:
bool tables_loaded, using_single_phase_table, is_mixture;
shared_ptr<CoolProp::AbstractState> 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<std::vector<double> > *z, *dzdx, *dzdy, *d2zdx2, *d2zdxdy, *d2zdy2;
std::vector<CoolPropDbl> mole_fractions;
public:
shared_ptr<CoolProp::AbstractState> AS;
TabularBackend(shared_ptr<CoolProp::AbstractState> 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<CoolProp::AbstractState> &AS);
};
class TabularDataLibrary
{
private:
std::map<std::string, TabularDataSet> data;
public:
TabularDataLibrary(){};
std::string path_to_tables(shared_ptr<CoolProp::AbstractState> &AS){
std::vector<std::string> fluids = AS->fluid_names();
std::vector<CoolPropDbl> fractions = AS->get_mole_fractions();
std::vector<std::string> 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<AbstractState> &AS);
};
} /* namespace CoolProp*/