diff --git a/src/Backends/Cubics/CubicsLibrary.cpp b/src/Backends/Cubics/CubicsLibrary.cpp new file mode 100644 index 00000000..503b0191 --- /dev/null +++ b/src/Backends/Cubics/CubicsLibrary.cpp @@ -0,0 +1,79 @@ +#include +#include +#include "CubicsLibrary.h" +#include "all_cubics_JSON.h" // Makes a std::string variable called all_cubics_JSON +#include "rapidjson_include.h" + +namespace CoolProp{ + +class CubicsLibrary{ +private: + std::map fluid_map; + std::map aliases_map; + bool empty; // Is empty +public: + CubicsLibrary() : empty(true) {}; + bool is_empty(){ return empty; }; + int add_many(rapidjson::Value &listing) + { + int counter = 0; + for (rapidjson::Value:: ValueIterator itr = listing.Begin(); + itr != listing.End(); ++itr) { + CubicsValues val; + val.Tc = cpjson::get_double(*itr, "Tc"); + val.pc = cpjson::get_double(*itr, "pc"); + val.acentric = cpjson::get_double(*itr, "acentric"); + val.molemass = cpjson::get_double(*itr, "molemass"); + val.name = cpjson::get_string(*itr, "name"); + val.aliases = cpjson::get_string_array(*itr, "aliases"); + fluid_map.insert(std::pair(val.name, val) ); + + for (std::vector::const_iterator it = val.aliases.begin(); it != val.aliases.end(); ++it){ + if (aliases_map.find(*it) == aliases_map.end()){ + // It's not already in aliases map + aliases_map.insert(std::pair(*it, val.name) ); + } + } + counter ++; + } + return counter; + }; + CubicsValues get(const std::string & identifier){ + // Try to find it + std::map::iterator it = fluid_map.find(identifier); + // If it is found + if (it != fluid_map.end()) { + return it->second; + } else { + std::map::iterator italias = aliases_map.find(identifier); + if (italias != aliases_map.end()){ + // Alias was found, use it to get the fluid name, and then the cubic values + return fluid_map.find(italias->second)->second; + } + else{ + throw ValueError(format("Fluid identifier [%s] was not found in CubicsLibrary", identifier.c_str())); + } + } + }; +}; +static CubicsLibrary library; + +void load_cubics_library(){ + rapidjson::Document dd; + // This json formatted string comes from the all_cubics_JSON.h header which is a C++-escaped version of the JSON file + dd.Parse<0>(all_cubics_JSON.c_str()); + if (dd.HasParseError()){ + throw ValueError("Unable to load all_cubics_JSON.json"); + } else{ + try{ + library.add_many(dd); + }catch(std::exception &e){std::cout << e.what() << std::endl;} + } +} + +CubicsValues get_cubic_values(const std::string &identifier){ + if (library.is_empty()){ load_cubics_library(); } + return library.get(identifier); +} + +} /* namepace CoolProp */ diff --git a/src/Backends/Cubics/CubicsLibrary.h b/src/Backends/Cubics/CubicsLibrary.h new file mode 100644 index 00000000..a4fabd25 --- /dev/null +++ b/src/Backends/Cubics/CubicsLibrary.h @@ -0,0 +1,29 @@ + + +#ifndef CUBICS_LIBRARY_H +#define CUBICS_LIBRARY_H + +#include +#include + +namespace CoolProp{ + +struct CubicsValues{ + double Tc, ///< Critical temperature (K) + pc, ///< Critical pressure (Pa) + molemass, ///< Molar mass (kg/mol) + acentric; ///< Acentric factor (-) + std::string name, // name of fluid + CAS, // CAS reference number of fluid + BibTeX; // BibTex key(s) for the values + std::vector aliases; +}; + +/** + * @param identifier The name or registry number of the fluid (or an alias) + */ +CubicsValues get_cubic_values(const std::string &identifier); + +} /* namespace CoolProp */ + +#endif \ No newline at end of file