From 98c7a106347727efad48171d87827077072054e6 Mon Sep 17 00:00:00 2001 From: Gaurav Harsha Date: Wed, 25 Feb 2026 16:16:35 -0500 Subject: [PATCH 1/3] refactor grids version check function defs from green-sc to green-grids --- src/green/sc/common_defs.h | 29 ----------------------- src/green/sc/except.h | 5 ---- src/green/sc/sc_loop.h | 48 +------------------------------------- 3 files changed, 1 insertion(+), 81 deletions(-) diff --git a/src/green/sc/common_defs.h b/src/green/sc/common_defs.h index cabe665..a12ebe9 100644 --- a/src/green/sc/common_defs.h +++ b/src/green/sc/common_defs.h @@ -84,34 +84,5 @@ namespace green::sc { p.define("const_density", "Maintain constant number of electrons through iterations", true); } - inline int compare_version_strings(const std::string& v1, const std::string& v2) { - int major_V1 = 0, minor_V1 = 0, patch_V1 = 0; - int major_V2 = 0, minor_V2 = 0, patch_V2 = 0; - - char suffix_V1[32] = ""; - char suffix_V2[32] = ""; - - int parsed_1 = std::sscanf(v1.c_str(), "%d.%d.%d%30s", &major_V1, &minor_V1, &patch_V1, suffix_V1); - int parsed_2 = std::sscanf(v2.c_str(), "%d.%d.%d%30s", &major_V2, &minor_V2, &patch_V2, suffix_V2); - - if (parsed_1 < 3) { - throw std::runtime_error("First version string (v1) failed to parse: '" + v1 + "'. Expected format: major.minor.patch[suffix]"); - } - if (parsed_2 < 3) { - throw std::runtime_error("Second version string (v2) failed to parse: '" + v2 + "'. Expected format: major.minor.patch[suffix]"); - } - - if (major_V1 != major_V2) { - return major_V1 > major_V2 ? 1 : -1; - } - if (minor_V1 != minor_V2) { - return minor_V1 > minor_V2 ? 1 : -1; - } - if (patch_V1 != patch_V2) { - return patch_V1 > patch_V2 ? 1 : -1; - } - - return 0; - } } // namespace green::sc #endif // GREEN_SC_COMMON_DEFS_H diff --git a/src/green/sc/except.h b/src/green/sc/except.h index 816509d..796aada 100644 --- a/src/green/sc/except.h +++ b/src/green/sc/except.h @@ -35,11 +35,6 @@ namespace green::sc { explicit sc_diis_vsp_error(const std::string& what) : std::runtime_error(what) {} }; - class outdated_results_file_error : public std::runtime_error { - public: - explicit outdated_results_file_error(const std::string& what) : std::runtime_error(what) {} - }; - } // namespace green::sc #endif // GREEN_SC_EXCEPT_H diff --git a/src/green/sc/sc_loop.h b/src/green/sc/sc_loop.h index aceae66..61cf3ea 100644 --- a/src/green/sc/sc_loop.h +++ b/src/green/sc/sc_loop.h @@ -78,7 +78,7 @@ namespace green::sc { _itermax(p["itermax"]), _iter(0), _e_thr(p["threshold"]), _e_thr_sp(p["E_thr_sp"]), _input_path(p["input_file"]), _results_file(p["results_file"]), _restart(p["restart"]), _dyson_solver(p), _mix(p), _context(comm) { if (_restart) { - check_grids_version_consistency(); + green::grids::check_grids_version_in_hdf5(p["results_file"], _dyson_solver.get_grids_version()); } } @@ -182,52 +182,6 @@ namespace green::sc { ar.close(); } - /** - * @brief Checks consistency between grid-file version used in current run vs. the version - * used to generate the results file that is being restarted from. - * This is to prevent users from accidentally restarting from a results file that was generated with - * an older version of green-grids, which can lead to silent errors in the results. - * - * 1. If the results file does not have a green-grids version attribute, treat it as having been - * generated with the baseline grids version (GRIDS_MIN_VERSION, currently 0.2.4) and check - * that the current grid-file version is not newer; otherwise an error is raised. - * 2. If the results file has a green-grids version attribute, compare that version with the current - * grid-file version and distinguish older, equal, and newer cases, allowing only compatible - * combinations and throwing specific errors when there is a mismatch. - */ - void check_grids_version_consistency() { - // If results file does not exist, nothing to check - if (!std::filesystem::exists(_results_file)) return; - - // Read grid-file version - const std::string& grid_file_version = _dyson_solver.get_grids_version(); - - h5pp::archive ar(_results_file, "r"); - if (ar.has_attribute("__grids_version__")) { - std::string grids_version_in_results; - grids_version_in_results = ar.get_attribute("__grids_version__"); - ar.close(); // safely close before throwing - if (compare_version_strings(grid_file_version, grids_version_in_results) < 0) { - throw green::grids::outdated_grids_file_error("The current green-grids version (" + grid_file_version + - ") is older than the green-grids version used to create the original results file (" - + grids_version_in_results + - "). Please update green-grids to version " + grids_version_in_results); - } else if (compare_version_strings(grid_file_version, grids_version_in_results) > 0) { - throw outdated_results_file_error("The green-grids version used to create the results file (" + grids_version_in_results + - ") is older than the current specified grid file (" + grid_file_version + - "). Please download the appropriate version from: " + - "https://github.com/Green-Phys/green-grids/releases/ or https://github.com/Green-Phys/green-grids/tags"); - } - } else if (compare_version_strings(grid_file_version, green::grids::GRIDS_MIN_VERSION) > 0) { - ar.close(); // safely close before throwing - throw outdated_results_file_error("The results file was created using un-versioned grid file (equiv. to " + green::grids::GRIDS_MIN_VERSION + - ") and the current green-grids version (" + grid_file_version + ") is newer.\n" + - "Please use old grid files from: https://github.com/Green-Phys/green-grids/releases/tag/v0.2.4."); - } else { - ar.close(); - } - } - DysonSolver& dyson_solver() { return _dyson_solver; } }; } // namespace green::sc From 96932b4886fef3c4a84af02f39e32de6bc39d16b Mon Sep 17 00:00:00 2001 From: Gaurav Harsha Date: Wed, 25 Feb 2026 16:59:05 -0500 Subject: [PATCH 2/3] fix error type in testing --- test/sc_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sc_test.cpp b/test/sc_test.cpp index cfda86f..90440d0 100644 --- a/test/sc_test.cpp +++ b/test/sc_test.cpp @@ -350,7 +350,7 @@ TEST_CASE("Self-consistency") { p5.parse(args_2); // Initialize p5 from args_2 (using the new grid file) and attempt to restart from res_file_2, // which was created with an older grid file; this should trigger outdated_results_file_error. - REQUIRE_THROWS_AS(green::sc::sc_loop(MPI_COMM_WORLD, p5), green::sc::outdated_results_file_error); + REQUIRE_THROWS_AS(green::sc::sc_loop(MPI_COMM_WORLD, p5), green::grids::outdated_results_file_error); } // Clean up From 90ab4a8d1a4bc9085a24f4fa41885d80d6f266b6 Mon Sep 17 00:00:00 2001 From: Gaurav Harsha Date: Thu, 26 Feb 2026 11:25:45 -0500 Subject: [PATCH 3/3] Update src/green/sc/sc_loop.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/green/sc/sc_loop.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/green/sc/sc_loop.h b/src/green/sc/sc_loop.h index 61cf3ea..f5b18e3 100644 --- a/src/green/sc/sc_loop.h +++ b/src/green/sc/sc_loop.h @@ -78,7 +78,7 @@ namespace green::sc { _itermax(p["itermax"]), _iter(0), _e_thr(p["threshold"]), _e_thr_sp(p["E_thr_sp"]), _input_path(p["input_file"]), _results_file(p["results_file"]), _restart(p["restart"]), _dyson_solver(p), _mix(p), _context(comm) { if (_restart) { - green::grids::check_grids_version_in_hdf5(p["results_file"], _dyson_solver.get_grids_version()); + green::grids::check_grids_version_in_hdf5(_results_file, _dyson_solver.get_grids_version()); } }