From 87d7aa95382e8b217c98514ac1070b97ec4d8a46 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Sun, 22 May 2016 18:40:54 +0100
Subject: [PATCH] Time between statistics output is to be specified in the
 parameter file

---
 examples/SedovBlast/sedov.yml  |  3 +++
 examples/SodShock/sodShock.yml |  3 +++
 src/engine.c                   | 32 +++++++++++++++++++++-----------
 src/engine.h                   |  4 +++-
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/examples/SedovBlast/sedov.yml b/examples/SedovBlast/sedov.yml
index b5ad8d8555..330ea51f4b 100644
--- a/examples/SedovBlast/sedov.yml
+++ b/examples/SedovBlast/sedov.yml
@@ -32,6 +32,9 @@ Snapshots:
   UnitCurrent_in_cgs:  1   # Amperes
   UnitTemp_in_cgs:     1   # Kelvin
 
+Statistics:
+  delta_time:          1e-3
+  
 # Parameters for the hydrodynamics scheme
 SPH:
   resolution_eta:        1.2348   # Target smoothing length in units of the mean inter-particle separation (1.2348 == 48Ngbs with the cubic spline kernel).
diff --git a/examples/SodShock/sodShock.yml b/examples/SodShock/sodShock.yml
index 7bb1895c15..8818d4b826 100644
--- a/examples/SodShock/sodShock.yml
+++ b/examples/SodShock/sodShock.yml
@@ -32,6 +32,9 @@ Snapshots:
   UnitCurrent_in_cgs:  1   # Amperes
   UnitTemp_in_cgs:     1   # Kelvin
 
+Statistics:
+  delta_time:          1e-2 # Time between statistics output
+  
 # Parameters for the hydrodynamics scheme
 SPH:
   resolution_eta:        1.2348   # Target smoothing length in units of the mean inter-particle separation (1.2348 == 48Ngbs with the cubic spline kernel).
diff --git a/src/engine.c b/src/engine.c
index d8121f0a93..cd3ddf4b21 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -1919,7 +1919,7 @@ void engine_collect_drift(struct cell *c) {
         engine_collect_drift(cp);
 
         /* And update */
-	mass += cp->mass;
+        mass += cp->mass;
         e_kin += cp->e_kin;
         e_int += cp->e_int;
         e_pot += cp->e_pot;
@@ -2008,11 +2008,14 @@ void engine_print_stats(struct engine *e) {
   const double e_tot = e_kin + e_int + e_pot;
 
   /* Print info */
-  fprintf(e->file_stats,
-          "  %6d %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e\n",
-          e->step, e->time, mass, e_tot, e_kin, e_int, e_pot, mom[0], mom[1], mom[2],
-          ang_mom[0], ang_mom[1], ang_mom[2]);
-  fflush(e->file_stats);
+  if (e->nodeID == 0) {
+    fprintf(
+        e->file_stats,
+        "  %6d %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e %14e\n",
+        e->step, e->time, mass, e_tot, e_kin, e_int, e_pot, mom[0], mom[1],
+        mom[2], ang_mom[0], ang_mom[1], ang_mom[2]);
+    fflush(e->file_stats);
+  }
 }
 
 /**
@@ -2193,7 +2196,10 @@ void engine_step(struct engine *e) {
   }
 
   /* Save some statistics */
-  engine_print_stats(e);
+  if (e->time - e->timeLastStatistics >= e->deltaTimeStatistics) {
+    engine_print_stats(e);
+    e->timeLastStatistics += e->deltaTimeStatistics;
+  }
 
   /* Re-distribute the particles amongst the nodes? */
   if (e->forcerepart != REPART_NONE) engine_repartition(e);
@@ -2564,6 +2570,9 @@ void engine_init(struct engine *e, struct space *s,
   e->dt_min = parser_get_param_double(params, "TimeIntegration:dt_min");
   e->dt_max = parser_get_param_double(params, "TimeIntegration:dt_max");
   e->file_stats = NULL;
+  e->deltaTimeStatistics =
+      parser_get_param_double(params, "Statistics:delta_time");
+  e->timeLastStatistics = e->timeBegin - e->deltaTimeStatistics;
   e->verbose = verbose;
   e->count_step = 0;
   e->wallclock_time = 0.f;
@@ -2666,10 +2675,11 @@ void engine_init(struct engine *e, struct space *s,
   /* Open some files */
   if (e->nodeID == 0) {
     e->file_stats = fopen("energy.txt", "w");
-    fprintf(e->file_stats,
-	    "# %6s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s\n",
-	    "Step", "Time", "Mass", "E_tot", "E_kin", "E_int", "E_pot", "p_x", "p_y", "p_z",
-	    "ang_x", "ang_y", "ang_z");
+    fprintf(
+        e->file_stats,
+        "# %6s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s\n",
+        "Step", "Time", "Mass", "E_tot", "E_kin", "E_int", "E_pot", "p_x",
+        "p_y", "p_z", "ang_x", "ang_y", "ang_z");
     fflush(e->file_stats);
   }
 
diff --git a/src/engine.h b/src/engine.h
index 92c7527e3c..8e8f120176 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -147,8 +147,10 @@ struct engine {
   char snapshotBaseName[200];
   struct UnitSystem *snapshotUnits;
 
-  /* File for statistics */
+  /* Statistics information */
   FILE *file_stats;
+  double timeLastStatistics;
+  double deltaTimeStatistics;
 
   /* The current step number. */
   int step;
-- 
GitLab