5#include "fourdst/composition/atomicSpecies.h"
6#include "fourdst/logging/logging.h"
7#include "quill/Logger.h"
8#include <unordered_map>
10#include <unordered_set>
13#include "cppad/cppad.hpp"
49 os <<
"[" << r.
a0 <<
", " << r.
a1 <<
", " << r.
a2 <<
", "
50 << r.
a3 <<
", " << r.
a4 <<
", " << r.
a5 <<
", " << r.
a6 <<
"]";
92 const std::string_view
id,
93 const std::string_view
peName,
95 const std::vector<fourdst::atomic::Species> &
reactants,
96 const std::vector<fourdst::atomic::Species> &
products,
98 const std::string_view label,
100 const bool reverse =
false);
107 [[nodiscard]]
virtual double calculate_rate(
const double T9)
const;
114 [[nodiscard]]
virtual CppAD::AD<double>
calculate_rate(
const CppAD::AD<double> T9)
const;
145 [[nodiscard]]
bool contains(
const fourdst::atomic::Species& species)
const;
152 [[nodiscard]]
bool contains_reactant(
const fourdst::atomic::Species& species)
const;
159 [[nodiscard]]
bool contains_product(
const fourdst::atomic::Species& species)
const;
165 [[nodiscard]] std::unordered_set<fourdst::atomic::Species>
all_species()
const;
171 [[nodiscard]] std::unordered_set<fourdst::atomic::Species>
reactant_species()
const;
177 [[nodiscard]] std::unordered_set<fourdst::atomic::Species>
product_species()
const;
190 [[nodiscard]]
int stoichiometry(
const fourdst::atomic::Species& species)
const;
196 [[nodiscard]] std::unordered_map<fourdst::atomic::Species, int>
stoichiometry()
const;
202 [[nodiscard]] std::string_view
id()
const {
return m_id; }
254 [[nodiscard]] uint64_t
hash(uint64_t seed = 0)
const;
257 return os <<
"(Reaction:" << r.
m_id <<
")";
261 quill::Logger*
m_logger = fourdst::logging::LogManager::getInstance().getLogger(
"log");
280 template <
typename T>
282 const T T913 = CppAD::pow(T9, 1.0/3.0);
283 const T T953 = CppAD::pow(T9, 5.0/3.0);
284 const T logT9 = CppAD::log(T9);
292 return CppAD::exp(exponent);
342 [[nodiscard]]
double calculate_rate(
const double T9)
const override;
349 [[nodiscard]] CppAD::AD<double>
calculate_rate(
const CppAD::AD<double> T9)
const override;
363 os <<
"(LogicalReaction: " << r.
id() <<
", reverse: " << r.
is_reverse() <<
")";
380 template <
typename T>
382 T sum =
static_cast<T
>(0.0);
383 const T T913 = CppAD::pow(T9, 1.0/3.0);
384 const T T953 = CppAD::pow(T9, 5.0/3.0);
385 const T logT9 = CppAD::log(T9);
387 for (
const auto& rate :
m_rates) {
388 const T exponent = rate.a0 +
395 sum += CppAD::exp(exponent);
401 template <
typename ReactionT>
440 [[nodiscard]]
bool contains(
const std::string_view&
id)
const;
487 [[nodiscard]]
const ReactionT&
operator[](
size_t index)
const;
495 [[nodiscard]]
const ReactionT&
operator[](
const std::string_view&
id)
const;
519 [[nodiscard]] uint64_t
hash(uint64_t seed = 0)
const;
532 os <<
"(ReactionSet: [";
549 quill::Logger*
m_logger = fourdst::logging::LogManager::getInstance().getLogger(
"log");
561 template <
typename ReactionT>
563 std::vector<ReactionT> reactions
576 template <
typename ReactionT>
579 for (
const auto& reaction_ptr: other.
m_reactions) {
589 template <
typename ReactionT>
591 if (
this != &other) {
599 template <
typename ReactionT>
606 template <
typename ReactionT>
619 template <
typename ReactionT>
629 template <
typename ReactionT>
639 template <
typename ReactionT>
645 template <
typename ReactionT>
655 template <
typename ReactionT>
658 if (r.contains_reactant(species)) {
665 template <
typename ReactionT>
668 if (r.contains_product(species)) {
675 template <
typename ReactionT>
679 throw std::out_of_range(
"Index" + std::to_string(index) +
" out of range for ReactionSet of size " + std::to_string(
m_reactions.size()) +
".");
684 template <
typename ReactionT>
690 throw std::out_of_range(
"Species " + std::string(
id) +
" does not exist in ReactionSet.");
693 template <
typename ReactionT>
701 template <
typename ReactionT>
703 return !(*
this == other);
706 template <
typename ReactionT>
709 return XXHash64::hash(
nullptr, 0, seed);
711 std::vector<uint64_t> individualReactionHashes;
712 individualReactionHashes.reserve(
m_reactions.size());
714 individualReactionHashes.push_back(
reaction.hash(seed));
717 std::ranges::sort(individualReactionHashes);
719 const auto data =
static_cast<const void*
>(individualReactionHashes.data());
720 const size_t sizeInBytes = individualReactionHashes.size() *
sizeof(uint64_t);
721 return XXHash64::hash(data, sizeInBytes, seed);
724 template<
typename ReactionT>
726 std::unordered_set<fourdst::atomic::Species> species;
728 const auto reactionSpecies =
reaction.all_species();
729 species.insert(reactionSpecies.begin(), reactionSpecies.end());
T calculate_rate(const T T9) const
Template implementation for calculating the total reaction rate.
friend std::ostream & operator<<(std::ostream &os, const LogicalReaction &r)
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< std::string > sources() const
Gets the list of source labels for the aggregated rates.
size_t size() const
Gets the number of source rates contributing to this logical reaction.
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.
friend std::ostream & operator<<(std::ostream &os, const Reaction &r)
bool operator!=(const Reaction &other) const
Compares this reaction with another for inequality.
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").
T calculate_rate(const T T9) const
Template implementation for calculating the reaction rate.
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.
int stoichiometry(const fourdst::atomic::Species &species) const
Calculates the stoichiometric coefficient for a given species.
virtual ~Reaction()=default
Virtual destructor.
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.
bool operator==(const Reaction &other) const
Compares this reaction with another for equality based on their IDs.
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.
void clear()
Removes all reactions from the set.
bool operator==(const TemplatedReactionSet &other) const
Compares this set with another for equality.
const ReactionT & operator[](const std::string_view &id) const
Accesses a reaction by its ID.
std::unordered_set< fourdst::atomic::Species > getReactionSetSpecies() const
uint64_t hash(uint64_t seed=0) const
Computes a hash for the entire set.
void add_reaction(ReactionT reaction)
Adds a reaction to the set.
std::unordered_map< std::string, Reaction > m_reactionNameMap
bool contains_product(const fourdst::atomic::Species &species) const
Checks if any reaction in the set contains the given species as a product.
friend std::ostream & operator<<(std::ostream &os, const TemplatedReactionSet< ReactionT > &r)
TemplatedReactionSet(std::vector< ReactionT > reactions)
Constructs a ReactionSet from a vector of reactions.
std::vector< Reaction > m_reactions
const ReactionT & operator[](size_t index) const
Accesses a reaction by its index.
size_t size() const
Gets the number of reactions in the set.
bool contains(const std::string_view &id) const
Checks if the set contains a reaction with the given ID.
void remove_reaction(const ReactionT &reaction)
Removes a reaction from the set.
bool operator!=(const TemplatedReactionSet &other) const
Compares this set with another for inequality.
bool contains(const Reaction &reaction) const
Checks if the set contains the given reaction.
bool contains_reactant(const fourdst::atomic::Species &species) const
Checks if any reaction in the set contains the given species as a reactant.
TemplatedReactionSet< ReactionT > & operator=(const TemplatedReactionSet< ReactionT > &other)
Copy assignment operator.
bool contains_species(const fourdst::atomic::Species &species) const
Checks if any reaction in the set involves the given species.
TemplatedReactionSet(const TemplatedReactionSet< ReactionT > &other)
Copy constructor.
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.
Holds the seven coefficients for the REACLIB rate equation.
friend std::ostream & operator<<(std::ostream &os, const RateCoefficientSet &r)
Overloads the stream insertion operator for easy printing.