SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
bindings.cpp
Go to the documentation of this file.
1#include <pybind11/pybind11.h>
2#include <pybind11/stl.h> // Needed for vectors, maps, sets, strings
3#include <pybind11/stl_bind.h> // Needed for binding std::vector, std::map etc if needed directly
4
5#include <string>
6
7#include "composition.h"
8#include "atomicSpecies.h"
9#include "species.h"
10
11#include "bindings.h"
12
13namespace py = pybind11;
14
15std::string sv_to_string(std::string_view sv) {
16 return std::string(sv);
17}
18
20 std::ostringstream oss;
21 oss << comp;
22 return oss.str();
23}
24
25
26void register_comp_bindings(pybind11::module &comp_submodule) {
27 // --- Bindings for composition and species module ---
28 py::class_<serif::composition::GlobalComposition>(comp_submodule, "GlobalComposition")
29 .def_readonly("specificNumberDensity", &serif::composition::GlobalComposition::specificNumberDensity)
30 .def_readonly("meanParticleMass", &serif::composition::GlobalComposition::meanParticleMass)
31 .def("__repr__", // Add a string representation for easy printing in Python
33 return "<GlobalComposition(specNumDens=" + std::to_string(gc.specificNumberDensity) +
34 ", meanMass=" + std::to_string(gc.meanParticleMass) + ")>";
35 });
36
37 py::class_<serif::composition::CompositionEntry>(comp_submodule, "CompositionEntry")
39 .def("mass_fraction",
40 py::overload_cast<>(&serif::composition::CompositionEntry::mass_fraction, py::const_),
41 "Gets the mass fraction of the species.")
42 .def("mass_fraction",
43 py::overload_cast<double>(&serif::composition::CompositionEntry::mass_fraction, py::const_),
44 py::arg("meanMolarMass"), // Name the argument in Python
45 "Gets the mass fraction of the species given the mean molar mass.")
46 .def("number_fraction",
47 py::overload_cast<>(&serif::composition::CompositionEntry::number_fraction, py::const_),
48 "Gets the number fraction of the species.")
49 .def("number_fraction",
50 py::overload_cast<double>(&serif::composition::CompositionEntry::number_fraction, py::const_),
51 py::arg("totalMoles"),
52 "Gets the number fraction of the species given the total moles.")
53
55 .def("isotope", &serif::composition::CompositionEntry::isotope) // Assuming Species is bound or convertible
57
58 .def("__repr__", // Optional: nice string representation
60 // You might want to include more info here now
61 return "<CompositionEntry(symbol='" + ce.symbol() + "', " +
62 "mass_frac=" + std::to_string(ce.mass_fraction()) + ", " +
63 "num_frac=" + std::to_string(ce.number_fraction()) + ")>";
64 });
65
66 // --- Binding for the main Composition class ---
67 py::class_<serif::composition::Composition>(comp_submodule, "Composition")
68 // Constructors
69 .def(py::init<>(), "Default constructor")
70 .def(py::init<const std::vector<std::string>&>(),
71 py::arg("symbols"),
72 "Constructor taking a list of symbols to register (defaults to mass fraction mode)")
73 // .def(py::init<const std::set<std::string>&>(), py::arg("symbols")) // Binding std::set constructor is possible but often less convenient from Python
74 .def(py::init<const std::vector<std::string>&, const std::vector<double>&, bool>(),
75 py::arg("symbols"), py::arg("fractions"), py::arg("massFracMode") = true,
76 "Constructor taking symbols, fractions, and mode (True=Mass, False=Number)")
77
78 // Methods
79 .def("finalize", &serif::composition::Composition::finalize, py::arg("norm") = false,
80 "Finalize the composition, optionally normalizing fractions to sum to 1.")
81
82 .def("registerSymbol", py::overload_cast<const std::string&, bool>(&serif::composition::Composition::registerSymbol),
83 py::arg("symbol"), py::arg("massFracMode") = true, "Register a single symbol.")
84 .def("registerSymbol", py::overload_cast<const std::vector<std::string>&, bool>(&serif::composition::Composition::registerSymbol),
85 py::arg("symbols"), py::arg("massFracMode") = true, "Register multiple symbols.")
86
88 "Get the set of registered symbols.")
89
90 .def("setMassFraction", py::overload_cast<const std::string&, const double&>(&serif::composition::Composition::setMassFraction),
91 py::arg("symbol"), py::arg("mass_fraction"), "Set mass fraction for a single symbol (requires massFracMode). Returns old value.")
92 .def("setMassFraction", py::overload_cast<const std::vector<std::string>&, const std::vector<double>&>(&serif::composition::Composition::setMassFraction),
93 py::arg("symbols"), py::arg("mass_fractions"), "Set mass fractions for multiple symbols (requires massFracMode). Returns list of old values.")
94
95 .def("setNumberFraction", py::overload_cast<const std::string&, const double&>(&serif::composition::Composition::setNumberFraction),
96 py::arg("symbol"), py::arg("number_fraction"), "Set number fraction for a single symbol (requires !massFracMode). Returns old value.")
97 .def("setNumberFraction", py::overload_cast<const std::vector<std::string>&, const std::vector<double>&>(&serif::composition::Composition::setNumberFraction),
98 py::arg("symbols"), py::arg("number_fractions"), "Set number fractions for multiple symbols (requires !massFracMode). Returns list of old values.")
99
100 .def("mix", &serif::composition::Composition::mix, py::arg("other"), py::arg("fraction"),
101 "Mix with another composition. Returns new Composition.")
102
103 .def("getMassFraction", py::overload_cast<const std::string&>(&serif::composition::Composition::getMassFraction, py::const_),
104 py::arg("symbol"), "Get mass fraction for a symbol (calculates if needed). Requires finalization.")
105 .def("getMassFraction", py::overload_cast<>(&serif::composition::Composition::getMassFraction, py::const_),
106 "Get dictionary of all mass fractions. Requires finalization.")
107
108 .def("getNumberFraction", py::overload_cast<const std::string&>(&serif::composition::Composition::getNumberFraction, py::const_),
109 py::arg("symbol"), "Get number fraction for a symbol (calculates if needed). Requires finalization.")
110 .def("getNumberFraction", py::overload_cast<>(&serif::composition::Composition::getNumberFraction, py::const_),
111 "Get dictionary of all number fractions. Requires finalization.")
112
113 // Note: pybind11 automatically converts std::pair to a Python tuple
114 .def("getComposition", py::overload_cast<const std::string&>(&serif::composition::Composition::getComposition, py::const_),
115 py::arg("symbol"), "Returns a tuple (CompositionEntry, GlobalComposition) for the symbol. Requires finalization.")
116 // Binding the version returning map<string, Entry> requires a bit more care or helper function
117 // to convert the map to a Python dict if needed directly. Let's bind the pair version for now.
118 .def("getComposition", py::overload_cast<>(&serif::composition::Composition::getComposition, py::const_),
119 "Returns a tuple (dict[str, CompositionEntry], GlobalComposition) for all symbols. Requires finalization.")
120
121
122 .def("subset", &serif::composition::Composition::subset, py::arg("symbols"), py::arg("method") = "norm",
123 "Create a new Composition containing only the specified symbols.")
124 .def("hasSymbol", &serif::composition::Composition::hasSymbol, py::arg("symbol"),
125 "Check if a symbol is registered.")
126 .def("setCompositionMode", &serif::composition::Composition::setCompositionMode, py::arg("massFracMode"),
127 "Set the mode (True=Mass, False=Number). Requires finalization before switching.")
128
129 // Operator overload
130 .def(py::self + py::self, "Mix equally with another composition.") // Binds operator+
131
132 // Add __repr__ or __str__
133 .def("__repr__", [](const serif::composition::Composition &comp) {
134 return get_ostream_str(comp); // Use helper for C++ operator<<
135 });
136
137
138
139}
140
141void register_species_bindings(pybind11::module &chem_submodule) {
142 // --- Bindings for species module ---
143 py::class_<serif::atomic::Species>(chem_submodule, "Species")
144 .def("mass", &serif::atomic::Species::mass, "Get atomic mass (amu)")
145 .def("massUnc", &serif::atomic::Species::massUnc, "Get atomic mass uncertainty (amu)")
146 .def("bindingEnergy", &serif::atomic::Species::bindingEnergy, "Get binding energy (keV/nucleon?)") // Check units
147 .def("betaDecayEnergy", &serif::atomic::Species::betaDecayEnergy, "Get beta decay energy (keV?)") // Check units
148 .def("betaCode", [](const serif::atomic::Species& s){ return sv_to_string(s.betaCode()); }, "Get beta decay code") // Convert string_view
149 .def("name", [](const serif::atomic::Species& s){ return sv_to_string(s.name()); }, "Get species name (e.g., 'H-1')") // Convert string_view
150 .def("el", [](const serif::atomic::Species& s){ return sv_to_string(s.el()); }, "Get element symbol (e.g., 'H')") // Convert string_view
151 .def("nz", &serif::atomic::Species::nz, "Get NZ value")
152 .def("n", &serif::atomic::Species::n, "Get neutron number N")
153 .def("z", &serif::atomic::Species::z, "Get proton number Z")
154 .def("a", &serif::atomic::Species::a, "Get mass number A")
155
156 .def("__repr__",
157 [](const serif::atomic::Species &s) {
158 std::ostringstream oss;
159 oss << s;
160 return oss.str();
161 });
162
163 chem_submodule.attr("species") = py::cast(serif::atomic::species); // Expose the species map
164}
Manages the composition of elements.
double getNumberFraction(const std::string &symbol) const
Gets the number fraction for a given symbol.
bool hasSymbol(const std::string &symbol) const
Check if a symbol is registered.
std::unordered_map< std::string, double > getMassFraction() const
Gets the mass fractions of all compositions.
std::set< std::string > getRegisteredSymbols() const
Gets the registered symbols.
Composition mix(const Composition &other, double fraction) const
Mix two compositions together with a given fraction.
void registerSymbol(const std::string &symbol, bool massFracMode=true)
Registers a new symbol.
Composition subset(const std::vector< std::string > &symbols, std::string method="norm") const
Gets a subset of the composition.
std::pair< CompositionEntry, GlobalComposition > getComposition(const std::string &symbol) const
Gets the composition entry and global composition for a given symbol.
bool finalize(bool norm=false)
Finalizes the composition.
double setMassFraction(const std::string &symbol, const double &mass_fraction)
Sets the mass fraction for a given symbol.
double setNumberFraction(const std::string &symbol, const double &number_fraction)
Sets the number fraction for a given symbol.
void setCompositionMode(bool massFracMode)
Sets the composition mode.
std::string sv_to_string(std::string_view sv)
Definition bindings.cpp:15
void register_comp_bindings(pybind11::module &comp_submodule)
Definition bindings.cpp:26
std::string get_ostream_str(const serif::composition::Composition &comp)
Definition bindings.cpp:19
void register_species_bindings(pybind11::module &chem_submodule)
Definition bindings.cpp:141
Represents an entry in the composition with a symbol and mass fraction.
Definition composition.h:64
std::string symbol() const
Gets the chemical symbol of the species.
serif::atomic::Species isotope() const
Gets the isotope of the species.
double rel_abundance() const
Gets the relative abundance of the species.
double number_fraction() const
Gets the number fraction of the species.
double mass_fraction() const
Gets the mass fraction of the species.
bool getMassFracMode() const
Gets the mode of the composition entry.
Represents the global composition of a system. This tends to be used after finalize and is primarily ...
Definition composition.h:53
double meanParticleMass
The mean particle mass of the composition (\sum_{i} \frac{n_i}{m_i}. where n_i is the number fraction...
Definition composition.h:55
double specificNumberDensity
The specific number density of the composition (\sum_{i} X_i m_i. Where X_i is the number fraction of...
Definition composition.h:54