From fa54e95cc63ddcdcd8ec764136db8552dbce3a9b Mon Sep 17 00:00:00 2001
From: "Peter W. Draper" <p.w.draper@durham.ac.uk>
Date: Fri, 2 Sep 2016 17:00:22 +0100
Subject: [PATCH] Drift all particles before repartitioning

Drifting them afterwards can result in some cells having inconsistent particle lists, so this must be done first
---
 examples/MultiTypes/multiTypes.yml |  3 ++-
 examples/main.c                    |  4 ++--
 src/engine.c                       | 25 +++++++++----------------
 3 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/examples/MultiTypes/multiTypes.yml b/examples/MultiTypes/multiTypes.yml
index 28d02fefa8..f5a5dbc808 100644
--- a/examples/MultiTypes/multiTypes.yml
+++ b/examples/MultiTypes/multiTypes.yml
@@ -39,4 +39,5 @@ PointMass:
   position_x:      50.     # location of external point mass in internal units
   position_y:      50.
   position_z:      50.	
-  mass:            1e10     # mass of external point mass in internal units
+  mass:            1e11     # mass of external point mass in internal units
+  timestep_mult:   1e-2
diff --git a/examples/main.c b/examples/main.c
index c2d3e3b8b9..615377b33f 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -492,7 +492,7 @@ int main(int argc, char *argv[]) {
   engine_init_particles(&e, flag_entropy_ICs);
 
   /* Write the state of the system before starting time integration. */
-  engine_dump_snapshot(&e);
+  //engine_dump_snapshot(&e);
 
   /* Legend */
   if (myrank == 0)
@@ -605,7 +605,7 @@ int main(int argc, char *argv[]) {
 #endif
 
   /* Write final output. */
-  engine_dump_snapshot(&e);
+  //engine_dump_snapshot(&e);
 
 #ifdef WITH_MPI
   if ((res = MPI_Finalize()) != MPI_SUCCESS)
diff --git a/src/engine.c b/src/engine.c
index 8c0501ae57..89e33041d0 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -2264,7 +2264,7 @@ void engine_prepare(struct engine *e) {
   /* Run through the tasks and mark as skip or not. */
   int rebuild = (e->forcerebuild || engine_marktasks(e));
 
-/* Collect the values of rebuild from all nodes. */
+  /* Collect the values of rebuild from all nodes. */
 #ifdef WITH_MPI
   int buff = 0;
   if (MPI_Allreduce(&rebuild, &buff, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD) !=
@@ -2273,20 +2273,8 @@ void engine_prepare(struct engine *e) {
   rebuild = buff;
 #endif
 
-  /* Did this not go through? */
-  if (rebuild) {
-
-    /* First drift all particles to the current time */
-    e->drift_all = 1;
-    threadpool_map(&e->threadpool, runner_do_drift_mapper, e->s->cells_top,
-                   e->s->nr_cells, sizeof(struct cell), 1, e);
-
-    /* Restore the default drifting policy */
-    e->drift_all = (e->policy & engine_policy_drift_all);
-
-    /* And now rebuild */
-    engine_rebuild(e);
-  }
+  /* And rebuild if necessary. */
+  if (rebuild) engine_rebuild(e);
 
   /* Re-rank the tasks every now and then. */
   if (e->tasks_age % engine_tasksreweight == 1) {
@@ -2705,10 +2693,15 @@ void engine_step(struct engine *e) {
     e->timeLastStatistics += e->deltaTimeStatistics;
   }
 
-  /* Drift only the necessary particles */
+  /* Drift only the necessary particles, that all means all particles
+   * if we are about to repartition. */
+  e->drift_all = (e->forcerepart != REPART_NONE) || e->drift_all;
   threadpool_map(&e->threadpool, runner_do_drift_mapper, e->s->cells_top,
                  e->s->nr_cells, sizeof(struct cell), 1, e);
 
+  /* Restore the default drifting policy */
+  e->drift_all = (e->policy & engine_policy_drift_all);
+
   /* Re-distribute the particles amongst the nodes? */
   if (e->forcerepart != REPART_NONE) engine_repartition(e);
 
-- 
GitLab