7#include "unsupported/Eigen/NonLinearOptimization"
50 std::vector<double>
m_Y;
73 const std::vector<double>& Y
94 static long bin(
double value,
double tol);
181 typedef std::tuple<std::vector<fourdst::atomic::Species>, std::vector<size_t>, std::vector<fourdst::atomic::Species>, std::vector<size_t>>
QSEPartition;
198 [[nodiscard]]
const std::vector<fourdst::atomic::Species> &
getNetworkSpecies()
const override;
226 const std::vector<double> &Y_full,
253 const std::vector<double> &Y_full,
328 const std::vector<double> &Y_full,
378 const std::vector<double> &Y,
402 const std::vector<double> &Y,
433 fourdst::composition::Composition
update(
498 const std::vector<std::vector<size_t>> ×cale_pools,
499 const std::vector<double> &Y,
532 const std::vector<double>& Y,
565 const std::string& filename,
566 const std::vector<double>& Y,
579 [[nodiscard]]
int getSpeciesIndex(
const fourdst::atomic::Species &species)
const override;
615 [[nodiscard]] std::vector<fourdst::atomic::Species>
getFastSpecies()
const;
627 [[nodiscard]]
const std::vector<fourdst::atomic::Species>&
getDynamicSpecies()
const;
649 const std::vector<double> &Y,
730 using InputType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
732 using JacobianType = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
775 const std::vector<size_t>& qse_solve_indices,
776 const std::vector<double>& Y_full_initial,
779 const Eigen::VectorXd& Y_scale
910 quill::Logger*
m_logger = LogManager::getInstance().getLogger(
"log");
980 const std::vector<double> &Y_full,
999 const std::unordered_set<size_t> &fast_reaction_indices
1021 const std::vector<QSEGroup> &candidate_groups,
1022 const std::vector<double>& Y,
1046 const std::vector<double> &Y_full,
1067 const std::vector<std::vector<size_t>>& pools,
1068 const std::vector<double> &Y,
1087 const std::vector<size_t>& species_pool
1109 const std::vector<std::vector<size_t>>& candidate_pools,
1110 const std::vector<double>& Y,
Abstract class for engines supporting Jacobian and stoichiometry operations.
Abstract base class for a "view" of a reaction network engine.
A reaction network engine that uses a graph-based representation.
GraphEngine & m_baseEngine
The base engine to which this view delegates calculations.
PrimingReport primeEngine(const NetIn &netIn) override
Primes the engine with a specific species.
MultiscalePartitioningEngineView(GraphEngine &baseEngine)
Constructs a MultiscalePartitioningEngineView.
void setScreeningModel(screening::ScreeningType model) override
Sets the electron screening model.
std::vector< QSEGroup > m_qse_groups
The list of identified equilibrium groups.
const std::vector< fourdst::atomic::Species > & getDynamicSpecies() const
Gets the dynamic species in the network.
const DynamicEngine & getBaseEngine() const override
Gets the base engine.
std::tuple< std::vector< fourdst::atomic::Species >, std::vector< size_t >, std::vector< fourdst::atomic::Species >, std::vector< size_t > > QSEPartition
Type alias for a QSE partition.
std::vector< size_t > m_dynamic_species_indices
Indices mapping the dynamic species back to the base engine's full species list.
std::vector< double > solveQSEAbundances(const std::vector< double > &Y_full, double T9, double rho)
Solves for the QSE abundances of the algebraic species in a given state.
std::vector< fourdst::atomic::Species > getFastSpecies() const
Gets the fast species in the network.
std::vector< size_t > m_activeReactionIndices
Indices of all reactions involving only active species.
std::vector< fourdst::atomic::Species > m_algebraic_species
Species that are treated as algebraic (in QSE) in the QSE groups.
fourdst::composition::Composition equilibrateNetwork(const std::vector< double > &Y, double T9, double rho)
Equilibrates the network by partitioning and solving for QSE abundances.
int getStoichiometryMatrixEntry(int speciesIndex, int reactionIndex) const override
Gets an entry from the stoichiometry matrix.
std::vector< size_t > m_algebraic_species_indices
Indices of algebraic species in the full network.
size_t identifyMeanSlowestPool(const std::vector< std::vector< size_t > > &pools, const std::vector< double > &Y, double T9, double rho) const
Identifies the pool with the slowest mean timescale.
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > getSpeciesTimescales(const std::vector< double > &Y, double T9, double rho) const override
Computes timescales for all species in the network.
std::vector< size_t > m_activeSpeciesIndices
Indices of all species considered active in the current partition (dynamic + algebraic).
const std::vector< fourdst::atomic::Species > & getNetworkSpecies() const override
Gets the list of species in the network.
fourdst::composition::Composition update(const NetIn &netIn) override
Updates the internal state of the engine, performing partitioning and QSE equilibration.
std::unordered_map< QSECacheKey, std::vector< double > > m_qse_abundance_cache
Cache for QSE abundances based on T9, rho, and Y.
std::expected< StepDerivatives< double >, expectations::StaleEngineError > calculateRHSAndEnergy(const std::vector< double > &Y_full, double T9, double rho) const override
Calculates the right-hand side (dY/dt) and energy generation.
double calculateMolarReactionFlow(const reaction::Reaction &reaction, const std::vector< double > &Y_full, double T9, double rho) const override
Calculates the molar reaction flow for a given reaction.
screening::ScreeningType getScreeningModel() const override
Gets the current electron screening model.
void partitionNetwork(const std::vector< double > &Y, double T9, double rho)
Partitions the network into dynamic and algebraic (QSE) groups based on timescales.
quill::Logger * m_logger
Logger instance for logging messages.
int getSpeciesIndex(const fourdst::atomic::Species &species) const override
Gets the index of a species in the full network.
std::expected< std::unordered_map< fourdst::atomic::Species, double >, expectations::StaleEngineError > getSpeciesDestructionTimescales(const std::vector< double > &Y, double T9, double rho) const override
Computes destruction timescales for all species in the network.
std::vector< QSEGroup > validateGroupsWithFluxAnalysis(const std::vector< QSEGroup > &candidate_groups, const std::vector< double > &Y, double T9, double rho) const
Validates candidate QSE groups using flux analysis.
CacheStats m_cacheStats
Statistics for the QSE abundance cache.
std::vector< double > mapNetInToMolarAbundanceVector(const NetIn &netIn) const override
Maps a NetIn struct to a molar abundance vector for the full network.
std::unordered_map< size_t, std::vector< size_t > > buildConnectivityGraph(const std::unordered_set< size_t > &fast_reaction_indices) const
Builds a connectivity graph from a set of fast reaction indices.
void generateStoichiometryMatrix() override
Generates the stoichiometry matrix for the network.
std::vector< QSEGroup > constructCandidateGroups(const std::vector< std::vector< size_t > > &candidate_pools, const std::vector< double > &Y, double T9, double rho) const
Constructs candidate QSE groups from connected timescale pools.
double getJacobianMatrixEntry(int i_full, int j_full) const override
Gets an entry from the previously generated Jacobian matrix.
void setNetworkReactions(const reaction::LogicalReactionSet &reactions) override
Sets the set of logical reactions in the network.
void generateJacobianMatrix(const std::vector< double > &Y_full, double T9, double rho) const override
Generates the Jacobian matrix for the current state.
void exportToDot(const std::string &filename, const std::vector< double > &Y, const double T9, const double rho) const
Exports the network to a DOT file for visualization.
std::vector< std::vector< size_t > > partitionByTimescale(const std::vector< double > &Y_full, double T9, double rho) const
Partitions the network by timescale.
const reaction::LogicalReactionSet & getNetworkReactions() const override
Gets the set of logical reactions in the network.
bool isStale(const NetIn &netIn) override
Checks if the engine's internal state is stale relative to the provided conditions.
std::vector< fourdst::atomic::Species > m_dynamic_species
The simplified set of species presented to the solver (the "slow" species).
std::vector< std::vector< size_t > > analyzeTimescalePoolConnectivity(const std::vector< std::vector< size_t > > ×cale_pools, const std::vector< double > &Y, double T9, double rho) const
Analyzes the connectivity of timescale pools.
Represents a single nuclear reaction from a specific data source.
Abstract interfaces for reaction network engines in GridFire.
Abstract interfaces for engine "views" in GridFire.
TemplatedReactionSet< LogicalReaction > LogicalReactionSet
A set of logical reactions.
ScreeningType
Enumerates the available plasma screening models.
Struct for tracking cache statistics.
size_t m_hit
Total number of cache hits.
size_t misses(const operators op=operators::All) const
Gets the number of misses for a specific operator or all operators.
void hit(const operators op=operators::Other)
Increments the hit counter for a given operator.
size_t m_miss
Total number of cache misses.
size_t hits(const operators op=operators::All) const
Gets the number of hits for a specific operator or all operators.
std::map< operators,size_t > m_operatorHits
Map from operators to the number of cache hits for that operator.
@ CalculateMolarReactionFlow
@ GetSpeciesDestructionTimescales
void miss(const operators op=operators::Other)
Increments the miss counter for a given operator.
std::map< operators,std::string > operatorsNameMap
Map from operators to their string names for logging.
std::map< operators,size_t > m_operatorMisses
Map from operators to the number of cache misses for that operator.
int inputs() const
Gets the number of input values to the functor (size of the variable vector).
EigenFunctor(MultiscalePartitioningEngineView &view, const std::vector< size_t > &qse_solve_indices, const std::vector< double > &Y_full_initial, const double T9, const double rho, const Eigen::VectorXd &Y_scale)
Constructs an EigenFunctor.
const std::vector< double > & m_Y_full_initial
Initial abundances of all species in the full network.
Eigen::Matrix< double, Eigen::Dynamic, 1 > InputType
Eigen::Matrix< double, Eigen::Dynamic, 1 > OutputType
const double m_rho
Density in g/cm^3.
const std::vector< size_t > & m_qse_solve_indices
Indices of the species to solve for in the QSE group.
int values() const
Gets the number of output values from the functor (size of the residual vector).
const double m_T9
Temperature in units of 10^9 K.
const Eigen::VectorXd & m_Y_scale
Scaling factors for the species abundances, used to improve solver stability.
int df(const InputType &v_qse, JacobianType &J_qse) const
Evaluates the Jacobian of the functor, J_qse = d(f_qse)/d(v_qse).
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > JacobianType
int operator()(const InputType &v_qse, OutputType &f_qse) const
Evaluates the functor's residual vector f_qse = dY_alg/dt.
MultiscalePartitioningEngineView * m_view
Pointer to the MultiscalePartitioningEngineView instance.
Struct representing a QSE group.
bool operator<(const QSEGroup &other) const
Less-than operator for QSEGroup, used for sorting.
std::set< size_t > species_indices
Indices of all species in this group.
double mean_timescale
Mean timescale of the group.
bool operator>(const QSEGroup &other) const
Greater-than operator for QSEGroup.
bool operator==(const QSEGroup &other) const
Equality operator for QSEGroup.
std::set< size_t > seed_indices
Indices of dynamic species in this group.
std::set< size_t > algebraic_indices
Indices of algebraic species in this group.
bool operator!=(const QSEGroup &other) const
Inequality operator for QSEGroup.
bool is_in_equilibrium
Flag set by flux analysis.
Captures the result of a network priming operation.
Configuration struct for the QSE cache.
double Yi_tol
Absolute tolerance to produce the same hash for species abundances.
double rho_tol
Absolute tolerance to produce the same hash for rho.
double T9_tol
Absolute tolerance to produce the same hash for T9.
Key struct for the QSE abundance cache.
QSECacheKey(const double T9, const double rho, const std::vector< double > &Y)
Constructs a QSECacheKey.
QSECacheConfig m_cacheConfig
size_t hash() const
Computes the hash value for this key.
std::size_t m_hash
Precomputed hash value for this key.
static long bin(double value, double tol)
Converts a value to a discrete bin based on a tolerance.
bool operator==(const QSECacheKey &other) const
Equality operator for QSECacheKey.
std::vector< double > m_Y
Note that the ordering of Y must match the dynamic species indices in the view.
size_t operator()(const gridfire::QSECacheKey &key) const noexcept
Computes the hash of a QSECacheKey for use in std::unordered_map.