GridFire 0.6.0
General Purpose Nuclear Network
|
An engine view that dynamically adapts the reaction network based on runtime conditions. More...
#include <engine_adaptive.h>
Classes | |
struct | ReactionFlow |
A struct to hold a reaction and its flow rate. More... | |
Public Member Functions | |
AdaptiveEngineView (DynamicEngine &baseEngine) | |
Constructs an AdaptiveEngineView. | |
fourdst::composition::Composition | update (const NetIn &netIn) override |
Updates the active species and reactions based on the current conditions. | |
bool | isStale (const NetIn &netIn) override |
const std::vector< fourdst::atomic::Species > & | getNetworkSpecies () const override |
Gets the list of active species in the network. | |
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. | |
void | generateJacobianMatrix (const std::vector< double > &Y_dynamic, const double T9, const double rho) const override |
Generates the Jacobian matrix for the active species. | |
double | getJacobianMatrixEntry (const int i_culled, const int j_culled) const override |
Gets an entry from the Jacobian matrix for the active species. | |
void | generateStoichiometryMatrix () override |
Generates the stoichiometry matrix for the active reactions and species. | |
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. | |
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. | |
const reaction::LogicalReactionSet & | getNetworkReactions () const override |
Gets the set of active logical reactions in the network. | |
void | setNetworkReactions (const reaction::LogicalReactionSet &reactions) override |
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. | |
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > | getSpeciesDestructionTimescales (const std::vector< double > &Y, double T9, double rho) const override |
const DynamicEngine & | getBaseEngine () const override |
Gets the base engine. | |
void | setScreeningModel (screening::ScreeningType model) override |
Sets the screening model for the base engine. | |
screening::ScreeningType | getScreeningModel () const override |
Gets the screening model from the base engine. | |
int | getSpeciesIndex (const fourdst::atomic::Species &species) const override |
Get the index of a species in the network. | |
std::vector< double > | mapNetInToMolarAbundanceVector (const NetIn &netIn) const override |
Map a NetIn object to a vector of molar abundances. | |
PrimingReport | primeEngine (const NetIn &netIn) override |
Prime the engine with initial conditions. | |
![]() | |
virtual void | generateJacobianMatrix (const std::vector< double > &Y_dynamic, double T9, double rho, const SparsityPattern &sparsityPattern) const |
virtual BuildDepthType | getDepth () const |
Get the depth of the network. | |
virtual void | rebuild (const fourdst::composition::Composition &comp, BuildDepthType depth) |
Rebuild the network with a specified depth. | |
![]() | |
virtual | ~Engine ()=default |
Virtual destructor. | |
![]() | |
virtual | ~EngineView ()=default |
Virtual destructor. | |
Private Types | |
using | Config = fourdst::config::Config |
using | LogManager = fourdst::logging::LogManager |
typedef std::pair< std::unordered_set< const reaction::LogicalReaction * >, std::unordered_set< fourdst::atomic::Species > > | RescueSet |
Private Member Functions | |
std::vector< size_t > | constructSpeciesIndexMap () const |
Constructs the species index map. | |
std::vector< size_t > | constructReactionIndexMap () const |
Constructs the reaction index map. | |
std::vector< double > | mapCulledToFull (const std::vector< double > &culled) const |
Maps a vector of culled abundances to a vector of full abundances. | |
std::vector< double > | mapFullToCulled (const std::vector< double > &full) const |
Maps a vector of full abundances to a vector of culled abundances. | |
size_t | mapCulledToFullSpeciesIndex (size_t culledSpeciesIndex) const |
Maps a culled species index to a full species index. | |
size_t | mapCulledToFullReactionIndex (size_t culledReactionIndex) const |
Maps a culled reaction index to a full reaction index. | |
void | validateState () const |
Validates that the AdaptiveEngineView is not stale. | |
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. | |
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. | |
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. | |
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) |
Finalizes the set of active species and reactions. | |
Private Attributes | |
Config & | m_config = Config::getInstance() |
A reference to the singleton Config instance, used for retrieving configuration parameters. | |
quill::Logger * | m_logger = LogManager::getInstance().getLogger("log") |
A pointer to the logger instance, used for logging messages. | |
DynamicEngine & | m_baseEngine |
The underlying engine to which this view delegates calculations. | |
std::vector< fourdst::atomic::Species > | m_activeSpecies |
The set of species that are currently active in the network. | |
reaction::LogicalReactionSet | m_activeReactions |
The set of reactions that are currently active in the network. | |
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 network. | |
std::vector< size_t > | m_reactionIndexMap |
A map from the indices of the active reactions to the indices of the corresponding reactions in the full network. | |
bool | m_isStale = true |
A flag indicating whether the view is stale and needs to be updated. | |
An engine view that dynamically adapts the reaction network based on runtime conditions.
This class implements an EngineView that dynamically culls species and reactions from the full reaction network based on their reaction flow rates and connectivity. This allows for efficient simulation of reaction networks by focusing computational effort on the most important species and reactions.
The AdaptiveEngineView maintains a subset of "active" species and reactions, and maps between the full network indices and the active subset indices. This allows the base engine to operate on the full network data, while the AdaptiveEngineView provides a reduced view for external clients.
The adaptation process is driven by the update()
method, which performs the following steps:
<DynamicEngine>
|
private |
|
private |
|
private |
|
explicit |
Constructs an AdaptiveEngineView.
baseEngine | The underlying DynamicEngine to which this view delegates calculations. |
Initializes the active species and reactions to the full network, and constructs the initial index maps.
|
private |
Calculates the molar reaction flow rate for all reactions in the full network.
This method iterates through all reactions in the base engine's network and calculates their molar flow rates based on the provided network input conditions (temperature, density, and composition). It also constructs a vector of molar abundances for all species in the full network.
netIn | The current network input, containing temperature, density, and composition. |
out_Y_Full | A vector that will be populated with the molar abundances of all species in the full network. |
out_Y_Full
.netIn.composition
. If the species is not found, its abundance is set to 0.0.calculateMolarReactionFlow
to get the flow rate.ReactionFlow
struct and adds it to the returned vector.
|
nodiscardoverridevirtual |
Calculates the molar reaction flow for a given reaction in the active network.
reaction | The reaction for which to calculate the flow. |
Y_culled | Vector of current abundances for the active species. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method maps the culled abundances to the full network abundances and calls the base engine to calculate the molar reaction flow.
std::runtime_error | If the AdaptiveEngineView is stale (i.e., update() has not been called). |
std::runtime_error | If the reaction is not part of the active reactions in the adaptive engine view. |
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Calculates the right-hand side (dY/dt) and energy generation for the active species.
Y_culled | A vector of abundances for the active species. |
T9 | The temperature in units of 10^9 K. |
rho | The density in g/cm^3. |
This method maps the culled abundances to the full network abundances, calls the base engine to calculate the RHS and energy generation, and then maps the full network derivatives back to the culled derivatives.
std::runtime_error | If the AdaptiveEngineView is stale (i.e., update() has not been called). |
Implements gridfire::Engine.
|
nodiscardprivate |
Constructs the reaction index map.
This method creates a map from the indices of the active reactions to the indices of the corresponding reactions in the full network.
|
nodiscardprivate |
Constructs the species index map.
This method creates a map from the indices of the active species to the indices of the corresponding species in the full network.
|
nodiscardprivate |
Culls reactions from the network based on their flow rates.
This method filters the list of all reactions, keeping only those with a flow rate above an absolute culling threshold. The threshold is calculated by multiplying the maximum flow rate by a relative culling threshold read from the configuration.
allFlows | A vector of all reactions and their flow rates. |
reachableSpecies | A set of all species reachable from the initial fuel. |
Y_full | A vector of molar abundances for all species in the full network. |
maxFlow | The maximum reaction flow rate in the network. |
RelativeCullingThreshold
from the configuration.absoluteCullingThreshold
by multiplying maxFlow
with the relative threshold.allFlows
.flowRate
is greater than the absoluteCullingThreshold
.
|
private |
Finalizes the set of active species and reactions.
This method takes the final list of culled reactions and populates the m_activeReactions
and m_activeSpecies
members. The active species are determined by collecting all reactants and products from the final reactions. The active species list is then sorted by mass.
finalReactions | A vector of pointers to the reactions to be included in the active set. |
m_activeReactions
is cleared and populated with the reactions from finalReactions
.m_activeSpecies
is cleared and populated with all unique species present in finalReactions
.m_activeSpecies
is sorted by atomic mass.
|
nodiscardprivate |
Finds all species that are reachable from the initial fuel through the reaction network.
This method performs a connectivity analysis to identify all species that can be produced starting from the initial fuel species. A species is considered part of the initial fuel if its mass fraction is above a certain threshold (ABUNDANCE_FLOOR
).
netIn | The current network input, containing the initial composition. |
reachable
and a queue to_visit
with the initial fuel species.reachable
set, all products of that reaction are added to the reachable
set.reachable
set.
|
overridevirtual |
Generates the Jacobian matrix for the active species.
Y_dynamic | A vector of abundances for the active species. |
T9 | The temperature in units of 10^9 K. |
rho | The density in g/cm^3. |
This method maps the culled abundances to the full network abundances and calls the base engine to generate the Jacobian matrix.
std::runtime_error | If the AdaptiveEngineView is stale (i.e., update() has not been called). |
Implements gridfire::DynamicEngine.
|
overridevirtual |
Generates the stoichiometry matrix for the active reactions and species.
This method calls the base engine to generate the stoichiometry matrix.
std::runtime_error | If the AdaptiveEngineView is stale (i.e., update() has not been called). |
Implements gridfire::DynamicEngine.
|
inlinenodiscardoverridevirtual |
Gets the base engine.
Implements gridfire::EngineView< DynamicEngine >.
|
nodiscardoverridevirtual |
Gets an entry from the Jacobian matrix for the active species.
i_culled | The row index (species index) in the culled matrix. |
j_culled | The column index (species index) in the culled matrix. |
This method maps the culled indices to the full network indices and calls the base engine to get the Jacobian matrix entry.
std::runtime_error | If the AdaptiveEngineView is stale (i.e., update() has not been called). |
std::out_of_range | If the culled index is out of bounds for the species index map. |
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Gets the set of active logical reactions in the network.
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Gets the list of active species in the network.
Implements gridfire::Engine.
|
nodiscardoverridevirtual |
Gets the screening model from the base engine.
This method delegates the call to the base engine to get the screening model.
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Get the index of a species in the network.
species | The species to look up. |
This method allows querying the index of a specific species in the engine's internal representation. It is useful for accessing species data efficiently.
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Computes timescales for all active species in the network.
Y_culled | Vector of current abundances for the active species. |
T9 | Temperature in units of 10^9 K. |
rho | Density in g/cm^3. |
This method maps the culled abundances to the full network abundances and calls the base engine to compute the species timescales.
std::runtime_error | If the AdaptiveEngineView is stale (i.e., update() has not been called). |
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Gets an entry from the stoichiometry matrix for the active species and reactions.
speciesIndex_culled | The index of the species in the culled species list. |
reactionIndex_culled | The index of the reaction in the culled reaction list. |
This method maps the culled indices to the full network indices and calls the base engine to get the stoichiometry matrix entry.
std::runtime_error | If the AdaptiveEngineView is stale (i.e., update() has not been called). |
std::out_of_range | If the culled index is out of bounds for the species or reaction index map. |
Implements gridfire::DynamicEngine.
|
overridevirtual |
Implements gridfire::DynamicEngine.
|
nodiscardprivate |
Maps a vector of culled abundances to a vector of full abundances.
culled | A vector of abundances for the active species. |
|
nodiscardprivate |
Maps a culled reaction index to a full reaction index.
culledReactionIndex | The index of the reaction in the culled reaction list. |
std::out_of_range | If the culled index is out of bounds for the reaction index map. |
|
nodiscardprivate |
Maps a culled species index to a full species index.
culledSpeciesIndex | The index of the species in the culled species list. |
std::out_of_range | If the culled index is out of bounds for the species index map. |
|
nodiscardprivate |
Maps a vector of full abundances to a vector of culled abundances.
full | A vector of abundances for the full network. |
|
nodiscardoverridevirtual |
Map a NetIn object to a vector of molar abundances.
netIn | The input conditions for the network. |
This method converts the input conditions into a vector of molar abundances, which can be used for further calculations or diagnostics.
Implements gridfire::DynamicEngine.
|
nodiscardoverridevirtual |
Prime the engine with initial conditions.
netIn | The input conditions for the network. |
This method is used to prepare the engine for calculations by setting up initial conditions, reactions, and species. It may involve compiling reaction rates, initializing internal data structures, and performing any necessary pre-computation.
Implements gridfire::DynamicEngine.
|
nodiscardprivate |
|
overridevirtual |
Implements gridfire::DynamicEngine.
|
overridevirtual |
Sets the screening model for the base engine.
This method delegates the call to the base engine to set the electron screening model.
model | The electron screening model to set. |
Implements gridfire::DynamicEngine.
|
overridevirtual |
Updates the active species and reactions based on the current conditions.
netIn | The current network input, containing temperature, density, and composition. |
This method performs the reaction flow calculation, reaction culling, connectivity analysis, and index map construction steps described above.
The culling thresholds are read from the configuration using the following keys:
gridfire:AdaptiveEngineView:RelativeCullingThreshold
(default: 1e-75)std::runtime_error | If there is a mismatch between the active reactions and the base engine. |
Implements gridfire::DynamicEngine.
|
private |
Validates that the AdaptiveEngineView is not stale.
std::runtime_error | If the AdaptiveEngineView is stale (i.e., update() has not been called). |
|
private |
The set of reactions that are currently active in the network.
|
private |
The set of species that are currently active in the network.
|
private |
The underlying engine to which this view delegates calculations.
|
private |
A reference to the singleton Config instance, used for retrieving configuration parameters.
|
private |
A flag indicating whether the view is stale and needs to be updated.
|
private |
A pointer to the logger instance, used for logging messages.
|
private |
A map from the indices of the active reactions to the indices of the corresponding reactions in the full network.
|
private |
A map from the indices of the active species to the indices of the corresponding species in the full network.