5#include "quill/LogMacros.h"
9#include <unordered_set>
11#include <unordered_map>
15 using fourdst::atomic::Species;
31 const std::vector<double> &Y_defined,
38 const auto result =
m_baseEngine.calculateRHSAndEnergy(Y_full, T9, rho);
41 return std::unexpected{result.error()};
44 const auto [dydt, nuclearEnergyGenerationRate] = result.value();
48 return definedResults;
52 const std::vector<double> &Y_dynamic,
71 return m_baseEngine.getJacobianMatrixEntry(i_full, j_full);
81 const int speciesIndex_defined,
82 const int reactionIndex_defined
88 return m_baseEngine.getStoichiometryMatrixEntry(i_full, j_full);
93 const std::vector<double> &Y_defined,
100 LOG_ERROR(
m_logger,
"Reaction '{}' is not part of the active reactions in the DefinedEngineView.",
reaction.id());
102 throw std::runtime_error(
"Reaction not found in active reactions: " + std::string(
reaction.id()));
115 std::vector<std::string> peNames;
116 for (
const auto&
reaction : reactions) {
117 peNames.push_back(std::string(
reaction.id()));
123 const std::vector<double> &Y_defined,
130 const auto result =
m_baseEngine.getSpeciesTimescales(Y_full, T9, rho);
132 return std::unexpected{result.error()};
134 const auto& fullTimescales = result.value();
136 std::unordered_map<Species, double> definedTimescales;
138 if (fullTimescales.contains(active_species)) {
139 definedTimescales[active_species] = fullTimescales.at(active_species);
142 return definedTimescales;
147 const std::vector<double> &Y_defined,
154 const auto result =
m_baseEngine.getSpeciesDestructionTimescales(Y_full, T9, rho);
157 return std::unexpected{result.error()};
160 const auto& destructionTimescales = result.value();
162 std::unordered_map<Species, double> definedTimescales;
164 if (destructionTimescales.contains(active_species)) {
165 definedTimescales[active_species] = destructionTimescales.at(active_species);
168 return definedTimescales;
195 LOG_ERROR(
m_logger,
"Species '{}' not found in active species list.", species.name());
197 throw std::runtime_error(
"Species not found in active species list: " + std::string(species.name()));
203 for (
const auto& [symbol, entry] : netIn.
composition) {
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();
221 fullSpeciesReverseMap.reserve(fullSpeciesList.size());
223 for (
size_t i = 0; i < fullSpeciesList.size(); ++i) {
224 fullSpeciesReverseMap[fullSpeciesList[i]] = i;
227 std::vector<size_t> speciesIndexMap;
231 auto it = fullSpeciesReverseMap.find(active_species);
232 if (it != fullSpeciesReverseMap.end()) {
233 speciesIndexMap.push_back(it->second);
235 LOG_ERROR(
m_logger,
"Species '{}' not found in full species map.", active_species.name());
237 throw std::runtime_error(
"Species not found in full species map: " + std::string(active_species.name()));
240 LOG_TRACE_L3(
m_logger,
"Species index map constructed with {} entries.", speciesIndexMap.size());
241 return speciesIndexMap;
246 LOG_TRACE_L3(
m_logger,
"Constructing reaction index map for DefinedEngineView...");
249 std::unordered_map<std::string_view, size_t> fullReactionReverseMap;
250 const auto& fullReactionSet =
m_baseEngine.getNetworkReactions();
251 fullReactionReverseMap.reserve(fullReactionSet.size());
253 for (
size_t i_full = 0; i_full < fullReactionSet.size(); ++i_full) {
254 fullReactionReverseMap[fullReactionSet[i_full].id()] = i_full;
258 std::vector<size_t> reactionIndexMap;
262 auto it = fullReactionReverseMap.find(active_reaction_ptr.id());
264 if (it != fullReactionReverseMap.end()) {
265 reactionIndexMap.push_back(it->second);
267 LOG_ERROR(
m_logger,
"Active reaction '{}' not found in base engine during reaction index map construction.", active_reaction_ptr.id());
269 throw std::runtime_error(
"Mismatch between active reactions and base engine.");
273 LOG_TRACE_L3(
m_logger,
"Reaction index map constructed with {} entries.", reactionIndexMap.size());
274 return reactionIndexMap;
278 std::vector<double> full(
m_baseEngine.getNetworkSpecies().size(), 0.0);
279 for (
size_t i_culled = 0; i_culled < culled.size(); ++i_culled) {
281 full[i_full] += culled[i_culled];
288 for (
size_t i_culled = 0; i_culled <
m_activeSpecies.size(); ++i_culled) {
290 culled[i_culled] = full[i_full];
297 LOG_ERROR(
m_logger,
"Defined index {} is out of bounds for species index map of size {}.", culledSpeciesIndex,
m_speciesIndexMap.size());
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()) +
".");
306 LOG_ERROR(
m_logger,
"Defined index {} is out of bounds for reaction index map of size {}.", culledReactionIndex,
m_reactionIndexMap.size());
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()) +
".");
315 LOG_ERROR(
m_logger,
"DefinedEngineView is stale. Please call update() with a valid NetIn object.");
317 throw std::runtime_error(
"DefinedEngineView is stale. Please call update() with a valid NetIn object.");
322 std::unordered_set<Species> seenSpecies;
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);
329 throw std::runtime_error(
"Reaction with name '" + std::string(peName) +
"' not found in the base engine's network reactions.");
331 auto reaction = fullNetworkReactionSet[peName];
332 for (
const auto& reactant :
reaction.reactants()) {
333 if (!seenSpecies.contains(reactant)) {
334 seenSpecies.insert(reactant);
338 for (
const auto& product :
reaction.products()) {
339 if (!seenSpecies.contains(product)) {
340 seenSpecies.insert(product);
347 LOG_TRACE_L3(
m_logger,
"Active species: {}", [
this]() -> std::string {
350 result += std::string(species.name()) +
", ";
352 if (!result.empty()) {
358 LOG_TRACE_L3(
m_logger,
"Active reactions: {}", [
this]() -> std::string {
361 result += std::string(
reaction.id()) +
", ";
363 if (!result.empty()) {
381 const std::string &fileName,
PrimingReport primeEngine(const NetIn &netIn) override
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.
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Gets the list of active species in the network defined by the file.
double getJacobianMatrixEntry(const int i_defined, const int j_defined) const override
Gets an entry from the Jacobian matrix for the active species.
std::vector< double > mapFullToView(const std::vector< double > &full) const
Maps a vector of full abundances to a vector of culled abundances.
reaction::LogicalReactionSet m_activeReactions
Maps indices of active species to indices in the full network.
screening::ScreeningType getScreeningModel() const override
Gets the screening model from the base engine.
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
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.
quill::Logger * m_logger
Active species in the defined engine.
void validateNetworkState() const
std::vector< double > mapViewToFull(const std::vector< double > &defined) const
Maps a vector of culled abundances to a vector of full abundances.
std::vector< fourdst::atomic::Species > m_activeSpecies
Active reactions in the defined engine.
const DynamicEngine & getBaseEngine() const override
Access the underlying engine instance.
DynamicEngine & m_baseEngine
std::vector< double > mapNetInToMolarAbundanceVector(const NetIn &netIn) const override
bool isStale(const NetIn &netIn) override
void setNetworkReactions(const reaction::LogicalReactionSet &reactions) override
DefinedEngineView(const std::vector< std::string > &peNames, DynamicEngine &baseEngine)
std::vector< size_t > constructSpeciesIndexMap() const
Constructs the species index map.
size_t mapViewToFullReactionIndex(size_t definedReactionIndex) const
Maps a culled reaction index to a full reaction index.
std::vector< size_t > constructReactionIndexMap() const
Constructs the reaction index map.
void setScreeningModel(screening::ScreeningType model) override
Sets the screening model for the base engine.
int getSpeciesIndex(const fourdst::atomic::Species &species) const override
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.
std::vector< size_t > m_speciesIndexMap
Maps indices of active reactions to indices in the full network.
void generateStoichiometryMatrix() override
Generates the stoichiometry matrix for the active reactions and species.
void generateJacobianMatrix(const std::vector< double > &Y_dynamic, const double T9, const double rho) const override
Generates the Jacobian matrix for the active species.
void collect(const std::vector< std::string > &peNames)
const reaction::LogicalReactionSet & getNetworkReactions() const override
Gets the set of active logical reactions in the network.
fourdst::composition::Composition update(const NetIn &netIn) override
Updates the engine view if it is marked as stale.
size_t mapViewToFullSpeciesIndex(size_t definedSpeciesIndex) const
Maps a culled species index to a full species index.
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.
std::vector< size_t > m_reactionIndexMap
Abstract class for engines supporting Jacobian and stoichiometry operations.
const io::NetworkFileParser & m_parser
std::string m_fileName
Parser for the network file.
FileDefinedEngineView(DynamicEngine &baseEngine, const std::string &fileName, const io::NetworkFileParser &parser)
FileDefinedEngineView Implementation ///.
An abstract base class for network file parsers.
Represents a single nuclear reaction from a specific data source.
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
ScreeningType
Enumerates the available plasma screening models.
fourdst::composition::Composition composition
Composition of the network.
Captures the result of a network priming operation.
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).