mirror of
https://git.gfz-potsdam.de/naaice/poet.git
synced 2025-12-16 12:54:50 +01:00
Modified IO functions
This commit is contained in:
parent
dcdf510125
commit
be07217aac
Binary file not shown.
Binary file not shown.
@ -3,7 +3,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "Datatypes.hpp"
|
#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);
|
||||||
|
|||||||
@ -2,15 +2,19 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iomanip> // for std::setw and std::setprecision
|
#include <iomanip>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
namespace poet
|
namespace poet
|
||||||
{
|
{
|
||||||
void writeStatsToCSV(const std::vector<ChemistryModule::SimulationErrorStats> &all_stats,
|
void writeStatsToCSV(const std::vector<ChemistryModule::SimulationErrorStats> &all_stats,
|
||||||
const std::vector<std::string> &species_names,
|
const std::vector<std::string> &species_names,
|
||||||
|
const std::string &out_dir,
|
||||||
const std::string &filename)
|
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())
|
if (!out.is_open())
|
||||||
{
|
{
|
||||||
std::cerr << "Could not open " << filename << " !" << std::endl;
|
std::cerr << "Could not open " << filename << " !" << std::endl;
|
||||||
@ -24,7 +28,7 @@ namespace poet
|
|||||||
<< std::setw(15) << "MAPE"
|
<< std::setw(15) << "MAPE"
|
||||||
<< std::setw(15) << "RRSME" << "\n";
|
<< std::setw(15) << "RRSME" << "\n";
|
||||||
|
|
||||||
out << std::string(75, '-') << "\n"; // separator line
|
out << std::string(75, '-') << "\n";
|
||||||
|
|
||||||
// data rows
|
// data rows
|
||||||
for (size_t i = 0; i < all_stats.size(); ++i)
|
for (size_t i = 0; i < all_stats.size(); ++i)
|
||||||
@ -39,7 +43,7 @@ namespace poet
|
|||||||
<< std::setw(15) << all_stats[i].rrmse[j]
|
<< std::setw(15) << all_stats[i].rrmse[j]
|
||||||
<< "\n";
|
<< "\n";
|
||||||
}
|
}
|
||||||
out << "\n"; // blank line between iterations
|
out << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
out.close();
|
out.close();
|
||||||
|
|||||||
@ -5,5 +5,6 @@ namespace poet
|
|||||||
{
|
{
|
||||||
void writeStatsToCSV(const std::vector<ChemistryModule::SimulationErrorStats> &all_stats,
|
void writeStatsToCSV(const std::vector<ChemistryModule::SimulationErrorStats> &all_stats,
|
||||||
const std::vector<std::string> &species_names,
|
const std::vector<std::string> &species_names,
|
||||||
|
const std::string &out_dir,
|
||||||
const std::string &filename);
|
const std::string &filename);
|
||||||
} // namespace poet
|
} // namespace poet
|
||||||
|
|||||||
@ -1,13 +1,20 @@
|
|||||||
#include "IO/Datatypes.hpp"
|
#include "IO/Datatypes.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <highfive/H5Easy.hpp>
|
#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
|
// TODO: errorhandling
|
||||||
H5Easy::File file(file_path, H5Easy::File::Overwrite);
|
H5Easy::File file(file_path, H5Easy::File::Overwrite);
|
||||||
|
|
||||||
|
|
||||||
H5Easy::dump(file, "/MetaParam/Iterations", checkpoint.iteration);
|
H5Easy::dump(file, "/MetaParam/Iterations", checkpoint.iteration);
|
||||||
H5Easy::dump(file, "/Grid/Names", checkpoint.field.GetProps());
|
H5Easy::dump(file, "/Grid/Names", checkpoint.field.GetProps());
|
||||||
H5Easy::dump(file, "/Grid/Chemistry", checkpoint.field.As2DVector());
|
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;
|
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");
|
checkpoint.iteration = H5Easy::load<uint32_t>(file, "/MetaParam/Iterations");
|
||||||
|
|
||||||
|
|||||||
@ -330,7 +330,7 @@ bool triggerRollbackIfExceeded(ChemistryModule &chem, RuntimeParameters ¶ms,
|
|||||||
" → rolling back to iteration " + std::to_string(rollback_iter));
|
" → rolling back to iteration " + std::to_string(rollback_iter));
|
||||||
|
|
||||||
Checkpoint_s checkpoint_read{.field = chem.getField()};
|
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;
|
current_iteration = checkpoint_read.iteration;
|
||||||
return true; // rollback happened
|
return true; // rollback happened
|
||||||
}
|
}
|
||||||
@ -490,13 +490,13 @@ static Rcpp::List RunMasterLoop(RInsidePOET &R, RuntimeParameters ¶ms,
|
|||||||
|
|
||||||
if(iter % params.checkpoint_interval == 0){
|
if(iter % params.checkpoint_interval == 0){
|
||||||
MSG("Writing checkpoint of iteration " + std::to_string(iter));
|
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});
|
{.field = chem.getField(), .iteration = iter});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.control_interval_enabled && !params.rollback_enabled)
|
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)){
|
if(triggerRollbackIfExceeded(chem, params, iter)){
|
||||||
params.rollback_enabled = true;
|
params.rollback_enabled = true;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user