feat(composition): added az_to_species binding and added bindings for fourdst exceptions
This commit is contained in:
@@ -34,6 +34,7 @@ py_installation.install_sources(
|
||||
meson.project_source_root() + '/src-pybind/fourdst/_phys/atomic.pyi',
|
||||
meson.project_source_root() + '/src-pybind/fourdst/_phys/config.pyi',
|
||||
meson.project_source_root() + '/src-pybind/fourdst/_phys/constants.pyi',
|
||||
meson.project_source_root() + '/src-pybind/fourdst/_phys/exceptions.pyi',
|
||||
),
|
||||
subdir: 'fourdst/',
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
project('fourdst', 'cpp', version: 'v0.9.6', default_options: ['cpp_std=c++23'], meson_version: '>=1.5.0')
|
||||
project('fourdst', 'cpp', version: 'v0.9.7', default_options: ['cpp_std=c++23'], meson_version: '>=1.5.0')
|
||||
|
||||
add_project_arguments('-fvisibility=default', language: 'cpp')
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
PYBIND11_MODULE(_phys, m) {
|
||||
m.doc() = "Python bindings for the fourdst utility modules which are a part of the 4D-STAR project.";
|
||||
|
||||
auto exceptionMod = m.def_submodule("exceptions", "Exception bindings");
|
||||
register_comp_exceptions(exceptionMod);
|
||||
|
||||
auto atomicMod = m.def_submodule("atomic", "Species bindings");
|
||||
register_species_bindings(atomicMod);
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "fourdst/atomic/species.h"
|
||||
#include "fourdst/composition/utils.h"
|
||||
#include "fourdst/composition/utils/composition_hash.h"
|
||||
#include "fourdst/composition/exceptions/exceptions_composition.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
@@ -335,7 +336,7 @@ void register_comp_bindings(pybind11::module &m) {
|
||||
|
||||
}
|
||||
|
||||
void register_species_bindings(const pybind11::module &chem_submodule) {
|
||||
void register_species_bindings(pybind11::module &chem_submodule) {
|
||||
// --- Bindings for species module ---
|
||||
py::class_<fourdst::atomic::Species>(chem_submodule, "Species")
|
||||
.def("mass", &fourdst::atomic::Species::mass, "Get atomic mass (amu)")
|
||||
@@ -372,4 +373,35 @@ void register_species_bindings(const pybind11::module &chem_submodule) {
|
||||
|
||||
|
||||
chem_submodule.attr("species") = py::cast(fourdst::atomic::species); // Expose the species map
|
||||
|
||||
auto replace_dash_with_underscore = [](const std::string& str) {
|
||||
std::string result = str;
|
||||
std::ranges::replace(result, '-', '_');
|
||||
return result;
|
||||
};
|
||||
|
||||
for (const auto& [name, species] : fourdst::atomic::species) {
|
||||
chem_submodule.attr(replace_dash_with_underscore(name).c_str()) = py::cast(species);
|
||||
}
|
||||
|
||||
chem_submodule.def("az_to_species",
|
||||
[](const int a, const int z) {
|
||||
const auto result = fourdst::atomic::az_to_species(a, z);
|
||||
if (!result) {
|
||||
throw fourdst::composition::exceptions::SpeciesError(std::format("Species with A={} and Z={} not found.", a, z));
|
||||
}
|
||||
return result.value();
|
||||
},
|
||||
py::arg("a"),
|
||||
py::arg("z"),
|
||||
"Get Species object from proton number (Z) and mass number (A)."
|
||||
);
|
||||
}
|
||||
|
||||
void register_comp_exceptions(pybind11::module &m) {
|
||||
py::register_exception<fourdst::composition::exceptions::CompositionError>(m, "CompositionError");
|
||||
py::register_exception<fourdst::composition::exceptions::InvalidCompositionError>(m, "InvalidCompositionError", m.attr("CompositionError"));
|
||||
py::register_exception<fourdst::composition::exceptions::SpeciesError>(m, "SpeciesError");
|
||||
py::register_exception<fourdst::composition::exceptions::UnknownSymbolError>(m, "UnknownSymbolError", m.attr("SpeciesError"));
|
||||
py::register_exception<fourdst::composition::exceptions::UnregisteredSymbolError>(m, "UnregisteredSymbolError", m.attr("SpeciesError"));
|
||||
}
|
||||
|
||||
@@ -2,5 +2,7 @@
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
|
||||
void register_comp_exceptions(pybind11::module &m);
|
||||
void register_species_bindings(pybind11::module &m);
|
||||
void register_comp_bindings(pybind11::module &m);
|
||||
void register_species_bindings(const pybind11::module &m);
|
||||
|
||||
|
||||
@@ -6,4 +6,5 @@ from . import atomic
|
||||
from . import composition
|
||||
from . import config
|
||||
from . import constants
|
||||
__all__: list[str] = ['atomic', 'composition', 'config', 'constants']
|
||||
from . import exceptions
|
||||
__all__: list[str] = ['atomic', 'composition', 'config', 'constants', 'exceptions']
|
||||
|
||||
File diff suppressed because one or more lines are too long
15
src-pybind/fourdst/_phys/exceptions.pyi
Normal file
15
src-pybind/fourdst/_phys/exceptions.pyi
Normal file
@@ -0,0 +1,15 @@
|
||||
"""
|
||||
Exception bindings
|
||||
"""
|
||||
from __future__ import annotations
|
||||
__all__: list[str] = ['CompositionError', 'InvalidCompositionError', 'SpeciesError', 'UnknownSymbolError', 'UnregisteredSymbolError']
|
||||
class CompositionError(Exception):
|
||||
pass
|
||||
class InvalidCompositionError(CompositionError):
|
||||
pass
|
||||
class SpeciesError(Exception):
|
||||
pass
|
||||
class UnknownSymbolError(SpeciesError):
|
||||
pass
|
||||
class UnregisteredSymbolError(SpeciesError):
|
||||
pass
|
||||
Reference in New Issue
Block a user