refactor(reaction): refactored to an abstract reaction class in prep for weak reactions

This commit is contained in:
2025-08-14 13:33:46 -04:00
parent d920a55ba6
commit 0b77f2e269
81 changed files with 1050041 additions and 913 deletions

View File

@@ -215,9 +215,9 @@ namespace gridfire {
*
* @return Reference to the LogicalReactionSet containing all reactions.
*/
[[nodiscard]] virtual const reaction::LogicalReactionSet& getNetworkReactions() const = 0;
[[nodiscard]] virtual const reaction::ReactionSet& getNetworkReactions() const = 0;
virtual void setNetworkReactions(const reaction::LogicalReactionSet& reactions) = 0;
virtual void setNetworkReactions(const reaction::ReactionSet& reactions) = 0;
/**
* @brief Compute timescales for all species in the network.
@@ -305,7 +305,7 @@ namespace gridfire {
* engine's internal representation. It is useful for accessing species
* data efficiently.
*/
[[nodiscard]] virtual int getSpeciesIndex(const fourdst::atomic::Species &species) const = 0;
[[nodiscard]] virtual size_t getSpeciesIndex(const fourdst::atomic::Species &species) const = 0;
/**
* @brief Map a NetIn object to a vector of molar abundances.
@@ -357,6 +357,7 @@ namespace gridfire {
*/
virtual void rebuild(const fourdst::composition::Composition& comp, BuildDepthType depth) {
throw std::logic_error("Setting network depth not supported by this engine.");
// ReSharper disable once CppDFAUnreachableCode
}
};

View File

