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;
101 explicit GraphEngine(
const fourdst::composition::Composition &composition);
127 const std::vector<double>& Y,
146 const std::vector<double>& Y,
173 const std::vector<double>&Y,
182 [[nodiscard]]
const std::vector<fourdst::atomic::Species>&
getNetworkSpecies()
const override;
228 const int speciesIndex,
229 const int reactionIndex
244 const std::vector<double>& Y,
258 const fourdst::atomic::Species& species
278 const std::string& filename
298 const std::string& filename
324 quill::Logger*
m_logger = LogManager::getInstance().getLogger(
"log");
403 const fourdst::composition::Composition &composition,
421 template <IsArithmeticOrAD T>
424 const std::vector<T> &Y,
441 template<IsArithmeticOrAD T>
443 const std::vector<T> &Y_in,
461 const std::vector<double>& Y_in,
479 const std::vector<ADDouble>& Y_in,
486 template<IsArithmeticOrAD T>
488 const std::vector<T> &Y_in, T T9, T rho)
const {
489 std::vector<T> screeningFactors =
m_screeningModel->calculateScreeningFactors(
503 const T zero =
static_cast<T
>(0.0);
504 const T one =
static_cast<T
>(1.0);
513 T threshold_flag = CppAD::CondExpLt(rho, rho_threshold, zero, one);
515 std::vector<T> Y = Y_in;
522 Y[i] = CppAD::CondExpLt(Y[i], zero, zero, Y[i]);
525 const T u =
static_cast<T
>(
m_constants.get(
"u").value);
526 const T N_A =
static_cast<T
>(
m_constants.get(
"N_a").value);
527 const T c =
static_cast<T
>(
m_constants.get(
"c").value);
530 for (
size_t reactionIndex = 0; reactionIndex <
m_reactions.size(); ++reactionIndex) {
537 for (
size_t speciesIndex = 0; speciesIndex <
m_networkSpecies.size(); ++speciesIndex) {
539 result.
dydt[speciesIndex] += threshold_flag * nu_ij * molarReactionFlow / rho;
543 T massProductionRate =
static_cast<T
>(0.0);
545 massProductionRate += result.
dydt[index] * species.mass() * u;
554 template <IsArithmeticOrAD T>
557 const std::vector<T> &Y,
564 const T zero =
static_cast<T
>(0.0);
565 const T one =
static_cast<T
>(1.0);
572 T threshold_flag = one;
575 const T k_reaction =
reaction.calculate_rate(T9);
578 std::unordered_map<std::string, int> reactant_counts;
579 reactant_counts.reserve(
reaction.reactants().size());
580 for (
const auto& reactant :
reaction.reactants()) {
581 reactant_counts[std::string(reactant.name())]++;
585 auto molar_concentration_product =
static_cast<T
>(1.0);
588 for (
const auto& [species_name, count] : reactant_counts) {
592 const size_t species_index = species_it->second;
593 const T Yi = Y[species_index];
596 threshold_flag *= CppAD::CondExpLt(Yi, Y_threshold, zero, one);
599 T molar_concentration = Yi * rho;
602 molar_concentration_product *= CppAD::pow(molar_concentration,
static_cast<T
>(count));
606 molar_concentration_product /=
static_cast<T
>(std::tgamma(
static_cast<double>(count + 1)));
614 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 update(const NetIn &netIn) override
screening::ScreeningType m_screeningType
Screening type for the reaction network. Default to no screening.
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 ...
screening::ScreeningType getScreeningModel() const override
int getStoichiometryMatrixEntry(const int speciesIndex, const int reactionIndex) const override
Gets an entry from the stoichiometry matrix.
void setScreeningModel(screening::ScreeningType) override
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)
@ BARE
No screening applied.
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).