10#include "quill/LogMacros.h"
12#include "fourdst/composition/atomicSpecies.h"
17 using namespace fourdst::atomic;
20 const std::string_view
id,
21 const std::string_view
peName,
24 const std::vector<Species>&
products,
26 const std::string_view label,
48 constexpr double r_p13 = 1.0 / 3.0;
49 constexpr double r_p53 = 5.0 / 3.0;
50 constexpr double r_p23 = 2.0 / 3.0;
51 constexpr double r_p43 = 4.0 / 3.0;
53 const double T9_m1 = 1.0 / T9;
54 const double T9_m23 = std::pow(T9, -r_p23);
55 const double T9_m43 = std::pow(T9, -r_p43);
57 const double d_log_k_fwd_dT9 =
65 return d_log_k_fwd_dT9;
75 if (reactant == species) {
84 if (product == species) {
94 rs.insert(ps.begin(), ps.end());
99 std::unordered_set<Species> reactantsSet;
101 reactantsSet.insert(reactant);
107 std::unordered_set<Species> productsSet;
109 productsSet.insert(product);
117 if (reactant == species) {
122 if (product == species) {
134 std::unordered_map<Species, int> stoichiometryMap;
136 stoichiometryMap[reactant]--;
139 stoichiometryMap[product]++;
141 return stoichiometryMap;
145 double reactantMass = 0.0;
146 double productMass = 0.0;
147 constexpr double AMU2MeV = 931.494893;
149 reactantMass += reactant.mass();
152 productMass += product.mass();
154 return (reactantMass - productMass) * AMU2MeV;
158 return XXHash64::hash(
m_id.data(),
m_id.size(), seed);
180 "LogicalReaction constructed with reactions having different Q-values. Expected {} got {}.",
185 throw std::runtime_error(
"LogicalReaction constructed with reactions having different Q-values. Expected " + std::to_string(
m_qValue) +
" got " + std::to_string(
reaction.qValue()) +
" (difference : " + std::to_string(std::abs(
reaction.qValue() -
m_qValue)) +
").");
194 LOG_ERROR(
m_logger,
"Cannot add reaction with different peName to LogicalReaction. Expected {} got {}.",
m_id,
reaction.peName());
196 throw std::runtime_error(
"Cannot add reaction with different peName to LogicalReaction. Expected " + std::string(
m_id) +
" got " + std::string(
reaction.peName()) +
".");
199 if (source ==
reaction.sourceLabel()) {
200 LOG_ERROR(
m_logger,
"Cannot add reaction with duplicate source label {} to LogicalReaction.",
reaction.sourceLabel());
202 throw std::runtime_error(
"Cannot add reaction with duplicate source label " + std::string(
reaction.sourceLabel()) +
" to LogicalReaction.");
206 LOG_ERROR(
m_logger,
"LogicalReaction constructed with reactions having different Q-values. Expected {} got {}.",
m_qValue,
reaction.qValue());
208 throw std::runtime_error(
"LogicalReaction constructed with reactions having different Q-values. Expected " + std::to_string(
m_qValue) +
" got " + std::to_string(
reaction.qValue()) +
".");
219 constexpr double r_p13 = 1.0 / 3.0;
220 constexpr double r_p53 = 5.0 / 3.0;
221 constexpr double r_p23 = 2.0 / 3.0;
222 constexpr double r_p43 = 4.0 / 3.0;
224 double totalRate = 0.0;
225 double totalRateDerivative = 0.0;
228 const double T9_m1 = 1.0 / T9;
229 const double T913 = std::pow(T9, r_p13);
230 const double T953 = std::pow(T9, r_p53);
231 const double logT9 = std::log(T9);
233 const double T9_m1_sq = T9_m1 * T9_m1;
234 const double T9_m23 = std::pow(T9, -r_p23);
235 const double T9_m43 = std::pow(T9, -r_p43);
236 const double T9_p23 = std::pow(T9, r_p23);
239 for (
const auto& coeffs :
m_rates) {
240 const double exponent = coeffs.a0 +
247 const double individualRate = std::exp(exponent);
249 const double d_exponent_T9 =
250 -coeffs.a1 * T9_m1_sq
251 - r_p13 * coeffs.a2 * T9_m43
252 + r_p13 * coeffs.a3 * T9_m23
254 + r_p53 * coeffs.a5 * T9_p23
257 const double individualRateDerivative = individualRate * d_exponent_T9;
258 totalRate += individualRate;
259 totalRateDerivative += individualRateDerivative;
262 if (totalRate == 0.0) {
266 return totalRateDerivative / totalRate;
274 std::unordered_map<std::string_view, std::vector<Reaction>> groupedReactions;
276 for (
const auto&
reaction: reactionSet) {
280 std::vector<LogicalReaction> reactions;
281 reactions.reserve(groupedReactions.size());
283 for (
const auto &reactionsGroup: groupedReactions | std::views::values) {
285 reactions.push_back(logicalReaction);
307 struct hash<
gridfire::reaction::LogicalReactionSet> {
Represents a "logical" reaction that aggregates rates from multiple sources.
void add_reaction(const Reaction &reaction)
Adds another Reaction source to this logical reaction.
double calculate_rate(const double T9) const override
Calculates the total reaction rate by summing all source rates.
LogicalReaction(const std::vector< Reaction > &reactions)
Constructs a LogicalReaction from a vector of Reaction objects.
std::vector< std::string > m_sources
List of source labels.
std::vector< RateCoefficientSet > m_rates
List of rate coefficient sets from each source.
virtual double calculate_forward_rate_log_derivative(const double T9) const override
Represents a single nuclear reaction from a specific data source.
std::string m_sourceLabel
Source label for the rate data (e.g., "wc12w", "st08").
std::unordered_set< fourdst::atomic::Species > product_species() const
Gets a set of all unique product species.
bool contains_product(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a product.
std::string_view id() const
Gets the unique identifier of the reaction.
bool m_reverse
Flag indicating if this is a reverse reaction rate.
const std::vector< fourdst::atomic::Species > & reactants() const
Gets the vector of reactant species.
int m_chapter
Chapter number from the REACLIB database, defining the reaction structure.
size_t num_species() const
Gets the number of unique species involved in the reaction.
virtual double calculate_forward_rate_log_derivative(const double T9) const
std::string_view sourceLabel() const
Gets the source label for the rate data.
std::vector< fourdst::atomic::Species > m_products
Products of the reaction.
double m_qValue
Q-value of the reaction in MeV.
std::string m_id
Unique identifier for the reaction (e.g., "h1+h1=>h2+e+nu").
int chapter() const
Gets the REACLIB chapter number.
std::string m_peName
Name of the reaction in (projectile, ejectile) notation (e.g. "p(p,g)d").
const std::vector< fourdst::atomic::Species > & products() const
Gets the vector of product species.
virtual std::string_view peName() const
Gets the reaction name in (projectile, ejectile) notation.
std::unordered_set< fourdst::atomic::Species > all_species() const
Gets a set of all unique species involved in the reaction.
Reaction(const std::string_view id, const std::string_view peName, const int chapter, const std::vector< fourdst::atomic::Species > &reactants, const std::vector< fourdst::atomic::Species > &products, const double qValue, const std::string_view label, const RateCoefficientSet &sets, const bool reverse=false)
Constructs a Reaction object.
std::unordered_set< fourdst::atomic::Species > reactant_species() const
Gets a set of all unique reactant species.
const RateCoefficientSet & rateCoefficients() const
Gets the set of rate coefficients.
std::vector< fourdst::atomic::Species > m_reactants
Reactants of the reaction.
double excess_energy() const
Calculates the excess energy from the mass difference of reactants and products.
RateCoefficientSet m_rateCoefficients
The seven rate coefficients.
bool is_reverse() const
Checks if this is a reverse reaction rate.
bool contains(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a reactant or product.
bool contains_reactant(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a reactant.
double qValue() const
Gets the Q-value of the reaction.
std::unordered_map< fourdst::atomic::Species, int > stoichiometry() const
Gets a map of all species to their stoichiometric coefficients.
virtual double calculate_rate(const double T9) const
Calculates the reaction rate for a given temperature.
uint64_t hash(uint64_t seed=0) const
Computes a hash for the reaction based on its ID.
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
LogicalReactionSet packReactionSetToLogicalReactionSet(const ReactionSet &reactionSet)
TemplatedReactionSet< Reaction > ReactionSet
A set of reactions, typically from a single source like REACLIB.
Defines classes for representing and managing nuclear reactions.
Holds the seven coefficients for the REACLIB rate equation.
size_t operator()(const gridfire::reaction::LogicalReactionSet &s) const noexcept
size_t operator()(const gridfire::reaction::Reaction &r) const noexcept
size_t operator()(const gridfire::reaction::ReactionSet &s) const noexcept