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

@@ -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);
}

View File

@@ -1,23 +1,74 @@
#include <pybind11/pybind11.h>
#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/numpy.h>
#include <string>
#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_<EOSio>(const_submodule, "EOSio")
py::class_<EOSio>(eos_submodule, "EOSio")
.def(py::init<std::string>(), 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 "<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')
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'],

View File

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