From d93c56e9ad11dfd917876816d94a71a79a5593ff Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Tue, 5 Nov 2019 15:06:49 +0100
Subject: [PATCH] When dumping a restart file at the end of a run, only drift
 the particles if no other form of i/o has taken place this step to avoid a
 drift towards the past.

---
 examples/main.c | 13 +++++++++++--
 src/engine.c    |  3 ++-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/examples/main.c b/examples/main.c
index 03ae3dec85..4eefe2a6b7 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -1275,8 +1275,17 @@ int main(int argc, char *argv[]) {
 
     /* Also if using nsteps to exit, will not have saved any restarts on exit,
      * make sure we do that (useful in testing only). */
-    if (force_stop || (e.restart_onexit && e.step - 1 == nsteps))
-      engine_dump_restarts(&e, 0, 1);
+    if (force_stop || (e.restart_onexit && e.step - 1 == nsteps)) {
+
+      /* Check whether we dumped snapshots/stats/groups and hence do not
+         need to drift (as we might drift to the past) */
+      int drifted_all = 0;
+      drifted_all |= (e.step_props & engine_step_prop_snapshot);
+      drifted_all |= (e.step_props & engine_step_prop_statistics);
+      drifted_all |= (e.step_props & engine_step_prop_stf);
+
+      engine_dump_restarts(&e, drifted_all, /*force=*/1);
+    }
 
     /* Dump the task data using the given frequency. */
     if (dump_tasks && (dump_tasks == 1 || j % dump_tasks == 1)) {
diff --git a/src/engine.c b/src/engine.c
index 700c287011..32190ecdbf 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -2352,7 +2352,8 @@ void engine_step(struct engine *e) {
   /********************************************************/
 
   /* Create a restart file if needed. */
-  engine_dump_restarts(e, 0, e->restart_onexit && engine_is_done(e));
+  engine_dump_restarts(e, /*drifted_all=*/0,
+                       e->restart_onexit && engine_is_done(e));
 
   engine_check_for_dumps(e);
 
-- 
GitLab