refactor(reaction): refactored to an abstract reaction class in prep for weak reactions

This commit is contained in:
2025-08-14 13:33:46 -04:00
parent d920a55ba6
commit 0b77f2e269
81 changed files with 1050041 additions and 913 deletions

View File

@@ -14,14 +14,13 @@
#include "quill/LogMacros.h"
namespace gridfire {
using reaction::LogicalReactionSet;
using reaction::ReactionSet;
using reaction::Reaction;
using fourdst::composition::Composition;
using fourdst::atomic::Species;
LogicalReactionSet build_reaclib_nuclear_network(
ReactionSet build_reaclib_nuclear_network(
const Composition &composition,
BuildDepthType maxLayers,
bool reverse
@@ -38,18 +37,18 @@ namespace gridfire {
throw std::logic_error("Network build depth is set to 0. No reactions will be collected.");
}
const auto allReactions = reaclib::get_all_reactions();
std::vector<Reaction> remainingReactions;
const auto& allReactions = reaclib::get_all_reaclib_reactions();
std::vector<Reaction*> remainingReactions;
for (const auto& reaction : allReactions) {
if (reaction.is_reverse() == reverse) {
remainingReactions.push_back(reaction);
if (reaction->is_reverse() == reverse) {
remainingReactions.push_back(reaction.get());
}
}
if (depth == static_cast<int>(NetworkBuildDepth::Full)) {
LOG_INFO(logger, "Building full nuclear network with a total of {} reactions.", allReactions.size());
const ReactionSet reactionSet(remainingReactions);
return reaction::packReactionSetToLogicalReactionSet(reactionSet);
return reaction::packReactionSet(reactionSet);
}
std::unordered_set<Species> availableSpecies;
@@ -60,12 +59,12 @@ namespace gridfire {
}
std::vector<Reaction> collectedReactions;
std::vector<Reaction*> collectedReactions;
LOG_INFO(logger, "Starting network construction with {} available species.", availableSpecies.size());
for (int layer = 0; layer < depth && !remainingReactions.empty(); ++layer) {
LOG_TRACE_L1(logger, "Collecting reactions for layer {} with {} remaining reactions. Currently there are {} available species", layer, remainingReactions.size(), availableSpecies.size());
std::vector<Reaction> reactionsForNextPass;
std::vector<Reaction*> reactionsForNextPass;
std::unordered_set<Species> newProductsThisLayer;
bool newReactionsAdded = false;
@@ -73,7 +72,7 @@ namespace gridfire {
for (const auto &reaction : remainingReactions) {
bool allReactantsAvailable = true;
for (const auto& reactant : reaction.reactants()) {
for (const auto& reactant : reaction->reactants()) {
if (!availableSpecies.contains(reactant)) {
allReactantsAvailable = false;
break;
@@ -84,7 +83,7 @@ namespace gridfire {
collectedReactions.push_back(reaction);
newReactionsAdded = true;
for (const auto& product : reaction.products()) {
for (const auto& product : reaction->products()) {
newProductsThisLayer.insert(product);
}
} else {
@@ -105,9 +104,7 @@ namespace gridfire {
LOG_INFO(logger, "Network construction completed with {} reactions collected.", collectedReactions.size());
const ReactionSet reactionSet(collectedReactions);
return reaction::packReactionSetToLogicalReactionSet(reactionSet);
return reaction::packReactionSet(reactionSet);
}
}

View File

@@ -24,11 +24,11 @@ namespace gridfire {
const reaction::Reaction* dominateReaction = nullptr;
double maxFlow = -1.0;
for (const auto& reaction : engine.getNetworkReactions()) {
if (reaction.contains(species) && reaction.stoichiometry(species) > 0) {
const double flow = engine.calculateMolarReactionFlow(reaction, Y, T9, rho);
if (reaction->contains(species) && reaction->stoichiometry(species) > 0) {
const double flow = engine.calculateMolarReactionFlow(*reaction, Y, T9, rho);
if (flow > maxFlow) {
maxFlow = flow;
dominateReaction = &reaction;
dominateReaction = reaction.get();
}
}
}
@@ -105,9 +105,8 @@ namespace gridfire {
const auto Y = primer.mapNetInToMolarAbundanceVector(tempNetIn);
const double destructionRateConstant = calculateDestructionRateConstant(primer, primingSpecies, Y, T9, rho);
double equilibriumMassFraction = 0.0;
if (destructionRateConstant > 1e-99) {
double equilibriumMassFraction = 0.0;
const double creationRate = calculateCreationRate(primer, primingSpecies, Y, T9, rho);
equilibriumMassFraction = (creationRate / destructionRateConstant) * primingSpecies.mass();
if (std::isnan(equilibriumMassFraction)) {
@@ -116,9 +115,7 @@ namespace gridfire {
}
LOG_TRACE_L3(logger, "Found equilibrium for {}: X_eq = {:.4e}", primingSpecies.name(), equilibriumMassFraction);
const reaction::Reaction* dominantChannel = findDominantCreationChannel(primer, primingSpecies, Y, T9, rho);
if (dominantChannel) {
if (const reaction::Reaction* dominantChannel = findDominantCreationChannel(primer, primingSpecies, Y, T9, rho)) {
LOG_TRACE_L3(logger, "Dominant creation channel for {}: {}", primingSpecies.name(), dominantChannel->peName());
double totalReactantMass = 0.0;
@@ -170,7 +167,7 @@ namespace gridfire {
std::vector<std::string> final_symbols;
std::vector<double> final_mass_fractions;
for(const auto& [species, mass_fraction] : currentMassFractions) {
final_symbols.push_back(std::string(species.name()));
final_symbols.emplace_back(species.name());
if (mass_fraction < 0.0 && std::abs(mass_fraction) < 1e-16) {
final_mass_fractions.push_back(0.0); // Avoid negative mass fractions
} else {
@@ -197,14 +194,14 @@ namespace gridfire {
const double T9,
const double rho
) {
const int speciesIndex = engine.getSpeciesIndex(species);
const size_t speciesIndex = engine.getSpeciesIndex(species);
std::vector<double> Y_scaled(Y.begin(), Y.end());
Y_scaled[speciesIndex] = 1.0; // Set the abundance of the species to 1.0 for rate constant calculation
double destructionRateConstant = 0.0;
for (const auto& reaction: engine.getNetworkReactions()) {
if (reaction.contains_reactant(species)) {
const int stoichiometry = reaction.stoichiometry(species);
destructionRateConstant += std::abs(stoichiometry) * engine.calculateMolarReactionFlow(reaction, Y_scaled, T9, rho);
if (reaction->contains_reactant(species)) {
const int stoichiometry = reaction->stoichiometry(species);
destructionRateConstant += std::abs(stoichiometry) * engine.calculateMolarReactionFlow(*reaction, Y_scaled, T9, rho);
}
}
return destructionRateConstant;
@@ -219,9 +216,9 @@ namespace gridfire {
) {
double creationRate = 0.0;
for (const auto& reaction: engine.getNetworkReactions()) {
const int stoichiometry = reaction.stoichiometry(species);
const int stoichiometry = reaction->stoichiometry(species);
if (stoichiometry > 0) {
creationRate += stoichiometry * engine.calculateMolarReactionFlow(reaction, Y, T9, rho);
creationRate += stoichiometry * engine.calculateMolarReactionFlow(*reaction, Y, T9, rho);
}
}
return creationRate;