Loading...
Searching...
No Matches
engine_defined.cpp
Go to the documentation of this file.
#include "gridfire/engine/views/engine_defined.h"
#include <ranges>
#include "quill/LogMacros.h"
#include <string>
#include <vector>
#include <unordered_set>
#include <stdexcept>
#include <unordered_map>
#include <utility>
namespace gridfire {
using fourdst::atomic::Species;
DefinedEngineView::DefinedEngineView(const std::vector<std::string>& peNames, DynamicEngine& baseEngine) :
m_baseEngine(baseEngine) {
collect(peNames);
}
const DynamicEngine & DefinedEngineView::getBaseEngine() const {
return m_baseEngine;
}
const std::vector<Species> & DefinedEngineView::getNetworkSpecies() const {
return m_activeSpecies;
}
std::expected<StepDerivatives<double>, expectations::StaleEngineError> DefinedEngineView::calculateRHSAndEnergy(
const std::vector<double> &Y_defined,
const double T9,
const double rho
) const {
validateNetworkState();
const auto Y_full = mapViewToFull(Y_defined);
const auto result = m_baseEngine.calculateRHSAndEnergy(Y_full, T9, rho);
if (!result) {
return std::unexpected{result.error()};
}
const auto [dydt, nuclearEnergyGenerationRate] = result.value();
StepDerivatives<double> definedResults;
definedResults.nuclearEnergyGenerationRate = nuclearEnergyGenerationRate;
definedResults.dydt = mapFullToView(dydt);
return definedResults;
}
void DefinedEngineView::generateJacobianMatrix(
const std::vector<double> &Y_dynamic,
const double T9,
const double rho
) const {
validateNetworkState();
const auto Y_full = mapViewToFull(Y_dynamic);
m_baseEngine.generateJacobianMatrix(Y_full, T9, rho);
}
double DefinedEngineView::getJacobianMatrixEntry(
const int i_defined,
const int j_defined
) const {
validateNetworkState();
const size_t i_full = mapViewToFullSpeciesIndex(i_defined);
const size_t j_full = mapViewToFullSpeciesIndex(j_defined);
return m_baseEngine.getJacobianMatrixEntry(i_full, j_full);
}
void DefinedEngineView::generateStoichiometryMatrix() {
validateNetworkState();
m_baseEngine.generateStoichiometryMatrix();
}
int DefinedEngineView::getStoichiometryMatrixEntry(
const int speciesIndex_defined,
const int reactionIndex_defined
) const {
validateNetworkState();
const size_t i_full = mapViewToFullSpeciesIndex(speciesIndex_defined);
const size_t j_full = mapViewToFullReactionIndex(reactionIndex_defined);
return m_baseEngine.getStoichiometryMatrixEntry(i_full, j_full);
}
double DefinedEngineView::calculateMolarReactionFlow(
const reaction::Reaction &reaction,
const std::vector<double> &Y_defined,
const double T9,
const double rho
) const {
validateNetworkState();
if (!m_activeReactions.contains(reaction)) {
LOG_ERROR(m_logger, "Reaction '{}' is not part of the active reactions in the DefinedEngineView.", reaction.id());
m_logger -> flush_log();
throw std::runtime_error("Reaction not found in active reactions: " + std::string(reaction.id()));
}
const auto Y_full = mapViewToFull(Y_defined);
return m_baseEngine.calculateMolarReactionFlow(reaction, Y_full, T9, rho);
}
const reaction::LogicalReactionSet & DefinedEngineView::getNetworkReactions() const {
validateNetworkState();
return m_activeReactions;
}
void DefinedEngineView::setNetworkReactions(const reaction::LogicalReactionSet &reactions) {
std::vector<std::string> peNames;
for (const auto& reaction : reactions) {
peNames.push_back(std::string(reaction.id()));
}
collect(peNames);
}
std::expected<std::unordered_map<Species, double>, expectations::StaleEngineError> DefinedEngineView::getSpeciesTimescales(
const std::vector<double> &Y_defined,
const double T9,
const double rho
) const {
validateNetworkState();
const auto Y_full = mapViewToFull(Y_defined);
const auto result = m_baseEngine.getSpeciesTimescales(Y_full, T9, rho);
if (!result) {
return std::unexpected{result.error()};
}
const auto& fullTimescales = result.value();
std::unordered_map<Species, double> definedTimescales;
for (const auto& active_species : m_activeSpecies) {
if (fullTimescales.contains(active_species)) {
definedTimescales[active_species] = fullTimescales.at(active_species);
}
}
return definedTimescales;
}
std::expected<std::unordered_map<fourdst::atomic::Species, double>, expectations::StaleEngineError>
DefinedEngineView::getSpeciesDestructionTimescales(
const std::vector<double> &Y_defined,
const double T9,
const double rho
) const {
validateNetworkState();
const auto Y_full = mapViewToFull(Y_defined);
const auto result = m_baseEngine.getSpeciesDestructionTimescales(Y_full, T9, rho);
if (!result) {
return std::unexpected{result.error()};
}
const auto& destructionTimescales = result.value();
std::unordered_map<Species, double> definedTimescales;
for (const auto& active_species : m_activeSpecies) {
if (destructionTimescales.contains(active_species)) {
definedTimescales[active_species] = destructionTimescales.at(active_species);
}
}
return definedTimescales;
}
fourdst::composition::Composition DefinedEngineView::update(const NetIn &netIn) {
return m_baseEngine.update(netIn);
}
bool DefinedEngineView::isStale(const NetIn &netIn) {
return m_baseEngine.isStale(netIn);
}
void DefinedEngineView::setScreeningModel(const screening::ScreeningType model) {
m_baseEngine.setScreeningModel(model);
}
screening::ScreeningType DefinedEngineView::getScreeningModel() const {
return m_baseEngine.getScreeningModel();
}
int DefinedEngineView::getSpeciesIndex(const Species &species) const {
validateNetworkState();
const auto it = std::ranges::find(m_activeSpecies, species);
if (it != m_activeSpecies.end()) {
return static_cast<int>(std::distance(m_activeSpecies.begin(), it));
} else {
LOG_ERROR(m_logger, "Species '{}' not found in active species list.", species.name());
m_logger->flush_log();
throw std::runtime_error("Species not found in active species list: " + std::string(species.name()));
}
}
std::vector<double> DefinedEngineView::mapNetInToMolarAbundanceVector(const NetIn &netIn) const {
std::vector<double> Y(m_activeSpecies.size(), 0.0); // Initialize with zeros
for (const auto& [symbol, entry] : netIn.composition) {
auto it = std::ranges::find(m_activeSpecies, entry.isotope());
if (it != m_activeSpecies.end()) {
Y[getSpeciesIndex(entry.isotope())] = netIn.composition.getMolarAbundance(symbol); // Map species to their molar abundance
}
}
return Y; // Return the vector of molar abundances
}
PrimingReport DefinedEngineView::primeEngine(const NetIn &netIn) {
return m_baseEngine.primeEngine(netIn);
}
std::vector<size_t> DefinedEngineView::constructSpeciesIndexMap() const {
LOG_TRACE_L3(m_logger, "Constructing species index map for DefinedEngineView...");
std::unordered_map<Species, size_t> fullSpeciesReverseMap;
const auto& fullSpeciesList = m_baseEngine.getNetworkSpecies();
fullSpeciesReverseMap.reserve(fullSpeciesList.size());
for (size_t i = 0; i < fullSpeciesList.size(); ++i) {
fullSpeciesReverseMap[fullSpeciesList[i]] = i;
}
std::vector<size_t> speciesIndexMap;
speciesIndexMap.reserve(m_activeSpecies.size());
for (const auto& active_species : m_activeSpecies) {
auto it = fullSpeciesReverseMap.find(active_species);
if (it != fullSpeciesReverseMap.end()) {
speciesIndexMap.push_back(it->second);
} else {
LOG_ERROR(m_logger, "Species '{}' not found in full species map.", active_species.name());
m_logger -> flush_log();
throw std::runtime_error("Species not found in full species map: " + std::string(active_species.name()));
}
}
LOG_TRACE_L3(m_logger, "Species index map constructed with {} entries.", speciesIndexMap.size());
return speciesIndexMap;
}
std::vector<size_t> DefinedEngineView::constructReactionIndexMap() const {
LOG_TRACE_L3(m_logger, "Constructing reaction index map for DefinedEngineView...");
// --- Step 1: Create a reverse map using the reaction's unique ID as the key. ---
std::unordered_map<std::string_view, size_t> fullReactionReverseMap;
const auto& fullReactionSet = m_baseEngine.getNetworkReactions();
fullReactionReverseMap.reserve(fullReactionSet.size());
for (size_t i_full = 0; i_full < fullReactionSet.size(); ++i_full) {
fullReactionReverseMap[fullReactionSet[i_full].id()] = i_full;
}
// --- Step 2: Build the final index map using the active reaction set. ---
std::vector<size_t> reactionIndexMap;
reactionIndexMap.reserve(m_activeReactions.size());
for (const auto& active_reaction_ptr : m_activeReactions) {
auto it = fullReactionReverseMap.find(active_reaction_ptr.id());
if (it != fullReactionReverseMap.end()) {
reactionIndexMap.push_back(it->second);
} else {
LOG_ERROR(m_logger, "Active reaction '{}' not found in base engine during reaction index map construction.", active_reaction_ptr.id());
m_logger->flush_log();
throw std::runtime_error("Mismatch between active reactions and base engine.");
}
}
LOG_TRACE_L3(m_logger, "Reaction index map constructed with {} entries.", reactionIndexMap.size());
return reactionIndexMap;
}
std::vector<double> DefinedEngineView::mapViewToFull(const std::vector<double>& culled) const {
std::vector<double> full(m_baseEngine.getNetworkSpecies().size(), 0.0);
for (size_t i_culled = 0; i_culled < culled.size(); ++i_culled) {
const size_t i_full = m_speciesIndexMap[i_culled];
full[i_full] += culled[i_culled];
}
return full;
}
std::vector<double> DefinedEngineView::mapFullToView(const std::vector<double>& full) const {
std::vector<double> culled(m_activeSpecies.size(), 0.0);
for (size_t i_culled = 0; i_culled < m_activeSpecies.size(); ++i_culled) {
const size_t i_full = m_speciesIndexMap[i_culled];
culled[i_culled] = full[i_full];
}
return culled;
}
size_t DefinedEngineView::mapViewToFullSpeciesIndex(size_t culledSpeciesIndex) const {
if (culledSpeciesIndex < 0 || culledSpeciesIndex >= m_speciesIndexMap.size()) {
LOG_ERROR(m_logger, "Defined index {} is out of bounds for species index map of size {}.", culledSpeciesIndex, m_speciesIndexMap.size());
m_logger->flush_log();
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()) + ".");
}
return m_speciesIndexMap[culledSpeciesIndex];
}
size_t DefinedEngineView::mapViewToFullReactionIndex(size_t culledReactionIndex) const {
if (culledReactionIndex < 0 || culledReactionIndex >= m_reactionIndexMap.size()) {
LOG_ERROR(m_logger, "Defined index {} is out of bounds for reaction index map of size {}.", culledReactionIndex, m_reactionIndexMap.size());
m_logger->flush_log();
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()) + ".");
}
return m_reactionIndexMap[culledReactionIndex];
}
void DefinedEngineView::validateNetworkState() const {
if (m_isStale) {
LOG_ERROR(m_logger, "DefinedEngineView is stale. Please call update() with a valid NetIn object.");
m_logger->flush_log();
throw std::runtime_error("DefinedEngineView is stale. Please call update() with a valid NetIn object.");
}
}
void DefinedEngineView::collect(const std::vector<std::string> &peNames) {
std::unordered_set<Species> seenSpecies;
const auto& fullNetworkReactionSet = m_baseEngine.getNetworkReactions();
for (const auto& peName : peNames) {
if (!fullNetworkReactionSet.contains(peName)) {
LOG_ERROR(m_logger, "Reaction with name '{}' not found in the base engine's network reactions. Aborting...", peName);
m_logger->flush_log();
throw std::runtime_error("Reaction with name '" + std::string(peName) + "' not found in the base engine's network reactions.");
}
auto reaction = fullNetworkReactionSet[peName];
for (const auto& reactant : reaction.reactants()) {
if (!seenSpecies.contains(reactant)) {
seenSpecies.insert(reactant);
m_activeSpecies.push_back(reactant);
}
}
for (const auto& product : reaction.products()) {
if (!seenSpecies.contains(product)) {
seenSpecies.insert(product);
m_activeSpecies.push_back(product);
}
}
m_activeReactions.add_reaction(reaction);
}
LOG_TRACE_L3(m_logger, "DefinedEngineView built with {} active species and {} active reactions.", m_activeSpecies.size(), m_activeReactions.size());
LOG_TRACE_L3(m_logger, "Active species: {}", [this]() -> std::string {
std::string result;
for (const auto& species : m_activeSpecies) {
result += std::string(species.name()) + ", ";
}
if (!result.empty()) {
result.pop_back(); // Remove last space
result.pop_back(); // Remove last comma
}
return result;
}());
LOG_TRACE_L3(m_logger, "Active reactions: {}", [this]() -> std::string {
std::string result;
for (const auto& reaction : m_activeReactions) {
result += std::string(reaction.id()) + ", ";
}
if (!result.empty()) {
result.pop_back(); // Remove last space
result.pop_back(); // Remove last comma
}
return result;
}());
m_speciesIndexMap = constructSpeciesIndexMap();
m_reactionIndexMap = constructReactionIndexMap();
m_isStale = false;
}
FileDefinedEngineView::FileDefinedEngineView(
DynamicEngine &baseEngine,
const std::string &fileName,
const io::NetworkFileParser &parser
):
DefinedEngineView(parser.parse(fileName), baseEngine),
m_fileName(fileName),
m_parser(parser) {}
}
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