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
116 [[nodiscard]] virtual double calculate_forward_rate_log_derivative(const double T9) const;
117
122 [[nodiscard]] virtual std::string_view peName() const { return m_peName; }
123
128 [[nodiscard]] int chapter() const { return m_chapter; }
129
134 [[nodiscard]] std::string_view sourceLabel() const { return m_sourceLabel; }
135
140 [[nodiscard]] const RateCoefficientSet& rateCoefficients() const { return m_rateCoefficients; }
141
147 [[nodiscard]] bool contains(const fourdst::atomic::Species& species) const;
148
154 [[nodiscard]] bool contains_reactant(const fourdst::atomic::Species& species) const;
155
161 [[nodiscard]] bool contains_product(const fourdst::atomic::Species& species) const;
162
167 [[nodiscard]] std::unordered_set<fourdst::atomic::Species> all_species() const;
168
173 [[nodiscard]] std::unordered_set<fourdst::atomic::Species> reactant_species() const;
174
179 [[nodiscard]] std::unordered_set<fourdst::atomic::Species> product_species() const;
180
185 [[nodiscard]] size_t num_species() const;
186
192 [[nodiscard]] int stoichiometry(const fourdst::atomic::Species& species) const;
193
198 [[nodiscard]] std::unordered_map<fourdst::atomic::Species, int> stoichiometry() const;
199
204 [[nodiscard]] std::string_view id() const { return m_id; }
205
210 [[nodiscard]] double qValue() const { return m_qValue; }
211
216 [[nodiscard]] const std::vector<fourdst::atomic::Species>& reactants() const { return m_reactants; }
217
222 [[nodiscard]] const std::vector<fourdst::atomic::Species>& products() const { return m_products; }
223
228 [[nodiscard]] bool is_reverse() const { return m_reverse; }
229
234 [[nodiscard]] double excess_energy() const;
235
241 bool operator==(const Reaction& other) const { return m_id == other.m_id; }
242
248 bool operator!=(const Reaction& other) const { return !(*this == other); }
249
256 [[nodiscard]] uint64_t hash(uint64_t seed = 0) const;
257
258 friend std::ostream& operator<<(std::ostream& os, const Reaction& r) {
259 return os << "(Reaction:" << r.m_id << ")";
260 }
261
262 protected:
263 quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
264 std::string m_id;
265 std::string m_peName;
267 double m_qValue = 0.0;
268 std::vector<fourdst::atomic::Species> m_reactants;
269 std::vector<fourdst::atomic::Species> m_products;
270 std::string m_sourceLabel;
272 bool m_reverse = false;
273 private:
282 template <typename T>
283 [[nodiscard]] T calculate_rate(const T T9) const {
284 const T T913 = CppAD::pow(T9, 1.0/3.0);
285 const T T953 = CppAD::pow(T9, 5.0/3.0);
286 const T logT9 = CppAD::log(T9);
287 const T exponent = m_rateCoefficients.a0 +
288 m_rateCoefficients.a1 / T9 +
289 m_rateCoefficients.a2 / T913 +
290 m_rateCoefficients.a3 * T913 +
291 m_rateCoefficients.a4 * T9 +
292 m_rateCoefficients.a5 * T953 +
293 m_rateCoefficients.a6 * logT9;
294 return CppAD::exp(exponent);
295 }
296 };
297
298
299
310 class LogicalReaction final : public Reaction {
311 public:
317 explicit LogicalReaction(const std::vector<Reaction> &reactions);
318
325 void add_reaction(const Reaction& reaction);
326
331 [[nodiscard]] size_t size() const { return m_rates.size(); }
332
337 [[nodiscard]] std::vector<std::string> sources() const { return m_sources; }
338
344 [[nodiscard]] double calculate_rate(const double T9) const override;
345
346 [[nodiscard]] virtual double calculate_forward_rate_log_derivative(const double T9) const override;
347
353 [[nodiscard]] CppAD::AD<double> calculate_rate(const CppAD::AD<double> T9) const override;
354
359 auto begin() { return m_rates.begin(); }
360 [[nodiscard]] auto begin() const { return m_rates.cbegin(); }
361 auto end() { return m_rates.end(); }
362 [[nodiscard]] auto end() const { return m_rates.cend(); }
365
366 friend std::ostream& operator<<(std::ostream& os, const LogicalReaction& r) {
367 os << "(LogicalReaction: " << r.id() << ", reverse: " << r.is_reverse() << ")";
368 return os;
369 }
370
371 private:
372 std::vector<std::string> m_sources;
373 std::vector<RateCoefficientSet> m_rates;
374
375 private:
384 template <typename T>
385 [[nodiscard]] T calculate_rate(const T T9) const {
386 T sum = static_cast<T>(0.0);
387 const T T913 = CppAD::pow(T9, 1.0/3.0);
388 const T T953 = CppAD::pow(T9, 5.0/3.0);
389 const T logT9 = CppAD::log(T9);
390 // ReSharper disable once CppUseStructuredBinding
391 for (const auto& rate : m_rates) {
392 const T exponent = rate.a0 +
393 rate.a1 / T9 +
394 rate.a2 / T913 +
395 rate.a3 * T913 +
396 rate.a4 * T9 +
397 rate.a5 * T953 +
398 rate.a6 * logT9;
399 sum += CppAD::exp(exponent);
400 }
401 return sum;
402 }
403 };
404
405 template <typename ReactionT>
407 public:
412 explicit TemplatedReactionSet(std::vector<ReactionT> reactions);
413
415
421
428
433 void add_reaction(ReactionT reaction);
434
439 void remove_reaction(const ReactionT& reaction);
440
446 [[nodiscard]] bool contains(const std::string_view& id) const;
447
453 [[nodiscard]] bool contains(const Reaction& reaction) const;
454
459 [[nodiscard]] size_t size() const { return m_reactions.size(); }
460
464 void clear();
465
471 [[nodiscard]] bool contains_species(const fourdst::atomic::Species& species) const;
472
478 [[nodiscard]] bool contains_reactant(const fourdst::atomic::Species& species) const;
479
485 [[nodiscard]] bool contains_product(const fourdst::atomic::Species& species) const;
486
493 [[nodiscard]] const ReactionT& operator[](size_t index) const;
494
501 [[nodiscard]] const ReactionT& operator[](const std::string_view& id) const;
502
508 bool operator==(const TemplatedReactionSet& other) const;
509
515 bool operator!=(const TemplatedReactionSet& other) const;
516
525 [[nodiscard]] uint64_t hash(uint64_t seed = 0) const;
526
531 auto begin() { return m_reactions.begin(); }
532 [[nodiscard]] auto begin() const { return m_reactions.cbegin(); }
533 auto end() { return m_reactions.end(); }
534 [[nodiscard]] auto end() const { return m_reactions.cend(); }
537 friend std::ostream& operator<<(std::ostream& os, const TemplatedReactionSet<ReactionT>& r) {
538 os << "(ReactionSet: [";
539 size_t counter = 0;
540 for (const auto& reaction : r.m_reactions) {
541 os << reaction;
542 if (counter < r.m_reactions.size() - 2) {
543 os << ", ";
544 } else if (counter == r.m_reactions.size() - 2) {
545 os << " and ";
546 }
547 ++counter;
548 }
549 os << "])";
550 return os;
551 }
552
553 [[nodiscard]] std::unordered_set<fourdst::atomic::Species> getReactionSetSpecies() const;
554 private:
555 quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
556 std::vector<ReactionT> m_reactions;
557 std::string m_id;
558 std::unordered_map<std::string, ReactionT> m_reactionNameMap;
559
560 };
561
564
566
567 template <typename ReactionT>
569 std::vector<ReactionT> reactions
570 ) :
571 m_reactions(std::move(reactions)) {
572 if (m_reactions.empty()) {
573 return; // Case where the reactions will be added later.
574 }
575 m_reactionNameMap.reserve(reactions.size());
576 for (const auto& reaction : m_reactions) {
577 m_id += reaction.id();
578 m_reactionNameMap.emplace(reaction.id(), reaction);
579 }
580 }
581
582 template<typename ReactionT>
584
585 template <typename ReactionT>
587 m_reactions.reserve(other.m_reactions.size());
588 for (const auto& reaction_ptr: other.m_reactions) {
589 m_reactions.push_back(reaction_ptr);
590 }
591
592 m_reactionNameMap.reserve(other.m_reactionNameMap.size());
593 for (const auto& reaction_ptr : m_reactions) {
594 m_reactionNameMap.emplace(reaction_ptr.id(), reaction_ptr);
595 }
596 }
597
598 template <typename ReactionT>
600 if (this != &other) {
601 TemplatedReactionSet temp(other);
602 std::swap(m_reactions, temp.m_reactions);
603 std::swap(m_reactionNameMap, temp.m_reactionNameMap);
604 }
605 return *this;
606 }
607
608 template <typename ReactionT>
610 m_reactions.emplace_back(reaction);
611 m_id += m_reactions.back().id();
612 m_reactionNameMap.emplace(m_reactions.back().id(), m_reactions.back());
613 }
614
615 template <typename ReactionT>
617 if (!m_reactionNameMap.contains(std::string(reaction.id()))) {
618 return;
619 }
620
621 m_reactionNameMap.erase(std::string(reaction.id()));
622
623 std::erase_if(m_reactions, [&reaction](const Reaction& r) {
624 return r == reaction;
625 });
626 }
627
628 template <typename ReactionT>
629 bool TemplatedReactionSet<ReactionT>::contains(const std::string_view& id) const {
630 for (const auto& reaction : m_reactions) {
631 if (reaction.id() == id) {
632 return true;
633 }
634 }
635 return false;
636 }
637
638 template <typename ReactionT>
640 for (const auto& r : m_reactions) {
641 if (r == reaction) {
642 return true;
643 }
644 }
645 return false;
646 }
647
648 template <typename ReactionT>
653
654 template <typename ReactionT>
655 bool TemplatedReactionSet<ReactionT>::contains_species(const fourdst::atomic::Species& species) const {
656 for (const auto& reaction : m_reactions) {
657 if (reaction.contains(species)) {
658 return true;
659 }
660 }
661 return false;
662 }
663
664 template <typename ReactionT>
665 bool TemplatedReactionSet<ReactionT>::contains_reactant(const fourdst::atomic::Species& species) const {
666 for (const auto& r : m_reactions) {
667 if (r.contains_reactant(species)) {
668 return true;
669 }
670 }
671 return false;
672 }
673
674 template <typename ReactionT>
675 bool TemplatedReactionSet<ReactionT>::contains_product(const fourdst::atomic::Species& species) const {
676 for (const auto& r : m_reactions) {
677 if (r.contains_product(species)) {
678 return true;
679 }
680 }
681 return false;
682 }
683
684 template <typename ReactionT>
685 const ReactionT& TemplatedReactionSet<ReactionT>::operator[](const size_t index) const {
686 if (index >= m_reactions.size()) {
687 m_logger -> flush_log();
688 throw std::out_of_range("Index" + std::to_string(index) + " out of range for ReactionSet of size " + std::to_string(m_reactions.size()) + ".");
689 }
690 return m_reactions[index];
691 }
692
693 template <typename ReactionT>
694 const ReactionT& TemplatedReactionSet<ReactionT>::operator[](const std::string_view& id) const {
695 if (auto it = m_reactionNameMap.find(std::string(id)); it != m_reactionNameMap.end()) {
696 return it->second;
697 }
698 m_logger -> flush_log();
699 throw std::out_of_range("Species " + std::string(id) + " does not exist in ReactionSet.");
700 }
701
702 template <typename ReactionT>
704 if (size() != other.size()) {
705 return false;
706 }
707 return hash() == other.hash();
708 }
709
710 template <typename ReactionT>
712 return !(*this == other);
713 }
714
715 template <typename ReactionT>
716 uint64_t TemplatedReactionSet<ReactionT>::hash(uint64_t seed) const {
717 if (m_reactions.empty()) {
718 return XXHash64::hash(nullptr, 0, seed);
719 }
720 std::vector<uint64_t> individualReactionHashes;
721 individualReactionHashes.reserve(m_reactions.size());
722 for (const auto& reaction : m_reactions) {
723 individualReactionHashes.push_back(reaction.hash(seed));
724 }
725
726 std::ranges::sort(individualReactionHashes);
727
728 const auto data = static_cast<const void*>(individualReactionHashes.data());
729 const size_t sizeInBytes = individualReactionHashes.size() * sizeof(uint64_t);
730 return XXHash64::hash(data, sizeInBytes, seed);
731 }
732
733 template<typename ReactionT>
734 std::unordered_set<fourdst::atomic::Species> TemplatedReactionSet<ReactionT>::getReactionSetSpecies() const {
735 std::unordered_set<fourdst::atomic::Species> species;
736 for (const auto& reaction : m_reactions) {
737 const auto reactionSpecies = reaction.all_species();
738 species.insert(reactionSpecies.begin(), reactionSpecies.end());
739 }
740 return species;
741 }
742}
743
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
T calculate_rate(const T T9) const
Template implementation for calculating the total reaction rate.
Definition reaction.h:385
friend std::ostream & operator<<(std::ostream &os, const LogicalReaction &r)
Definition reaction.h:366
void add_reaction(const Reaction &reaction)
Adds another Reaction source to this logical reaction.
Definition reaction.cpp:192
double calculate_rate(const double T9) const override
Calculates the total reaction rate by summing all source rates.
Definition reaction.cpp:214
LogicalReaction(const std::vector< Reaction > &reactions)
Constructs a LogicalReaction from a vector of Reaction objects.
Definition reaction.cpp:163
std::vector< std::string > m_sources
List of source labels.
Definition reaction.h:372
std::vector< RateCoefficientSet > m_rates
List of rate coefficient sets from each source.
Definition reaction.h:373
virtual double calculate_forward_rate_log_derivative(const double T9) const override
Definition reaction.cpp:218
std::vector< std::string > sources() const
Gets the list of source labels for the aggregated rates.
Definition reaction.h:337
size_t size() const
Gets the number of source rates contributing to this logical reaction.
Definition reaction.h:331
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:270
std::unordered_set< fourdst::atomic::Species > product_species() const
Gets a set of all unique product species.
Definition reaction.cpp:106
bool contains_product(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a product.
Definition reaction.cpp:82
std::string_view id() const
Gets the unique identifier of the reaction.
Definition reaction.h:204
bool m_reverse
Flag indicating if this is a reverse reaction rate.
Definition reaction.h:272
const std::vector< fourdst::atomic::Species > & reactants() const
Gets the vector of reactant species.
Definition reaction.h:216
int m_chapter
Chapter number from the REACLIB database, defining the reaction structure.
Definition reaction.h:266
size_t num_species() const
Gets the number of unique species involved in the reaction.
Definition reaction.cpp:129
friend std::ostream & operator<<(std::ostream &os, const Reaction &r)
Definition reaction.h:258
bool operator!=(const Reaction &other) const
Compares this reaction with another for inequality.
Definition reaction.h:248
virtual double calculate_forward_rate_log_derivative(const double T9) const
Definition reaction.cpp:47
std::string_view sourceLabel() const
Gets the source label for the rate data.
Definition reaction.h:134
std::vector< fourdst::atomic::Species > m_products
Products of the reaction.
Definition reaction.h:269
double m_qValue
Q-value of the reaction in MeV.
Definition reaction.h:267
std::string m_id
Unique identifier for the reaction (e.g., "h1+h1=>h2+e+nu").
Definition reaction.h:264
int chapter() const
Gets the REACLIB chapter number.
Definition reaction.h:128
std::string m_peName
Name of the reaction in (projectile, ejectile) notation (e.g. "p(p,g)d").
Definition reaction.h:265
T calculate_rate(const T T9) const
Template implementation for calculating the reaction rate.
Definition reaction.h:283
const std::vector< fourdst::atomic::Species > & products() const
Gets the vector of product species.
Definition reaction.h:222
quill::Logger * m_logger
Definition reaction.h:263
virtual std::string_view peName() const
Gets the reaction name in (projectile, ejectile) notation.
Definition reaction.h:122
std::unordered_set< fourdst::atomic::Species > all_species() const
Gets a set of all unique species involved in the reaction.
Definition reaction.cpp:91
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:98
const RateCoefficientSet & rateCoefficients() const
Gets the set of rate coefficients.
Definition reaction.h:140
std::vector< fourdst::atomic::Species > m_reactants
Reactants of the reaction.
Definition reaction.h:268
double excess_energy() const
Calculates the excess energy from the mass difference of reactants and products.
Definition reaction.cpp:144
RateCoefficientSet m_rateCoefficients
The seven rate coefficients.
Definition reaction.h:271
bool is_reverse() const
Checks if this is a reverse reaction rate.
Definition reaction.h:228
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:68
bool contains_reactant(const fourdst::atomic::Species &species) const
Checks if the reaction involves a given species as a reactant.
Definition reaction.cpp:73
double qValue() const
Gets the Q-value of the reaction.
Definition reaction.h:210
bool operator==(const Reaction &other) const
Compares this reaction with another for equality based on their IDs.
Definition reaction.h:241
std::unordered_map< fourdst::atomic::Species, int > stoichiometry() const
Gets a map of all species to their stoichiometric coefficients.
Definition reaction.cpp:133
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:157
void clear()
Removes all reactions from the set.
Definition reaction.h:649
bool operator==(const TemplatedReactionSet &other) const
Compares this set with another for equality.
Definition reaction.h:703
const ReactionT & operator[](const std::string_view &id) const
Accesses a reaction by its ID.
Definition reaction.h:694
std::unordered_set< fourdst::atomic::Species > getReactionSetSpecies() const
Definition reaction.h:734
uint64_t hash(uint64_t seed=0) const
Computes a hash for the entire set.
Definition reaction.h:716
void add_reaction(ReactionT reaction)
Adds a reaction to the set.
Definition reaction.h:609
std::unordered_map< std::string, Reaction > m_reactionNameMap
Definition reaction.h:558
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:675
friend std::ostream & operator<<(std::ostream &os, const TemplatedReactionSet< ReactionT > &r)
Definition reaction.h:537
TemplatedReactionSet(std::vector< ReactionT > reactions)
Constructs a ReactionSet from a vector of reactions.
Definition reaction.h:568
const ReactionT & operator[](size_t index) const
Accesses a reaction by its index.
Definition reaction.h:685
size_t size() const
Gets the number of reactions in the set.
Definition reaction.h:459
bool contains(const std::string_view &id) const
Checks if the set contains a reaction with the given ID.
Definition reaction.h:629
void remove_reaction(const ReactionT &reaction)
Removes a reaction from the set.
Definition reaction.h:616
bool operator!=(const TemplatedReactionSet &other) const
Compares this set with another for inequality.
Definition reaction.h:711
bool contains(const Reaction &reaction) const
Checks if the set contains the given reaction.
Definition reaction.h:639
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:665
TemplatedReactionSet< ReactionT > & operator=(const TemplatedReactionSet< ReactionT > &other)
Copy assignment operator.
Definition reaction.h:599
bool contains_species(const fourdst::atomic::Species &species) const
Checks if any reaction in the set involves the given species.
Definition reaction.h:655
TemplatedReactionSet(const TemplatedReactionSet< ReactionT > &other)
Copy constructor.
Definition reaction.h:586
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:563
LogicalReactionSet packReactionSetToLogicalReactionSet(const ReactionSet &reactionSet)
Definition reaction.cpp:273
TemplatedReactionSet< Reaction > ReactionSet
A set of reactions, typically from a single source like REACLIB.
Definition reaction.h:562
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