From 7126bd3231edbf5c51a16ca9d04dd53a0104bdb4 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Mon, 14 Nov 2016 16:04:56 -0700
Subject: [PATCH] Added debugging checks to the pair and self tasks to make
 sure cells have been drifted to this point before they get updated.

---
 src/active.h        | 28 ++++++++++++++++++++++++++++
 src/runner_doiact.h | 18 ++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/src/active.h b/src/active.h
index 17adfd07d2..f0aba74df4 100644
--- a/src/active.h
+++ b/src/active.h
@@ -28,6 +28,34 @@
 #include "engine.h"
 #include "part.h"
 
+/**
+ * @brief Check that a cell been drifted to the current time.
+ *
+ * Only used for debugging. Calls error() if the cell has not
+ * been drifted. Does nothing if SWIFT_DEBUG_CHECKS is not defined.
+ *
+ * @param c The #cell.
+ * @param e The #engine containing information about the current time.
+ */
+__attribute__((always_inline)) INLINE static void cell_is_drifted(
+    const struct cell *c, const struct engine *e) {
+
+#ifdef SWIFT_DEBUG_CHECKS
+  if (c->ti_old > e->ti_current)
+    error(
+        "Cell has been drifted too far forward in time! c->ti_old=%d "
+        "e->ti_current=%d",
+        c->ti_old, e->ti_current);
+
+  if (c->ti_old != e->ti_current) {
+    error(
+        "Cell has not been drifted to the current time c->ti_old=%d, "
+        "e->ti_current=%d",
+        c->ti_old, e->ti_current);
+  }
+#endif
+}
+
 /**
  * @brief Does a cell contain any active particle ?
  *
diff --git a/src/runner_doiact.h b/src/runner_doiact.h
index c067d3bc9a..f4183c8024 100644
--- a/src/runner_doiact.h
+++ b/src/runner_doiact.h
@@ -721,6 +721,11 @@ void DOPAIR1(struct runner *r, struct cell *ci, struct cell *cj) {
   /* Anything to do here? */
   if (!cell_is_active(ci, e) && !cell_is_active(cj, e)) return;
 
+#ifdef SWIFT_DEBUG_CHECKS
+  cell_is_drifted(ci, e);
+  cell_is_drifted(cj, e);
+#endif
+
   /* Get the sort ID. */
   double shift[3] = {0.0, 0.0, 0.0};
   const int sid = space_getsid(e->s, &ci, &cj, shift);
@@ -913,6 +918,11 @@ void DOPAIR2(struct runner *r, struct cell *ci, struct cell *cj) {
   /* Anything to do here? */
   if (!cell_is_active(ci, e) && !cell_is_active(cj, e)) return;
 
+#ifdef SWIFT_DEBUG_CHECKS
+  cell_is_drifted(ci, e);
+  cell_is_drifted(cj, e);
+#endif
+
   /* Get the shift ID. */
   double shift[3] = {0.0, 0.0, 0.0};
   const int sid = space_getsid(e->s, &ci, &cj, shift);
@@ -1303,6 +1313,10 @@ void DOSELF1(struct runner *r, struct cell *restrict c) {
 
   if (!cell_is_active(c, e)) return;
 
+#ifdef SWIFT_DEBUG_CHECKS
+  cell_is_drifted(ci, e);
+#endif
+
   struct part *restrict parts = c->parts;
   const int count = c->count;
 
@@ -1534,6 +1548,10 @@ void DOSELF2(struct runner *r, struct cell *restrict c) {
 
   if (!cell_is_active(c, e)) return;
 
+#ifdef SWIFT_DEBUG_CHECKS
+  cell_is_drifted(ci, e);
+#endif
+
   struct part *restrict parts = c->parts;
   const int count = c->count;
 
-- 
GitLab