From 354ce2e1bbeedabd1a8a43c3f05d7ace20051914 Mon Sep 17 00:00:00 2001 From: rastogi Date: Fri, 17 Oct 2025 15:37:49 +0200 Subject: [PATCH] Modified IO functions --- src/IO/HDF5Functions.hpp | 4 ++-- src/IO/StatsIO.cpp | 12 ++++++++---- src/IO/StatsIO.hpp | 1 + src/IO/checkpoint.cpp | 22 ++++++++++++++++++---- src/poet.cpp | 6 +++--- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/IO/HDF5Functions.hpp b/src/IO/HDF5Functions.hpp index 87687f2b9..2eec6d2b7 100644 --- a/src/IO/HDF5Functions.hpp +++ b/src/IO/HDF5Functions.hpp @@ -3,7 +3,7 @@ #include #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); diff --git a/src/IO/StatsIO.cpp b/src/IO/StatsIO.cpp index 5155ffd1f..8e3c2978c 100644 --- a/src/IO/StatsIO.cpp +++ b/src/IO/StatsIO.cpp @@ -2,15 +2,19 @@ #include #include #include -#include // for std::setw and std::setprecision +#include +#include namespace poet { void writeStatsToCSV(const std::vector &all_stats, const std::vector &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(); diff --git a/src/IO/StatsIO.hpp b/src/IO/StatsIO.hpp index a7fd1c606..cb432f939 100644 --- a/src/IO/StatsIO.hpp +++ b/src/IO/StatsIO.hpp @@ -5,5 +5,6 @@ namespace poet { void writeStatsToCSV(const std::vector &all_stats, const std::vector &species_names, + const std::string &out_dir, const std::string &filename); } // namespace poet diff --git a/src/IO/checkpoint.cpp b/src/IO/checkpoint.cpp index f197c22f5..e053700a0 100644 --- a/src/IO/checkpoint.cpp +++ b/src/IO/checkpoint.cpp @@ -1,13 +1,20 @@ #include "IO/Datatypes.hpp" #include #include +#include -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(file, "/MetaParam/Iterations"); diff --git a/src/poet.cpp b/src/poet.cpp index b40eeedac..4b920aa02 100644 --- a/src/poet.cpp +++ b/src/poet.cpp @@ -330,7 +330,7 @@ bool triggerRollbackIfExceeded(ChemistryModule &chem, RuntimeParameters ¶ms, " → 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 ¶ms, 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;