From 00ea9c0af4d6105c7d09a63bff3633f1cab0fb04 Mon Sep 17 00:00:00 2001
From: James Willis <james.s.willis@durham.ac.uk>
Date: Tue, 29 May 2018 11:40:03 +0100
Subject: [PATCH] Find the root of pi in the outer loop and update it on the
 fly in the inner loop for the brute force version of the FOF.

---
 src/fof.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/fof.c b/src/fof.c
index 15a0012fdc..c9b1133ab2 100644
--- a/src/fof.c
+++ b/src/fof.c
@@ -175,10 +175,12 @@ void fof_search_serial(struct space *s) {
     const double piy = pi->x[1];
     const double piz = pi->x[2];
 
+    /* Find the root of pi. */
+    int root_i = fof_find(i, pid);
+    
     for (size_t j = i + 1; j < nr_gparts; j++) {
 
       /* Find the roots of pi and pj. */
-      const int root_i = fof_find(i, pid);
       const int root_j = fof_find(j, pid);
 
       /* Skip particles in the same group. */
@@ -204,8 +206,13 @@ void fof_search_serial(struct space *s) {
       /* Hit or miss? */
       if (r2 < l_x2) {
 
-        if (root_j < root_i)
+        /* If the root ID of pj is lower than pi's root ID set pi's root to point to pj's. 
+         * Otherwise set pj's to root to point to pi's.*/
+        if (root_j < root_i) {
           pid[root_i] = root_j;
+          /* Update root_i on the fly. */
+          root_i = root_j;
+        }
         else
           pid[root_j] = root_i;
 
-- 
GitLab