From ce11b10cbf83de5dac837b1d5f270e74d9440201 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Tue, 25 Sep 2018 17:10:28 +0100
Subject: [PATCH] Add function to convert a spart to a gpart.

---
 src/cell.c   | 32 ++++++++++++++++++++++++++++++--
 src/cell.h   |  4 ++++
 src/runner.c |  2 +-
 3 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/src/cell.c b/src/cell.c
index 51f2cef052..62384b948d 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -3291,8 +3291,8 @@ void cell_remove_spart(const struct engine *e, struct cell *c,
  * @param p The #part to remove.
  * @param xp The extended data of the particle to remove.
  */
-void cell_convert_part_to_dark_matter(const struct engine *e, struct cell *c,
-                                      struct part *p, struct xpart *xp) {
+void cell_convert_part_to_gpart(const struct engine *e, struct cell *c,
+                                struct part *p, struct xpart *xp) {
 
   /* Quick cross-check */
   if (c->nodeID != e->nodeID)
@@ -3309,6 +3309,34 @@ void cell_convert_part_to_dark_matter(const struct engine *e, struct cell *c,
   }
 }
 
+/**
+ * @brief "Remove" a spart particle from the calculation and convert its gpart
+ * friend to a dark matter particle.
+ *
+ * The particle is inhibited and will officially be removed at the next rebuild.
+ *
+ * @param e The #engine running on this node.
+ * @param c The #cell from which to remove the particle.
+ * @param sp The #spart to remove.
+ */
+void cell_convert_spart_to_gpart(const struct engine *e, struct cell *c,
+				 struct spart *sp) {
+
+  /* Quick cross-check */
+  if (c->nodeID != e->nodeID)
+    error("Can't remove a particle in a foreign cell.");
+
+  /* Mark the particle as inhibited */
+  sp->time_bin = time_bin_inhibited;
+
+  if (sp->gpart) {
+    sp->gpart->type = swift_type_dark_matter;
+    sp->gpart->id_or_neg_offset = sp->id;
+  } else {
+    error("Trying to convert part without gpart friend to dark matter!");
+  }
+}
+
 /**
  * @brief Can we use the MM interactions fo a given pair of cells?
  *
diff --git a/src/cell.h b/src/cell.h
index 670c4b777a..559504b798 100644
--- a/src/cell.h
+++ b/src/cell.h
@@ -622,6 +622,10 @@ void cell_remove_gpart(const struct engine *e, struct cell *c,
                        struct gpart *gp);
 void cell_remove_spart(const struct engine *e, struct cell *c,
                        struct spart *sp);
+void cell_convert_part_to_gpart(const struct engine *e, struct cell *c,
+                                struct part *p, struct xpart *xp);
+void cell_convert_spart_to_gpart(const struct engine *e, struct cell *c,
+				 struct spart *sp);
 int cell_can_use_pair_mm(const struct cell *ci, const struct cell *cj,
                          const struct engine *e, const struct space *s);
 int cell_can_use_pair_mm_rebuild(const struct cell *ci, const struct cell *cj,
diff --git a/src/runner.c b/src/runner.c
index bc6bc4f738..f471b51eae 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -2052,7 +2052,7 @@ void runner_do_end_force(struct runner *r, struct cell *c, int timer) {
 
         if (p->rho > 1.5e7 && e->step > 2) {
           message("Removing particle id=%lld rho=%e", p->id, p->rho);
-          cell_remove_part(e, c, p, xp);
+          cell_convert_part_to_gpart(e, c, p, xp);
         }
       }
     }
-- 
GitLab