Modified IO functions

This commit is contained in:
rastogi 2025-10-17 15:37:49 +02:00
parent dcdf510125
commit be07217aac
7 changed files with 32 additions and 13 deletions

Binary file not shown.

View File

@ -3,7 +3,7 @@
#include <string>
#include "Datatypes.hpp"
int write_checkpoint(const std::string &file_path, struct Checkpoint_s &&checkpoint);
int read_checkpoint(const std::string &file_path, struct Checkpoint_s &checkpoint);
int write_checkpoint(const std::string &dir_path, const std::string &file_name, struct Checkpoint_s &&checkpoint);
int read_checkpoint(const std::string &dir_path, const std::string &file_name, struct Checkpoint_s &checkpoint);

View File

@ -2,15 +2,19 @@
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip> // for std::setw and std::setprecision
#include <iomanip>
#include <filesystem>
namespace poet
{
void writeStatsToCSV(const std::vector<ChemistryModule::SimulationErrorStats> &all_stats,
const std::vector<std::string> &species_names,
const std::string &out_dir,
const std::string &filename)
{
std::ofstream out(filename);
std::filesystem::path full_path = std::filesystem::path(out_dir) / filename;
std::ofstream out(full_path);
if (!out.is_open())
{
std::cerr << "Could not open " << filename << " !" << std::endl;
@ -24,7 +28,7 @@ namespace poet
<< std::setw(15) << "MAPE"
<< std::setw(15) << "RRSME" << "\n";
out << std::string(75, '-') << "\n"; // separator line
out << std::string(75, '-') << "\n";
// data rows
for (size_t i = 0; i < all_stats.size(); ++i)
@ -39,7 +43,7 @@ namespace poet
<< std::setw(15) << all_stats[i].rrmse[j]
<< "\n";
}
out << "\n"; // blank line between iterations
out << "\n";
}
out.close();

View File

@ -5,5 +5,6 @@ namespace poet
{
void writeStatsToCSV(const std::vector<ChemistryModule::SimulationErrorStats> &all_stats,
const std::vector<std::string> &species_names,
const std::string &out_dir,
const std::string &filename);
} // namespace poet

View File

@ -1,13 +1,20 @@
#include "IO/Datatypes.hpp"
#include <cstdint>
#include <highfive/H5Easy.hpp>
#include <filesystem>
int write_checkpoint(const std::string &file_path, struct Checkpoint_s &&checkpoint){
namespace fs = std::filesystem;
int write_checkpoint(const std::string &dir_path, const std::string &file_name, struct Checkpoint_s &&checkpoint){
if (!fs::exists(dir_path)) {
std::cerr << "Directory does not exist: " << dir_path << std::endl;
return -1;
}
fs::path file_path = fs::path(dir_path) / file_name;
// TODO: errorhandling
H5Easy::File file(file_path, H5Easy::File::Overwrite);
H5Easy::dump(file, "/MetaParam/Iterations", checkpoint.iteration);
H5Easy::dump(file, "/Grid/Names", checkpoint.field.GetProps());
H5Easy::dump(file, "/Grid/Chemistry", checkpoint.field.As2DVector());
@ -15,9 +22,16 @@ int write_checkpoint(const std::string &file_path, struct Checkpoint_s &&checkpo
return 0;
}
int read_checkpoint(const std::string &file_path, struct Checkpoint_s &checkpoint){
int read_checkpoint(const std::string &dir_path, const std::string &file_name, struct Checkpoint_s &checkpoint){
fs::path file_path = fs::path(dir_path) / file_name;
H5Easy::File file(file_path, H5Easy::File::ReadOnly);
if (!fs::exists(file_path)) {
std::cerr << "File does not exist: " << file_path << std::endl;
return -1;
}
H5Easy::File file(file_path, H5Easy::File::ReadOnly);
checkpoint.iteration = H5Easy::load<uint32_t>(file, "/MetaParam/Iterations");

View File

@ -330,7 +330,7 @@ bool triggerRollbackIfExceeded(ChemistryModule &chem, RuntimeParameters &params,
" → rolling back to iteration " + std::to_string(rollback_iter));
Checkpoint_s checkpoint_read{.field = chem.getField()};
read_checkpoint("checkpoint" + std::to_string(rollback_iter) + ".hdf5", checkpoint_read);
read_checkpoint(params.out_dir, "checkpoint" + std::to_string(rollback_iter) + ".hdf5", checkpoint_read);
current_iteration = checkpoint_read.iteration;
return true; // rollback happened
}
@ -490,13 +490,13 @@ static Rcpp::List RunMasterLoop(RInsidePOET &R, RuntimeParameters &params,
if(iter % params.checkpoint_interval == 0){
MSG("Writing checkpoint of iteration " + std::to_string(iter));
write_checkpoint("checkpoint" + std::to_string(iter) + ".hdf5",
write_checkpoint(params.out_dir, "checkpoint" + std::to_string(iter) + ".hdf5",
{.field = chem.getField(), .iteration = iter});
}
if (params.control_interval_enabled && !params.rollback_enabled)
{
writeStatsToCSV(chem.error_history, chem.getField().GetProps(), "stats_overview");
writeStatsToCSV(chem.error_history, chem.getField().GetProps(), params.out_dir,"stats_overview");
if(triggerRollbackIfExceeded(chem, params, iter)){
params.rollback_enabled = true;