docs(ridfire)
Added more documentation, also moved all engine code into gridfire::engine namespace to be more in line with other parts of teh code base
This commit is contained in:
@@ -1,99 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include "gridfire/engine/engine_abstract.h"
|
||||
#include "gridfire/network.h"
|
||||
|
||||
#include <functional>
|
||||
#include <any>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <string>
|
||||
|
||||
namespace gridfire::solver {
|
||||
/**
|
||||
* @struct SolverContextBase
|
||||
* @brief Base class for solver callback contexts.
|
||||
*
|
||||
* This struct serves as a base class for contexts that can be passed to solver callbacks, it enforces
|
||||
* that derived classes implement a `describe` method that returns a vector of tuples describing
|
||||
* the context that a callback will receive when called.
|
||||
*/
|
||||
class SolverContextBase {
|
||||
public:
|
||||
virtual ~SolverContextBase() = default;
|
||||
|
||||
/**
|
||||
* @brief Describe the context for callback functions.
|
||||
* @return A vector of tuples, each containing a string for the parameters name and a string for its type.
|
||||
*
|
||||
* This method should be overridden by derived classes to provide a description of the context
|
||||
* that will be passed to the callback function. The intent of this method is that an end user can investigate
|
||||
* the context that will be passed to the callback function, and use this information to craft their own
|
||||
* callback function.
|
||||
*/
|
||||
[[nodiscard]] virtual std::vector<std::tuple<std::string, std::string>> describe() const = 0;
|
||||
};
|
||||
/**
|
||||
* @class NetworkSolverStrategy
|
||||
* @brief Abstract base class for network solver strategies.
|
||||
*
|
||||
* This class defines the interface for network solver strategies, which are responsible
|
||||
* for integrating the reaction network ODEs over a given timestep. It is templated on the
|
||||
* engine type to allow for different engine implementations to be used with the same solver.
|
||||
*
|
||||
* @tparam EngineT The type of engine to use with this solver strategy. Must inherit from Engine.
|
||||
*/
|
||||
template <typename EngineT>
|
||||
class NetworkSolverStrategy {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for the NetworkSolverStrategy.
|
||||
* @param engine The engine to use for evaluating the network.
|
||||
*/
|
||||
explicit NetworkSolverStrategy(EngineT& engine) : m_engine(engine) {};
|
||||
|
||||
/**
|
||||
* @brief Virtual destructor.
|
||||
*/
|
||||
virtual ~NetworkSolverStrategy() = default;
|
||||
|
||||
/**
|
||||
* @brief Evaluates the network for a given timestep.
|
||||
* @param netIn The input conditions for the network.
|
||||
* @return The output conditions after the timestep.
|
||||
*/
|
||||
virtual NetOut evaluate(const NetIn& netIn) = 0;
|
||||
|
||||
/**
|
||||
* @brief set the callback function to be called at the end of each timestep.
|
||||
*
|
||||
* This function allows the user to set a callback function that will be called at the end of each timestep.
|
||||
* The callback function will receive a gridfire::solver::<SOMESOLVER>::TimestepContext object. Note that
|
||||
* depending on the solver, this context may contain different information. Further, the exact
|
||||
* signature of the callback function is left up to each solver. Every solver should provide a type or type alias
|
||||
* TimestepCallback that defines the signature of the callback function so that the user can easily
|
||||
* get that type information.
|
||||
*
|
||||
* @param callback The callback function to be called at the end of each timestep.
|
||||
*/
|
||||
virtual void set_callback(const std::any& callback) = 0;
|
||||
|
||||
/**
|
||||
* @brief Describe the context that will be passed to the callback function.
|
||||
* @return A vector of tuples, each containing a string for the parameter's name and a string for its type.
|
||||
*
|
||||
* This method should be overridden by derived classes to provide a description of the context
|
||||
* that will be passed to the callback function. The intent of this method is that an end user can investigate
|
||||
* the context that will be passed to the callback function, and use this information to craft their own
|
||||
* callback function.
|
||||
*/
|
||||
[[nodiscard]] virtual std::vector<std::tuple<std::string, std::string>> describe_callback_context() const = 0;
|
||||
protected:
|
||||
EngineT& m_engine; ///< The engine used by this solver strategy.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Type alias for a network solver strategy that uses a DynamicEngine.
|
||||
*/
|
||||
using DynamicNetworkSolverStrategy = NetworkSolverStrategy<DynamicEngine>;
|
||||
}
|
||||
#include "gridfire/solver/strategies/strategies.h"
|
||||
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "gridfire/solver/solver.h"
|
||||
#include "gridfire/solver/strategies/strategy_abstract.h"
|
||||
#include "gridfire/engine/engine_abstract.h"
|
||||
#include "gridfire/network.h"
|
||||
#include "gridfire/types/types.h"
|
||||
#include "gridfire/exceptions/exceptions.h"
|
||||
|
||||
#include "fourdst/atomic/atomicSpecies.h"
|
||||
@@ -85,7 +85,7 @@ namespace gridfire::solver {
|
||||
* @param engine DynamicEngine used for RHS/Jacobian evaluation and network access.
|
||||
* @throws std::runtime_error If SUNContext_Create fails.
|
||||
*/
|
||||
explicit CVODESolverStrategy(DynamicEngine& engine);
|
||||
explicit CVODESolverStrategy(engine::DynamicEngine& engine);
|
||||
/**
|
||||
* @brief Destructor: cleans CVODE/SUNDIALS resources and frees SUNContext.
|
||||
*/
|
||||
@@ -152,6 +152,12 @@ namespace gridfire::solver {
|
||||
*/
|
||||
void set_stdout_logging_enabled(bool logging_enabled);
|
||||
|
||||
void set_absTol(double absTol);
|
||||
void set_relTol(double relTol);
|
||||
|
||||
double get_absTol() const;
|
||||
double get_relTol() const;
|
||||
|
||||
/**
|
||||
* @brief Schema of fields exposed to the timestep callback context.
|
||||
*/
|
||||
@@ -173,7 +179,7 @@ namespace gridfire::solver {
|
||||
const double T9; ///< Temperature in GK.
|
||||
const double rho; ///< Density [g cm^-3].
|
||||
const size_t num_steps; ///< Number of CVODE steps taken so far.
|
||||
const DynamicEngine& engine; ///< Reference to the engine.
|
||||
const engine::DynamicEngine& engine; ///< Reference to the engine.
|
||||
const std::vector<fourdst::atomic::Species>& networkSpecies; ///< Species layout.
|
||||
const size_t currentConvergenceFailures; ///< Total number of convergence failures
|
||||
const size_t currentNonlinearIterations; ///< Total number of non-linear iterations
|
||||
@@ -190,7 +196,7 @@ namespace gridfire::solver {
|
||||
double t9,
|
||||
double rho,
|
||||
size_t num_steps,
|
||||
const DynamicEngine& engine,
|
||||
const engine::DynamicEngine& engine,
|
||||
const std::vector<fourdst::atomic::Species>& networkSpecies,
|
||||
size_t currentConvergenceFailure,
|
||||
size_t currentNonlinearIterations,
|
||||
@@ -219,12 +225,12 @@ namespace gridfire::solver {
|
||||
*/
|
||||
struct CVODEUserData {
|
||||
CVODESolverStrategy* solver_instance{}; // Pointer back to the class instance
|
||||
DynamicEngine* engine{};
|
||||
engine::DynamicEngine* engine{};
|
||||
double T9{};
|
||||
double rho{};
|
||||
double energy{};
|
||||
const std::vector<fourdst::atomic::Species>* networkSpecies{};
|
||||
std::unique_ptr<exceptions::StaleEngineTrigger> captured_exception = nullptr;
|
||||
std::unique_ptr<exceptions::EngineError> captured_exception = nullptr;
|
||||
std::optional<std::map<fourdst::atomic::Species, std::unordered_map<std::string, double>>> reaction_contribution_map;
|
||||
};
|
||||
|
||||
@@ -281,6 +287,9 @@ namespace gridfire::solver {
|
||||
*/
|
||||
void cleanup_cvode_resources(bool memFree);
|
||||
|
||||
void set_detailed_step_logging(bool enabled);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Compute and print per-component error ratios; run diagnostic helpers.
|
||||
*
|
||||
@@ -288,8 +297,8 @@ namespace gridfire::solver {
|
||||
* sorted table of species with the highest error ratios; then invokes diagnostic routines to
|
||||
* inspect Jacobian stiffness and species balance.
|
||||
*/
|
||||
void log_step_diagnostics(const CVODEUserData& user_data, bool displayJacobianStiffness, bool saveIntermediateJacobians, bool
|
||||
displaySpeciesBalance) const;
|
||||
void log_step_diagnostics(const CVODEUserData& user_data, bool displayJacobianStiffness, bool
|
||||
displaySpeciesBalance, bool to_file, std::optional<std::string> filename) const;
|
||||
private:
|
||||
SUNContext m_sun_ctx = nullptr; ///< SUNDIALS context (lifetime of the solver).
|
||||
void* m_cvode_mem = nullptr; ///< CVODE memory block.
|
||||
@@ -305,5 +314,10 @@ namespace gridfire::solver {
|
||||
bool m_stdout_logging_enabled = true; ///< If true, print per-step logs and use CV_ONE_STEP.
|
||||
|
||||
N_Vector m_constraints = nullptr; ///< CVODE constraints vector (>= 0 for species entries).
|
||||
|
||||
std::optional<double> m_absTol; ///< User-specified absolute tolerance.
|
||||
std::optional<double> m_relTol; ///< User-specified relative tolerance.
|
||||
|
||||
bool m_detailed_step_logging = false; ///< If true, log detailed step diagnostics (error ratios, Jacobian, species balance).
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "gridfire/solver/strategies/triggers/triggers.h"
|
||||
#include "gridfire/solver/strategies/strategy_abstract.h"
|
||||
#include "gridfire/solver/strategies/CVODE_solver_strategy.h"
|
||||
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
#include "gridfire/engine/engine_abstract.h"
|
||||
#include "gridfire/types/types.h"
|
||||
|
||||
#include <functional>
|
||||
#include <any>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <string>
|
||||
|
||||
namespace gridfire::solver {
|
||||
/**
|
||||
* @struct SolverContextBase
|
||||
* @brief Base class for solver callback contexts.
|
||||
*
|
||||
* This struct serves as a base class for contexts that can be passed to solver callbacks, it enforces
|
||||
* that derived classes implement a `describe` method that returns a vector of tuples describing
|
||||
* the context that a callback will receive when called.
|
||||
*/
|
||||
class SolverContextBase {
|
||||
public:
|
||||
virtual ~SolverContextBase() = default;
|
||||
|
||||
/**
|
||||
* @brief Describe the context for callback functions.
|
||||
* @return A vector of tuples, each containing a string for the parameters name and a string for its type.
|
||||
*
|
||||
* This method should be overridden by derived classes to provide a description of the context
|
||||
* that will be passed to the callback function. The intent of this method is that an end user can investigate
|
||||
* the context that will be passed to the callback function, and use this information to craft their own
|
||||
* callback function.
|
||||
*/
|
||||
[[nodiscard]] virtual std::vector<std::tuple<std::string, std::string>> describe() const = 0;
|
||||
};
|
||||
/**
|
||||
* @class NetworkSolverStrategy
|
||||
* @brief Abstract base class for network solver strategies.
|
||||
*
|
||||
* This class defines the interface for network solver strategies, which are responsible
|
||||
* for integrating the reaction network ODEs over a given timestep. It is templated on the
|
||||
* engine type to allow for different engine implementations to be used with the same solver.
|
||||
*
|
||||
* @tparam EngineT The type of engine to use with this solver strategy. Must inherit from Engine.
|
||||
*/
|
||||
template <typename EngineT>
|
||||
class NetworkSolverStrategy {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for the NetworkSolverStrategy.
|
||||
* @param engine The engine to use for evaluating the network.
|
||||
*/
|
||||
explicit NetworkSolverStrategy(EngineT& engine) : m_engine(engine) {};
|
||||
|
||||
/**
|
||||
* @brief Virtual destructor.
|
||||
*/
|
||||
virtual ~NetworkSolverStrategy() = default;
|
||||
|
||||
/**
|
||||
* @brief Evaluates the network for a given timestep.
|
||||
* @param netIn The input conditions for the network.
|
||||
* @return The output conditions after the timestep.
|
||||
*/
|
||||
virtual NetOut evaluate(const NetIn& netIn) = 0;
|
||||
|
||||
/**
|
||||
* @brief set the callback function to be called at the end of each timestep.
|
||||
*
|
||||
* This function allows the user to set a callback function that will be called at the end of each timestep.
|
||||
* The callback function will receive a gridfire::solver::<SOMESOLVER>::TimestepContext object. Note that
|
||||
* depending on the solver, this context may contain different information. Further, the exact
|
||||
* signature of the callback function is left up to each solver. Every solver should provide a type or type alias
|
||||
* TimestepCallback that defines the signature of the callback function so that the user can easily
|
||||
* get that type information.
|
||||
*
|
||||
* @param callback The callback function to be called at the end of each timestep.
|
||||
*/
|
||||
virtual void set_callback(const std::any& callback) = 0;
|
||||
|
||||
/**
|
||||
* @brief Describe the context that will be passed to the callback function.
|
||||
* @return A vector of tuples, each containing a string for the parameter's name and a string for its type.
|
||||
*
|
||||
* This method should be overridden by derived classes to provide a description of the context
|
||||
* that will be passed to the callback function. The intent of this method is that an end user can investigate
|
||||
* the context that will be passed to the callback function, and use this information to craft their own
|
||||
* callback function.
|
||||
*/
|
||||
[[nodiscard]] virtual std::vector<std::tuple<std::string, std::string>> describe_callback_context() const = 0;
|
||||
protected:
|
||||
EngineT& m_engine; ///< The engine used by this solver strategy.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Type alias for a network solver strategy that uses a DynamicEngine.
|
||||
*/
|
||||
using DynamicNetworkSolverStrategy = NetworkSolverStrategy<engine::DynamicEngine>;
|
||||
}
|
||||
|
||||
@@ -331,8 +331,8 @@ namespace gridfire::trigger::solver::CVODE {
|
||||
*
|
||||
* @param simulationTimeInterval Interval used by SimulationTimeTrigger (> 0).
|
||||
* @param offDiagonalThreshold Off-diagonal Jacobian magnitude threshold (>= 0).
|
||||
* @param relativeTimestepCollapseThreshold Threshold for timestep deviation (>= 0, and <= 1 when relative).
|
||||
* @param timestepGrowthWindowSize Window size for timestep averaging (>= 1 recommended).
|
||||
* @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.
|
||||
@@ -340,7 +340,7 @@ namespace gridfire::trigger::solver::CVODE {
|
||||
std::unique_ptr<Trigger<gridfire::solver::CVODESolverStrategy::TimestepContext>> makeEnginePartitioningTrigger(
|
||||
const double simulationTimeInterval,
|
||||
const double offDiagonalThreshold,
|
||||
const double relativeTimestepCollapseThreshold,
|
||||
const size_t timestepGrowthWindowSize
|
||||
const double timestepCollapseRatio,
|
||||
const size_t maxConvergenceFailures
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include "gridfire/solver/strategies/triggers/engine_partitioning_trigger.h"
|
||||
Reference in New Issue
Block a user