From 7df3481ff41596cee757359a7a876faa1fe3fe11 Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 5 May 2025 12:00:40 -0400 Subject: [PATCH] refactor(python/composition): updated python comp bindings to use register model --- src/python/composition/bindings.cpp | 57 +++++++++++++++-------------- src/python/composition/bindings.h | 6 +++ 2 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 src/python/composition/bindings.h diff --git a/src/python/composition/bindings.cpp b/src/python/composition/bindings.cpp index 8658fc6..a7bf817 100644 --- a/src/python/composition/bindings.cpp +++ b/src/python/composition/bindings.cpp @@ -7,6 +7,8 @@ #include "composition.h" #include "atomicSpecies.h" +#include "bindings.h" + namespace py = pybind11; std::string sv_to_string(std::string_view sv) { @@ -19,12 +21,8 @@ std::string get_ostream_str(const composition::Composition& comp) { return oss.str(); } -PYBIND11_MODULE(fourdsse_bindings, m) { // Module name must match meson.build - m.doc() = "Python bindings for the 4DSSE project"; // Optional module docstring - - auto comp_submodule = m.def_submodule("composition", "Bindings for the Composition module"); - auto chem_submodule = m.def_submodule("species", "Bindings for the Chemical Species module"); +void register_comp_bindings(pybind11::module &comp_submodule) { // --- Bindings for composition and species module --- py::class_(comp_submodule, "GlobalComposition") .def_readonly("specificNumberDensity", &composition::GlobalComposition::specificNumberDensity) @@ -135,28 +133,31 @@ PYBIND11_MODULE(fourdsse_bindings, m) { // Module name must match meson.build return get_ostream_str(comp); // Use helper for C++ operator<< }); - // --- Bindings for species module --- - py::class_(chem_submodule, "Species") - .def("mass", &chemSpecies::Species::mass, "Get atomic mass (amu)") - .def("massUnc", &chemSpecies::Species::massUnc, "Get atomic mass uncertainty (amu)") - .def("bindingEnergy", &chemSpecies::Species::bindingEnergy, "Get binding energy (keV/nucleon?)") // Check units - .def("betaDecayEnergy", &chemSpecies::Species::betaDecayEnergy, "Get beta decay energy (keV?)") // Check units - .def("betaCode", [](const chemSpecies::Species& s){ return sv_to_string(s.betaCode()); }, "Get beta decay code") // Convert string_view - .def("name", [](const chemSpecies::Species& s){ return sv_to_string(s.name()); }, "Get species name (e.g., 'H-1')") // Convert string_view - .def("el", [](const chemSpecies::Species& s){ return sv_to_string(s.el()); }, "Get element symbol (e.g., 'H')") // Convert string_view - .def("nz", &chemSpecies::Species::nz, "Get NZ value") - .def("n", &chemSpecies::Species::n, "Get neutron number N") - .def("z", &chemSpecies::Species::z, "Get proton number Z") - .def("a", &chemSpecies::Species::a, "Get mass number A") - - .def("__repr__", - [](const chemSpecies::Species &s) { - std::ostringstream oss; - oss << s; - return oss.str(); - }); - - chem_submodule.attr("species") = py::cast(chemSpecies::species); // Expose the species map -} \ No newline at end of file +} + +void register_species_bindings(pybind11::module &chem_submodule) { + // --- Bindings for species module --- + py::class_(chem_submodule, "Species") + .def("mass", &chemSpecies::Species::mass, "Get atomic mass (amu)") + .def("massUnc", &chemSpecies::Species::massUnc, "Get atomic mass uncertainty (amu)") + .def("bindingEnergy", &chemSpecies::Species::bindingEnergy, "Get binding energy (keV/nucleon?)") // Check units + .def("betaDecayEnergy", &chemSpecies::Species::betaDecayEnergy, "Get beta decay energy (keV?)") // Check units + .def("betaCode", [](const chemSpecies::Species& s){ return sv_to_string(s.betaCode()); }, "Get beta decay code") // Convert string_view + .def("name", [](const chemSpecies::Species& s){ return sv_to_string(s.name()); }, "Get species name (e.g., 'H-1')") // Convert string_view + .def("el", [](const chemSpecies::Species& s){ return sv_to_string(s.el()); }, "Get element symbol (e.g., 'H')") // Convert string_view + .def("nz", &chemSpecies::Species::nz, "Get NZ value") + .def("n", &chemSpecies::Species::n, "Get neutron number N") + .def("z", &chemSpecies::Species::z, "Get proton number Z") + .def("a", &chemSpecies::Species::a, "Get mass number A") + + .def("__repr__", + [](const chemSpecies::Species &s) { + std::ostringstream oss; + oss << s; + return oss.str(); + }); + + chem_submodule.attr("species") = py::cast(chemSpecies::species); // Expose the species map +} diff --git a/src/python/composition/bindings.h b/src/python/composition/bindings.h new file mode 100644 index 0000000..5d0f02a --- /dev/null +++ b/src/python/composition/bindings.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +void register_comp_bindings(pybind11::module &m); +void register_species_bindings(pybind11::module &m); \ No newline at end of file