From dcdf5101250af0ab295a0f4a0b78c873c1b16a63 Mon Sep 17 00:00:00 2001 From: rastogi Date: Wed, 15 Oct 2025 11:32:29 +0200 Subject: [PATCH] Added rrmse threshold values --- src/poet.cpp | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/poet.cpp b/src/poet.cpp index 4cc308788..b40eeedac 100644 --- a/src/poet.cpp +++ b/src/poet.cpp @@ -271,6 +271,8 @@ int parseInitValues(int argc, char **argv, RuntimeParameters ¶ms) Rcpp::as(global_rt_setup->operator[]("checkpoint_interval")); params.mape_threshold = Rcpp::as>(global_rt_setup->operator[]("mape_threshold")); + params.rrmse_threshold = + Rcpp::as>(global_rt_setup->operator[]("rrmse_threshold")); } catch (const std::exception &e) { @@ -303,37 +305,40 @@ void call_master_iter_end(RInside &R, const Field &trans, const Field &chem) bool triggerRollbackIfExceeded(ChemistryModule &chem, RuntimeParameters ¶ms, uint32_t ¤t_iteration) { - const std::vector &mape_values = chem.error_history.back().mape; + const auto &mape = chem.error_history.back().mape; + const auto &rrmse = chem.error_history.back().rrmse; + const auto &props = chem.getField().GetProps(); - for (uint32_t i = 0; i < params.mape_threshold.size(); i++) - { - // Skip if no meaningful MAPE value - if(mape_values[i] == 0){ - continue; - } - if (mape_values[i] > params.mape_threshold[i]) + for (uint32_t i = 0; i < params.mape_threshold.size(); ++i) { - uint32_t rollback_iteration = ((current_iteration - 1) / params.checkpoint_interval) * params.checkpoint_interval; + // Skip invalid entries + if ((mape[i] == 0 && rrmse[i] == 0)) + continue; - MSG("[THRESHOLD EXCEEDED] " + chem.getField().GetProps()[i] + " has MAPE = " + - std::to_string(mape_values[i]) + " exceeding threshold = " + std::to_string(params.mape_threshold[i]) + - " → rolling back to iteration " + std::to_string(rollback_iteration)); + bool mape_exceeded = mape[i] > params.mape_threshold[i]; + bool rrmse_exceeded = rrmse[i] > params.rrmse_threshold[i]; - Checkpoint_s checkpoint_read{.field = chem.getField()}; - read_checkpoint("checkpoint" + std::to_string(rollback_iteration) + ".hdf5", checkpoint_read); - current_iteration = checkpoint_read.iteration; + if (mape_exceeded || rrmse_exceeded) + { + uint32_t rollback_iter = ((current_iteration - 1) / params.checkpoint_interval) * params.checkpoint_interval; + std::string metric = mape_exceeded ? "MAPE" : "RRMSE"; + double value = mape_exceeded ? mape[i] : rrmse[i]; + double threshold = mape_exceeded ? params.mape_threshold[i] : params.rrmse_threshold[i]; - // Rollback happend - return true; + MSG("[THRESHOLD EXCEEDED] " + props[i] + " has " + metric + " = " + + std::to_string(value) + " exceeding threshold = " + std::to_string(threshold) + + " → 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); + current_iteration = checkpoint_read.iteration; + return true; // rollback happened + } } - } - - MSG("All species are within their error thresholds."); - return false; + MSG("All species are within their MAPE and RRMSE thresholds."); + return false; } - - static Rcpp::List RunMasterLoop(RInsidePOET &R, RuntimeParameters ¶ms, DiffusionModule &diffusion, ChemistryModule &chem)