diff --git a/src/eos/private/eosIO.cpp b/src/eos/private/eosIO.cpp new file mode 100644 index 0000000..ba3e471 --- /dev/null +++ b/src/eos/private/eosIO.cpp @@ -0,0 +1,36 @@ +#include + +#include "eosIO.h" +#include "helm.h" +#include "debug.h" + +EosIO::EosIO(const std::string filename) : m_filename(filename) { + load(); +} + +std::string EosIO::getFormat() const { + return m_format; +} + + +EOSTable& EosIO::getTable() { + return m_table; +} + +void EosIO::load() { + // Load the EOS table from the file + // For now, just set the format to HELM + + m_format = "helm"; + if (m_format == "helm") { + loadHelm(); + } +} + +void EosIO::loadHelm() { + // Load the HELM table from the file + auto helmTabptr = helmholtz::read_helm_table(m_filename); + m_table = std::move(helmTabptr); + m_loaded = true; +} + diff --git a/src/eos/private/helm.cpp b/src/eos/private/helm.cpp index 90c482e..ed60a3e 100644 --- a/src/eos/private/helm.cpp +++ b/src/eos/private/helm.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -121,21 +122,22 @@ namespace helmholtz { } // this function reads in the HELM table and stores in the above arrays - HELMTable read_helm_table(const std::string filename) { + std::unique_ptr read_helm_table(const std::string filename) { Config& config = Config::getInstance(); std::string logFile = config.get("EOS:Helm:LogFile", "log"); Probe::LogManager& logManager = Probe::LogManager::getInstance(); quill::Logger* logger = logManager.getLogger(logFile); LOG_INFO(logger, "read_helm_table : Reading HELM table from file {}", filename); - HELMTable table; + // Make a unique pointer to the HELMTable + std::unique_ptr table = std::make_unique(); string data; int i, j; //set T and Rho (d) arrays - for (j=0; jjmax; j++) { table->t[j] = pow(10, tlo + tstp*j); } - for (i=0; iimax; i++) { table->d[i] = pow(10, dlo + dstp*i); } ifstream helm_table(filename); if (!helm_table) { @@ -144,83 +146,83 @@ namespace helmholtz { throw std::runtime_error("Error (" + std::to_string(errorCode) + ") opening file " + filename); } //read the Helmholtz free energy and its derivatives - for (j=0; jjmax; j++) { + for (i=0; iimax; i++){ getline(helm_table, data); stringstream id(data); - id >> table.f[i][j]; - id >> table.fd[i][j]; - id >> table.ft[i][j]; - id >> table.fdd[i][j]; - id >> table.ftt[i][j]; - id >> table.fdt[i][j]; - id >> table.fddt[i][j]; - id >> table.fdtt[i][j]; - id >> table.fddtt[i][j]; + id >> table->f[i][j]; + id >> table->fd[i][j]; + id >> table->ft[i][j]; + id >> table->fdd[i][j]; + id >> table->ftt[i][j]; + id >> table->fdt[i][j]; + id >> table->fddt[i][j]; + id >> table->fdtt[i][j]; + id >> table->fddtt[i][j]; } } //read the pressure derivative with density - for (j=0; jjmax; j++) { + for (i=0; iimax; i++){ getline(helm_table, data); stringstream id(data); - id >> table.dpdf[i][j]; - id >> table.dpdfd[i][j]; - id >> table.dpdft[i][j]; - id >> table.dpdfdt[i][j]; + id >> table->dpdf[i][j]; + id >> table->dpdfd[i][j]; + id >> table->dpdft[i][j]; + id >> table->dpdfdt[i][j]; } } //read the electron chemical potential - for (j=0; jjmax; j++) { + for (i=0; iimax; i++){ getline(helm_table, data); stringstream id(data); - id >> table.ef[i][j]; - id >> table.efd[i][j]; - id >> table.eft[i][j]; - id >> table.efdt[i][j]; + id >> table->ef[i][j]; + id >> table->efd[i][j]; + id >> table->eft[i][j]; + id >> table->efdt[i][j]; } } //read the number density - for (j=0; jjmax; j++) { + for (i=0; iimax; i++){ getline(helm_table, data); stringstream id(data); - id >> table.xf[i][j]; - id >> table.xfd[i][j]; - id >> table.xft[i][j]; - id >> table.xfdt[i][j]; + id >> table->xf[i][j]; + id >> table->xfd[i][j]; + id >> table->xft[i][j]; + id >> table->xfdt[i][j]; } } helm_table.close(); //done reading // construct the temperature and density deltas and their inverses - for (j=0; jjmax; j++) { + double dth = table->t[j+1] - table->t[j]; double dt2 = dth * dth; double dti = 1.0/dth; double dt2i = 1.0/dt2; - table.dt_sav[j] = dth; - table.dt2_sav[j] = dt2; - table.dti_sav[j] = dti; - table.dt2i_sav[j] = dt2i; + table->dt_sav[j] = dth; + table->dt2_sav[j] = dt2; + table->dti_sav[j] = dti; + table->dt2i_sav[j] = dt2i; } - for (i=0; iimax; i++) { + double dd = table->d[i+1] - table->d[i]; double dd2 = dd * dd; double ddi = 1.0/dd; - table.dd_sav[i] = dd; - table.dd2_sav[i] = dd2; - table.ddi_sav[i] = ddi; + table->dd_sav[i] = dd; + table->dd2_sav[i] = dd2; + table->ddi_sav[i] = ddi; } - table.loaded = true; + table->loaded = true; return table; } diff --git a/src/eos/public/eosIO.h b/src/eos/public/eosIO.h new file mode 100644 index 0000000..4087f9f --- /dev/null +++ b/src/eos/public/eosIO.h @@ -0,0 +1,34 @@ +#ifndef EOSIO_H +#define EOSIO_H + +#include +#include +#include + +// EOS table format includes +#include "helm.h" + +using EOSTable = std::variant< + std::unique_ptr + >; + +class EosIO { +private: + std::string m_filename; + bool m_loaded = false; + std::string m_format; + EOSTable m_table; + void load(); + + // Loaders for each format, right now just HELM + void loadHelm(); +public: + EosIO(const std::string filename); + ~EosIO() = default; + + std::string getFormat() const; + + EOSTable& getTable(); +}; + +#endif // EOSIO_H \ No newline at end of file diff --git a/src/eos/public/helm.h b/src/eos/public/helm.h index bdbfdad..64e7a8a 100644 --- a/src/eos/public/helm.h +++ b/src/eos/public/helm.h @@ -35,6 +35,8 @@ #include #include +#include "debug.h" + /** * @brief 2D array template alias. * @tparam T Type of the array elements. @@ -139,6 +141,118 @@ namespace helmholtz heap_deallocate_contiguous_2D_memory(xfdt); } + // // Delete copy constructor and copy assignment operator to prevent accidental shallow copies + // HELMTable(const HELMTable&) = delete; + // HELMTable& operator=(const HELMTable&) = delete; + + // // Move constructor + // HELMTable(HELMTable&& other) noexcept + // : loaded(other.loaded), + // f(other.f), fd(other.fd), ft(other.ft), fdd(other.fdd), ftt(other.ftt), fdt(other.fdt), + // fddt(other.fddt), fdtt(other.fdtt), fddtt(other.fddtt), + // dpdf(other.dpdf), dpdfd(other.dpdfd), dpdft(other.dpdft), dpdfdt(other.dpdfdt), + // ef(other.ef), efd(other.efd), eft(other.eft), efdt(other.efdt), + // xf(other.xf), xfd(other.xfd), xft(other.xft), xfdt(other.xfdt) + // { + // other.f = nullptr; + // other.fd = nullptr; + // other.ft = nullptr; + // other.fdd = nullptr; + // other.ftt = nullptr; + // other.fdt = nullptr; + // other.fddt = nullptr; + // other.fdtt = nullptr; + // other.fddtt = nullptr; + // other.dpdf = nullptr; + // other.dpdfd = nullptr; + // other.dpdft = nullptr; + // other.dpdfdt = nullptr; + // other.ef = nullptr; + // other.efd = nullptr; + // other.eft = nullptr; + // other.efdt = nullptr; + // other.xf = nullptr; + // other.xfd = nullptr; + // other.xft = nullptr; + // other.xfdt = nullptr; + // } + + // // Move assignment operator + // HELMTable& operator=(HELMTable&& other) noexcept { + // if (this != &other) { + // // Deallocate current memory + // heap_deallocate_contiguous_2D_memory(f); + // heap_deallocate_contiguous_2D_memory(fd); + // heap_deallocate_contiguous_2D_memory(ft); + // heap_deallocate_contiguous_2D_memory(fdd); + // heap_deallocate_contiguous_2D_memory(ftt); + // heap_deallocate_contiguous_2D_memory(fdt); + // heap_deallocate_contiguous_2D_memory(fddt); + // heap_deallocate_contiguous_2D_memory(fdtt); + // heap_deallocate_contiguous_2D_memory(fddtt); + // heap_deallocate_contiguous_2D_memory(dpdf); + // heap_deallocate_contiguous_2D_memory(dpdfd); + // heap_deallocate_contiguous_2D_memory(dpdft); + // heap_deallocate_contiguous_2D_memory(dpdfdt); + // heap_deallocate_contiguous_2D_memory(ef); + // heap_deallocate_contiguous_2D_memory(efd); + // heap_deallocate_contiguous_2D_memory(eft); + // heap_deallocate_contiguous_2D_memory(efdt); + // heap_deallocate_contiguous_2D_memory(xf); + // heap_deallocate_contiguous_2D_memory(xfd); + // heap_deallocate_contiguous_2D_memory(xft); + // heap_deallocate_contiguous_2D_memory(xfdt); + + // // Transfer ownership of resources + // loaded = other.loaded; + // f = other.f; + // fd = other.fd; + // ft = other.ft; + // fdd = other.fdd; + // ftt = other.ftt; + // fdt = other.fdt; + // fddt = other.fddt; + // fdtt = other.fdtt; + // fddtt = other.fddtt; + // dpdf = other.dpdf; + // dpdfd = other.dpdfd; + // dpdft = other.dpdft; + // dpdfdt = other.dpdfdt; + // ef = other.ef; + // efd = other.efd; + // eft = other.eft; + // efdt = other.efdt; + // xf = other.xf; + // xfd = other.xfd; + // xft = other.xft; + // xfdt = other.xfdt; + + // // Null out the other object's pointers + // other.f = nullptr; + // other.fd = nullptr; + // other.ft = nullptr; + // other.fdd = nullptr; + // other.ftt = nullptr; + // other.fdt = nullptr; + // other.fddt = nullptr; + // other.fdtt = nullptr; + // other.fddtt = nullptr; + // other.dpdf = nullptr; + // other.dpdfd = nullptr; + // other.dpdft = nullptr; + // other.dpdfdt = nullptr; + // other.ef = nullptr; + // other.efd = nullptr; + // other.eft = nullptr; + // other.efdt = nullptr; + // other.xf = nullptr; + // other.xfd = nullptr; + // other.xft = nullptr; + // other.xfdt = nullptr; + // } + // return *this; + // } + friend std::ostream& operator<<(std::ostream& os, const helmholtz::HELMTable& table) { if (!table.loaded) { os << "HELMTable not loaded\n"; @@ -371,7 +485,7 @@ namespace helmholtz * @param filename Path to the file containing the table. * @return HELMTable structure containing the table data. */ - HELMTable read_helm_table(const std::string filename); + std::unique_ptr read_helm_table(const std::string filename); /** * @brief Calculate the Helmholtz EOS components.