feat(python-eos): work on python eos module

This commit is contained in:
2025-05-13 14:18:38 -04:00
parent bc36dd459d
commit b5980ea57a
9 changed files with 81 additions and 21 deletions

View File

@@ -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/composition/bindings.cpp',
meson.project_source_root() + '/src/python/const/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/config/bindings.cpp',
meson.project_source_root() + '/src/python/eos/bindings.cpp',
], ],
dependencies : [ dependencies : [
pybind11_dep, pybind11_dep,
const_dep, const_dep,
config_dep, config_dep,
composition_dep, composition_dep,
eos_dep,
species_weight_dep species_weight_dep
], ],
cpp_args : ['-UNDEBUG'], # Example: Ensure assertions are enabled if needed cpp_args : ['-UNDEBUG'], # Example: Ensure assertions are enabled if needed

View File

@@ -1,12 +1,12 @@
# Define the library # Define the library
eos_sources = files( eos_sources = files(
'private/helm.cpp', 'private/helm.cpp',
'private/eosIO.cpp' 'private/EOSio.cpp'
) )
eos_headers = files( eos_headers = files(
'public/helm.h', 'public/helm.h',
'public/eosIO.h' 'public/EOSio.h'
) )
dependencies = [ dependencies = [

View File

@@ -43,8 +43,11 @@
using namespace std; using namespace std;
double** heap_allocate_contiguous_2D_memory(int rows, int cols) { double** heap_allocate_contiguous_2D_memory(const int rows, const int cols) {
double **array = new double*[rows]; 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]; array[0] = new double[rows * cols];
for (int i = 1; i < rows; i++) { for (int i = 1; i < rows; i++) {

View File

@@ -46,7 +46,7 @@ public:
* @brief Constructs an EosIO object with the given filename. * @brief Constructs an EosIO object with the given filename.
* @param filename The filename of the EOS table. * @param filename The filename of the EOS table.
*/ */
EOSio(std::string filename); explicit EOSio(std::string filename);
/** /**
* @brief Default destructor. * @brief Default destructor.
@@ -64,4 +64,6 @@ public:
* @return A reference to the EOS table. * @return A reference to the EOS table.
*/ */
EOSTable& getTable(); EOSTable& getTable();
std::string getFilename() const { return m_filename; }
}; };

View File

@@ -5,6 +5,7 @@
#include "const/bindings.h" #include "const/bindings.h"
#include "composition/bindings.h" #include "composition/bindings.h"
#include "config/bindings.h" #include "config/bindings.h"
#include "eos/bindings.h"
PYBIND11_MODULE(fourdsse_bindings, m) { PYBIND11_MODULE(fourdsse_bindings, m) {
m.doc() = "Python bindings for the 4DSSE project"; 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"); auto configMod = m.def_submodule("config", "Configuration-module bindings");
register_config_bindings(configMod); register_config_bindings(configMod);
auto eosMod = m.def_submodule("eos", "EOS-module bindings");
register_eos_bindings(eosMod);
} }

View File

@@ -1,23 +1,74 @@
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <pybind11/stl.h> // Needed for vectors, maps, sets, strings #include <pybind11/stl.h> // Needed for vectors, maps, sets, strings
#include <pybind11/stl_bind.h> // Needed for binding std::vector, std::map etc if needed directly #include <pybind11/stl_bind.h> // Needed for binding std::vector, std::map etc if needed directly
#include <pybind11/numpy.h>
#include <string> #include <string>
#include "helm.h" #include "helm.h"
#include "resourceManager.h" // #include "resourceManager.h"
#include "bindings.h" #include "bindings.h"
#include "EOSio.h"
#include "../../eos/public/helm.h"
namespace py = pybind11; namespace py = pybind11;
void register_eos_bindings(pybind11::module &eos_submodule) { void register_eos_bindings(pybind11::module &eos_submodule) {
py::class_<EOSio>(const_submodule, "EOSio") py::class_<EOSio>(eos_submodule, "EOSio")
.def(py::init<std::string>(), py::arg("filename")) .def(py::init<std::string>(), py::arg("filename"))
.def("load", &EOSio::load) // .def("load", &EOSio::load)
.def_readonly("getFormat", &EOSio::getFormat) .def("getFormat", &EOSio::getFormat, "Get the format of the EOS table.")
.def_readonly("getTable", &EOSio::getTable)
.def("__repr__", [](const EOSio &eos) {) .def("__repr__", [](const EOSio &eos) {
return "<EOSio(filename='" + eos.getFilename() + "', format='" + eos.getFormat() + "')>"; return "<EOSio(filename='" + eos.getFilename() + "', format='" + eos.getFormat() + "')>";
}); });
}
py::class_<EOSTable>(eos_submodule, "EOSTable");
py::class_<helmholtz::HELMTable>(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 "<HELMTable(loaded=" + std::to_string(table.loaded) + ", imax=" + std::to_string(table.imax) +
", jmax=" + std::to_string(table.jmax) + ")>";
})
.def_property_readonly("f", [](helmholtz::HELMTable &table) -> py::array_t<double> {
// --- 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<double>();
}
// 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<double>();
}
// --- Get necessary info ---
py::ssize_t rows = static_cast<py::ssize_t>(table.imax);
py::ssize_t cols = static_cast<py::ssize_t>(table.jmax);
double* data_ptr = table.f[0]; // Pointer to the start of contiguous data block
// --- Define NumPy array shape and strides ---
std::vector<py::ssize_t> shape = {rows, cols};
std::vector<py::ssize_t> 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<double>(
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
}

View File

@@ -3,12 +3,14 @@ bindings_sources = files('bindings.cpp')
bindings_headers = files('bindings.h') bindings_headers = files('bindings.h')
dependencies = [ dependencies = [
const_dep, eos_dep,
config_dep,
resourceManager_dep,
python3_dep, python3_dep,
pybind11_dep, pybind11_dep,
] ]
shared_module('py_const', shared_module('py_eos',
bindings_sources, bindings_sources,
include_directories: include_directories('.'), include_directories: include_directories('.'),
cpp_args: ['-fvisibility=default'], cpp_args: ['-fvisibility=default'],

View File

@@ -1,3 +1,4 @@
subdir('composition') subdir('composition')
subdir('const') subdir('const')
subdir('config') subdir('config')
subdir('eos')

View File

@@ -18,18 +18,15 @@
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// //
// *********************************************************************** */ // *********************************************************************** */
#ifndef RESOURCE_MANAGER_H #pragma once
#define RESOURCE_MANAGER_H
#include <vector> #include <vector>
#include <string> #include <string>
#include <stdexcept>
#include <unordered_map> #include <unordered_map>
#include "resourceManagerTypes.h" #include "resourceManagerTypes.h"
#include "config.h" #include "config.h"
#include "probe.h" #include "probe.h"
#include "quill/LogMacros.h"
/** /**
* @class ResourceManager * @class ResourceManager
@@ -131,5 +128,3 @@ public:
*/ */
std::unordered_map<std::string, bool> loadAllResources(); std::unordered_map<std::string, bool> loadAllResources();
}; };
#endif // RESOURCE_MANAGER_H