feat(MultiscalePartitioningEngineView): added *much* more robust qse group identifiction and solving

This commit is contained in:
2025-07-10 09:36:05 -04:00
parent 1ac6b451b8
commit 7012eb819a
23 changed files with 1863 additions and 378 deletions

View File

@@ -123,7 +123,7 @@ namespace gridfire {
/**
* @brief Generate the Jacobian matrix for the current state.
*
* @param Y Vector of current abundances.
* @param Y_dynamic Vector of current abundances.
* @param T9 Temperature in units of 10^9 K.
* @param rho Density in g/cm^3.
*
@@ -131,7 +131,7 @@ namespace gridfire {
* for the current state. The matrix can then be accessed via getJacobianMatrixEntry().
*/
virtual void generateJacobianMatrix(
const std::vector<double>& Y,
const std::vector<double>& Y_dynamic,
double T9, double rho
) = 0;
@@ -265,5 +265,9 @@ namespace gridfire {
* @endcode
*/
[[nodiscard]] virtual screening::ScreeningType getScreeningModel() const = 0;
[[nodiscard]] virtual int getSpeciesIndex(const fourdst::atomic::Species &species) const = 0;
[[nodiscard]] virtual std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const = 0;
};
}

View File

@@ -17,10 +17,12 @@
#include <unordered_map>
#include <vector>
#include <memory>
#include <ranges>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include "cppad/cppad.hpp"
#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.
@@ -139,7 +141,7 @@ namespace gridfire {
/**
* @brief Generates the Jacobian matrix for the current state.
*
* @param Y Vector of current abundances.
* @param Y_dynamic Vector of current abundances.
* @param T9 Temperature in units of 10^9 K.
* @param rho Density in g/cm^3.
*
@@ -150,7 +152,7 @@ namespace gridfire {
* @see getJacobianMatrixEntry()
*/
void generateJacobianMatrix(
const std::vector<double>& Y,
const std::vector<double>& Y_dynamic,
const double T9,
const double rho
) override;
@@ -317,18 +319,48 @@ namespace gridfire {
[[nodiscard]] double calculateReverseRate(
const reaction::Reaction &reaction,
double T9,
double expFactor
double T9
) const;
double calculateReverseRateTwoBody(
const reaction::Reaction &reaction,
const double T9,
const double forwardRate,
const double expFactor
) const;
double calculateReverseRateTwoBodyDerivative(
const reaction::Reaction &reaction,
const double T9,
const double reverseRate
) const;
bool isUsingReverseReactions() const;
void setUseReverseReactions(bool useReverse);
int getSpeciesIndex(
const fourdst::atomic::Species& species
) const override;
std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const override;
private:
struct PrecomputedReaction {
// Forward cacheing
size_t reaction_index;
std::vector<size_t> unique_reactant_indices;
std::vector<int> reactant_powers;
double symmetry_factor;
std::vector<size_t> affected_species_indices;
std::vector<int> stoichiometric_coefficients;
// Reverse cacheing
std::vector<size_t> unique_product_indices; ///< Unique product indices for reverse reactions.
std::vector<int> product_powers; ///< Powers of each unique product in the reverse reaction.
double reverse_symmetry_factor; ///< Symmetry factor for reverse reactions.
};
struct constants {
@@ -337,7 +369,47 @@ namespace gridfire {
const double c = Constants::getInstance().get("c").value; ///< Speed of light in cm/s.
const double kB = Constants::getInstance().get("kB").value; ///< Boltzmann constant in erg/K.
};
private:
class AtomicReverseRate final : public CppAD::atomic_base<double> {
public:
AtomicReverseRate(
const reaction::Reaction& reaction,
const GraphEngine& engine
):
atomic_base<double>("AtomicReverseRate"),
m_reaction(reaction),
m_engine(engine) {}
bool forward(
size_t p,
size_t q,
const CppAD::vector<bool>& vx,
CppAD::vector<bool>& vy,
const CppAD::vector<double>& tx,
CppAD::vector<double>& ty
) override;
bool reverse(
size_t q,
const CppAD::vector<double>& tx,
const CppAD::vector<double>& ty,
CppAD::vector<double>& px,
const CppAD::vector<double>& py
) override;
bool for_sparse_jac(
size_t q,
const CppAD::vector<std::set<size_t>>&r,
CppAD::vector<std::set<size_t>>& s
) override;
bool rev_sparse_jac(
size_t q,
const CppAD::vector<std::set<size_t>>&rt,
CppAD::vector<std::set<size_t>>& st
) override;
private:
const reaction::Reaction& m_reaction;
const GraphEngine& m_engine;
};
private:
Config& m_config = Config::getInstance();
quill::Logger* m_logger = LogManager::getInstance().getLogger("log");
@@ -355,12 +427,15 @@ namespace gridfire {
boost::numeric::ublas::compressed_matrix<double> m_jacobianMatrix; ///< Jacobian matrix (species x species).
CppAD::ADFun<double> m_rhsADFun; ///< CppAD function for the right-hand side of the ODE.
std::vector<std::unique_ptr<AtomicReverseRate>> m_atomicReverseRates;
screening::ScreeningType m_screeningType = screening::ScreeningType::BARE; ///< Screening type for the reaction network. Default to no screening.
std::unique_ptr<screening::ScreeningModel> m_screeningModel = screening::selectScreeningModel(m_screeningType);
bool m_usePrecomputation = true; ///< Flag to enable or disable using precomputed reactions for efficiency. Mathematically, this should not change the results. Generally end users should not need to change this.
bool m_useReverseReactions = true; ///< Flag to enable or disable reverse reactions. If false, only forward reactions are considered.
std::vector<PrecomputedReaction> m_precomputedReactions; ///< Precomputed reactions for efficiency.
std::unique_ptr<partition::PartitionFunction> m_partitionFunction; ///< Partition function for the network.
@@ -418,8 +493,11 @@ namespace gridfire {
*/
void recordADTape();
void collectAtomicReverseRateAtomicBases();
void precomputeNetwork();
/**
* @brief Validates mass and charge conservation across all reactions.
*
@@ -449,24 +527,12 @@ namespace gridfire {
);
double calculateReverseRateTwoBody(
const reaction::Reaction &reaction,
const double T9,
const double forwardRate,
const double expFactor
) const;
double GraphEngine::calculateReverseRateTwoBodyDerivative(
const reaction::Reaction &reaction,
const double T9,
const double reverseRate
) const;
[[nodiscard]] StepDerivatives<double> calculateAllDerivativesUsingPrecomputation(
const std::vector<double> &Y_in,
const std::vector<double>& bare_rates,
double T9,
double rho
const std::vector<double> &bare_reverse_rates,
double T9, double rho
) const;
/**
@@ -490,6 +556,16 @@ namespace gridfire {
const T rho
) const;
template<IsArithmeticOrAD T>
T calculateReverseMolarReactionFlow(
T T9,
T rho,
std::vector<T> screeningFactors,
std::vector<T> Y,
size_t reactionIndex,
const reaction::LogicalReaction &reaction
) const;
/**
* @brief Calculates all derivatives (dY/dt) and the energy generation rate.
*
@@ -547,6 +623,74 @@ namespace gridfire {
};
template <IsArithmeticOrAD T>
T GraphEngine::calculateReverseMolarReactionFlow(
T T9,
T rho,
std::vector<T> screeningFactors,
std::vector<T> Y,
size_t reactionIndex,
const reaction::LogicalReaction &reaction
) const {
if (!m_useReverseReactions) {
return static_cast<T>(0.0); // If reverse reactions are not used, return zero
}
T reverseMolarFlow = static_cast<T>(0.0);
if (reaction.qValue() != 0.0) {
T reverseRateConstant = static_cast<T>(0.0);
if constexpr (std::is_same_v<T, ADDouble>) { // Check if T is an AD type at compile time
const auto& atomic_func_ptr = m_atomicReverseRates[reactionIndex];
if (atomic_func_ptr != nullptr) {
// A. Instantiate the atomic operator for the specific reaction
// B. Marshal the input vector
std::vector<T> ax = { T9 };
std::vector<T> ay(1);
(*atomic_func_ptr)(ax, ay);
reverseRateConstant = static_cast<T>(ay[0]);
} else {
return reverseMolarFlow; // If no atomic function is available, return zero
}
} else {
// A,B If not calling with an AD type, calculate the reverse rate directly
reverseRateConstant = calculateReverseRate(reaction, T9);
}
// C. Get product multiplicities
std::unordered_map<fourdst::atomic::Species, int> productCounts;
for (const auto& product : reaction.products()) {
productCounts[product]++;
}
// D. Calculate the symmetry factor
T reverseSymmetryFactor = static_cast<T>(1.0);
for (const auto &count: productCounts | std::views::values) {
reverseSymmetryFactor /= static_cast<T>(std::tgamma(static_cast<double>(count + 1))); // Gamma function for factorial
}
// E. Calculate the abundance term
T productAbundanceTerm = static_cast<T>(1.0);
for (const auto& [species, count] : productCounts) {
const int speciesIndex = m_speciesToIndexMap.at(species);
productAbundanceTerm *= CppAD::pow(Y[speciesIndex], count);
}
// F. Determine the power for the density term
const size_t num_products = reaction.products().size();
const T rho_power = CppAD::pow(rho, static_cast<T>(num_products > 1 ? num_products - 1 : 0)); // Density raised to the power of (N-1) for N products
// G. Assemble the reverse molar flow rate
reverseMolarFlow = screeningFactors[reactionIndex] *
reverseRateConstant *
productAbundanceTerm *
reverseSymmetryFactor *
rho_power;
}
return reverseMolarFlow;
}
template<IsArithmeticOrAD T>
StepDerivatives<T> GraphEngine::calculateAllDerivatives(
const std::vector<T> &Y_in, T T9, T rho) const {
@@ -594,13 +738,39 @@ namespace gridfire {
for (size_t reactionIndex = 0; reactionIndex < m_reactions.size(); ++reactionIndex) {
const auto& reaction = m_reactions[reactionIndex];
// 1. Calculate reaction rate
const T molarReactionFlow = screeningFactors[reactionIndex] * calculateMolarReactionFlow<T>(reaction, Y, T9, rho);
// 1. Calculate forward reaction rate
const T forwardMolarReactionFlow = screeningFactors[reactionIndex] *
calculateMolarReactionFlow<T>(reaction, Y, T9, rho);
// 2. Use the rate to update all relevant species derivatives (dY/dt)
// 2. Calculate reverse reaction rate
T reverseMolarFlow = calculateReverseMolarReactionFlow<T>(
T9,
rho,
screeningFactors,
Y,
reactionIndex,
reaction
);
const T molarReactionFlow = forwardMolarReactionFlow - reverseMolarFlow; // Net molar reaction flow
std::stringstream ss;
ss << "Forward: " << forwardMolarReactionFlow
<< ", Reverse: " << reverseMolarFlow
<< ", Net: " << molarReactionFlow;
LOG_DEBUG(
m_logger,
"Reaction: {}, {}",
reaction.peName(),
ss.str()
);
// std::cout << "Forward molar flow for reaction " << reaction.peName() << ": " << forwardMolarReactionFlow << std::endl;
// std::cout << "Reverse molar flow for reaction " << reaction.peName() << ": " << reverseMolarFlow << std::endl;
// std::cout << "Net molar flow for reaction " << reaction.peName() << ": " << molarReactionFlow << std::endl;
// 3. Use the rate to update all relevant species derivatives (dY/dt)
for (size_t speciesIndex = 0; speciesIndex < m_networkSpecies.size(); ++speciesIndex) {
const T nu_ij = static_cast<T>(m_stoichiometryMatrix(speciesIndex, reactionIndex));
result.dydt[speciesIndex] += threshold_flag * nu_ij * molarReactionFlow / rho;
result.dydt[speciesIndex] += threshold_flag * nu_ij * molarReactionFlow;
}
}
@@ -644,6 +814,7 @@ namespace gridfire {
for (const auto& reactant : reaction.reactants()) {
reactant_counts[std::string(reactant.name())]++;
}
const int totalReactants = static_cast<int>(reaction.reactants().size());
// --- Accumulator for the molar concentration ---
auto molar_concentration_product = static_cast<T>(1.0);
@@ -657,56 +828,23 @@ namespace gridfire {
const T Yi = Y[species_index];
// --- Check if the species abundance is below the threshold where we ignore reactions ---
threshold_flag *= CppAD::CondExpLt(Yi, Y_threshold, zero, one);
// --- Convert from molar abundance to molar concentration ---
T molar_concentration = Yi * rho;
// threshold_flag *= CppAD::CondExpLt(Yi, Y_threshold, zero, one);
// --- If count is > 1 , we need to raise the molar concentration to the power of count since there are really count bodies in that reaction ---
molar_concentration_product *= CppAD::pow(molar_concentration, static_cast<T>(count)); // ni^count
molar_concentration_product *= CppAD::pow(Yi, static_cast<T>(count)); // ni^count
// --- Apply factorial correction for identical reactions ---
if (count > 1) {
molar_concentration_product /= static_cast<T>(std::tgamma(static_cast<double>(count + 1))); // Gamma function for factorial
}
}
// --- Final reaction flow calculation [mol][s^-1][cm^-3] ---
// --- Final reaction flow calculation [mol][s^-1][g^-1] ---
// Note: If the threshold flag ever gets set to zero this will return zero.
// This will result basically in multiple branches being written to the AD tape, which will make
// the tape more expensive to record, but it will also mean that we only need to record it once for
// the entire network.
return molar_concentration_product * k_reaction * threshold_flag;
const T densityTerm = CppAD::pow(rho, totalReactants > 1 ? static_cast<T>(totalReactants - 1) : zero); // Density raised to the power of (N-1) for N reactants
return molar_concentration_product * k_reaction * threshold_flag * densityTerm;
}
class AtomicReverseRate final : public CppAD::atomic_base<double> {
public:
AtomicReverseRate(
const reaction::Reaction& reaction,
const GraphEngine& engine
):
atomic_base<double>("AtomicReverseRate"),
m_reaction(reaction),
m_engine(engine) {}
bool forward(
size_t p,
size_t q,
const CppAD::vector<bool>& vx,
CppAD::vector<bool>& vy,
const CppAD::vector<double>& tx,
CppAD::vector<double>& ty
) override;
bool reverse(
size_t id,
size_t an,
const CppAD::vector<double>& tx,
const CppAD::vector<double>& ty,
CppAD::vector<double>& px,
const CppAD::vector<double>& py
);
private:
const double m_kB = Constants::getInstance().get("k_b").value; ///< Boltzmann constant in erg/K.
const reaction::Reaction& m_reaction;
const GraphEngine& m_engine;
};
};

View File

@@ -0,0 +1,31 @@
#pragma once
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/network.h"
#include "fourdst/composition/composition.h"
#include "fourdst/composition/atomicSpecies.h"
namespace gridfire {
fourdst::composition::Composition primeNetwork(
const NetIn&,
DynamicEngine& engine
);
double calculateDestructionRateConstant(
const DynamicEngine& engine,
const fourdst::atomic::Species& species,
const std::vector<double>& Y,
double T9,
double rho
);
double calculateCreationRate(
const DynamicEngine& engine,
const fourdst::atomic::Species& species,
const std::vector<double>& Y,
double T9,
double rho
);
}

View File

@@ -106,7 +106,7 @@ namespace gridfire {
/**
* @brief Generates the Jacobian matrix for the active species.
*
* @param Y_culled A vector of abundances for the active species.
* @param Y_dynamic A vector of abundances for the active species.
* @param T9 The temperature in units of 10^9 K.
* @param rho The density in g/cm^3.
*
@@ -117,7 +117,7 @@ namespace gridfire {
* @see AdaptiveEngineView::update()
*/
void generateJacobianMatrix(
const std::vector<double> &Y_culled,
const std::vector<double> &Y_dynamic,
const double T9,
const double rho
) override;
@@ -256,6 +256,10 @@ namespace gridfire {
* @endcode
*/
[[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;
private:
using Config = fourdst::config::Config;
using LogManager = fourdst::logging::LogManager;

View File

@@ -13,56 +13,9 @@
#include <string>
namespace gridfire{
/**
* @class FileDefinedEngineView
* @brief An engine view that uses a user-defined reaction network from a file.
*
* This class implements an EngineView that restricts the reaction network to a specific set of
* reactions defined in an external file. It acts as a filter or a view on a larger, more
* comprehensive base engine. The file provides a list of reaction identifiers, and this view
* will only consider those reactions and the species involved in them.
*
* This is useful for focusing on a specific sub-network for analysis, debugging, or performance
* reasons, without modifying the underlying full network.
*
* The view maintains mappings between the indices of its active (defined) species and reactions
* and the corresponding indices in the full network of the base engine. All calculations are
* delegated to the base engine after mapping the inputs from the view's context to the full
* network context, and the results are mapped back.
*
* @implements DynamicEngine
* @implements EngineView<DynamicEngine>
*/
class FileDefinedEngineView final: public DynamicEngine, public EngineView<DynamicEngine> {
class DefinedEngineView : public DynamicEngine, public EngineView<DynamicEngine> {
public:
/**
* @brief Constructs a FileDefinedEngineView.
*
* @param baseEngine The underlying DynamicEngine to which this view delegates calculations.
* @param fileName The path to the file that defines the reaction network for this view.
* @param parser A reference to a parser object capable of parsing the network file.
*
* @par Usage Example:
* @code
* MyParser parser;
* DynamicEngine baseEngine(...);
* FileDefinedEngineView view(baseEngine, "my_network.net", parser);
* @endcode
*
* @post The view is initialized with the reactions and species from the specified file.
* @throws std::runtime_error If a reaction from the file is not found in the base engine.
*/
explicit FileDefinedEngineView(
DynamicEngine& baseEngine,
const std::string& fileName,
const io::NetworkFileParser& parser
);
// --- EngineView Interface ---
/**
* @brief Gets the base engine.
* @return A const reference to the base engine.
*/
DefinedEngineView(const std::vector<std::string>& peNames, DynamicEngine& baseEngine);
const DynamicEngine& getBaseEngine() const override;
// --- Engine Interface ---
@@ -92,14 +45,14 @@ namespace gridfire{
/**
* @brief Generates the Jacobian matrix for the active species.
*
* @param Y_defined A vector of abundances for the active species.
* @param Y_dynamic A vector of abundances for the active species.
* @param T9 The temperature in units of 10^9 K.
* @param rho The density in g/cm^3.
*
* @throws std::runtime_error If the view is stale.
*/
void generateJacobianMatrix(
const std::vector<double>& Y_defined,
const std::vector<double>& Y_dynamic,
const double T9,
const double rho
) override;
@@ -191,21 +144,6 @@ namespace gridfire{
*/
void update(const NetIn &netIn) override;
/**
* @brief Sets a new network file to define the active reactions.
*
* @param fileName The path to the new network definition file.
*
* @par Usage Example:
* @code
* view.setNetworkFile("another_network.net");
* view.update(netIn); // Must be called before using the view again
* @endcode
*
* @post The view is marked as stale. `update()` must be called before further use.
*/
void setNetworkFile(const std::string& fileName);
/**
* @brief Sets the screening model for the base engine.
*
@@ -219,21 +157,15 @@ namespace gridfire{
* @return The current screening model type.
*/
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
private:
using Config = fourdst::config::Config;
using LogManager = fourdst::logging::LogManager;
/** @brief A reference to the singleton Config instance. */
Config& m_config = Config::getInstance();
/** @brief A pointer to the logger instance. */
quill::Logger* m_logger = LogManager::getInstance().getLogger("log");
/** @brief The underlying engine to which this view delegates calculations. */
[[nodiscard]] int getSpeciesIndex(const fourdst::atomic::Species &species) const override;
[[nodiscard]] std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const override;
protected:
bool m_isStale = true;
DynamicEngine& m_baseEngine;
///< Name of the file defining the reaction set considered by the engine view.
std::string m_fileName;
///< Parser for the network file.
const io::NetworkFileParser& m_parser;
private:
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log"); ///< Logger instance for trace and debug information.
///< Active species in the defined engine.
std::vector<fourdst::atomic::Species> m_activeSpecies;
///< Active reactions in the defined engine.
@@ -243,30 +175,7 @@ namespace gridfire{
std::vector<size_t> m_speciesIndexMap;
///< Maps indices of active reactions to indices in the full network.
std::vector<size_t> m_reactionIndexMap;
/** @brief A flag indicating whether the view is stale and needs to be updated. */
bool m_isStale = true;
private:
/**
* @brief Builds the active species and reaction sets from a file.
*
* This method uses the provided parser to read reaction names from the given file.
* It then finds these reactions in the base engine's full network and populates
* the `m_activeReactions` and `m_activeSpecies` members. Finally, it constructs
* the index maps for the active sets.
*
* @param fileName The path to the network definition file.
*
* @post
* - `m_activeReactions` and `m_activeSpecies` are populated.
* - `m_speciesIndexMap` and `m_reactionIndexMap` are constructed.
* - `m_isStale` is set to false.
*
* @throws std::runtime_error If a reaction from the file is not found in the base engine.
*/
void buildFromFile(const std::string& fileName);
/**
* @brief Constructs the species index map.
*
@@ -329,11 +238,25 @@ namespace gridfire{
*/
size_t mapViewToFullReactionIndex(size_t definedReactionIndex) const;
/**
* @brief Validates that the FileDefinedEngineView is not stale.
*
* @throws std::runtime_error If the view is stale (i.e., `update()` has not been called after the view was made stale).
*/
void validateNetworkState() const;
};
class FileDefinedEngineView final: public DefinedEngineView {
public:
explicit FileDefinedEngineView(
DynamicEngine& baseEngine,
const std::string& fileName,
const io::NetworkFileParser& parser
);
std::string getNetworkFile() const { return m_fileName; }
const io::NetworkFileParser& getParser() const { return m_parser; }
private:
using Config = fourdst::config::Config;
using LogManager = fourdst::logging::LogManager;
Config& m_config = Config::getInstance();
quill::Logger* m_logger = LogManager::getInstance().getLogger("log");
std::string m_fileName;
///< Parser for the network file.
const io::NetworkFileParser& m_parser;
};
}

View File

@@ -0,0 +1,188 @@
#pragma once
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/engine/views/engine_view_abstract.h"
#include "gridfire/engine/engine_graph.h"
#include "Eigen/Dense"
#include "unsupported/Eigen/NonLinearOptimization"
namespace gridfire {
class MultiscalePartitioningEngineView final: public DynamicEngine, public EngineView<DynamicEngine> {
public:
explicit MultiscalePartitioningEngineView(GraphEngine& baseEngine);
[[nodiscard]] const std::vector<fourdst::atomic::Species> & getNetworkSpecies() const override;
[[nodiscard]] StepDerivatives<double> calculateRHSAndEnergy(
const std::vector<double> &Y,
double T9,
double rho
) const override;
void generateJacobianMatrix(
const std::vector<double> &Y_dynamic,
double T9,
double rho
) override;
[[nodiscard]] double getJacobianMatrixEntry(
int i,
int j
) const override;
void generateStoichiometryMatrix() override;
[[nodiscard]] int getStoichiometryMatrixEntry(
int speciesIndex,
int reactionIndex
) const override;
[[nodiscard]] double calculateMolarReactionFlow(
const reaction::Reaction &reaction,
const std::vector<double> &Y,
double T9,
double rho
) const override;
[[nodiscard]] const reaction::LogicalReactionSet & getNetworkReactions() const override;
[[nodiscard]] std::unordered_map<fourdst::atomic::Species, double> getSpeciesTimescales(
const std::vector<double> &Y,
double T9,
double rho
) const override;
void update(
const NetIn &netIn
) override;
void setScreeningModel(
screening::ScreeningType model
) override;
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
const DynamicEngine & getBaseEngine() const override;
void partitionNetwork(
const std::vector<double>& Y,
double T9,
double rho,
double dt_control
);
void partitionNetwork(
const NetIn& netIn,
double dt_control
);
void exportToDot(const std::string& filename) const;
[[nodiscard]] int getSpeciesIndex(const fourdst::atomic::Species &species) const override;
[[nodiscard]] std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const override;
std::vector<fourdst::atomic::Species> getFastSpecies() const;
const std::vector<fourdst::atomic::Species>& getDynamicSpecies() const;
void equilibrateNetwork(
const std::vector<double>& Y,
double T9,
double rho,
double dt_control
);
void equilibrateNetwork(
const NetIn& netIn,
const double dt_control
);
private:
struct QSEGroup {
std::vector<size_t> species_indices; ///< Indices of all species in this group.
size_t seed_nucleus_index; ///< Index of the one species that will be evolved dynamically.
bool is_in_equilibrium = false; ///< Flag set by flux analysis.
};
struct EigenFunctor {
using InputType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
using OutputType = Eigen::Matrix<double, Eigen::Dynamic, 1>;
using JacobianType = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
enum {
InputsAtCompileTime = Eigen::Dynamic,
ValuesAtCompileTime = Eigen::Dynamic
};
MultiscalePartitioningEngineView* m_view;
const std::vector<size_t>& m_qse_solve_indices;
const std::vector<double>& m_Y_full_initial;
const double m_T9;
const double m_rho;
const Eigen::VectorXd& m_Y_scale;
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
) :
m_view(&view),
m_qse_solve_indices(qse_solve_indices),
m_Y_full_initial(Y_full_initial),
m_T9(T9),
m_rho(rho),
m_Y_scale(Y_scale) {}
int values() const { return m_qse_solve_indices.size(); }
int inputs() const { return m_qse_solve_indices.size(); }
int operator()(const InputType& v_qse, OutputType& f_qse) const;
int df(const InputType& v_qse, JacobianType& J_qse) const;
};
private:
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
GraphEngine& m_baseEngine; ///< The base engine to which this view delegates calculations.
std::vector<QSEGroup> m_qse_groups; ///< The list of identified equilibrium groups.
std::vector<fourdst::atomic::Species> m_dynamic_species; ///< The simplified set of species presented to the solver.
std::vector<size_t> m_dynamic_species_indices; ///< Indices mapping the dynamic species back to the master engine's list.
std::unordered_map<size_t, std::vector<size_t>> m_connectivity_graph;
private:
std::unordered_set<size_t> identifyFastReactions(
const std::vector<double>& Y_full,
double T9,
double rho,
double dt_control
) const;
void buildConnectivityGraph(
const std::unordered_set<size_t>& fast_reaction_indices
);
void findConnectedComponents();
void validateGroupsWithFluxAnalysis(
const std::vector<double>& Y,
double T9,
double rho
);
std::pair<std::vector<fourdst::atomic::Species>, std::vector<size_t>> identifyDynamicSpecies(
const std::vector<double>& Y,
const std::vector<QSEGroup>& qse_groups,
double T9,
double rho
) const;
std::vector<double> solveQSEAbundances(
const std::vector<double> &Y_full,
double T9,
double rho
);
};
}

View File

@@ -0,0 +1,38 @@
#pragma once
#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"
#include <vector>
#include <string>
namespace gridfire {
class NetworkPrimingEngineView final : public DefinedEngineView {
public:
NetworkPrimingEngineView(const std::string& primingSymbol, DynamicEngine& baseEngine);
NetworkPrimingEngineView(const fourdst::atomic::Species& primingSpecies, DynamicEngine& baseEngine);
const std::vector<std::string>& getPrimingReactionNames() const { return m_peNames; }
const fourdst::atomic::Species& getPrimingSpecies() const { return m_primingSpecies; }
private:
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
std::vector<std::string> m_peNames; ///< Names of the priming reactions.
fourdst::atomic::Species m_primingSpecies; ///< The priming species, if specified.
private:
std::vector<std::string> constructPrimingReactionSet(
const fourdst::atomic::Species& primingSpecies,
const DynamicEngine& baseEngine
) const;
};
}

View File

@@ -9,23 +9,7 @@
#include <vector>
namespace gridfire::io {
/**
* @struct ParsedNetworkData
* @brief Holds the data parsed from a network file.
*
* This struct is used to return the results of parsing a reaction network
* file. It contains the list of reaction names that define the network.
*/
struct ParsedNetworkData {
/**
* @brief A vector of reaction names in their PEN-style format.
*
* Projectile, Ejectile style names p(p,e+)d is a common format for representing
* nuclear reactions as strings.
*/
std::vector<std::string> reactionPENames;
};
typedef std::vector<std::string> ParsedNetworkData;
/**
* @class NetworkFileParser

View File

@@ -411,6 +411,8 @@ namespace gridfire::reaction {
*/
explicit TemplatedReactionSet(std::vector<ReactionT> reactions);
TemplatedReactionSet();
/**
* @brief Copy constructor.
* @param other The ReactionSet to copy.
@@ -577,6 +579,9 @@ namespace gridfire::reaction {
}
}
template<typename ReactionT>
TemplatedReactionSet<ReactionT>::TemplatedReactionSet() {}
template <typename ReactionT>
TemplatedReactionSet<ReactionT>::TemplatedReactionSet(const TemplatedReactionSet<ReactionT> &other) {
m_reactions.reserve(other.m_reactions.size());

View File

@@ -145,7 +145,7 @@ namespace gridfire::screening {
const T T9,
const T rho
) const {
LOG_TRACE_L1(
LOG_TRACE_L3(
m_logger,
"Calculating weak screening factors for {} reactions...",
reactions.size()

View File

@@ -205,6 +205,7 @@ namespace gridfire::solver {
const Eigen::VectorXd& m_Y_QSE; ///< Steady-state abundances of the QSE species.
const double m_T9; ///< Temperature in units of 10^9 K.
const double m_rho; ///< Density in g/cm^3.
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log"); ///< Logger instance for trace and debug information.
bool m_isViewInitialized = false;
@@ -316,11 +317,13 @@ namespace gridfire::solver {
};
DynamicEngine& m_engine; ///< The engine used to evaluate the network.
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log"); ///< Logger instance for trace and debug information.
const std::vector<double>& m_YFull; ///< The full, initial abundance vector
const std::vector<size_t>& m_dynamicSpeciesIndices; ///< Indices of the dynamic species.
const std::vector<size_t>& m_QSESpeciesIndices; ///< Indices of the QSE species.
const double m_T9; ///< Temperature in units of 10^9 K.
const double m_rho; ///< Density in g/cm^3.
const Eigen::VectorXd& m_YScale; ///< Scaling vector for the QSE species for asinh scaling.
/**
* @brief Constructor for the EigenFunctor.
@@ -337,14 +340,16 @@ namespace gridfire::solver {
const std::vector<size_t>& dynamicSpeciesIndices,
const std::vector<size_t>& QSESpeciesIndices,
const double T9,
const double rho
const double rho,
const Eigen::VectorXd& YScale
) :
m_engine(engine),
m_YFull(YFull),
m_dynamicSpeciesIndices(dynamicSpeciesIndices),
m_QSESpeciesIndices(QSESpeciesIndices),
m_T9(T9),
m_rho(rho) {}
m_rho(rho),
m_YScale(YScale) {}
int values() const { return m_QSESpeciesIndices.size(); }
int inputs() const { return m_QSESpeciesIndices.size(); }
@@ -493,9 +498,20 @@ namespace gridfire::solver {
};
template<typename T>
int QSENetworkSolver::EigenFunctor<T>::operator()(const InputType &v_QSE_log, OutputType &f_QSE) const {
int QSENetworkSolver::EigenFunctor<T>::operator()(const InputType &v_QSE, OutputType &f_QSE) const {
std::vector<double> y = m_YFull; // Full vector of species abundances
Eigen::VectorXd Y_QSE = v_QSE_log.array().exp();
Eigen::VectorXd Y_QSE = m_YScale.array() * v_QSE.array().sinh(); // Convert to physical abundances using asinh scaling
LOG_DEBUG(
m_logger,
"Calling QSENetworkSolver::EigenFunctor::operator() with QSE abundances {}",
[&]() -> std::string {
std::stringstream ss;
ss << std::scientific << std::setprecision(5);
for (size_t i = 0; i < m_QSESpeciesIndices.size(); ++i) {
ss << std::string(m_engine.getNetworkSpecies()[m_QSESpeciesIndices[i]].name()) << ": " << Y_QSE(i) << ", ";
}
return ss.str();
}());
for (size_t i = 0; i < m_QSESpeciesIndices.size(); ++i) {
y[m_QSESpeciesIndices[i]] = Y_QSE(i);
@@ -503,6 +519,17 @@ namespace gridfire::solver {
const auto [dydt, specificEnergyRate] = m_engine.calculateRHSAndEnergy(y, m_T9, m_rho);
LOG_DEBUG(
m_logger,
"Calling QSENetworkSolver::EigenFunctor::operator() found QSE dydt {}",
[&]() -> std::string {
std::stringstream ss;
ss << std::scientific << std::setprecision(5);
for (size_t i = 0; i < m_QSESpeciesIndices.size(); ++i) {
ss << std::string(m_engine.getNetworkSpecies()[m_QSESpeciesIndices[i]].name()) << ": " << dydt[m_QSESpeciesIndices[i]] << ", ";
}
return ss.str();
}());
f_QSE.resize(m_QSESpeciesIndices.size());
for (size_t i = 0; i < m_QSESpeciesIndices.size(); ++i) {
f_QSE(i) = dydt[m_QSESpeciesIndices[i]];
@@ -511,9 +538,9 @@ namespace gridfire::solver {
}
template <typename T>
int QSENetworkSolver::EigenFunctor<T>::df(const InputType& v_QSE_log, JacobianType& J_QSE) const {
int QSENetworkSolver::EigenFunctor<T>::df(const InputType& v_QSE, JacobianType& J_QSE) const {
std::vector<double> y = m_YFull;
Eigen::VectorXd Y_QSE = v_QSE_log.array().exp();
Eigen::VectorXd Y_QSE = m_YScale.array() * v_QSE.array().sinh();
for (size_t i = 0; i < m_QSESpeciesIndices.size(); ++i) {
y[m_QSESpeciesIndices[i]] = Y_QSE(i);
@@ -528,8 +555,32 @@ namespace gridfire::solver {
}
}
std::string format_jacobian = [&]() -> std::string {
std::stringstream ss;
ss << std::scientific << std::setprecision(5);
for (size_t i = 0; i < m_QSESpeciesIndices.size(); ++i) {
for (size_t j = 0; j < m_QSESpeciesIndices.size(); ++j) {
ss << J_QSE(i, j) << " ";
}
ss << "\n";
}
return ss.str();
}();
for (long j = 0; j < J_QSE.cols(); ++j) {
J_QSE(j, j) *= Y_QSE(j); // chain rule for log space transformation
LOG_DEBUG(
m_logger,
"Jacobian ({},{}) before chain rule: {}",
j, j, J_QSE(j, j)
);
const double dY_dv = m_YScale(j) * CppAD::cosh(v_QSE(j));
J_QSE.col(j) *= dY_dv; // chain rule for log space transformation
LOG_DEBUG(
m_logger,
"Jacobian ({},{}) after chain rule: {} (dY/dv = {})",
j, j, J_QSE(j, j), dY_dv
);
}
return 0;
}