GridFire 0.0.1a General Purpose Nuclear Network

Loading...
Searching...
No Matches
py_engine.cpp
Go to the documentation of this file.
1#include "py_engine.h"
2
3#include "gridfire/engine/engine.h"
4
5#include <pybind11/pybind11.h>
6#include <pybind11/stl.h>
7#include <pybind11/functional.h> // Needed for std::function
8
9#include <expected>
10#include <unordered_map>
11#include <vector>
12
13
14 namespace py = pybind11;
15
16 const std::vector<fourdst::atomic::Species>& PyEngine::getNetworkSpecies() const {
17 /*
18 * Acquire the GIL (Global Interpreter Lock) for thread safety
19 * with the Python interpreter.
20 */
21 py::gil_scoped_acquire gil;
22
23 /*
24 * get_override() looks for a Python method that overrides this C++ one.
25 */
26 py::function override = py::get_override(this, "getNetworkSpecies");
27
28 if (override) {
29 py::object result = override();
30 m_species_cache = result.cast<std::vector<fourdst::atomic::Species>>();
31 return m_species_cache;
32 }
33
34 py::pybind11_fail("Tried to call pure virtual function \"DynamicEngine::getNetworkSpecies\"");
35}
36
37std::expected<gridfire::StepDerivatives<double>, gridfire::expectations::StaleEngineError> PyEngine::calculateRHSAndEnergy(const std::vector<double> &Y, double T9, double rho) const {
38 PYBIND11_OVERRIDE_PURE(
39 PYBIND11_TYPE(std::expected<gridfire::StepDerivatives<double>, gridfire::expectations::StaleEngineError>),
40 gridfire::Engine,
41 calculateRHSAndEnergy,
42 Y, T9, rho
43 );
44}
45
49
50 const std::vector<fourdst::atomic::Species>& PyDynamicEngine::getNetworkSpecies() const {
51 /*
52 * Acquire the GIL (Global Interpreter Lock) for thread safety
53 * with the Python interpreter.
54 */
55 py::gil_scoped_acquire gil;
56
57 /*
58 * get_override() looks for a Python method that overrides this C++ one.
59 */
60 py::function override = py::get_override(this, "getNetworkSpecies");
61
62 if (override) {
63 py::object result = override();
64 m_species_cache = result.cast<std::vector<fourdst::atomic::Species>>();
65 return m_species_cache;
66 }
67
68 py::pybind11_fail("Tried to call pure virtual function \"DynamicEngine::getNetworkSpecies\"");
69}
70std::expected<gridfire::StepDerivatives<double>, gridfire::expectations::StaleEngineError> PyDynamicEngine::calculateRHSAndEnergy(const std::vector<double> &Y, double T9, double rho) const {
71 PYBIND11_OVERRIDE_PURE(
72 PYBIND11_TYPE(std::expected<gridfire::StepDerivatives<double>, gridfire::expectations::StaleEngineError>),
73 gridfire::Engine,
74 calculateRHSAndEnergy,
75 Y, T9, rho
76 );
77}
78
79 void PyDynamicEngine::generateJacobianMatrix(const std::vector<double> &Y_dynamic, double T9, double rho) const {
80 PYBIND11_OVERRIDE_PURE(
81 void,
82 gridfire::DynamicEngine,
83 generateJacobianMatrix,
84 Y_dynamic, T9, rho
85 );
86}
87
88 void PyDynamicEngine::generateJacobianMatrix(const std::vector<double> &Y_dynamic, double T9, double rho, const gridfire::SparsityPattern &sparsityPattern) const {
89 PYBIND11_OVERRIDE_PURE(
90 void,
91 gridfire::DynamicEngine,
92 generateJacobianMatrix,
93 Y_dynamic, T9, rho, sparsityPattern
94 );
95}
96
97 double PyDynamicEngine::getJacobianMatrixEntry(int i, int j) const {
98 PYBIND11_OVERRIDE_PURE(
99 double,
100 gridfire::DynamicEngine,
101 getJacobianMatrixEntry,
102 i, j
103 );
104}
105
106 void PyDynamicEngine::generateStoichiometryMatrix() {
107 PYBIND11_OVERRIDE_PURE(
108 void,
109 gridfire::DynamicEngine,
110 generateStoichiometryMatrix
111 );
112}
113
114 int PyDynamicEngine::getStoichiometryMatrixEntry(int speciesIndex, int reactionIndex) const {
115 PYBIND11_OVERRIDE_PURE(
116 int,
117 gridfire::DynamicEngine,
118 getStoichiometryMatrixEntry,
119 speciesIndex, reactionIndex
120 );
121}
122
123 double PyDynamicEngine::calculateMolarReactionFlow(const gridfire::reaction::Reaction &reaction, const std::vector<double> &Y, double T9, double rho) const {
124 PYBIND11_OVERRIDE_PURE(
125 double,
126 gridfire::DynamicEngine,
127 calculateMolarReactionFlow,
128 reaction, Y, T9, rho
129 );
130}
131
132 const gridfire::reaction::LogicalReactionSet& PyDynamicEngine::getNetworkReactions() const {
133 PYBIND11_OVERRIDE_PURE(
134 const gridfire::reaction::LogicalReactionSet&,
135 gridfire::DynamicEngine,
136 getNetworkReactions
137 );
138}
139
140 void PyDynamicEngine::setNetworkReactions(const gridfire::reaction::LogicalReactionSet& reactions) {
141 PYBIND11_OVERRIDE_PURE(
142 void,
143 gridfire::DynamicEngine,
144 setNetworkReactions,
145 reactions
146 );
147}
148
149std::expected<std::unordered_map<fourdst::atomic::Species, double>, gridfire::expectations::StaleEngineError> PyDynamicEngine::getSpeciesTimescales(const std::vector<double> &Y, double T9, double rho) const {
150 PYBIND11_OVERRIDE_PURE(
151 PYBIND11_TYPE(std::expected<std::unordered_map<fourdst::atomic::Species, double>, gridfire::expectations::StaleEngineError>),
152 gridfire::DynamicEngine,
153 getSpeciesTimescales,
154 Y, T9, rho
155 );
156}
157
158std::expected<std::unordered_map<fourdst::atomic::Species, double>, gridfire::expectations::StaleEngineError> PyDynamicEngine::getSpeciesDestructionTimescales(const std::vector<double> &Y, double T9, double rho) const {
159 PYBIND11_OVERRIDE_PURE(
160 PYBIND11_TYPE(std::expected<std::unordered_map<fourdst::atomic::Species, double>, gridfire::expectations::StaleEngineError>),
161 gridfire::DynamicEngine,
162 getSpeciesDestructionTimescales,
163 Y, T9, rho
164 );
165}
166
167fourdst::composition::Composition PyDynamicEngine::update(const gridfire::NetIn &netIn) {
168 PYBIND11_OVERRIDE_PURE(
169 fourdst::composition::Composition,
170 gridfire::DynamicEngine,
171 update,
172 netIn
173 );
174}
175
176 bool PyDynamicEngine::isStale(const gridfire::NetIn &netIn) {
177 PYBIND11_OVERRIDE_PURE(
178 bool,
179 gridfire::DynamicEngine,
180 isStale,
181 netIn
182 );
183}
184
185 void PyDynamicEngine::setScreeningModel(gridfire::screening::ScreeningType model) {
186 PYBIND11_OVERRIDE_PURE(
187 void,
188 gridfire::DynamicEngine,
189 setScreeningModel,
190 model
191 );
192}
193
194 gridfire::screening::ScreeningType PyDynamicEngine::getScreeningModel() const {
195 PYBIND11_OVERRIDE_PURE(
196 gridfire::screening::ScreeningType,
197 gridfire::DynamicEngine,
198 getScreeningModel
199 );
200}
201
202 int PyDynamicEngine::getSpeciesIndex(const fourdst::atomic::Species &species) const {
203 PYBIND11_OVERRIDE_PURE(
204 int,
205 gridfire::DynamicEngine,
206 getSpeciesIndex,
207 species
208 );
209}
210
211std::vector<double> PyDynamicEngine::mapNetInToMolarAbundanceVector(const gridfire::NetIn &netIn) const {
212 PYBIND11_OVERRIDE_PURE(
213 std::vector<double>,
214 gridfire::DynamicEngine,
215 mapNetInToMolarAbundanceVector,
216 netIn
217 );
218}
219
220 gridfire::PrimingReport PyDynamicEngine::primeEngine(const gridfire::NetIn &netIn) {
221 PYBIND11_OVERRIDE_PURE(
222 gridfire::PrimingReport,
223 gridfire::DynamicEngine,
224 primeEngine,
225 netIn
226 );
227}
228
229 const gridfire::Engine& PyEngineView::getBaseEngine() const {
230 PYBIND11_OVERRIDE_PURE(
231 const gridfire::Engine&,
232 gridfire::EngineView<gridfire::Engine>,
233 getBaseEngine
234 );
235}
236
237 const gridfire::DynamicEngine& PyDynamicEngineView::getBaseEngine() const {
238 PYBIND11_OVERRIDE_PURE(
239 const gridfire::DynamicEngine&,
240 gridfire::EngineView<gridfire::DynamicEngine>,
241 getBaseEngine
242 );
243}
244
245
PyDynamicEngine::getSpeciesDestructionTimescales
std::expected< std::unordered_map< fourdst::atomic::Species, double >, gridfire::expectations::StaleEngineError > getSpeciesDestructionTimescales(const std::vector< double > &Y, double T9, double rho) const override
Definition py_engine.cpp:158
PyDynamicEngine::getSpeciesTimescales
std::expected< std::unordered_map< fourdst::atomic::Species, double >, gridfire::expectations::StaleEngineError > getSpeciesTimescales(const std::vector< double > &Y, double T9, double rho) const override
Compute timescales for all species in the network.
Definition py_engine.cpp:149
PyDynamicEngine::getJacobianMatrixEntry
double getJacobianMatrixEntry(int i, int j) const override
Get an entry from the previously generated Jacobian matrix.
Definition py_engine.cpp:97
PyDynamicEngine::generateStoichiometryMatrix
void generateStoichiometryMatrix() override
Generate the stoichiometry matrix for the network.
Definition py_engine.cpp:106
PyDynamicEngine::m_species_cache
std::vector< fourdst::atomic::Species > m_species_cache
Definition py_engine.h:48
PyDynamicEngine::getSpeciesIndex
int getSpeciesIndex(const fourdst::atomic::Species &species) const override
Definition py_engine.cpp:202
PyDynamicEngine::isStale
bool isStale(const gridfire::NetIn &netIn) override
Definition py_engine.cpp:176
PyDynamicEngine::getNetworkReactions
const gridfire::reaction::LogicalReactionSet & getNetworkReactions() const override
Get the set of logical reactions in the network.
Definition py_engine.cpp:132
PyDynamicEngine::calculateRHSAndEnergy
std::expected< gridfire::StepDerivatives< double >, gridfire::expectations::StaleEngineError > calculateRHSAndEnergy(const std::vector< double > &Y, double T9, double rho) const override
Calculate the right-hand side (dY/dt) and energy generation.
Definition py_engine.cpp:70
PyDynamicEngine::generateJacobianMatrix
void generateJacobianMatrix(const std::vector< double > &Y_dynamic, double T9, double rho) const override
Generate the Jacobian matrix for the current state.
Definition py_engine.cpp:79
PyDynamicEngine::mapNetInToMolarAbundanceVector
std::vector< double > mapNetInToMolarAbundanceVector(const gridfire::NetIn &netIn) const override
Definition py_engine.cpp:211
PyDynamicEngine::calculateMolarReactionFlow
double calculateMolarReactionFlow(const gridfire::reaction::Reaction &reaction, const std::vector< double > &Y, double T9, double rho) const override
Calculate the molar reaction flow for a given reaction.
Definition py_engine.cpp:123
PyDynamicEngine::getStoichiometryMatrixEntry
int getStoichiometryMatrixEntry(int speciesIndex, int reactionIndex) const override
Get an entry from the stoichiometry matrix.
Definition py_engine.cpp:114
PyDynamicEngine::getScreeningModel
gridfire::screening::ScreeningType getScreeningModel() const override
Get the current electron screening model.
Definition py_engine.cpp:194
PyDynamicEngine::primeEngine
gridfire::PrimingReport primeEngine(const gridfire::NetIn &netIn) override
Definition py_engine.cpp:220
PyDynamicEngine::update
fourdst::composition::Composition update(const gridfire::NetIn &netIn) override
Update the internal state of the engine.
Definition py_engine.cpp:167
PyDynamicEngine::setScreeningModel
void setScreeningModel(gridfire::screening::ScreeningType model) override
Set the electron screening model.
Definition py_engine.cpp:185
PyDynamicEngine::getNetworkSpecies
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
PyDynamicEngine Implementation ///.
Definition py_engine.cpp:50
PyDynamicEngine::setNetworkReactions
void setNetworkReactions(const gridfire::reaction::LogicalReactionSet &reactions) override
Definition py_engine.cpp:140
PyDynamicEngineView::getBaseEngine
const gridfire::DynamicEngine & getBaseEngine() const override
Access the underlying engine instance.
Definition py_engine.cpp:237
PyEngine::getNetworkSpecies
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Get the list of species in the network.
Definition py_engine.cpp:16
PyEngine::calculateRHSAndEnergy
std::expected< gridfire::StepDerivatives< double >, gridfire::expectations::StaleEngineError > calculateRHSAndEnergy(const std::vector< double > &Y, double T9, double rho) const override
Calculate the right-hand side (dY/dt) and energy generation.
Definition py_engine.cpp:37
PyEngine::m_species_cache
std::vector< fourdst::atomic::Species > m_species_cache
Definition py_engine.h:17
PyEngineView::getBaseEngine
const gridfire::Engine & getBaseEngine() const override
Access the underlying engine instance.
Definition py_engine.cpp:229
gridfire::DynamicEngine
Abstract class for engines supporting Jacobian and stoichiometry operations.
Definition engine_abstract.h:130
gridfire::Engine
Abstract base class for a reaction network engine.
Definition engine_abstract.h:84
gridfire::EngineView
Abstract base class for a "view" of a reaction network engine.
Definition engine_view_abstract.h:74
gridfire::reaction::Reaction
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
engine.h
Core header for the GridFire reaction network engine module.
gridfire::reaction::LogicalReactionSet
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:563
gridfire::screening::ScreeningType
ScreeningType
Enumerates the available plasma screening models.
Definition screening_types.h:15
gridfire::SparsityPattern
std::vector< std::pair< size_t, size_t > > SparsityPattern
Definition engine_abstract.h:65
py_engine.h
gridfire::NetIn
Definition network.h:53
gridfire::PrimingReport
Captures the result of a network priming operation.
Definition reporting.h:67
gridfire::StepDerivatives
Structure holding derivatives and energy generation for a network step.
Definition engine_abstract.h:60
gridfire::expectations::StaleEngineError
Definition expected_engine.h:44