2025-07-01 07:24:18 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
2025-07-01 11:40:03 -04:00
|
|
|
#include "gridfire/engine/views/engine_view_abstract.h"
|
|
|
|
|
#include "gridfire/engine/engine_abstract.h"
|
2025-10-22 09:54:10 -04:00
|
|
|
#include "gridfire/engine/engine_graph.h"
|
2025-07-01 11:40:03 -04:00
|
|
|
#include "gridfire/io/network_file.h"
|
|
|
|
|
#include "gridfire/network.h"
|
2025-07-01 07:24:18 -04:00
|
|
|
|
|
|
|
|
#include "fourdst/config/config.h"
|
|
|
|
|
#include "fourdst/logging/logging.h"
|
|
|
|
|
|
|
|
|
|
#include "quill/Logger.h"
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
namespace gridfire{
|
2025-07-10 09:36:05 -04:00
|
|
|
class DefinedEngineView : public DynamicEngine, public EngineView<DynamicEngine> {
|
2025-07-01 07:24:18 -04:00
|
|
|
public:
|
2025-10-22 09:54:10 -04:00
|
|
|
DefinedEngineView(const std::vector<std::string>& peNames, GraphEngine& baseEngine);
|
|
|
|
|
|
|
|
|
|
/** @brief Get the base engine associated with this defined engine view.
|
|
|
|
|
* @return A const reference to the base DynamicEngine.
|
|
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] const DynamicEngine& getBaseEngine() const override;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
|
|
|
|
// --- Engine Interface ---
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Gets the list of active species in the network defined by the file.
|
|
|
|
|
* @return A const reference to the vector of active species.
|
|
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] const std::vector<fourdst::atomic::Species>& getNetworkSpecies() const override;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
|
|
|
|
// --- DynamicEngine Interface ---
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Calculates the right-hand side (dY/dt) and energy generation for the active species.
|
|
|
|
|
*
|
2025-10-07 15:16:03 -04:00
|
|
|
* @param comp A Composition object containing the current composition of the system
|
2025-07-01 15:06:22 -04:00
|
|
|
* @param T9 The temperature in units of 10^9 K.
|
|
|
|
|
* @param rho The density in g/cm^3.
|
|
|
|
|
* @return A StepDerivatives struct containing the derivatives of the active species and the
|
|
|
|
|
* nuclear energy generation rate.
|
|
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale (i.e., `update()` has not been called after `setNetworkFile()`).
|
|
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] std::expected<StepDerivatives<double>, expectations::StaleEngineError> calculateRHSAndEnergy(
|
2025-10-07 15:16:03 -04:00
|
|
|
const fourdst::composition::Composition& comp,
|
|
|
|
|
double T9,
|
|
|
|
|
double rho
|
2025-07-01 07:24:18 -04:00
|
|
|
) const override;
|
2025-09-19 15:14:46 -04:00
|
|
|
|
|
|
|
|
[[nodiscard]] EnergyDerivatives calculateEpsDerivatives(
|
2025-10-07 15:16:03 -04:00
|
|
|
const fourdst::composition::Composition& comp,
|
|
|
|
|
double T9,
|
|
|
|
|
double rho
|
2025-09-19 15:14:46 -04:00
|
|
|
) const override;
|
|
|
|
|
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Generates the Jacobian matrix for the active species.
|
|
|
|
|
*
|
2025-10-07 15:16:03 -04:00
|
|
|
* @param comp A Composition object containing the current composition of the system
|
2025-07-01 15:06:22 -04:00
|
|
|
* @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.
|
|
|
|
|
*/
|
2025-07-01 07:24:18 -04:00
|
|
|
void generateJacobianMatrix(
|
2025-10-07 15:16:03 -04:00
|
|
|
const fourdst::composition::Composition& comp,
|
2025-10-24 11:17:22 -04:00
|
|
|
double T9,
|
|
|
|
|
double rho
|
|
|
|
|
) const override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Generates the Jacobian matrix for the active species.
|
|
|
|
|
*
|
|
|
|
|
* @param comp A Composition object containing the current composition of the system
|
|
|
|
|
* @param T9 The temperature in units of 10^9 K.
|
|
|
|
|
* @param rho The density in g/cm^3.
|
|
|
|
|
* @param activeSpecies The vector of active species to include in the Jacobian.
|
|
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale.
|
|
|
|
|
*/
|
|
|
|
|
void generateJacobianMatrix(
|
|
|
|
|
const fourdst::composition::Composition &comp,
|
|
|
|
|
double T9,
|
|
|
|
|
double rho,
|
|
|
|
|
const std::vector<fourdst::atomic::Species> &activeSpecies
|
|
|
|
|
) const override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Generates the Jacobian matrix for a given sparsity pattern
|
|
|
|
|
*
|
|
|
|
|
* @param comp A Composition object containing the current composition of the system
|
|
|
|
|
* @param T9 The temperature in units of 10^9 K.
|
|
|
|
|
* @param rho The density in g/cm^3.
|
|
|
|
|
* @param sparsityPattern The sparsity pattern to use for the Jacobian matrix.
|
|
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale.
|
|
|
|
|
*/
|
|
|
|
|
void generateJacobianMatrix(
|
|
|
|
|
const fourdst::composition::Composition &comp,
|
|
|
|
|
double T9,
|
|
|
|
|
double rho,
|
|
|
|
|
const SparsityPattern &sparsityPattern
|
2025-07-18 15:23:43 -04:00
|
|
|
) const override;
|
2025-10-24 11:17:22 -04:00
|
|
|
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Gets an entry from the Jacobian matrix for the active species.
|
|
|
|
|
*
|
2025-10-07 15:16:03 -04:00
|
|
|
* @param rowSpecies The species corresponding to the row index.
|
|
|
|
|
* @param colSpecies The species corresponding to the column index.
|
|
|
|
|
* @return The value of the Jacobian matrix at (row species index, col species index).
|
2025-07-01 15:06:22 -04:00
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale.
|
|
|
|
|
* @throws std::out_of_range If an index is out of bounds.
|
|
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] double getJacobianMatrixEntry(
|
2025-10-07 15:16:03 -04:00
|
|
|
const fourdst::atomic::Species& rowSpecies,
|
|
|
|
|
const fourdst::atomic::Species& colSpecies
|
2025-07-01 07:24:18 -04:00
|
|
|
) const override;
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Generates the stoichiometry matrix for the active reactions and species.
|
|
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale.
|
|
|
|
|
*/
|
2025-07-01 07:24:18 -04:00
|
|
|
void generateStoichiometryMatrix() override;
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Gets an entry from the stoichiometry matrix for the active species and reactions.
|
|
|
|
|
*
|
2025-10-07 15:16:03 -04:00
|
|
|
* @param species The species for which to get the stoichiometric coefficient.
|
|
|
|
|
* @param reaction The reaction for which to get the stoichiometric coefficient.
|
2025-07-01 15:06:22 -04:00
|
|
|
* @return The stoichiometric coefficient for the given species and reaction.
|
|
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale.
|
|
|
|
|
* @throws std::out_of_range If an index is out of bounds.
|
|
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] int getStoichiometryMatrixEntry(
|
2025-10-07 15:16:03 -04:00
|
|
|
const fourdst::atomic::Species& species,
|
|
|
|
|
const reaction::Reaction& reaction
|
2025-07-01 07:24:18 -04:00
|
|
|
) const override;
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Calculates the molar reaction flow for a given reaction in the active network.
|
|
|
|
|
*
|
|
|
|
|
* @param reaction The reaction for which to calculate the flow.
|
2025-10-07 15:16:03 -04:00
|
|
|
* @param comp A Composition object containing the current composition of the system
|
2025-07-01 15:06:22 -04:00
|
|
|
* @param T9 Temperature in units of 10^9 K.
|
|
|
|
|
* @param rho Density in g/cm^3.
|
|
|
|
|
* @return Molar flow rate for the reaction (e.g., mol/g/s).
|
|
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale or if the reaction is not in the active set.
|
|
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] double calculateMolarReactionFlow(
|
2025-07-01 07:24:18 -04:00
|
|
|
const reaction::Reaction& reaction,
|
2025-10-07 15:16:03 -04:00
|
|
|
const fourdst::composition::Composition& comp,
|
|
|
|
|
double T9,
|
|
|
|
|
double rho
|
2025-07-01 07:24:18 -04:00
|
|
|
) const override;
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Gets the set of active logical reactions in the network.
|
|
|
|
|
*
|
|
|
|
|
* @return Reference to the LogicalReactionSet containing all active reactions.
|
|
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale.
|
|
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] const reaction::ReactionSet& getNetworkReactions() const override;
|
2025-07-22 12:48:24 -04:00
|
|
|
|
2025-08-14 13:33:46 -04:00
|
|
|
void setNetworkReactions(const reaction::ReactionSet& reactions) override;
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Computes timescales for all active species in the network.
|
|
|
|
|
*
|
2025-10-07 15:16:03 -04:00
|
|
|
* @param comp A Composition object containing the current composition of the system
|
2025-07-01 15:06:22 -04:00
|
|
|
* @param T9 Temperature in units of 10^9 K.
|
|
|
|
|
* @param rho Density in g/cm^3.
|
|
|
|
|
* @return Map from Species to their characteristic timescales (s).
|
|
|
|
|
*
|
|
|
|
|
* @throws std::runtime_error If the view is stale.
|
|
|
|
|
*/
|
2025-07-22 12:48:24 -04:00
|
|
|
[[nodiscard]] std::expected<std::unordered_map<fourdst::atomic::Species, double>, expectations::StaleEngineError> getSpeciesTimescales(
|
2025-10-07 15:16:03 -04:00
|
|
|
const fourdst::composition::Composition& comp,
|
|
|
|
|
double T9,
|
|
|
|
|
double rho
|
2025-07-22 12:48:24 -04:00
|
|
|
) const override;
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] std::expected<std::unordered_map<fourdst::atomic::Species, double>, expectations::StaleEngineError> getSpeciesDestructionTimescales(
|
2025-10-07 15:16:03 -04:00
|
|
|
const fourdst::composition::Composition& comp,
|
|
|
|
|
double T9,
|
|
|
|
|
double rho
|
2025-07-01 07:24:18 -04:00
|
|
|
) const override;
|
2025-07-01 11:40:03 -04:00
|
|
|
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Updates the engine view if it is marked as stale.
|
|
|
|
|
*
|
|
|
|
|
* This method checks if the view is stale (e.g., after `setNetworkFile` was called).
|
|
|
|
|
* If it is, it rebuilds the active network from the currently set file.
|
|
|
|
|
* The `netIn` parameter is not used by this implementation but is required by the interface.
|
|
|
|
|
*
|
|
|
|
|
* @param netIn The current network input (unused).
|
|
|
|
|
*
|
|
|
|
|
* @post If the view was stale, it is rebuilt and is no longer stale.
|
|
|
|
|
*/
|
2025-07-18 15:23:43 -04:00
|
|
|
fourdst::composition::Composition update(const NetIn &netIn) override;
|
|
|
|
|
|
2025-10-22 09:54:10 -04:00
|
|
|
[[deprecated]] bool isStale(const NetIn& netIn) override;
|
2025-07-01 11:40:03 -04:00
|
|
|
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Sets the screening model for the base engine.
|
|
|
|
|
*
|
|
|
|
|
* @param model The screening model to set.
|
|
|
|
|
*/
|
2025-07-01 11:40:03 -04:00
|
|
|
void setScreeningModel(screening::ScreeningType model) override;
|
|
|
|
|
|
2025-07-01 15:06:22 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Gets the screening model from the base engine.
|
|
|
|
|
*
|
|
|
|
|
* @return The current screening model type.
|
|
|
|
|
*/
|
2025-07-01 11:40:03 -04:00
|
|
|
[[nodiscard]] screening::ScreeningType getScreeningModel() const override;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] size_t getSpeciesIndex(const fourdst::atomic::Species &species) const override;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
2025-07-10 09:36:05 -04:00
|
|
|
[[nodiscard]] std::vector<double> mapNetInToMolarAbundanceVector(const NetIn &netIn) const override;
|
2025-07-14 14:50:49 -04:00
|
|
|
|
|
|
|
|
[[nodiscard]] PrimingReport primeEngine(const NetIn &netIn) override;
|
2025-10-30 15:05:08 -04:00
|
|
|
|
|
|
|
|
fourdst::composition::Composition collectComposition(fourdst::composition::Composition &comp) const override;
|
2025-07-10 09:36:05 -04:00
|
|
|
protected:
|
|
|
|
|
bool m_isStale = true;
|
2025-10-22 09:54:10 -04:00
|
|
|
GraphEngine& m_baseEngine;
|
2025-07-10 09:36:05 -04:00
|
|
|
private:
|
|
|
|
|
quill::Logger* m_logger = fourdst::logging::LogManager::getInstance().getLogger("log"); ///< Logger instance for trace and debug information.
|
2025-07-01 15:06:22 -04:00
|
|
|
///< Active species in the defined engine.
|
2025-10-22 09:54:10 -04:00
|
|
|
std::set<fourdst::atomic::Species> m_activeSpecies;
|
|
|
|
|
|
|
|
|
|
///< Cache for the active species vector to avoid dangling references.
|
|
|
|
|
mutable std::optional<std::vector<fourdst::atomic::Species>> m_activeSpeciesVectorCache = std::nullopt;
|
|
|
|
|
|
2025-07-01 15:06:22 -04:00
|
|
|
///< Active reactions in the defined engine.
|
2025-08-14 13:33:46 -04:00
|
|
|
reaction::ReactionSet m_activeReactions;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
2025-07-01 15:06:22 -04:00
|
|
|
///< Maps indices of active species to indices in the full network.
|
|
|
|
|
std::vector<size_t> m_speciesIndexMap;
|
|
|
|
|
///< Maps indices of active reactions to indices in the full network.
|
|
|
|
|
std::vector<size_t> m_reactionIndexMap;
|
2025-07-01 07:24:18 -04:00
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* @brief Constructs the species index map.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @return A vector mapping defined species indices to full species indices.
|
2025-07-01 07:24:18 -04:00
|
|
|
*
|
|
|
|
|
* This method creates a map from the indices of the active species to the indices of the
|
|
|
|
|
* corresponding species in the full network.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @throws std::runtime_error If an active species is not found in the base engine's species list.
|
2025-07-01 07:24:18 -04:00
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] std::vector<size_t> constructSpeciesIndexMap() const;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Constructs the reaction index map.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @return A vector mapping defined reaction indices to full reaction indices.
|
2025-07-01 07:24:18 -04:00
|
|
|
*
|
|
|
|
|
* This method creates a map from the indices of the active reactions to the indices of the
|
|
|
|
|
* corresponding reactions in the full network.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @throws std::runtime_error If an active reaction is not found in the base engine's reaction list.
|
2025-07-01 07:24:18 -04:00
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] std::vector<size_t> constructReactionIndexMap() const;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Maps a vector of culled abundances to a vector of full abundances.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @param defined A vector of abundances for the active species.
|
2025-07-01 07:24:18 -04:00
|
|
|
* @return A vector of abundances for the full network, with the abundances of the active
|
2025-07-01 15:06:22 -04:00
|
|
|
* species copied from the defined vector.
|
2025-07-01 07:24:18 -04:00
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] std::vector<double> mapViewToFull(const std::vector<double>& defined) const;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Maps a vector of full abundances to a vector of culled abundances.
|
|
|
|
|
*
|
|
|
|
|
* @param full A vector of abundances for the full network.
|
|
|
|
|
* @return A vector of abundances for the active species, with the abundances of the active
|
|
|
|
|
* species copied from the full vector.
|
|
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] std::vector<double> mapFullToView(const std::vector<double>& full) const;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Maps a culled species index to a full species index.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @param definedSpeciesIndex The index of the species in the defined species list.
|
2025-07-01 07:24:18 -04:00
|
|
|
* @return The index of the corresponding species in the full network.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @throws std::out_of_range If the defined index is out of bounds for the species index map.
|
2025-07-01 07:24:18 -04:00
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] size_t mapViewToFullSpeciesIndex(size_t definedSpeciesIndex) const;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Maps a culled reaction index to a full reaction index.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @param definedReactionIndex The index of the reaction in the defined reaction list.
|
2025-07-01 07:24:18 -04:00
|
|
|
* @return The index of the corresponding reaction in the full network.
|
|
|
|
|
*
|
2025-07-01 15:06:22 -04:00
|
|
|
* @throws std::out_of_range If the defined index is out of bounds for the reaction index map.
|
2025-07-01 07:24:18 -04:00
|
|
|
*/
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] size_t mapViewToFullReactionIndex(size_t definedReactionIndex) const;
|
2025-07-01 07:24:18 -04:00
|
|
|
|
2025-07-01 11:40:03 -04:00
|
|
|
void validateNetworkState() const;
|
2025-07-22 12:48:24 -04:00
|
|
|
|
|
|
|
|
void collect(const std::vector<std::string>& peNames);
|
|
|
|
|
|
2025-07-01 07:24:18 -04:00
|
|
|
};
|
2025-07-10 09:36:05 -04:00
|
|
|
|
|
|
|
|
class FileDefinedEngineView final: public DefinedEngineView {
|
|
|
|
|
public:
|
|
|
|
|
explicit FileDefinedEngineView(
|
2025-10-22 09:54:10 -04:00
|
|
|
GraphEngine& baseEngine,
|
2025-07-10 09:36:05 -04:00
|
|
|
const std::string& fileName,
|
|
|
|
|
const io::NetworkFileParser& parser
|
|
|
|
|
);
|
2025-08-14 13:33:46 -04:00
|
|
|
[[nodiscard]] std::string getNetworkFile() const { return m_fileName; }
|
|
|
|
|
[[nodiscard]] const io::NetworkFileParser& getParser() const { return m_parser; }
|
2025-07-10 09:36:05 -04:00
|
|
|
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;
|
|
|
|
|
};
|
2025-07-01 07:24:18 -04:00
|
|
|
}
|