feat(dynamic-engine): added derivitves for energy generation rate. dε/dT and dε/dρ have been added to NetOut and computed with auto diff

This commit is contained in:
2025-09-19 15:14:46 -04:00
parent ed1c5a1ac7
commit 813e62bdd6
24 changed files with 1215 additions and 190 deletions

View File

@@ -0,0 +1,34 @@
#pragma once
#include "gridfire/engine/engine_abstract.h"
#include <vector>
#include <string>
namespace gridfire::diagnostics {
void report_limiting_species(
const DynamicEngine& engine,
const std::vector<double>& Y_full,
const std::vector<double>& E_full,
const std::vector<double>& dydt_full,
double relTol,
double absTol,
size_t top_n = 10
);
void inspect_species_balance(
const DynamicEngine& engine,
const std::string& species_name,
const std::vector<double>& Y_full,
double T9,
double rho
);
void inspect_jacobian_stiffness(
const DynamicEngine& engine,
const std::vector<double>& Y_full,
double T9,
double rho
);
}

View File

@@ -64,6 +64,16 @@ namespace gridfire {
using SparsityPattern = std::vector<std::pair<size_t, size_t>>;
struct EnergyDerivatives {
double dEps_dT = 0.0; ///< Partial derivative of energy generation rate with respect to temperature.
double dEps_dRho = 0.0;///< Partial derivative of energy generation rate with respect to density.
friend std::ostream& operator<<(std::ostream& os, const EnergyDerivatives& ed) {
os << "<dε/dT: " << ed.dEps_dT << ", dε/dρ: " << ed.dEps_dRho << ">";
return os;
}
};
/**
* @brief Abstract base class for a reaction network engine.
*
@@ -210,6 +220,23 @@ namespace gridfire {
double rho
) const = 0;
/**
* @brief Calculate the derivatives of the energy generation rate with respect to T and rho.
*
* @param Y Vector of current abundances.
* @param T9 Temperature in units of 10^9 K.
* @param rho Density in g/cm^3.
* @return EnergyDerivatives containing dEps/dT and dEps/dRho.
*
* This method computes the partial derivatives of the specific nuclear energy
* generation rate with respect to temperature and density for the current state.
*/
[[nodiscard]] virtual EnergyDerivatives calculateEpsDerivatives(
const std::vector<double>& Y,
const double T9,
const double rho
) const = 0;
/**
* @brief Get the set of logical reactions in the network.
*

View File

@@ -68,6 +68,7 @@ namespace gridfire {
static constexpr double MIN_JACOBIAN_THRESHOLD = 1e-24;
/**
* @class GraphEngine
* @brief A reaction network engine that uses a graph-based representation.
@@ -143,6 +144,12 @@ namespace gridfire {
const double rho
) const override;
[[nodiscard]] EnergyDerivatives calculateEpsDerivatives(
const std::vector<double>& Y,
const double T9,
const double rho
) const override;
/**
* @brief Generates the Jacobian matrix for the current state.
*
@@ -581,6 +588,7 @@ namespace gridfire {
mutable boost::numeric::ublas::compressed_matrix<double> m_jacobianMatrix; ///< Jacobian matrix (species x species).
mutable CppAD::ADFun<double> m_rhsADFun; ///< CppAD function for the right-hand side of the ODE.
mutable CppAD::ADFun<double> m_epsADFun; ///< CppAD function for the energy generation rate.
mutable CppAD::sparse_jac_work m_jac_work; ///< Work object for sparse Jacobian calculations.
CppAD::sparse_rc<std::vector<size_t>> m_full_jacobian_sparsity_pattern; ///< Full sparsity pattern for the Jacobian matrix.
@@ -652,6 +660,8 @@ namespace gridfire {
*/
void recordADTape() const;
void recordEpsADTape() const;
void collectAtomicReverseRateAtomicBases();
void precomputeNetwork();

View File

@@ -107,6 +107,12 @@ namespace gridfire {
const double rho
) const override;
[[nodiscard]] EnergyDerivatives calculateEpsDerivatives(
const std::vector<double> &Y_culled,
const double T9,
const double rho
) const override;
/**
* @brief Generates the Jacobian matrix for the active species.
*

View File

@@ -42,6 +42,13 @@ namespace gridfire{
const double T9,
const double rho
) const override;
[[nodiscard]] EnergyDerivatives calculateEpsDerivatives(
const std::vector<double> &Y,
const double T9,
const double rho
) const override;
/**
* @brief Generates the Jacobian matrix for the active species.
*

View File

@@ -236,6 +236,12 @@ namespace gridfire {
double rho
) const override;
[[nodiscard]] EnergyDerivatives calculateEpsDerivatives(
const std::vector<double> &Y,
const double T9,
const double rho
) const override;
/**
* @brief Generates the Jacobian matrix for the current state.
*
@@ -754,6 +760,10 @@ namespace gridfire {
* @return True if the sets of species indices are not identical.
*/
bool operator!=(const QSEGroup& other) const;
std::string toString() const;
std::string toString(DynamicEngine &engine) const;
};
/**
@@ -1056,9 +1066,11 @@ namespace gridfire {
* flux exceeds a configurable threshold, the group is considered valid and is added
* to the returned vector.
*/
std::vector<QSEGroup> validateGroupsWithFluxAnalysis(
std::pair<std::vector<MultiscalePartitioningEngineView::QSEGroup>, std::vector<MultiscalePartitioningEngineView
::
QSEGroup>> validateGroupsWithFluxAnalysis(
const std::vector<QSEGroup> &candidate_groups,
const std::vector<double>& Y,
const std::vector<double> &Y,
double T9,
double rho
) const;