From 36296f3a233137542eadd8045e75bd37e7c66ed8 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Thu, 5 Jan 2017 18:54:01 +0100
Subject: [PATCH] Added infrastructure to check that particles follow the
 Kick-Drift-Kick scheme correctly

---
 src/drift.h                    | 10 ++++++++++
 src/hydro/Gadget2/hydro_part.h | 10 ++++++++++
 src/kick.h                     | 12 +++++++++++-
 src/runner.c                   |  6 ++----
 src/space.c                    |  5 +++++
 5 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/src/drift.h b/src/drift.h
index 478971e30a..c01b1a5c6b 100644
--- a/src/drift.h
+++ b/src/drift.h
@@ -66,6 +66,16 @@ __attribute__((always_inline)) INLINE static void drift_part(
     struct part *restrict p, struct xpart *restrict xp, float dt,
     double timeBase, integertime_t ti_old, integertime_t ti_current) {
 
+#ifdef SWIFT_DEBUG_CHECKS
+  if (p->ti_drift != ti_old)
+    error(
+        "Particle has not been drifted to the current time p->ti_drift=%lld, "
+        "c->ti_old=%lld, ti_current=%lld",
+        p->ti_drift, ti_old, ti_current);
+
+  p->ti_drift = ti_current;
+#endif
+
   /* Drift... */
   p->x[0] += xp->v_full[0] * dt;
   p->x[1] += xp->v_full[1] * dt;
diff --git a/src/hydro/Gadget2/hydro_part.h b/src/hydro/Gadget2/hydro_part.h
index 8fb4b1d7b7..69ae79666e 100644
--- a/src/hydro/Gadget2/hydro_part.h
+++ b/src/hydro/Gadget2/hydro_part.h
@@ -130,6 +130,16 @@ struct part {
   /* Time-step length */
   timebin_t time_bin;
 
+#ifdef SWIFT_DEBUG_CHECKS
+
+  /* Time of the last drift */
+  integertime_t ti_drift;
+
+  /* Time of the last kick */
+  integertime_t ti_kick;
+
+#endif
+
 } SWIFT_STRUCT_ALIGN;
 
 #endif /* SWIFT_GADGET2_HYDRO_PART_H */
diff --git a/src/kick.h b/src/kick.h
index d532b2a9ab..a679da2741 100644
--- a/src/kick.h
+++ b/src/kick.h
@@ -64,11 +64,21 @@ __attribute__((always_inline)) INLINE static void kick_gpart(
  */
 __attribute__((always_inline)) INLINE static void kick_part(
     struct part *restrict p, struct xpart *restrict xp, integertime_t ti_start,
-    integertime_t ti_end, integertime_t ti_current, double timeBase) {
+    integertime_t ti_end, double timeBase) {
 
   /* Time interval for this half-kick */
   const float dt = (ti_end - ti_start) * timeBase;
 
+#ifdef SWIFT_DEBUG_CHECKS
+  if (p->ti_kick != ti_start)
+    error(
+        "Particle has not been kicked to the current time p->ti_kick=%lld, "
+        "ti_start=%lld, ti_end=%lld",
+        p->ti_kick, ti_start, ti_end);
+
+  p->ti_kick = ti_end;
+#endif
+
   /* Get the acceleration */
   float a_tot[3] = {p->a_hydro[0], p->a_hydro[1], p->a_hydro[2]};
   if (p->gpart != NULL) {
diff --git a/src/runner.c b/src/runner.c
index 50e72606b4..c224fd7cd2 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -867,8 +867,7 @@ void runner_do_kick1(struct runner *r, struct cell *c, int timer) {
             get_integer_time_begin(ti_current, p->time_bin);
 
         /* do the kick */
-        kick_part(p, xp, ti_begin, ti_begin + ti_step / 2, ti_current,
-                  timeBase);
+        kick_part(p, xp, ti_begin, ti_begin + ti_step / 2, timeBase);
       }
     }
 
@@ -944,8 +943,7 @@ void runner_do_kick2(struct runner *r, struct cell *c, int timer) {
             get_integer_time_begin(ti_current, p->time_bin);
 
         /* Finish the time-step with a second half-kick */
-        kick_part(p, xp, ti_begin + ti_step / 2, ti_begin + ti_step, ti_current,
-                  timeBase);
+        kick_part(p, xp, ti_begin + ti_step / 2, ti_begin + ti_step, timeBase);
 
         /* Prepare the values to be drifted */
         hydro_reset_predicted_values(p, xp);
diff --git a/src/space.c b/src/space.c
index a0a36da641..bca8a16533 100644
--- a/src/space.c
+++ b/src/space.c
@@ -1770,6 +1770,11 @@ void space_init_parts(struct space *s) {
 #endif
 
     hydro_first_init_part(&p[i], &xp[i]);
+
+#ifdef SWIFT_DEBUG_CHECKS
+    p->ti_drift = 0;
+    p->ti_kick = 0;
+#endif
   }
 }
 
-- 
GitLab