From 2bf58671a0c0efed643dc673f9e7440a6088c343 Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 5 May 2025 11:58:57 -0400 Subject: [PATCH 1/6] refactor(python): added global binding module --- src/python/bindings.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/python/bindings.cpp diff --git a/src/python/bindings.cpp b/src/python/bindings.cpp new file mode 100644 index 0000000..4b56e17 --- /dev/null +++ b/src/python/bindings.cpp @@ -0,0 +1,16 @@ +#include + +#include + +#include "const/bindings.h" +#include "composition/bindings.h" + +PYBIND11_MODULE(fourdsse_bindings, m) { + m.doc() = "Python bindings for the 4DSSE project"; + + auto compMod = m.def_submodule("composition", "Composition-module bindings"); + register_comp_bindings(compMod); + + auto constMod = m.def_submodule("constants", "Constants-module bindings"); + register_const_bindings(constMod); +} \ No newline at end of file From 1ed0e9cde1dcc3623403b338b844529f3932c654 Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 5 May 2025 11:59:24 -0400 Subject: [PATCH 2/6] feat(python/const): added constants bindings constants module can now be fully accessed from python --- src/python/const/bindings.cpp | 39 +++++++++++++++++++++++++++++++++++ src/python/const/bindings.h | 5 +++++ src/python/const/meson.build | 17 +++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/python/const/bindings.cpp create mode 100644 src/python/const/bindings.h create mode 100644 src/python/const/meson.build diff --git a/src/python/const/bindings.cpp b/src/python/const/bindings.cpp new file mode 100644 index 0000000..c606e76 --- /dev/null +++ b/src/python/const/bindings.cpp @@ -0,0 +1,39 @@ +#include +#include // Needed for vectors, maps, sets, strings +#include // Needed for binding std::vector, std::map etc if needed directly + +#include +#include "const.h" +#include "bindings.h" + +namespace py = pybind11; + + +void register_const_bindings(pybind11::module &const_submodule) { + py::class_(const_submodule, "Constant") + .def_readonly("name", &Constant::name) + .def_readonly("value", &Constant::value) + .def_readonly("uncertainty", &Constant::uncertainty) + .def_readonly("unit", &Constant::unit) + .def_readonly("reference", &Constant::reference) + .def("__repr__", [](const Constant &c) { + return ""; + }); + + py::class_(const_submodule, "Constants") + .def_property_readonly("loaded", &Constants::isLoaded) + .def("get", &Constants::get, py::arg("name"), "Get a constant by name. Returns None if not found.") + .def("__getitem__", &Constants::get, py::arg("name")) + .def("has", &Constants::has, py::arg("name"), "Check if a constant exists by name.") + .def("keys", &Constants::keys, "Get a list of all constant names.") + .def(py::init([]() { + return &Constants::getInstance(); + }), + // Tell pybind11 Python doesn't own this memory. + py::return_value_policy::reference, + "Get the singleton instance of Constants." + ); + +} diff --git a/src/python/const/bindings.h b/src/python/const/bindings.h new file mode 100644 index 0000000..01cd891 --- /dev/null +++ b/src/python/const/bindings.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +void register_const_bindings(pybind11::module &const_submodule); diff --git a/src/python/const/meson.build b/src/python/const/meson.build new file mode 100644 index 0000000..a816819 --- /dev/null +++ b/src/python/const/meson.build @@ -0,0 +1,17 @@ +# Define the library +bindings_sources = files('bindings.cpp') +bindings_headers = files('bindings.h') + +dependencies = [ + const_dep, + python3_dep, + pybind11_dep, +] + +shared_module('py_const', + bindings_sources, + include_directories: include_directories('.'), + cpp_args: ['-fvisibility=default'], + install : true, + dependencies: dependencies, +) From 6bcd9d2e694f0c14277b229bac0055397249765f Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 5 May 2025 12:00:08 -0400 Subject: [PATCH 3/6] build(build-python): added sources for const and global module build-python meson.build includes (and must include) all relevant source --- build-python/meson.build | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build-python/meson.build b/build-python/meson.build index bd87563..b7adeae 100644 --- a/build-python/meson.build +++ b/build-python/meson.build @@ -4,10 +4,13 @@ py_installation = import('python').find_installation('python3') py_mod = py_installation.extension_module( 'fourdsse_bindings', # Name of the generated .so/.pyd file (without extension) sources: [ + meson.project_source_root() + '/src/python/bindings.cpp', meson.project_source_root() + '/src/python/composition/bindings.cpp', + meson.project_source_root() + '/src/python/const/bindings.cpp', ], dependencies : [ pybind11_dep, + const_dep, composition_dep, species_weight_dep ], From 7df3481ff41596cee757359a7a876faa1fe3fe11 Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 5 May 2025 12:00:40 -0400 Subject: [PATCH 4/6] 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 From a3adbacc3bf0d714cc2e721b6fbbc62f6a424132 Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 5 May 2025 12:01:20 -0400 Subject: [PATCH 5/6] refactor(const): header guard -> pragma once also removed unused includes --- src/const/public/const.h | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/const/public/const.h b/src/const/public/const.h index 419382c..c65dd4e 100644 --- a/src/const/public/const.h +++ b/src/const/public/const.h @@ -18,13 +18,10 @@ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // *********************************************************************** */ -#ifndef CONST_H -#define CONST_H +#pragma once #include -#include #include -#include #include #include @@ -96,7 +93,7 @@ private: public: /** - * @brief get instance of constants singelton + * @brief get instance of constants singleton * @return instance of constants */ static Constants& getInstance() { @@ -108,7 +105,7 @@ public: * @brief Check if constants are loaded. * @return True if constants are loaded, false otherwise. */ - bool isLoaded() { return loaded_; } + bool isLoaded() const { return loaded_; } /** * @brief Get a constant by key. @@ -139,5 +136,3 @@ public: std::set keys() const; }; - -#endif From 2bd000039b3133d942f20de841269e1d6bc5ba84 Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 5 May 2025 12:01:55 -0400 Subject: [PATCH 6/6] build(python): updated build system to point to all python interface modules --- src/python/composition/meson.build | 14 ++++++++------ src/python/meson.build | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/python/composition/meson.build b/src/python/composition/meson.build index 2449f5b..fce5833 100644 --- a/src/python/composition/meson.build +++ b/src/python/composition/meson.build @@ -1,5 +1,6 @@ # Define the library bindings_sources = files('bindings.cpp') +bindings_headers = files('bindings.h') dependencies = [ composition_dep, @@ -8,9 +9,10 @@ dependencies = [ pybind11_dep, ] -libPYcomposition = shared_module('py_composition', - bindings_sources, - cpp_args: ['-fvisibility=default'], - install : true, - dependencies: dependencies, -) +shared_module('py_composition', + bindings_sources, + cpp_args: ['-fvisibility=default'], + install : true, + dependencies: dependencies, + include_directories: include_directories('.') +) \ No newline at end of file diff --git a/src/python/meson.build b/src/python/meson.build index 33d6298..be2d427 100644 --- a/src/python/meson.build +++ b/src/python/meson.build @@ -1 +1,2 @@ -subdir('composition') \ No newline at end of file +subdir('composition') +subdir('const') \ No newline at end of file