From 66ab3368247e5119d3d0647a66bf1ef53ea73196 Mon Sep 17 00:00:00 2001
From: Stuart Mcalpine <s.r.mcalpine@durham.ac.uk>
Date: Thu, 1 Aug 2019 14:08:52 +0100
Subject: [PATCH] Rather that checking for duplicate times the snapshot and stf
 output_lists.

A better catch all solution is to track if stf has been run this timestep,
e->stf_this_timestep, and only invoke velociraptor is this value is 0.
---
 src/engine.c                 | 22 ++++++++--------------
 src/engine.h                 |  3 +++
 src/outputlist.c             | 17 -----------------
 src/velociraptor_interface.c |  2 ++
 4 files changed, 13 insertions(+), 31 deletions(-)

diff --git a/src/engine.c b/src/engine.c
index 1d4ad74a71..cb4a0a5eee 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -3742,6 +3742,7 @@ void engine_step(struct engine *e) {
   e->step += 1;
   engine_current_step = e->step;
   e->step_props = engine_step_prop_none;
+  e->stf_this_timestep = 0;
 
   /* When restarting, move everyone to the current time. */
   if (e->restarting) engine_drift_all(e, /*drift_mpole=*/1);
@@ -3973,7 +3974,7 @@ void engine_check_for_dumps(struct engine *e) {
       case output_snapshot:
 
         /* Do we want a corresponding VELOCIraptor output? */
-        if (with_stf && e->snapshot_invoke_stf) {
+        if (with_stf && e->snapshot_invoke_stf && !e->stf_this_timestep) {
 
 #ifdef HAVE_VELOCIRAPTOR
           velociraptor_invoke(e, /*linked_with_snap=*/1);
@@ -3994,7 +3995,7 @@ void engine_check_for_dumps(struct engine *e) {
 #endif
 
         /* Free the memory allocated for VELOCIraptor i/o. */
-        if (with_stf && e->snapshot_invoke_stf) {
+        if (with_stf && e->snapshot_invoke_stf && !e->stf_this_timestep) {
 #ifdef HAVE_VELOCIRAPTOR
           swift_free("gpart_group_data", e->s->gpart_group_data);
           e->s->gpart_group_data = NULL;
@@ -4019,8 +4020,10 @@ void engine_check_for_dumps(struct engine *e) {
 
 #ifdef HAVE_VELOCIRAPTOR
         /* Unleash the raptor! */
-        velociraptor_invoke(e, /*linked_with_snap=*/0);
-        e->step_props |= engine_step_prop_stf;
+        if (!e->stf_this_timestep) {
+          velociraptor_invoke(e, /*linked_with_snap=*/0);
+          e->step_props |= engine_step_prop_stf;
+        }
 
         /* ... and find the next output time */
         engine_compute_next_stf_time(e);
@@ -4989,6 +4992,7 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params,
   e->chemistry = chemistry;
   e->fof_properties = fof_properties;
   e->parameter_file = params;
+  e->stf_this_timestep = 0;
 #ifdef WITH_MPI
   e->cputime_last_step = 0;
   e->last_repartition = 0;
@@ -5441,16 +5445,6 @@ void engine_config(int restart, int fof, struct engine *e,
               "Scale-factor of first stf output (%e) must be after the "
               "simulation start a=%e.",
               e->a_first_stf_output, e->cosmology->a_begin);
-
-        if ((e->snapshot_invoke_stf && e->output_list_stf &&
-             e->output_list_snapshots) &&
-            (output_list_check_duplicates(e->output_list_snapshots,
-                                          e->output_list_stf)))
-            error("Cannot have duplicate time entries between "
-                  "StructureFinding:output_list and "
-                  "Snapshots:output_list when Snapshots:invoke_stf "
-                  "is selected.");
-
       }
 
       if (e->policy & engine_policy_fof) {
diff --git a/src/engine.h b/src/engine.h
index 8296933561..84397a6dbd 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -471,6 +471,9 @@ struct engine {
 
   /* Label of the run */
   char run_name[PARSER_MAX_LINE_SIZE];
+
+  /* Has there been an stf this timestep? */
+  int stf_this_timestep;
 };
 
 /* Function prototypes, engine.c. */
diff --git a/src/outputlist.c b/src/outputlist.c
index 7e742a07c3..8e2a262419 100644
--- a/src/outputlist.c
+++ b/src/outputlist.c
@@ -314,20 +314,3 @@ void output_list_struct_restore(struct output_list *list, FILE *stream) {
   restart_read_blocks(list->times, list->size, sizeof(double), stream, NULL,
                       "times");
 }
-
-/**
- * @brief Check if there are duplicate times between two output lists
- *
- * @param list_a The first #output_list.
- * @param list_b THe second #output_list.
- */
-int output_list_check_duplicates(const struct output_list *list_a,
-                                 const struct output_list *list_b) {
-
-  for (size_t ind_a=0; ind_a < list_a->size; ind_a++) {
-    for (size_t ind_b=0; ind_b < list_a->size; ind_b++) {
-      if (list_a->times[ind_a] == list_b->times[ind_b]) return 1;
-    }
-  }
-  return 0;
-}
diff --git a/src/velociraptor_interface.c b/src/velociraptor_interface.c
index a7a5876e93..6e73836f23 100644
--- a/src/velociraptor_interface.c
+++ b/src/velociraptor_interface.c
@@ -606,6 +606,8 @@ void velociraptor_invoke(struct engine *e, const int linked_with_snap) {
   /* Increase output counter (if not linked with snapshots) */
   if (!linked_with_snap) e->stf_output_count++;
 
+  /* Record we have ran stf this timestep */
+  e->stf_this_timestep = 1;
 #else
   error("SWIFT not configure to run with VELOCIraptor.");
 #endif /* HAVE_VELOCIRAPTOR */
-- 
GitLab