From d33aa37efc290ab6c142b391be3be00b40bd0436 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Tue, 8 Oct 2019 18:36:35 +0200
Subject: [PATCH] Add a flag to verify that a synchronized particle gets its
 time-step recomputed.

---
 src/hydro/Minimal/hydro.h      |  4 +++
 src/hydro/Minimal/hydro_part.h |  3 +++
 src/runner_time_integration.c  |  8 ++++++
 src/space.c                    |  2 ++
 src/timestep_sync.h            | 49 +++++++++++++++++++---------------
 5 files changed, 45 insertions(+), 21 deletions(-)

diff --git a/src/hydro/Minimal/hydro.h b/src/hydro/Minimal/hydro.h
index 5438eba823..fac59f1b89 100644
--- a/src/hydro/Minimal/hydro.h
+++ b/src/hydro/Minimal/hydro.h
@@ -878,6 +878,10 @@ __attribute__((always_inline)) INLINE static void hydro_first_init_part(
   xp->a_grav[2] = 0.f;
   xp->u_full = p->u;
 
+#ifdef SWIFT_DEBUG_CHECKS
+  p->synchronized = 0;
+#endif
+
   hydro_reset_acceleration(p);
   hydro_init_part(p, NULL);
 }
diff --git a/src/hydro/Minimal/hydro_part.h b/src/hydro/Minimal/hydro_part.h
index d9596dcd3a..c65c2e7c80 100644
--- a/src/hydro/Minimal/hydro_part.h
+++ b/src/hydro/Minimal/hydro_part.h
@@ -194,6 +194,9 @@ struct part {
   /* Time of the last kick */
   integertime_t ti_kick;
 
+  /* Has the particle been synchronised back on the timeline this step? */
+  char synchronized;
+
 #endif
 
 } SWIFT_STRUCT_ALIGN;
diff --git a/src/runner_time_integration.c b/src/runner_time_integration.c
index eee891e51c..142a44bcf4 100644
--- a/src/runner_time_integration.c
+++ b/src/runner_time_integration.c
@@ -620,6 +620,10 @@ void runner_do_timestep(struct runner *r, struct cell *c, int timer) {
                                with_cosmology, e->cosmology,
                                e->hydro_properties, e->cooling_func, e->time);
 
+#ifdef SWIFT_DEBUG_CHECKS
+        p->synchronized = 0;
+#endif
+
         /* Number of updated particles */
         updated++;
         if (p->gpart != NULL) g_updated++;
