3#include "fourdst/composition/atomicSpecies.h"
4#include "fourdst/composition/composition.h"
5#include "fourdst/logging/logging.h"
6#include "fourdst/config/config.h"
15#include <unordered_map>
19#include <boost/numeric/ublas/matrix_sparse.hpp>
21#include "cppad/cppad.hpp"
36 using fourdst::config::Config;
37 using fourdst::logging::LogManager;
38 using fourdst::constant::Constants;
102 explicit GraphEngine(
const fourdst::composition::Composition &composition);
128 const std::vector<double>& Y,
147 const std::vector<double>& Y,
174 const std::vector<double>&Y,
183 [[nodiscard]]
const std::vector<fourdst::atomic::Species>&
getNetworkSpecies()
const override;
229 const int speciesIndex,
230 const int reactionIndex
245 const std::vector<double>& Y,
259 const fourdst::atomic::Species& species
279 const std::string& filename
299 const std::string& filename
321 const double u = Constants::getInstance().get(
"u").value;
322 const double Na = Constants::getInstance().get(
"N_a").value;
323 const double c = Constants::getInstance().get(
"c").value;
328 quill::Logger*
m_logger = LogManager::getInstance().getLogger(
"log");
430 const fourdst::composition::Composition &composition,
436 const std::vector<double> &Y_in,
437 const std::vector<double>& bare_rates,
455 template <IsArithmeticOrAD T>
458 const std::vector<T> &Y,
475 template<IsArithmeticOrAD T>
477 const std::vector<T> &Y_in,
495 const std::vector<double>& Y_in,
513 const std::vector<ADDouble>& Y_in,
520 template<IsArithmeticOrAD T>
522 const std::vector<T> &Y_in, T T9, T rho)
const {
523 std::vector<T> screeningFactors =
m_screeningModel->calculateScreeningFactors(
537 const T zero =
static_cast<T
>(0.0);
538 const T one =
static_cast<T
>(1.0);
547 T threshold_flag = CppAD::CondExpLt(rho, rho_threshold, zero, one);
549 std::vector<T> Y = Y_in;
556 Y[i] = CppAD::CondExpLt(Y[i], zero, zero, Y[i]);
564 for (
size_t reactionIndex = 0; reactionIndex <
m_reactions.size(); ++reactionIndex) {
571 for (
size_t speciesIndex = 0; speciesIndex <
m_networkSpecies.size(); ++speciesIndex) {
573 result.
dydt[speciesIndex] += threshold_flag * nu_ij * molarReactionFlow / rho;
577 T massProductionRate =
static_cast<T
>(0.0);
579 massProductionRate += result.
dydt[index] * species.mass() * u;
588 template <IsArithmeticOrAD T>
591 const std::vector<T> &Y,
598 const T zero =
static_cast<T
>(0.0);
599 const T one =
static_cast<T
>(1.0);
606 T threshold_flag = one;
609 const T k_reaction =
reaction.calculate_rate(T9);
612 std::unordered_map<std::string, int> reactant_counts;
613 reactant_counts.reserve(
reaction.reactants().size());
614 for (
const auto& reactant :
reaction.reactants()) {
615 reactant_counts[std::string(reactant.name())]++;
619 auto molar_concentration_product =
static_cast<T
>(1.0);
622 for (
const auto& [species_name, count] : reactant_counts) {
626 const size_t species_index = species_it->second;
627 const T Yi = Y[species_index];
630 threshold_flag *= CppAD::CondExpLt(Yi, Y_threshold, zero, one);
633 T molar_concentration = Yi * rho;
636 molar_concentration_product *= CppAD::pow(molar_concentration,
static_cast<T
>(count));
640 molar_concentration_product /=
static_cast<T
>(std::tgamma(
static_cast<double>(count + 1)));
648 return molar_concentration_product * k_reaction * threshold_flag;
Abstract class for engines supporting Jacobian and stoichiometry operations.
bool isPrecomputationEnabled() const
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Gets the list of species in the network.
bool m_usePrecomputation
Flag to enable or disable using precomputed reactions for efficiency. Mathematically,...
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 populateReactionIDMap()
Populates the reaction ID map.
CppAD::ADFun< double > m_rhsADFun
CppAD function for the right-hand side of the ODE.
boost::numeric::ublas::compressed_matrix< double > m_jacobianMatrix
Jacobian matrix (species x species).
double getJacobianMatrixEntry(const int i, const int j) const override
Gets an entry from the previously generated Jacobian matrix.
std::unordered_map< std::string_view, fourdst::atomic::Species > m_networkSpeciesMap
Map from species name to Species object.
void populateSpeciesToIndexMap()
Populates the species-to-index map.
void update(const NetIn &netIn) override
Update the internal state of the engine.
screening::ScreeningType m_screeningType
Screening type for the reaction network. Default to no screening.
void reserveJacobianMatrix()
Reserves space for the Jacobian matrix.
std::vector< PrecomputedReaction > m_precomputedReactions
Precomputed reactions for efficiency.
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 ...
StepDerivatives< double > calculateAllDerivativesUsingPrecomputation(const std::vector< double > &Y_in, const std::vector< double > &bare_rates, double T9, double rho) const
screening::ScreeningType getScreeningModel() const override
Get the current electron screening model.
int getStoichiometryMatrixEntry(const int speciesIndex, const int reactionIndex) const override
Gets an entry from the stoichiometry matrix.
void setPrecomputation(bool precompute)
void setScreeningModel(screening::ScreeningType) override
Set the electron screening model.
void exportToCSV(const std::string &filename) const
Exports the network to a CSV file for analysis.
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.
static std::unordered_map< fourdst::atomic::Species, int > getNetReactionStoichiometry(const reaction::Reaction &reaction)
Gets the net stoichiometry for a given reaction.
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.
std::vector< fourdst::atomic::Species > m_networkSpecies
Vector of unique species in the network.
void recordADTape()
Records the AD tape for the right-hand side of the ODE.
GraphEngine(const fourdst::composition::Composition &composition)
Constructs a GraphEngine from a composition.
bool involvesSpecies(const fourdst::atomic::Species &species) const
Checks if a given species is involved in the network.
reaction::LogicalReactionSet m_reactions
Set of REACLIB reactions in the network.
void syncInternalMaps()
Synchronizes the internal maps.
bool validateConservation() const
Validates mass and charge conservation across all reactions.
boost::numeric::ublas::compressed_matrix< int > m_stoichiometryMatrix
Stoichiometry matrix (species x reactions).
const reaction::LogicalReactionSet & getNetworkReactions() const override
Gets the set of logical reactions in the network.
std::unordered_map< fourdst::atomic::Species, size_t > m_speciesToIndexMap
Map from species to their index in the stoichiometry matrix.
void exportToDot(const std::string &filename) const
Exports the network to a DOT file for visualization.
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.
void collectNetworkSpecies()
Collects the unique species in the network.
void validateComposition(const fourdst::composition::Composition &composition, double culling, double T9)
Validates the composition against the current reaction set.
std::unique_ptr< screening::ScreeningModel > m_screeningModel
StepDerivatives< T > calculateAllDerivatives(const std::vector< T > &Y_in, T T9, T rho) const
Calculates all derivatives (dY/dt) and the energy generation rate.
Represents a single nuclear reaction from a specific data source.
Abstract interfaces for reaction network engines in GridFire.
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
std::unique_ptr< ScreeningModel > selectScreeningModel(ScreeningType type)
A factory function to select and create a screening model.
ScreeningType
Enumerates the available plasma screening models.
@ BARE
No screening applied. The screening factor is always 1.0.
CppAD::AD< double > ADDouble
Alias for CppAD AD type for double precision.
static constexpr double MIN_ABUNDANCE_THRESHOLD
Minimum abundance threshold below which species are ignored.
static constexpr double MIN_DENSITY_THRESHOLD
Minimum density threshold below which reactions are ignored.
static constexpr double MIN_JACOBIAN_THRESHOLD
Minimum value for Jacobian matrix entries.
Defines classes for representing and managing nuclear reactions.
std::vector< int > reactant_powers
std::vector< size_t > affected_species_indices
std::vector< size_t > unique_reactant_indices
std::vector< int > stoichiometric_coefficients
const double u
Atomic mass unit in g.
const double Na
Avogadro's number.
const double c
Speed of light in cm/s.
Structure holding derivatives and energy generation for a network step.
T nuclearEnergyGenerationRate
Specific energy generation rate (e.g., erg/g/s).
std::vector< T > dydt
Derivatives of abundances (dY/dt for each species).