GridFire 0.6.0
General Purpose Nuclear Network
|
A reaction network engine that uses a graph-based representation. More...
#include <engine_graph.h>
Classes | |
class | AtomicReverseRate |
struct | constants |
struct | PrecomputedReaction |
Public Member Functions | |
GraphEngine (const fourdst::composition::Composition &composition, const BuildDepthType=NetworkBuildDepth::Full) | |
Constructs a GraphEngine from a composition. | |
GraphEngine (const fourdst::composition::Composition &composition, const partition::PartitionFunction &partitionFunction, const BuildDepthType buildDepth=NetworkBuildDepth::Full) | |
GraphEngine (const reaction::LogicalReactionSet &reactions) | |
Constructs a GraphEngine from a set of reactions. | |
std::expected< StepDerivatives< double >, expectations::StaleEngineError > | 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_dynamic, const double T9, const double rho) const override |
Generates the Jacobian matrix for the current state. | |
void | generateJacobianMatrix (const std::vector< double > &Y_dynamic, double T9, double rho, const SparsityPattern &sparsityPattern) const override |
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. | |
void | setNetworkReactions (const reaction::LogicalReactionSet &reactions) override |
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::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > | getSpeciesTimescales (const std::vector< double > &Y, double T9, double rho) const override |
Computes timescales for all species in the network. | |
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > | getSpeciesDestructionTimescales (const std::vector< double > &Y, double T9, double rho) const override |
fourdst::composition::Composition | update (const NetIn &netIn) override |
Update the internal state of the engine. | |
bool | isStale (const NetIn &netIn) override |
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 model) override |
Sets the electron screening model for reaction rate calculations. | |
screening::ScreeningType | getScreeningModel () const override |
Gets the current electron screening model. | |
void | setPrecomputation (bool precompute) |
Sets whether to precompute reaction rates. | |
bool | isPrecomputationEnabled () const |
Checks if precomputation of reaction rates is enabled. | |
const partition::PartitionFunction & | getPartitionFunction () const |
Gets the partition function used for reaction rate calculations. | |
double | calculateReverseRate (const reaction::Reaction &reaction, double T9) const |
Calculates the reverse rate for a given reaction. | |
double | calculateReverseRateTwoBody (const reaction::Reaction &reaction, const double T9, const double forwardRate, const double expFactor) const |
Calculates the reverse rate for a two-body reaction. | |
double | calculateReverseRateTwoBodyDerivative (const reaction::Reaction &reaction, const double T9, const double reverseRate) const |
bool | isUsingReverseReactions () const |
Checks if reverse reactions are enabled. | |
void | setUseReverseReactions (bool useReverse) |
Sets whether to use reverse reactions in the engine. | |
int | getSpeciesIndex (const fourdst::atomic::Species &species) const override |
Gets the index of a species in the network. | |
std::vector< double > | mapNetInToMolarAbundanceVector (const NetIn &netIn) const override |
Maps the NetIn object to a vector of molar abundances. | |
PrimingReport | primeEngine (const NetIn &netIn) override |
Prepares the engine for calculations with initial conditions. | |
BuildDepthType | getDepth () const override |
Gets the depth of the network. | |
void | rebuild (const fourdst::composition::Composition &comp, const BuildDepthType depth) override |
Rebuilds the reaction network based on a new composition. | |
![]() | |
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 () const |
Reserves space for the Jacobian matrix. | |
void | recordADTape () |
Records the AD tape for the right-hand side of the ODE. | |
void | collectAtomicReverseRateAtomicBases () |
void | precomputeNetwork () |
bool | validateConservation () const |
Validates mass and charge conservation across all reactions. | |
StepDerivatives< double > | calculateAllDerivativesUsingPrecomputation (const std::vector< double > &Y_in, const std::vector< double > &bare_rates, const std::vector< double > &bare_reverse_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> | |
T | calculateReverseMolarReactionFlow (T T9, T rho, std::vector< T > screeningFactors, std::vector< T > Y, size_t reactionIndex, const reaction::LogicalReaction &reaction) const |
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. | |
CppAD::sparse_jac_work | m_jac_work |
Work object for sparse Jacobian calculations. | |
CppAD::sparse_rc< std::vector< size_t > > | m_full_jacobian_sparsity_pattern |
Full sparsity pattern for the Jacobian matrix. | |
std::vector< std::unique_ptr< AtomicReverseRate > > | m_atomicReverseRates |
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. | |
bool | m_useReverseReactions = true |
Flag to enable or disable reverse reactions. If false, only forward reactions are considered. | |
BuildDepthType | m_depth |
std::vector< PrecomputedReaction > | m_precomputedReactions |
Precomputed reactions for efficiency. | |
std::unique_ptr< partition::PartitionFunction > | m_partitionFunction |
Partition function for the network. | |
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:
|
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.
|
explicit |
|
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.
|
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.
|
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.
|
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.
|
nodiscardprivate |
|
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.
|
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.
|
private |
|
nodiscard |
Calculates the reverse rate for a given reaction.
reaction | The reaction for which to calculate the reverse rate. |
T9 | Temperature in units of 10^9 K. |
This method computes the reverse rate based on the forward rate and thermodynamic properties of the reaction.
|
nodiscard |
Calculates the reverse rate for a two-body reaction.
reaction | The reaction for which to calculate the reverse rate. |
T9 | Temperature in units of 10^9 K. |
forwardRate | The forward rate of the reaction. |
expFactor | Exponential factor for the reaction. |
This method computes the reverse rate using the forward rate and thermodynamic properties of the reaction.
|
nodiscard |
|
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.
|
private |
|
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.
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:
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:
|
overridevirtual |
Generates the Jacobian matrix for the current state.
Y_dynamic | 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.
|
overridevirtual |
Reimplemented from gridfire::DynamicEngine.
|
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.
|
nodiscardoverridevirtual |
Gets the depth of the network.
This method returns the current build depth of the reaction network, which indicates how many levels of reactions are included in the network.
Reimplemented from gridfire::DynamicEngine.
|
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.
|
staticnodiscard |
Gets the net stoichiometry for a given reaction.
reaction | The reaction for which to get the stoichiometry. |
|
nodiscardoverridevirtual |
Gets the set of logical reactions in the network.
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Gets the list of species in the network.
Implements gridfire::Engine.
|
nodiscard |
Gets the partition function used for reaction rate calculations.
This method provides access to the partition function used in the engine, which is essential for calculating thermodynamic properties and reaction rates.
|
nodiscardoverridevirtual |
Gets the current electron screening model.
Example usage:
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Gets the index of a species in the network.
species | The species for which to get the index. |
This method returns the index of the given species in the network's species vector. If the species is not found, it returns -1.
Implements gridfire::DynamicEngine.
|
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.
|
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.
|
nodiscard |
Checks if a given species is involved in the network.
species | The species to check. |
|
nodiscard |
Checks if precomputation of reaction rates is enabled.
This method allows checking the current state of precomputation for reaction rates in the engine.
|
overridevirtual |
Implements gridfire::DynamicEngine.
|
nodiscard |
Checks if reverse reactions are enabled.
This method allows checking whether the engine is configured to use reverse reactions in its calculations.
|
nodiscardoverridevirtual |
Maps the NetIn object to a vector of molar abundances.
netIn | The NetIn object containing the input conditions. |
This method converts the NetIn object into a vector of molar abundances for each species in the network, which can be used for further calculations.
Implements gridfire::DynamicEngine.
|
private |
Populates the reaction ID map.
This method populates the reaction ID map, which maps reaction IDs to REACLIBReaction objects.
|
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.
|
private |
|
nodiscardoverridevirtual |
Prepares the engine for calculations with initial conditions.
netIn | The input conditions for the network. |
This method initializes the engine with the provided input conditions, setting up reactions, species, and precomputing necessary data.
Implements gridfire::DynamicEngine.
|
overridevirtual |
Rebuilds the reaction network based on a new composition.
comp | The new composition to use for rebuilding the network. |
depth | The build depth to use for the network. |
This method rebuilds the reaction network using the provided composition and build depth. It updates all internal data structures accordingly.
Reimplemented from gridfire::DynamicEngine.
|
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. |
|
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.
|
overridevirtual |
Implements gridfire::DynamicEngine.
void gridfire::GraphEngine::setPrecomputation | ( | bool | precompute | ) |
Sets whether to precompute reaction rates.
precompute | True to enable precomputation, false to disable. |
This method allows enabling or disabling precomputation of reaction rates for performance optimization. When enabled, reaction rates are computed once and stored for later use.
|
overridevirtual |
Sets the electron screening model for reaction rate calculations.
model | The type of screening model to use. |
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.
void gridfire::GraphEngine::setUseReverseReactions | ( | bool | useReverse | ) |
Sets whether to use reverse reactions in the engine.
useReverse | True to enable reverse reactions, false to disable. |
This method allows enabling or disabling reverse reactions in the engine. If disabled, only forward reactions will be considered in calculations.
|
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.
|
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.
|
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.
|
private |
|
private |
|
private |
|
private |
|
private |
Full sparsity pattern for the Jacobian matrix.
|
mutableprivate |
Work object for sparse Jacobian calculations.
|
mutableprivate |
Jacobian matrix (species x species).
|
private |
|
private |
Vector of unique species in the network.
|
private |
Map from species name to Species object.
|
private |
Partition function for the network.
|
private |
Precomputed reactions for efficiency.
|
private |
Map from reaction ID to REACLIBReaction. //PERF: This makes copies of REACLIBReaction and could be a performance bottleneck.
|
private |
Set of REACLIB reactions in the network.
|
mutableprivate |
CppAD function for the right-hand side of the ODE.
|
private |
|
private |
Screening type for the reaction network. Default to no screening.
|
private |
Map from species to their index in the stoichiometry matrix.
|
private |
Stoichiometry matrix (species x reactions).
|
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.
|
private |
Flag to enable or disable reverse reactions. If false, only forward reactions are considered.