From 1dbaf1bd5fc8661eed45f1eaeb589d37ad0c7835 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Sat, 20 Aug 2016 00:38:39 +0100
Subject: [PATCH] Allow users to drift all particles via an engine policy

---
 README                         |  4 ++--
 examples/main.c                | 12 ++++++++++--
 src/engine.c                   | 13 +++++++++----
 src/engine.h                   |  3 ++-
 src/hydro/Gadget2/hydro_iact.h |  4 ++--
 src/runner.c                   |  8 ++++----
 src/serial_io.c                |  4 ++--
 src/timestep.h                 |  8 +++++---
 8 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/README b/README
index cd2a397a18..2fc16c28f5 100644
--- a/README
+++ b/README
@@ -22,12 +22,12 @@ Valid options are:
   -d          Dry run. Read the parameter file, allocate memory but does not read 
               the particles from ICs and exit before the start of time integration.
               Allows user to check validy of parameter and IC files as well as memory limits.
+  -D          Always drift all particles even the ones far from active particles.
   -e          Enable floating-point exceptions (debugging mode)
   -f    {int} Overwrite the CPU frequency (Hz) to be used for time measurements
   -g          Run with an external gravitational potential
   -G          Run with self-gravity
-  -n    {int} Execute a fixed number of time steps. When unset use the time_end
-              parameter to stop. 
+  -n    {int} Execute a fixed number of time steps. When unset use the time_end parameter to stop. 
   -s          Run with SPH
   -t    {int} The number of threads to use on each MPI rank. Defaults to 1 if not specified.
   -v     [12] Increase the level of verbosity 1: MPI-rank 0 writes 
