GridFire 0.0.1a
General Purpose Nuclear Network
|
A reaction network engine that uses a graph-based representation. More...
#include <engine_graph.h>
Classes | |
struct | constants |
struct | PrecomputedReaction |
Public Member Functions | |
GraphEngine (const fourdst::composition::Composition &composition) | |
Constructs a GraphEngine from a composition. | |
GraphEngine (const reaction::LogicalReactionSet &reactions) | |
Constructs a GraphEngine from a set of reactions. | |
StepDerivatives< double > | calculateRHSAndEnergy (const std::vector< double > &Y, const double T9, const double rho) const override |
Calculates the right-hand side (dY/dt) and energy generation rate. | |
void | generateJacobianMatrix (const std::vector< double > &Y, const double T9, const double rho) override |
Generates the Jacobian matrix for the current state. | |
void | generateStoichiometryMatrix () override |
Generates the stoichiometry matrix for the network. | |
double | calculateMolarReactionFlow (const reaction::Reaction &reaction, const std::vector< double > &Y, const double T9, const double rho) const override |
Calculates the molar reaction flow for a given reaction. | |
const std::vector< fourdst::atomic::Species > & | getNetworkSpecies () const override |
Gets the list of species in the network. | |
const reaction::LogicalReactionSet & | getNetworkReactions () const override |
Gets the set of logical reactions in the network. | |
double | getJacobianMatrixEntry (const int i, const int j) const override |
Gets an entry from the previously generated Jacobian matrix. | |
int | getStoichiometryMatrixEntry (const int speciesIndex, const int reactionIndex) const override |
Gets an entry from the stoichiometry matrix. | |
std::unordered_map< fourdst::atomic::Species, double > | getSpeciesTimescales (const std::vector< double > &Y, double T9, double rho) const override |
Computes timescales for all species in the network. | |
void | update (const NetIn &netIn) override |
Update the internal state of the engine. | |
bool | involvesSpecies (const fourdst::atomic::Species &species) const |
Checks if a given species is involved in the network. | |
void | exportToDot (const std::string &filename) const |
Exports the network to a DOT file for visualization. | |
void | exportToCSV (const std::string &filename) const |
Exports the network to a CSV file for analysis. | |
void | setScreeningModel (screening::ScreeningType) override |
Set the electron screening model. | |
screening::ScreeningType | getScreeningModel () const override |
Get the current electron screening model. | |
void | setPrecomputation (bool precompute) |
bool | isPrecomputationEnabled () const |
![]() | |
virtual | ~Engine ()=default |
Virtual destructor. | |
Static Public Member Functions | |
static std::unordered_map< fourdst::atomic::Species, int > | getNetReactionStoichiometry (const reaction::Reaction &reaction) |
Gets the net stoichiometry for a given reaction. | |
Private Member Functions | |
void | syncInternalMaps () |
Synchronizes the internal maps. | |
void | collectNetworkSpecies () |
Collects the unique species in the network. | |
void | populateReactionIDMap () |
Populates the reaction ID map. | |
void | populateSpeciesToIndexMap () |
Populates the species-to-index map. | |
void | reserveJacobianMatrix () |
Reserves space for the Jacobian matrix. | |
void | recordADTape () |
Records the AD tape for the right-hand side of the ODE. | |
void | precomputeNetwork () |
bool | validateConservation () const |
Validates mass and charge conservation across all reactions. | |
void | validateComposition (const fourdst::composition::Composition &composition, double culling, double T9) |
Validates the composition against the current reaction set. | |
StepDerivatives< double > | calculateAllDerivativesUsingPrecomputation (const std::vector< double > &Y_in, const std::vector< double > &bare_rates, double T9, double rho) const |
template<IsArithmeticOrAD T> | |
T | calculateMolarReactionFlow (const reaction::Reaction &reaction, const std::vector< T > &Y, const T T9, const T rho) const |
Calculates the molar reaction flow for a given reaction. | |
template<IsArithmeticOrAD T> | |
StepDerivatives< T > | calculateAllDerivatives (const std::vector< T > &Y_in, T T9, T rho) const |
Calculates all derivatives (dY/dt) and the energy generation rate. | |
StepDerivatives< double > | calculateAllDerivatives (const std::vector< double > &Y_in, const double T9, const double rho) const |
Calculates all derivatives (dY/dt) and the energy generation rate (double precision). | |
StepDerivatives< ADDouble > | calculateAllDerivatives (const std::vector< ADDouble > &Y_in, const ADDouble &T9, const ADDouble &rho) const |
Calculates all derivatives (dY/dt) and the energy generation rate (automatic differentiation). | |
Private Attributes | |
Config & | m_config = Config::getInstance() |
quill::Logger * | m_logger = LogManager::getInstance().getLogger("log") |
constants | m_constants |
reaction::LogicalReactionSet | m_reactions |
Set of REACLIB reactions in the network. | |
std::unordered_map< std::string_view, reaction::Reaction * > | m_reactionIDMap |
Map from reaction ID to REACLIBReaction. //PERF: This makes copies of REACLIBReaction and could be a performance bottleneck. | |
std::vector< fourdst::atomic::Species > | m_networkSpecies |
Vector of unique species in the network. | |
std::unordered_map< std::string_view, fourdst::atomic::Species > | m_networkSpeciesMap |
Map from species name to Species object. | |
std::unordered_map< fourdst::atomic::Species, size_t > | m_speciesToIndexMap |
Map from species to their index in the stoichiometry matrix. | |
boost::numeric::ublas::compressed_matrix< int > | m_stoichiometryMatrix |
Stoichiometry matrix (species x reactions). | |
boost::numeric::ublas::compressed_matrix< double > | m_jacobianMatrix |
Jacobian matrix (species x species). | |
CppAD::ADFun< double > | m_rhsADFun |
CppAD function for the right-hand side of the ODE. | |
screening::ScreeningType | m_screeningType = screening::ScreeningType::BARE |
Screening type for the reaction network. Default to no screening. | |
std::unique_ptr< screening::ScreeningModel > | m_screeningModel = screening::selectScreeningModel(m_screeningType) |
bool | m_usePrecomputation = true |
Flag to enable or disable using precomputed reactions for efficiency. Mathematically, this should not change the results. Generally end users should not need to change this. | |
std::vector< PrecomputedReaction > | m_precomputedReactions |
Precomputed reactions for efficiency. | |
A reaction network engine that uses a graph-based representation.
The GraphEngine class implements the DynamicEngine interface using a graph-based representation of the reaction network. It uses sparse matrices for efficient storage and computation of the stoichiometry and Jacobian matrices. Automatic differentiation (AD) is used to calculate the Jacobian matrix.
The engine supports:
Definition at line 90 of file engine_graph.h.
|
explicit |
Constructs a GraphEngine from a composition.
composition | The composition of the material. |
This constructor builds the reaction network from the given composition using the build_reaclib_nuclear_network
function.
Definition at line 26 of file engine_graph.cpp.
|
explicit |
Constructs a GraphEngine from a set of reactions.
reactions | The set of reactions to use in the network. |
This constructor uses the given set of reactions to construct the reaction network.
Definition at line 34 of file engine_graph.cpp.
|
nodiscardprivate |
Calculates all derivatives (dY/dt) and the energy generation rate (automatic differentiation).
Y_in | Vector of current abundances for all species. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method calculates the time derivatives of all species and the specific nuclear energy generation rate for the current state using automatic differentiation.
Definition at line 352 of file engine_graph.cpp.
|
nodiscardprivate |
Calculates all derivatives (dY/dt) and the energy generation rate (double precision).
Y_in | Vector of current abundances for all species. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method calculates the time derivatives of all species and the specific nuclear energy generation rate for the current state using double precision arithmetic.
Definition at line 344 of file engine_graph.cpp.
|
nodiscardprivate |
Calculates all derivatives (dY/dt) and the energy generation rate.
T | The numeric type to use for the calculation. |
Y_in | Vector of current abundances for all species. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method calculates the time derivatives of all species and the specific nuclear energy generation rate for the current state.
Definition at line 521 of file engine_graph.h.
|
nodiscardprivate |
Definition at line 223 of file engine_graph.cpp.
|
nodiscardoverridevirtual |
Calculates the molar reaction flow for a given reaction.
reaction | The reaction for which to calculate the flow. |
Y | Vector of current abundances. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method computes the net rate at which the given reaction proceeds under the current state.
Implements gridfire::DynamicEngine.
Definition at line 377 of file engine_graph.cpp.
|
private |
Calculates the molar reaction flow for a given reaction.
T | The numeric type to use for the calculation. |
reaction | The reaction for which to calculate the flow. |
Y | Vector of current abundances. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method computes the net rate at which the given reaction proceeds under the current state.
Definition at line 589 of file engine_graph.h.
|
nodiscardoverridevirtual |
Calculates the right-hand side (dY/dt) and energy generation rate.
Y | Vector of current abundances for all species. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method calculates the time derivatives of all species and the specific nuclear energy generation rate for the current state.
Implements gridfire::Engine.
Definition at line 42 of file engine_graph.cpp.
|
private |
Collects the unique species in the network.
This method collects the unique species in the network from the reactants and products of all reactions.
Definition at line 72 of file engine_graph.cpp.
void gridfire::GraphEngine::exportToCSV | ( | const std::string & | filename | ) | const |
Exports the network to a CSV file for analysis.
filename | The name of the CSV file to create. |
This method generates a CSV file containing information about the reactions in the network, including the reactants, products, Q-value, and reaction rate coefficients.
std::runtime_error | If the file cannot be opened for writing. |
Example usage:
Definition at line 484 of file engine_graph.cpp.
void gridfire::GraphEngine::exportToDot | ( | const std::string & | filename | ) | const |
Exports the network to a DOT file for visualization.
filename | The name of the DOT file to create. |
This method generates a DOT file that can be used to visualize the reaction network as a graph. The DOT file can be converted to a graphical image using Graphviz.
std::runtime_error | If the file cannot be opened for writing. |
Example usage:
Definition at line 436 of file engine_graph.cpp.
|
overridevirtual |
Generates the Jacobian matrix for the current state.
Y | Vector of current abundances. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method computes and stores the Jacobian matrix (∂(dY/dt)_i/∂Y_j) for the current state using automatic differentiation. The matrix can then be accessed via getJacobianMatrixEntry()
.
Implements gridfire::DynamicEngine.
Definition at line 386 of file engine_graph.cpp.
|
overridevirtual |
Generates the stoichiometry matrix for the network.
This method computes and stores the stoichiometry matrix, which encodes the net change of each species in each reaction.
Implements gridfire::DynamicEngine.
Definition at line 303 of file engine_graph.cpp.
|
nodiscardoverridevirtual |
Gets an entry from the previously generated Jacobian matrix.
i | Row index (species index). |
j | Column index (species index). |
The Jacobian must have been generated by generateJacobianMatrix()
before calling this.
Implements gridfire::DynamicEngine.
Definition at line 419 of file engine_graph.cpp.
|
staticnodiscard |
Gets the net stoichiometry for a given reaction.
reaction | The reaction for which to get the stoichiometry. |
Definition at line 423 of file engine_graph.cpp.
|
nodiscardoverridevirtual |
Gets the set of logical reactions in the network.
Implements gridfire::DynamicEngine.
Definition at line 134 of file engine_graph.cpp.
|
nodiscardoverridevirtual |
Gets the list of species in the network.
Implements gridfire::Engine.
Definition at line 128 of file engine_graph.cpp.
|
nodiscardoverridevirtual |
Get the current electron screening model.
Implements gridfire::DynamicEngine.
Definition at line 365 of file engine_graph.cpp.
|
nodiscardoverridevirtual |
Computes timescales for all species in the network.
Y | Vector of current abundances. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method estimates the timescale for abundance change of each species, which can be used for timestep control or diagnostics.
Implements gridfire::DynamicEngine.
Definition at line 538 of file engine_graph.cpp.
|
nodiscardoverridevirtual |
Gets an entry from the stoichiometry matrix.
speciesIndex | Index of the species. |
reactionIndex | Index of the reaction. |
The stoichiometry matrix must have been generated by generateStoichiometryMatrix()
.
Implements gridfire::DynamicEngine.
Definition at line 429 of file engine_graph.cpp.
|
nodiscard |
Checks if a given species is involved in the network.
species | The species to check. |
Definition at line 140 of file engine_graph.cpp.
|
nodiscard |
Definition at line 373 of file engine_graph.cpp.
|
private |
Populates the reaction ID map.
This method populates the reaction ID map, which maps reaction IDs to REACLIBReaction objects.
Definition at line 101 of file engine_graph.cpp.
|
private |
Populates the species-to-index map.
This method populates the species-to-index map, which maps species to their index in the stoichiometry matrix.
Definition at line 110 of file engine_graph.cpp.
|
private |
Definition at line 603 of file engine_graph.cpp.
|
private |
Records the AD tape for the right-hand side of the ODE.
This method records the AD tape for the right-hand side of the ODE, which is used to calculate the Jacobian matrix using automatic differentiation.
std::runtime_error | If there are no species in the network. |
Definition at line 558 of file engine_graph.cpp.
|
private |
Reserves space for the Jacobian matrix.
This method reserves space for the Jacobian matrix, which is used to store the partial derivatives of the right-hand side of the ODE with respect to the species abundances.
Definition at line 117 of file engine_graph.cpp.
void gridfire::GraphEngine::setPrecomputation | ( | bool | precompute | ) |
Definition at line 369 of file engine_graph.cpp.
|
overridevirtual |
Set the electron screening model.
model | The type of screening model to use for reaction rate calculations. |
This method allows changing the screening model at runtime. Screening corrections account for the electrostatic shielding of nuclei by electrons, which affects reaction rates in dense stellar plasmas.
Implements gridfire::DynamicEngine.
Definition at line 360 of file engine_graph.cpp.
|
private |
Synchronizes the internal maps.
This method synchronizes the internal maps used by the engine, including the species map, reaction ID map, and species-to-index map. It also generates the stoichiometry matrix and records the AD tape.
Definition at line 62 of file engine_graph.cpp.
|
overridevirtual |
Update the internal state of the engine.
netIn | A struct containing the current network input, such as temperature, density, and composition. |
This method is intended to be implemented by derived classes to update their internal state based on the provided network conditions. For example, an adaptive engine might use this to re-evaluate which reactions and species are active. For other engines that do not support manually updating, this method might do nothing.
Implements gridfire::DynamicEngine.
Definition at line 554 of file engine_graph.cpp.
|
private |
Validates the composition against the current reaction set.
composition | The composition to validate. |
culling | The culling threshold to use. |
T9 | The temperature to use. |
This method validates the composition against the current reaction set. If the composition is not compatible with the reaction set, the reaction set is rebuilt from the composition.
Definition at line 203 of file engine_graph.cpp.
|
nodiscardprivate |
Validates mass and charge conservation across all reactions.
This method checks that all reactions in the network conserve mass and charge. If any reaction does not conserve mass or charge, an error message is logged and false is returned.
Definition at line 148 of file engine_graph.cpp.
|
private |
Definition at line 327 of file engine_graph.h.
|
private |
Definition at line 330 of file engine_graph.h.
|
private |
Jacobian matrix (species x species).
Definition at line 340 of file engine_graph.h.
|
private |
Definition at line 328 of file engine_graph.h.
|
private |
Vector of unique species in the network.
Definition at line 335 of file engine_graph.h.
|
private |
Map from species name to Species object.
Definition at line 336 of file engine_graph.h.
|
private |
Precomputed reactions for efficiency.
Definition at line 349 of file engine_graph.h.
|
private |
Map from reaction ID to REACLIBReaction. //PERF: This makes copies of REACLIBReaction and could be a performance bottleneck.
Definition at line 333 of file engine_graph.h.
|
private |
Set of REACLIB reactions in the network.
Definition at line 332 of file engine_graph.h.
|
private |
CppAD function for the right-hand side of the ODE.
Definition at line 342 of file engine_graph.h.
|
private |
Definition at line 345 of file engine_graph.h.
|
private |
Screening type for the reaction network. Default to no screening.
Definition at line 344 of file engine_graph.h.
|
private |
Map from species to their index in the stoichiometry matrix.
Definition at line 337 of file engine_graph.h.
|
private |
Stoichiometry matrix (species x reactions).
Definition at line 339 of file engine_graph.h.
|
private |
Flag to enable or disable using precomputed reactions for efficiency. Mathematically, this should not change the results. Generally end users should not need to change this.
Definition at line 347 of file engine_graph.h.