From 89f52fef95d6f0b8720d6ea772b8eaaa4b24b3c8 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 9 Oct 2026 12:32:10 +0200 Subject: [PATCH] ModelValidation: remove a map file before writing it again, rather than truncating it The validation in the model's setting writes its three maps and the map MTZ over the ones the first validation wrote seconds before, and a two-pass run writes over the first pass's. XFS (like ext4) flushes a file that was truncated and rewritten when it is closed, so each overwrite forced ~155 MB out to the disk on the spot. Measured on /data (XFS on a hard disk): three 155 MB files rewritten 3.16 s, written fresh 0.62 s, removed and written again 0.66 s. The contents are the same bytes either way. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi --- .../structure_refinement/ModelValidation.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/image_analysis/structure_refinement/ModelValidation.cpp b/image_analysis/structure_refinement/ModelValidation.cpp index 14bc6ed39..17d4f39e6 100644 --- a/image_analysis/structure_refinement/ModelValidation.cpp +++ b/image_analysis/structure_refinement/ModelValidation.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -59,11 +60,20 @@ gemmi::Grid map_from_coefficients(gemmi::AsuData> &co return MapFromFPhi(gemmi::get_f_phi_on_grid(coef, size, true)); } +// A file this run may already have written - the validation in the model's setting writes over the +// first one's maps - is removed before it is written again, not truncated: XFS (and ext4) flush a file +// truncated and rewritten when it is closed, and three 150 MB maps forced out to a disk that way cost +// seconds where a fresh file costs nothing. +void remove_before_rewriting(const std::string &path) { + std::remove(path.c_str()); +} + // Write a map as CCP4; return its RMS (the sigma the map is read in). double write_ccp4(const gemmi::Grid &map, const std::string &path) { gemmi::Ccp4 ccp4; ccp4.grid = map; ccp4.update_ccp4_header(2); + remove_before_rewriting(path); ccp4.write_ccp4_map(path); return ccp4.hstats.rms; } @@ -1466,6 +1476,7 @@ ModelValidationResult Validate(const std::vector &merged, } mtz.nreflections = static_cast(terms.size()); mtz.data = std::move(data); + remove_before_rewriting(output_prefix + "_maps.mtz"); mtz.write_to_file(output_prefix + "_maps.mtz"); } catch (const std::exception &e) { logger.Warning("Model validation: could not write map MTZ: {}", e.what());