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,
54 if (reactant == species) {
63 if (product == species) {
73 rs.insert(ps.begin(), ps.end());
78 std::unordered_set<Species> reactantsSet;
80 reactantsSet.insert(reactant);
86 std::unordered_set<Species> productsSet;
88 productsSet.insert(product);
96 if (reactant == species) {
101 if (product == species) {
113 std::unordered_map<Species, int> stoichiometryMap;
115 stoichiometryMap[reactant]--;
118 stoichiometryMap[product]++;
120 return stoichiometryMap;
124 double reactantMass = 0.0;
125 double productMass = 0.0;
126 constexpr double AMU2MeV = 931.494893;
128 reactantMass += reactant.mass();
131 productMass += product.mass();
133 return (reactantMass - productMass) * AMU2MeV;
137 return XXHash64::hash(
m_id.data(),
m_id.size(), seed);
141 std::vector<Reaction> reactions
156 for (
const auto& reaction_ptr: other.
m_reactions) {
167 if (
this != &other) {
227 if (r.contains_reactant(species)) {
236 if (r.contains_product(species)) {
246 throw std::out_of_range(
"Index" + std::to_string(index) +
" out of range for ReactionSet of size " + std::to_string(
m_reactions.size()) +
".");
256 throw std::out_of_range(
"Species " + std::string(
id) +
" does not exist in ReactionSet.");
267 return !(*
this == other);
272 return XXHash64::hash(
nullptr, 0, seed);
274 std::vector<uint64_t> individualReactionHashes;
275 individualReactionHashes.reserve(
m_reactions.size());
277 individualReactionHashes.push_back(
reaction.hash(seed));
280 std::ranges::sort(individualReactionHashes);
282 const void* data =
static_cast<const void*
>(individualReactionHashes.data());
283 size_t sizeInBytes = individualReactionHashes.size() *
sizeof(uint64_t);
284 return XXHash64::hash(data, sizeInBytes, seed);
305 "LogicalReaction constructed with reactions having different Q-values. Expected {} got {}.",
310 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)) +
").");
319 LOG_ERROR(
m_logger,
"Cannot add reaction with different peName to LogicalReaction. Expected {} got {}.",
m_id,
reaction.peName());
321 throw std::runtime_error(
"Cannot add reaction with different peName to LogicalReaction. Expected " + std::string(
m_id) +
" got " + std::string(
reaction.peName()) +
".");
324 if (source ==
reaction.sourceLabel()) {
325 LOG_ERROR(
m_logger,
"Cannot add reaction with duplicate source label {} to LogicalReaction.",
reaction.sourceLabel());
327 throw std::runtime_error(
"Cannot add reaction with duplicate source label " + std::string(
reaction.sourceLabel()) +
" to LogicalReaction.");
331 LOG_ERROR(
m_logger,
"LogicalReaction constructed with reactions having different Q-values. Expected {} got {}.",
m_qValue,
reaction.qValue());
333 throw std::runtime_error(
"LogicalReaction constructed with reactions having different Q-values. Expected " + std::to_string(
m_qValue) +
" got " + std::to_string(
reaction.qValue()) +
".");
350 std::unordered_map<std::string_view, std::vector<Reaction>> grouped_reactions;
352 for (
const auto&
reaction : reactionSet) {
357 for (
const auto &reactions_for_peName: grouped_reactions | std::views::values) {
360 m_reactions.push_back(std::move(logical_reaction));
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.
std::vector< LogicalReaction > m_reactions
std::unordered_map< std::string, LogicalReaction > m_reactionNameMap
Maps reaction IDs to LogicalReaction objects for quick lookup.
LogicalReactionSet()=delete
Deleted default constructor.
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.
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.
A collection of Reaction objects.
virtual void remove_reaction(const Reaction &reaction)
Removes a reaction from the set.
bool contains_reactant(const fourdst::atomic::Species &species) const
Checks if any reaction in the set contains the given species as a reactant.
uint64_t hash(uint64_t seed=0) const
Computes a hash for the entire set.
virtual const Reaction & operator[](size_t index) const
Accesses a reaction by its index.
bool contains_product(const fourdst::atomic::Species &species) const
Checks if any reaction in the set contains the given species as a product.
bool operator==(const ReactionSet &other) const
Compares this set with another for equality.
bool operator!=(const ReactionSet &other) const
Compares this set with another for inequality.
bool contains_species(const fourdst::atomic::Species &species) const
Checks if any reaction in the set involves the given species.
virtual size_t size() const
Gets the number of reactions in the set.
void clear()
Removes all reactions from the set.
std::vector< Reaction > m_reactions
ReactionSet & operator=(const ReactionSet &other)
Copy assignment operator.
virtual void add_reaction(Reaction reaction)
Adds a reaction to the set.
std::unordered_map< std::string, Reaction > m_reactionNameMap
Maps reaction IDs to Reaction objects for quick lookup.
bool contains(const std::string_view &id) const
Checks if the set contains a reaction with the given ID.
ReactionSet(std::vector< Reaction > reactions)
Constructs a ReactionSet from a vector of reactions.
Defines classes for representing and managing nuclear reactions.
Holds the seven coefficients for the REACLIB rate equation.
size_t operator()(const gridfire::reaction::Reaction &r) const noexcept
size_t operator()(const gridfire::reaction::ReactionSet &s) const noexcept