From ec7014d2e0abb1f545bfe44f5bbee2de2e546bef Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Mon, 18 Jul 2016 18:32:37 +0100
Subject: [PATCH] Be verbose about unit conversion when writing snapshots

---
 src/parallel_io.c | 35 ++++++++++++++++++++++++++++++++++-
 src/serial_io.c   | 35 ++++++++++++++++++++++++++++++++++-
 src/single_io.c   | 35 ++++++++++++++++++++++++++++++++++-
 3 files changed, 102 insertions(+), 3 deletions(-)

diff --git a/src/parallel_io.c b/src/parallel_io.c
index 47f2e893f9..5e358c34f7 100644
--- a/src/parallel_io.c
+++ b/src/parallel_io.c
@@ -39,9 +39,9 @@
 #include "common_io.h"
 #include "engine.h"
 #include "error.h"
-#include "hydro_properties.h"
 #include "gravity_io.h"
 #include "hydro_io.h"
+#include "hydro_properties.h"
 #include "io_properties.h"
 #include "kernel_hydro.h"
 #include "part.h"
@@ -706,6 +706,39 @@ void write_output_parallel(struct engine* e, const char* baseName,
   /* Print the system of Units used internally */
   writeUnitSystem(h_file, internal_units, "InternalCodeUnits");
 
+  /* Tell the user if a conversion will be needed */
+  if (e->verbose && mpi_rank == 0) {
+    if (units_are_equal(snapshot_units, internal_units)) {
+
+      message("Snapshot and internal units match. No conversion needed.");
+
+    } else {
+
+      message("Conversion needed from:");
+      message("(Snapshot) Unit system: U_M =      %e g.",
+              snapshot_units->UnitMass_in_cgs);
+      message("(Snapshot) Unit system: U_L =      %e cm.",
+              snapshot_units->UnitLength_in_cgs);
+      message("(Snapshot) Unit system: U_t =      %e s.",
+              snapshot_units->UnitTime_in_cgs);
+      message("(Snapshot) Unit system: U_I =      %e A.",
+              snapshot_units->UnitCurrent_in_cgs);
+      message("(Snapshot) Unit system: U_T =      %e K.",
+              snapshot_units->UnitTemperature_in_cgs);
+      message("to:");
+      message("(internal) Unit system: U_M = %e g.",
+              internal_units->UnitMass_in_cgs);
+      message("(internal) Unit system: U_L = %e cm.",
+              internal_units->UnitLength_in_cgs);
+      message("(internal) Unit system: U_t = %e s.",
+              internal_units->UnitTime_in_cgs);
+      message("(internal) Unit system: U_I = %e A.",
+              internal_units->UnitCurrent_in_cgs);
+      message("(internal) Unit system: U_T = %e K.",
+              internal_units->UnitTemperature_in_cgs);
+    }
+  }
+
   /* Loop over all particle types */
   for (int ptype = 0; ptype < NUM_PARTICLE_TYPES; ptype++) {
 
diff --git a/src/serial_io.c b/src/serial_io.c
index 7bb829174c..0134fa3e52 100644
--- a/src/serial_io.c
+++ b/src/serial_io.c
@@ -39,9 +39,9 @@
 #include "common_io.h"
 #include "engine.h"
 #include "error.h"
-#include "hydro_properties.h"
 #include "gravity_io.h"
 #include "hydro_io.h"
+#include "hydro_properties.h"
 #include "io_properties.h"
 #include "kernel_hydro.h"
 #include "part.h"
@@ -764,6 +764,39 @@ void write_output_serial(struct engine* e, const char* baseName,
     /* Print the system of Units used internally */
     writeUnitSystem(h_file, internal_units, "InternalCodeUnits");
 
+    /* Tell the user if a conversion will be needed */
+    if (e->verbose) {
+      if (units_are_equal(snapshot_units, internal_units)) {
+
+        message("Snapshot and internal units match. No conversion needed.");
+
+      } else {
+
+        message("Conversion needed from:");
+        message("(Snapshot) Unit system: U_M =      %e g.",
+                snapshot_units->UnitMass_in_cgs);
+        message("(Snapshot) Unit system: U_L =      %e cm.",
+                snapshot_units->UnitLength_in_cgs);
+        message("(Snapshot) Unit system: U_t =      %e s.",
+                snapshot_units->UnitTime_in_cgs);
+        message("(Snapshot) Unit system: U_I =      %e A.",
+                snapshot_units->UnitCurrent_in_cgs);
+        message("(Snapshot) Unit system: U_T =      %e K.",
+                snapshot_units->UnitTemperature_in_cgs);
+        message("to:");
+        message("(internal) Unit system: U_M = %e g.",
+                internal_units->UnitMass_in_cgs);
+        message("(internal) Unit system: U_L = %e cm.",
+                internal_units->UnitLength_in_cgs);
+        message("(internal) Unit system: U_t = %e s.",
+                internal_units->UnitTime_in_cgs);
+        message("(internal) Unit system: U_I = %e A.",
+                internal_units->UnitCurrent_in_cgs);
+        message("(internal) Unit system: U_T = %e K.",
+                internal_units->UnitTemperature_in_cgs);
+      }
+    }
+
     /* Loop over all particle types */
     for (int ptype = 0; ptype < NUM_PARTICLE_TYPES; ptype++) {
 
diff --git a/src/single_io.c b/src/single_io.c
index de7b968123..3f63473719 100644
--- a/src/single_io.c
+++ b/src/single_io.c
@@ -38,9 +38,9 @@
 #include "common_io.h"
 #include "engine.h"
 #include "error.h"
-#include "hydro_properties.h"
 #include "gravity_io.h"
 #include "hydro_io.h"
+#include "hydro_properties.h"
 #include "io_properties.h"
 #include "kernel_hydro.h"
 #include "part.h"
@@ -610,6 +610,39 @@ void write_output_single(struct engine* e, const char* baseName,
   /* Print the system of Units used internally */
   writeUnitSystem(h_file, internal_units, "InternalCodeUnits");
 
+  /* Tell the user if a conversion will be needed */
+  if (e->verbose) {
+    if (units_are_equal(snapshot_units, internal_units)) {
+
+      message("Snapshot and internal units match. No conversion needed.");
+
+    } else {
+
+      message("Conversion needed from:");
+      message("(Snapshot) Unit system: U_M =      %e g.",
+              snapshot_units->UnitMass_in_cgs);
+      message("(Snapshot) Unit system: U_L =      %e cm.",
+              snapshot_units->UnitLength_in_cgs);
+      message("(Snapshot) Unit system: U_t =      %e s.",
+              snapshot_units->UnitTime_in_cgs);
+      message("(Snapshot) Unit system: U_I =      %e A.",
+              snapshot_units->UnitCurrent_in_cgs);
+      message("(Snapshot) Unit system: U_T =      %e K.",
+              snapshot_units->UnitTemperature_in_cgs);
+      message("to:");
+      message("(internal) Unit system: U_M = %e g.",
+              internal_units->UnitMass_in_cgs);
+      message("(internal) Unit system: U_L = %e cm.",
+              internal_units->UnitLength_in_cgs);
+      message("(internal) Unit system: U_t = %e s.",
+              internal_units->UnitTime_in_cgs);
+      message("(internal) Unit system: U_I = %e A.",
+              internal_units->UnitCurrent_in_cgs);
+      message("(internal) Unit system: U_T = %e K.",
+              internal_units->UnitTemperature_in_cgs);
+    }
+  }
+
   /* Loop over all particle types */
   for (int ptype = 0; ptype < NUM_PARTICLE_TYPES; ptype++) {
 
-- 
GitLab