diff --git a/examples/main.c b/examples/main.c
index e1eace107a..561f20ae3f 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -67,6 +67,8 @@ void print_help_message() {
   printf("  %2s %8s %s\n", "", "",
          "Allows user to check validy of parameter and IC files as well as "
          "memory limits.");
+  printf("  %2s %8s %s\n", "-D", "",
+         "Always drift all particles even the ones far from active particles.");
   printf("  %2s %8s %s\n", "-e", "",
          "Enable floating-point exceptions (debugging mode)");
   printf("  %2s %8s %s\n", "-f", "{int}",
@@ -75,7 +77,8 @@ void print_help_message() {
          "Run with an external gravitational potential");
   printf("  %2s %8s %s\n", "-G", "", "Run with self-gravity");
   printf("  %2s %8s %s\n", "-n", "{int}",
-         "Execute a fixed number of time steps");
+         "Execute a fixed number of time steps. When unset use the time_end "
+         "parameter to stop.");
   printf("  %2s %8s %s\n", "-s", "", "Run with SPH");
   printf("  %2s %8s %s\n", "-t", "{int}",
          "The number of threads to use on each MPI rank. Defaults to 1 if not "
@@ -146,6 +149,7 @@ int main(int argc, char *argv[]) {
   int with_self_gravity = 0;
   int with_hydro = 0;
   int with_fp_exceptions = 0;
+  int with_drift_all = 0;
   int verbose = 0;
   int nr_threads = 1;
   char paramFileName[200] = "";
@@ -153,7 +157,7 @@ int main(int argc, char *argv[]) {
 
   /* Parse the parameters */
   int c;
-  while ((c = getopt(argc, argv, "acdef:gGhn:st:v:y:")) != -1) switch (c) {
+  while ((c = getopt(argc, argv, "acdDef:gGhn:st:v:y:")) != -1) switch (c) {
       case 'a':
         with_aff = 1;
         break;
@@ -163,6 +167,9 @@ int main(int argc, char *argv[]) {
       case 'd':
         dry_run = 1;
         break;
+      case 'D':
+        with_drift_all = 1;
+        break;
       case 'e':
         with_fp_exceptions = 1;
         break;
@@ -438,6 +445,7 @@ int main(int argc, char *argv[]) {
 
   /* Construct the engine policy */
   int engine_policies = ENGINE_POLICY | engine_policy_steal;
+  if (with_drift_all) engine_policies |= engine_policy_drift_all;
   if (with_hydro) engine_policies |= engine_policy_hydro;
   if (with_self_gravity) engine_policies |= engine_policy_self_gravity;
   if (with_external_gravity) engine_policies |= engine_policy_external_gravity;
diff --git a/src/engine.c b/src/engine.c
index 348b8b02a7..75cccd7eee 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -68,7 +68,7 @@
 #include "units.h"
 #include "version.h"
 
-const char *engine_policy_names[13] = {"none",
+const char *engine_policy_names[14] = {"none",
                                        "rand",
                                        "steal",
                                        "keep",
@@ -80,7 +80,8 @@ const char *engine_policy_names[13] = {"none",
                                        "hydro",
                                        "self_gravity",
                                        "external_gravity",
-                                       "cosmology_integration"};
+                                       "cosmology_integration",
+                                       "drift_all"};
 
 /** The rank of the engine as a global variable (for messages). */
 int engine_rank;
@@ -2093,7 +2094,9 @@ void engine_prepare(struct engine *e) {
     e->drift_all = 1;
     threadpool_map(&e->threadpool, runner_do_drift_mapper, e->s->cells,
                    e->s->nr_cells, sizeof(struct cell), 1, e);
-    e->drift_all = 0;
+
+    /* Restore the default drifting policy */
+    e->drift_all = (e->policy & engine_policy_drift_all);
 
     /* And now rebuild */
     engine_rebuild(e);
@@ -2479,7 +2482,9 @@ void engine_step(struct engine *e) {
     e->drift_all = 1;
     threadpool_map(&e->threadpool, runner_do_drift_mapper, e->s->cells,
                    e->s->nr_cells, sizeof(struct cell), 1, e);
-    e->drift_all = 0;
+
+    /* Restore the default drifting policy */
+    e->drift_all = (e->policy & engine_policy_drift_all);
 
     /* Dump... */
     engine_dump_snapshot(e);
diff --git a/src/engine.h b/src/engine.h
index 18055279e9..3d688925e7 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -61,7 +61,8 @@ enum engine_policy {
   engine_policy_hydro = (1 << 8),
   engine_policy_self_gravity = (1 << 9),
   engine_policy_external_gravity = (1 << 10),
-  engine_policy_cosmology = (1 << 11)
+  engine_policy_cosmology = (1 << 11),
+  engine_policy_drift_all = (1 << 12)
 };
 
 extern const char *engine_policy_names[];
diff --git a/src/hydro/Gadget2/hydro_iact.h b/src/hydro/Gadget2/hydro_iact.h
index bec6b004a4..0108e0663c 100644
--- a/src/hydro/Gadget2/hydro_iact.h
+++ b/src/hydro/Gadget2/hydro_iact.h
@@ -432,7 +432,7 @@ __attribute__((always_inline)) INLINE static void runner_iact_force(
   const float balsara_j = pj->force.balsara;
 
   /* Are the particles moving towards each others ? */
-  const float omega_ij = (dvdr < 0.f) ? dvdr: 0.f;
+  const float omega_ij = (dvdr < 0.f) ? dvdr : 0.f;
   const float mu_ij = fac_mu * r_inv * omega_ij; /* This is 0 or negative */
 
   /* Signal velocity */
@@ -707,7 +707,7 @@ __attribute__((always_inline)) INLINE static void runner_iact_nonsym_force(
   const float balsara_j = pj->force.balsara;
 
   /* Are the particles moving towards each others ? */
-  const float omega_ij = (dvdr < 0.f) ? dvdr: 0.f;
+  const float omega_ij = (dvdr < 0.f) ? dvdr : 0.f;
   const float mu_ij = fac_mu * r_inv * omega_ij; /* This is 0 or negative */
 
   /* Signal velocity */
diff --git a/src/runner.c b/src/runner.c
index 13c8fa6ec1..8434b3445b 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -487,8 +487,8 @@ void runner_do_ghost(struct runner *r, struct cell *c) {
           h_corr = (target_wcount - p->density.wcount) / p->density.wcount_dh;
 
           /* Truncate to the range [ -p->h/2 , p->h ]. */
-	  h_corr = (h_corr < p->h) ? h_corr : p->h;
-	  h_corr = (h_corr > -0.5f * p->h) ? h_corr : -0.5f * p->h;
+          h_corr = (h_corr < p->h) ? h_corr : p->h;
+          h_corr = (h_corr > -0.5f * p->h) ? h_corr : -0.5f * p->h;
         }
 
         /* Did we get the right number density? */
@@ -626,7 +626,7 @@ static void runner_do_drift(struct cell *c, struct engine *e) {
       const float dx2 = gp->x_diff[0] * gp->x_diff[0] +
                         gp->x_diff[1] * gp->x_diff[1] +
                         gp->x_diff[2] * gp->x_diff[2];
-      dx2_max = (dx2_max  > dx2) ? dx2_max : dx2;
+      dx2_max = (dx2_max > dx2) ? dx2_max : dx2;
     }
 
     /* Loop over all the particles in the cell (more work for these !) */
@@ -644,7 +644,7 @@ static void runner_do_drift(struct cell *c, struct engine *e) {
       const float dx2 = xp->x_diff[0] * xp->x_diff[0] +
                         xp->x_diff[1] * xp->x_diff[1] +
                         xp->x_diff[2] * xp->x_diff[2];
-      dx2_max = (dx2_max  > dx2) ? dx2_max : dx2;
+      dx2_max = (dx2_max > dx2) ? dx2_max : dx2;
 
       /* Maximal smoothing length */
       h_max = (h_max > p->h) ? h_max : p->h;
diff --git a/src/serial_io.c b/src/serial_io.c
index 83f1f35746..d0618ea93e 100644
--- a/src/serial_io.c
+++ b/src/serial_io.c
@@ -225,11 +225,11 @@ void prepareArray(struct engine* e, hid_t grp, char* fileName, FILE* xmfFile,
   }
 
   /* Impose data compression */
-  if(e->snapshotCompression > 0) {
+  if (e->snapshotCompression > 0) {
     h_err = H5Pset_deflate(h_prop, e->snapshotCompression);
     if (h_err < 0) {
       error("Error while setting compression options for field '%s'.",
-	    props.name);
+            props.name);
     }
   }
 
diff --git a/src/timestep.h b/src/timestep.h
index 582f10a658..d92f88d064 100644
--- a/src/timestep.h
+++ b/src/timestep.h
@@ -74,7 +74,8 @@ __attribute__((always_inline)) INLINE static int get_gpart_timestep(
   /*     gravity_compute_timestep_self(e->physical_constants, gp); */
   const float new_dt_self = FLT_MAX;  // MATTHIEU
 
-  float new_dt = (new_dt_external < new_dt_self) ? new_dt_external : new_dt_self;
+  float new_dt =
+      (new_dt_external < new_dt_self) ? new_dt_external : new_dt_self;
 
   /* Limit timestep within the allowed range */
   new_dt = (new_dt < e->dt_max) ? new_dt : e->dt_max;
@@ -111,11 +112,12 @@ __attribute__((always_inline)) INLINE static int get_part_timestep(
     /*     gravity_compute_timestep_self(e->physical_constants, p->gpart); */
     const float new_dt_self = FLT_MAX;  // MATTHIEU
 
-    new_dt_grav = (new_dt_external < new_dt_self) ? new_dt_external : new_dt_self;
+    new_dt_grav =
+        (new_dt_external < new_dt_self) ? new_dt_external : new_dt_self;
   }
 
   /* Final time-step is minimum of hydro and gravity */
-  float new_dt = (new_dt_hydro < new_dt_grav) ? new_dt_hydro: new_dt_grav;
+  float new_dt = (new_dt_hydro < new_dt_grav) ? new_dt_hydro : new_dt_grav;
 
   /* Limit change in h */
   const float dt_h_change =
-- 
GitLab