3#include "fourdst/composition/atomicSpecies.h"
4#include "fourdst/composition/composition.h"
5#include "fourdst/logging/logging.h"
6#include "fourdst/config/config.h"
13#include <unordered_map>
16#include <boost/numeric/ublas/matrix_sparse.hpp>
18#include "cppad/cppad.hpp"
33 using fourdst::config::Config;
34 using fourdst::logging::LogManager;
35 using fourdst::constant::Constants;
98 explicit GraphEngine(
const fourdst::composition::Composition &composition);
124 const std::vector<double>& Y,
143 const std::vector<double>& Y,
170 const std::vector<double>&Y,
179 [[nodiscard]]
const std::vector<fourdst::atomic::Species>&
getNetworkSpecies()
const override;
225 const int speciesIndex,
226 const int reactionIndex
241 const std::vector<double>& Y,
253 const fourdst::atomic::Species& species
273 const std::string& filename
293 const std::string& filename
312 quill::Logger*
m_logger = LogManager::getInstance().getLogger(
"log");
391 const fourdst::composition::Composition &composition,
409 template <IsArithmeticOrAD T>
412 const std::vector<T> &Y,
429 template<IsArithmeticOrAD T>
431 const std::vector<T> &Y_in,
449 const std::vector<double>& Y_in,
467 const std::vector<ADDouble>& Y_in,
474 template<IsArithmeticOrAD T>
476 const std::vector<T> &Y_in, T T9, T rho)
const {
484 const T zero =
static_cast<T
>(0.0);
485 const T one =
static_cast<T
>(1.0);
494 T threshold_flag = CppAD::CondExpLt(rho, rho_threshold, zero, one);
496 std::vector<T> Y = Y_in;
503 Y[i] = CppAD::CondExpLt(Y[i], zero, zero, Y[i]);
506 const T u =
static_cast<T
>(
m_constants.get(
"u").value);
507 const T N_A =
static_cast<T
>(
m_constants.get(
"N_a").value);
508 const T c =
static_cast<T
>(
m_constants.get(
"c").value);
511 for (
size_t reactionIndex = 0; reactionIndex <
m_reactions.size(); ++reactionIndex) {
518 for (
size_t speciesIndex = 0; speciesIndex <
m_networkSpecies.size(); ++speciesIndex) {
520 result.
dydt[speciesIndex] += threshold_flag * nu_ij * molarReactionFlow / rho;
524 T massProductionRate =
static_cast<T
>(0.0);
526 massProductionRate += result.
dydt[index] * species.mass() * u;
535 template <IsArithmeticOrAD T>
538 const std::vector<T> &Y,
545 const T zero =
static_cast<T
>(0.0);
546 const T one =
static_cast<T
>(1.0);
553 T threshold_flag = one;
556 const T k_reaction =
reaction.calculate_rate(T9);
559 std::unordered_map<std::string, int> reactant_counts;
560 reactant_counts.reserve(
reaction.reactants().size());
561 for (
const auto& reactant :
reaction.reactants()) {
562 reactant_counts[std::string(reactant.name())]++;
566 auto molar_concentration_product =
static_cast<T
>(1.0);
569 for (
const auto& [species_name, count] : reactant_counts) {
573 const size_t species_index = species_it->second;
574 const T Yi = Y[species_index];
577 threshold_flag *= CppAD::CondExpLt(Yi, Y_threshold, zero, one);
580 T molar_concentration = Yi * rho;
583 molar_concentration_product *= CppAD::pow(molar_concentration,
static_cast<T
>(count));
587 molar_concentration_product /=
static_cast<T
>(std::tgamma(
static_cast<double>(count + 1)));
595 return molar_concentration_product * k_reaction * threshold_flag;
Abstract class for engines supporting Jacobian and stoichiometry operations.
Constants & m_constants
Access to physical constants.
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Gets the list of species in the network.
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 reserveJacobianMatrix()
Reserves space for the Jacobian matrix.
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 ...
int getStoichiometryMatrixEntry(const int speciesIndex, const int reactionIndex) const override
Gets an entry from the stoichiometry matrix.
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.
StepDerivatives< T > calculateAllDerivatives(const std::vector< T > &Y_in, T T9, T rho) const
Calculates all derivatives (dY/dt) and the energy generation rate.
A collection of LogicalReaction objects.
Represents a single nuclear reaction from a specific data source.
Abstract interfaces for reaction network engines in GridFire.
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.
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).