Loading...
Searching...
No Matches
py_engine.cpp
Go to the documentation of this file.
#include "py_engine.h"
#include "gridfire/engine/engine.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h> // Needed for std::function
#include <expected>
#include <unordered_map>
#include <vector>
namespace py = pybind11;
const std::vector<fourdst::atomic::Species>& PyEngine::getNetworkSpecies() const {
/*
* Acquire the GIL (Global Interpreter Lock) for thread safety
* with the Python interpreter.
*/
py::gil_scoped_acquire gil;
/*
* get_override() looks for a Python method that overrides this C++ one.
*/
py::function override = py::get_override(this, "getNetworkSpecies");
if (override) {
py::object result = override();
m_species_cache = result.cast<std::vector<fourdst::atomic::Species>>();
return m_species_cache;
}
py::pybind11_fail("Tried to call pure virtual function \"DynamicEngine::getNetworkSpecies\"");
}
std::expected<gridfire::StepDerivatives<double>, gridfire::expectations::StaleEngineError> PyEngine::calculateRHSAndEnergy(const std::vector<double> &Y, double T9, double rho) const {
PYBIND11_OVERRIDE_PURE(
PYBIND11_TYPE(std::expected<gridfire::StepDerivatives<double>, gridfire::expectations::StaleEngineError>),
gridfire::Engine,
calculateRHSAndEnergy,
Y, T9, rho
);
}
const std::vector<fourdst::atomic::Species>& PyDynamicEngine::getNetworkSpecies() const {
/*
* Acquire the GIL (Global Interpreter Lock) for thread safety
* with the Python interpreter.
*/
py::gil_scoped_acquire gil;
/*
* get_override() looks for a Python method that overrides this C++ one.
*/
py::function override = py::get_override(this, "getNetworkSpecies");
if (override) {
py::object result = override();
m_species_cache = result.cast<std::vector<fourdst::atomic::Species>>();
return m_species_cache;
}
py::pybind11_fail("Tried to call pure virtual function \"DynamicEngine::getNetworkSpecies\"");
}
std::expected<gridfire::StepDerivatives<double>, gridfire::expectations::StaleEngineError> PyDynamicEngine::calculateRHSAndEnergy(const std::vector<double> &Y, double T9, double rho) const {
PYBIND11_OVERRIDE_PURE(
PYBIND11_TYPE(std::expected<gridfire::StepDerivatives<double>, gridfire::expectations::StaleEngineError>),
gridfire::Engine,
calculateRHSAndEnergy,
Y, T9, rho
);
}
void PyDynamicEngine::generateJacobianMatrix(const std::vector<double> &Y_dynamic, double T9, double rho) const {
PYBIND11_OVERRIDE_PURE(
void,
gridfire::DynamicEngine,
generateJacobianMatrix,
Y_dynamic, T9, rho
);
}
void PyDynamicEngine::generateJacobianMatrix(const std::vector<double> &Y_dynamic, double T9, double rho, const gridfire::SparsityPattern &sparsityPattern) const {
PYBIND11_OVERRIDE_PURE(
void,
gridfire::DynamicEngine,
generateJacobianMatrix,
Y_dynamic, T9, rho, sparsityPattern
);
}
double PyDynamicEngine::getJacobianMatrixEntry(int i, int j) const {
PYBIND11_OVERRIDE_PURE(
double,
gridfire::DynamicEngine,
getJacobianMatrixEntry,
i, j
);
}
void PyDynamicEngine::generateStoichiometryMatrix() {
PYBIND11_OVERRIDE_PURE(
void,
gridfire::DynamicEngine,
generateStoichiometryMatrix
);
}
int PyDynamicEngine::getStoichiometryMatrixEntry(int speciesIndex, int reactionIndex) const {
PYBIND11_OVERRIDE_PURE(
int,
gridfire::DynamicEngine,
getStoichiometryMatrixEntry,
speciesIndex, reactionIndex
);
}
double PyDynamicEngine::calculateMolarReactionFlow(const gridfire::reaction::Reaction &reaction, const std::vector<double> &Y, double T9, double rho) const {
PYBIND11_OVERRIDE_PURE(
double,
gridfire::DynamicEngine,
calculateMolarReactionFlow,
reaction, Y, T9, rho
);
}
const gridfire::reaction::LogicalReactionSet& PyDynamicEngine::getNetworkReactions() const {
PYBIND11_OVERRIDE_PURE(
const gridfire::reaction::LogicalReactionSet&,
gridfire::DynamicEngine,
getNetworkReactions
);
}
void PyDynamicEngine::setNetworkReactions(const gridfire::reaction::LogicalReactionSet& reactions) {
PYBIND11_OVERRIDE_PURE(
void,
gridfire::DynamicEngine,
setNetworkReactions,
reactions
);
}
std::expected<std::unordered_map<fourdst::atomic::Species, double>, gridfire::expectations::StaleEngineError> PyDynamicEngine::getSpeciesTimescales(const std::vector<double> &Y, double T9, double rho) const {
PYBIND11_OVERRIDE_PURE(
PYBIND11_TYPE(std::expected<std::unordered_map<fourdst::atomic::Species, double>, gridfire::expectations::StaleEngineError>),
gridfire::DynamicEngine,
getSpeciesTimescales,
Y, T9, rho
);
}
std::expected<std::unordered_map<fourdst::atomic::Species, double>, gridfire::expectations::StaleEngineError> PyDynamicEngine::getSpeciesDestructionTimescales(const std::vector<double> &Y, double T9, double rho) const {
PYBIND11_OVERRIDE_PURE(
PYBIND11_TYPE(std::expected<std::unordered_map<fourdst::atomic::Species, double>, gridfire::expectations::StaleEngineError>),
gridfire::DynamicEngine,
getSpeciesDestructionTimescales,
Y, T9, rho
);
}
fourdst::composition::Composition PyDynamicEngine::update(const gridfire::NetIn &netIn) {
PYBIND11_OVERRIDE_PURE(
fourdst::composition::Composition,
gridfire::DynamicEngine,
update,
netIn
);
}
bool PyDynamicEngine::isStale(const gridfire::NetIn &netIn) {
PYBIND11_OVERRIDE_PURE(
bool,
gridfire::DynamicEngine,
isStale,
netIn
);
}
void PyDynamicEngine::setScreeningModel(gridfire::screening::ScreeningType model) {
PYBIND11_OVERRIDE_PURE(
void,
gridfire::DynamicEngine,
setScreeningModel,
model
);
}
gridfire::screening::ScreeningType PyDynamicEngine::getScreeningModel() const {
PYBIND11_OVERRIDE_PURE(
gridfire::screening::ScreeningType,
gridfire::DynamicEngine,
getScreeningModel
);
}
int PyDynamicEngine::getSpeciesIndex(const fourdst::atomic::Species &species) const {
PYBIND11_OVERRIDE_PURE(
int,
gridfire::DynamicEngine,
getSpeciesIndex,
species
);
}
std::vector<double> PyDynamicEngine::mapNetInToMolarAbundanceVector(const gridfire::NetIn &netIn) const {
PYBIND11_OVERRIDE_PURE(
std::vector<double>,
gridfire::DynamicEngine,
mapNetInToMolarAbundanceVector,
netIn
);
}
gridfire::PrimingReport PyDynamicEngine::primeEngine(const gridfire::NetIn &netIn) {
PYBIND11_OVERRIDE_PURE(
gridfire::PrimingReport,
gridfire::DynamicEngine,
primeEngine,
netIn
);
}
const gridfire::Engine& PyEngineView::getBaseEngine() const {
PYBIND11_OVERRIDE_PURE(
const gridfire::Engine&,
gridfire::EngineView<gridfire::Engine>,
getBaseEngine
);
}
const gridfire::DynamicEngine& PyDynamicEngineView::getBaseEngine() const {
PYBIND11_OVERRIDE_PURE(
const gridfire::DynamicEngine&,
gridfire::EngineView<gridfire::DynamicEngine>,
getBaseEngine
);
}
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