From ed94510090bffd0d75fa26e38c6b55b45e9cbea0 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Thu, 10 Mar 2016 16:56:18 +0000
Subject: [PATCH] space_split() now does its job correctly even with 0 parts

---
 src/space.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/space.c b/src/space.c
index e47fc2f402..d2ca6ca085 100644
--- a/src/space.c
+++ b/src/space.c
@@ -1003,7 +1003,9 @@ void space_map_cells_pre(struct space *s, int full,
 
 void space_do_split(struct space *s, struct cell *c) {
 
-  int k, count = c->count, gcount = c->gcount, maxdepth = 0;
+  const int count = c->count;
+  const int gcount = c->gcount;
+  int maxdepth = 0;
   float h, h_max = 0.0f;
   int ti_end_min = max_nr_timesteps, ti_end_max = 0, ti_end;
   struct cell *temp;
@@ -1020,7 +1022,7 @@ void space_do_split(struct space *s, struct cell *c) {
     c->split = 1;
 
     /* Create the cell's progeny. */
-    for (k = 0; k < 8; k++) {
+    for (int k = 0; k < 8; k++) {
       temp = space_getcell(s);
       temp->count = 0;
       temp->gcount = 0;
@@ -1047,7 +1049,7 @@ void space_do_split(struct space *s, struct cell *c) {
     cell_split(c);
 
     /* Remove any progeny with zero parts. */
-    for (k = 0; k < 8; k++)
+    for (int k = 0; k < 8; k++)
       if (c->progeny[k]->count == 0 && c->progeny[k]->gcount == 0) {
         space_recycle(s, c->progeny[k]);
         c->progeny[k] = NULL;
@@ -1077,8 +1079,7 @@ void space_do_split(struct space *s, struct cell *c) {
     c->maxdepth = c->depth;
 
     /* Get dt_min/dt_max. */
-
-    for (k = 0; k < count; k++) {
+    for (int k = 0; k < count; k++) {
       p = &parts[k];
       xp = &xparts[k];
       xp->x_old[0] = p->x[0];
@@ -1096,7 +1097,10 @@ void space_do_split(struct space *s, struct cell *c) {
   }
 
   /* Set ownership according to the start of the parts array. */
-  c->owner = ((c->parts - s->parts) % s->nr_parts) * s->nr_queues / s->nr_parts;
+  if(count > 0)
+    c->owner = ((c->parts - s->parts) % s->nr_parts) * s->nr_queues / s->nr_parts;
+  else
+    c->owner = ((c->gparts - s->gparts) % s->nr_gparts) * s->nr_queues / s->nr_gparts;
 }
 
 /**
-- 
GitLab