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
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
14namespace py = pybind11;
15
16const 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(
42 Y, T9, rho
43 );
44}
45
49
50const 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(
75 Y, T9, rho
76 );
77}
78
79void PyDynamicEngine::generateJacobianMatrix(const std::vector<double> &Y_dynamic, double T9, double rho) const {
80 PYBIND11_OVERRIDE_PURE(
81 void,
84 Y_dynamic, T9, rho
85 );
86}
87
88void PyDynamicEngine::generateJacobianMatrix(const std::vector<double> &Y_dynamic, double T9, double rho, const gridfire::SparsityPattern &sparsityPattern) const {
89 PYBIND11_OVERRIDE_PURE(
90 void,
93 Y_dynamic, T9, rho, sparsityPattern
94 );
95}
96
97double PyDynamicEngine::getJacobianMatrixEntry(int i, int j) const {
98 PYBIND11_OVERRIDE_PURE(
99 double,
102 i, j
103 );
104}
105
107 PYBIND11_OVERRIDE_PURE(
108 void,
111 );
112}
113
114int PyDynamicEngine::getStoichiometryMatrixEntry(int speciesIndex, int reactionIndex) const {
115 PYBIND11_OVERRIDE_PURE(
116 int,
119 speciesIndex, reactionIndex
120 );
121}
122
123double PyDynamicEngine::calculateMolarReactionFlow(const gridfire::reaction::Reaction &reaction, const std::vector<double> &Y, double T9, double rho) const {
124 PYBIND11_OVERRIDE_PURE(
125 double,
128 reaction, Y, T9, rho
129 );
130}
131
139
141 PYBIND11_OVERRIDE_PURE(
142 void,
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>),
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>),
163 Y, T9, rho
164 );
165}
166
167fourdst::composition::Composition PyDynamicEngine::update(const gridfire::NetIn &netIn) {
168 PYBIND11_OVERRIDE_PURE(
169 fourdst::composition::Composition,
171 update,
172 netIn
173 );
174}
175
177 PYBIND11_OVERRIDE_PURE(
178 bool,
180 isStale,
181 netIn
182 );
183}
184
186 PYBIND11_OVERRIDE_PURE(
187 void,
190 model
191 );
192}
193
201
202int PyDynamicEngine::getSpeciesIndex(const fourdst::atomic::Species &species) const {
203 PYBIND11_OVERRIDE_PURE(
204 int,
207 species
208 );
209}
210
212 PYBIND11_OVERRIDE_PURE(
213 std::vector<double>,
216 netIn
217 );
218}
219
221 PYBIND11_OVERRIDE_PURE(
225 netIn
226 );
227}
228
230 PYBIND11_OVERRIDE_PURE(
231 const gridfire::Engine&,
234 );
235}
236
244
245
std::expected< std::unordered_map< fourdst::atomic::Species, double >, gridfire::expectations::StaleEngineError > getSpeciesDestructionTimescales(const std::vector< double > &Y, double T9, double rho) const override
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.
double getJacobianMatrixEntry(int i, int j) const override
Get an entry from the previously generated Jacobian matrix.
Definition py_engine.cpp:97
void generateStoichiometryMatrix() override
Generate the stoichiometry matrix for the network.
std::vector< fourdst::atomic::Species > m_species_cache
Definition py_engine.h:48
int getSpeciesIndex(const fourdst::atomic::Species &species) const override
bool isStale(const gridfire::NetIn &netIn) override
const gridfire::reaction::LogicalReactionSet & getNetworkReactions() const override
Get the set of logical reactions in the network.
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
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
std::vector< double > mapNetInToMolarAbundanceVector(const gridfire::NetIn &netIn) const override
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.
int getStoichiometryMatrixEntry(int speciesIndex, int reactionIndex) const override
Get an entry from the stoichiometry matrix.
gridfire::screening::ScreeningType getScreeningModel() const override
Get the current electron screening model.
gridfire::PrimingReport primeEngine(const gridfire::NetIn &netIn) override
fourdst::composition::Composition update(const gridfire::NetIn &netIn) override
Update the internal state of the engine.
void setScreeningModel(gridfire::screening::ScreeningType model) override
Set the electron screening model.
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
PyDynamicEngine Implementation ///.
Definition py_engine.cpp:50
void setNetworkReactions(const gridfire::reaction::LogicalReactionSet &reactions) override
const gridfire::DynamicEngine & getBaseEngine() const override
Access the underlying engine instance.
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Get the list of species in the network.
Definition py_engine.cpp:16
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
std::vector< fourdst::atomic::Species > m_species_cache
Definition py_engine.h:17
const gridfire::Engine & getBaseEngine() const override
Access the underlying engine instance.
Abstract class for engines supporting Jacobian and stoichiometry operations.
Abstract base class for a reaction network engine.
Abstract base class for a "view" of a reaction network engine.
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
Core header for the GridFire reaction network engine module.
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:563
ScreeningType
Enumerates the available plasma screening models.
std::vector< std::pair< size_t, size_t > > SparsityPattern
Captures the result of a network priming operation.
Definition reporting.h:67
Structure holding derivatives and energy generation for a network step.