GridFire 0.0.1a
General Purpose Nuclear Network
Loading...
Searching...
No Matches
reaction.cpp
Go to the documentation of this file.
2
3#include<string_view>
4#include<string>
5#include<vector>
6#include<unordered_set>
7#include<algorithm>
8#include <ranges>
9
10#include "quill/LogMacros.h"
11
12#include "fourdst/composition/atomicSpecies.h"
13
14#include "xxhash64.h"
15
16namespace gridfire::reaction {
17 using namespace fourdst::atomic;
18
20 const std::string_view id,
21 const std::string_view peName,
22 const int chapter,
23 const std::vector<Species>& reactants,
24 const std::vector<Species>& products,
25 const double qValue,
26 const std::string_view label,
27 const RateCoefficientSet& sets,
28 const bool reverse) :
29 m_id(id),
35 m_sourceLabel(label),
37 m_reverse(reverse) {}
38
39 double Reaction::calculate_rate(const double T9) const {
40 return calculate_rate<double>(T9);
41 }
42
43 CppAD::AD<double> Reaction::calculate_rate(const CppAD::AD<double> T9) const {
45 }
46
47 bool Reaction::contains(const Species &species) const {
48 return contains_reactant(species) || contains_product(species);
49 }
50
51
52 bool Reaction::contains_reactant(const Species& species) const {
53 for (const auto& reactant : m_reactants) {
54 if (reactant == species) {
55 return true;
56 }
57 }
58 return false;
59 }
60
61 bool Reaction::contains_product(const Species& species) const {
62 for (const auto& product : m_products) {
63 if (product == species) {
64 return true;
65 }
66 }
67 return false;
68 }
69
70 std::unordered_set<Species> Reaction::all_species() const {
71 auto rs = reactant_species();
72 auto ps = product_species();
73 rs.insert(ps.begin(), ps.end());
74 return rs;
75 }
76
77 std::unordered_set<Species> Reaction::reactant_species() const {
78 std::unordered_set<Species> reactantsSet;
79 for (const auto& reactant : m_reactants) {
80 reactantsSet.insert(reactant);
81 }
82 return reactantsSet;
83 }
84
85 std::unordered_set<Species> Reaction::product_species() const {
86 std::unordered_set<Species> productsSet;
87 for (const auto& product : m_products) {
88 productsSet.insert(product);
89 }
90 return productsSet;
91 }
92
93 int Reaction::stoichiometry(const Species& species) const {
94 int s = 0;
95 for (const auto& reactant : m_reactants) {
96 if (reactant == species) {
97 s--;
98 }
99 }
100 for (const auto& product : m_products) {
101 if (product == species) {
102 s++;
103 }
104 }
105 return s;
106 }
107
108 size_t Reaction::num_species() const {
109 return all_species().size();
110 }
111
112 std::unordered_map<Species, int> Reaction::stoichiometry() const {
113 std::unordered_map<Species, int> stoichiometryMap;
114 for (const auto& reactant : m_reactants) {
115 stoichiometryMap[reactant]--;
116 }
117 for (const auto& product : m_products) {
118 stoichiometryMap[product]++;
119 }
120 return stoichiometryMap;
121 }
122
123 double Reaction::excess_energy() const {
124 double reactantMass = 0.0;
125 double productMass = 0.0;
126 constexpr double AMU2MeV = 931.494893; // Conversion factor from atomic mass unit to MeV
127 for (const auto& reactant : m_reactants) {
128 reactantMass += reactant.mass();
129 }
130 for (const auto& product : m_products) {
131 productMass += product.mass();
132 }
133 return (reactantMass - productMass) * AMU2MeV;
134 }
135
136 uint64_t Reaction::hash(uint64_t seed) const {
137 return XXHash64::hash(m_id.data(), m_id.size(), seed);
138 }
139
140
141
142 LogicalReaction::LogicalReaction(const std::vector<Reaction>& reactants) :
143 Reaction(reactants.front().peName(),
144 reactants.front().peName(),
145 reactants.front().chapter(),
146 reactants.front().reactants(),
147 reactants.front().products(),
148 reactants.front().qValue(),
149 reactants.front().sourceLabel(),
150 reactants.front().rateCoefficients(),
151 reactants.front().is_reverse()) {
152
153 m_sources.reserve(reactants.size());
154 m_rates.reserve(reactants.size());
155 for (const auto& reaction : reactants) {
156 if (std::abs(std::abs(reaction.qValue()) - std::abs(m_qValue)) > 1e-6) {
157 LOG_ERROR(
158 m_logger,
159 "LogicalReaction constructed with reactions having different Q-values. Expected {} got {}.",
160 m_qValue,
161 reaction.qValue()
162 );
163 m_logger -> flush_log();
164 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)) + ").");
165 }
166 m_sources.push_back(std::string(reaction.sourceLabel()));
167 m_rates.push_back(reaction.rateCoefficients());
168 }
169 }
170
172 if (reaction.peName() != m_id) {
173 LOG_ERROR(m_logger, "Cannot add reaction with different peName to LogicalReaction. Expected {} got {}.", m_id, reaction.peName());
174 m_logger -> flush_log();
175 throw std::runtime_error("Cannot add reaction with different peName to LogicalReaction. Expected " + std::string(m_id) + " got " + std::string(reaction.peName()) + ".");
176 }
177 for (const auto& source : m_sources) {
178 if (source == reaction.sourceLabel()) {
179 LOG_ERROR(m_logger, "Cannot add reaction with duplicate source label {} to LogicalReaction.", reaction.sourceLabel());
180 m_logger -> flush_log();
181 throw std::runtime_error("Cannot add reaction with duplicate source label " + std::string(reaction.sourceLabel()) + " to LogicalReaction.");
182 }
183 }
184 if (std::abs(reaction.qValue() - m_qValue) > 1e-6) {
185 LOG_ERROR(m_logger, "LogicalReaction constructed with reactions having different Q-values. Expected {} got {}.", m_qValue, reaction.qValue());
186 m_logger -> flush_log();
187 throw std::runtime_error("LogicalReaction constructed with reactions having different Q-values. Expected " + std::to_string(m_qValue) + " got " + std::to_string(reaction.qValue()) + ".");
188 }
189 m_sources.push_back(std::string(reaction.sourceLabel()));
190 m_rates.push_back(reaction.rateCoefficients());
191 }
192
193 double LogicalReaction::calculate_rate(const double T9) const {
194 return calculate_rate<double>(T9);
195 }
196
197 CppAD::AD<double> LogicalReaction::calculate_rate(const CppAD::AD<double> T9) const {
199 }
200
202 std::unordered_map<std::string_view, std::vector<Reaction>> groupedReactions;
203
204 for (const auto& reaction: reactionSet) {
205 groupedReactions[reaction.peName()].push_back(reaction);
206 }
207
208 std::vector<LogicalReaction> reactions;
209 reactions.reserve(groupedReactions.size());
210
211 for (const auto &reactionsGroup: groupedReactions | std::views::values) {
212 LogicalReaction logicalReaction(reactionsGroup);
213 reactions.push_back(logicalReaction);
214 }
215 return LogicalReactionSet(std::move(reactions));
216 }
217}
218
219namespace std {
220 template<>
221 struct hash<gridfire::reaction::Reaction> {
222 size_t operator()(const gridfire::reaction::Reaction& r) const noexcept {
223 return r.hash(0);
224 }
225 };
226
227 template<>
228 struct hash<gridfire::reaction::ReactionSet> {
229 size_t operator()(const gridfire::reaction::ReactionSet& s) const noexcept {
230 return s.hash(0);
231 }
232 };
233
234 template<>
235 struct hash<gridfire::reaction::LogicalReactionSet> {
236 size_t operator()(const gridfire::reaction::LogicalReactionSet& s) const noexcept {
237 return s.hash(0);
238 }
239 };
240} // namespace std
Represents a "logical" reaction that aggregates rates from multiple sources.
Definition reaction.h:308
void add_reaction(const Reaction &reaction)
Adds another Reaction source to this logical reaction.
Definition reaction.cpp:171
double calculate_rate(const double T9) const override
Calculates the total reaction rate by summing all source rates.
Definition reaction.cpp:193
LogicalReaction(const std::vector< Reaction > &reactions)
Constructs a LogicalReaction from a vector of Reaction objects.
Definition reaction.cpp:142
std::vector< std::string > m_sources
List of source labels.
Definition reaction.h:368
std::vector< RateCoefficientSet > m_rates
List of rate coefficient sets from each source.
Definition reaction.h:369
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
std::string m_sourceLabel
Source label for the rate data (e.g., "wc12w", "st08").
Definition reaction.h:268
std::unordered_set< fourdst::atomic::Species > product_species() const
Gets a set of all unique product species.
Definition reaction.cpp:85
bool contains_product(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a product.
Definition reaction.cpp:61
std::string_view id() const
Gets the unique identifier of the reaction.
Definition reaction.h:202
bool m_reverse
Flag indicating if this is a reverse reaction rate.
Definition reaction.h:270
const std::vector< fourdst::atomic::Species > & reactants() const
Gets the vector of reactant species.
Definition reaction.h:214
int m_chapter
Chapter number from the REACLIB database, defining the reaction structure.
Definition reaction.h:264
size_t num_species() const
Gets the number of unique species involved in the reaction.
Definition reaction.cpp:108
std::string_view sourceLabel() const
Gets the source label for the rate data.
Definition reaction.h:132
std::vector< fourdst::atomic::Species > m_products
Products of the reaction.
Definition reaction.h:267
double m_qValue
Q-value of the reaction in MeV.
Definition reaction.h:265
std::string m_id
Unique identifier for the reaction (e.g., "h1+h1=>h2+e+nu").
Definition reaction.h:262
int chapter() const
Gets the REACLIB chapter number.
Definition reaction.h:126
std::string m_peName
Name of the reaction in (projectile, ejectile) notation (e.g. "p(p,g)d").
Definition reaction.h:263
const std::vector< fourdst::atomic::Species > & products() const
Gets the vector of product species.
Definition reaction.h:220
quill::Logger * m_logger
Definition reaction.h:261
virtual std::string_view peName() const
Gets the reaction name in (projectile, ejectile) notation.
Definition reaction.h:120
std::unordered_set< fourdst::atomic::Species > all_species() const
Gets a set of all unique species involved in the reaction.
Definition reaction.cpp:70
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.
Definition reaction.cpp:19
std::unordered_set< fourdst::atomic::Species > reactant_species() const
Gets a set of all unique reactant species.
Definition reaction.cpp:77
const RateCoefficientSet & rateCoefficients() const
Gets the set of rate coefficients.
Definition reaction.h:138
std::vector< fourdst::atomic::Species > m_reactants
Reactants of the reaction.
Definition reaction.h:266
double excess_energy() const
Calculates the excess energy from the mass difference of reactants and products.
Definition reaction.cpp:123
RateCoefficientSet m_rateCoefficients
The seven rate coefficients.
Definition reaction.h:269
bool is_reverse() const
Checks if this is a reverse reaction rate.
Definition reaction.h:226
bool contains(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a reactant or product.
Definition reaction.cpp:47
bool contains_reactant(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a reactant.
Definition reaction.cpp:52
double qValue() const
Gets the Q-value of the reaction.
Definition reaction.h:208
std::unordered_map< fourdst::atomic::Species, int > stoichiometry() const
Gets a map of all species to their stoichiometric coefficients.
Definition reaction.cpp:112
virtual double calculate_rate(const double T9) const
Calculates the reaction rate for a given temperature.
Definition reaction.cpp:39
uint64_t hash(uint64_t seed=0) const
Computes a hash for the reaction based on its ID.
Definition reaction.cpp:136
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:557
LogicalReactionSet packReactionSetToLogicalReactionSet(const ReactionSet &reactionSet)
Definition reaction.cpp:201
TemplatedReactionSet< Reaction > ReactionSet
A set of reactions, typically from a single source like REACLIB.
Definition reaction.h:556
STL namespace.
Defines classes for representing and managing nuclear reactions.
Holds the seven coefficients for the REACLIB rate equation.
Definition reaction.h:33
size_t operator()(const gridfire::reaction::LogicalReactionSet &s) const noexcept
Definition reaction.cpp:236
size_t operator()(const gridfire::reaction::Reaction &r) const noexcept
Definition reaction.cpp:222
size_t operator()(const gridfire::reaction::ReactionSet &s) const noexcept
Definition reaction.cpp:229