From 735da2ce3a91dbd065f78a98b383c69eabf92b94 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Fri, 21 Sep 2018 15:13:26 +0200
Subject: [PATCH] Update the global particle counters in the engine after a
 rebuild.

---
 src/engine.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/engine.c b/src/engine.c
index 94cee6ba99..cd989bb5d0 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -4408,6 +4408,17 @@ void engine_rebuild(struct engine *e, int clean_smoothing_length_values) {
   /* Construct the list of purely local cells */
   space_list_local_cells(e->s);
 
+  /* Update the global counters of particles */
+  long long num_particles[3] = {e->s->nr_parts, e->s->nr_gparts,
+                                e->s->nr_sparts};
+#ifdef WITH_MPI
+  MPI_Allreduce(MPI_IN_PLACE, num_particles, 3, MPI_LONG_LONG, MPI_SUM,
+                MPI_COMM_WORLD);
+#endif
+  e->total_nr_parts = num_particles[0];
+  e->total_nr_gparts = num_particles[1];
+  e->total_nr_sparts = num_particles[2];
+
   /* Re-compute the mesh forces */
   if ((e->policy & engine_policy_self_gravity) && e->s->periodic)
     pm_mesh_compute_potential(e->mesh, e->s, &e->threadpool, e->verbose);
-- 
GitLab