@@ -25,16 +25,10 @@
#include "cppad/utility/sparse_rc.hpp"
#include "cppad/speed/sparse_jac_fun.hpp"
#include "procedures/priming.h"
#include "quill/LogMacros.h"
// PERF: The function getNetReactionStoichiometry returns a map of species to their stoichiometric coefficients for a given reaction.
// this makes extra copies of the species, which is not ideal and could be optimized further.
// Even more relevant is the member m_reactionIDMap which makes copies of a REACLIBReaction for each reaction ID.
// REACLIBReactions are quite large data structures, so this could be a performance bottleneck.
// static bool isF17 = false;
namespace gridfire {
/**
* @brief Alias for CppAD AD type for double precision.
@@ -128,7 +122,7 @@ namespace gridfire {
* This constructor uses the given set of reactions to construct the
* reaction network.
*/
explicit GraphEngine(const reaction::LogicalReactionSet &reactions);
explicit GraphEngine(const reaction::ReactionSet &reactions);
/**
* @brief Calculates the right-hand side (dY/dt) and energy generation rate.
@@ -212,9 +206,9 @@ namespace gridfire {
* @brief Gets the set of logical reactions in the network.
* @return Reference to the LogicalReactionSet containing all reactions.
*/
[[nodiscard]] const reaction::LogicalReactionSet& getNetworkReactions() const override;
[[nodiscard]] const reaction::ReactionSet& getNetworkReactions() const override;
void setNetworkReactions(const reaction::LogicalReactionSet& reactions) override;
void setNetworkReactions(const reaction::ReactionSet& reactions) override;
/**
* @brief Gets an entry from the previously generated Jacobian matrix.
@@ -394,6 +388,8 @@ namespace gridfire {
*
* @param reaction The reaction for which to calculate the reverse rate.
* @param T9 Temperature in units of 10^9 K.
* @param rho
* @param Y
* @return Reverse rate for the reaction (e.g., mol/g/s).
*
* This method computes the reverse rate based on the forward rate and
@@ -401,7 +397,7 @@ namespace gridfire {
*/
[[nodiscard]] double calculateReverseRate(
const reaction::Reaction &reaction,
double T9
double T9, double rho, const std::vector<double> &Y
) const;
/**
@@ -426,7 +422,7 @@ namespace gridfire {
[[nodiscard]] double calculateReverseRateTwoBodyDerivative(
const reaction::Reaction &reaction,
const double T9,
const double reverseRate
double rho, const std::vector<double> &Y, const double reverseRate
) const;
/**
@@ -458,8 +454,8 @@ namespace gridfire {
* This method returns the index of the given species in the network's
* species vector. If the species is not found, it returns -1.
*/
[[nodiscard]] int getSpeciesIndex(
const fourdst::atomic::Species& species
[[nodiscard]] size_t getSpeciesIndex(
const fourdst::atomic::Species &species
) const override;
/**
@@ -574,7 +570,7 @@ namespace gridfire {
constants m_constants;
reaction::LogicalReactionSet m_reactions; ///< Set of REACLIB reactions in the network.
reaction::ReactionSet m_reactions; ///< Set of REACLIB reactions in the network.
std::unordered_map<std::string_view, reaction::Reaction*> m_reactionIDMap; ///< Map from reaction ID to REACLIBReaction. //PERF: This makes copies of REACLIBReaction and could be a performance bottleneck.
std::vector<fourdst::atomic::Species> m_networkSpecies; ///< Vector of unique species in the network.
@@ -654,7 +650,7 @@ namespace gridfire {
*
* @throws std::runtime_error If there are no species in the network.
*/
void recordADTape();
void recordADTape() const;
void collectAtomicReverseRateAtomicBases();
@@ -708,7 +704,7 @@ namespace gridfire {
std::vector<T> screeningFactors,
std::vector<T> Y,
size_t reactionIndex,
const reaction::LogicalReaction &reaction
const reaction::Reaction &reaction
) const;
/**
@@ -776,7 +772,7 @@ namespace gridfire {
std::vector<T> screeningFactors,
std::vector<T> Y,
size_t reactionIndex,
const reaction::LogicalReaction &reaction
const reaction::Reaction &reaction
) const {
if (!m_useReverseReactions) {
return static_cast<T>(0.0); // If reverse reactions are not used, return zero
@@ -800,7 +796,7 @@ namespace gridfire {
}
} else {
// A,B If not calling with an AD type, calculate the reverse rate directly
reverseRateConstant = calculateReverseRate(reaction, T9);
reverseRateConstant = calculateReverseRate(reaction, T9, 0, {});
}
// C. Get product multiplicities
@@ -888,14 +884,18 @@ namespace gridfire {
calculateMolarReactionFlow<T>(reaction, Y, T9, rho);
// 2. Calculate reverse reaction rate
T reverseMolarFlow = calculateReverseMolarReactionFlow<T>(
T9,
rho,
screeningFactors,
Y,
reactionIndex,
reaction
);
T reverseMolarFlow = static_cast<T>(0.0);
// Do not calculate reverse flow for weak reactions
if (reaction.type() == reaction::ReactionType::LOGICAL_REACLIB || reaction.type() == reaction::ReactionType::REACLIB) {
reverseMolarFlow = calculateReverseMolarReactionFlow<T>(
T9,
rho,
screeningFactors,
Y,
reactionIndex,
reaction
);
}
const T molarReactionFlow = forwardMolarReactionFlow - reverseMolarFlow; // Net molar reaction flow
@@ -930,7 +930,7 @@ namespace gridfire {
const T zero = static_cast<T>(0.0);
// --- Calculate the molar reaction rate (in units of [s^-1][cm^3(N-1)][mol^(1-N)] for N reactants) ---
const T k_reaction = reaction.calculate_rate(T9);
const T k_reaction = reaction.calculate_rate(T9, rho, Y);
// --- Cound the number of each reactant species to account for species multiplicity ---
std::unordered_map<std::string, int> reactant_counts;

View File

@@ -30,7 +30,7 @@ namespace gridfire {
* @return A LogicalReactionSet encapsulating the collected reactions for graph-based engines.
* @throws std::logic_error If the resolved network depth is zero (no reactions can be collected).
*/
reaction::LogicalReactionSet build_reaclib_nuclear_network(
reaction::ReactionSet build_reaclib_nuclear_network(
const fourdst::composition::Composition &composition,
BuildDepthType maxLayers = NetworkBuildDepth::Full,
bool reverse = false

View File

@@ -3,13 +3,8 @@
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/network.h"
#include "fourdst/composition/composition.h"
#include "fourdst/composition/atomicSpecies.h"
#include <map>
#include <ranges>
#include <sstream>
namespace gridfire {
@@ -30,7 +25,7 @@ namespace gridfire {
* @return PrimingReport encapsulating the results of the priming operation.
*/
PrimingReport primeNetwork(
const NetIn&,
const NetIn& netIn,
DynamicEngine& engine
);

View File

@@ -9,7 +9,6 @@
#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"
@@ -203,9 +202,9 @@ namespace gridfire {
*
* @return Reference to the LogicalReactionSet containing all active reactions.
*/
[[nodiscard]] const reaction::LogicalReactionSet& getNetworkReactions() const override;
[[nodiscard]] const reaction::ReactionSet& getNetworkReactions() const override;
void setNetworkReactions(const reaction::LogicalReactionSet& reactions) override;
void setNetworkReactions(const reaction::ReactionSet& reactions) override;
/**
* @brief Computes timescales for all active species in the network.
@@ -270,7 +269,7 @@ namespace gridfire {
*/
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
[[nodiscard]] int getSpeciesIndex(const fourdst::atomic::Species &species) const override;
[[nodiscard]] size_t getSpeciesIndex(const fourdst::atomic::Species &species) const override;
[[nodiscard]] std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const override;
@@ -289,7 +288,7 @@ namespace gridfire {
/** @brief The set of species that are currently active in the network. */
std::vector<fourdst::atomic::Species> m_activeSpecies;
/** @brief The set of reactions that are currently active in the network. */
reaction::LogicalReactionSet m_activeReactions;
reaction::ReactionSet m_activeReactions;
/** @brief 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_speciesIndexMap;
@@ -304,7 +303,7 @@ namespace gridfire {
* @brief A struct to hold a reaction and its flow rate.
*/
struct ReactionFlow {
const reaction::LogicalReaction* reactionPtr;
const reaction::Reaction* reactionPtr;
double flowRate;
};
private:
@@ -442,20 +441,20 @@ namespace gridfire {
* 4. A reaction is kept if its `flowRate` is greater than the `absoluteCullingThreshold`.
* 5. The pointers to the kept reactions are stored in a vector and returned.
*/
[[nodiscard]] std::vector<const reaction::LogicalReaction*> cullReactionsByFlow(
[[nodiscard]] std::vector<const reaction::Reaction*> 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;
typedef std::pair<std::unordered_set<const reaction::Reaction*>, 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 reaction::ReactionSet& activeReactions
) const;
/**
* @brief Finalizes the set of active species and reactions.
@@ -473,7 +472,7 @@ namespace gridfire {
* - `m_activeSpecies` is sorted by atomic mass.
*/
void finalizeActiveSet(
const std::vector<const reaction::LogicalReaction*>& finalReactions
const std::vector<const reaction::Reaction*>& finalReactions
);
};
}

View File

@@ -16,14 +16,14 @@ namespace gridfire{
class DefinedEngineView : public DynamicEngine, public EngineView<DynamicEngine> {
public:
DefinedEngineView(const std::vector<std::string>& peNames, DynamicEngine& baseEngine);
const DynamicEngine& getBaseEngine() const override;
[[nodiscard]] const DynamicEngine& getBaseEngine() const override;
// --- Engine Interface ---
/**
* @brief Gets the list of active species in the network defined by the file.
* @return A const reference to the vector of active species.
*/
const std::vector<fourdst::atomic::Species>& getNetworkSpecies() const override;
[[nodiscard]] const std::vector<fourdst::atomic::Species>& getNetworkSpecies() const override;
// --- DynamicEngine Interface ---
/**
@@ -37,7 +37,7 @@ namespace gridfire{
*
* @throws std::runtime_error If the view is stale (i.e., `update()` has not been called after `setNetworkFile()`).
*/
std::expected<StepDerivatives<double>, expectations::StaleEngineError> calculateRHSAndEnergy(
[[nodiscard]] std::expected<StepDerivatives<double>, expectations::StaleEngineError> calculateRHSAndEnergy(
const std::vector<double>& Y_defined,
const double T9,
const double rho
@@ -66,7 +66,7 @@ namespace gridfire{
* @throws std::runtime_error If the view is stale.
* @throws std::out_of_range If an index is out of bounds.
*/
double getJacobianMatrixEntry(
[[nodiscard]] double getJacobianMatrixEntry(
const int i_defined,
const int j_defined
) const override;
@@ -86,7 +86,7 @@ namespace gridfire{
* @throws std::runtime_error If the view is stale.
* @throws std::out_of_range If an index is out of bounds.
*/
int getStoichiometryMatrixEntry(
[[nodiscard]] int getStoichiometryMatrixEntry(
const int speciesIndex_defined,
const int reactionIndex_defined
) const override;
@@ -101,7 +101,7 @@ namespace gridfire{
*
* @throws std::runtime_error If the view is stale or if the reaction is not in the active set.
*/
double calculateMolarReactionFlow(
[[nodiscard]] double calculateMolarReactionFlow(
const reaction::Reaction& reaction,
const std::vector<double>& Y_defined,
const double T9,
@@ -114,9 +114,9 @@ namespace gridfire{
*
* @throws std::runtime_error If the view is stale.
*/
const reaction::LogicalReactionSet& getNetworkReactions() const override;
[[nodiscard]] const reaction::ReactionSet& getNetworkReactions() const override;
void setNetworkReactions(const reaction::LogicalReactionSet& reactions) override;
void setNetworkReactions(const reaction::ReactionSet& reactions) override;
/**
* @brief Computes timescales for all active species in the network.
*
@@ -168,7 +168,7 @@ namespace gridfire{
*/
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
[[nodiscard]] int getSpeciesIndex(const fourdst::atomic::Species &species) const override;
[[nodiscard]] size_t getSpeciesIndex(const fourdst::atomic::Species &species) const override;
[[nodiscard]] std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const override;
@@ -181,7 +181,7 @@ namespace gridfire{
///< Active species in the defined engine.
std::vector<fourdst::atomic::Species> m_activeSpecies;
///< Active reactions in the defined engine.
reaction::LogicalReactionSet m_activeReactions;
reaction::ReactionSet m_activeReactions;
///< Maps indices of active species to indices in the full network.
std::vector<size_t> m_speciesIndexMap;
@@ -198,7 +198,7 @@ namespace gridfire{
*
* @throws std::runtime_error If an active species is not found in the base engine's species list.
*/
std::vector<size_t> constructSpeciesIndexMap() const;
[[nodiscard]] std::vector<size_t> constructSpeciesIndexMap() const;
/**
* @brief Constructs the reaction index map.
@@ -210,7 +210,7 @@ namespace gridfire{
*
* @throws std::runtime_error If an active reaction is not found in the base engine's reaction list.
*/
std::vector<size_t> constructReactionIndexMap() const;
[[nodiscard]] std::vector<size_t> constructReactionIndexMap() const;
/**
* @brief Maps a vector of culled abundances to a vector of full abundances.
@@ -219,7 +219,7 @@ namespace gridfire{
* @return A vector of abundances for the full network, with the abundances of the active
* species copied from the defined vector.
*/
std::vector<double> mapViewToFull(const std::vector<double>& defined) const;
[[nodiscard]] std::vector<double> mapViewToFull(const std::vector<double>& defined) const;
/**
* @brief Maps a vector of full abundances to a vector of culled abundances.
@@ -228,7 +228,7 @@ namespace gridfire{
* @return A vector of abundances for the active species, with the abundances of the active
* species copied from the full vector.
*/
std::vector<double> mapFullToView(const std::vector<double>& full) const;
[[nodiscard]] std::vector<double> mapFullToView(const std::vector<double>& full) const;
/**
* @brief Maps a culled species index to a full species index.
@@ -238,7 +238,7 @@ namespace gridfire{
*
* @throws std::out_of_range If the defined index is out of bounds for the species index map.
*/
size_t mapViewToFullSpeciesIndex(size_t definedSpeciesIndex) const;
[[nodiscard]] size_t mapViewToFullSpeciesIndex(size_t definedSpeciesIndex) const;
/**
* @brief Maps a culled reaction index to a full reaction index.
@@ -248,7 +248,7 @@ namespace gridfire{
*
* @throws std::out_of_range If the defined index is out of bounds for the reaction index map.
*/
size_t mapViewToFullReactionIndex(size_t definedReactionIndex) const;
[[nodiscard]] size_t mapViewToFullReactionIndex(size_t definedReactionIndex) const;
void validateNetworkState() const;
@@ -263,8 +263,8 @@ namespace gridfire{
const std::string& fileName,
const io::NetworkFileParser& parser
);
std::string getNetworkFile() const { return m_fileName; }
const io::NetworkFileParser& getParser() const { return m_parser; }
[[nodiscard]] std::string getNetworkFile() const { return m_fileName; }
[[nodiscard]] const io::NetworkFileParser& getParser() const { return m_parser; }
private:
using Config = fourdst::config::Config;
using LogManager = fourdst::logging::LogManager;

View File

@@ -86,7 +86,7 @@ namespace gridfire {
* This method combines the hashes of the binned temperature, density, and
* each species abundance. The `bin()` static method is used for discretization.
*/
size_t hash() const;
[[nodiscard]] size_t hash() const;
/**
* @brief Converts a value to a discrete bin based on a tolerance.
@@ -110,20 +110,18 @@ namespace gridfire {
}
// Needs to be in this order (splitting gridfire namespace up) to avoid some issues with forward declarations and the () operator.
namespace std {
template <>
struct hash<gridfire::QSECacheKey> {
/**
template <>
struct std::hash<gridfire::QSECacheKey> {
/**
* @brief Computes the hash of a QSECacheKey for use in `std::unordered_map`.
* @param key The QSECacheKey to hash.
* @return The pre-computed hash value of the key.
*/
size_t operator()(const gridfire::QSECacheKey& key) const noexcept {
// The hash is pre-computed, so we just return it.
return key.m_hash;
}
};
} // namespace std
size_t operator()(const gridfire::QSECacheKey& key) const noexcept {
// The hash is pre-computed, so we just return it.
return key.m_hash;
}
}; // namespace std
namespace gridfire {
/**
@@ -356,7 +354,7 @@ namespace gridfire {
* @return A const reference to the `LogicalReactionSet` from the base engine,
* containing all reactions in the full network.
*/
[[nodiscard]] const reaction::LogicalReactionSet & getNetworkReactions() const override;
[[nodiscard]] const reaction::ReactionSet & getNetworkReactions() const override;
/**
* @brief Sets the set of logical reactions in the network.
@@ -375,7 +373,7 @@ namespace gridfire {
* @throws exceptions::UnableToSetNetworkReactionsError Always.
*/
void setNetworkReactions(
const reaction::LogicalReactionSet &reactions
const reaction::ReactionSet &reactions
) override;
/**
@@ -615,7 +613,7 @@ namespace gridfire {
* @par How
* This method delegates directly to the base engine's `getSpeciesIndex()`.
*/
[[nodiscard]] int getSpeciesIndex(const fourdst::atomic::Species &species) const override;
[[nodiscard]] size_t getSpeciesIndex(const fourdst::atomic::Species &species) const override;
/**
* @brief Maps a `NetIn` struct to a molar abundance vector for the full network.
@@ -841,12 +839,12 @@ namespace gridfire {
* @brief Gets the number of output values from the functor (size of the residual vector).
* @return The number of algebraic species being solved.
*/
[[nodiscard]] int values() const { return m_qse_solve_indices.size(); }
[[nodiscard]] size_t values() const { return m_qse_solve_indices.size(); }
/**
* @brief Gets the number of input values to the functor (size of the variable vector).
* @return The number of algebraic species being solved.
*/
[[nodiscard]] int inputs() const { return m_qse_solve_indices.size(); }
[[nodiscard]] size_t inputs() const { return m_qse_solve_indices.size(); }
/**
* @brief Evaluates the functor's residual vector `f_qse = dY_alg/dt`.
@@ -1038,25 +1036,6 @@ namespace gridfire {
double rho
) const;
/**
* @brief Builds a connectivity graph from a set of fast reaction indices.
*
* @param fast_reaction_indices A set of indices for reactions considered "fast".
* @return An unordered map representing the adjacency list of the connectivity graph,
* where keys are species indices and values are vectors of connected species indices.
*
* @par Purpose
* To represent the reaction pathways among a subset of reactions.
*
* @par How
* It iterates through the specified fast reactions. For each reaction, it creates
* a two-way edge in the graph between every reactant and every product, signifying
* that mass can flow between them.
*/
std::unordered_map<size_t, std::vector<size_t>> buildConnectivityGraph(
const std::unordered_set<size_t> &fast_reaction_indices
) const;
/**
* @brief Validates candidate QSE groups using flux analysis.
*

View File

@@ -3,11 +3,9 @@
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/engine/views/engine_defined.h"
#include "gridfire/network.h"
#include "fourdst/logging/logging.h"
#include "fourdst/composition/atomicSpecies.h"
#include "fourdst/composition/composition.h"
#include "quill/Logger.h"
@@ -66,7 +64,7 @@ namespace gridfire {
* @return Vector of reaction name strings containing the priming species.
* @throws std::runtime_error If no reactions involve the priming species.
*/
std::vector<std::string> constructPrimingReactionSet(
[[nodiscard]] std::vector<std::string> constructPrimingReactionSet(
const fourdst::atomic::Species& primingSpecies,
const DynamicEngine& baseEngine
) const;