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