From 41d1a9895ca8dda82f97fb2dfc64f0726c91aae7 Mon Sep 17 00:00:00 2001 From: rastogi Date: Fri, 12 Sep 2025 09:47:00 +0200 Subject: [PATCH] rollback implemented, triggered when MAPE exceeds epsilon --- src/poet.cpp | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/poet.cpp b/src/poet.cpp index 2f39673be..2f1abd5f1 100644 --- a/src/poet.cpp +++ b/src/poet.cpp @@ -300,6 +300,35 @@ void call_master_iter_end(RInside &R, const Field &trans, const Field &chem) *global_rt_setup = R["setup"]; } +bool checkAndRollback(ChemistryModule &chem, RuntimeParameters ¶ms, uint32_t &iter) +{ + for (uint32_t i = 0; i < chem.error_stats_history.size(); i++) + { + if (iter == chem.error_stats_history[i].iteration) + { + for (uint32_t j = 0; j < params.species_epsilon.size(); j++) + { + if (params.species_epsilon[j] < chem.error_stats_history[i].mape[j] && chem.error_stats_history[i].mape[j] != 0 && chem.control_iteration_counter > 1) + { + uint32_t rollback_iter = iter - params.control_iteration; + + std::cout << chem.getField().GetProps()[j] << " with a MAPE value of " << chem.error_stats_history[i].mape[j] << " exceeds epsilon of " + << params.species_epsilon[j] << "! " << std::endl; + + Checkpoint_s checkpoint_read{.field = chem.getField()}; + read_checkpoint("checkpoint" + std::to_string(rollback_iter) + ".hdf5", checkpoint_read); + iter = checkpoint_read.iteration; + + chem.control_iteration_counter--; + + return true; + } + } + } + } + return false; +} + static Rcpp::List RunMasterLoop(RInsidePOET &R, RuntimeParameters ¶ms, DiffusionModule &diffusion, ChemistryModule &chem) @@ -441,21 +470,15 @@ static Rcpp::List RunMasterLoop(RInsidePOET &R, RuntimeParameters ¶ms, MSG("End of *coupling* iteration " + std::to_string(iter) + "/" + std::to_string(maxiter)); - /* - if (params.control_iteration_active) + if (iter % params.control_iteration == 0) { - std::string file_path = "checkpoint" + std::to_string(iter) + ".hdf5"; - write_checkpoint(file_path, - {.field = chem.getField(), .iteration = iter}); - } + writeStatsToCSV(chem.error_stats_history, chem.getField().GetProps(), "stats_overview"); - - if (iter % params.control_iteration == 0 && iter != 0) - { write_checkpoint("checkpoint" + std::to_string(iter) + ".hdf5", {.field = chem.getField(), .iteration = iter}); + checkAndRollback(chem, params, iter); + } - */ // MSG(); } // END SIMULATION LOOP @@ -503,8 +526,6 @@ static Rcpp::List RunMasterLoop(RInsidePOET &R, RuntimeParameters ¶ms, chem.MasterLoopBreak(); - writeStatsToCSV(chem.error_stats_history, chem.getField().GetProps(), "stats_overview"); - return profiling; }