GridFire 0.0.1a
General Purpose Nuclear Network
Loading...
Searching...
No Matches
reaction.h
Go to the documentation of this file.
1#pragma once
2
3#include <string_view>
4
5#include "fourdst/composition/atomicSpecies.h"
6#include "fourdst/logging/logging.h"
7#include "quill/Logger.h"
8#include <unordered_map>
9#include <vector>
10#include <unordered_set>
11
12
13#include "cppad/cppad.hpp"
14#include "xxhash64.h"
15
34 double a0;
35 double a1;
36 double a2;
37 double a3;
38 double a4;
39 double a5;
40 double a6;
41
48 friend std::ostream& operator<<(std::ostream& os, const RateCoefficientSet& r) {
49 os << "[" << r.a0 << ", " << r.a1 << ", " << r.a2 << ", "
50 << r.a3 << ", " << r.a4 << ", " << r.a5 << ", " << r.a6 << "]";
51 return os;
52 }
53 };
54
72 class Reaction {
73 public:
77 virtual ~Reaction() = default;
78
92 const std::string_view id,
93 const std::string_view peName,
94 const int chapter,
95 const std::vector<fourdst::atomic::Species> &reactants,
96 const std::vector<fourdst::atomic::Species> &products,
97 const double qValue,
98 const std::string_view label,
99 const RateCoefficientSet &sets,
100 const bool reverse = false);
101
107 [[nodiscard]] virtual double calculate_rate(const double T9) const;
108
114 [[nodiscard]] virtual CppAD::AD<double> calculate_rate(const CppAD::AD<double> T9) const;
115
120 [[nodiscard]] virtual std::string_view peName() const { return m_peName; }
121
126 [[nodiscard]] int chapter() const { return m_chapter; }
127
132 [[nodiscard]] std::string_view sourceLabel() const { return m_sourceLabel; }
133
138 [[nodiscard]] const RateCoefficientSet& rateCoefficients() const { return m_rateCoefficients; }
139
145 [[nodiscard]] bool contains(const fourdst::atomic::Species& species) const;
146
152 [[nodiscard]] bool contains_reactant(const fourdst::atomic::Species& species) const;
153
159 [[nodiscard]] bool contains_product(const fourdst::atomic::Species& species) const;
160
165 [[nodiscard]] std::unordered_set<fourdst::atomic::Species> all_species() const;
166
171 [[nodiscard]] std::unordered_set<fourdst::atomic::Species> reactant_species() const;
172
177 [[nodiscard]] std::unordered_set<fourdst::atomic::Species> product_species() const;
178
183 [[nodiscard]] size_t num_species() const;
184
190 [[nodiscard]] int stoichiometry(const fourdst::atomic::Species& species) const;
191
196 [[nodiscard]] std::unordered_map<fourdst::atomic::Species, int> stoichiometry() const;
197
202 [[nodiscard]] std::string_view id() const { return m_id; }
203
208 [[nodiscard]] double qValue() const { return m_qValue; }
209
214 [[nodiscard]] const std::vector<fourdst::atomic::Species>& reactants() const { return m_reactants; }
215
220 [[nodiscard]] const std::vector<fourdst::atomic::Species>& products() const { return m_products; }
221
226 [[nodiscard]] bool is_reverse() const { return m_reverse; }
227
232 [[nodiscard]] double excess_energy() const;
233
239 bool operator==(const Reaction& other) const { return m_id == other.m_id; }
240
246 bool operator!=(const Reaction& other) const { return !(*this == other); }
247
254 [[nodiscard]] uint64_t hash(uint64_t seed = 0) const;
255
256 friend std::ostream& operator<<(std::ostream& os, const Reaction& r) {
257 return os << "(Reaction:" << r.m_id << ")";
258 }
259
260 protected:
261 quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
262 std::string m_id;
263 std::string m_peName;
265 double m_qValue = 0.0;
266 std::vector<fourdst::atomic::Species> m_reactants;
267 std::vector<fourdst::atomic::Species> m_products;
268 std::string m_sourceLabel;
270 bool m_reverse = false;
271 private:
280 template <typename T>
281 [[nodiscard]] T calculate_rate(const T T9) const {
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);
285 const T exponent = m_rateCoefficients.a0 +
286 m_rateCoefficients.a1 / T9 +
287 m_rateCoefficients.a2 / T913 +
288 m_rateCoefficients.a3 * T913 +
289 m_rateCoefficients.a4 * T9 +
290 m_rateCoefficients.a5 * T953 +
291 m_rateCoefficients.a6 * logT9;
292 return CppAD::exp(exponent);
293 }
294 };
295
296
297
308 class LogicalReaction final : public Reaction {
309 public:
315 explicit LogicalReaction(const std::vector<Reaction> &reactions);
316
323 void add_reaction(const Reaction& reaction);
324
329 [[nodiscard]] size_t size() const { return m_rates.size(); }
330
335 [[nodiscard]] std::vector<std::string> sources() const { return m_sources; }
336
342 [[nodiscard]] double calculate_rate(const double T9) const override;
343
349 [[nodiscard]] CppAD::AD<double> calculate_rate(const CppAD::AD<double> T9) const override;
350
355 auto begin() { return m_rates.begin(); }
356 [[nodiscard]] auto begin() const { return m_rates.cbegin(); }
357 auto end() { return m_rates.end(); }
358 [[nodiscard]] auto end() const { return m_rates.cend(); }
361
362 friend std::ostream& operator<<(std::ostream& os, const LogicalReaction& r) {
363 os << "(LogicalReaction: " << r.id() << ", reverse: " << r.is_reverse() << ")";
364 return os;
365 }
366
367 private:
368 std::vector<std::string> m_sources;
369 std::vector<RateCoefficientSet> m_rates;
370
371 private:
380 template <typename T>
381 [[nodiscard]] T calculate_rate(const T T9) const {
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);
386 // ReSharper disable once CppUseStructuredBinding
387 for (const auto& rate : m_rates) {
388 const T exponent = rate.a0 +
389 rate.a1 / T9 +
390 rate.a2 / T913 +
391 rate.a3 * T913 +
392 rate.a4 * T9 +
393 rate.a5 * T953 +
394 rate.a6 * logT9;
395 sum += CppAD::exp(exponent);
396 }
397 return sum;
398 }
399 };
400
401 template <typename ReactionT>
403 public:
408 explicit TemplatedReactionSet(std::vector<ReactionT> reactions);
409
415
422
427 void add_reaction(ReactionT reaction);
428
433 void remove_reaction(const ReactionT& reaction);
434
440 [[nodiscard]] bool contains(const std::string_view& id) const;
441
447 [[nodiscard]] bool contains(const Reaction& reaction) const;
448
453 [[nodiscard]] size_t size() const { return m_reactions.size(); }
454
458 void clear();
459
465 [[nodiscard]] bool contains_species(const fourdst::atomic::Species& species) const;
466
472 [[nodiscard]] bool contains_reactant(const fourdst::atomic::Species& species) const;
473
479 [[nodiscard]] bool contains_product(const fourdst::atomic::Species& species) const;
480
487 [[nodiscard]] const ReactionT& operator[](size_t index) const;
488
495 [[nodiscard]] const ReactionT& operator[](const std::string_view& id) const;
496
502 bool operator==(const TemplatedReactionSet& other) const;
503
509 bool operator!=(const TemplatedReactionSet& other) const;
510
519 [[nodiscard]] uint64_t hash(uint64_t seed = 0) const;
520
525 auto begin() { return m_reactions.begin(); }
526 [[nodiscard]] auto begin() const { return m_reactions.cbegin(); }
527 auto end() { return m_reactions.end(); }
528 [[nodiscard]] auto end() const { return m_reactions.cend(); }
531 friend std::ostream& operator<<(std::ostream& os, const TemplatedReactionSet<ReactionT>& r) {
532 os << "(ReactionSet: [";
533 int counter = 0;
534 for (const auto& reaction : r.m_reactions) {
535 os << reaction;
536 if (counter < r.m_reactions.size() - 2) {
537 os << ", ";
538 } else if (counter == r.m_reactions.size() - 2) {
539 os << " and ";
540 }
541 ++counter;
542 }
543 os << "])";
544 return os;
545 }
546
547 [[nodiscard]] std::unordered_set<fourdst::atomic::Species> getReactionSetSpecies() const;
548 private:
549 quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
550 std::vector<ReactionT> m_reactions;
551 std::string m_id;
552 std::unordered_map<std::string, ReactionT> m_reactionNameMap;
553
554 };
555
558
560
561 template <typename ReactionT>
563 std::vector<ReactionT> reactions
564 ) :
565 m_reactions(std::move(reactions)) {
566 if (m_reactions.empty()) {
567 return; // Case where the reactions will be added later.
568 }
569 m_reactionNameMap.reserve(reactions.size());
570 for (const auto& reaction : m_reactions) {
571 m_id += reaction.id();
572 m_reactionNameMap.emplace(reaction.id(), reaction);
573 }
574 }
575
576 template <typename ReactionT>
578 m_reactions.reserve(other.m_reactions.size());
579 for (const auto& reaction_ptr: other.m_reactions) {
580 m_reactions.push_back(reaction_ptr);
581 }
582
583 m_reactionNameMap.reserve(other.m_reactionNameMap.size());
584 for (const auto& reaction_ptr : m_reactions) {
585 m_reactionNameMap.emplace(reaction_ptr.id(), reaction_ptr);
586 }
587 }
588
589 template <typename ReactionT>
591 if (this != &other) {
592 TemplatedReactionSet temp(other);
593 std::swap(m_reactions, temp.m_reactions);
594 std::swap(m_reactionNameMap, temp.m_reactionNameMap);
595 }
596 return *this;
597 }
598
599 template <typename ReactionT>
601 m_reactions.emplace_back(reaction);
602 m_id += m_reactions.back().id();
603 m_reactionNameMap.emplace(m_reactions.back().id(), m_reactions.back());
604 }
605
606 template <typename ReactionT>
608 if (!m_reactionNameMap.contains(std::string(reaction.id()))) {
609 return;
610 }
611
612 m_reactionNameMap.erase(std::string(reaction.id()));
613
614 std::erase_if(m_reactions, [&reaction](const Reaction& r) {
615 return r == reaction;
616 });
617 }
618
619 template <typename ReactionT>
620 bool TemplatedReactionSet<ReactionT>::contains(const std::string_view& id) const {
621 for (const auto& reaction : m_reactions) {
622 if (reaction.id() == id) {
623 return true;
624 }
625 }
626 return false;
627 }
628
629 template <typename ReactionT>
631 for (const auto& r : m_reactions) {
632 if (r == reaction) {
633 return true;
634 }
635 }
636 return false;
637 }
638
639 template <typename ReactionT>
644
645 template <typename ReactionT>
646 bool TemplatedReactionSet<ReactionT>::contains_species(const fourdst::atomic::Species& species) const {
647 for (const auto& reaction : m_reactions) {
648 if (reaction.contains(species)) {
649 return true;
650 }
651 }
652 return false;
653 }
654
655 template <typename ReactionT>
656 bool TemplatedReactionSet<ReactionT>::contains_reactant(const fourdst::atomic::Species& species) const {
657 for (const auto& r : m_reactions) {
658 if (r.contains_reactant(species)) {
659 return true;
660 }
661 }
662 return false;
663 }
664
665 template <typename ReactionT>
666 bool TemplatedReactionSet<ReactionT>::contains_product(const fourdst::atomic::Species& species) const {
667 for (const auto& r : m_reactions) {
668 if (r.contains_product(species)) {
669 return true;
670 }
671 }
672 return false;
673 }
674
675 template <typename ReactionT>
676 const ReactionT& TemplatedReactionSet<ReactionT>::operator[](const size_t index) const {
677 if (index >= m_reactions.size()) {
678 m_logger -> flush_log();
679 throw std::out_of_range("Index" + std::to_string(index) + " out of range for ReactionSet of size " + std::to_string(m_reactions.size()) + ".");
680 }
681 return m_reactions[index];
682 }
683
684 template <typename ReactionT>
685 const ReactionT& TemplatedReactionSet<ReactionT>::operator[](const std::string_view& id) const {
686 if (auto it = m_reactionNameMap.find(std::string(id)); it != m_reactionNameMap.end()) {
687 return it->second;
688 }
689 m_logger -> flush_log();
690 throw std::out_of_range("Species " + std::string(id) + " does not exist in ReactionSet.");
691 }
692
693 template <typename ReactionT>
695 if (size() != other.size()) {
696 return false;
697 }
698 return hash() == other.hash();
699 }
700
701 template <typename ReactionT>
703 return !(*this == other);
704 }
705
706 template <typename ReactionT>
707 uint64_t TemplatedReactionSet<ReactionT>::hash(uint64_t seed) const {
708 if (m_reactions.empty()) {
709 return XXHash64::hash(nullptr, 0, seed);
710 }
711 std::vector<uint64_t> individualReactionHashes;
712 individualReactionHashes.reserve(m_reactions.size());
713 for (const auto& reaction : m_reactions) {
714 individualReactionHashes.push_back(reaction.hash(seed));
715 }
716
717 std::ranges::sort(individualReactionHashes);
718
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);
722 }
723
724 template<typename ReactionT>
725 std::unordered_set<fourdst::atomic::Species> TemplatedReactionSet<ReactionT>::getReactionSetSpecies() const {
726 std::unordered_set<fourdst::atomic::Species> species;
727 for (const auto& reaction : m_reactions) {
728 const auto reactionSpecies = reaction.all_species();
729 species.insert(reactionSpecies.begin(), reactionSpecies.end());
730 }
731 return species;
732 }
733}
734
T calculate_rate(const T T9) const
Template implementation for calculating the total reaction rate.
Definition reaction.h:381
friend std::ostream & operator<<(std::ostream &os, const LogicalReaction &r)
Definition reaction.h:362
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
std::vector< std::string > sources() const
Gets the list of source labels for the aggregated rates.
Definition reaction.h:335
size_t size() const
Gets the number of source rates contributing to this logical reaction.
Definition reaction.h:329
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
friend std::ostream & operator<<(std::ostream &os, const Reaction &r)
Definition reaction.h:256
bool operator!=(const Reaction &other) const
Compares this reaction with another for inequality.
Definition reaction.h:246
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
T calculate_rate(const T T9) const
Template implementation for calculating the reaction rate.
Definition reaction.h:281
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
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.
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
bool operator==(const Reaction &other) const
Compares this reaction with another for equality based on their IDs.
Definition reaction.h:239
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
void clear()
Removes all reactions from the set.
Definition reaction.h:640
bool operator==(const TemplatedReactionSet &other) const
Compares this set with another for equality.
Definition reaction.h:694
const ReactionT & operator[](const std::string_view &id) const
Accesses a reaction by its ID.
Definition reaction.h:685
std::unordered_set< fourdst::atomic::Species > getReactionSetSpecies() const
Definition reaction.h:725
uint64_t hash(uint64_t seed=0) const
Computes a hash for the entire set.
Definition reaction.h:707
void add_reaction(ReactionT reaction)
Adds a reaction to the set.
Definition reaction.h:600
std::unordered_map< std::string, Reaction > m_reactionNameMap
Definition reaction.h:552
bool contains_product(const fourdst::atomic::Species &species) const
Checks if any reaction in the set contains the given species as a product.
Definition reaction.h:666
friend std::ostream & operator<<(std::ostream &os, const TemplatedReactionSet< ReactionT > &r)
Definition reaction.h:531
TemplatedReactionSet(std::vector< ReactionT > reactions)
Constructs a ReactionSet from a vector of reactions.
Definition reaction.h:562
const ReactionT & operator[](size_t index) const
Accesses a reaction by its index.
Definition reaction.h:676
size_t size() const
Gets the number of reactions in the set.
Definition reaction.h:453
bool contains(const std::string_view &id) const
Checks if the set contains a reaction with the given ID.
Definition reaction.h:620
void remove_reaction(const ReactionT &reaction)
Removes a reaction from the set.
Definition reaction.h:607
bool operator!=(const TemplatedReactionSet &other) const
Compares this set with another for inequality.
Definition reaction.h:702
bool contains(const Reaction &reaction) const
Checks if the set contains the given reaction.
Definition reaction.h:630
bool contains_reactant(const fourdst::atomic::Species &species) const
Checks if any reaction in the set contains the given species as a reactant.
Definition reaction.h:656
TemplatedReactionSet< ReactionT > & operator=(const TemplatedReactionSet< ReactionT > &other)
Copy assignment operator.
Definition reaction.h:590
bool contains_species(const fourdst::atomic::Species &species) const
Checks if any reaction in the set involves the given species.
Definition reaction.h:646
TemplatedReactionSet(const TemplatedReactionSet< ReactionT > &other)
Copy constructor.
Definition reaction.h:577
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.
Holds the seven coefficients for the REACLIB rate equation.
Definition reaction.h:33
friend std::ostream & operator<<(std::ostream &os, const RateCoefficientSet &r)
Overloads the stream insertion operator for easy printing.
Definition reaction.h:48