From ef7b5f19c136c83d4c1d9ac83e3a8d1be58113d4 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Thu, 4 Jul 2019 18:19:57 +0200
Subject: [PATCH] Merge onto the most massive of the two BHs.

---
 src/black_holes/EAGLE/black_holes_iact.h   | 17 +++++++++++------
 src/black_holes/EAGLE/black_holes_struct.h |  5 +++++
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/black_holes/EAGLE/black_holes_iact.h b/src/black_holes/EAGLE/black_holes_iact.h
index 0bfe2a51e8..bd3d8f0ce2 100644
--- a/src/black_holes/EAGLE/black_holes_iact.h
+++ b/src/black_holes/EAGLE/black_holes_iact.h
@@ -219,11 +219,12 @@ runner_iact_nonsym_bh_bh_swallow(const float r2, const float *dx,
 
   const float G = 43.;  // MATTHIEU: Fix this!!!
 
-  /* The BH with the smaller ID will be merged onto the one with the larger ID
-   */
-  if (bj->id < bi->id) {
-
-    message("ID is smaller");
+  /* The BH with the smaller mass will be merged onto the one with the
+   * larger mass.
+   * To avoid rounding issues, we additionally check for IDs if the BHs
+   * have the exact same mass. */
+  if ((bj->subgrid_mass < bi->subgrid_mass) ||
+      (bj->subgrid_mass == bi->subgrid_mass && bj->id < bi->id)) {
 
     /* Merge if gravitationally bound
      * Note that we use the kernel support here as the size and not just the
@@ -234,11 +235,15 @@ runner_iact_nonsym_bh_bh_swallow(const float r2, const float *dx,
 
       /* This particle is swallowed by the BH with the largest ID of all the
        * candidates wanting to swallow it */
-      if (bj->merger_data.swallow_id < bi->id) {
+      if ((bj->merger_data.swallow_mass < bi->subgrid_mass) ||
+          (bj->merger_data.swallow_mass == bi->subgrid_mass &&
+           bj->merger_data.swallow_id < bi->id)) {
 
         message("BH %lld wants to swallow BH particle %lld", bi->id, bj->id);
 
         bj->merger_data.swallow_id = bi->id;
+        bj->merger_data.swallow_mass = bi->subgrid_mass;
+
       } else {
 
         message(
diff --git a/src/black_holes/EAGLE/black_holes_struct.h b/src/black_holes/EAGLE/black_holes_struct.h
index 96d9afd4d7..98f5380e9f 100644
--- a/src/black_holes/EAGLE/black_holes_struct.h
+++ b/src/black_holes/EAGLE/black_holes_struct.h
@@ -35,6 +35,9 @@ struct black_holes_bpart_data {
 
   /*! ID of the black-hole that will swallow this #bpart. */
   long long swallow_id;
+
+  /*! Mass of the black-hole that will swallow this #bpart. */
+  float swallow_mass;
 };
 
 /**
@@ -82,6 +85,7 @@ __attribute__((always_inline)) INLINE static void
 black_holes_mark_bpart_as_not_swallowed(struct black_holes_bpart_data* p_data) {
 
   p_data->swallow_id = -1;
+  p_data->swallow_mass = 0.f;
 }
 
 /**
@@ -94,6 +98,7 @@ __attribute__((always_inline)) INLINE static void
 black_holes_mark_bpart_as_merged(struct black_holes_bpart_data* p_data) {
 
   p_data->swallow_id = -2;
+  p_data->swallow_mass = -1.f;
 }
 
 /**
-- 
GitLab