GridFire 0.0.1a General Purpose Nuclear Network

Loading...
Searching...
No Matches
reaclib.cpp
Go to the documentation of this file.
1#include "fourdst/composition/atomicSpecies.h"
2#include "fourdst/composition/species.h"
3
4#include "gridfire/reaction/reaclib.h"
5#include "gridfire/reaction/reactions_data.h"
6#include "gridfire/network.h"
7
8#include <stdexcept>
9#include <sstream>
10#include <vector>
11#include <string>
12
13std::string trim_whitespace(const std::string& str) {
14 auto startIt = str.begin();
15 const auto endIt = str.end();
16
17 while (startIt != endIt && std::isspace(static_cast<unsigned char>(*startIt))) {
18 ++startIt;
19 }
20 if (startIt == endIt) {
21 return "";
22 }
23 const auto ritr = std::find_if(str.rbegin(), std::string::const_reverse_iterator(startIt),
24 [](const unsigned char ch){ return !std::isspace(ch); });
25 return std::string(startIt, ritr.base());
26}
27
28 namespace gridfire::reaclib {
29 static reaction::LogicalReactionSet* s_all_reaclib_reactions_ptr = nullptr;
30
31 #pragma pack(push, 1)
32 struct ReactionRecord {
33 int32_t chapter;
34 double qValue;
35 double coeffs[7];
36 bool reverse;
37 char label[8];
38 char rpName[64];
39 char reactants_str[128];
40 char products_str[128];
41 };
42 #pragma pack(pop)
43
44 std::ostream& operator<<(std::ostream& os, const ReactionRecord& r) {
45 os << "Chapter: " << r.chapter
46 << ", Q-value: " << r.qValue
47 << ", Coefficients: [" << r.coeffs[0] << ", " << r.coeffs[1] << ", "
48 << r.coeffs[2] << ", " << r.coeffs[3] << ", " << r.coeffs[4] << ", "
49 << r.coeffs[5] << ", " << r.coeffs[6] << "]"
50 << ", Reverse: " << (r.reverse ? "true" : "false")
51 << ", Label: '" << std::string(r.label, strnlen(r.label, sizeof(r.label))) << "'"
52 << ", RP Name: '" << std::string(r.rpName, strnlen(r.rpName, sizeof(r.rpName))) << "'"
53 << ", Reactants: '" << std::string(r.reactants_str, strnlen(r.reactants_str, sizeof(r.reactants_str))) << "'"
54 << ", Products: '" << std::string(r.products_str, strnlen(r.products_str, sizeof(r.products_str))) << "'";
55 return os;
56 }
57
58 static std::vector<fourdst::atomic::Species> parseSpeciesString(const std::string_view str) {
59 std::vector<fourdst::atomic::Species> result;
60 std::stringstream ss{std::string(str)};
61 std::string name;
62
63 while (ss >> name) {
64 // Trim whitespace that might be left over from the fixed-width char arrays
65 const auto trimmed_name = trim_whitespace(name);
66 if (trimmed_name.empty()) continue;
67
68 auto it = fourdst::atomic::species.find(trimmed_name);
69 if (it != fourdst::atomic::species.end()) {
70 result.push_back(it->second);
71 } else {
72 // If a species is not found, it's a critical data error.
73 throw std::runtime_error("Unknown species in reaction data: " + std::string(trimmed_name));
74 }
75 }
76 return result;
77 }
78
79 static void initializeAllReaclibReactions() {
80 if (s_initialized) {
81 return;
82 }
83
84 // Cast the raw byte data to our structured record format.
85 const auto* records = reinterpret_cast<const ReactionRecord*>(raw_reactions_data);
86 constexpr size_t num_reactions = raw_reactions_data_len / sizeof(ReactionRecord);
87
88 std::vector<reaction::Reaction> reaction_list;
89 reaction_list.reserve(num_reactions);
90
91 for (size_t i = 0; i < num_reactions; ++i) {
92 const auto&[chapter, qValue, coeffs, reverse, label, rpName, reactants_str, products_str] = records[i];
93
94 // The char arrays from the binary are not guaranteed to be null-terminated
95 // if the string fills the entire buffer. We create null-terminated string_views.
96 const std::string_view label_sv(label, strnlen(label, sizeof(label)));
97 const std::string_view rpName_sv(rpName, strnlen(rpName, sizeof(rpName)));
98 const std::string_view reactants_sv(reactants_str, strnlen(reactants_str, sizeof(reactants_str)));
99 const std::string_view products_sv(products_str, strnlen(products_str, sizeof(products_str)));
100
101 auto reactants = parseSpeciesString(reactants_sv);
102 auto products = parseSpeciesString(products_sv);
103
104 const reaction::RateCoefficientSet rate_coeffs = {
105 coeffs[0], coeffs[1], coeffs[2],
106 coeffs[3], coeffs[4], coeffs[5],
107 coeffs[6]
108 };
109
110 // Construct the Reaction object. We use rpName for both the unique ID and the human-readable name.
111 reaction_list.emplace_back(
112 rpName_sv,
113 rpName_sv,
114 chapter,
115 reactants,
116 products,
117 qValue,
118 label_sv,
119 rate_coeffs,
120 reverse
121 );
122 }
123
124 // The ReactionSet takes the vector of all individual reactions.
125 const reaction::ReactionSet reaction_set(std::move(reaction_list));
126
127 // The LogicalReactionSet groups reactions by their peName, which is what we want.
128 s_all_reaclib_reactions_ptr = new reaction::LogicalReactionSet(
129 reaction::packReactionSetToLogicalReactionSet(reaction_set)
130 );
131
132 s_initialized = true;
133 }
134
135
136 // --- Public Interface Implementation ---
137
138 const reaction::LogicalReactionSet& get_all_reactions() {
139 // This ensures that the initialization happens only on the first call.
140 if (!s_initialized) {
141 initializeAllReaclibReactions();
142 }
143 if (s_all_reaclib_reactions_ptr == nullptr) {
144 throw std::runtime_error("Reaclib reactions have not been initialized.");
145 }
146 return *s_all_reaclib_reactions_ptr;
147 }
148} // namespace gridfire::reaclib
gridfire::reaclib
Definition reaclib.h:5
gridfire::reaclib::initializeAllReaclibReactions
static void initializeAllReaclibReactions()
Definition reaclib.cpp:79
gridfire::reaclib::s_all_reaclib_reactions_ptr
static reaction::LogicalReactionSet * s_all_reaclib_reactions_ptr
Definition reaclib.cpp:29
gridfire::reaclib::operator<<
std::ostream & operator<<(std::ostream &os, const ReactionRecord &r)
Definition reaclib.cpp:44
gridfire::reaclib::get_all_reactions
const reaction::LogicalReactionSet & get_all_reactions()
Provides global access to the fully initialized REACLIB reaction set.
Definition reaclib.cpp:138
gridfire::reaclib::s_initialized
static bool s_initialized
Definition reaclib.h:7
gridfire::reaclib::parseSpeciesString
static std::vector< fourdst::atomic::Species > parseSpeciesString(const std::string_view str)
Definition reaclib.cpp:58
gridfire::reaction::LogicalReactionSet
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:563
gridfire::reaction::packReactionSetToLogicalReactionSet
LogicalReactionSet packReactionSetToLogicalReactionSet(const ReactionSet &reactionSet)
Definition reaction.cpp:273
gridfire::reaction::ReactionSet
TemplatedReactionSet< Reaction > ReactionSet
A set of reactions, typically from a single source like REACLIB.
Definition reaction.h:562
gridfire::trim_whitespace
std::string trim_whitespace(const std::string &str)
Definition network.cpp:65
network.h
trim_whitespace
std::string trim_whitespace(const std::string &str)
Definition reaclib.cpp:13
reaclib.h
reactions_data.h
raw_reactions_data_len
const size_t raw_reactions_data_len
Definition reactions_data.h:181934
raw_reactions_data
const unsigned char raw_reactions_data[]
Definition reactions_data.h:5
gridfire::reaclib::ReactionRecord
Definition reaclib.cpp:32
gridfire::reaclib::ReactionRecord::label
char label[8]
Definition reaclib.cpp:37
gridfire::reaclib::ReactionRecord::rpName
char rpName[64]
Definition reaclib.cpp:38
gridfire::reaclib::ReactionRecord::chapter
int32_t chapter
Definition reaclib.cpp:33
gridfire::reaclib::ReactionRecord::reactants_str
char reactants_str[128]
Definition reaclib.cpp:39
gridfire::reaclib::ReactionRecord::coeffs
double coeffs[7]
Definition reaclib.cpp:35
gridfire::reaclib::ReactionRecord::reverse
bool reverse
Definition reaclib.cpp:36
gridfire::reaclib::ReactionRecord::qValue
double qValue
Definition reaclib.cpp:34
gridfire::reaclib::ReactionRecord::products_str
char products_str[128]
Definition reaclib.cpp:40
gridfire::reaction::RateCoefficientSet
Holds the seven coefficients for the REACLIB rate equation.
Definition reaction.h:33