Loading...
Searching...
No Matches
engine_graph.h
Go to the documentation of this file.
#pragma once
#include "fourdst/composition/atomicSpecies.h"
#include "fourdst/composition/composition.h"
#include "fourdst/logging/logging.h"
#include "fourdst/config/config.h"
#include "gridfire/network.h"
#include "gridfire/reaction/reaction.h"
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/screening/screening_abstract.h"
#include "gridfire/screening/screening_types.h"
#include "gridfire/partition/partition_abstract.h"
#include "gridfire/engine/procedures/construction.h"
#include <string>
#include <unordered_map>
#include <vector>
#include <memory>
#include <ranges>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include "cppad/cppad.hpp"
#include "cppad/utility/sparse_rc.hpp"
#include "cppad/speed/sparse_jac_fun.hpp"
#include "procedures/priming.h"
#include "quill/LogMacros.h"
// PERF: The function getNetReactionStoichiometry returns a map of species to their stoichiometric coefficients for a given reaction.
// this makes extra copies of the species, which is not ideal and could be optimized further.
// Even more relevant is the member m_reactionIDMap which makes copies of a REACLIBReaction for each reaction ID.
// REACLIBReactions are quite large data structures, so this could be a performance bottleneck.
// static bool isF17 = false;
namespace gridfire {
typedef CppAD::AD<double> ADDouble;
using fourdst::config::Config;
using fourdst::logging::LogManager;
using fourdst::constant::Constants;
static constexpr double MIN_DENSITY_THRESHOLD = 1e-18;
static constexpr double MIN_ABUNDANCE_THRESHOLD = 1e-18;
static constexpr double MIN_JACOBIAN_THRESHOLD = 1e-24;
class GraphEngine final : public DynamicEngine{
public:
explicit GraphEngine(
const fourdst::composition::Composition &composition,
const BuildDepthType = NetworkBuildDepth::Full
);
explicit GraphEngine(
const fourdst::composition::Composition &composition,
const partition::PartitionFunction& partitionFunction,
const BuildDepthType buildDepth = NetworkBuildDepth::Full
);
explicit GraphEngine(const reaction::LogicalReactionSet &reactions);
[[nodiscard]] std::expected<StepDerivatives<double>, expectations::StaleEngineError> calculateRHSAndEnergy(
const std::vector<double>& Y,
const double T9,
const double rho
) const override;
void generateJacobianMatrix(
const std::vector<double>& Y_dynamic,
const double T9,
const double rho
) const override;
void generateJacobianMatrix(
const std::vector<double> &Y_dynamic,
double T9,
double rho,
const SparsityPattern &sparsityPattern
) const override;
void generateStoichiometryMatrix() override;
[[nodiscard]] double calculateMolarReactionFlow(
const reaction::Reaction& reaction,
const std::vector<double>&Y,
const double T9,
const double rho
) const override;
[[nodiscard]] const std::vector<fourdst::atomic::Species>& getNetworkSpecies() const override;
[[nodiscard]] const reaction::LogicalReactionSet& getNetworkReactions() const override;
void setNetworkReactions(const reaction::LogicalReactionSet& reactions) override;
[[nodiscard]] double getJacobianMatrixEntry(
const int i,
const int j
) const override;
[[nodiscard]] static std::unordered_map<fourdst::atomic::Species, int> getNetReactionStoichiometry(
const reaction::Reaction& reaction
);
[[nodiscard]] int getStoichiometryMatrixEntry(
const int speciesIndex,
const int reactionIndex
) const override;
[[nodiscard]] std::expected<std::unordered_map<fourdst::atomic::Species, double>, expectations::StaleEngineError> getSpeciesTimescales(
const std::vector<double>& Y,
double T9,
double rho
) const override;
[[nodiscard]] 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;
bool isStale(const NetIn &netIn) override;
[[nodiscard]] bool involvesSpecies(
const fourdst::atomic::Species& species
) const;
void exportToDot(
const std::string& filename
) const;
void exportToCSV(
const std::string& filename
) const;
void setScreeningModel(screening::ScreeningType) override;
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
void setPrecomputation(bool precompute);
[[nodiscard]] bool isPrecomputationEnabled() const;
[[nodiscard]] const partition::PartitionFunction& getPartitionFunction() const;
[[nodiscard]] double calculateReverseRate(
const reaction::Reaction &reaction,
double T9
) const;
[[nodiscard]] double calculateReverseRateTwoBody(
const reaction::Reaction &reaction,
const double T9,
const double forwardRate,
const double expFactor
) const;
[[nodiscard]] double calculateReverseRateTwoBodyDerivative(
const reaction::Reaction &reaction,
const double T9,
const double reverseRate
) const;
[[nodiscard]] bool isUsingReverseReactions() const;
void setUseReverseReactions(bool useReverse);
[[nodiscard]] int getSpeciesIndex(
const fourdst::atomic::Species& species
) const override;
[[nodiscard]] std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const override;
[[nodiscard]] PrimingReport primeEngine(const NetIn &netIn) override;
[[nodiscard]] BuildDepthType getDepth() const override;
void rebuild(const fourdst::composition::Composition& comp, const BuildDepthType depth) override;
private:
struct PrecomputedReaction {
// Forward cacheing
size_t reaction_index;
std::vector<size_t> unique_reactant_indices;
std::vector<int> reactant_powers;
double symmetry_factor;
std::vector<size_t> affected_species_indices;
std::vector<int> stoichiometric_coefficients;
// Reverse cacheing
std::vector<size_t> unique_product_indices;
std::vector<int> product_powers;
double reverse_symmetry_factor;
};
struct constants {
const double u = Constants::getInstance().get("u").value;
const double Na = Constants::getInstance().get("N_a").value;
const double c = Constants::getInstance().get("c").value;
const double kB = Constants::getInstance().get("kB").value;
};
private:
class AtomicReverseRate final : public CppAD::atomic_base<double> {
public:
AtomicReverseRate(
const reaction::Reaction& reaction,
const GraphEngine& engine
):
atomic_base<double>("AtomicReverseRate"),
m_reaction(reaction),
m_engine(engine) {}
bool forward(
size_t p,
size_t q,
const CppAD::vector<bool>& vx,
CppAD::vector<bool>& vy,
const CppAD::vector<double>& tx,
CppAD::vector<double>& ty
) override;
bool reverse(
size_t q,
const CppAD::vector<double>& tx,
const CppAD::vector<double>& ty,
CppAD::vector<double>& px,
const CppAD::vector<double>& py
) override;
bool for_sparse_jac(
size_t q,
const CppAD::vector<std::set<size_t>>&r,
CppAD::vector<std::set<size_t>>& s
) override;
bool rev_sparse_jac(
size_t q,
const CppAD::vector<std::set<size_t>>&rt,
CppAD::vector<std::set<size_t>>& st
) override;
private:
const reaction::Reaction& m_reaction;
const GraphEngine& m_engine;
};
private:
Config& m_config = Config::getInstance();
quill::Logger* m_logger = LogManager::getInstance().getLogger("log");
constants m_constants;
reaction::LogicalReactionSet m_reactions;
std::unordered_map<std::string_view, reaction::Reaction*> m_reactionIDMap;
std::vector<fourdst::atomic::Species> m_networkSpecies;
std::unordered_map<std::string_view, fourdst::atomic::Species> m_networkSpeciesMap;
std::unordered_map<fourdst::atomic::Species, size_t> m_speciesToIndexMap;
boost::numeric::ublas::compressed_matrix<int> m_stoichiometryMatrix;
mutable boost::numeric::ublas::compressed_matrix<double> m_jacobianMatrix;
mutable CppAD::ADFun<double> m_rhsADFun;
mutable CppAD::sparse_jac_work m_jac_work;
CppAD::sparse_rc<std::vector<size_t>> m_full_jacobian_sparsity_pattern;
std::vector<std::unique_ptr<AtomicReverseRate>> m_atomicReverseRates;
screening::ScreeningType m_screeningType = screening::ScreeningType::BARE;
std::unique_ptr<screening::ScreeningModel> m_screeningModel = screening::selectScreeningModel(m_screeningType);
bool m_usePrecomputation = true;
bool m_useReverseReactions = true;
BuildDepthType m_depth;
std::vector<PrecomputedReaction> m_precomputedReactions;
std::unique_ptr<partition::PartitionFunction> m_partitionFunction;
private:
void syncInternalMaps();
void collectNetworkSpecies();
void populateReactionIDMap();
void populateSpeciesToIndexMap();
void reserveJacobianMatrix() const;
void recordADTape();
void collectAtomicReverseRateAtomicBases();
void precomputeNetwork();
[[nodiscard]] bool validateConservation() const;
[[nodiscard]] 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;
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>
[[nodiscard]] StepDerivatives<T> calculateAllDerivatives(
const std::vector<T> &Y_in,
T T9,
T rho
) const;
[[nodiscard]] StepDerivatives<double> calculateAllDerivatives(
const std::vector<double>& Y_in,
const double T9,
const double rho
) const;
[[nodiscard]] StepDerivatives<ADDouble> calculateAllDerivatives(
const std::vector<ADDouble>& Y_in,
const ADDouble &T9,
const ADDouble &rho
) const;
};
template <IsArithmeticOrAD T>
T GraphEngine::calculateReverseMolarReactionFlow(
T T9,
T rho,
std::vector<T> screeningFactors,
std::vector<T> Y,
size_t reactionIndex,
const reaction::LogicalReaction &reaction
) const {
if (!m_useReverseReactions) {
return static_cast<T>(0.0); // If reverse reactions are not used, return zero
}
T reverseMolarFlow = static_cast<T>(0.0);
if (reaction.qValue() != 0.0) {
T reverseRateConstant = static_cast<T>(0.0);
if constexpr (std::is_same_v<T, ADDouble>) { // Check if T is an AD type at compile time
const auto& atomic_func_ptr = m_atomicReverseRates[reactionIndex];
if (atomic_func_ptr != nullptr) {
// A. Instantiate the atomic operator for the specific reaction
// B. Marshal the input vector
std::vector<T> ax = { T9 };
std::vector<T> ay(1);
(*atomic_func_ptr)(ax, ay);
reverseRateConstant = static_cast<T>(ay[0]);
} else {
return reverseMolarFlow; // If no atomic function is available, return zero
}
} else {
// A,B If not calling with an AD type, calculate the reverse rate directly
reverseRateConstant = calculateReverseRate(reaction, T9);
}
// C. Get product multiplicities
std::unordered_map<fourdst::atomic::Species, int> productCounts;
for (const auto& product : reaction.products()) {
productCounts[product]++;
}
// D. Calculate the symmetry factor
T reverseSymmetryFactor = static_cast<T>(1.0);
for (const auto &count: productCounts | std::views::values) {
reverseSymmetryFactor /= static_cast<T>(std::tgamma(static_cast<double>(count + 1))); // Gamma function for factorial
}
// E. Calculate the abundance term
T productAbundanceTerm = static_cast<T>(1.0);
for (const auto& [species, count] : productCounts) {
const unsigned long speciesIndex = m_speciesToIndexMap.at(species);
productAbundanceTerm *= CppAD::pow(Y[speciesIndex], count);
}
// F. Determine the power for the density term
const size_t num_products = reaction.products().size();
const T rho_power = CppAD::pow(rho, static_cast<T>(num_products > 1 ? num_products - 1 : 0)); // Density raised to the power of (N-1) for N products
// G. Assemble the reverse molar flow rate
reverseMolarFlow = screeningFactors[reactionIndex] *
reverseRateConstant *
productAbundanceTerm *
reverseSymmetryFactor *
rho_power;
}
return reverseMolarFlow;
}
template<IsArithmeticOrAD T>
StepDerivatives<T> GraphEngine::calculateAllDerivatives(
const std::vector<T> &Y_in, T T9, T rho) const {
std::vector<T> screeningFactors = m_screeningModel->calculateScreeningFactors(
m_reactions,
m_networkSpecies,
Y_in,
T9,
rho
);
// --- Setup output derivatives structure ---
StepDerivatives<T> result;
result.dydt.resize(m_networkSpecies.size(), static_cast<T>(0.0));
// --- AD Pre-setup (flags to control conditionals in an AD safe / branch aware manner) ---
// ----- Constants for AD safe calculations ---
const T zero = static_cast<T>(0.0);
const T one = static_cast<T>(1.0);
// ----- Initialize variables for molar concentration product and thresholds ---
// Note: the logic here is that we use CppAD::CondExprLt to test thresholds and if they are less we set the flag
// to zero so that the final returned reaction flow is 0. This is as opposed to standard if statements
// which create branches that break the AD tape.
const T rho_threshold = static_cast<T>(MIN_DENSITY_THRESHOLD);
// --- Check if the density is below the threshold where we ignore reactions ---
T threshold_flag = CppAD::CondExpLt(rho, rho_threshold, zero, one); // If rho < threshold, set flag to 0
std::vector<T> Y = Y_in;
for (size_t i = 0; i < m_networkSpecies.size(); ++i) {
// We use CppAD::CondExpLt to handle AD taping and prevent branching
// Note that while this is syntactically more complex this is equivalent to
// if (Y[i] < 0) {Y[i] = 0;}
// The issue is that this would introduce a branch which would require the auto diff tape to be re-recorded
// each timestep, which is very inefficient.
Y[i] = CppAD::CondExpLt(Y[i], zero, zero, Y[i]); // Ensure no negative abundances
}
const T u = static_cast<T>(m_constants.u); // Atomic mass unit in grams
const T N_A = static_cast<T>(m_constants.Na); // Avogadro's number in mol^-1
const T c = static_cast<T>(m_constants.c); // Speed of light in cm/s
// --- SINGLE LOOP OVER ALL REACTIONS ---
for (size_t reactionIndex = 0; reactionIndex < m_reactions.size(); ++reactionIndex) {
const auto& reaction = m_reactions[reactionIndex];
// 1. Calculate forward reaction rate
const T forwardMolarReactionFlow = screeningFactors[reactionIndex] *
calculateMolarReactionFlow<T>(reaction, Y, T9, rho);
// 2. Calculate reverse reaction rate
T reverseMolarFlow = calculateReverseMolarReactionFlow<T>(
T9,
rho,
screeningFactors,
Y,
reactionIndex,
reaction
);
const T molarReactionFlow = forwardMolarReactionFlow - reverseMolarFlow; // Net molar reaction flow
// 3. Use the rate to update all relevant species derivatives (dY/dt)
for (size_t speciesIndex = 0; speciesIndex < m_networkSpecies.size(); ++speciesIndex) {
const T nu_ij = static_cast<T>(m_stoichiometryMatrix(speciesIndex, reactionIndex));
result.dydt[speciesIndex] += threshold_flag * nu_ij * molarReactionFlow;
}
}
T massProductionRate = static_cast<T>(0.0); // [mol][s^-1]
for (const auto& [species, index] : m_speciesToIndexMap) {
massProductionRate += result.dydt[index] * species.mass() * u;
}
result.nuclearEnergyGenerationRate = -massProductionRate * N_A * c * c; // [cm^2][s^-3] = [erg][s^-1][g^-1]
return result;
}
template <IsArithmeticOrAD T>
T GraphEngine::calculateMolarReactionFlow(
const reaction::Reaction &reaction,
const std::vector<T> &Y,
const T T9,
const T rho
) const {
// --- Pre-setup (flags to control conditionals in an AD safe / branch aware manner) ---
// ----- Constants for AD safe calculations ---
const T zero = static_cast<T>(0.0);
// --- Calculate the molar reaction rate (in units of [s^-1][cm^3(N-1)][mol^(1-N)] for N reactants) ---
const T k_reaction = reaction.calculate_rate(T9);
// --- Cound the number of each reactant species to account for species multiplicity ---
std::unordered_map<std::string, int> reactant_counts;
reactant_counts.reserve(reaction.reactants().size());
for (const auto& reactant : reaction.reactants()) {
reactant_counts[std::string(reactant.name())]++;
}
const int totalReactants = static_cast<int>(reaction.reactants().size());
// --- Accumulator for the molar concentration ---
auto molar_concentration_product = static_cast<T>(1.0);
// --- Loop through each unique reactant species and calculate the molar concentration for that species then multiply that into the accumulator ---
for (const auto& [species_name, count] : reactant_counts) {
// --- Resolve species to molar abundance ---
// PERF: Could probably optimize out this lookup
const auto species_it = m_speciesToIndexMap.find(m_networkSpeciesMap.at(species_name));
const size_t species_index = species_it->second;
const T Yi = Y[species_index];
// --- If count is > 1 , we need to raise the molar concentration to the power of count since there are really count bodies in that reaction ---
molar_concentration_product *= CppAD::pow(Yi, static_cast<T>(count)); // ni^count
// --- Apply factorial correction for identical reactions ---
if (count > 1) {
molar_concentration_product /= static_cast<T>(std::tgamma(static_cast<double>(count + 1))); // Gamma function for factorial
}
}
// --- Final reaction flow calculation [mol][s^-1][g^-1] ---
// Note: If the threshold flag ever gets set to zero this will return zero.
// This will result basically in multiple branches being written to the AD tape, which will make
// the tape more expensive to record, but it will also mean that we only need to record it once for
// the entire network.
const T densityTerm = CppAD::pow(rho, totalReactants > 1 ? static_cast<T>(totalReactants - 1) : zero); // Density raised to the power of (N-1) for N reactants
return molar_concentration_product * k_reaction * densityTerm;
}
};
gridfire::DynamicEngine
Abstract class for engines supporting Jacobian and stoichiometry operations.
Definition engine_abstract.h:130
gridfire::GraphEngine::AtomicReverseRate::AtomicReverseRate
AtomicReverseRate(const reaction::Reaction &reaction, const GraphEngine &engine)
Definition engine_graph.h:408
gridfire::GraphEngine::AtomicReverseRate::reverse
bool reverse(size_t q, const CppAD::vector< double > &tx, const CppAD::vector< double > &ty, CppAD::vector< double > &px, const CppAD::vector< double > &py) override
Definition engine_graph.cpp:1051
gridfire::GraphEngine::AtomicReverseRate::m_engine
const GraphEngine & m_engine
Definition engine_graph.h:444
gridfire::GraphEngine::AtomicReverseRate::rev_sparse_jac
bool rev_sparse_jac(size_t q, const CppAD::vector< std::set< size_t > > &rt, CppAD::vector< std::set< size_t > > &st) override
Definition engine_graph.cpp:1078
gridfire::GraphEngine::AtomicReverseRate::m_reaction
const reaction::Reaction & m_reaction
Definition engine_graph.h:443
gridfire::GraphEngine::AtomicReverseRate::forward
bool forward(size_t p, size_t q, const CppAD::vector< bool > &vx, CppAD::vector< bool > &vy, const CppAD::vector< double > &tx, CppAD::vector< double > &ty) override
Definition engine_graph.cpp:1029
gridfire::GraphEngine::AtomicReverseRate::for_sparse_jac
bool for_sparse_jac(size_t q, const CppAD::vector< std::set< size_t > > &r, CppAD::vector< std::set< size_t > > &s) override
Definition engine_graph.cpp:1069
gridfire::GraphEngine::isPrecomputationEnabled
bool isPrecomputationEnabled() const
Definition engine_graph.cpp:615
gridfire::GraphEngine::calculateReverseRateTwoBody
double calculateReverseRateTwoBody(const reaction::Reaction &reaction, const double T9, const double forwardRate, const double expFactor) const
Definition engine_graph.cpp:269
gridfire::GraphEngine::calculateReverseRate
double calculateReverseRate(const reaction::Reaction &reaction, double T9) const
Definition engine_graph.cpp:242
gridfire::GraphEngine::getNetworkSpecies
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Gets the list of species in the network.
Definition engine_graph.cpp:162
gridfire::GraphEngine::m_constants
constants m_constants
Definition engine_graph.h:450
gridfire::GraphEngine::getDepth
BuildDepthType getDepth() const override
Definition engine_graph.cpp:434
gridfire::GraphEngine::calculateReverseMolarReactionFlow
T calculateReverseMolarReactionFlow(T T9, T rho, std::vector< T > screeningFactors, std::vector< T > Y, size_t reactionIndex, const reaction::LogicalReaction &reaction) const
Definition engine_graph.h:648
gridfire::GraphEngine::m_usePrecomputation
bool m_usePrecomputation
Flag to enable or disable using precomputed reactions for efficiency. Mathematically,...
Definition engine_graph.h:471
gridfire::GraphEngine::m_full_jacobian_sparsity_pattern
CppAD::sparse_rc< std::vector< size_t > > m_full_jacobian_sparsity_pattern
Full sparsity pattern for the Jacobian matrix.
Definition engine_graph.h:464
gridfire::GraphEngine::m_jac_work
CppAD::sparse_jac_work m_jac_work
Work object for sparse Jacobian calculations.
Definition engine_graph.h:463
gridfire::GraphEngine::populateReactionIDMap
void populateReactionIDMap()
Populates the reaction ID map.
Definition engine_graph.cpp:135
gridfire::GraphEngine::mapNetInToMolarAbundanceVector
std::vector< double > mapNetInToMolarAbundanceVector(const NetIn &netIn) const override
Definition engine_graph.cpp:399
gridfire::GraphEngine::collectAtomicReverseRateAtomicBases
void collectAtomicReverseRateAtomicBases()
Definition engine_graph.cpp:947
gridfire::GraphEngine::m_rhsADFun
CppAD::ADFun< double > m_rhsADFun
CppAD function for the right-hand side of the ODE.
Definition engine_graph.h:462
gridfire::GraphEngine::m_jacobianMatrix
boost::numeric::ublas::compressed_matrix< double > m_jacobianMatrix
Jacobian matrix (species x species).
Definition engine_graph.h:461
gridfire::GraphEngine::getJacobianMatrixEntry
double getJacobianMatrixEntry(const int i, const int j) const override
Gets an entry from the previously generated Jacobian matrix.
Definition engine_graph.cpp:722
gridfire::GraphEngine::m_networkSpeciesMap
std::unordered_map< std::string_view, fourdst::atomic::Species > m_networkSpeciesMap
Map from species name to Species object.
Definition engine_graph.h:456
gridfire::GraphEngine::m_useReverseReactions
bool m_useReverseReactions
Flag to enable or disable reverse reactions. If false, only forward reactions are considered.
Definition engine_graph.h:473
gridfire::GraphEngine::m_partitionFunction
std::unique_ptr< partition::PartitionFunction > m_partitionFunction
Partition function for the network.
Definition engine_graph.h:478
gridfire::GraphEngine::setNetworkReactions
void setNetworkReactions(const reaction::LogicalReactionSet &reactions) override
Definition engine_graph.cpp:174
gridfire::GraphEngine::m_config
Config & m_config
Definition engine_graph.h:447
gridfire::GraphEngine::setUseReverseReactions
void setUseReverseReactions(bool useReverse)
Definition engine_graph.cpp:391
gridfire::GraphEngine::populateSpeciesToIndexMap
void populateSpeciesToIndexMap()
Populates the species-to-index map.
Definition engine_graph.cpp:144
gridfire::GraphEngine::m_logger
quill::Logger * m_logger
Definition engine_graph.h:448
gridfire::GraphEngine::m_screeningType
screening::ScreeningType m_screeningType
Screening type for the reaction network. Default to no screening.
Definition engine_graph.h:468
gridfire::GraphEngine::update
fourdst::composition::Composition update(const NetIn &netIn) override
Update the internal state of the engine.
Definition engine_graph.cpp:886
gridfire::GraphEngine::m_precomputedReactions
std::vector< PrecomputedReaction > m_precomputedReactions
Precomputed reactions for efficiency.
Definition engine_graph.h:477
gridfire::GraphEngine::m_reactionIDMap
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 ...
Definition engine_graph.h:453
gridfire::GraphEngine::getSpeciesTimescales
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.
Definition engine_graph.cpp:842
gridfire::GraphEngine::getScreeningModel
screening::ScreeningType getScreeningModel() const override
Get the current electron screening model.
Definition engine_graph.cpp:607
gridfire::GraphEngine::getStoichiometryMatrixEntry
int getStoichiometryMatrixEntry(const int speciesIndex, const int reactionIndex) const override
Gets an entry from the stoichiometry matrix.
Definition engine_graph.cpp:733
gridfire::GraphEngine::setPrecomputation
void setPrecomputation(bool precompute)
Definition engine_graph.cpp:611
gridfire::GraphEngine::m_depth
BuildDepthType m_depth
Definition engine_graph.h:475
gridfire::GraphEngine::setScreeningModel
void setScreeningModel(screening::ScreeningType) override
Set the electron screening model.
Definition engine_graph.cpp:602
gridfire::GraphEngine::m_atomicReverseRates
std::vector< std::unique_ptr< AtomicReverseRate > > m_atomicReverseRates
Definition engine_graph.h:466
gridfire::GraphEngine::exportToCSV
void exportToCSV(const std::string &filename) const
Exports the network to a CSV file for analysis.
Definition engine_graph.cpp:788
gridfire::GraphEngine::getNetReactionStoichiometry
static std::unordered_map< fourdst::atomic::Species, int > getNetReactionStoichiometry(const reaction::Reaction &reaction)
Gets the net stoichiometry for a given reaction.
Definition engine_graph.cpp:727
gridfire::GraphEngine::reserveJacobianMatrix
void reserveJacobianMatrix() const
Reserves space for the Jacobian matrix.
Definition engine_graph.cpp:151
gridfire::GraphEngine::getSpeciesIndex
int getSpeciesIndex(const fourdst::atomic::Species &species) const override
Definition engine_graph.cpp:395
gridfire::GraphEngine::calculateMolarReactionFlow
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.
Definition engine_graph.cpp:623
gridfire::GraphEngine::precomputeNetwork
void precomputeNetwork()
Definition engine_graph.cpp:960
gridfire::GraphEngine::m_networkSpecies
std::vector< fourdst::atomic::Species > m_networkSpecies
Vector of unique species in the network.
Definition engine_graph.h:455
gridfire::GraphEngine::recordADTape
void recordADTape()
Records the AD tape for the right-hand side of the ODE.
Definition engine_graph.cpp:902
gridfire::GraphEngine::calculateAllDerivativesUsingPrecomputation
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
Definition engine_graph.cpp:448
gridfire::GraphEngine::involvesSpecies
bool involvesSpecies(const fourdst::atomic::Species &species) const
Checks if a given species is involved in the network.
Definition engine_graph.cpp:179
gridfire::GraphEngine::calculateRHSAndEnergy
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.
Definition engine_graph.cpp:57
gridfire::GraphEngine::m_reactions
reaction::LogicalReactionSet m_reactions
Set of REACLIB reactions in the network.
Definition engine_graph.h:452
gridfire::GraphEngine::syncInternalMaps
void syncInternalMaps()
Synchronizes the internal maps.
Definition engine_graph.cpp:81
gridfire::GraphEngine::validateConservation
bool validateConservation() const
Validates mass and charge conservation across all reactions.
Definition engine_graph.cpp:187
gridfire::GraphEngine::generateJacobianMatrix
void generateJacobianMatrix(const std::vector< double > &Y_dynamic, const double T9, const double rho) const override
Generates the Jacobian matrix for the current state.
Definition engine_graph.cpp:632
gridfire::GraphEngine::m_stoichiometryMatrix
boost::numeric::ublas::compressed_matrix< int > m_stoichiometryMatrix
Stoichiometry matrix (species x reactions).
Definition engine_graph.h:459
gridfire::GraphEngine::getNetworkReactions
const reaction::LogicalReactionSet & getNetworkReactions() const override
Gets the set of logical reactions in the network.
Definition engine_graph.cpp:168
gridfire::GraphEngine::m_speciesToIndexMap
std::unordered_map< fourdst::atomic::Species, size_t > m_speciesToIndexMap
Map from species to their index in the stoichiometry matrix.
Definition engine_graph.h:457
gridfire::GraphEngine::rebuild
void rebuild(const fourdst::composition::Composition &comp, const BuildDepthType depth) override
Definition engine_graph.cpp:438
gridfire::GraphEngine::exportToDot
void exportToDot(const std::string &filename) const
Exports the network to a DOT file for visualization.
Definition engine_graph.cpp:740
gridfire::GraphEngine::getPartitionFunction
const partition::PartitionFunction & getPartitionFunction() const
Definition engine_graph.cpp:619
gridfire::GraphEngine::isUsingReverseReactions
bool isUsingReverseReactions() const
Definition engine_graph.cpp:387
gridfire::GraphEngine::primeEngine
PrimingReport primeEngine(const NetIn &netIn) override
Definition engine_graph.cpp:407
gridfire::GraphEngine::generateStoichiometryMatrix
void generateStoichiometryMatrix() override
Generates the stoichiometry matrix for the network.
Definition engine_graph.cpp:545
gridfire::GraphEngine::collectNetworkSpecies
void collectNetworkSpecies()
Collects the unique species in the network.
Definition engine_graph.cpp:106
gridfire::GraphEngine::getSpeciesDestructionTimescales
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > getSpeciesDestructionTimescales(const std::vector< double > &Y, double T9, double rho) const override
Definition engine_graph.cpp:861
gridfire::GraphEngine::isStale
bool isStale(const NetIn &netIn) override
Definition engine_graph.cpp:898
gridfire::GraphEngine::m_screeningModel
std::unique_ptr< screening::ScreeningModel > m_screeningModel
Definition engine_graph.h:469
gridfire::GraphEngine::calculateReverseRateTwoBodyDerivative
double calculateReverseRateTwoBodyDerivative(const reaction::Reaction &reaction, const double T9, const double reverseRate) const
Definition engine_graph.cpp:346
gridfire::GraphEngine::calculateAllDerivatives
StepDerivatives< T > calculateAllDerivatives(const std::vector< T > &Y_in, T T9, T rho) const
Calculates all derivatives (dY/dt) and the energy generation rate.
Definition engine_graph.h:715
gridfire::GraphEngine::GraphEngine
GraphEngine(const fourdst::composition::Composition &composition, const BuildDepthType=NetworkBuildDepth::Full)
Constructs a GraphEngine from a composition.
Definition engine_graph.cpp:34
gridfire::partition::PartitionFunction
Abstract interface for evaluating nuclear partition functions.
Definition partition_abstract.h:17
gridfire::reaction::LogicalReaction
Represents a "logical" reaction that aggregates rates from multiple sources.
Definition reaction.h:310
gridfire::reaction::Reaction
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
construction.h
engine_abstract.h
Abstract interfaces for reaction network engines in GridFire.
gridfire::reaction
Definition reaction.h:25
gridfire::reaction::LogicalReactionSet
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:563
gridfire::screening::selectScreeningModel
std::unique_ptr< ScreeningModel > selectScreeningModel(ScreeningType type)
A factory function to select and create a screening model.
Definition screening_types.cpp:9
gridfire::screening::ScreeningType
ScreeningType
Enumerates the available plasma screening models.
Definition screening_types.h:15
gridfire::screening::ScreeningType::BARE
@ BARE
No screening applied. The screening factor is always 1.0.
Definition screening_types.h:16
gridfire
Definition engine_abstract.h:31
gridfire::NetworkBuildDepth::Full
@ Full
Definition building.h:22
gridfire::ADDouble
CppAD::AD< double > ADDouble
Alias for CppAD AD type for double precision.
Definition engine_graph.h:44
gridfire::BuildDepthType
std::variant< NetworkBuildDepth, int > BuildDepthType
Variant specifying either a predefined NetworkBuildDepth or a custom integer depth.
Definition building.h:37
gridfire::SparsityPattern
std::vector< std::pair< size_t, size_t > > SparsityPattern
Definition engine_abstract.h:65
gridfire::MIN_ABUNDANCE_THRESHOLD
static constexpr double MIN_ABUNDANCE_THRESHOLD
Minimum abundance threshold below which species are ignored.
Definition engine_graph.h:66
gridfire::MIN_DENSITY_THRESHOLD
static constexpr double MIN_DENSITY_THRESHOLD
Minimum density threshold below which reactions are ignored.
Definition engine_graph.h:57
gridfire::MIN_JACOBIAN_THRESHOLD
static constexpr double MIN_JACOBIAN_THRESHOLD
Minimum value for Jacobian matrix entries.
Definition engine_graph.h:74
network.h
partition_abstract.h
priming.h
reaction.h
Defines classes for representing and managing nuclear reactions.
screening_abstract.h
screening_types.h
gridfire::GraphEngine::PrecomputedReaction
Definition engine_graph.h:384
gridfire::GraphEngine::PrecomputedReaction::reactant_powers
std::vector< int > reactant_powers
Definition engine_graph.h:388
gridfire::GraphEngine::PrecomputedReaction::product_powers
std::vector< int > product_powers
Powers of each unique product in the reverse reaction.
Definition engine_graph.h:395
gridfire::GraphEngine::PrecomputedReaction::affected_species_indices
std::vector< size_t > affected_species_indices
Definition engine_graph.h:390
gridfire::GraphEngine::PrecomputedReaction::unique_reactant_indices
std::vector< size_t > unique_reactant_indices
Definition engine_graph.h:387
gridfire::GraphEngine::PrecomputedReaction::reverse_symmetry_factor
double reverse_symmetry_factor
Symmetry factor for reverse reactions.
Definition engine_graph.h:396
gridfire::GraphEngine::PrecomputedReaction::stoichiometric_coefficients
std::vector< int > stoichiometric_coefficients
Definition engine_graph.h:391
gridfire::GraphEngine::PrecomputedReaction::reaction_index
size_t reaction_index
Definition engine_graph.h:386
gridfire::GraphEngine::PrecomputedReaction::unique_product_indices
std::vector< size_t > unique_product_indices
Unique product indices for reverse reactions.
Definition engine_graph.h:394
gridfire::GraphEngine::PrecomputedReaction::symmetry_factor
double symmetry_factor
Definition engine_graph.h:389
gridfire::GraphEngine::constants
Definition engine_graph.h:399
gridfire::GraphEngine::constants::kB
const double kB
Boltzmann constant in erg/K.
Definition engine_graph.h:403
gridfire::GraphEngine::constants::u
const double u
Atomic mass unit in g.
Definition engine_graph.h:400
gridfire::GraphEngine::constants::Na
const double Na
Avogadro's number.
Definition engine_graph.h:401
gridfire::GraphEngine::constants::c
const double c
Speed of light in cm/s.
Definition engine_graph.h:402
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::StepDerivatives::nuclearEnergyGenerationRate
T nuclearEnergyGenerationRate
Specific energy generation rate (e.g., erg/g/s).
Definition engine_abstract.h:62
gridfire::StepDerivatives::dydt
std::vector< T > dydt
Derivatives of abundances (dY/dt for each species).
Definition engine_abstract.h:61
gridfire::expectations::StaleEngineError
Definition expected_engine.h:44