feat(GridFire): added weak electron screening

This commit is contained in:
2025-07-01 11:40:03 -04:00
parent 40b28477ed
commit 0c16e81e98
26 changed files with 1408 additions and 444 deletions

View File

@@ -1,6 +1,9 @@
#pragma once
#include "gridfire/reaction/reaction.h"
#include "gridfire/network.h"
#include "gridfire/screening/screening_abstract.h"
#include "gridfire/screening/screening_types.h"
#include <vector>
#include <unordered_map>
@@ -80,7 +83,7 @@ namespace gridfire {
* @brief Get the list of species in the network.
* @return Vector of Species objects representing all network species.
*/
virtual const std::vector<fourdst::atomic::Species>& getNetworkSpecies() const = 0;
[[nodiscard]] virtual const std::vector<fourdst::atomic::Species>& getNetworkSpecies() const = 0;
/**
* @brief Calculate the right-hand side (dY/dt) and energy generation.
@@ -94,7 +97,7 @@ namespace gridfire {
* time derivatives of all species and the specific nuclear energy generation
* rate for the current state.
*/
virtual StepDerivatives<double> calculateRHSAndEnergy(
[[nodiscard]] virtual StepDerivatives<double> calculateRHSAndEnergy(
const std::vector<double>& Y,
double T9,
double rho
@@ -141,7 +144,7 @@ namespace gridfire {
*
* The Jacobian must have been generated by generateJacobianMatrix() before calling this.
*/
virtual double getJacobianMatrixEntry(
[[nodiscard]] virtual double getJacobianMatrixEntry(
int i,
int j
) const = 0;
@@ -163,7 +166,7 @@ namespace gridfire {
*
* The stoichiometry matrix must have been generated by generateStoichiometryMatrix().
*/
virtual int getStoichiometryMatrixEntry(
[[nodiscard]] virtual int getStoichiometryMatrixEntry(
int speciesIndex,
int reactionIndex
) const = 0;
@@ -180,7 +183,7 @@ namespace gridfire {
* This method computes the net rate at which the given reaction proceeds
* under the current state.
*/
virtual double calculateMolarReactionFlow(
[[nodiscard]] virtual double calculateMolarReactionFlow(
const reaction::Reaction& reaction,
const std::vector<double>& Y,
double T9,
@@ -192,7 +195,7 @@ namespace gridfire {
*
* @return Reference to the LogicalReactionSet containing all reactions.
*/
virtual const reaction::LogicalReactionSet& getNetworkReactions() const = 0;
[[nodiscard]] virtual const reaction::LogicalReactionSet& getNetworkReactions() const = 0;
/**
* @brief Compute timescales for all species in the network.
@@ -205,10 +208,16 @@ namespace gridfire {
* This method estimates the timescale for abundance change of each species,
* which can be used for timestep control, diagnostics, and reaction network culling.
*/
virtual std::unordered_map<fourdst::atomic::Species, double> getSpeciesTimescales(
[[nodiscard]] virtual std::unordered_map<fourdst::atomic::Species, double> getSpeciesTimescales(
const std::vector<double>& Y,
double T9,
double rho
) const = 0;
virtual void update(const NetIn& netIn) = 0;
virtual void setScreeningModel(screening::ScreeningType model) = 0;
[[nodiscard]] virtual screening::ScreeningType getScreeningModel() const = 0;
};
}

View File

@@ -8,10 +8,13 @@
#include "gridfire/network.h"
#include "gridfire/reaction/reaction.h"
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/screening/screening_abstract.h"
#include "gridfire/screening/screening_types.h"
#include <string>
#include <unordered_map>
#include <vector>
#include <memory>
#include <boost/numeric/ublas/matrix_sparse.hpp>
@@ -120,7 +123,7 @@ namespace gridfire {
*
* @see StepDerivatives
*/
StepDerivatives<double> calculateRHSAndEnergy(
[[nodiscard]] StepDerivatives<double> calculateRHSAndEnergy(
const std::vector<double>& Y,
const double T9,
const double rho
@@ -165,7 +168,7 @@ namespace gridfire {
* This method computes the net rate at which the given reaction proceeds
* under the current state.
*/
double calculateMolarReactionFlow(
[[nodiscard]] double calculateMolarReactionFlow(
const reaction::Reaction& reaction,
const std::vector<double>&Y,
const double T9,
@@ -243,6 +246,8 @@ namespace gridfire {
double rho
) const override;
void update(const NetIn& netIn) override;
/**
* @brief Checks if a given species is involved in the network.
*
@@ -293,6 +298,10 @@ namespace gridfire {
const std::string& filename
) const;
void setScreeningModel(screening::ScreeningType) override;
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
private:
reaction::LogicalReactionSet m_reactions; ///< Set of REACLIB reactions in the network.
@@ -307,6 +316,9 @@ namespace gridfire {
CppAD::ADFun<double> m_rhsADFun; ///< CppAD function for the right-hand side of the ODE.
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);
Config& m_config = Config::getInstance();
Constants& m_constants = Constants::getInstance(); ///< Access to physical constants.
quill::Logger* m_logger = LogManager::getInstance().getLogger("log");
@@ -427,7 +439,7 @@ namespace gridfire {
* specific nuclear energy generation rate for the current state.
*/
template<IsArithmeticOrAD T>
StepDerivatives<T> calculateAllDerivatives(
[[nodiscard]] StepDerivatives<T> calculateAllDerivatives(
const std::vector<T> &Y_in,
T T9,
T rho
@@ -445,7 +457,7 @@ namespace gridfire {
* specific nuclear energy generation rate for the current state using
* double precision arithmetic.
*/
StepDerivatives<double> calculateAllDerivatives(
[[nodiscard]] StepDerivatives<double> calculateAllDerivatives(
const std::vector<double>& Y_in,
const double T9,
const double rho
@@ -463,7 +475,7 @@ namespace gridfire {
* specific nuclear energy generation rate for the current state using
* automatic differentiation.
*/
StepDerivatives<ADDouble> calculateAllDerivatives(
[[nodiscard]] StepDerivatives<ADDouble> calculateAllDerivatives(
const std::vector<ADDouble>& Y_in,
const ADDouble &T9,
const ADDouble &rho
@@ -474,6 +486,13 @@ namespace gridfire {
template<IsArithmeticOrAD T>
StepDerivatives<T> GraphEngine::calculateAllDerivatives(
const std::vector<T> &Y_in, T T9, T rho) const {
std::vector<T> screeningFactors = m_screeningModel->calculateScreeningFactors(
m_reactions,
m_networkSpecies,
Y_in,
T9,
rho
);
// --- Setup output derivatives structure ---
StepDerivatives<T> result;
@@ -512,7 +531,7 @@ namespace gridfire {
const auto& reaction = m_reactions[reactionIndex];
// 1. Calculate reaction rate
const T molarReactionFlow = calculateMolarReactionFlow<T>(reaction, Y, T9, rho);
const T molarReactionFlow = screeningFactors[reactionIndex] * calculateMolarReactionFlow<T>(reaction, Y, T9, rho);
// 2. Use the rate to update all relevant species derivatives (dY/dt)
for (size_t speciesIndex = 0; speciesIndex < m_networkSpecies.size(); ++speciesIndex) {

View File

@@ -1,6 +1,8 @@
#pragma once
#include "gridfire/engine/engine_abstract.h"
#include "engine_view_abstract.h"
#include "gridfire/engine/views/engine_view_abstract.h"
#include "gridfire/screening/screening_abstract.h"
#include "gridfire/screening/screening_types.h"
#include "gridfire/network.h"
#include "fourdst/composition/atomicSpecies.h"
@@ -71,13 +73,13 @@ namespace gridfire {
* @see AdaptiveEngineView::constructSpeciesIndexMap()
* @see AdaptiveEngineView::constructReactionIndexMap()
*/
void update(const NetIn& netIn);
void update(const NetIn& netIn) override;
/**
* @brief Gets the list of active species in the network.
* @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;
/**
* @brief Calculates the right-hand side (dY/dt) and energy generation for the active species.
@@ -95,7 +97,7 @@ namespace gridfire {
* @throws std::runtime_error If the AdaptiveEngineView is stale (i.e., `update()` has not been called).
* @see AdaptiveEngineView::update()
*/
StepDerivatives<double> calculateRHSAndEnergy(
[[nodiscard]] StepDerivatives<double> calculateRHSAndEnergy(
const std::vector<double> &Y_culled,
const double T9,
const double rho
@@ -134,7 +136,7 @@ namespace gridfire {
* @throws std::out_of_range If the culled index is out of bounds for the species index map.
* @see AdaptiveEngineView::update()
*/
double getJacobianMatrixEntry(
[[nodiscard]] double getJacobianMatrixEntry(
const int i_culled,
const int j_culled
) const override;
@@ -164,7 +166,7 @@ namespace gridfire {
* @throws std::out_of_range If the culled index is out of bounds for the species or reaction index map.
* @see AdaptiveEngineView::update()
*/
int getStoichiometryMatrixEntry(
[[nodiscard]] int getStoichiometryMatrixEntry(
const int speciesIndex_culled,
const int reactionIndex_culled
) const override;
@@ -184,7 +186,7 @@ namespace gridfire {
* @throws std::runtime_error If the AdaptiveEngineView is stale (i.e., `update()` has not been called).
* @throws std::runtime_error If the reaction is not part of the active reactions in the adaptive engine view.
*/
double calculateMolarReactionFlow(
[[nodiscard]] double calculateMolarReactionFlow(
const reaction::Reaction &reaction,
const std::vector<double> &Y_culled,
double T9,
@@ -196,7 +198,7 @@ namespace gridfire {
*
* @return Reference to the LogicalReactionSet containing all active reactions.
*/
const reaction::LogicalReactionSet& getNetworkReactions() const override;
[[nodiscard]] const reaction::LogicalReactionSet& getNetworkReactions() const override;
/**
* @brief Computes timescales for all active species in the network.
@@ -211,7 +213,7 @@ namespace gridfire {
*
* @throws std::runtime_error If the AdaptiveEngineView is stale (i.e., `update()` has not been called).
*/
std::unordered_map<fourdst::atomic::Species, double> getSpeciesTimescales(
[[nodiscard]] std::unordered_map<fourdst::atomic::Species, double> getSpeciesTimescales(
const std::vector<double> &Y_culled,
double T9,
double rho
@@ -221,7 +223,11 @@ namespace gridfire {
* @brief Gets the base engine.
* @return A const reference to the base engine.
*/
const DynamicEngine& getBaseEngine() const override { return m_baseEngine; }
[[nodiscard]] const DynamicEngine& getBaseEngine() const override { return m_baseEngine; }
void setScreeningModel(screening::ScreeningType model) override;
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
private:
using Config = fourdst::config::Config;
using LogManager = fourdst::logging::LogManager;
@@ -257,7 +263,7 @@ namespace gridfire {
*
* @see AdaptiveEngineView::update()
*/
std::vector<size_t> constructSpeciesIndexMap() const;
[[nodiscard]] std::vector<size_t> constructSpeciesIndexMap() const;
/**
* @brief Constructs the reaction index map.
@@ -269,7 +275,7 @@ namespace gridfire {
*
* @see AdaptiveEngineView::update()
*/
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.
@@ -278,7 +284,7 @@ namespace gridfire {
* @return A vector of abundances for the full network, with the abundances of the active
* species copied from the culled vector.
*/
std::vector<double> mapCulledToFull(const std::vector<double>& culled) const;
[[nodiscard]] std::vector<double> mapCulledToFull(const std::vector<double>& culled) const;
/**
* @brief Maps a vector of full abundances to a vector of culled abundances.
@@ -287,7 +293,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> mapFullToCulled(const std::vector<double>& full) const;
[[nodiscard]] std::vector<double> mapFullToCulled(const std::vector<double>& full) const;
/**
* @brief Maps a culled species index to a full species index.
@@ -297,7 +303,7 @@ namespace gridfire {
*
* @throws std::out_of_range If the culled index is out of bounds for the species index map.
*/
size_t mapCulledToFullSpeciesIndex(size_t culledSpeciesIndex) const;
[[nodiscard]] size_t mapCulledToFullSpeciesIndex(size_t culledSpeciesIndex) const;
/**
* @brief Maps a culled reaction index to a full reaction index.
@@ -307,7 +313,7 @@ namespace gridfire {
*
* @throws std::out_of_range If the culled index is out of bounds for the reaction index map.
*/
size_t mapCulledToFullReactionIndex(size_t culledReactionIndex) const;
[[nodiscard]] size_t mapCulledToFullReactionIndex(size_t culledReactionIndex) const;
/**
* @brief Validates that the AdaptiveEngineView is not stale.
@@ -320,10 +326,10 @@ namespace gridfire {
const NetIn& netIn,
std::vector<double>& out_Y_Full
) const;
std::unordered_set<fourdst::atomic::Species> findReachableSpecies(
[[nodiscard]] std::unordered_set<fourdst::atomic::Species> findReachableSpecies(
const NetIn& netIn
) const;
std::vector<const reaction::LogicalReaction*> cullReactionsByFlow(
[[nodiscard]] 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,

View File

@@ -1,7 +1,9 @@
#pragma once
#include "engine_view_abstract.h"
#include "../engine_abstract.h"
#include "gridfire/engine/views/engine_view_abstract.h"
#include "gridfire/engine/engine_abstract.h"
#include "gridfire/io/network_file.h"
#include "gridfire/network.h"
#include "fourdst/config/config.h"
#include "fourdst/logging/logging.h"
@@ -13,7 +15,11 @@
namespace gridfire{
class FileDefinedEngineView final: public DynamicEngine, public EngineView<DynamicEngine> {
public:
explicit FileDefinedEngineView(DynamicEngine& baseEngine, const std::string& fileName);
explicit FileDefinedEngineView(
DynamicEngine& baseEngine,
const std::string& fileName,
const io::NetworkFileParser& parser
);
// --- EngineView Interface ---
const DynamicEngine& getBaseEngine() const override;
@@ -53,6 +59,14 @@ namespace gridfire{
const double T9,
const double rho
) const override;
void update(const NetIn &netIn) override;
void setNetworkFile(const std::string& fileName);
void setScreeningModel(screening::ScreeningType model) override;
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
private:
using Config = fourdst::config::Config;
using LogManager = fourdst::logging::LogManager;
@@ -60,12 +74,17 @@ namespace gridfire{
quill::Logger* m_logger = LogManager::getInstance().getLogger("log");
DynamicEngine& m_baseEngine;
std::string m_fileName; ///< Name of the file defining the reaction set considered by the engine view.
const io::NetworkFileParser& m_parser; ///< Parser for the network file.
std::vector<fourdst::atomic::Species> m_activeSpecies; ///< Active species in the defined engine.
reaction::LogicalReactionSet m_activeReactions; ///< Active reactions in the defined engine.
std::vector<size_t> m_speciesIndexMap; ///< Maps indices of active species to indices in the full network.
std::vector<size_t> m_reactionIndexMap; ///< Maps indices of active reactions to indices in the full network.
bool m_isStale = true;
private:
void buildFromFile(const std::string& fileName);
@@ -100,7 +119,7 @@ namespace gridfire{
* @return A vector of abundances for the full network, with the abundances of the active
* species copied from the culled vector.
*/
std::vector<double> mapCulledToFull(const std::vector<double>& culled) const;
std::vector<double> mapViewToFull(const std::vector<double>& culled) const;
/**
* @brief Maps a vector of full abundances to a vector of culled abundances.
@@ -109,7 +128,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> mapFullToCulled(const std::vector<double>& full) const;
std::vector<double> mapFullToView(const std::vector<double>& full) const;
/**
* @brief Maps a culled species index to a full species index.
@@ -119,7 +138,7 @@ namespace gridfire{
*
* @throws std::out_of_range If the culled index is out of bounds for the species index map.
*/
size_t mapCulledToFullSpeciesIndex(size_t culledSpeciesIndex) const;
size_t mapViewToFullSpeciesIndex(size_t culledSpeciesIndex) const;
/**
* @brief Maps a culled reaction index to a full reaction index.
@@ -129,7 +148,8 @@ namespace gridfire{
*
* @throws std::out_of_range If the culled index is out of bounds for the reaction index map.
*/
size_t mapCulledToFullReactionIndex(size_t culledReactionIndex) const;
size_t mapViewToFullReactionIndex(size_t culledReactionIndex) const;
void validateNetworkState() const;
};
}