From af2ad857a985f1fa73e8ed5839c3102b0690d733 Mon Sep 17 00:00:00 2001
From: Pedro Gonnet <gonnet@google.com>
Date: Thu, 17 Mar 2016 21:03:23 +0100
Subject: [PATCH] fix the cell linking in cell_split.

---
 src/cell.c  | 6 ++++--
 src/cell.h  | 5 ++++-
 src/space.c | 2 +-
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/cell.c b/src/cell.c
index 6d3aa7ce16..d990ba6f6d 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -339,9 +339,11 @@ void cell_gunlocktree(struct cell *c) {
  * @brief Sort the parts into eight bins along the given pivots.
  *
  * @param c The #cell array to be sorted.
+ * @param parts_offset Offset of the cell parts array relative to the
+ *        space's parts array, i.e. c->parts - s->parts.
  */
 
-void cell_split(struct cell *c) {
+void cell_split(struct cell *c, ptrdiff_t parts_offset) {
 
   int i, j;
   const int count = c->count, gcount = c->gcount;
@@ -452,7 +454,7 @@ void cell_split(struct cell *c) {
   /* Re-link the gparts. */
   for (int k = 0; k < count; k++)
     if (parts[k].gpart != NULL) {
-      parts[k].gpart->id_or_neg_offset = -k;
+      parts[k].gpart->id_or_neg_offset = -(k + parts_offset);
     }
 
   /* Verify that _all_ the parts have been assigned to a cell. */
diff --git a/src/cell.h b/src/cell.h
index b0451b311f..857aa92829 100644
--- a/src/cell.h
+++ b/src/cell.h
@@ -20,6 +20,9 @@
 #define SWIFT_CELL_H
 
 /* Includes. */
+#include <stddef.h>
+
+/* Local includes. */
 #include "lock.h"
 #include "multipole.h"
 #include "part.h"
@@ -167,7 +170,7 @@ struct cell {
   ((int)(k) + (cdim)[2] * ((int)(j) + (cdim)[1] * (int)(i)))
 
 /* Function prototypes. */
-void cell_split(struct cell *c);
+void cell_split(struct cell *c, ptrdiff_t parts_offset);
 int cell_locktree(struct cell *c);
 void cell_unlocktree(struct cell *c);
 int cell_glocktree(struct cell *c);
diff --git a/src/space.c b/src/space.c
index 286ded6b4a..ef825545d9 100644
--- a/src/space.c
+++ b/src/space.c
@@ -1100,7 +1100,7 @@ void space_do_split(struct space *s, struct cell *c) {
     }
 
     /* Split the cell data. */
-    cell_split(c);
+    cell_split(c, c->parts - s->parts);
 
     /* Remove any progeny with zero parts. */
     for (int k = 0; k < 8; k++)
-- 
GitLab