Merge pull request #55 from tboudreaux/feature/pythonInterface/const
Constant Module Interface
This commit is contained in:
@@ -4,10 +4,13 @@ py_installation = import('python').find_installation('python3')
|
|||||||
py_mod = py_installation.extension_module(
|
py_mod = py_installation.extension_module(
|
||||||
'fourdsse_bindings', # Name of the generated .so/.pyd file (without extension)
|
'fourdsse_bindings', # Name of the generated .so/.pyd file (without extension)
|
||||||
sources: [
|
sources: [
|
||||||
|
meson.project_source_root() + '/src/python/bindings.cpp',
|
||||||
meson.project_source_root() + '/src/python/composition/bindings.cpp',
|
meson.project_source_root() + '/src/python/composition/bindings.cpp',
|
||||||
|
meson.project_source_root() + '/src/python/const/bindings.cpp',
|
||||||
],
|
],
|
||||||
dependencies : [
|
dependencies : [
|
||||||
pybind11_dep,
|
pybind11_dep,
|
||||||
|
const_dep,
|
||||||
composition_dep,
|
composition_dep,
|
||||||
species_weight_dep
|
species_weight_dep
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -18,13 +18,10 @@
|
|||||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
//
|
//
|
||||||
// *********************************************************************** */
|
// *********************************************************************** */
|
||||||
#ifndef CONST_H
|
#pragma once
|
||||||
#define CONST_H
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
@@ -96,7 +93,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief get instance of constants singelton
|
* @brief get instance of constants singleton
|
||||||
* @return instance of constants
|
* @return instance of constants
|
||||||
*/
|
*/
|
||||||
static Constants& getInstance() {
|
static Constants& getInstance() {
|
||||||
@@ -108,7 +105,7 @@ public:
|
|||||||
* @brief Check if constants are loaded.
|
* @brief Check if constants are loaded.
|
||||||
* @return True if constants are loaded, false otherwise.
|
* @return True if constants are loaded, false otherwise.
|
||||||
*/
|
*/
|
||||||
bool isLoaded() { return loaded_; }
|
bool isLoaded() const { return loaded_; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get a constant by key.
|
* @brief Get a constant by key.
|
||||||
@@ -139,5 +136,3 @@ public:
|
|||||||
std::set<std::string> keys() const;
|
std::set<std::string> keys() const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
16
src/python/bindings.cpp
Normal file
16
src/python/bindings.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <pybind11/pybind11.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@
|
|||||||
#include "composition.h"
|
#include "composition.h"
|
||||||
#include "atomicSpecies.h"
|
#include "atomicSpecies.h"
|
||||||
|
|
||||||
|
#include "bindings.h"
|
||||||
|
|
||||||
namespace py = pybind11;
|
namespace py = pybind11;
|
||||||
|
|
||||||
std::string sv_to_string(std::string_view sv) {
|
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();
|
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 ---
|
// --- Bindings for composition and species module ---
|
||||||
py::class_<composition::GlobalComposition>(comp_submodule, "GlobalComposition")
|
py::class_<composition::GlobalComposition>(comp_submodule, "GlobalComposition")
|
||||||
.def_readonly("specificNumberDensity", &composition::GlobalComposition::specificNumberDensity)
|
.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<<
|
return get_ostream_str(comp); // Use helper for C++ operator<<
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- Bindings for species module ---
|
|
||||||
py::class_<chemSpecies::Species>(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
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void register_species_bindings(pybind11::module &chem_submodule) {
|
||||||
|
// --- Bindings for species module ---
|
||||||
|
py::class_<chemSpecies::Species>(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
|
||||||
|
}
|
||||||
|
|||||||
6
src/python/composition/bindings.h
Normal file
6
src/python/composition/bindings.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <pybind11/pybind11.h>
|
||||||
|
|
||||||
|
void register_comp_bindings(pybind11::module &m);
|
||||||
|
void register_species_bindings(pybind11::module &m);
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
# Define the library
|
# Define the library
|
||||||
bindings_sources = files('bindings.cpp')
|
bindings_sources = files('bindings.cpp')
|
||||||
|
bindings_headers = files('bindings.h')
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
composition_dep,
|
composition_dep,
|
||||||
@@ -8,9 +9,10 @@ dependencies = [
|
|||||||
pybind11_dep,
|
pybind11_dep,
|
||||||
]
|
]
|
||||||
|
|
||||||
libPYcomposition = shared_module('py_composition',
|
shared_module('py_composition',
|
||||||
bindings_sources,
|
bindings_sources,
|
||||||
cpp_args: ['-fvisibility=default'],
|
cpp_args: ['-fvisibility=default'],
|
||||||
install : true,
|
install : true,
|
||||||
dependencies: dependencies,
|
dependencies: dependencies,
|
||||||
)
|
include_directories: include_directories('.')
|
||||||
|
)
|
||||||
39
src/python/const/bindings.cpp
Normal file
39
src/python/const/bindings.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#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 <string>
|
||||||
|
#include "const.h"
|
||||||
|
#include "bindings.h"
|
||||||
|
|
||||||
|
namespace py = pybind11;
|
||||||
|
|
||||||
|
|
||||||
|
void register_const_bindings(pybind11::module &const_submodule) {
|
||||||
|
py::class_<Constant>(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 "<Constant(name='" + c.name + "', value=" + std::to_string(c.value) +
|
||||||
|
", uncertainty=" + std::to_string(c.uncertainty) +
|
||||||
|
", unit='" + c.unit + "')>";
|
||||||
|
});
|
||||||
|
|
||||||
|
py::class_<Constants>(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."
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
5
src/python/const/bindings.h
Normal file
5
src/python/const/bindings.h
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <pybind11/pybind11.h>
|
||||||
|
|
||||||
|
void register_const_bindings(pybind11::module &const_submodule);
|
||||||
17
src/python/const/meson.build
Normal file
17
src/python/const/meson.build
Normal file
@@ -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,
|
||||||
|
)
|
||||||
@@ -1 +1,2 @@
|
|||||||
subdir('composition')
|
subdir('composition')
|
||||||
|
subdir('const')
|
||||||
Reference in New Issue
Block a user