@@ -646,6 +650,10 @@ void runner_do_timestep(struct runner *r, struct cell *c, int timer) {
 
       else { /* part is inactive */
 
+#ifdef SWIFT_DEBUG_CHECKS
+        if (p->synchronized) p->synchronized = 2;
+#endif
+
         if (!part_is_inhibited(p, e)) {
 
           const integertime_t ti_end =
diff --git a/src/space.c b/src/space.c
index 4d28330903..e2c8d135f0 100644
--- a/src/space.c
+++ b/src/space.c
@@ -5295,6 +5295,8 @@ void space_check_limiter_mapper(void *map_data, int nr_parts,
     if (parts[k].wakeup == time_bin_awake)
       error("Particle still woken up! id=%lld", parts[k].id);
 
+    if (parts[k].synchronized != 0) error("Synchronized particle not treated!");
+
     if (parts[k].gpart != NULL)
       if (parts[k].time_bin != parts[k].gpart->time_bin)
         error("Gpart not on the same time-bin as part");
diff --git a/src/timestep_sync.h b/src/timestep_sync.h
index aee0cde666..12f810ff8d 100644
--- a/src/timestep_sync.h
+++ b/src/timestep_sync.h
@@ -24,24 +24,28 @@
 
 #include "kick.h"
 
-INLINE static void timestep_sync_part(struct part *p, struct xpart *xp, const struct engine *e,
-				      const struct cosmology *cosmo) {
+INLINE static void timestep_sync_part(struct part *p, struct xpart *xp,
+                                      const struct engine *e,
+                                      const struct cosmology *cosmo) {
 
   const int with_cosmology = (e->policy & engine_policy_cosmology);
   const integertime_t ti_current = e->ti_current;
   const timebin_t max_active_bin = e->max_active_bin;
+  const timebin_t min_active_bin = e->min_active_bin;
   const double time_base = e->time_base;
 
   /* This particle is already active. Nothing to do here... */
   if (p->time_bin <= max_active_bin) return;
 
   message("Synchronizing particle %lld", p->id);
-  
+
   /* We want to make the particle finish it's time-step now. */
 
   /* Start by recovering the start and end point of the particle's time-step. */
-  const integertime_t old_ti_beg = get_integer_time_begin(ti_current, p->time_bin);
-  const integertime_t old_ti_end = get_integer_time_end(ti_current, p->time_bin);
+  const integertime_t old_ti_beg =
+      get_integer_time_begin(ti_current, p->time_bin);
+  const integertime_t old_ti_end =
+      get_integer_time_end(ti_current, p->time_bin);
 
   /* Old time-step length on the time-line */
   const integertime_t old_dti = old_ti_end - old_ti_beg;
@@ -70,8 +74,7 @@ INLINE static void timestep_sync_part(struct part *p, struct xpart *xp, const st
   if (new_dti > old_dti) error("New time-step larger than old one");
 #endif
 
-
-    double dt_kick_grav = 0., dt_kick_hydro = 0., dt_kick_therm = 0.,
+  double dt_kick_grav = 0., dt_kick_hydro = 0., dt_kick_therm = 0.,
          dt_kick_corr = 0.;
 
   /* Now we need to reverse the kick1... (the dt are negative here) */
@@ -95,21 +98,21 @@ INLINE static void timestep_sync_part(struct part *p, struct xpart *xp, const st
             e->cosmology, e->hydro_properties, e->entropy_floor,
             old_ti_beg + old_dti / 2, old_ti_beg);
 
-    /* ...and apply the new one (dt is positiive) */
+  /* ...and apply the new one (dt is positiive) */
   if (with_cosmology) {
     dt_kick_hydro = cosmology_get_hydro_kick_factor(cosmo, new_ti_beg,
-                                                    new_ti_beg + new_dti );
-    dt_kick_grav = cosmology_get_grav_kick_factor(cosmo, new_ti_beg,
-                                                  new_ti_beg + new_dti );
+                                                    new_ti_beg + new_dti);
+    dt_kick_grav =
+        cosmology_get_grav_kick_factor(cosmo, new_ti_beg, new_ti_beg + new_dti);
     dt_kick_therm = cosmology_get_therm_kick_factor(cosmo, new_ti_beg,
-                                                    new_ti_beg + new_dti );
-    dt_kick_corr = cosmology_get_corr_kick_factor(cosmo, new_ti_beg,
-                                                  new_ti_beg + new_dti );
+                                                    new_ti_beg + new_dti);
+    dt_kick_corr =
+        cosmology_get_corr_kick_factor(cosmo, new_ti_beg, new_ti_beg + new_dti);
   } else {
-    dt_kick_hydro = (new_dti ) * time_base;
-    dt_kick_grav = (new_dti ) * time_base;
-    dt_kick_therm = (new_dti ) * time_base;
-    dt_kick_corr = (new_dti ) * time_base;
+    dt_kick_hydro = (new_dti)*time_base;
+    dt_kick_grav = (new_dti)*time_base;
+    dt_kick_therm = (new_dti)*time_base;
+    dt_kick_corr = (new_dti)*time_base;
   }
 
   kick_part(p, xp, dt_kick_hydro, dt_kick_grav, dt_kick_therm, dt_kick_corr,
@@ -117,12 +120,16 @@ INLINE static void timestep_sync_part(struct part *p, struct xpart *xp, const st
             new_ti_beg + new_dti);
 
 #ifdef SWIFT_DEBUG_CHECKS
-  if (p->ti_kick != ti_current) error("Particle has not been synchronized correctly.");				  
+  if (p->ti_kick != ti_current)
+    error("Particle has not been synchronized correctly.");
+  p->synchronized = 1;
 #endif
 
+  p->time_bin = min_active_bin;
+  p->wakeup = time_bin_not_awake;
 
-  /* The particle is now ready to compute its new time-step size and for the next kick */
+  /* The particle is now ready to compute its new time-step size and for the
+   * next kick */
 }
 
-
 #endif /* SWIFT_TIMESTEP_SYNC_H */
-- 
GitLab