#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 "bindings.h" #include "EOSio.h" #include "helm.h" namespace serif::eos { class EOSio; } namespace py = pybind11; void register_eos_bindings(pybind11::module &eos_submodule) { py::class_(eos_submodule, "EOSio") .def(py::init(), py::arg("filename")) // .def("load", &EOSio::load) .def("getFormat", &serif::eos::EOSio::getFormat, "Get the format of the EOS table.") .def("__repr__", [](const serif::eos::EOSio &eos) { return ""; }); py::class_(eos_submodule, "EOSTable"); py::class_(eos_submodule, "HELMTable") .def_readonly("loaded", &serif::eos::helmholtz::HELMTable::loaded) .def_readonly("imax", &serif::eos::helmholtz::HELMTable::imax) .def_readonly("jmax", &serif::eos::helmholtz::HELMTable::jmax) .def_readonly("t", &serif::eos::helmholtz::HELMTable::t) .def_readonly("d", &serif::eos::helmholtz::HELMTable::d) .def("__repr__", [](const serif::eos::helmholtz::HELMTable &table) { return ""; }) .def_property_readonly("f", [](serif::eos::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 = { static_cast(cols * sizeof(double)), // Stride to next row static_cast( 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 }