diff --git a/build-python/meson.build b/build-python/meson.build index 7141c6e..4e07504 100644 --- a/build-python/meson.build +++ b/build-python/meson.build @@ -8,12 +8,14 @@ py_mod = py_installation.extension_module( meson.project_source_root() + '/src/python/composition/bindings.cpp', meson.project_source_root() + '/src/python/const/bindings.cpp', meson.project_source_root() + '/src/python/config/bindings.cpp', + meson.project_source_root() + '/src/python/eos/bindings.cpp', ], dependencies : [ pybind11_dep, const_dep, config_dep, composition_dep, + eos_dep, species_weight_dep ], cpp_args : ['-UNDEBUG'], # Example: Ensure assertions are enabled if needed diff --git a/src/eos/meson.build b/src/eos/meson.build index f7abb67..00e285c 100644 --- a/src/eos/meson.build +++ b/src/eos/meson.build @@ -1,12 +1,12 @@ # Define the library eos_sources = files( 'private/helm.cpp', - 'private/eosIO.cpp' + 'private/EOSio.cpp' ) eos_headers = files( 'public/helm.h', - 'public/eosIO.h' + 'public/EOSio.h' ) dependencies = [ diff --git a/src/eos/private/helm.cpp b/src/eos/private/helm.cpp index ed60a3e..e02ee41 100644 --- a/src/eos/private/helm.cpp +++ b/src/eos/private/helm.cpp @@ -43,8 +43,11 @@ using namespace std; -double** heap_allocate_contiguous_2D_memory(int rows, int cols) { - double **array = new double*[rows]; +double** heap_allocate_contiguous_2D_memory(const int rows, const int cols) { + if (rows <= 0 || cols <= 0) { + throw std::invalid_argument("Rows and columns must be positive integers."); + } + auto array = new double*[rows]; array[0] = new double[rows * cols]; for (int i = 1; i < rows; i++) { diff --git a/src/eos/public/EOSio.h b/src/eos/public/EOSio.h index 15899f0..aad9894 100644 --- a/src/eos/public/EOSio.h +++ b/src/eos/public/EOSio.h @@ -46,7 +46,7 @@ public: * @brief Constructs an EosIO object with the given filename. * @param filename The filename of the EOS table. */ - EOSio(std::string filename); + explicit EOSio(std::string filename); /** * @brief Default destructor. @@ -64,4 +64,6 @@ public: * @return A reference to the EOS table. */ EOSTable& getTable(); + + std::string getFilename() const { return m_filename; } }; diff --git a/src/python/bindings.cpp b/src/python/bindings.cpp index 1eb5d01..f6fda2e 100644 --- a/src/python/bindings.cpp +++ b/src/python/bindings.cpp @@ -5,6 +5,7 @@ #include "const/bindings.h" #include "composition/bindings.h" #include "config/bindings.h" +#include "eos/bindings.h" PYBIND11_MODULE(fourdsse_bindings, m) { m.doc() = "Python bindings for the 4DSSE project"; @@ -17,4 +18,7 @@ PYBIND11_MODULE(fourdsse_bindings, m) { auto configMod = m.def_submodule("config", "Configuration-module bindings"); register_config_bindings(configMod); + + auto eosMod = m.def_submodule("eos", "EOS-module bindings"); + register_eos_bindings(eosMod); } \ No newline at end of file diff --git a/src/python/eos/bindings.cpp b/src/python/eos/bindings.cpp index 775f567..18218b3 100644 --- a/src/python/eos/bindings.cpp +++ b/src/python/eos/bindings.cpp @@ -1,23 +1,74 @@ #include #include // Needed for vectors, maps, sets, strings #include // Needed for binding std::vector, std::map etc if needed directly +#include #include #include "helm.h" -#include "resourceManager.h" +// #include "resourceManager.h" #include "bindings.h" +#include "EOSio.h" +#include "../../eos/public/helm.h" namespace py = pybind11; void register_eos_bindings(pybind11::module &eos_submodule) { - py::class_(const_submodule, "EOSio") + py::class_(eos_submodule, "EOSio") .def(py::init(), py::arg("filename")) - .def("load", &EOSio::load) - .def_readonly("getFormat", &EOSio::getFormat) - .def_readonly("getTable", &EOSio::getTable) + // .def("load", &EOSio::load) + .def("getFormat", &EOSio::getFormat, "Get the format of the EOS table.") - .def("__repr__", [](const EOSio &eos) {) + .def("__repr__", [](const EOSio &eos) { return ""; }); -} + + py::class_(eos_submodule, "EOSTable"); + + py::class_(eos_submodule, "HELMTable") + .def_readonly("loaded", &helmholtz::HELMTable::loaded) + .def_readonly("imax", &helmholtz::HELMTable::imax) + .def_readonly("jmax", &helmholtz::HELMTable::jmax) + .def_readonly("t", &helmholtz::HELMTable::t) + .def_readonly("d", &helmholtz::HELMTable::d) + .def("__repr__", [](const helmholtz::HELMTable &table) { + return ""; + }) + .def_property_readonly("f", [](helmholtz::HELMTable &table) -> py::array_t { + // --- Check Preconditions --- + // 1. Check if dimensions are valid + if (table.imax <= 0 || table.jmax <= 0) { + // Return empty array or throw error for invalid dimensions + throw std::runtime_error("HELMTable dimensions (imax, jmax) are non-positive."); + // Alternatively: return py::array_t(); + } + // 2. Check if pointer 'f' and the data block 'f[0]' are non-null + // (Essential check assuming f could be null if not loaded/initialized) + if (!table.f || !table.f[0]) { + throw std::runtime_error("HELMTable data buffer 'f' is null or not initialized."); + // Alternatively: return py::array_t(); + } + + // --- Get necessary info --- + py::ssize_t rows = static_cast(table.imax); + py::ssize_t cols = static_cast(table.jmax); + double* data_ptr = table.f[0]; // Pointer to the start of contiguous data block + + // --- Define NumPy array shape and strides --- + std::vector shape = {rows, cols}; + std::vector strides = {cols * sizeof(double), // Stride to next row + sizeof(double)}; // Stride to next element in row + + // --- Create and return the py::array_t --- + // py::cast(table) creates a py::object that acts as the 'base'. + // This tells NumPy not to manage the memory of 'data_ptr' and + // ensures the 'table' object stays alive as long as the NumPy array view exists. + return py::array_t( + shape, // The dimensions of the array + strides, // How many bytes to step in each dimension + data_ptr, // Pointer to the actual data + py::cast(table) // Owner object (keeps C++ object alive) + ); + }, py::return_value_policy::reference_internal); // Keep parent 'table' alive + } diff --git a/src/python/eos/meson.build b/src/python/eos/meson.build index a816819..e98ace6 100644 --- a/src/python/eos/meson.build +++ b/src/python/eos/meson.build @@ -3,12 +3,14 @@ bindings_sources = files('bindings.cpp') bindings_headers = files('bindings.h') dependencies = [ - const_dep, + eos_dep, + config_dep, + resourceManager_dep, python3_dep, pybind11_dep, ] -shared_module('py_const', +shared_module('py_eos', bindings_sources, include_directories: include_directories('.'), cpp_args: ['-fvisibility=default'], diff --git a/src/python/meson.build b/src/python/meson.build index 4d21619..c865fc2 100644 --- a/src/python/meson.build +++ b/src/python/meson.build @@ -1,3 +1,4 @@ subdir('composition') subdir('const') -subdir('config') \ No newline at end of file +subdir('config') +subdir('eos') \ No newline at end of file diff --git a/src/resource/public/resourceManager.h b/src/resource/public/resourceManager.h index fb03a12..7cd7a22 100644 --- a/src/resource/public/resourceManager.h +++ b/src/resource/public/resourceManager.h @@ -18,18 +18,15 @@ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // *********************************************************************** */ -#ifndef RESOURCE_MANAGER_H -#define RESOURCE_MANAGER_H +#pragma once #include #include -#include #include #include "resourceManagerTypes.h" #include "config.h" #include "probe.h" -#include "quill/LogMacros.h" /** * @class ResourceManager @@ -131,5 +128,3 @@ public: */ std::unordered_map loadAllResources(); }; - -#endif // RESOURCE_MANAGER_H \ No newline at end of file