1#include <pybind11/pybind11.h>
2#include <pybind11/stl.h>
3#include <pybind11/stl_bind.h>
8#include "atomicSpecies.h"
13namespace py = pybind11;
16 return std::string(sv);
20 std::ostringstream oss;
28 py::class_<serif::composition::GlobalComposition>(comp_submodule,
"GlobalComposition")
37 py::class_<serif::composition::CompositionEntry>(comp_submodule,
"CompositionEntry")
41 "Gets the mass fraction of the species.")
44 py::arg(
"meanMolarMass"),
45 "Gets the mass fraction of the species given the mean molar mass.")
46 .def(
"number_fraction",
48 "Gets the number fraction of the species.")
49 .def(
"number_fraction",
51 py::arg(
"totalMoles"),
52 "Gets the number fraction of the species given the total moles.")
61 return "<CompositionEntry(symbol='" + ce.
symbol() +
"', " +
67 py::class_<serif::composition::Composition>(comp_submodule,
"Composition")
69 .def(py::init<>(),
"Default constructor")
70 .def(py::init<
const std::vector<std::string>&>(),
72 "Constructor taking a list of symbols to register (defaults to mass fraction mode)")
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)")
80 "Finalize the composition, optionally normalizing fractions to sum to 1.")
83 py::arg(
"symbol"), py::arg(
"massFracMode") =
true,
"Register a single symbol.")
85 py::arg(
"symbols"), py::arg(
"massFracMode") =
true,
"Register multiple symbols.")
88 "Get the set of registered symbols.")
91 py::arg(
"symbol"), py::arg(
"mass_fraction"),
"Set mass fraction for a single symbol (requires massFracMode). Returns old value.")
93 py::arg(
"symbols"), py::arg(
"mass_fractions"),
"Set mass fractions for multiple symbols (requires massFracMode). Returns list of old values.")
96 py::arg(
"symbol"), py::arg(
"number_fraction"),
"Set number fraction for a single symbol (requires !massFracMode). Returns old value.")
98 py::arg(
"symbols"), py::arg(
"number_fractions"),
"Set number fractions for multiple symbols (requires !massFracMode). Returns list of old values.")
101 "Mix with another composition. Returns new Composition.")
104 py::arg(
"symbol"),
"Get mass fraction for a symbol (calculates if needed). Requires finalization.")
106 "Get dictionary of all mass fractions. Requires finalization.")
109 py::arg(
"symbol"),
"Get number fraction for a symbol (calculates if needed). Requires finalization.")
111 "Get dictionary of all number fractions. Requires finalization.")
115 py::arg(
"symbol"),
"Returns a tuple (CompositionEntry, GlobalComposition) for the symbol. Requires finalization.")
119 "Returns a tuple (dict[str, CompositionEntry], GlobalComposition) for all symbols. Requires finalization.")
123 "Create a new Composition containing only the specified symbols.")
125 "Check if a symbol is registered.")
127 "Set the mode (True=Mass, False=Number). Requires finalization before switching.")
130 .def(py::self + py::self,
"Mix equally with another composition.")
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?)")
147 .def(
"betaDecayEnergy", &serif::atomic::Species::betaDecayEnergy,
"Get beta decay energy (keV?)")
148 .def(
"betaCode", [](
const serif::atomic::Species& s){
return sv_to_string(s.betaCode()); },
"Get beta decay code")
149 .def(
"name", [](
const serif::atomic::Species& s){
return sv_to_string(s.name()); },
"Get species name (e.g., 'H-1')")
150 .def(
"el", [](
const serif::atomic::Species& s){
return sv_to_string(s.el()); },
"Get element symbol (e.g., 'H')")
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")
157 [](
const serif::atomic::Species &s) {
158 std::ostringstream oss;
163 chem_submodule.attr(
"species") = py::cast(serif::atomic::species);
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)
void register_comp_bindings(pybind11::module &comp_submodule)
std::string get_ostream_str(const serif::composition::Composition &comp)
void register_species_bindings(pybind11::module &chem_submodule)
Represents an entry in the composition with a symbol and mass fraction.
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 ...
double meanParticleMass
The mean particle mass of the composition (\sum_{i} \frac{n_i}{m_i}. where n_i is the number fraction...
double specificNumberDensity
The specific number density of the composition (\sum_{i} X_i m_i. Where X_i is the number fraction of...