GridFire 0.0.1a General Purpose Nuclear Network

Loading...
Searching...
No Matches
engine_defined.cpp
Go to the documentation of this file.
1#include "gridfire/engine/views/engine_defined.h"
2
3#include <ranges>
4
5#include "quill/LogMacros.h"
6
7#include <string>
8#include <vector>
9#include <unordered_set>
10#include <stdexcept>
11#include <unordered_map>
12#include <utility>
13
14 namespace gridfire {
15 using fourdst::atomic::Species;
16
17 DefinedEngineView::DefinedEngineView(const std::vector<std::string>& peNames, DynamicEngine& baseEngine) :
18 m_baseEngine(baseEngine) {
19 collect(peNames);
20 }
21
22 const DynamicEngine & DefinedEngineView::getBaseEngine() const {
23 return m_baseEngine;
24 }
25
26 const std::vector<Species> & DefinedEngineView::getNetworkSpecies() const {
27 return m_activeSpecies;
28 }
29
30 std::expected<StepDerivatives<double>, expectations::StaleEngineError> DefinedEngineView::calculateRHSAndEnergy(
31 const std::vector<double> &Y_defined,
32 const double T9,
33 const double rho
34 ) const {
35 validateNetworkState();
36
37 const auto Y_full = mapViewToFull(Y_defined);
38 const auto result = m_baseEngine.calculateRHSAndEnergy(Y_full, T9, rho);
39
40 if (!result) {
41 return std::unexpected{result.error()};
42 }
43
44 const auto [dydt, nuclearEnergyGenerationRate] = result.value();
45 StepDerivatives<double> definedResults;
46 definedResults.nuclearEnergyGenerationRate = nuclearEnergyGenerationRate;
47 definedResults.dydt = mapFullToView(dydt);
48 return definedResults;
49 }
50
51 void DefinedEngineView::generateJacobianMatrix(
52 const std::vector<double> &Y_dynamic,
53 const double T9,
54 const double rho
55 ) const {
56 validateNetworkState();
57
58 const auto Y_full = mapViewToFull(Y_dynamic);
59 m_baseEngine.generateJacobianMatrix(Y_full, T9, rho);
60 }
61
62 double DefinedEngineView::getJacobianMatrixEntry(
63 const int i_defined,
64 const int j_defined
65 ) const {
66 validateNetworkState();
67
68 const size_t i_full = mapViewToFullSpeciesIndex(i_defined);
69 const size_t j_full = mapViewToFullSpeciesIndex(j_defined);
70
71 return m_baseEngine.getJacobianMatrixEntry(i_full, j_full);
72 }
73
74 void DefinedEngineView::generateStoichiometryMatrix() {
75 validateNetworkState();
76
77 m_baseEngine.generateStoichiometryMatrix();
78 }
79
80 int DefinedEngineView::getStoichiometryMatrixEntry(
81 const int speciesIndex_defined,
82 const int reactionIndex_defined
83 ) const {
84 validateNetworkState();
85
86 const size_t i_full = mapViewToFullSpeciesIndex(speciesIndex_defined);
87 const size_t j_full = mapViewToFullReactionIndex(reactionIndex_defined);
88 return m_baseEngine.getStoichiometryMatrixEntry(i_full, j_full);
89 }
90
91 double DefinedEngineView::calculateMolarReactionFlow(
92 const reaction::Reaction &reaction,
93 const std::vector<double> &Y_defined,
94 const double T9,
95 const double rho
96 ) const {
97 validateNetworkState();
98
99 if (!m_activeReactions.contains(reaction)) {
100 LOG_ERROR(m_logger, "Reaction '{}' is not part of the active reactions in the DefinedEngineView.", reaction.id());
101 m_logger -> flush_log();
102 throw std::runtime_error("Reaction not found in active reactions: " + std::string(reaction.id()));
103 }
104 const auto Y_full = mapViewToFull(Y_defined);
105 return m_baseEngine.calculateMolarReactionFlow(reaction, Y_full, T9, rho);
106 }
107
108 const reaction::LogicalReactionSet & DefinedEngineView::getNetworkReactions() const {
109 validateNetworkState();
110
111 return m_activeReactions;
112 }
113
114 void DefinedEngineView::setNetworkReactions(const reaction::LogicalReactionSet &reactions) {
115 std::vector<std::string> peNames;
116 for (const auto& reaction : reactions) {
117 peNames.push_back(std::string(reaction.id()));
118 }
119 collect(peNames);
120 }
121
122 std::expected<std::unordered_map<Species, double>, expectations::StaleEngineError> DefinedEngineView::getSpeciesTimescales(
123 const std::vector<double> &Y_defined,
124 const double T9,
125 const double rho
126 ) const {
127 validateNetworkState();
128
129 const auto Y_full = mapViewToFull(Y_defined);
130 const auto result = m_baseEngine.getSpeciesTimescales(Y_full, T9, rho);
131 if (!result) {
132 return std::unexpected{result.error()};
133 }
134 const auto& fullTimescales = result.value();
135
136 std::unordered_map<Species, double> definedTimescales;
137 for (const auto& active_species : m_activeSpecies) {
138 if (fullTimescales.contains(active_species)) {
139 definedTimescales[active_species] = fullTimescales.at(active_species);
140 }
141 }
142 return definedTimescales;
143 }
144
145 std::expected<std::unordered_map<fourdst::atomic::Species, double>, expectations::StaleEngineError>
146 DefinedEngineView::getSpeciesDestructionTimescales(
147 const std::vector<double> &Y_defined,
148 const double T9,
149 const double rho
150 ) const {
151 validateNetworkState();
152
153 const auto Y_full = mapViewToFull(Y_defined);
154 const auto result = m_baseEngine.getSpeciesDestructionTimescales(Y_full, T9, rho);
155
156 if (!result) {
157 return std::unexpected{result.error()};
158 }
159
160 const auto& destructionTimescales = result.value();
161
162 std::unordered_map<Species, double> definedTimescales;
163 for (const auto& active_species : m_activeSpecies) {
164 if (destructionTimescales.contains(active_species)) {
165 definedTimescales[active_species] = destructionTimescales.at(active_species);
166 }
167 }
168 return definedTimescales;
169 }
170
171 fourdst::composition::Composition DefinedEngineView::update(const NetIn &netIn) {
172 return m_baseEngine.update(netIn);
173 }
174
175 bool DefinedEngineView::isStale(const NetIn &netIn) {
176 return m_baseEngine.isStale(netIn);
177 }
178
179
180 void DefinedEngineView::setScreeningModel(const screening::ScreeningType model) {
181 m_baseEngine.setScreeningModel(model);
182 }
183
184 screening::ScreeningType DefinedEngineView::getScreeningModel() const {
185 return m_baseEngine.getScreeningModel();
186 }
187
188 int DefinedEngineView::getSpeciesIndex(const Species &species) const {
189 validateNetworkState();
190
191 const auto it = std::ranges::find(m_activeSpecies, species);
192 if (it != m_activeSpecies.end()) {
193 return static_cast<int>(std::distance(m_activeSpecies.begin(), it));
194 } else {
195 LOG_ERROR(m_logger, "Species '{}' not found in active species list.", species.name());
196 m_logger->flush_log();
197 throw std::runtime_error("Species not found in active species list: " + std::string(species.name()));
198 }
199 }
200
201 std::vector<double> DefinedEngineView::mapNetInToMolarAbundanceVector(const NetIn &netIn) const {
202 std::vector<double> Y(m_activeSpecies.size(), 0.0); // Initialize with zeros
203 for (const auto& [symbol, entry] : netIn.composition) {
204 auto it = std::ranges::find(m_activeSpecies, entry.isotope());
205 if (it != m_activeSpecies.end()) {
206 Y[getSpeciesIndex(entry.isotope())] = netIn.composition.getMolarAbundance(symbol); // Map species to their molar abundance
207 }
208 }
209 return Y; // Return the vector of molar abundances
210 }
211
212 PrimingReport DefinedEngineView::primeEngine(const NetIn &netIn) {
213 return m_baseEngine.primeEngine(netIn);
214 }
215
216 std::vector<size_t> DefinedEngineView::constructSpeciesIndexMap() const {
217 LOG_TRACE_L3(m_logger, "Constructing species index map for DefinedEngineView...");
218 std::unordered_map<Species, size_t> fullSpeciesReverseMap;
219 const auto& fullSpeciesList = m_baseEngine.getNetworkSpecies();
220
221 fullSpeciesReverseMap.reserve(fullSpeciesList.size());
222
223 for (size_t i = 0; i < fullSpeciesList.size(); ++i) {
224 fullSpeciesReverseMap[fullSpeciesList[i]] = i;
225 }
226
227 std::vector<size_t> speciesIndexMap;
228 speciesIndexMap.reserve(m_activeSpecies.size());
229
230 for (const auto& active_species : m_activeSpecies) {
231 auto it = fullSpeciesReverseMap.find(active_species);
232 if (it != fullSpeciesReverseMap.end()) {
233 speciesIndexMap.push_back(it->second);
234 } else {
235 LOG_ERROR(m_logger, "Species '{}' not found in full species map.", active_species.name());
236 m_logger -> flush_log();
237 throw std::runtime_error("Species not found in full species map: " + std::string(active_species.name()));
238 }
239 }
240 LOG_TRACE_L3(m_logger, "Species index map constructed with {} entries.", speciesIndexMap.size());
241 return speciesIndexMap;
242
243 }
244
245 std::vector<size_t> DefinedEngineView::constructReactionIndexMap() const {
246 LOG_TRACE_L3(m_logger, "Constructing reaction index map for DefinedEngineView...");
247
248 // --- Step 1: Create a reverse map using the reaction's unique ID as the key. ---
249 std::unordered_map<std::string_view, size_t> fullReactionReverseMap;
250 const auto& fullReactionSet = m_baseEngine.getNetworkReactions();
251 fullReactionReverseMap.reserve(fullReactionSet.size());
252
253 for (size_t i_full = 0; i_full < fullReactionSet.size(); ++i_full) {
254 fullReactionReverseMap[fullReactionSet[i_full].id()] = i_full;
255 }
256
257 // --- Step 2: Build the final index map using the active reaction set. ---
258 std::vector<size_t> reactionIndexMap;
259 reactionIndexMap.reserve(m_activeReactions.size());
260
261 for (const auto& active_reaction_ptr : m_activeReactions) {
262 auto it = fullReactionReverseMap.find(active_reaction_ptr.id());
263
264 if (it != fullReactionReverseMap.end()) {
265 reactionIndexMap.push_back(it->second);
266 } else {
267 LOG_ERROR(m_logger, "Active reaction '{}' not found in base engine during reaction index map construction.", active_reaction_ptr.id());
268 m_logger->flush_log();
269 throw std::runtime_error("Mismatch between active reactions and base engine.");
270 }
271 }
272
273 LOG_TRACE_L3(m_logger, "Reaction index map constructed with {} entries.", reactionIndexMap.size());
274 return reactionIndexMap;
275 }
276
277 std::vector<double> DefinedEngineView::mapViewToFull(const std::vector<double>& culled) const {
278 std::vector<double> full(m_baseEngine.getNetworkSpecies().size(), 0.0);
279 for (size_t i_culled = 0; i_culled < culled.size(); ++i_culled) {
280 const size_t i_full = m_speciesIndexMap[i_culled];
281 full[i_full] += culled[i_culled];
282 }
283 return full;
284 }
285
286 std::vector<double> DefinedEngineView::mapFullToView(const std::vector<double>& full) const {
287 std::vector<double> culled(m_activeSpecies.size(), 0.0);
288 for (size_t i_culled = 0; i_culled < m_activeSpecies.size(); ++i_culled) {
289 const size_t i_full = m_speciesIndexMap[i_culled];
290 culled[i_culled] = full[i_full];
291 }
292 return culled;
293 }
294
295 size_t DefinedEngineView::mapViewToFullSpeciesIndex(size_t culledSpeciesIndex) const {
296 if (culledSpeciesIndex < 0 || culledSpeciesIndex >= m_speciesIndexMap.size()) {
297 LOG_ERROR(m_logger, "Defined index {} is out of bounds for species index map of size {}.", culledSpeciesIndex, m_speciesIndexMap.size());
298 m_logger->flush_log();
299 throw std::out_of_range("Defined index " + std::to_string(culledSpeciesIndex) + " is out of bounds for species index map of size " + std::to_string(m_speciesIndexMap.size()) + ".");
300 }
301 return m_speciesIndexMap[culledSpeciesIndex];
302 }
303
304 size_t DefinedEngineView::mapViewToFullReactionIndex(size_t culledReactionIndex) const {
305 if (culledReactionIndex < 0 || culledReactionIndex >= m_reactionIndexMap.size()) {
306 LOG_ERROR(m_logger, "Defined index {} is out of bounds for reaction index map of size {}.", culledReactionIndex, m_reactionIndexMap.size());
307 m_logger->flush_log();
308 throw std::out_of_range("Defined index " + std::to_string(culledReactionIndex) + " is out of bounds for reaction index map of size " + std::to_string(m_reactionIndexMap.size()) + ".");
309 }
310 return m_reactionIndexMap[culledReactionIndex];
311 }
312
313 void DefinedEngineView::validateNetworkState() const {
314 if (m_isStale) {
315 LOG_ERROR(m_logger, "DefinedEngineView is stale. Please call update() with a valid NetIn object.");
316 m_logger->flush_log();
317 throw std::runtime_error("DefinedEngineView is stale. Please call update() with a valid NetIn object.");
318 }
319 }
320
321 void DefinedEngineView::collect(const std::vector<std::string> &peNames) {
322 std::unordered_set<Species> seenSpecies;
323
324 const auto& fullNetworkReactionSet = m_baseEngine.getNetworkReactions();
325 for (const auto& peName : peNames) {
326 if (!fullNetworkReactionSet.contains(peName)) {
327 LOG_ERROR(m_logger, "Reaction with name '{}' not found in the base engine's network reactions. Aborting...", peName);
328 m_logger->flush_log();
329 throw std::runtime_error("Reaction with name '" + std::string(peName) + "' not found in the base engine's network reactions.");
330 }
331 auto reaction = fullNetworkReactionSet[peName];
332 for (const auto& reactant : reaction.reactants()) {
333 if (!seenSpecies.contains(reactant)) {
334 seenSpecies.insert(reactant);
335 m_activeSpecies.push_back(reactant);
336 }
337 }
338 for (const auto& product : reaction.products()) {
339 if (!seenSpecies.contains(product)) {
340 seenSpecies.insert(product);
341 m_activeSpecies.push_back(product);
342 }
343 }
344 m_activeReactions.add_reaction(reaction);
345 }
346 LOG_TRACE_L3(m_logger, "DefinedEngineView built with {} active species and {} active reactions.", m_activeSpecies.size(), m_activeReactions.size());
347 LOG_TRACE_L3(m_logger, "Active species: {}", [this]() -> std::string {
348 std::string result;
349 for (const auto& species : m_activeSpecies) {
350 result += std::string(species.name()) + ", ";
351 }
352 if (!result.empty()) {
353 result.pop_back(); // Remove last space
354 result.pop_back(); // Remove last comma
355 }
356 return result;
357 }());
358 LOG_TRACE_L3(m_logger, "Active reactions: {}", [this]() -> std::string {
359 std::string result;
360 for (const auto& reaction : m_activeReactions) {
361 result += std::string(reaction.id()) + ", ";
362 }
363 if (!result.empty()) {
364 result.pop_back(); // Remove last space
365 result.pop_back(); // Remove last comma
366 }
367 return result;
368 }());
369 m_speciesIndexMap = constructSpeciesIndexMap();
370 m_reactionIndexMap = constructReactionIndexMap();
371 m_isStale = false;
372 }
373
374
378
379 FileDefinedEngineView::FileDefinedEngineView(
380 DynamicEngine &baseEngine,
381 const std::string &fileName,
382 const io::NetworkFileParser &parser
383 ):
384 DefinedEngineView(parser.parse(fileName), baseEngine),
385 m_fileName(fileName),
386 m_parser(parser) {}
387}
gridfire::DefinedEngineView::primeEngine
PrimingReport primeEngine(const NetIn &netIn) override
Definition engine_defined.cpp:212
gridfire::DefinedEngineView::calculateMolarReactionFlow
double calculateMolarReactionFlow(const reaction::Reaction &reaction, const std::vector< double > &Y_defined, const double T9, const double rho) const override
Calculates the molar reaction flow for a given reaction in the active network.
Definition engine_defined.cpp:91
gridfire::DefinedEngineView::getNetworkSpecies
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Gets the list of active species in the network defined by the file.
Definition engine_defined.cpp:26
gridfire::DefinedEngineView::m_isStale
bool m_isStale
Definition engine_defined.h:177
gridfire::DefinedEngineView::getJacobianMatrixEntry
double getJacobianMatrixEntry(const int i_defined, const int j_defined) const override
Gets an entry from the Jacobian matrix for the active species.
Definition engine_defined.cpp:62
gridfire::DefinedEngineView::mapFullToView
std::vector< double > mapFullToView(const std::vector< double > &full) const
Maps a vector of full abundances to a vector of culled abundances.
Definition engine_defined.cpp:286
gridfire::DefinedEngineView::m_activeReactions
reaction::LogicalReactionSet m_activeReactions
Maps indices of active species to indices in the full network.
Definition engine_defined.h:184
gridfire::DefinedEngineView::getScreeningModel
screening::ScreeningType getScreeningModel() const override
Gets the screening model from the base engine.
Definition engine_defined.cpp:184
gridfire::DefinedEngineView::getSpeciesDestructionTimescales
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > getSpeciesDestructionTimescales(const std::vector< double > &Y_defined, const double T9, const double rho) const override
Definition engine_defined.cpp:146
gridfire::DefinedEngineView::calculateRHSAndEnergy
std::expected< StepDerivatives< double >, expectations::StaleEngineError > calculateRHSAndEnergy(const std::vector< double > &Y_defined, const double T9, const double rho) const override
Calculates the right-hand side (dY/dt) and energy generation for the active species.
Definition engine_defined.cpp:30
gridfire::DefinedEngineView::m_logger
quill::Logger * m_logger
Active species in the defined engine.
Definition engine_defined.h:180
gridfire::DefinedEngineView::validateNetworkState
void validateNetworkState() const
Definition engine_defined.cpp:313
gridfire::DefinedEngineView::mapViewToFull
std::vector< double > mapViewToFull(const std::vector< double > &defined) const
Maps a vector of culled abundances to a vector of full abundances.
Definition engine_defined.cpp:277
gridfire::DefinedEngineView::m_activeSpecies
std::vector< fourdst::atomic::Species > m_activeSpecies
Active reactions in the defined engine.
Definition engine_defined.h:182
gridfire::DefinedEngineView::getBaseEngine
const DynamicEngine & getBaseEngine() const override
Access the underlying engine instance.
Definition engine_defined.cpp:22
gridfire::DefinedEngineView::m_baseEngine
DynamicEngine & m_baseEngine
Definition engine_defined.h:178
gridfire::DefinedEngineView::mapNetInToMolarAbundanceVector
std::vector< double > mapNetInToMolarAbundanceVector(const NetIn &netIn) const override
Definition engine_defined.cpp:201
gridfire::DefinedEngineView::isStale
bool isStale(const NetIn &netIn) override
Definition engine_defined.cpp:175
gridfire::DefinedEngineView::setNetworkReactions
void setNetworkReactions(const reaction::LogicalReactionSet &reactions) override
Definition engine_defined.cpp:114
gridfire::DefinedEngineView::DefinedEngineView
DefinedEngineView(const std::vector< std::string > &peNames, DynamicEngine &baseEngine)
Definition engine_defined.cpp:17
gridfire::DefinedEngineView::constructSpeciesIndexMap
std::vector< size_t > constructSpeciesIndexMap() const
Constructs the species index map.
Definition engine_defined.cpp:216
gridfire::DefinedEngineView::mapViewToFullReactionIndex
size_t mapViewToFullReactionIndex(size_t definedReactionIndex) const
Maps a culled reaction index to a full reaction index.
Definition engine_defined.cpp:304
gridfire::DefinedEngineView::constructReactionIndexMap
std::vector< size_t > constructReactionIndexMap() const
Constructs the reaction index map.
Definition engine_defined.cpp:245
gridfire::DefinedEngineView::setScreeningModel
void setScreeningModel(screening::ScreeningType model) override
Sets the screening model for the base engine.
Definition engine_defined.cpp:180
gridfire::DefinedEngineView::getSpeciesIndex
int getSpeciesIndex(const fourdst::atomic::Species &species) const override
Definition engine_defined.cpp:188
gridfire::DefinedEngineView::getSpeciesTimescales
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > getSpeciesTimescales(const std::vector< double > &Y_defined, const double T9, const double rho) const override
Computes timescales for all active species in the network.
Definition engine_defined.cpp:122
gridfire::DefinedEngineView::m_speciesIndexMap
std::vector< size_t > m_speciesIndexMap
Maps indices of active reactions to indices in the full network.
Definition engine_defined.h:187
gridfire::DefinedEngineView::generateStoichiometryMatrix
void generateStoichiometryMatrix() override
Generates the stoichiometry matrix for the active reactions and species.
Definition engine_defined.cpp:74
gridfire::DefinedEngineView::generateJacobianMatrix
void generateJacobianMatrix(const std::vector< double > &Y_dynamic, const double T9, const double rho) const override
Generates the Jacobian matrix for the active species.
Definition engine_defined.cpp:51
gridfire::DefinedEngineView::collect
void collect(const std::vector< std::string > &peNames)
Definition engine_defined.cpp:321
gridfire::DefinedEngineView::getNetworkReactions
const reaction::LogicalReactionSet & getNetworkReactions() const override
Gets the set of active logical reactions in the network.
Definition engine_defined.cpp:108
gridfire::DefinedEngineView::update
fourdst::composition::Composition update(const NetIn &netIn) override
Updates the engine view if it is marked as stale.
Definition engine_defined.cpp:171
gridfire::DefinedEngineView::mapViewToFullSpeciesIndex
size_t mapViewToFullSpeciesIndex(size_t definedSpeciesIndex) const
Maps a culled species index to a full species index.
Definition engine_defined.cpp:295
gridfire::DefinedEngineView::getStoichiometryMatrixEntry
int getStoichiometryMatrixEntry(const int speciesIndex_defined, const int reactionIndex_defined) const override
Gets an entry from the stoichiometry matrix for the active species and reactions.
Definition engine_defined.cpp:80
gridfire::DefinedEngineView::m_reactionIndexMap
std::vector< size_t > m_reactionIndexMap
Definition engine_defined.h:189
gridfire::DynamicEngine
Abstract class for engines supporting Jacobian and stoichiometry operations.
Definition engine_abstract.h:130
gridfire::FileDefinedEngineView::m_parser
const io::NetworkFileParser & m_parser
Definition engine_defined.h:275
gridfire::FileDefinedEngineView::m_fileName
std::string m_fileName
Parser for the network file.
Definition engine_defined.h:273
gridfire::FileDefinedEngineView::FileDefinedEngineView
FileDefinedEngineView(DynamicEngine &baseEngine, const std::string &fileName, const io::NetworkFileParser &parser)
FileDefinedEngineView Implementation ///.
Definition engine_defined.cpp:379
gridfire::io::NetworkFileParser
An abstract base class for network file parsers.
Definition network_file.h:22
gridfire::reaction::Reaction
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
engine_defined.h
gridfire::reaction
Definition reaction.h:25
gridfire::reaction::LogicalReactionSet
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:563
gridfire::screening::ScreeningType
ScreeningType
Enumerates the available plasma screening models.
Definition screening_types.h:15
gridfire
Definition engine_abstract.h:31
gridfire::NetIn
Definition network.h:53
gridfire::NetIn::composition
fourdst::composition::Composition composition
Composition of the network.
Definition network.h:54
gridfire::PrimingReport
Captures the result of a network priming operation.
Definition reporting.h:67
gridfire::StepDerivatives
Structure holding derivatives and energy generation for a network step.
Definition engine_abstract.h:60
gridfire::StepDerivatives::nuclearEnergyGenerationRate
T nuclearEnergyGenerationRate
Specific energy generation rate (e.g., erg/g/s).
Definition engine_abstract.h:62
gridfire::StepDerivatives::dydt
std::vector< T > dydt
Derivatives of abundances (dY/dt for each species).
Definition engine_abstract.h:61
gridfire::expectations::StaleEngineError
Definition expected_engine.h:44