GridFire 0.0.1a
General Purpose Nuclear Network
Loading...
Searching...
No Matches
gridfire::GraphEngine Class Referencefinal

A reaction network engine that uses a graph-based representation. More...

#include <engine_graph.h>

Inheritance diagram for gridfire::GraphEngine:
gridfire::DynamicEngine gridfire::Engine

Public Member Functions

 GraphEngine (const fourdst::composition::Composition &composition)
 Constructs a GraphEngine from a composition.
 
 GraphEngine (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::LogicalReactionSetgetNetworkReactions () 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.
 
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.
 
- Public Member Functions inherited from gridfire::Engine
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.
 
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.
 
template<IsArithmeticOrAD 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< ADDoublecalculateAllDerivatives (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

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.
 
Config & m_config = Config::getInstance()
 
Constants & m_constants = Constants::getInstance()
 Access to physical constants.
 
quill::Logger * m_logger = LogManager::getInstance().getLogger("log")
 

Detailed Description

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:

  • Calculation of the right-hand side (dY/dt) and energy generation rate.
  • Generation and access to the Jacobian matrix.
  • Generation and access to the stoichiometry matrix.
  • Calculation of molar reaction flows.
  • Access to the set of logical reactions in the network.
  • Computation of timescales for each species.
  • Exporting the network to DOT and CSV formats for visualization and analysis.
See also
engine_abstract.h

Definition at line 86 of file engine_graph.h.

Constructor & Destructor Documentation

◆ GraphEngine() [1/2]

gridfire::GraphEngine::GraphEngine ( const fourdst::composition::Composition & composition)
explicit

Constructs a GraphEngine from a composition.

Parameters
compositionThe composition of the material.

This constructor builds the reaction network from the given composition using the build_reaclib_nuclear_network function.

See also
build_reaclib_nuclear_network

Definition at line 25 of file engine_graph.cpp.

◆ GraphEngine() [2/2]

gridfire::GraphEngine::GraphEngine ( reaction::LogicalReactionSet reactions)
explicit

Constructs a GraphEngine from a set of reactions.

Parameters
reactionsThe set of reactions to use in the network.

This constructor uses the given set of reactions to construct the reaction network.

Definition at line 32 of file engine_graph.cpp.

Member Function Documentation

◆ calculateAllDerivatives() [1/3]

StepDerivatives< ADDouble > gridfire::GraphEngine::calculateAllDerivatives ( const std::vector< ADDouble > & Y_in,
const ADDouble & T9,
const ADDouble & rho ) const
private

Calculates all derivatives (dY/dt) and the energy generation rate (automatic differentiation).

Parameters
Y_inVector of current abundances for all species.
T9Temperature in units of 10^9 K.
rhoDensity in g/cm^3.
Returns
StepDerivatives<ADDouble> containing dY/dt and energy generation rate.

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 257 of file engine_graph.cpp.

◆ calculateAllDerivatives() [2/3]

StepDerivatives< double > gridfire::GraphEngine::calculateAllDerivatives ( const std::vector< double > & Y_in,
const double T9,
const double rho ) const
private

Calculates all derivatives (dY/dt) and the energy generation rate (double precision).

Parameters
Y_inVector of current abundances for all species.
T9Temperature in units of 10^9 K.
rhoDensity in g/cm^3.
Returns
StepDerivatives<double> containing dY/dt and energy generation rate.

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 249 of file engine_graph.cpp.

◆ calculateAllDerivatives() [3/3]

template<IsArithmeticOrAD T>
StepDerivatives< T > gridfire::GraphEngine::calculateAllDerivatives ( const std::vector< T > & Y_in,
T T9,
T rho ) const
private

Calculates all derivatives (dY/dt) and the energy generation rate.

Template Parameters
TThe numeric type to use for the calculation.
Parameters
Y_inVector of current abundances for all species.
T9Temperature in units of 10^9 K.
rhoDensity in g/cm^3.
Returns
StepDerivatives<T> containing dY/dt and energy generation rate.

This method calculates the time derivatives of all species and the specific nuclear energy generation rate for the current state.

Definition at line 475 of file engine_graph.h.

◆ calculateMolarReactionFlow() [1/2]

double gridfire::GraphEngine::calculateMolarReactionFlow ( const reaction::Reaction & reaction,
const std::vector< double > & Y,
const double T9,
const double rho ) const
overridevirtual

Calculates the molar reaction flow for a given reaction.

Parameters
reactionThe reaction for which to calculate the flow.
YVector of current abundances.
T9Temperature in units of 10^9 K.
rhoDensity in g/cm^3.
Returns
Molar flow rate for the reaction (e.g., mol/g/s).

This method computes the net rate at which the given reaction proceeds under the current state.

Implements gridfire::DynamicEngine.

Definition at line 265 of file engine_graph.cpp.

◆ calculateMolarReactionFlow() [2/2]

template<IsArithmeticOrAD T>
T gridfire::GraphEngine::calculateMolarReactionFlow ( const reaction::Reaction & reaction,
const std::vector< T > & Y,
const T T9,
const T rho ) const
private

Calculates the molar reaction flow for a given reaction.

Template Parameters
TThe numeric type to use for the calculation.
Parameters
reactionThe reaction for which to calculate the flow.
YVector of current abundances.
T9Temperature in units of 10^9 K.
rhoDensity in g/cm^3.
Returns
Molar flow rate for the reaction (e.g., mol/g/s).

This method computes the net rate at which the given reaction proceeds under the current state.

Definition at line 536 of file engine_graph.h.

◆ calculateRHSAndEnergy()

StepDerivatives< double > gridfire::GraphEngine::calculateRHSAndEnergy ( const std::vector< double > & Y,
const double T9,
const double rho ) const
overridevirtual

Calculates the right-hand side (dY/dt) and energy generation rate.

Parameters
YVector of current abundances for all species.
T9Temperature in units of 10^9 K.
rhoDensity in g/cm^3.
Returns
StepDerivatives<double> containing dY/dt and energy generation rate.

This method calculates the time derivatives of all species and the specific nuclear energy generation rate for the current state.

See also
StepDerivatives

Implements gridfire::Engine.

Definition at line 37 of file engine_graph.cpp.

◆ collectNetworkSpecies()

void gridfire::GraphEngine::collectNetworkSpecies ( )
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 56 of file engine_graph.cpp.

◆ exportToCSV()

void gridfire::GraphEngine::exportToCSV ( const std::string & filename) const

Exports the network to a CSV file for analysis.

Parameters
filenameThe 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.

Exceptions
std::runtime_errorIf the file cannot be opened for writing.

Example usage:

engine.exportToCSV("network.csv");

Definition at line 372 of file engine_graph.cpp.

◆ exportToDot()

void gridfire::GraphEngine::exportToDot ( const std::string & filename) const

Exports the network to a DOT file for visualization.

Parameters
filenameThe 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.

Exceptions
std::runtime_errorIf the file cannot be opened for writing.

Example usage:

engine.exportToDot("network.dot");

Definition at line 324 of file engine_graph.cpp.

◆ generateJacobianMatrix()

void gridfire::GraphEngine::generateJacobianMatrix ( const std::vector< double > & Y,
const double T9,
const double rho )
overridevirtual

Generates the Jacobian matrix for the current state.

Parameters
YVector of current abundances.
T9Temperature in units of 10^9 K.
rhoDensity 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().

See also
getJacobianMatrixEntry()

Implements gridfire::DynamicEngine.

Definition at line 274 of file engine_graph.cpp.

◆ generateStoichiometryMatrix()

void gridfire::GraphEngine::generateStoichiometryMatrix ( )
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 208 of file engine_graph.cpp.

◆ getJacobianMatrixEntry()

double gridfire::GraphEngine::getJacobianMatrixEntry ( const int i,
const int j ) const
nodiscardoverridevirtual

Gets an entry from the previously generated Jacobian matrix.

Parameters
iRow index (species index).
jColumn index (species index).
Returns
Value of the Jacobian matrix at (i, j).

The Jacobian must have been generated by generateJacobianMatrix() before calling this.

See also
generateJacobianMatrix()

Implements gridfire::DynamicEngine.

Definition at line 307 of file engine_graph.cpp.

◆ getNetReactionStoichiometry()

std::unordered_map< fourdst::atomic::Species, int > gridfire::GraphEngine::getNetReactionStoichiometry ( const reaction::Reaction & reaction)
staticnodiscard

Gets the net stoichiometry for a given reaction.

Parameters
reactionThe reaction for which to get the stoichiometry.
Returns
Map of species to their stoichiometric coefficients.

Definition at line 311 of file engine_graph.cpp.

◆ getNetworkReactions()

const reaction::LogicalReactionSet & gridfire::GraphEngine::getNetworkReactions ( ) const
nodiscardoverridevirtual

Gets the set of logical reactions in the network.

Returns
Reference to the LogicalReactionSet containing all reactions.

Implements gridfire::DynamicEngine.

Definition at line 118 of file engine_graph.cpp.

◆ getNetworkSpecies()

const std::vector< fourdst::atomic::Species > & gridfire::GraphEngine::getNetworkSpecies ( ) const
nodiscardoverridevirtual

Gets the list of species in the network.

Returns
Vector of Species objects representing all network species.

Implements gridfire::Engine.

Definition at line 112 of file engine_graph.cpp.

◆ getSpeciesTimescales()

std::unordered_map< fourdst::atomic::Species, double > gridfire::GraphEngine::getSpeciesTimescales ( const std::vector< double > & Y,
double T9,
double rho ) const
nodiscardoverridevirtual

Computes timescales for all species in the network.

Parameters
YVector of current abundances.
T9Temperature in units of 10^9 K.
rhoDensity in g/cm^3.
Returns
Map from Species to their characteristic timescales (s).

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 426 of file engine_graph.cpp.

◆ getStoichiometryMatrixEntry()

int gridfire::GraphEngine::getStoichiometryMatrixEntry ( const int speciesIndex,
const int reactionIndex ) const
nodiscardoverridevirtual

Gets an entry from the stoichiometry matrix.

Parameters
speciesIndexIndex of the species.
reactionIndexIndex of the reaction.
Returns
Stoichiometric coefficient for the species in the reaction.

The stoichiometry matrix must have been generated by generateStoichiometryMatrix().

See also
generateStoichiometryMatrix()

Implements gridfire::DynamicEngine.

Definition at line 317 of file engine_graph.cpp.

◆ involvesSpecies()

bool gridfire::GraphEngine::involvesSpecies ( const fourdst::atomic::Species & species) const
nodiscard

Checks if a given species is involved in the network.

Parameters
speciesThe species to check.
Returns
True if the species is involved in the network, false otherwise.

Definition at line 124 of file engine_graph.cpp.

◆ populateReactionIDMap()

void gridfire::GraphEngine::populateReactionIDMap ( )
private

Populates the reaction ID map.

This method populates the reaction ID map, which maps reaction IDs to REACLIBReaction objects.

Definition at line 85 of file engine_graph.cpp.

◆ populateSpeciesToIndexMap()

void gridfire::GraphEngine::populateSpeciesToIndexMap ( )
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 94 of file engine_graph.cpp.

◆ recordADTape()

void gridfire::GraphEngine::recordADTape ( )
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.

Exceptions
std::runtime_errorIf there are no species in the network.

Definition at line 442 of file engine_graph.cpp.

◆ reserveJacobianMatrix()

void gridfire::GraphEngine::reserveJacobianMatrix ( )
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 101 of file engine_graph.cpp.

◆ syncInternalMaps()

void gridfire::GraphEngine::syncInternalMaps ( )
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 46 of file engine_graph.cpp.

◆ validateComposition()

void gridfire::GraphEngine::validateComposition ( const fourdst::composition::Composition & composition,
double culling,
double T9 )
private

Validates the composition against the current reaction set.

Parameters
compositionThe composition to validate.
cullingThe culling threshold to use.
T9The 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 187 of file engine_graph.cpp.

◆ validateConservation()

bool gridfire::GraphEngine::validateConservation ( ) const
nodiscardprivate

Validates mass and charge conservation across all reactions.

Returns
True if all reactions conserve mass and charge, false otherwise.

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 132 of file engine_graph.cpp.

Member Data Documentation

◆ m_config

Config& gridfire::GraphEngine::m_config = Config::getInstance()
private

Definition at line 310 of file engine_graph.h.

◆ m_constants

Constants& gridfire::GraphEngine::m_constants = Constants::getInstance()
private

Access to physical constants.

Definition at line 311 of file engine_graph.h.

◆ m_jacobianMatrix

boost::numeric::ublas::compressed_matrix<double> gridfire::GraphEngine::m_jacobianMatrix
private

Jacobian matrix (species x species).

Definition at line 306 of file engine_graph.h.

◆ m_logger

quill::Logger* gridfire::GraphEngine::m_logger = LogManager::getInstance().getLogger("log")
private

Definition at line 312 of file engine_graph.h.

◆ m_networkSpecies

std::vector<fourdst::atomic::Species> gridfire::GraphEngine::m_networkSpecies
private

Vector of unique species in the network.

Definition at line 301 of file engine_graph.h.

◆ m_networkSpeciesMap

std::unordered_map<std::string_view, fourdst::atomic::Species> gridfire::GraphEngine::m_networkSpeciesMap
private

Map from species name to Species object.

Definition at line 302 of file engine_graph.h.

◆ m_reactionIDMap

std::unordered_map<std::string_view, reaction::Reaction*> gridfire::GraphEngine::m_reactionIDMap
private

Map from reaction ID to REACLIBReaction. //PERF: This makes copies of REACLIBReaction and could be a performance bottleneck.

Definition at line 299 of file engine_graph.h.

◆ m_reactions

reaction::LogicalReactionSet gridfire::GraphEngine::m_reactions
private

Set of REACLIB reactions in the network.

Definition at line 298 of file engine_graph.h.

◆ m_rhsADFun

CppAD::ADFun<double> gridfire::GraphEngine::m_rhsADFun
private

CppAD function for the right-hand side of the ODE.

Definition at line 308 of file engine_graph.h.

◆ m_speciesToIndexMap

std::unordered_map<fourdst::atomic::Species, size_t> gridfire::GraphEngine::m_speciesToIndexMap
private

Map from species to their index in the stoichiometry matrix.

Definition at line 303 of file engine_graph.h.

◆ m_stoichiometryMatrix

boost::numeric::ublas::compressed_matrix<int> gridfire::GraphEngine::m_stoichiometryMatrix
private

Stoichiometry matrix (species x reactions).

Definition at line 305 of file engine_graph.h.


The documentation for this class was generated from the following files: