GridFire 0.0.1a
General Purpose Nuclear Network
Loading...
Searching...
No Matches
engine_defined.cpp
Go to the documentation of this file.
2
3#include "quill/LogMacros.h"
4
5namespace gridfire {
6 using fourdst::atomic::Species;
7
9 DynamicEngine &baseEngine,
10 const std::string &fileName,
11 const io::NetworkFileParser &parser
12 ):
13 m_baseEngine(baseEngine),
14 m_fileName(fileName),
15 m_parser(parser),
18 buildFromFile(fileName);
19 }
20
24
25 const std::vector<Species> & FileDefinedEngineView::getNetworkSpecies() const {
26 return m_activeSpecies;
27 }
28
30 const std::vector<double> &Y_defined,
31 const double T9,
32 const double rho
33 ) const {
35
36 const auto Y_full = mapViewToFull(Y_defined);
37 const auto [dydt, nuclearEnergyGenerationRate] = m_baseEngine.calculateRHSAndEnergy(Y_full, T9, rho);
38
39 StepDerivatives<double> definedResults;
40 definedResults.nuclearEnergyGenerationRate = nuclearEnergyGenerationRate;
41 definedResults.dydt = mapFullToView(dydt);
42 return definedResults;
43 }
44
46 const std::vector<double> &Y_defined,
47 const double T9,
48 const double rho
49 ) {
51
52 const auto Y_full = mapViewToFull(Y_defined);
53 m_baseEngine.generateJacobianMatrix(Y_full, T9, rho);
54 }
55
57 const int i_defined,
58 const int j_defined
59 ) const {
61
62 const size_t i_full = mapViewToFullSpeciesIndex(i_defined);
63 const size_t j_full = mapViewToFullSpeciesIndex(j_defined);
64
65 return m_baseEngine.getJacobianMatrixEntry(i_full, j_full);
66 }
67
70
71 m_baseEngine.generateStoichiometryMatrix();
72 }
73
75 const int speciesIndex_defined,
76 const int reactionIndex_defined
77 ) const {
79
80 const size_t i_full = mapViewToFullSpeciesIndex(speciesIndex_defined);
81 const size_t j_full = mapViewToFullReactionIndex(reactionIndex_defined);
82 return m_baseEngine.getStoichiometryMatrixEntry(i_full, j_full);
83 }
84
87 const std::vector<double> &Y_defined,
88 const double T9,
89 const double rho
90 ) const {
92
93 if (!m_activeReactions.contains(reaction)) {
94 LOG_ERROR(m_logger, "Reaction '{}' is not part of the active reactions in the file defined engine view.", reaction.id());
95 m_logger -> flush_log();
96 throw std::runtime_error("Reaction not found in active reactions: " + std::string(reaction.id()));
97 }
98 const auto Y_full = mapViewToFull(Y_defined);
99 return m_baseEngine.calculateMolarReactionFlow(reaction, Y_full, T9, rho);
100 }
101
107
108 std::unordered_map<Species, double> FileDefinedEngineView::getSpeciesTimescales(
109 const std::vector<double> &Y_defined,
110 const double T9,
111 const double rho
112 ) const {
114
115 const auto Y_full = mapViewToFull(Y_defined);
116 const auto fullTimescales = m_baseEngine.getSpeciesTimescales(Y_full, T9, rho);
117
118 std::unordered_map<Species, double> definedTimescales;
119 for (const auto& active_species : m_activeSpecies) {
120 if (fullTimescales.contains(active_species)) {
121 definedTimescales[active_species] = fullTimescales.at(active_species);
122 }
123 }
124 return definedTimescales;
125 }
126
128 if (m_isStale) {
130 }
131 }
132
133 void FileDefinedEngineView::setNetworkFile(const std::string &fileName) {
134 m_isStale = true;
135 m_fileName = fileName;
136 LOG_DEBUG(m_logger, "File '{}' set to '{}'. FileDefinedNetworkView is now stale! You MUST call update() before you use it!", m_fileName, fileName);
137 }
138
140 m_baseEngine.setScreeningModel(model);
141 }
142
146
148 LOG_TRACE_L1(m_logger, "Constructing species index map for file defined engine view...");
149 std::unordered_map<Species, size_t> fullSpeciesReverseMap;
150 const auto& fullSpeciesList = m_baseEngine.getNetworkSpecies();
151
152 fullSpeciesReverseMap.reserve(fullSpeciesList.size());
153
154 for (size_t i = 0; i < fullSpeciesList.size(); ++i) {
155 fullSpeciesReverseMap[fullSpeciesList[i]] = i;
156 }
157
158 std::vector<size_t> speciesIndexMap;
159 speciesIndexMap.reserve(m_activeSpecies.size());
160
161 for (const auto& active_species : m_activeSpecies) {
162 auto it = fullSpeciesReverseMap.find(active_species);
163 if (it != fullSpeciesReverseMap.end()) {
164 speciesIndexMap.push_back(it->second);
165 } else {
166 LOG_ERROR(m_logger, "Species '{}' not found in full species map.", active_species.name());
167 m_logger -> flush_log();
168 throw std::runtime_error("Species not found in full species map: " + std::string(active_species.name()));
169 }
170 }
171 LOG_TRACE_L1(m_logger, "Species index map constructed with {} entries.", speciesIndexMap.size());
172 return speciesIndexMap;
173
174 }
175
177 LOG_TRACE_L1(m_logger, "Constructing reaction index map for file defined engine view...");
178
179 // --- Step 1: Create a reverse map using the reaction's unique ID as the key. ---
180 std::unordered_map<std::string_view, size_t> fullReactionReverseMap;
181 const auto& fullReactionSet = m_baseEngine.getNetworkReactions();
182 fullReactionReverseMap.reserve(fullReactionSet.size());
183
184 for (size_t i_full = 0; i_full < fullReactionSet.size(); ++i_full) {
185 fullReactionReverseMap[fullReactionSet[i_full].id()] = i_full;
186 }
187
188 // --- Step 2: Build the final index map using the active reaction set. ---
189 std::vector<size_t> reactionIndexMap;
190 reactionIndexMap.reserve(m_activeReactions.size());
191
192 for (const auto& active_reaction_ptr : m_activeReactions) {
193 auto it = fullReactionReverseMap.find(active_reaction_ptr.id());
194
195 if (it != fullReactionReverseMap.end()) {
196 reactionIndexMap.push_back(it->second);
197 } else {
198 LOG_ERROR(m_logger, "Active reaction '{}' not found in base engine during reaction index map construction.", active_reaction_ptr.id());
199 m_logger->flush_log();
200 throw std::runtime_error("Mismatch between active reactions and base engine.");
201 }
202 }
203
204 LOG_TRACE_L1(m_logger, "Reaction index map constructed with {} entries.", reactionIndexMap.size());
205 return reactionIndexMap;
206 }
207
208 void FileDefinedEngineView::buildFromFile(const std::string &fileName) {
209 LOG_TRACE_L1(m_logger, "Building file defined engine view from {}...", fileName);
210 auto [reactionPENames] = m_parser.parse(fileName);
211
212 m_activeReactions.clear();
213 m_activeSpecies.clear();
214
215 std::unordered_set<Species> seenSpecies;
216
217 const auto& fullNetworkReactionSet = m_baseEngine.getNetworkReactions();
218 for (const auto& peName : reactionPENames) {
219 if (!fullNetworkReactionSet.contains(peName)) {
220 LOG_ERROR(m_logger, "Reaction with name '{}' not found in the base engine's network reactions. Aborting...", peName);
221 m_logger->flush_log();
222 throw std::runtime_error("Reaction with name '" + std::string(peName) + "' not found in the base engine's network reactions.");
223 }
224 auto reaction = fullNetworkReactionSet[peName];
225 for (const auto& reactant : reaction.reactants()) {
226 if (!seenSpecies.contains(reactant)) {
227 seenSpecies.insert(reactant);
228 m_activeSpecies.push_back(reactant);
229 }
230 }
231 for (const auto& product : reaction.products()) {
232 if (!seenSpecies.contains(product)) {
233 seenSpecies.insert(product);
234 m_activeSpecies.push_back(product);
235 }
236 }
237 m_activeReactions.add_reaction(reaction);
238 }
239 LOG_TRACE_L1(m_logger, "File defined engine view built with {} active species and {} active reactions.", m_activeSpecies.size(), m_activeReactions.size());
240 LOG_DEBUG(m_logger, "Active species: {}", [this]() -> std::string {
241 std::string result;
242 for (const auto& species : m_activeSpecies) {
243 result += std::string(species.name()) + ", ";
244 }
245 if (!result.empty()) {
246 result.pop_back(); // Remove last space
247 result.pop_back(); // Remove last comma
248 }
249 return result;
250 }());
251 LOG_DEBUG(m_logger, "Active reactions: {}", [this]() -> std::string {
252 std::string result;
253 for (const auto& reaction : m_activeReactions) {
254 result += std::string(reaction.id()) + ", ";
255 }
256 if (!result.empty()) {
257 result.pop_back(); // Remove last space
258 result.pop_back(); // Remove last comma
259 }
260 return result;
261 }());
264 m_isStale = false;
265 }
266
267 std::vector<double> FileDefinedEngineView::mapViewToFull(const std::vector<double>& culled) const {
268 std::vector<double> full(m_baseEngine.getNetworkSpecies().size(), 0.0);
269 for (size_t i_culled = 0; i_culled < culled.size(); ++i_culled) {
270 const size_t i_full = m_speciesIndexMap[i_culled];
271 full[i_full] += culled[i_culled];
272 }
273 return full;
274 }
275
276 std::vector<double> FileDefinedEngineView::mapFullToView(const std::vector<double>& full) const {
277 std::vector<double> culled(m_activeSpecies.size(), 0.0);
278 for (size_t i_culled = 0; i_culled < m_activeSpecies.size(); ++i_culled) {
279 const size_t i_full = m_speciesIndexMap[i_culled];
280 culled[i_culled] = full[i_full];
281 }
282 return culled;
283 }
284
285 size_t FileDefinedEngineView::mapViewToFullSpeciesIndex(size_t culledSpeciesIndex) const {
286 if (culledSpeciesIndex < 0 || culledSpeciesIndex >= static_cast<int>(m_speciesIndexMap.size())) {
287 LOG_ERROR(m_logger, "Defined index {} is out of bounds for species index map of size {}.", culledSpeciesIndex, m_speciesIndexMap.size());
288 m_logger->flush_log();
289 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()) + ".");
290 }
291 return m_speciesIndexMap[culledSpeciesIndex];
292 }
293
294 size_t FileDefinedEngineView::mapViewToFullReactionIndex(size_t culledReactionIndex) const {
295 if (culledReactionIndex < 0 || culledReactionIndex >= static_cast<int>(m_reactionIndexMap.size())) {
296 LOG_ERROR(m_logger, "Defined index {} is out of bounds for reaction index map of size {}.", culledReactionIndex, m_reactionIndexMap.size());
297 m_logger->flush_log();
298 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()) + ".");
299 }
300 return m_reactionIndexMap[culledReactionIndex];
301 }
302
304 if (m_isStale) {
305 LOG_ERROR(m_logger, "File defined engine view is stale. Please call update() with a valid NetIn object.");
306 m_logger->flush_log();
307 throw std::runtime_error("File defined engine view is stale. Please call update() with a valid NetIn object.");
308 }
309 }
310}
Abstract class for engines supporting Jacobian and stoichiometry operations.
const io::NetworkFileParser & m_parser
Active species in the defined engine.
double getJacobianMatrixEntry(const int i_defined, const int j_defined) const override
Gets an entry from the Jacobian matrix for the active species.
std::string m_fileName
Parser for the network file.
void generateStoichiometryMatrix() override
Generates the stoichiometry matrix for the active reactions and species.
std::vector< fourdst::atomic::Species > m_activeSpecies
Active reactions in the defined engine.
StepDerivatives< double > 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.
void buildFromFile(const std::string &fileName)
Builds the active species and reaction sets from a file.
void generateJacobianMatrix(const std::vector< double > &Y_defined, const double T9, const double rho) override
Generates the Jacobian matrix for the active species.
const DynamicEngine & getBaseEngine() const override
Gets the base engine.
std::vector< size_t > constructSpeciesIndexMap() const
Constructs the species index map.
const reaction::LogicalReactionSet & getNetworkReactions() const override
Gets the set of active logical reactions in the network.
bool m_isStale
A flag indicating whether the view is stale and needs to be updated.
size_t mapViewToFullReactionIndex(size_t definedReactionIndex) const
Maps a culled reaction index to a full reaction index.
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Gets the list of active species in the network defined by the file.
FileDefinedEngineView(DynamicEngine &baseEngine, const std::string &fileName, const io::NetworkFileParser &parser)
Constructs a FileDefinedEngineView.
void setNetworkFile(const std::string &fileName)
Sets a new network file to define the active reactions.
std::vector< double > mapFullToView(const std::vector< double > &full) const
Maps a vector of full abundances to a vector of culled abundances.
quill::Logger * m_logger
A pointer to the logger instance.
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.
size_t mapViewToFullSpeciesIndex(size_t definedSpeciesIndex) const
Maps a culled species index to a full species index.
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.
void update(const NetIn &netIn) override
Updates the engine view if it is marked as stale.
std::unordered_map< fourdst::atomic::Species, double > getSpeciesTimescales(const std::vector< double > &Y_defined, const double T9, const double rho) const override
Computes timescales for all active species in the network.
DynamicEngine & m_baseEngine
The underlying engine to which this view delegates calculations.
void setScreeningModel(screening::ScreeningType model) override
Sets the screening model for the base engine.
std::vector< double > mapViewToFull(const std::vector< double > &defined) const
Maps a vector of culled abundances to a vector of full abundances.
screening::ScreeningType getScreeningModel() const override
Gets the screening model from the base engine.
reaction::LogicalReactionSet m_activeReactions
Maps indices of active species to indices in the full network.
void validateNetworkState() const
Validates that the FileDefinedEngineView is not stale.
std::vector< size_t > constructReactionIndexMap() const
Constructs the reaction index map.
std::vector< size_t > m_speciesIndexMap
Maps indices of active reactions to indices in the full network.
std::vector< size_t > m_reactionIndexMap
An abstract base class for network file parsers.
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
Definition reaction.h:557
ScreeningType
Enumerates the available plasma screening models.
Structure holding derivatives and energy generation for a network step.
T nuclearEnergyGenerationRate
Specific energy generation rate (e.g., erg/g/s).
std::vector< T > dydt
Derivatives of abundances (dY/dt for each species).