diff --git a/python/src/section/8/FissionYieldData.python.cpp b/python/src/section/8/FissionYieldData.python.cpp index 89e55bfd..00daabcc 100644 --- a/python/src/section/8/FissionYieldData.python.cpp +++ b/python/src/section/8/FissionYieldData.python.cpp @@ -1,6 +1,7 @@ // system includes #include #include +#include // local includes #include "ENDFtk/section/8/FissionYieldData.hpp" @@ -134,6 +135,61 @@ void wrap_8_FissionYieldData( python::module& module, python::module& viewmodule { return self.Y(); }, "The fission yield values and uncertainties" ) + .def_property_readonly( + + "Ypy", + [](const Component& self ) { + + // Array shape and stride + std::vector shape = { self.NFP(), 2 }; + std::vector stride = { 4*sizeof(double), sizeof(double) }; + + // create an array descriptor. If the handle (last parameter) is not void, the data is not copied. + // python::none() is the most simple type to achive this + auto array = python::array( + shape, + stride, + self.listPtr() + 2, // drop(2) + python::none()); + + // make the array read-only. hack from https://github.com/pybind/pybind11/issues/481 + reinterpret_cast(array.ptr())->flags &= ~python::detail::npy_api::NPY_ARRAY_WRITEABLE_; + + // Rerutn the array + return array; + + }, + "The fission yield values and uncertainties as numpy array" + ) + .def_property_readonly( + + "ZAFPpy", + [](const Component& self ) { + + // Array shape and stride. The underlying data are still doubles + std::vector shape = { self.NFP() }; + std::vector stride = { 4*sizeof(double) }; + + // create an array descriptor. If the handle (last parameter) is not void, the data is not copied. + // python::none() is the most simple type to achive this + auto array = python::array( + shape, + stride, + self.listPtr(), + python::none()); + + // make the array read-only. hack from https://github.com/pybind/pybind11/issues/481 + reinterpret_cast(array.ptr())->flags &= ~python::detail::npy_api::NPY_ARRAY_WRITEABLE_; + + // Function to convert array to ont. This will do a copy and reduce performance. + // Could be a proper function in a real implementation. + auto toInt = [](const python::array_t& a) -> python::array_t { return a; }; + + // Return integer array + return toInt(array); + }, + "The fission product ZA identifiers as numpy array" + ) .def_property_readonly( "fission_yields", diff --git a/src/ENDFtk/ListRecord.hpp b/src/ENDFtk/ListRecord.hpp index f2a84327..28ac1ffe 100644 --- a/src/ENDFtk/ListRecord.hpp +++ b/src/ENDFtk/ListRecord.hpp @@ -127,6 +127,13 @@ namespace ENDFtk { return std20::views::all( this->data ); } + /** + * @brief Return a pointer to the list of values for zero-copy transfer to python + */ + auto listPtr() const { + return this->data.data(); + } + /** * @brief Return the list of values */ diff --git a/src/ENDFtk/section/8/FissionYieldData.hpp b/src/ENDFtk/section/8/FissionYieldData.hpp index 26bbbff4..8937d08a 100644 --- a/src/ENDFtk/section/8/FissionYieldData.hpp +++ b/src/ENDFtk/section/8/FissionYieldData.hpp @@ -117,6 +117,13 @@ namespace section{ | std23::views::stride( 2 ); } + /** + * @brief Return a pointer to the list of values for zero-copy transfer to python + */ + auto listPtr() const { + return ListRecord::listPtr(); // ListRecord::listPtr() is protected + } + /** * @brief Return the fission yield values and uncertainties */