From f260f0c6ac33bb11bfcae2960fa0c03d78ca395a Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Wed, 21 Nov 2018 14:54:13 +0100
Subject: [PATCH] When linking the hierarchical gravity tasks, do not link to
 the parents if we are at the super-level.

---
 src/engine_maketasks.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/engine_maketasks.c b/src/engine_maketasks.c
index 2a5fc530ed..68841aa599 100644
--- a/src/engine_maketasks.c
+++ b/src/engine_maketasks.c
@@ -1115,9 +1115,19 @@ void engine_link_gravity_tasks(struct engine *e) {
     const enum task_types t_type = t->type;
     const enum task_subtypes t_subtype = t->subtype;
 
-    struct cell *ci_parent = (ci->parent != NULL) ? ci->parent : ci;
-    struct cell *cj_parent =
-        (cj != NULL && cj->parent != NULL) ? cj->parent : cj;
+    /* Pointers to the parent cells for tasks going up and down the tree
+     * In the case where we are at the super-level we don't
+     * want the parent as no tasks are defined above that level. */
+    struct cell *ci_parent, *cj_parent;
+    if (ci->parent != NULL && ci->grav.super != ci)
+      ci_parent = ci->parent;
+    else
+      ci_parent = ci;
+
+    if (cj != NULL && cj->parent != NULL && cj->grav.super != cj)
+      cj_parent = cj->parent;
+    else
+      cj_parent = cj;
 
 /* Node ID (if running with MPI) */
 #ifdef WITH_MPI
-- 
GitLab