feat(GridFire): added weak electron screening
This commit is contained in:
@@ -509,7 +509,6 @@ namespace gridfire::approx8{
|
||||
vector_type Approx8Network::convert_netIn(const NetIn &netIn) {
|
||||
vector_type y(Approx8Net::nVar, 0.0);
|
||||
y[Approx8Net::ih1] = netIn.composition.getNumberFraction("H-1");
|
||||
std::cout << "Approx8::convert_netIn -> H-1 fraction: " << y[Approx8Net::ih1] << std::endl;
|
||||
y[Approx8Net::ihe3] = netIn.composition.getNumberFraction("He-3");
|
||||
y[Approx8Net::ihe4] = netIn.composition.getNumberFraction("He-4");
|
||||
y[Approx8Net::ic12] = netIn.composition.getNumberFraction("C-12");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "gridfire/engine/engine_graph.h"
|
||||
#include "gridfire/reaction/reaction.h"
|
||||
#include "gridfire/network.h"
|
||||
#include "gridfire/screening/screening_types.h"
|
||||
|
||||
#include "fourdst/composition/species.h"
|
||||
#include "fourdst/composition/atomicSpecies.h"
|
||||
@@ -262,6 +263,15 @@ namespace gridfire {
|
||||
return calculateAllDerivatives<ADDouble>(Y_in, T9, rho);
|
||||
}
|
||||
|
||||
void GraphEngine::setScreeningModel(const screening::ScreeningType model) {
|
||||
m_screeningModel = screening::selectScreeningModel(model);
|
||||
m_screeningType = model;
|
||||
}
|
||||
|
||||
screening::ScreeningType GraphEngine::getScreeningModel() const {
|
||||
return m_screeningType;
|
||||
}
|
||||
|
||||
double GraphEngine::calculateMolarReactionFlow(
|
||||
const reaction::Reaction &reaction,
|
||||
const std::vector<double> &Y,
|
||||
@@ -439,6 +449,10 @@ namespace gridfire {
|
||||
return speciesTimescales;
|
||||
}
|
||||
|
||||
void GraphEngine::update(const NetIn &netIn) {
|
||||
return; // No-op for GraphEngine, as it does not support manually triggering updates.
|
||||
}
|
||||
|
||||
void GraphEngine::recordADTape() {
|
||||
LOG_TRACE_L1(m_logger, "Recording AD tape for the RHS calculation...");
|
||||
|
||||
|
||||
@@ -213,6 +213,14 @@ namespace gridfire {
|
||||
|
||||
}
|
||||
|
||||
void AdaptiveEngineView::setScreeningModel(const screening::ScreeningType model) {
|
||||
m_baseEngine.setScreeningModel(model);
|
||||
}
|
||||
|
||||
screening::ScreeningType AdaptiveEngineView::getScreeningModel() const {
|
||||
return m_baseEngine.getScreeningModel();
|
||||
}
|
||||
|
||||
std::vector<double> AdaptiveEngineView::mapCulledToFull(const std::vector<double>& culled) const {
|
||||
std::vector<double> full(m_baseEngine.getNetworkSpecies().size(), 0.0);
|
||||
for (size_t i_culled = 0; i_culled < culled.size(); ++i_culled) {
|
||||
@@ -338,7 +346,7 @@ namespace gridfire {
|
||||
const double maxFlow
|
||||
) const {
|
||||
LOG_TRACE_L1(m_logger, "Culling reactions based on flow rates...");
|
||||
const double relative_culling_threshold = m_config.get<double>("gridfire:AdaptiveEngineView:RelativeCullingThreshold", 1e-75);
|
||||
const auto relative_culling_threshold = m_config.get<double>("gridfire:AdaptiveEngineView:RelativeCullingThreshold", 1e-75);
|
||||
double absoluteCullingThreshold = relative_culling_threshold * maxFlow;
|
||||
LOG_DEBUG(m_logger, "Relative culling threshold: {:0.3E} ({})", relative_culling_threshold, absoluteCullingThreshold);
|
||||
std::vector<const reaction::LogicalReaction*> culledReactions;
|
||||
|
||||
@@ -1,3 +1,310 @@
|
||||
//
|
||||
// Created by Emily Boudreaux on 6/30/25.
|
||||
//
|
||||
#include "gridfire/engine/views/engine_defined.h"
|
||||
|
||||
#include "quill/LogMacros.h"
|
||||
|
||||
namespace gridfire {
|
||||
using fourdst::atomic::Species;
|
||||
|
||||
FileDefinedEngineView::FileDefinedEngineView(
|
||||
DynamicEngine &baseEngine,
|
||||
const std::string &fileName,
|
||||
const io::NetworkFileParser &parser
|
||||
):
|
||||
m_baseEngine(baseEngine),
|
||||
m_fileName(fileName),
|
||||
m_parser(parser),
|
||||
m_activeSpecies(baseEngine.getNetworkSpecies()),
|
||||
m_activeReactions(baseEngine.getNetworkReactions()) {
|
||||
buildFromFile(fileName);
|
||||
}
|
||||
|
||||
const DynamicEngine & FileDefinedEngineView::getBaseEngine() const {
|
||||
return m_baseEngine;
|
||||
}
|
||||
|
||||
const std::vector<Species> & FileDefinedEngineView::getNetworkSpecies() const {
|
||||
return m_activeSpecies;
|
||||
}
|
||||
|
||||
StepDerivatives<double> FileDefinedEngineView::calculateRHSAndEnergy(
|
||||
const std::vector<double> &Y_defined,
|
||||
const double T9,
|
||||
const double rho
|
||||
) const {
|
||||
validateNetworkState();
|
||||
|
||||
const auto Y_full = mapViewToFull(Y_defined);
|
||||
const auto [dydt, nuclearEnergyGenerationRate] = m_baseEngine.calculateRHSAndEnergy(Y_full, T9, rho);
|
||||
|
||||
StepDerivatives<double> definedResults;
|
||||
definedResults.nuclearEnergyGenerationRate = nuclearEnergyGenerationRate;
|
||||
definedResults.dydt = mapFullToView(dydt);
|
||||
return definedResults;
|
||||
}
|
||||
|
||||
void FileDefinedEngineView::generateJacobianMatrix(
|
||||
const std::vector<double> &Y_defined,
|
||||
const double T9,
|
||||
const double rho
|
||||
) {
|
||||
validateNetworkState();
|
||||
|
||||
const auto Y_full = mapViewToFull(Y_defined);
|
||||
m_baseEngine.generateJacobianMatrix(Y_full, T9, rho);
|
||||
}
|
||||
|
||||
double FileDefinedEngineView::getJacobianMatrixEntry(
|
||||
const int i_defined,
|
||||
const int j_defined
|
||||
) const {
|
||||
validateNetworkState();
|
||||
|
||||
const size_t i_full = mapViewToFullSpeciesIndex(i_defined);
|
||||
const size_t j_full = mapViewToFullSpeciesIndex(j_defined);
|
||||
|
||||
return m_baseEngine.getJacobianMatrixEntry(i_full, j_full);
|
||||
}
|
||||
|
||||
void FileDefinedEngineView::generateStoichiometryMatrix() {
|
||||
validateNetworkState();
|
||||
|
||||
m_baseEngine.generateStoichiometryMatrix();
|
||||
}
|
||||
|
||||
int FileDefinedEngineView::getStoichiometryMatrixEntry(
|
||||
const int speciesIndex_defined,
|
||||
const int reactionIndex_defined
|
||||
) const {
|
||||
validateNetworkState();
|
||||
|
||||
const size_t i_full = mapViewToFullSpeciesIndex(speciesIndex_defined);
|
||||
const size_t j_full = mapViewToFullReactionIndex(reactionIndex_defined);
|
||||
return m_baseEngine.getStoichiometryMatrixEntry(i_full, j_full);
|
||||
}
|
||||
|
||||
double FileDefinedEngineView::calculateMolarReactionFlow(
|
||||
const reaction::Reaction &reaction,
|
||||
const std::vector<double> &Y_defined,
|
||||
const double T9,
|
||||
const double rho
|
||||
) const {
|
||||
validateNetworkState();
|
||||
|
||||
if (!m_activeReactions.contains(reaction)) {
|
||||
LOG_ERROR(m_logger, "Reaction '{}' is not part of the active reactions in the file defined engine view.", reaction.id());
|
||||
m_logger -> flush_log();
|
||||
throw std::runtime_error("Reaction not found in active reactions: " + std::string(reaction.id()));
|
||||
}
|
||||
const auto Y_full = mapViewToFull(Y_defined);
|
||||
return m_baseEngine.calculateMolarReactionFlow(reaction, Y_full, T9, rho);
|
||||
}
|
||||
|
||||
const reaction::LogicalReactionSet & FileDefinedEngineView::getNetworkReactions() const {
|
||||
validateNetworkState();
|
||||
|
||||
return m_activeReactions;
|
||||
}
|
||||
|
||||
std::unordered_map<Species, double> FileDefinedEngineView::getSpeciesTimescales(
|
||||
const std::vector<double> &Y_defined,
|
||||
const double T9,
|
||||
const double rho
|
||||
) const {
|
||||
validateNetworkState();
|
||||
|
||||
const auto Y_full = mapViewToFull(Y_defined);
|
||||
const auto fullTimescales = m_baseEngine.getSpeciesTimescales(Y_full, T9, rho);
|
||||
|
||||
std::unordered_map<Species, double> definedTimescales;
|
||||
for (const auto& active_species : m_activeSpecies) {
|
||||
if (fullTimescales.contains(active_species)) {
|
||||
definedTimescales[active_species] = fullTimescales.at(active_species);
|
||||
}
|
||||
}
|
||||
return definedTimescales;
|
||||
}
|
||||
|
||||
void FileDefinedEngineView::update(const NetIn &netIn) {
|
||||
if (m_isStale) {
|
||||
buildFromFile(m_fileName);
|
||||
}
|
||||
}
|
||||
|
||||
void FileDefinedEngineView::setNetworkFile(const std::string &fileName) {
|
||||
m_isStale = true;
|
||||
m_fileName = fileName;
|
||||
LOG_DEBUG(m_logger, "File '{}' set to '{}'. FileDefinedNetworkView is now stale! You MUST call update() before you use it!", m_fileName, fileName);
|
||||
}
|
||||
|
||||
void FileDefinedEngineView::setScreeningModel(const screening::ScreeningType model) {
|
||||
m_baseEngine.setScreeningModel(model);
|
||||
}
|
||||
|
||||
screening::ScreeningType FileDefinedEngineView::getScreeningModel() const {
|
||||
return m_baseEngine.getScreeningModel();
|
||||
}
|
||||
|
||||
std::vector<size_t> FileDefinedEngineView::constructSpeciesIndexMap() const {
|
||||
LOG_TRACE_L1(m_logger, "Constructing species index map for file defined engine view...");
|
||||
std::unordered_map<Species, size_t> fullSpeciesReverseMap;
|
||||
const auto& fullSpeciesList = m_baseEngine.getNetworkSpecies();
|
||||
|
||||
fullSpeciesReverseMap.reserve(fullSpeciesList.size());
|
||||
|
||||
for (size_t i = 0; i < fullSpeciesList.size(); ++i) {
|
||||
fullSpeciesReverseMap[fullSpeciesList[i]] = i;
|
||||
}
|
||||
|
||||
std::vector<size_t> speciesIndexMap;
|
||||
speciesIndexMap.reserve(m_activeSpecies.size());
|
||||
|
||||
for (const auto& active_species : m_activeSpecies) {
|
||||
auto it = fullSpeciesReverseMap.find(active_species);
|
||||
if (it != fullSpeciesReverseMap.end()) {
|
||||
speciesIndexMap.push_back(it->second);
|
||||
} else {
|
||||
LOG_ERROR(m_logger, "Species '{}' not found in full species map.", active_species.name());
|
||||
m_logger -> flush_log();
|
||||
throw std::runtime_error("Species not found in full species map: " + std::string(active_species.name()));
|
||||
}
|
||||
}
|
||||
LOG_TRACE_L1(m_logger, "Species index map constructed with {} entries.", speciesIndexMap.size());
|
||||
return speciesIndexMap;
|
||||
|
||||
}
|
||||
|
||||
std::vector<size_t> FileDefinedEngineView::constructReactionIndexMap() const {
|
||||
LOG_TRACE_L1(m_logger, "Constructing reaction index map for file defined engine view...");
|
||||
|
||||
// --- Step 1: Create a reverse map using the reaction's unique ID as the key. ---
|
||||
std::unordered_map<std::string_view, size_t> fullReactionReverseMap;
|
||||
const auto& fullReactionSet = m_baseEngine.getNetworkReactions();
|
||||
fullReactionReverseMap.reserve(fullReactionSet.size());
|
||||
|
||||
for (size_t i_full = 0; i_full < fullReactionSet.size(); ++i_full) {
|
||||
fullReactionReverseMap[fullReactionSet[i_full].id()] = i_full;
|
||||
}
|
||||
|
||||
// --- Step 2: Build the final index map using the active reaction set. ---
|
||||
std::vector<size_t> reactionIndexMap;
|
||||
reactionIndexMap.reserve(m_activeReactions.size());
|
||||
|
||||
for (const auto& active_reaction_ptr : m_activeReactions) {
|
||||
auto it = fullReactionReverseMap.find(active_reaction_ptr.id());
|
||||
|
||||
if (it != fullReactionReverseMap.end()) {
|
||||
reactionIndexMap.push_back(it->second);
|
||||
} else {
|
||||
LOG_ERROR(m_logger, "Active reaction '{}' not found in base engine during reaction index map construction.", active_reaction_ptr.id());
|
||||
m_logger->flush_log();
|
||||
throw std::runtime_error("Mismatch between active reactions and base engine.");
|
||||
}
|
||||
}
|
||||
|
||||
LOG_TRACE_L1(m_logger, "Reaction index map constructed with {} entries.", reactionIndexMap.size());
|
||||
return reactionIndexMap;
|
||||
}
|
||||
|
||||
void FileDefinedEngineView::buildFromFile(const std::string &fileName) {
|
||||
LOG_TRACE_L1(m_logger, "Building file defined engine view from {}...", fileName);
|
||||
auto [reactionPENames] = m_parser.parse(fileName);
|
||||
|
||||
m_activeReactions.clear();
|
||||
m_activeSpecies.clear();
|
||||
|
||||
std::unordered_set<Species> seenSpecies;
|
||||
|
||||
const auto& fullNetworkReactionSet = m_baseEngine.getNetworkReactions();
|
||||
for (const auto& peName : reactionPENames) {
|
||||
if (!fullNetworkReactionSet.contains(peName)) {
|
||||
LOG_ERROR(m_logger, "Reaction with name '{}' not found in the base engine's network reactions. Aborting...", peName);
|
||||
m_logger->flush_log();
|
||||
throw std::runtime_error("Reaction with name '" + std::string(peName) + "' not found in the base engine's network reactions.");
|
||||
}
|
||||
auto reaction = fullNetworkReactionSet[peName];
|
||||
for (const auto& reactant : reaction.reactants()) {
|
||||
if (!seenSpecies.contains(reactant)) {
|
||||
seenSpecies.insert(reactant);
|
||||
m_activeSpecies.push_back(reactant);
|
||||
}
|
||||
}
|
||||
for (const auto& product : reaction.products()) {
|
||||
if (!seenSpecies.contains(product)) {
|
||||
seenSpecies.insert(product);
|
||||
m_activeSpecies.push_back(product);
|
||||
}
|
||||
}
|
||||
m_activeReactions.add_reaction(reaction);
|
||||
}
|
||||
LOG_TRACE_L1(m_logger, "File defined engine view built with {} active species and {} active reactions.", m_activeSpecies.size(), m_activeReactions.size());
|
||||
LOG_DEBUG(m_logger, "Active species: {}", [this]() -> std::string {
|
||||
std::string result;
|
||||
for (const auto& species : m_activeSpecies) {
|
||||
result += std::string(species.name()) + ", ";
|
||||
}
|
||||
if (!result.empty()) {
|
||||
result.pop_back(); // Remove last space
|
||||
result.pop_back(); // Remove last comma
|
||||
}
|
||||
return result;
|
||||
}());
|
||||
LOG_DEBUG(m_logger, "Active reactions: {}", [this]() -> std::string {
|
||||
std::string result;
|
||||
for (const auto& reaction : m_activeReactions) {
|
||||
result += std::string(reaction.id()) + ", ";
|
||||
}
|
||||
if (!result.empty()) {
|
||||
result.pop_back(); // Remove last space
|
||||
result.pop_back(); // Remove last comma
|
||||
}
|
||||
return result;
|
||||
}());
|
||||
m_speciesIndexMap = constructSpeciesIndexMap();
|
||||
m_reactionIndexMap = constructReactionIndexMap();
|
||||
m_isStale = false;
|
||||
}
|
||||
|
||||
std::vector<double> FileDefinedEngineView::mapViewToFull(const std::vector<double>& culled) const {
|
||||
std::vector<double> full(m_baseEngine.getNetworkSpecies().size(), 0.0);
|
||||
for (size_t i_culled = 0; i_culled < culled.size(); ++i_culled) {
|
||||
const size_t i_full = m_speciesIndexMap[i_culled];
|
||||
full[i_full] += culled[i_culled];
|
||||
}
|
||||
return full;
|
||||
}
|
||||
|
||||
std::vector<double> FileDefinedEngineView::mapFullToView(const std::vector<double>& full) const {
|
||||
std::vector<double> culled(m_activeSpecies.size(), 0.0);
|
||||
for (size_t i_culled = 0; i_culled < m_activeSpecies.size(); ++i_culled) {
|
||||
const size_t i_full = m_speciesIndexMap[i_culled];
|
||||
culled[i_culled] = full[i_full];
|
||||
}
|
||||
return culled;
|
||||
}
|
||||
|
||||
size_t FileDefinedEngineView::mapViewToFullSpeciesIndex(size_t culledSpeciesIndex) const {
|
||||
if (culledSpeciesIndex < 0 || culledSpeciesIndex >= static_cast<int>(m_speciesIndexMap.size())) {
|
||||
LOG_ERROR(m_logger, "Defined index {} is out of bounds for species index map of size {}.", culledSpeciesIndex, m_speciesIndexMap.size());
|
||||
m_logger->flush_log();
|
||||
throw std::out_of_range("Defined index " + std::to_string(culledSpeciesIndex) + " is out of bounds for species index map of size " + std::to_string(m_speciesIndexMap.size()) + ".");
|
||||
}
|
||||
return m_speciesIndexMap[culledSpeciesIndex];
|
||||
}
|
||||
|
||||
size_t FileDefinedEngineView::mapViewToFullReactionIndex(size_t culledReactionIndex) const {
|
||||
if (culledReactionIndex < 0 || culledReactionIndex >= static_cast<int>(m_reactionIndexMap.size())) {
|
||||
LOG_ERROR(m_logger, "Defined index {} is out of bounds for reaction index map of size {}.", culledReactionIndex, m_reactionIndexMap.size());
|
||||
m_logger->flush_log();
|
||||
throw std::out_of_range("Defined index " + std::to_string(culledReactionIndex) + " is out of bounds for reaction index map of size " + std::to_string(m_reactionIndexMap.size()) + ".");
|
||||
}
|
||||
return m_reactionIndexMap[culledReactionIndex];
|
||||
}
|
||||
|
||||
void FileDefinedEngineView::validateNetworkState() const {
|
||||
if (m_isStale) {
|
||||
LOG_ERROR(m_logger, "File defined engine view is stale. Please call update() with a valid NetIn object.");
|
||||
m_logger->flush_log();
|
||||
throw std::runtime_error("File defined engine view is stale. Please call update() with a valid NetIn object.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,77 @@
|
||||
//
|
||||
// Created by Emily Boudreaux on 6/30/25.
|
||||
//
|
||||
#include "gridfire/io/network_file.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "quill/LogMacros.h"
|
||||
|
||||
namespace gridfire::io {
|
||||
namespace {
|
||||
inline void ltrim(std::string &s) {
|
||||
s.erase(
|
||||
s.begin(),
|
||||
std::ranges::find_if(s,
|
||||
[](const unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
inline void rtrim(std::string &s) {
|
||||
s.erase(
|
||||
std::find_if(
|
||||
s.rbegin(),
|
||||
s.rend(),
|
||||
[](const unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(),
|
||||
s.end()
|
||||
);
|
||||
}
|
||||
|
||||
inline void trim(std::string &s) {
|
||||
ltrim(s);
|
||||
rtrim(s);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
SimpleReactionListFileParser::SimpleReactionListFileParser() {}
|
||||
|
||||
ParsedNetworkData SimpleReactionListFileParser::parse(const std::string& filename) const {
|
||||
LOG_TRACE_L1(m_logger, "Parsing simple reaction list file: {}", filename);
|
||||
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
LOG_ERROR(m_logger, "Failed to open file: {}", filename);
|
||||
m_logger -> flush_log();
|
||||
throw std::runtime_error("Could not open file: " + filename);
|
||||
}
|
||||
|
||||
ParsedNetworkData parsed;
|
||||
std::string line;
|
||||
int line_number = 0;
|
||||
while (std::getline(file, line)) {
|
||||
line_number++;
|
||||
LOG_TRACE_L3(m_logger, "Parsing reaction list file {}, line {}: {}", filename, line_number, line);
|
||||
|
||||
const size_t comment_pos = line.find('#');
|
||||
if (comment_pos != std::string::npos) {
|
||||
line = line.substr(0, comment_pos);
|
||||
}
|
||||
|
||||
trim(line);
|
||||
|
||||
if (line.empty()) {
|
||||
continue; // Skip empty lines
|
||||
}
|
||||
parsed.reactionPENames.push_back(line);
|
||||
}
|
||||
LOG_TRACE_L1(m_logger, "Parsed {} reactions from file: {}", parsed.reactionPENames.size(), filename);
|
||||
return parsed;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -84,7 +84,7 @@ namespace gridfire {
|
||||
}
|
||||
}
|
||||
const ReactionSet reactionSet(reaclibReactions);
|
||||
return LogicalReactionSet(reactionSet);
|
||||
return packReactionSetToLogicalReactionSet(reactionSet);
|
||||
}
|
||||
|
||||
// Trim whitespace from both ends of a string
|
||||
|
||||
@@ -122,10 +122,12 @@ namespace gridfire::reaclib {
|
||||
}
|
||||
|
||||
// The ReactionSet takes the vector of all individual reactions.
|
||||
reaction::ReactionSet reaction_set(std::move(reaction_list));
|
||||
const reaction::ReactionSet reaction_set(std::move(reaction_list));
|
||||
|
||||
// The LogicalReactionSet groups reactions by their peName, which is what we want.
|
||||
s_all_reaclib_reactions_ptr = new reaction::LogicalReactionSet(reaction_set);
|
||||
s_all_reaclib_reactions_ptr = new reaction::LogicalReactionSet(
|
||||
reaction::packReactionSetToLogicalReactionSet(reaction_set)
|
||||
);
|
||||
|
||||
s_initialized = true;
|
||||
}
|
||||
|
||||
@@ -137,152 +137,6 @@ namespace gridfire::reaction {
|
||||
return XXHash64::hash(m_id.data(), m_id.size(), seed);
|
||||
}
|
||||
|
||||
ReactionSet::ReactionSet(
|
||||
std::vector<Reaction> reactions
|
||||
) :
|
||||
m_reactions(std::move(reactions)) {
|
||||
if (m_reactions.empty()) {
|
||||
return; // Case where the reactions will be added later.
|
||||
}
|
||||
m_reactionNameMap.reserve(reactions.size());
|
||||
for (const auto& reaction : m_reactions) {
|
||||
m_id += reaction.id();
|
||||
m_reactionNameMap.emplace(reaction.id(), reaction);
|
||||
}
|
||||
}
|
||||
|
||||
ReactionSet::ReactionSet(const ReactionSet &other) {
|
||||
m_reactions.reserve(other.m_reactions.size());
|
||||
for (const auto& reaction_ptr: other.m_reactions) {
|
||||
m_reactions.push_back(reaction_ptr);
|
||||
}
|
||||
|
||||
m_reactionNameMap.reserve(other.m_reactionNameMap.size());
|
||||
for (const auto& reaction_ptr : m_reactions) {
|
||||
m_reactionNameMap.emplace(reaction_ptr.id(), reaction_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
ReactionSet & ReactionSet::operator=(const ReactionSet &other) {
|
||||
if (this != &other) {
|
||||
ReactionSet temp(other);
|
||||
std::swap(m_reactions, temp.m_reactions);
|
||||
std::swap(m_reactionNameMap, temp.m_reactionNameMap);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void ReactionSet::add_reaction(Reaction reaction) {
|
||||
m_reactions.emplace_back(reaction);
|
||||
m_id += m_reactions.back().id();
|
||||
m_reactionNameMap.emplace(m_reactions.back().id(), m_reactions.back());
|
||||
}
|
||||
|
||||
void ReactionSet::remove_reaction(const Reaction& reaction) {
|
||||
if (!m_reactionNameMap.contains(std::string(reaction.id()))) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_reactionNameMap.erase(std::string(reaction.id()));
|
||||
|
||||
std::erase_if(m_reactions, [&reaction](const Reaction& r) {
|
||||
return r == reaction;
|
||||
});
|
||||
}
|
||||
|
||||
bool ReactionSet::contains(const std::string_view& id) const {
|
||||
for (const auto& reaction : m_reactions) {
|
||||
if (reaction.id() == id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReactionSet::contains(const Reaction& reaction) const {
|
||||
for (const auto& r : m_reactions) {
|
||||
if (r == reaction) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ReactionSet::clear() {
|
||||
m_reactions.clear();
|
||||
m_reactionNameMap.clear();
|
||||
}
|
||||
|
||||
bool ReactionSet::contains_species(const Species& species) const {
|
||||
for (const auto& reaction : m_reactions) {
|
||||
if (reaction.contains(species)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReactionSet::contains_reactant(const Species& species) const {
|
||||
for (const auto& r : m_reactions) {
|
||||
if (r.contains_reactant(species)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReactionSet::contains_product(const Species& species) const {
|
||||
for (const auto& r : m_reactions) {
|
||||
if (r.contains_product(species)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const Reaction& ReactionSet::operator[](const size_t index) const {
|
||||
if (index >= m_reactions.size()) {
|
||||
m_logger -> flush_log();
|
||||
throw std::out_of_range("Index" + std::to_string(index) + " out of range for ReactionSet of size " + std::to_string(m_reactions.size()) + ".");
|
||||
}
|
||||
return m_reactions[index];
|
||||
}
|
||||
|
||||
const Reaction& ReactionSet::operator[](const std::string_view& id) const {
|
||||
if (auto it = m_reactionNameMap.find(std::string(id)); it != m_reactionNameMap.end()) {
|
||||
return it->second;
|
||||
}
|
||||
m_logger -> flush_log();
|
||||
throw std::out_of_range("Species " + std::string(id) + " does not exist in ReactionSet.");
|
||||
}
|
||||
|
||||
bool ReactionSet::operator==(const ReactionSet& other) const {
|
||||
if (size() != other.size()) {
|
||||
return false;
|
||||
}
|
||||
return hash() == other.hash();
|
||||
}
|
||||
|
||||
bool ReactionSet::operator!=(const ReactionSet& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
uint64_t ReactionSet::hash(uint64_t seed) const {
|
||||
if (m_reactions.empty()) {
|
||||
return XXHash64::hash(nullptr, 0, seed);
|
||||
}
|
||||
std::vector<uint64_t> individualReactionHashes;
|
||||
individualReactionHashes.reserve(m_reactions.size());
|
||||
for (const auto& reaction : m_reactions) {
|
||||
individualReactionHashes.push_back(reaction.hash(seed));
|
||||
}
|
||||
|
||||
std::ranges::sort(individualReactionHashes);
|
||||
|
||||
const void* data = static_cast<const void*>(individualReactionHashes.data());
|
||||
size_t sizeInBytes = individualReactionHashes.size() * sizeof(uint64_t);
|
||||
return XXHash64::hash(data, sizeInBytes, seed);
|
||||
}
|
||||
|
||||
|
||||
LogicalReaction::LogicalReaction(const std::vector<Reaction>& reactants) :
|
||||
@@ -344,21 +198,21 @@ namespace gridfire::reaction {
|
||||
return calculate_rate<CppAD::AD<double>>(T9);
|
||||
}
|
||||
|
||||
LogicalReactionSet::LogicalReactionSet(const ReactionSet &reactionSet) :
|
||||
ReactionSet(std::vector<Reaction>()) {
|
||||
LogicalReactionSet packReactionSetToLogicalReactionSet(const ReactionSet& reactionSet) {
|
||||
std::unordered_map<std::string_view, std::vector<Reaction>> groupedReactions;
|
||||
|
||||
std::unordered_map<std::string_view, std::vector<Reaction>> grouped_reactions;
|
||||
for (const auto& reaction: reactionSet) {
|
||||
groupedReactions[reaction.peName()].push_back(reaction);
|
||||
}
|
||||
|
||||
for (const auto& reaction : reactionSet) {
|
||||
grouped_reactions[reaction.peName()].push_back(reaction);
|
||||
}
|
||||
m_reactions.reserve(grouped_reactions.size());
|
||||
m_reactionNameMap.reserve(grouped_reactions.size());
|
||||
for (const auto &reactions_for_peName: grouped_reactions | std::views::values) {
|
||||
LogicalReaction logical_reaction(reactions_for_peName);
|
||||
m_reactionNameMap.emplace(logical_reaction.id(), logical_reaction);
|
||||
m_reactions.push_back(std::move(logical_reaction));
|
||||
std::vector<LogicalReaction> reactions;
|
||||
reactions.reserve(groupedReactions.size());
|
||||
|
||||
for (const auto &reactionsGroup: groupedReactions | std::views::values) {
|
||||
LogicalReaction logicalReaction(reactionsGroup);
|
||||
reactions.push_back(logicalReaction);
|
||||
}
|
||||
return LogicalReactionSet(std::move(reactions));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -376,4 +230,11 @@ namespace std {
|
||||
return s.hash(0);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct hash<gridfire::reaction::LogicalReactionSet> {
|
||||
size_t operator()(const gridfire::reaction::LogicalReactionSet& s) const noexcept {
|
||||
return s.hash(0);
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
31
src/network/lib/screening/screening_bare.cpp
Normal file
31
src/network/lib/screening/screening_bare.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "gridfire/screening/screening_bare.h"
|
||||
|
||||
#include "fourdst/composition/atomicSpecies.h"
|
||||
|
||||
#include "cppad/cppad.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace gridfire::screening {
|
||||
using ADDouble = CppAD::AD<double>;
|
||||
std::vector<ADDouble> BareScreeningModel::calculateScreeningFactors(
|
||||
const reaction::LogicalReactionSet &reactions,
|
||||
const std::vector<fourdst::atomic::Species>& species,
|
||||
const std::vector<ADDouble> &Y,
|
||||
const ADDouble T9,
|
||||
const ADDouble rho
|
||||
) const {
|
||||
return calculateFactors_impl<ADDouble>(reactions, species, Y, T9, rho);
|
||||
}
|
||||
|
||||
std::vector<double> BareScreeningModel::calculateScreeningFactors(
|
||||
const reaction::LogicalReactionSet &reactions,
|
||||
const std::vector<fourdst::atomic::Species>& species,
|
||||
const std::vector<double> &Y,
|
||||
const double T9,
|
||||
const double rho
|
||||
) const {
|
||||
return calculateFactors_impl<double>(reactions, species, Y, T9, rho);
|
||||
}
|
||||
}
|
||||
19
src/network/lib/screening/screening_types.cpp
Normal file
19
src/network/lib/screening/screening_types.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "gridfire/screening/screening_abstract.h"
|
||||
#include "gridfire/screening/screening_types.h"
|
||||
#include "gridfire/screening/screening_weak.h"
|
||||
#include "gridfire/screening/screening_bare.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace gridfire::screening {
|
||||
std::unique_ptr<ScreeningModel> selectScreeningModel(const ScreeningType type) {
|
||||
switch (type) {
|
||||
case ScreeningType::WEAK:
|
||||
return std::make_unique<WeakScreeningModel>();
|
||||
case ScreeningType::BARE:
|
||||
return std::make_unique<BareScreeningModel>();
|
||||
default:
|
||||
return std::make_unique<BareScreeningModel>();
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/network/lib/screening/screening_weak.cpp
Normal file
31
src/network/lib/screening/screening_weak.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "gridfire/screening/screening_weak.h"
|
||||
|
||||
#include "fourdst/composition/atomicSpecies.h"
|
||||
|
||||
#include "cppad/cppad.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace gridfire::screening {
|
||||
using ADDouble = CppAD::AD<double>;
|
||||
std::vector<ADDouble> WeakScreeningModel::calculateScreeningFactors(
|
||||
const reaction::LogicalReactionSet &reactions,
|
||||
const std::vector<fourdst::atomic::Species>& species,
|
||||
const std::vector<ADDouble> &Y,
|
||||
const ADDouble T9,
|
||||
const ADDouble rho
|
||||
) const {
|
||||
return calculateFactors_impl<ADDouble>(reactions, species, Y, T9, rho);
|
||||
}
|
||||
|
||||
std::vector<double> WeakScreeningModel::calculateScreeningFactors(
|
||||
const reaction::LogicalReactionSet &reactions,
|
||||
const std::vector<fourdst::atomic::Species>& species,
|
||||
const std::vector<double> &Y,
|
||||
const double T9,
|
||||
const double rho
|
||||
) const {
|
||||
return calculateFactors_impl<double>(reactions, species, Y, T9, rho);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
#include "gridfire/engine/engine_graph.h"
|
||||
#include "gridfire/network.h"
|
||||
|
||||
#include "gridfire/utils/logging.h"
|
||||
|
||||
#include "fourdst/composition/atomicSpecies.h"
|
||||
#include "fourdst/composition/composition.h"
|
||||
#include "fourdst/config/config.h"
|
||||
@@ -15,6 +17,7 @@
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <iomanip>
|
||||
|
||||
#include "quill/LogMacros.h"
|
||||
|
||||
@@ -59,6 +62,21 @@ namespace gridfire::solver {
|
||||
Eigen::VectorXd Y_QSE;
|
||||
try {
|
||||
Y_QSE = calculateSteadyStateAbundances(Y_sanitized_initial, T9, rho, indices);
|
||||
LOG_DEBUG(m_logger, "QSE Abundances: {}", [*this](const dynamicQSESpeciesIndices& indices, const Eigen::VectorXd& Y_QSE) -> std::string {
|
||||
std::stringstream ss;
|
||||
ss << std::scientific << std::setprecision(5);
|
||||
for (size_t i = 0; i < indices.QSESpeciesIndices.size(); ++i) {
|
||||
ss << std::string(m_engine.getNetworkSpecies()[indices.QSESpeciesIndices[i]].name()) + ": ";
|
||||
ss << Y_QSE(i);
|
||||
if (i < indices.QSESpeciesIndices.size() - 2) {
|
||||
ss << ", ";
|
||||
} else if (i == indices.QSESpeciesIndices.size() - 2) {
|
||||
ss << ", and ";
|
||||
}
|
||||
|
||||
}
|
||||
return ss.str();
|
||||
}(indices, Y_QSE));
|
||||
} catch (const std::runtime_error& e) {
|
||||
LOG_ERROR(m_logger, "Failed to calculate steady state abundances. Aborting QSE evaluation.");
|
||||
m_logger->flush_log();
|
||||
@@ -185,18 +203,62 @@ namespace gridfire::solver {
|
||||
}
|
||||
|
||||
Eigen::VectorXd QSENetworkSolver::calculateSteadyStateAbundances(
|
||||
const std::vector<double> &Y,
|
||||
const double T9,
|
||||
const double rho,
|
||||
const dynamicQSESpeciesIndices &indices
|
||||
const std::vector<double> &Y,
|
||||
const double T9,
|
||||
const double rho,
|
||||
const dynamicQSESpeciesIndices &indices
|
||||
) const {
|
||||
LOG_TRACE_L1(m_logger, "Calculating steady state abundances for QSE species...");
|
||||
LOG_WARNING(m_logger, "QSE solver logic not yet implemented, assuming all QSE species have 0 abundance.");
|
||||
|
||||
// --- Prepare the QSE species vector ---
|
||||
Eigen::VectorXd v_qse(indices.QSESpeciesIndices.size());
|
||||
v_qse.setZero();
|
||||
return v_qse.array();
|
||||
if (indices.QSESpeciesIndices.empty()) {
|
||||
LOG_DEBUG(m_logger, "No QSE species to solve for.");
|
||||
return Eigen::VectorXd(0);
|
||||
}
|
||||
// Use the EigenFunctor with Eigen's nonlinear solver
|
||||
EigenFunctor<double> functor(
|
||||
m_engine,
|
||||
Y,
|
||||
indices.dynamicSpeciesIndices,
|
||||
indices.QSESpeciesIndices,
|
||||
T9,
|
||||
rho
|
||||
);
|
||||
|
||||
Eigen::VectorXd v_qse_log_initial(indices.QSESpeciesIndices.size());
|
||||
for (size_t i = 0; i < indices.QSESpeciesIndices.size(); ++i) {
|
||||
v_qse_log_initial(i) = std::log(std::max(Y[indices.QSESpeciesIndices[i]], 1e-99));
|
||||
}
|
||||
|
||||
const static std::unordered_map<Eigen::LevenbergMarquardtSpace::Status, const char*> statusMessages = {
|
||||
{Eigen::LevenbergMarquardtSpace::NotStarted, "Not started"},
|
||||
{Eigen::LevenbergMarquardtSpace::Running, "Running"},
|
||||
{Eigen::LevenbergMarquardtSpace::ImproperInputParameters, "Improper input parameters"},
|
||||
{Eigen::LevenbergMarquardtSpace::RelativeReductionTooSmall, "Relative reduction too small"},
|
||||
{Eigen::LevenbergMarquardtSpace::RelativeErrorTooSmall, "Relative error too small"},
|
||||
{Eigen::LevenbergMarquardtSpace::RelativeErrorAndReductionTooSmall, "Relative error and reduction too small"},
|
||||
{Eigen::LevenbergMarquardtSpace::CosinusTooSmall, "Cosine too small"},
|
||||
{Eigen::LevenbergMarquardtSpace::TooManyFunctionEvaluation, "Too many function evaluations"},
|
||||
{Eigen::LevenbergMarquardtSpace::FtolTooSmall, "Function tolerance too small"},
|
||||
{Eigen::LevenbergMarquardtSpace::XtolTooSmall, "X tolerance too small"},
|
||||
{Eigen::LevenbergMarquardtSpace::GtolTooSmall, "Gradient tolerance too small"},
|
||||
{Eigen::LevenbergMarquardtSpace::UserAsked, "User asked to stop"}
|
||||
};
|
||||
|
||||
Eigen::LevenbergMarquardt lm(functor);
|
||||
const Eigen::LevenbergMarquardtSpace::Status info = lm.minimize(v_qse_log_initial);
|
||||
|
||||
if (info <= 0 || info >= 4) {
|
||||
LOG_ERROR(m_logger, "QSE species minimization failed with status: {} ({})",
|
||||
static_cast<int>(info), statusMessages.at(info));
|
||||
throw std::runtime_error(
|
||||
"QSE species minimization failed with status: " + std::to_string(static_cast<int>(info)) +
|
||||
" (" + std::string(statusMessages.at(info)) + ")"
|
||||
);
|
||||
}
|
||||
LOG_DEBUG(m_logger, "QSE species minimization completed successfully with status: {} ({})",
|
||||
static_cast<int>(info), statusMessages.at(info));
|
||||
return v_qse_log_initial.array().exp();
|
||||
|
||||
}
|
||||
|
||||
NetOut QSENetworkSolver::initializeNetworkWithShortIgnition(const NetIn &netIn) const {
|
||||
@@ -232,9 +294,14 @@ namespace gridfire::solver {
|
||||
preIgnition.tMax = ignitionTime;
|
||||
preIgnition.dt0 = ignitionStepSize;
|
||||
|
||||
const auto prevScreeningModel = m_engine.getScreeningModel();
|
||||
LOG_DEBUG(m_logger, "Setting screening model to BARE for high temperature and density ignition.");
|
||||
m_engine.setScreeningModel(screening::ScreeningType::BARE);
|
||||
DirectNetworkSolver ignitionSolver(m_engine);
|
||||
NetOut postIgnition = ignitionSolver.evaluate(preIgnition);
|
||||
LOG_INFO(m_logger, "Network ignition completed in {} steps.", postIgnition.num_steps);
|
||||
m_engine.setScreeningModel(prevScreeningModel);
|
||||
LOG_DEBUG(m_logger, "Restoring previous screening model: {}", static_cast<int>(prevScreeningModel));
|
||||
return postIgnition;
|
||||
}
|
||||
|
||||
@@ -360,7 +427,8 @@ namespace gridfire::solver {
|
||||
speciesNames.push_back(std::string(species.name()));
|
||||
}
|
||||
|
||||
Composition outputComposition(speciesNames, finalMassFractions);
|
||||
Composition outputComposition(speciesNames);
|
||||
outputComposition.setMassFraction(speciesNames, finalMassFractions);
|
||||
outputComposition.finalize(true);
|
||||
|
||||
NetOut netOut;
|
||||
@@ -377,6 +445,15 @@ namespace gridfire::solver {
|
||||
double t
|
||||
) const {
|
||||
const std::vector<double> y(Y.begin(), m_numSpecies + Y.begin());
|
||||
|
||||
// std::string timescales = utils::formatNuclearTimescaleLogString(
|
||||
// m_engine,
|
||||
// y,
|
||||
// m_T9,
|
||||
// m_rho
|
||||
// );
|
||||
// LOG_TRACE_L2(m_logger, "{}", timescales);
|
||||
|
||||
auto [dydt, eps] = m_engine.calculateRHSAndEnergy(y, m_T9, m_rho);
|
||||
dYdt.resize(m_numSpecies + 1);
|
||||
std::ranges::copy(dydt, dYdt.begin());
|
||||
|
||||
60
src/network/lib/utils/logging.cpp
Normal file
60
src/network/lib/utils/logging.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "gridfire/utils/logging.h"
|
||||
#include "gridfire/engine/engine_abstract.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
std::string gridfire::utils::formatNuclearTimescaleLogString(
|
||||
const DynamicEngine& engine,
|
||||
std::vector<double> const& Y,
|
||||
const double T9,
|
||||
const double rho
|
||||
) {
|
||||
auto const& timescales = engine.getSpeciesTimescales(Y, T9, rho);
|
||||
|
||||
// Figure out how wide the "Species" column needs to be:
|
||||
std::size_t maxNameLen = std::string_view("Species").size();
|
||||
for (const auto &key: timescales | std::views::keys) {
|
||||
std::string_view name = key.name();
|
||||
maxNameLen = std::max(maxNameLen, name.size());
|
||||
}
|
||||
|
||||
// Pick a fixed width for the timescale column:
|
||||
constexpr int timescaleWidth = 12;
|
||||
|
||||
std::ostringstream ss;
|
||||
ss << "== Timescales (s) ==\n";
|
||||
|
||||
// Header row
|
||||
ss << std::left << std::setw(static_cast<int>(maxNameLen) + 2) << "Species"
|
||||
<< std::right << std::setw(timescaleWidth) << "Timescale (s)" << "\n";
|
||||
|
||||
// Underline
|
||||
ss << std::string(static_cast<int>(maxNameLen) + 2 + timescaleWidth, '=') << "\n";
|
||||
|
||||
ss << std::scientific;
|
||||
|
||||
// Data rows
|
||||
for (auto const& [species, timescale] : timescales) {
|
||||
const std::string_view name = species.name();
|
||||
ss << std::left << std::setw(static_cast<int>(maxNameLen) + 2) << name;
|
||||
|
||||
if (std::isinf(timescale)) {
|
||||
ss << std::right << std::setw(timescaleWidth) << "inf" << "\n";
|
||||
} else {
|
||||
ss << std::right << std::setw(timescaleWidth)
|
||||
<< std::scientific << std::setprecision(3) << timescale << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Footer underline
|
||||
ss << std::string(static_cast<int>(maxNameLen) + 2 + timescaleWidth, '=') << "\n";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
Reference in New Issue
Block a user