GridFire 0.0.1a General Purpose Nuclear Network

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_view>
6#include <vector>
7
8#include "bindings.h"
9
10#include "gridfire/reaction/reaction.h"
11#include "gridfire/reaction/reaclib.h"
12
13 namespace py = pybind11;
14
15
16 void register_reaction_bindings(py::module &m) {
17 py::class_<gridfire::reaction::RateCoefficientSet>(m, "RateCoefficientSet")
18 .def(py::init<double, double, double, double, double, double, double>(),
19 py::arg("a0"), py::arg("a1"), py::arg("a2"), py::arg("a3"),
20 py::arg("a4"), py::arg("a5"), py::arg("a6"),
21 "Construct a RateCoefficientSet with the given parameters."
22 );
23
24 using fourdst::atomic::Species;
25 py::class_<gridfire::reaction::Reaction>(m, "Reaction")
26 .def(py::init<const std::string_view, const std::string_view, int, const std::vector<Species>&, const std::vector<Species>&, double, std::string_view, gridfire::reaction::RateCoefficientSet, bool>(),
27 py::arg("id"), py::arg("peName"), py::arg("chapter"),
28 py::arg("reactants"), py::arg("products"), py::arg("qValue"),
29 py::arg("label"), py::arg("sets"), py::arg("reverse") = false,
30 "Construct a Reaction with the given parameters.")
31 .def("calculate_rate", static_cast<double (gridfire::reaction::Reaction::*)(double) const>(&gridfire::reaction::Reaction::calculate_rate),
32 py::arg("T9"), "Calculate the reaction rate at a given temperature T9 (in units of 10^9 K).")
33 .def("peName", &gridfire::reaction::Reaction::peName,
34 "Get the reaction name in (projectile, ejectile) notation (e.g., 'p(p,g)d').")
35 .def("chapter", &gridfire::reaction::Reaction::chapter,
36 "Get the REACLIB chapter number defining the reaction structure.")
37 .def("sourceLabel", &gridfire::reaction::Reaction::sourceLabel,
38 "Get the source label for the rate data (e.g., 'wc12w', 'st08').")
39 .def("rateCoefficients", &gridfire::reaction::Reaction::rateCoefficients,
40 "get the set of rate coefficients.")
41 .def("contains", &gridfire::reaction::Reaction::contains,
42 py::arg("species"), "Check if the reaction contains a specific species.")
43 .def("contains_reactant", &gridfire::reaction::Reaction::contains_reactant,
44 "Check if the reaction contains a specific reactant species.")
45 .def("contains_product", &gridfire::reaction::Reaction::contains_product,
46 "Check if the reaction contains a specific product species.")
47 .def("all_species", &gridfire::reaction::Reaction::all_species,
48 "Get all species involved in the reaction (both reactants and products) as a set.")
49 .def("reactant_species", &gridfire::reaction::Reaction::reactant_species,
50 "Get the reactant species of the reaction as a set.")
51 .def("product_species", &gridfire::reaction::Reaction::product_species,
52 "Get the product species of the reaction as a set.")
53 .def("num_species", &gridfire::reaction::Reaction::num_species,
54 "Count the number of species in the reaction.")
55 .def("stoichiometry", static_cast<int (gridfire::reaction::Reaction::*)(const Species&) const>(&gridfire::reaction::Reaction::stoichiometry),
56 py::arg("species"),
57 "Get the stoichiometry of the reaction as a map from species to their coefficients.")
58 .def("stoichiometry", static_cast<std::unordered_map<Species, int> (gridfire::reaction::Reaction::*)() const>(&gridfire::reaction::Reaction::stoichiometry),
59 "Get the stoichiometry of the reaction as a map from species to their coefficients.")
60 .def("id", &gridfire::reaction::Reaction::id,
61 "Get the unique identifier of the reaction.")
62 .def("qValue", &gridfire::reaction::Reaction::qValue,
63 "Get the Q-value of the reaction in MeV.")
64 .def("reactants", &gridfire::reaction::Reaction::reactants,
65 "Get a list of reactant species in the reaction.")
66 .def("products", &gridfire::reaction::Reaction::products,
67 "Get a list of product species in the reaction.")
68 .def("is_reverse", &gridfire::reaction::Reaction::is_reverse,
69 "Check if this is a reverse reaction rate.")
70 .def("excess_energy", &gridfire::reaction::Reaction::excess_energy,
71 "Calculate the excess energy from the mass difference of reactants and products.")
72 .def("__eq__", &gridfire::reaction::Reaction::operator==,
73 "Equality operator for reactions based on their IDs.")
74 .def("__neq__", &gridfire::reaction::Reaction::operator!=,
75 "Inequality operator for reactions based on their IDs.")
76 .def("hash", &gridfire::reaction::Reaction::hash,
77 py::arg("seed") = 0,
78 "Compute a hash for the reaction based on its ID.")
79 .def("__repr__", [](const gridfire::reaction::Reaction& self) {
80 std::stringstream ss;
81 ss << self; // Use the existing operator<< for Reaction
82 return ss.str();
83 });
84
85 py::class_<gridfire::reaction::LogicalReaction, gridfire::reaction::Reaction>(m, "LogicalReaction")
86 .def(py::init<const std::vector<gridfire::reaction::Reaction>>(),
87 py::arg("reactions"),
88 "Construct a LogicalReaction from a vector of Reaction objects.")
89 .def("add_reaction", &gridfire::reaction::LogicalReaction::add_reaction,
90 py::arg("reaction"),
91 "Add another Reaction source to this logical reaction.")
92 .def("size", &gridfire::reaction::LogicalReaction::size,
93 "Get the number of source rates contributing to this logical reaction.")
94 .def("__len__", &gridfire::reaction::LogicalReaction::size,
95 "Overload len() to return the number of source rates.")
96 .def("sources", &gridfire::reaction::LogicalReaction::sources,
97 "Get the list of source labels for the aggregated rates.")
98 .def("calculate_rate", static_cast<double (gridfire::reaction::LogicalReaction::*)(double) const>(&gridfire::reaction::LogicalReaction::calculate_rate),
99 py::arg("T9"), "Calculate the reaction rate at a given temperature T9 (in units of 10^9 K).")
100 .def("calculate_forward_rate_log_derivative", &gridfire::reaction::LogicalReaction::calculate_forward_rate_log_derivative,
101 py::arg("T9"), "Calculate the forward rate log derivative at a given temperature T9 (in units of 10^9 K).");
102
103 py::class_<gridfire::reaction::LogicalReactionSet>(m, "LogicalReactionSet")
104 .def(py::init<const std::vector<gridfire::reaction::LogicalReaction>>(),
105 py::arg("reactions"),
106 "Construct a LogicalReactionSet from a vector of LogicalReaction objects.")
107 .def(py::init<>(),
108 "Default constructor for an empty LogicalReactionSet.")
109 .def(py::init<const gridfire::reaction::LogicalReactionSet&>(),
110 py::arg("other"),
111 "Copy constructor for LogicalReactionSet.")
112 .def("add_reaction", &gridfire::reaction::LogicalReactionSet::add_reaction,
113 py::arg("reaction"),
114 "Add a LogicalReaction to the set.")
115 .def("remove_reaction", &gridfire::reaction::LogicalReactionSet::remove_reaction,
116 py::arg("reaction"),
117 "Remove a LogicalReaction from the set.")
118 .def("contains", py::overload_cast<const std::string_view&>(&gridfire::reaction::LogicalReactionSet::contains, py::const_),
119 py::arg("id"),
120 "Check if the set contains a specific LogicalReaction.")
121 .def("contains", py::overload_cast<const gridfire::reaction::Reaction&>(&gridfire::reaction::LogicalReactionSet::contains, py::const_),
122 py::arg("reaction"),
123 "Check if the set contains a specific Reaction.")
124 .def("size", &gridfire::reaction::LogicalReactionSet::size,
125 "Get the number of LogicalReactions in the set.")
126 .def("__len__", &gridfire::reaction::LogicalReactionSet::size,
127 "Overload len() to return the number of LogicalReactions.")
128 .def("clear", &gridfire::reaction::LogicalReactionSet::clear,
129 "Remove all LogicalReactions from the set.")
130 .def("containes_species", &gridfire::reaction::LogicalReactionSet::contains_species,
131 py::arg("species"),
132 "Check if any reaction in the set involves the given species.")
133 .def("contains_reactant", &gridfire::reaction::LogicalReactionSet::contains_reactant,
134 py::arg("species"),
135 "Check if any reaction in the set has the species as a reactant.")
136 .def("contains_product", &gridfire::reaction::LogicalReactionSet::contains_product,
137 py::arg("species"),
138 "Check if any reaction in the set has the species as a product.")
139 .def("__getitem__", py::overload_cast<size_t>(&gridfire::reaction::LogicalReactionSet::operator[], py::const_),
140 py::arg("index"),
141 "Get a LogicalReaction by index.")
142 .def("__getitem___", py::overload_cast<const std::string_view&>(&gridfire::reaction::LogicalReactionSet::operator[], py::const_),
143 py::arg("id"),
144 "Get a LogicalReaction by its ID.")
145 .def("__eq__", &gridfire::reaction::LogicalReactionSet::operator==,
146 py::arg("LogicalReactionSet"),
147 "Equality operator for LogicalReactionSets based on their contents.")
148 .def("__ne__", &gridfire::reaction::LogicalReactionSet::operator!=,
149 py::arg("LogicalReactionSet"),
150 "Inequality operator for LogicalReactionSets based on their contents.")
151 .def("hash", &gridfire::reaction::LogicalReactionSet::hash,
152 py::arg("seed") = 0,
153 "Compute a hash for the LogicalReactionSet based on its contents."
154 )
155 .def("__repr__", [](const gridfire::reaction::LogicalReactionSet& self) {
156 std::stringstream ss;
157 ss << self;
158 return ss.str();
159 })
160 .def("getReactionSetSpecies", &gridfire::reaction::LogicalReactionSet::getReactionSetSpecies,
161 "Get all species involved in the reactions of the set as a set of Species objects.");
162
163 m.def("packReactionSetToLogicalReactionSet",
164 &gridfire::reaction::packReactionSetToLogicalReactionSet,
165 py::arg("reactionSet"),
166 "Convert a ReactionSet to a LogicalReactionSet by aggregating reactions with the same peName."
167 );
168
169 m.def("get_all_reactions", &gridfire::reaclib::get_all_reactions,
170 "Get all reactions from the REACLIB database.");
171}
gridfire::reaction::LogicalReaction
Represents a "logical" reaction that aggregates rates from multiple sources.
Definition reaction.h:310
gridfire::reaction::LogicalReaction::add_reaction
void add_reaction(const Reaction &reaction)
Adds another Reaction source to this logical reaction.
Definition reaction.cpp:192
gridfire::reaction::LogicalReaction::calculate_rate
double calculate_rate(const double T9) const override
Calculates the total reaction rate by summing all source rates.
Definition reaction.cpp:214
gridfire::reaction::LogicalReaction::calculate_forward_rate_log_derivative
virtual double calculate_forward_rate_log_derivative(const double T9) const override
Definition reaction.cpp:218
gridfire::reaction::LogicalReaction::sources
std::vector< std::string > sources() const
Gets the list of source labels for the aggregated rates.
Definition reaction.h:337
gridfire::reaction::LogicalReaction::size
size_t size() const
Gets the number of source rates contributing to this logical reaction.
Definition reaction.h:331
gridfire::reaction::Reaction
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
gridfire::reaction::Reaction::product_species
std::unordered_set< fourdst::atomic::Species > product_species() const
Gets a set of all unique product species.
Definition reaction.cpp:106
gridfire::reaction::Reaction::contains_product
bool contains_product(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a product.
Definition reaction.cpp:82
gridfire::reaction::Reaction::id
std::string_view id() const
Gets the unique identifier of the reaction.
Definition reaction.h:204
gridfire::reaction::Reaction::reactants
const std::vector< fourdst::atomic::Species > & reactants() const
Gets the vector of reactant species.
Definition reaction.h:216
gridfire::reaction::Reaction::num_species
size_t num_species() const
Gets the number of unique species involved in the reaction.
Definition reaction.cpp:129
gridfire::reaction::Reaction::sourceLabel
std::string_view sourceLabel() const
Gets the source label for the rate data.
Definition reaction.h:134
gridfire::reaction::Reaction::chapter
int chapter() const
Gets the REACLIB chapter number.
Definition reaction.h:128
gridfire::reaction::Reaction::products
const std::vector< fourdst::atomic::Species > & products() const
Gets the vector of product species.
Definition reaction.h:222
gridfire::reaction::Reaction::peName
virtual std::string_view peName() const
Gets the reaction name in (projectile, ejectile) notation.
Definition reaction.h:122
gridfire::reaction::Reaction::all_species
std::unordered_set< fourdst::atomic::Species > all_species() const
Gets a set of all unique species involved in the reaction.
Definition reaction.cpp:91
gridfire::reaction::Reaction::reactant_species
std::unordered_set< fourdst::atomic::Species > reactant_species() const
Gets a set of all unique reactant species.
Definition reaction.cpp:98
gridfire::reaction::Reaction::rateCoefficients
const RateCoefficientSet & rateCoefficients() const
Gets the set of rate coefficients.
Definition reaction.h:140
gridfire::reaction::Reaction::excess_energy
double excess_energy() const
Calculates the excess energy from the mass difference of reactants and products.
Definition reaction.cpp:144
gridfire::reaction::Reaction::is_reverse
bool is_reverse() const
Checks if this is a reverse reaction rate.
Definition reaction.h:228
gridfire::reaction::Reaction::stoichiometry
int stoichiometry(const fourdst::atomic::Species &species) const
Calculates the stoichiometric coefficient for a given species.
gridfire::reaction::Reaction::contains
bool contains(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a reactant or product.
Definition reaction.cpp:68
gridfire::reaction::Reaction::contains_reactant
bool contains_reactant(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a reactant.
Definition reaction.cpp:73
gridfire::reaction::Reaction::qValue
double qValue() const
Gets the Q-value of the reaction.
Definition reaction.h:210
gridfire::reaction::Reaction::calculate_rate
virtual double calculate_rate(const double T9) const
Calculates the reaction rate for a given temperature.
Definition reaction.cpp:39
gridfire::reaction::Reaction::hash
uint64_t hash(uint64_t seed=0) const
Computes a hash for the reaction based on its ID.
Definition reaction.cpp:157
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::clear
void clear()
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::getReactionSetSpecies
std::unordered_set< fourdst::atomic::Species > getReactionSetSpecies() const
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::hash
uint64_t hash(uint64_t seed=0) const
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::add_reaction
void add_reaction(LogicalReaction reaction)
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::contains_product
bool contains_product(const fourdst::atomic::Species &species) const
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::size
size_t size() const
Definition reaction.h:459
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::contains
bool contains(const std::string_view &id) const
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::remove_reaction
void remove_reaction(const LogicalReaction &reaction)
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::contains_reactant
bool contains_reactant(const fourdst::atomic::Species &species) const
gridfire::reaction::TemplatedReactionSet< LogicalReaction >::contains_species
bool contains_species(const fourdst::atomic::Species &species) const
gridfire::reaclib::get_all_reactions
const reaction::LogicalReactionSet & get_all_reactions()
Provides global access to the fully initialized REACLIB reaction set.
Definition reaclib.cpp:138
gridfire::reaction::LogicalReactionSet
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:563
gridfire::reaction::packReactionSetToLogicalReactionSet
LogicalReactionSet packReactionSetToLogicalReactionSet(const ReactionSet &reactionSet)
Definition reaction.cpp:273
reaclib.h
register_reaction_bindings
void register_reaction_bindings(py::module &m)
Definition bindings.cpp:16
bindings.h
reaction.h
Defines classes for representing and managing nuclear reactions.
gridfire::reaction::RateCoefficientSet
Holds the seven coefficients for the REACLIB rate equation.
Definition reaction.h:33