Loading...
Searching...
No Matches
engine_adaptive.h
Go to the documentation of this file.
#pragma once
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/engine/views/engine_view_abstract.h"
#include "gridfire/screening/screening_abstract.h"
#include "gridfire/screening/screening_types.h"
#include "gridfire/network.h"
#include "fourdst/composition/atomicSpecies.h"
#include "fourdst/config/config.h"
#include "fourdst/logging/logging.h"
#include "gridfire/engine/procedures/priming.h"
#include "gridfire/engine/procedures/construction.h"
#include "quill/Logger.h"
namespace gridfire {
class AdaptiveEngineView final : public DynamicEngine, public EngineView<DynamicEngine> {
public:
explicit AdaptiveEngineView(DynamicEngine& baseEngine);
fourdst::composition::Composition update(const NetIn &netIn) override;
bool isStale(const NetIn& netIn) override;
[[nodiscard]] const std::vector<fourdst::atomic::Species>& getNetworkSpecies() const override;
[[nodiscard]] std::expected<StepDerivatives<double>, expectations::StaleEngineError> calculateRHSAndEnergy(
const std::vector<double> &Y_culled,
const double T9,
const double rho
) const override;
void generateJacobianMatrix(
const std::vector<double> &Y_dynamic,
const double T9,
const double rho
) const override;
[[nodiscard]] double getJacobianMatrixEntry(
const int i_culled,
const int j_culled
) const override;
void generateStoichiometryMatrix() override;
[[nodiscard]] int getStoichiometryMatrixEntry(
const int speciesIndex_culled,
const int reactionIndex_culled
) const override;
[[nodiscard]] double calculateMolarReactionFlow(
const reaction::Reaction &reaction,
const std::vector<double> &Y_culled,
double T9,
double rho
) const override;
[[nodiscard]] const reaction::LogicalReactionSet& getNetworkReactions() const override;
void setNetworkReactions(const reaction::LogicalReactionSet& reactions) override;
[[nodiscard]] std::expected<std::unordered_map<fourdst::atomic::Species, double>, expectations::StaleEngineError> getSpeciesTimescales(
const std::vector<double> &Y_culled,
double T9,
double rho
) const override;
[[nodiscard]] std::expected<std::unordered_map<fourdst::atomic::Species, double>, expectations::StaleEngineError> getSpeciesDestructionTimescales(
const std::vector<double> &Y,
double T9,
double rho
) const override;
[[nodiscard]] const DynamicEngine& getBaseEngine() const override { return m_baseEngine; }
void setScreeningModel(screening::ScreeningType model) override;
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
[[nodiscard]] int getSpeciesIndex(const fourdst::atomic::Species &species) const override;
[[nodiscard]] std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const override;
[[nodiscard]] PrimingReport primeEngine(const NetIn &netIn) override;
private:
using Config = fourdst::config::Config;
using LogManager = fourdst::logging::LogManager;
Config& m_config = Config::getInstance();
quill::Logger* m_logger = LogManager::getInstance().getLogger("log");
DynamicEngine& m_baseEngine;
std::vector<fourdst::atomic::Species> m_activeSpecies;
reaction::LogicalReactionSet m_activeReactions;
std::vector<size_t> m_speciesIndexMap;
std::vector<size_t> m_reactionIndexMap;
bool m_isStale = true;
private:
struct ReactionFlow {
const reaction::LogicalReaction* reactionPtr;
double flowRate;
};
private:
[[nodiscard]] std::vector<size_t> constructSpeciesIndexMap() const;
[[nodiscard]] std::vector<size_t> constructReactionIndexMap() const;
[[nodiscard]] std::vector<double> mapCulledToFull(const std::vector<double>& culled) const;
[[nodiscard]] std::vector<double> mapFullToCulled(const std::vector<double>& full) const;
[[nodiscard]] size_t mapCulledToFullSpeciesIndex(size_t culledSpeciesIndex) const;
[[nodiscard]] size_t mapCulledToFullReactionIndex(size_t culledReactionIndex) const;
void validateState() const;
std::vector<ReactionFlow> calculateAllReactionFlows(
const NetIn& netIn,
std::vector<double>& out_Y_Full
) const;
[[nodiscard]] std::unordered_set<fourdst::atomic::Species> findReachableSpecies(
const NetIn& netIn
) const;
[[nodiscard]] std::vector<const reaction::LogicalReaction*> cullReactionsByFlow(
const std::vector<ReactionFlow>& allFlows,
const std::unordered_set<fourdst::atomic::Species>& reachableSpecies,
const std::vector<double>& Y_full,
double maxFlow
) const;
typedef std::pair<std::unordered_set<const reaction::LogicalReaction*>, std::unordered_set<fourdst::atomic::Species>> RescueSet;
[[nodiscard]] RescueSet rescueEdgeSpeciesDestructionChannel(
const std::vector<double>& Y_full,
const double T9,
const double rho,
const std::vector<fourdst::atomic::Species>& activeSpecies,
const reaction::LogicalReactionSet& activeReactions
) const;
void finalizeActiveSet(
const std::vector<const reaction::LogicalReaction*>& finalReactions
);
};
}
gridfire::AdaptiveEngineView::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_adaptive.cpp:175
gridfire::AdaptiveEngineView::calculateMolarReactionFlow
double calculateMolarReactionFlow(const reaction::Reaction &reaction, const std::vector< double > &Y_culled, double T9, double rho) const override
Calculates the molar reaction flow for a given reaction in the active network.
Definition engine_adaptive.cpp:212
gridfire::AdaptiveEngineView::getScreeningModel
screening::ScreeningType getScreeningModel() const override
Gets the screening model from the base engine.
Definition engine_adaptive.cpp:296
gridfire::AdaptiveEngineView::findReachableSpecies
std::unordered_set< fourdst::atomic::Species > findReachableSpecies(const NetIn &netIn) const
Finds all species that are reachable from the initial fuel through the reaction network.
Definition engine_adaptive.cpp:399
gridfire::AdaptiveEngineView::getNetworkReactions
const reaction::LogicalReactionSet & getNetworkReactions() const override
Gets the set of active logical reactions in the network.
Definition engine_adaptive.cpp:229
gridfire::AdaptiveEngineView::m_config
Config & m_config
A reference to the singleton Config instance, used for retrieving configuration parameters.
Definition engine_adaptive.h:282
gridfire::AdaptiveEngineView::m_activeReactions
reaction::LogicalReactionSet m_activeReactions
The set of reactions that are currently active in the network.
Definition engine_adaptive.h:292
gridfire::AdaptiveEngineView::m_reactionIndexMap
std::vector< size_t > m_reactionIndexMap
A map from the indices of the active reactions to the indices of the corresponding reactions in the f...
Definition engine_adaptive.h:297
gridfire::AdaptiveEngineView::generateStoichiometryMatrix
void generateStoichiometryMatrix() override
Generates the stoichiometry matrix for the active reactions and species.
Definition engine_adaptive.cpp:197
gridfire::AdaptiveEngineView::mapCulledToFullSpeciesIndex
size_t mapCulledToFullSpeciesIndex(size_t culledSpeciesIndex) const
Maps a culled species index to a full species index.
Definition engine_adaptive.cpp:341
gridfire::AdaptiveEngineView::update
fourdst::composition::Composition update(const NetIn &netIn) override
Updates the active species and reactions based on the current conditions.
Definition engine_adaptive.cpp:88
gridfire::AdaptiveEngineView::mapFullToCulled
std::vector< double > mapFullToCulled(const std::vector< double > &full) const
Maps a vector of full abundances to a vector of culled abundances.
Definition engine_adaptive.cpp:332
gridfire::AdaptiveEngineView::cullReactionsByFlow
std::vector< const reaction::LogicalReaction * > cullReactionsByFlow(const std::vector< ReactionFlow > &allFlows, const std::unordered_set< fourdst::atomic::Species > &reachableSpecies, const std::vector< double > &Y_full, double maxFlow) const
Culls reactions from the network based on their flow rates.
Definition engine_adaptive.cpp:442
gridfire::AdaptiveEngineView::getJacobianMatrixEntry
double getJacobianMatrixEntry(const int i_culled, const int j_culled) const override
Gets an entry from the Jacobian matrix for the active species.
Definition engine_adaptive.cpp:186
gridfire::AdaptiveEngineView::m_baseEngine
DynamicEngine & m_baseEngine
The underlying engine to which this view delegates calculations.
Definition engine_adaptive.h:287
gridfire::AdaptiveEngineView::getSpeciesTimescales
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > getSpeciesTimescales(const std::vector< double > &Y_culled, double T9, double rho) const override
Computes timescales for all active species in the network.
Definition engine_adaptive.cpp:238
gridfire::AdaptiveEngineView::RescueSet
std::pair< std::unordered_set< const reaction::LogicalReaction * >, std::unordered_set< fourdst::atomic::Species > > RescueSet
Definition engine_adaptive.h:452
gridfire::AdaptiveEngineView::getSpeciesDestructionTimescales
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > getSpeciesDestructionTimescales(const std::vector< double > &Y, double T9, double rho) const override
Definition engine_adaptive.cpp:266
gridfire::AdaptiveEngineView::LogManager
fourdst::logging::LogManager LogManager
Definition engine_adaptive.h:280
gridfire::AdaptiveEngineView::m_speciesIndexMap
std::vector< size_t > m_speciesIndexMap
A map from the indices of the active species to the indices of the corresponding species in the full ...
Definition engine_adaptive.h:295
gridfire::AdaptiveEngineView::m_isStale
bool m_isStale
A flag indicating whether the view is stale and needs to be updated.
Definition engine_adaptive.h:300
gridfire::AdaptiveEngineView::getStoichiometryMatrixEntry
int getStoichiometryMatrixEntry(const int speciesIndex_culled, const int reactionIndex_culled) const override
Gets an entry from the stoichiometry matrix for the active species and reactions.
Definition engine_adaptive.cpp:202
gridfire::AdaptiveEngineView::mapCulledToFull
std::vector< double > mapCulledToFull(const std::vector< double > &culled) const
Maps a vector of culled abundances to a vector of full abundances.
Definition engine_adaptive.cpp:323
gridfire::AdaptiveEngineView::primeEngine
PrimingReport primeEngine(const NetIn &netIn) override
Definition engine_adaptive.cpp:308
gridfire::AdaptiveEngineView::setNetworkReactions
void setNetworkReactions(const reaction::LogicalReactionSet &reactions) override
Definition engine_adaptive.cpp:233
gridfire::AdaptiveEngineView::mapNetInToMolarAbundanceVector
std::vector< double > mapNetInToMolarAbundanceVector(const NetIn &netIn) const override
Definition engine_adaptive.cpp:300
gridfire::AdaptiveEngineView::rescueEdgeSpeciesDestructionChannel
RescueSet rescueEdgeSpeciesDestructionChannel(const std::vector< double > &Y_full, const double T9, const double rho, const std::vector< fourdst::atomic::Species > &activeSpecies, const reaction::LogicalReactionSet &activeReactions) const
Definition engine_adaptive.cpp:485
gridfire::AdaptiveEngineView::constructReactionIndexMap
std::vector< size_t > constructReactionIndexMap() const
Constructs the reaction index map.
Definition engine_adaptive.cpp:56
gridfire::AdaptiveEngineView::constructSpeciesIndexMap
std::vector< size_t > constructSpeciesIndexMap() const
Constructs the species index map.
Definition engine_adaptive.cpp:27
gridfire::AdaptiveEngineView::getSpeciesIndex
int getSpeciesIndex(const fourdst::atomic::Species &species) const override
Definition engine_adaptive.cpp:312
gridfire::AdaptiveEngineView::mapCulledToFullReactionIndex
size_t mapCulledToFullReactionIndex(size_t culledReactionIndex) const
Maps a culled reaction index to a full reaction index.
Definition engine_adaptive.cpp:350
gridfire::AdaptiveEngineView::finalizeActiveSet
void finalizeActiveSet(const std::vector< const reaction::LogicalReaction * > &finalReactions)
Finalizes the set of active species and reactions.
Definition engine_adaptive.cpp:641
gridfire::AdaptiveEngineView::setScreeningModel
void setScreeningModel(screening::ScreeningType model) override
Sets the screening model for the base engine.
Definition engine_adaptive.cpp:292
gridfire::AdaptiveEngineView::calculateAllReactionFlows
std::vector< ReactionFlow > calculateAllReactionFlows(const NetIn &netIn, std::vector< double > &out_Y_Full) const
Calculates the molar reaction flow rate for all reactions in the full network.
Definition engine_adaptive.cpp:368
gridfire::AdaptiveEngineView::m_logger
quill::Logger * m_logger
A pointer to the logger instance, used for logging messages.
Definition engine_adaptive.h:284
gridfire::AdaptiveEngineView::getNetworkSpecies
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Gets the list of active species in the network.
Definition engine_adaptive.cpp:148
gridfire::AdaptiveEngineView::isStale
bool isStale(const NetIn &netIn) override
Definition engine_adaptive.cpp:144
gridfire::AdaptiveEngineView::AdaptiveEngineView
AdaptiveEngineView(DynamicEngine &baseEngine)
Constructs an AdaptiveEngineView.
Definition engine_adaptive.cpp:16
gridfire::AdaptiveEngineView::validateState
void validateState() const
Validates that the AdaptiveEngineView is not stale.
Definition engine_adaptive.cpp:359
gridfire::AdaptiveEngineView::getBaseEngine
const DynamicEngine & getBaseEngine() const override
Gets the base engine.
Definition engine_adaptive.h:239
gridfire::AdaptiveEngineView::m_activeSpecies
std::vector< fourdst::atomic::Species > m_activeSpecies
The set of species that are currently active in the network.
Definition engine_adaptive.h:290
gridfire::AdaptiveEngineView::calculateRHSAndEnergy
std::expected< StepDerivatives< double >, expectations::StaleEngineError > calculateRHSAndEnergy(const std::vector< double > &Y_culled, const double T9, const double rho) const override
Calculates the right-hand side (dY/dt) and energy generation for the active species.
Definition engine_adaptive.cpp:152
gridfire::AdaptiveEngineView::Config
fourdst::config::Config Config
Definition engine_adaptive.h:279
gridfire::DynamicEngine
Abstract class for engines supporting Jacobian and stoichiometry operations.
Definition engine_abstract.h:130
gridfire::EngineView
Abstract base class for a "view" of a reaction network engine.
Definition engine_view_abstract.h:74
gridfire::reaction::LogicalReaction
Represents a "logical" reaction that aggregates rates from multiple sources.
Definition reaction.h:310
gridfire::reaction::Reaction
Represents a single nuclear reaction from a specific data source.
Definition reaction.h:72
construction.h
engine_abstract.h
Abstract interfaces for reaction network engines in GridFire.
engine_view_abstract.h
Abstract interfaces for engine "views" in GridFire.
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
network.h
priming.h
screening_abstract.h
screening_types.h
gridfire::AdaptiveEngineView::ReactionFlow
A struct to hold a reaction and its flow rate.
Definition engine_adaptive.h:306
gridfire::AdaptiveEngineView::ReactionFlow::reactionPtr
const reaction::LogicalReaction * reactionPtr
Definition engine_adaptive.h:307
gridfire::AdaptiveEngineView::ReactionFlow::flowRate
double flowRate
Definition engine_adaptive.h:308
gridfire::NetIn
Definition network.h:53
gridfire::PrimingReport
Captures the result of a network priming operation.
Definition reporting.h:67
gridfire::expectations::StaleEngineError
Definition expected_engine.h:44