feat(GridFire): Added a number of python hooks
python hooks to make getting base composition more reliable; further, a number of small changes made to aid in my analysis in response to ref report 1
This commit is contained in:
@@ -3,13 +3,25 @@
|
||||
#include "fourdst/config/config.h"
|
||||
|
||||
namespace gridfire::config {
|
||||
struct CVODESolverConfig {
|
||||
struct BoundaryFluxConfig {
|
||||
double relativeThreshold = 3e-8;
|
||||
double absoluteThreshold = 1e-24;
|
||||
};
|
||||
|
||||
struct TriggerConfig {
|
||||
double offDiagonalThreshold = 1e10;
|
||||
double timestepCollapseRatio = 0.5;
|
||||
double maxConvergenceFailures = 2;
|
||||
BoundaryFluxConfig boundaryFlux;
|
||||
};
|
||||
struct PointSolverConfig {
|
||||
double absTol = 1.0e-8;
|
||||
double relTol = 1.0e-5;
|
||||
TriggerConfig trigger;
|
||||
};
|
||||
|
||||
struct SolverConfig {
|
||||
CVODESolverConfig cvode;
|
||||
PointSolverConfig pointSolver;
|
||||
};
|
||||
|
||||
struct AdaptiveEngineViewConfig {
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "gridfire/engine/types/reporting.h"
|
||||
#include "gridfire/engine/types/jacobian.h"
|
||||
|
||||
#include "gridfire/exceptions/error_engine.h"
|
||||
|
||||
#include "gridfire/engine/scratchpads/blob.h"
|
||||
|
||||
#include "fourdst/composition/composition_abstract.h"
|
||||
@@ -183,6 +185,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Generate the Jacobian matrix for the current state.
|
||||
*
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
* @param comp Composition object containing current abundances.
|
||||
* @param T9 Temperature in units of 10^9 K.
|
||||
* @param rho Density in g/cm^3.
|
||||
@@ -200,6 +203,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Generate the Jacobian matrix for the current state using a subset of active species.
|
||||
*
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
* @param comp Composition object containing current abundances.
|
||||
* @param T9 Temperature in units of 10^9 K.
|
||||
* @param rho Density in g/cm^3.
|
||||
@@ -221,6 +225,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Generate the Jacobian matrix for the current state with a specified sparsity pattern.
|
||||
*
|
||||
* @param ctx Get the scratchpad context for the current state.
|
||||
* @param comp Composition object containing current abundances.
|
||||
* @param T9 Temperature in units of 10^9 K.
|
||||
* @param rho Density in g/cm^3.
|
||||
@@ -245,6 +250,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Calculate the molar reaction flow for a given reaction.
|
||||
*
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
* @param reaction The reaction for which to calculate the flow.
|
||||
* @param comp Composition object containing current abundances.
|
||||
* @param T9 Temperature in units of 10^9 K.
|
||||
@@ -289,6 +295,39 @@ namespace gridfire::engine {
|
||||
scratch::StateBlob& ctx
|
||||
) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the set of inactive reactions in the network.
|
||||
*
|
||||
* @return ReactionSet containing all inactive reactions.
|
||||
*
|
||||
* By default, this method returns an empty set. Derived classes can override
|
||||
* this method to provide the actual set of inactive reactions based on their
|
||||
* internal logic (e.g., reaction flow culling, QSE partitioning).
|
||||
*/
|
||||
[[nodiscard]] virtual reaction::ReactionSet getInactiveNetworkReactions(
|
||||
scratch::StateBlob &ctx
|
||||
) const {
|
||||
return reaction::ReactionSet{};
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual double getInactiveReactionMolarReactionFlow(
|
||||
scratch::StateBlob& ctx,
|
||||
const reaction::Reaction &reaction,
|
||||
const fourdst::composition::CompositionAbstract &comp,
|
||||
const double T9,
|
||||
const double rho
|
||||
) const {
|
||||
std::string warning_msg = std::format(
|
||||
"[GridFire Warning ({}, {}, {})]: Engine of type '{}' does not implement getInactiveReactionMolarReactionFlow. Returning 0.0 flow for reaction '{}'.",
|
||||
__FILE__,
|
||||
__LINE__,
|
||||
__FUNCTION__,
|
||||
typeid(*this).name(),
|
||||
reaction.id()
|
||||
);
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Compute timescales for all species in the network.
|
||||
@@ -311,6 +350,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Compute destruction timescales for all species in the network.
|
||||
*
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
* @param comp Composition object containing current abundances.
|
||||
* @param T9 Temperature in units of 10^9 K.
|
||||
* @param rho Density in g/cm^3.
|
||||
@@ -329,6 +369,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Update the thread local scratch pad state of a network.
|
||||
*
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
* @param netIn A struct containing the current network input, such as
|
||||
* temperature, density, and composition.
|
||||
*
|
||||
@@ -354,6 +395,8 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Get the current electron screening model.
|
||||
*
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
*
|
||||
* @return The currently active screening model type.
|
||||
*
|
||||
* @par Usage Example:
|
||||
@@ -368,6 +411,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Get the index of a species in the network.
|
||||
*
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
* @param species The species to look up.
|
||||
*
|
||||
* This method allows querying the index of a specific species in the
|
||||
@@ -382,6 +426,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Prime the engine with initial conditions.
|
||||
*
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
* @param netIn The input conditions for the network.
|
||||
* @return PrimingReport containing information about the priming process.
|
||||
*
|
||||
@@ -403,6 +448,7 @@ namespace gridfire::engine {
|
||||
* from each sub engine.
|
||||
* @note It is up to each engine to decide how to handle filling in the return composition.
|
||||
* @note These methods return an unfinalized composition which must then be finalized by the caller
|
||||
* @param ctx The scratchpad context for the current state.
|
||||
* @param comp Input composition to "normalize".
|
||||
* @param T9
|
||||
* @param rho
|
||||
@@ -434,5 +480,7 @@ namespace gridfire::engine {
|
||||
scratch::StateBlob& ctx
|
||||
) const = 0;
|
||||
|
||||
[[nodiscard]] virtual std::unique_ptr<scratch::StateBlob> constructStateBlob(const scratch::StateBlob *blob) const = 0;
|
||||
|
||||
};
|
||||
}
|
||||
@@ -137,6 +137,18 @@ namespace gridfire::engine {
|
||||
*/
|
||||
explicit GraphEngine(const reaction::ReactionSet &reactions);
|
||||
|
||||
void addReaction(
|
||||
const reaction::Reaction& reaction
|
||||
);
|
||||
|
||||
void addReaction(
|
||||
const std::string& reaction_id
|
||||
);
|
||||
|
||||
std::unique_ptr<scratch::StateBlob> constructStateBlob(
|
||||
const scratch::StateBlob *blob = nullptr
|
||||
) const override;
|
||||
|
||||
/**
|
||||
* @brief Calculates the right-hand side (dY/dt) and energy generation rate.
|
||||
*
|
||||
@@ -204,6 +216,7 @@ namespace gridfire::engine {
|
||||
double rho
|
||||
) const override;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Calculates the derivatives of the energy generation rate with respect to temperature and density for a subset of reactions
|
||||
*
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "fourdst/config/config.h"
|
||||
#include "fourdst/logging/logging.h"
|
||||
|
||||
#include "gridfire/engine/procedures/construction.h"
|
||||
#include "gridfire/engine/scratchpads/blob.h"
|
||||
|
||||
#include "quill/Logger.h"
|
||||
@@ -234,6 +233,26 @@ namespace gridfire::engine {
|
||||
scratch::StateBlob& ctx
|
||||
) const override;
|
||||
|
||||
/**
|
||||
* @brief Gets the set of inactive logical reactions in the network.
|
||||
*
|
||||
* @return ReactionSet containing all inactive reactions.
|
||||
*
|
||||
* This method returns the set of reactions that have been culled from the active
|
||||
* network based on the adaptation criteria.
|
||||
*/
|
||||
[[nodiscard]] reaction::ReactionSet getInactiveNetworkReactions(
|
||||
scratch::StateBlob &ctx
|
||||
) const override;
|
||||
|
||||
[[nodiscard]] double getInactiveReactionMolarReactionFlow(
|
||||
scratch::StateBlob& ctx,
|
||||
const reaction::Reaction &reaction,
|
||||
const fourdst::composition::CompositionAbstract &comp,
|
||||
double T9,
|
||||
double rho
|
||||
) const override;
|
||||
|
||||
/**
|
||||
* @brief Computes timescales for all active species in the network.
|
||||
*
|
||||
@@ -319,6 +338,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Primes the engine with the given network input.
|
||||
*
|
||||
* @param ctx The scratchpad context for storing thread-local data.
|
||||
* @param netIn The current network input, containing temperature, density, and composition.
|
||||
* @return A PrimingReport indicating the result of the priming operation.
|
||||
*
|
||||
@@ -367,6 +387,8 @@ namespace gridfire::engine {
|
||||
[[nodiscard]] std::optional<StepDerivatives<double>>getMostRecentRHSCalculation(
|
||||
scratch::StateBlob &ctx
|
||||
) const override;
|
||||
|
||||
[[nodiscard]] std::unique_ptr<scratch::StateBlob> constructStateBlob(const scratch::StateBlob *blob) const override;
|
||||
private:
|
||||
using LogManager = fourdst::logging::LogManager;
|
||||
|
||||
@@ -399,7 +421,7 @@ namespace gridfire::engine {
|
||||
* @param netIn The current network input, containing temperature, density, and composition.
|
||||
* @return A pair with the first element a vector of ReactionFlow structs, each containing a pointer to a
|
||||
* reaction and its calculated flow rate and the second being a composition object where species which were not
|
||||
* present in netIn but are present in the definition of the base engine are registered but have 0 mass fraction
|
||||
* present in netIn but are present in the definition of the base engine are registered but have 0 mass fraction.
|
||||
*
|
||||
* @par Algorithm:
|
||||
* 1. Iterates through all species in the base engine's network.
|
||||
|
||||
@@ -255,6 +255,9 @@ namespace gridfire::engine {
|
||||
[[nodiscard]] std::optional<StepDerivatives<double>>getMostRecentRHSCalculation(
|
||||
scratch::StateBlob &ctx
|
||||
) const override;
|
||||
|
||||
[[nodiscard]] std::unique_ptr<scratch::StateBlob> constructStateBlob(const scratch::StateBlob *blob) const override;
|
||||
|
||||
protected:
|
||||
bool m_isStale = true;
|
||||
GraphEngine& m_baseEngine;
|
||||
@@ -343,7 +346,6 @@ namespace gridfire::engine {
|
||||
scratch::StateBlob& ctx,
|
||||
const std::vector<std::string>& peNames
|
||||
) const;
|
||||
|
||||
};
|
||||
|
||||
class FileDefinedEngineView final: public DefinedEngineView {
|
||||
|
||||
@@ -611,6 +611,8 @@ namespace gridfire::engine {
|
||||
[[nodiscard]] std::optional<StepDerivatives<double>>getMostRecentRHSCalculation(
|
||||
scratch::StateBlob &
|
||||
) const override;
|
||||
|
||||
[[nodiscard]] std::unique_ptr<scratch::StateBlob> constructStateBlob(const scratch::StateBlob *blob) const override;
|
||||
public:
|
||||
/**
|
||||
* @brief Struct representing a QSE group.
|
||||
@@ -990,9 +992,6 @@ namespace gridfire::engine {
|
||||
const std::vector<QSEGroup> &groups,
|
||||
const std::vector<reaction::ReactionSet> &groupReactions
|
||||
);
|
||||
|
||||
public:
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Constructs the view by looking up the priming species by symbol.
|
||||
*
|
||||
* @param ctx State Blob containing Engine context
|
||||
* @param primingSymbol Symbol string of the species to prime.
|
||||
* @param baseEngine Reference to the base DynamicEngine to wrap.
|
||||
* @pre primingSymbol must correspond to a valid species in atomic::species registry.
|
||||
@@ -46,6 +47,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Constructs the view using an existing Species object.
|
||||
*
|
||||
* @param ctx State Blob containing Engine context
|
||||
* @param primingSpecies The species object to prime.
|
||||
* @param baseEngine Reference to the base DynamicEngine to wrap.
|
||||
* @pre primingSpecies must be valid and present in the network of baseEngine.
|
||||
@@ -66,6 +68,7 @@ namespace gridfire::engine {
|
||||
/**
|
||||
* @brief Constructs the set of reaction names that involve the priming species.
|
||||
*
|
||||
* @param ctx State blob containing engine context
|
||||
* @param primingSpecies Species for which to collect priming reactions.
|
||||
* @param baseEngine Base engine containing the full network of reactions.
|
||||
* @pre baseEngine.getNetworkReactions() returns a valid iterable set of reactions.
|
||||
|
||||
@@ -11,4 +11,4 @@
|
||||
#include "gridfire/trigger/trigger.h"
|
||||
#include "gridfire/utils/utils.h"
|
||||
|
||||
#include "types/types.h"
|
||||
#include "gridfire/types/types.h"
|
||||
|
||||
@@ -58,6 +58,8 @@ namespace gridfire::solver {
|
||||
const size_t currentNonlinearIterations; ///< Total number of non-linear iterations
|
||||
const std::map<fourdst::atomic::Species, std::unordered_map<std::string, double>>& reactionContributionMap; ///< Map of reaction contributions for the current step
|
||||
engine::scratch::StateBlob& state_ctx; ///< Reference to the engine scratch state blob
|
||||
double current_total_energy = 0.0; ///< Current energy generation rate [erg/g/s]
|
||||
double current_neutrino_energy_loss_rate = 0.0; ///< Current neutrino energy loss rate [erg/g/s]
|
||||
|
||||
PointSolverTimestepContext(
|
||||
double t,
|
||||
@@ -76,6 +78,8 @@ namespace gridfire::solver {
|
||||
);
|
||||
|
||||
[[nodiscard]] std::vector<std::tuple<std::string, std::string>> describe() const override;
|
||||
|
||||
[[nodiscard]] fourdst::composition::Composition getPhysicalComposition() const;
|
||||
};
|
||||
|
||||
using TimestepCallback = std::function<void(const PointSolverTimestepContext& context)>; ///< Type alias for a timestep callback function.
|
||||
@@ -169,6 +173,13 @@ namespace gridfire::solver {
|
||||
const engine::DynamicEngine& engine
|
||||
);
|
||||
|
||||
PointSolver(
|
||||
const engine::DynamicEngine& engine,
|
||||
const config::GridFireConfig& config
|
||||
);
|
||||
|
||||
|
||||
config::GridFireConfig getConfig() const { return *m_config; }
|
||||
/**
|
||||
* @brief Integrate from t=0 to netIn.tMax and return final composition and energy.
|
||||
*
|
||||
@@ -264,6 +275,17 @@ namespace gridfire::solver {
|
||||
*/
|
||||
static int cvode_jac_wrapper(sunrealtype t, N_Vector y, N_Vector ydot, SUNMatrix J, void *user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
|
||||
|
||||
/**
|
||||
* @brief CVODE error handler that logs errors and warnings from SUNDIALS using the solver's logger.
|
||||
* @param line
|
||||
* @param func
|
||||
* @param file
|
||||
* @param msg
|
||||
* @param err_code
|
||||
* @param err_user_data
|
||||
* @param sunctx
|
||||
*/
|
||||
static void cvode_error_handler(int line, const char *func, const char *file, const char *msg, SUNErrCode err_code, void *err_user_data, SUNContext sunctx);
|
||||
/**
|
||||
* @brief Compute RHS into ydot at time t from the engine and current state y.
|
||||
*
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "gridfire/trigger/trigger_result.h"
|
||||
#include "gridfire/solver/strategies/PointSolver.h"
|
||||
#include "fourdst/logging/logging.h"
|
||||
#include "gridfire/config/config.h"
|
||||
|
||||
#include <string>
|
||||
#include <deque>
|
||||
@@ -316,6 +317,46 @@ namespace gridfire::trigger::solver::CVODE {
|
||||
bool rel_failure(const gridfire::solver::PointSolverTimestepContext& ctx) const;
|
||||
};
|
||||
|
||||
class BoundaryFluxTrigger final : public Trigger<gridfire::solver::PointSolverTimestepContext> {
|
||||
public:
|
||||
explicit BoundaryFluxTrigger(double relativeThreshold, double absoluteThreshold);
|
||||
bool check(const gridfire::solver::PointSolverTimestepContext &ctx) const override;
|
||||
void update(const gridfire::solver::PointSolverTimestepContext &ctx) override;
|
||||
void step(const gridfire::solver::PointSolverTimestepContext &ctx) override;
|
||||
void reset() override;
|
||||
|
||||
std::string name() const override;
|
||||
TriggerResult why(const gridfire::solver::PointSolverTimestepContext &ctx) const override;
|
||||
std::string describe() const override;
|
||||
size_t numTriggers() const override;
|
||||
size_t numMisses() const override;
|
||||
private:
|
||||
enum class ReactionSetType : uint8_t {
|
||||
ACTIVE,
|
||||
INACTIVE
|
||||
};
|
||||
|
||||
static double get_reaction_set_flow(
|
||||
const reaction::ReactionSet& reactions,
|
||||
const gridfire::solver::PointSolverTimestepContext& ctx,
|
||||
const fourdst::composition::Composition& comp,
|
||||
double T9,
|
||||
double rho,
|
||||
ReactionSetType type
|
||||
);
|
||||
private:
|
||||
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log");
|
||||
|
||||
mutable size_t m_hits = 0;
|
||||
mutable size_t m_misses = 0;
|
||||
mutable size_t m_updates = 0;
|
||||
mutable size_t m_resets = 0;
|
||||
|
||||
double m_relativeThreshold;
|
||||
double m_absoluteThreshold;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Compose a trigger suitable for deciding engine re-partitioning during CVODE solves.
|
||||
*
|
||||
@@ -329,18 +370,9 @@ namespace gridfire::trigger::solver::CVODE {
|
||||
* See engine_partitioning_trigger.cpp for construction details using OrTrigger and
|
||||
* EveryNthTrigger from trigger_logical.h.
|
||||
*
|
||||
* @param simulationTimeInterval Interval used by SimulationTimeTrigger (> 0).
|
||||
* @param offDiagonalThreshold Off-diagonal Jacobian magnitude threshold (>= 0).
|
||||
* @param timestepCollapseRatio Threshold for timestep deviation (>= 0, and <= 1 when relative).
|
||||
* @param maxConvergenceFailures Window size for timestep averaging (>= 1 recommended).
|
||||
* @return A unique_ptr to a composed Trigger<TimestepContext> implementing the policy above.
|
||||
*
|
||||
* @note The exact policy is subject to change; this function centralizes that decision.
|
||||
*/
|
||||
std::unique_ptr<Trigger<gridfire::solver::PointSolverTimestepContext>> makeEnginePartitioningTrigger(
|
||||
double simulationTimeInterval,
|
||||
double offDiagonalThreshold,
|
||||
double timestepCollapseRatio,
|
||||
size_t maxConvergenceFailures
|
||||
);
|
||||
std::unique_ptr<Trigger<gridfire::solver::PointSolverTimestepContext>> makeEnginePartitioningTrigger(const config::TriggerConfig& cfg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user