From e97e0e4d267fb9162fd955071a3eec1672ff55a5 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Tue, 9 Jul 2024 14:48:25 +0200
Subject: [PATCH] Fix to the recording triggers. Coming from COLIBRE downstream
 fork.

---
 src/engine_io.c | 98 +++++++++++++++++++++++++++----------------------
 1 file changed, 55 insertions(+), 43 deletions(-)

diff --git a/src/engine_io.c b/src/engine_io.c
index f8fe3d2971..7a72d4bdf4 100644
--- a/src/engine_io.c
+++ b/src/engine_io.c
@@ -694,6 +694,57 @@ void engine_io(struct engine *e) {
   e->time = time;
 }
 
+/**
+ * @brief Set the value of the recording trigger windows based
+ * on the user's desires and the time to the next snapshot.
+ *
+ * @param e The #engine.
+ */
+void engine_set_and_verify_snapshot_triggers(struct engine *e) {
+
+  /* Time until the next snapshot */
+  double time_to_next_snap;
+  if (e->policy & engine_policy_cosmology) {
+    time_to_next_snap = cosmology_get_delta_time(e->cosmology, e->ti_current,
+                                                 e->ti_next_snapshot);
+  } else {
+    time_to_next_snap = (e->ti_next_snapshot - e->ti_current) * e->time_base;
+  }
+
+  /* Do we need to reduce any of the recording trigger times?
+   * Or can we set them with the user's desired range? */
+  for (int k = 0; k < num_snapshot_triggers_part; ++k) {
+    if (e->snapshot_recording_triggers_desired_part[k] > 0) {
+      if (e->snapshot_recording_triggers_desired_part[k] > time_to_next_snap) {
+        e->snapshot_recording_triggers_part[k] = time_to_next_snap;
+      } else {
+        e->snapshot_recording_triggers_part[k] =
+            e->snapshot_recording_triggers_desired_part[k];
+      }
+    }
+  }
+  for (int k = 0; k < num_snapshot_triggers_spart; ++k) {
+    if (e->snapshot_recording_triggers_desired_spart[k] > 0) {
+      if (e->snapshot_recording_triggers_desired_spart[k] > time_to_next_snap) {
+        e->snapshot_recording_triggers_spart[k] = time_to_next_snap;
+      } else {
+        e->snapshot_recording_triggers_spart[k] =
+            e->snapshot_recording_triggers_desired_spart[k];
+      }
+    }
+  }
+  for (int k = 0; k < num_snapshot_triggers_bpart; ++k) {
+    if (e->snapshot_recording_triggers_desired_bpart[k] > 0) {
+      if (e->snapshot_recording_triggers_desired_bpart[k] > time_to_next_snap) {
+        e->snapshot_recording_triggers_bpart[k] = time_to_next_snap;
+      } else {
+        e->snapshot_recording_triggers_bpart[k] =
+            e->snapshot_recording_triggers_desired_bpart[k];
+      }
+    }
+  }
+}
+
 /**
  * @brief Computes the next time (on the time line) for a dump
  *
@@ -705,6 +756,8 @@ void engine_compute_next_snapshot_time(struct engine *e) {
   if (e->output_list_snapshots) {
     output_list_read_next_time(e->output_list_snapshots, e, "snapshots",
                                &e->ti_next_snapshot);
+
+    engine_set_and_verify_snapshot_triggers(e);
     return;
   }
 
@@ -762,49 +815,8 @@ void engine_compute_next_snapshot_time(struct engine *e) {
         message("Next snapshot time set to t=%e.", next_snapshot_time);
     }
 
-    /* Time until the next snapshot */
-    double time_to_next_snap;
-    if (e->policy & engine_policy_cosmology) {
-      time_to_next_snap = cosmology_get_delta_time(e->cosmology, e->ti_current,
-                                                   e->ti_next_snapshot);
-    } else {
-      time_to_next_snap = (e->ti_next_snapshot - e->ti_current) * e->time_base;
-    }
-
-    /* Do we need to reduce any of the recording trigger times? */
-    for (int k = 0; k < num_snapshot_triggers_part; ++k) {
-      if (e->snapshot_recording_triggers_desired_part[k] > 0) {
-        if (e->snapshot_recording_triggers_desired_part[k] >
-            time_to_next_snap) {
-          e->snapshot_recording_triggers_part[k] = time_to_next_snap;
-        } else {
-          e->snapshot_recording_triggers_part[k] =
-              e->snapshot_recording_triggers_desired_part[k];
-        }
-      }
-    }
-    for (int k = 0; k < num_snapshot_triggers_spart; ++k) {
-      if (e->snapshot_recording_triggers_desired_spart[k] > 0) {
-        if (e->snapshot_recording_triggers_desired_spart[k] >
-            time_to_next_snap) {
-          e->snapshot_recording_triggers_spart[k] = time_to_next_snap;
-        } else {
-          e->snapshot_recording_triggers_spart[k] =
-              e->snapshot_recording_triggers_desired_spart[k];
-        }
-      }
-    }
-    for (int k = 0; k < num_snapshot_triggers_bpart; ++k) {
-      if (e->snapshot_recording_triggers_desired_bpart[k] > 0) {
-        if (e->snapshot_recording_triggers_desired_bpart[k] >
-            time_to_next_snap) {
-          e->snapshot_recording_triggers_bpart[k] = time_to_next_snap;
-        } else {
-          e->snapshot_recording_triggers_bpart[k] =
-              e->snapshot_recording_triggers_desired_bpart[k];
-        }
-      }
-    }
+    /* Set the recording triggers accordingly for the next output */
+    engine_set_and_verify_snapshot_triggers(e);
   }
 }
 
-- 
GitLab