2025-06-26 15:13:46 -04:00
|
|
|
|
#include <iostream>
|
2025-06-29 14:54:25 -04:00
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
|
|
|
#include "gridfire/engine/engine_graph.h"
|
2025-07-01 11:41:14 -04:00
|
|
|
|
#include "gridfire/engine/views/engine_adaptive.h"
|
2025-08-15 12:11:32 -04:00
|
|
|
|
#include "gridfire/solver/strategies/CVODE_solver_strategy.h"
|
2025-11-10 10:40:03 -05:00
|
|
|
|
#include "gridfire/policy/stellar_policy.h"
|
|
|
|
|
|
#include "gridfire/utils/table_format.h"
|
2025-06-29 14:54:25 -04:00
|
|
|
|
|
2025-06-26 15:13:46 -04:00
|
|
|
|
#include "gridfire/network.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "fourdst/composition/composition.h"
|
|
|
|
|
|
|
2025-08-14 13:33:46 -04:00
|
|
|
|
#include "fourdst/plugin/bundle/bundle.h"
|
|
|
|
|
|
|
2025-06-29 14:54:25 -04:00
|
|
|
|
#include "fourdst/logging/logging.h"
|
|
|
|
|
|
#include "quill/Logger.h"
|
|
|
|
|
|
#include "quill/LogMacros.h"
|
|
|
|
|
|
#include "quill/Backend.h"
|
|
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
#include <clocale>
|
2025-07-01 14:30:45 -04:00
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
#include "fourdst/atomic/species.h"
|
|
|
|
|
|
#include "fourdst/composition/utils.h"
|
2025-07-02 11:32:45 -04:00
|
|
|
|
|
2025-06-29 14:54:25 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
static std::terminate_handler g_previousHandler = nullptr;
|
2025-07-01 14:30:45 -04:00
|
|
|
|
|
2025-06-29 14:54:25 -04:00
|
|
|
|
void quill_terminate_handler()
|
|
|
|
|
|
{
|
|
|
|
|
|
quill::Backend::stop();
|
|
|
|
|
|
if (g_previousHandler)
|
|
|
|
|
|
g_previousHandler();
|
|
|
|
|
|
else
|
|
|
|
|
|
std::abort();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
gridfire::NetIn init() {
|
|
|
|
|
|
std::setlocale(LC_ALL, "");
|
2025-06-29 14:54:25 -04:00
|
|
|
|
g_previousHandler = std::set_terminate(quill_terminate_handler);
|
|
|
|
|
|
quill::Logger* logger = fourdst::logging::LogManager::getInstance().getLogger("log");
|
2025-11-10 10:40:03 -05:00
|
|
|
|
logger->set_log_level(quill::LogLevel::TraceL1);
|
2025-06-29 14:54:25 -04:00
|
|
|
|
|
2025-06-26 15:13:46 -04:00
|
|
|
|
using namespace gridfire;
|
2025-11-10 10:40:03 -05:00
|
|
|
|
const std::vector<double> X = {0.7081145999999999, 2.94e-5, 0.276, 0.003, 0.0011, 9.62e-3, 1.62e-3, 5.16e-4};
|
2025-07-14 14:54:22 -04:00
|
|
|
|
const std::vector<std::string> symbols = {"H-1", "He-3", "He-4", "C-12", "N-14", "O-16", "Ne-20", "Mg-24"};
|
2025-06-26 15:13:46 -04:00
|
|
|
|
|
2025-07-10 09:36:05 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
fourdst::composition::Composition composition = fourdst::composition::buildCompositionFromMassFractions(symbols, X);
|
2025-06-26 15:13:46 -04:00
|
|
|
|
|
|
|
|
|
|
NetIn netIn;
|
|
|
|
|
|
netIn.composition = composition;
|
2025-07-31 15:04:57 -04:00
|
|
|
|
netIn.temperature = 1.5e7;
|
|
|
|
|
|
netIn.density = 1.6e2;
|
2025-07-14 14:54:22 -04:00
|
|
|
|
netIn.energy = 0;
|
2025-10-30 15:06:59 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
netIn.tMax = 3e16;
|
2025-07-14 14:54:22 -04:00
|
|
|
|
netIn.dt0 = 1e-12;
|
2025-07-16 12:14:02 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
return netIn;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void log_results(const gridfire::NetOut& netOut, const gridfire::NetIn& netIn) {
|
|
|
|
|
|
double initialH1_X = netIn.composition.getMassFraction("H-1");
|
|
|
|
|
|
double finalH1_X = netOut.composition.getMassFraction("H-1");
|
|
|
|
|
|
double deltaH1_X = finalH1_X - initialH1_X;
|
|
|
|
|
|
double fractionalChangeH1 = (deltaH1_X) / initialH1_X * 100.0;
|
|
|
|
|
|
|
|
|
|
|
|
double initialHe4_X = netIn.composition.getMassFraction("He-4");
|
|
|
|
|
|
double finalHe4_X = netOut.composition.getMassFraction("He-4");
|
|
|
|
|
|
double deltaHe4_X = finalHe4_X - initialHe4_X;
|
|
|
|
|
|
double fractionalChangeHe4 = (deltaHe4_X) / initialHe4_X * 100.0;
|
|
|
|
|
|
|
|
|
|
|
|
double initialBe7_X = 0.0;
|
|
|
|
|
|
double finalBe7_X = netOut.composition.getMassFraction("Be-7");
|
|
|
|
|
|
double deltaBe7_X = finalBe7_X - initialBe7_X;
|
|
|
|
|
|
double fractionalChangeBe7 = (deltaBe7_X) / initialBe7_X * 100.0;
|
|
|
|
|
|
|
|
|
|
|
|
double initialC12_X = netIn.composition.getMassFraction("C-12");
|
|
|
|
|
|
double finalC12_X = netOut.composition.getMassFraction("C-12");
|
|
|
|
|
|
double deltaC12_X = finalC12_X - initialC12_X;
|
|
|
|
|
|
double fractionalChangeC12 = (deltaC12_X) / initialC12_X * 100.0;
|
|
|
|
|
|
|
|
|
|
|
|
double finalEnergy = netOut.energy;
|
|
|
|
|
|
double dEpsdT = netOut.dEps_dT;
|
|
|
|
|
|
double dEpsdRho = netOut.dEps_dRho;
|
|
|
|
|
|
|
|
|
|
|
|
double initialMeanMolecularMass = netIn.composition.getMeanParticleMass();
|
|
|
|
|
|
double finalMeanMolecularMass = netOut.composition.getMeanParticleMass();
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> rowLabels = {"H-1", "He-4", "Be-7", "C-12", "ε", "dε/dT", "dε/dρ", "<μ>"};
|
|
|
|
|
|
std::vector<std::string> header = {"Parameter", "Initial", "Final", "Δ", "% Change"};
|
|
|
|
|
|
std::vector<double> H1Data = {initialH1_X, finalH1_X, deltaH1_X, fractionalChangeH1};
|
|
|
|
|
|
std::vector<double> He4Data = {initialHe4_X, finalHe4_X, deltaHe4_X, fractionalChangeHe4};
|
|
|
|
|
|
std::vector<double> Be7Data = {initialBe7_X, finalBe7_X, deltaBe7_X, fractionalChangeBe7};
|
|
|
|
|
|
std::vector<double> C12Data = {initialC12_X, finalC12_X, deltaC12_X, fractionalChangeC12};
|
|
|
|
|
|
std::vector<double> energyData = {0.0, finalEnergy, finalEnergy, 0.0};
|
|
|
|
|
|
std::vector<double> dEpsdTData = {0.0, dEpsdT, dEpsdT, 0.0};
|
|
|
|
|
|
std::vector<double> dEpsdRhoData = {0.0, dEpsdRho, dEpsdRho, 0.0};
|
|
|
|
|
|
std::vector<double> meanMolecularMassData = {initialMeanMolecularMass, finalMeanMolecularMass, finalMeanMolecularMass - initialMeanMolecularMass,
|
|
|
|
|
|
(finalMeanMolecularMass - initialMeanMolecularMass) / initialMeanMolecularMass * 100.0};
|
|
|
|
|
|
|
|
|
|
|
|
gridfire::utils::Column<std::string> paramCol(header[0], rowLabels);
|
|
|
|
|
|
gridfire::utils::Column<double> initialCol(header[1], {H1Data[0], He4Data[0], Be7Data[0], C12Data[0], energyData[0], dEpsdTData[0], dEpsdRhoData[0], meanMolecularMassData[0]});
|
|
|
|
|
|
gridfire::utils::Column<double> finalCol (header[2], {H1Data[1], He4Data[1], Be7Data[1], C12Data[1], energyData[1], dEpsdTData[1], dEpsdRhoData[1], meanMolecularMassData[1]});
|
|
|
|
|
|
gridfire::utils::Column<double> deltaCol (header[3], {H1Data[2], He4Data[2], Be7Data[2], C12Data[2], energyData[2], dEpsdTData[2], dEpsdRhoData[2], meanMolecularMassData[2]});
|
|
|
|
|
|
gridfire::utils::Column<double> percentCol(header[4], {H1Data[3], He4Data[3], Be7Data[3], C12Data[3], energyData[3], dEpsdTData[3], dEpsdRhoData[3], meanMolecularMassData[3]});
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<gridfire::utils::ColumnBase>> columns;
|
|
|
|
|
|
columns.push_back(std::make_unique<gridfire::utils::Column<std::string>>(paramCol));
|
|
|
|
|
|
columns.push_back(std::make_unique<gridfire::utils::Column<double>>(initialCol));
|
|
|
|
|
|
columns.push_back(std::make_unique<gridfire::utils::Column<double>>(finalCol));
|
|
|
|
|
|
columns.push_back(std::make_unique<gridfire::utils::Column<double>>(deltaCol));
|
|
|
|
|
|
columns.push_back(std::make_unique<gridfire::utils::Column<double>>(percentCol));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gridfire::utils::print_table("Simulation Results", columns);
|
|
|
|
|
|
}
|
2025-10-30 15:06:59 -04:00
|
|
|
|
|
2025-09-19 15:14:46 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
static std::vector<std::pair<double, std::unordered_map<std::string, std::pair<double, double>>>> g_callbackHistory;
|
|
|
|
|
|
void record_abundance_history_callback(const gridfire::solver::CVODESolverStrategy::TimestepContext& ctx) {
|
|
|
|
|
|
const auto& engine = ctx.engine;
|
|
|
|
|
|
std::unordered_map<std::string, std::pair<double, double>> abundances;
|
|
|
|
|
|
for (const auto& species : engine.getNetworkSpecies()) {
|
|
|
|
|
|
const size_t sid = engine.getSpeciesIndex(species);
|
|
|
|
|
|
abundances[std::string(species.name())] = std::make_pair(species.mass(), N_VGetArrayPointer(ctx.state)[sid]);
|
|
|
|
|
|
}
|
|
|
|
|
|
g_callbackHistory.emplace_back(ctx.t, abundances);
|
|
|
|
|
|
}
|
2025-10-30 15:06:59 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
void save_callback_data(const std::string_view filename) {
|
|
|
|
|
|
std::set<std::string> unique_species;
|
|
|
|
|
|
for (const auto &abundances: g_callbackHistory | std::views::values) {
|
|
|
|
|
|
for (const auto &species_name: abundances | std::views::keys) {
|
|
|
|
|
|
unique_species.insert(species_name);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
std::ofstream csvFile(filename.data(), std::ios::out);
|
|
|
|
|
|
csvFile << "t,";
|
|
|
|
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
for (const auto& species_name : unique_species) {
|
|
|
|
|
|
csvFile << species_name;
|
|
|
|
|
|
if (i < unique_species.size() - 1) {
|
|
|
|
|
|
csvFile << ",";
|
|
|
|
|
|
}
|
|
|
|
|
|
i++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
csvFile << "\n";
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto& [time, data] : g_callbackHistory) {
|
|
|
|
|
|
csvFile << time << ",";
|
|
|
|
|
|
size_t j = 0;
|
|
|
|
|
|
for (const auto& species_name : unique_species) {
|
|
|
|
|
|
if (!data.contains(species_name)) {
|
|
|
|
|
|
csvFile << "0.0";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
csvFile << data.at(species_name).second;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (j < unique_species.size() - 1) {
|
|
|
|
|
|
csvFile << ",";
|
|
|
|
|
|
}
|
|
|
|
|
|
++j;
|
|
|
|
|
|
}
|
|
|
|
|
|
csvFile << "\n";
|
|
|
|
|
|
}
|
2025-08-15 12:11:32 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
csvFile.close();
|
|
|
|
|
|
}
|
2025-07-31 15:04:57 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
int main() {
|
|
|
|
|
|
using namespace gridfire;
|
|
|
|
|
|
const NetIn netIn = init();
|
2025-07-31 15:04:57 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
policy::MainSequencePolicy stellarPolicy(netIn.composition);
|
|
|
|
|
|
DynamicEngine& engine = stellarPolicy.construct();
|
2025-10-30 15:06:59 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
solver::CVODESolverStrategy solver(engine);
|
|
|
|
|
|
solver.set_callback(solver::CVODESolverStrategy::TimestepCallback(record_abundance_history_callback));
|
2025-10-30 15:06:59 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
const NetOut netOut = solver.evaluate(netIn, false);
|
2025-07-22 12:48:24 -04:00
|
|
|
|
|
2025-11-10 10:40:03 -05:00
|
|
|
|
log_results(netOut, netIn);
|
|
|
|
|
|
save_callback_data("abundance_history.csv");
|
2025-06-26 15:13:46 -04:00
|
|
|
|
}
|