2025-07-10 09:36:05 -04:00
|
|
|
#include "gridfire/engine/procedures/priming.h"
|
2025-11-10 10:40:03 -05:00
|
|
|
|
|
|
|
|
#include "fourdst/atomic/species.h"
|
|
|
|
|
#include "fourdst/composition/utils.h"
|
2025-07-10 09:36:05 -04:00
|
|
|
#include "gridfire/engine/views/engine_priming.h"
|
|
|
|
|
#include "gridfire/solver/solver.h"
|
|
|
|
|
|
|
|
|
|
#include "gridfire/engine/engine_abstract.h"
|
2025-11-24 09:07:49 -05:00
|
|
|
#include "gridfire/types/types.h"
|
2025-11-14 10:55:03 -05:00
|
|
|
#include "gridfire/exceptions/error_solver.h"
|
2025-07-10 09:36:05 -04:00
|
|
|
|
2025-07-14 14:50:49 -04:00
|
|
|
#include "fourdst/logging/logging.h"
|
2025-11-14 10:55:03 -05:00
|
|
|
#include "gridfire/solver/strategies/CVODE_solver_strategy.h"
|
2025-07-14 14:50:49 -04:00
|
|
|
#include "quill/Logger.h"
|
|
|
|
|
#include "quill/LogMacros.h"
|
|
|
|
|
|
2025-10-14 13:37:48 -04:00
|
|
|
|
2025-11-24 09:07:49 -05:00
|
|
|
namespace gridfire::engine {
|
2025-07-14 14:50:49 -04:00
|
|
|
using fourdst::composition::Composition;
|
|
|
|
|
using fourdst::atomic::Species;
|
|
|
|
|
|
2025-10-14 13:37:48 -04:00
|
|
|
PrimingReport primeNetwork(
|
2025-11-14 10:55:03 -05:00
|
|
|
const NetIn& netIn,
|
|
|
|
|
GraphEngine& engine,
|
|
|
|
|
const std::optional<std::vector<reaction::ReactionType>>& ignoredReactionTypes
|
2025-10-14 13:37:48 -04:00
|
|
|
) {
|
2025-11-14 10:55:03 -05:00
|
|
|
const auto logger = LogManager::getInstance().getLogger("log");
|
|
|
|
|
solver::CVODESolverStrategy integrator(engine);
|
2025-11-24 09:07:49 -05:00
|
|
|
|
|
|
|
|
// Do not need high precision for priming
|
|
|
|
|
integrator.set_absTol(1e-3);
|
|
|
|
|
integrator.set_relTol(1e-3);
|
|
|
|
|
|
2025-11-14 10:55:03 -05:00
|
|
|
integrator.set_stdout_logging_enabled(false);
|
|
|
|
|
NetIn solverInput(netIn);
|
2025-10-14 13:37:48 -04:00
|
|
|
|
2025-11-14 10:55:03 -05:00
|
|
|
solverInput.tMax = 1e-15;
|
|
|
|
|
solverInput.temperature = 1e7;
|
2025-07-10 09:36:05 -04:00
|
|
|
|
2025-11-14 10:55:03 -05:00
|
|
|
LOG_INFO(logger, "Short timescale ({}) network ignition started.", solverInput.tMax);
|
2025-07-14 14:50:49 -04:00
|
|
|
PrimingReport report;
|
2025-11-14 10:55:03 -05:00
|
|
|
try {
|
|
|
|
|
const NetOut netOut = integrator.evaluate(solverInput, false);
|
|
|
|
|
LOG_INFO(logger, "Network ignition completed.");
|
|
|
|
|
LOG_TRACE_L2(
|
|
|
|
|
logger,
|
|
|
|
|
"After ignition composition is {}",
|
|
|
|
|
[netOut, netIn]() -> std::string {
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
for (const auto& [species, abundance] : netOut.composition) {
|
|
|
|
|
ss << species.name() << ": " << abundance << " (prior: " << netIn.composition.getMolarAbundance(species);
|
|
|
|
|
ss << ", fractional change: " << (abundance - netIn.composition.getMolarAbundance(species)) / netIn.composition.getMolarAbundance(species) * 100.0 << "%)";
|
|
|
|
|
if (i < netOut.composition.size() - 1) {
|
|
|
|
|
ss << ", ";
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return ss.str();
|
|
|
|
|
}()
|
2025-10-14 13:37:48 -04:00
|
|
|
);
|
2025-11-14 10:55:03 -05:00
|
|
|
report.primedComposition = netOut.composition;
|
|
|
|
|
std::unordered_set<Species> unprimedSpecies;
|
|
|
|
|
double minAbundance = std::numeric_limits<double>::max();
|
|
|
|
|
for (const auto& [sp, y] : report.primedComposition) {
|
|
|
|
|
if (y == 0) {
|
|
|
|
|
unprimedSpecies.insert(sp);
|
2025-11-10 10:40:03 -05:00
|
|
|
}
|
2025-11-14 10:55:03 -05:00
|
|
|
if (y != 0 && y < minAbundance) {
|
|
|
|
|
minAbundance = y;
|
2025-09-19 15:14:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-14 10:55:03 -05:00
|
|
|
double abundanceForUnprimedSpecies = minAbundance / 1e10;
|
|
|
|
|
for (const auto& sp : unprimedSpecies) {
|
|
|
|
|
LOG_TRACE_L1(logger, "Clamping Species {}: initial abundance {}, primed abundance {} to {}", sp.name(), netIn.composition.getMolarAbundance(sp), report.primedComposition.getMolarAbundance(sp), abundanceForUnprimedSpecies);
|
|
|
|
|
report.primedComposition.setMolarAbundance(sp, abundanceForUnprimedSpecies);
|
2025-07-14 14:50:49 -04:00
|
|
|
}
|
2025-11-14 10:55:03 -05:00
|
|
|
report.success = true;
|
|
|
|
|
report.status = PrimingReportStatus::SUCCESS;
|
|
|
|
|
} catch (const exceptions::SolverError& e) {
|
|
|
|
|
LOG_ERROR(logger, "Failed to prime network: solver failure encountered: {}", e.what());
|
|
|
|
|
std::rethrow_exception(std::current_exception());
|
2025-07-14 14:50:49 -04:00
|
|
|
}
|
|
|
|
|
return report;
|
2025-07-10 09:36:05 -04:00
|
|
|
}
|
|
|
|
|
}
|