Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/green/sc/common_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,5 @@ namespace green::sc {
p.define<bool>("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
5 changes: 0 additions & 5 deletions src/green/sc/except.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 1 addition & 47 deletions src/green/sc/sc_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(_results_file, _dyson_solver.get_grids_version());
}
}

Expand Down Expand Up @@ -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<std::string>("__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
Expand Down
2 changes: 1 addition & 1 deletion test/sc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<fourth_power_equation_dyson>(MPI_COMM_WORLD, p5), green::sc::outdated_results_file_error);
REQUIRE_THROWS_AS(green::sc::sc_loop<fourth_power_equation_dyson>(MPI_COMM_WORLD, p5), green::grids::outdated_results_file_error);
}

// Clean up
Expand Down