From 43d2c7e0554e8aaf21d04bc0cc4eba449dc38e19 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Thu, 18 Aug 2016 18:50:54 +0100
Subject: [PATCH] Check that the code and ICs have the same dimensitonality

---
 examples/BigCosmoVolume/makeIC.py                  |  1 +
 examples/ExternalPointMass/makeIC.py               |  2 +-
 examples/GreshoVortex_2D/makeIC.py                 |  1 +
 examples/IsothermalPotential/GravityOnly/makeIC.py |  2 +-
 examples/KelvinHelmholtz_2D/makeIC.py              |  1 +
 examples/MultiTypes/makeIC.py                      |  1 +
 examples/PerturbedBox_2D/makeIC.py                 |  1 +
 examples/PerturbedBox_3D/makeIC.py                 |  1 +
 examples/SedovBlast_1D/makeIC.py                   |  1 +
 examples/SedovBlast_2D/makeIC.py                   |  1 +
 examples/SedovBlast_3D/makeIC.py                   |  1 +
 examples/SodShock_1D/makeIC.py                     |  1 +
 examples/SodShock_2D/makeIC.py                     |  1 +
 examples/SodShock_3D/makeIC.py                     |  1 +
 examples/SquareTest_2D/makeIC.py                   |  1 +
 examples/UniformBox_2D/makeIC.py                   |  1 +
 examples/UniformBox_3D/makeIC.py                   |  1 +
 examples/UniformDMBox/makeIC.py                    |  1 +
 src/parallel_io.c                                  | 11 +++++++++++
 src/serial_io.c                                    | 11 +++++++++++
 src/single_io.c                                    | 11 +++++++++++
 21 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/examples/BigCosmoVolume/makeIC.py b/examples/BigCosmoVolume/makeIC.py
index 411ac54b41..c141337c06 100644
--- a/examples/BigCosmoVolume/makeIC.py
+++ b/examples/BigCosmoVolume/makeIC.py
@@ -133,6 +133,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/ExternalPointMass/makeIC.py b/examples/ExternalPointMass/makeIC.py
index 37fc46a924..3261833989 100644
--- a/examples/ExternalPointMass/makeIC.py
+++ b/examples/ExternalPointMass/makeIC.py
@@ -83,7 +83,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = [0, 0, 0, 0, 0, 0]
-
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/GreshoVortex_2D/makeIC.py b/examples/GreshoVortex_2D/makeIC.py
index 96fa7f098d..4f4ec3407b 100644
--- a/examples/GreshoVortex_2D/makeIC.py
+++ b/examples/GreshoVortex_2D/makeIC.py
@@ -87,6 +87,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFileOutputsPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = [0, 0, 0, 0, 0, 0]
+grp.attrs["Dimension"] = 2
 
 #Runtime parameters
 grp = fileOutput.create_group("/RuntimePars")
diff --git a/examples/IsothermalPotential/GravityOnly/makeIC.py b/examples/IsothermalPotential/GravityOnly/makeIC.py
index 88115668bd..07993f19d4 100644
--- a/examples/IsothermalPotential/GravityOnly/makeIC.py
+++ b/examples/IsothermalPotential/GravityOnly/makeIC.py
@@ -101,7 +101,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = [0, 0, 0, 0, 0, 0]
-
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/KelvinHelmholtz_2D/makeIC.py b/examples/KelvinHelmholtz_2D/makeIC.py
index 5c8632dea5..bd0f39ed90 100644
--- a/examples/KelvinHelmholtz_2D/makeIC.py
+++ b/examples/KelvinHelmholtz_2D/makeIC.py
@@ -120,6 +120,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFileOutputsPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = [0, 0, 0, 0, 0, 0]
+grp.attrs["Dimension"] = 2
 
 #Runtime parameters
 grp = fileOutput.create_group("/RuntimePars")
diff --git a/examples/MultiTypes/makeIC.py b/examples/MultiTypes/makeIC.py
index cf889f9b6e..229450b67c 100644
--- a/examples/MultiTypes/makeIC.py
+++ b/examples/MultiTypes/makeIC.py
@@ -61,6 +61,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, massDM, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/PerturbedBox_2D/makeIC.py b/examples/PerturbedBox_2D/makeIC.py
index 20b720419f..87a4151777 100644
--- a/examples/PerturbedBox_2D/makeIC.py
+++ b/examples/PerturbedBox_2D/makeIC.py
@@ -84,6 +84,7 @@ grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = [0, 0, 0, 0, 0, 0]
 grp.attrs["NumPart_Total"] = numPart
+grp.attrs["Dimension"] = 2
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/PerturbedBox_3D/makeIC.py b/examples/PerturbedBox_3D/makeIC.py
index cc7fffe14d..1b0fc284e4 100644
--- a/examples/PerturbedBox_3D/makeIC.py
+++ b/examples/PerturbedBox_3D/makeIC.py
@@ -86,6 +86,7 @@ grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = [0, 0, 0, 0, 0, 0]
 grp.attrs["NumPart_Total"] = numPart
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/SedovBlast_1D/makeIC.py b/examples/SedovBlast_1D/makeIC.py
index 4bdf69eee9..0c3a311703 100644
--- a/examples/SedovBlast_1D/makeIC.py
+++ b/examples/SedovBlast_1D/makeIC.py
@@ -70,6 +70,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 1
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/SedovBlast_2D/makeIC.py b/examples/SedovBlast_2D/makeIC.py
index 05233576f6..0e83c7b19b 100644
--- a/examples/SedovBlast_2D/makeIC.py
+++ b/examples/SedovBlast_2D/makeIC.py
@@ -70,6 +70,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 2
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/SedovBlast_3D/makeIC.py b/examples/SedovBlast_3D/makeIC.py
index 3c1e36a74b..e1b743c6cd 100644
--- a/examples/SedovBlast_3D/makeIC.py
+++ b/examples/SedovBlast_3D/makeIC.py
@@ -70,6 +70,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/SodShock_1D/makeIC.py b/examples/SodShock_1D/makeIC.py
index e024188e86..a5c7f03b24 100644
--- a/examples/SodShock_1D/makeIC.py
+++ b/examples/SodShock_1D/makeIC.py
@@ -90,6 +90,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 1
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/SodShock_2D/makeIC.py b/examples/SodShock_2D/makeIC.py
index ac2b9ab45f..fdc1610df8 100644
--- a/examples/SodShock_2D/makeIC.py
+++ b/examples/SodShock_2D/makeIC.py
@@ -96,6 +96,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 2
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/SodShock_3D/makeIC.py b/examples/SodShock_3D/makeIC.py
index 84283732af..c71c07c6c9 100644
--- a/examples/SodShock_3D/makeIC.py
+++ b/examples/SodShock_3D/makeIC.py
@@ -96,6 +96,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/SquareTest_2D/makeIC.py b/examples/SquareTest_2D/makeIC.py
index 2cb624f994..186e653124 100644
--- a/examples/SquareTest_2D/makeIC.py
+++ b/examples/SquareTest_2D/makeIC.py
@@ -94,6 +94,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = [0, 0, 0, 0, 0, 0]
+grp.attrs["Dimension"] = 2
 
 #Runtime parameters
 grp = fileOutput.create_group("/RuntimePars")
diff --git a/examples/UniformBox_2D/makeIC.py b/examples/UniformBox_2D/makeIC.py
index 41b7d695a2..642896c6ec 100644
--- a/examples/UniformBox_2D/makeIC.py
+++ b/examples/UniformBox_2D/makeIC.py
@@ -83,6 +83,7 @@ grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = [0, 0, 0, 0, 0, 0]
 grp.attrs["NumPart_Total"] = numPart
+grp.attrs["Dimension"] = 2
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/UniformBox_3D/makeIC.py b/examples/UniformBox_3D/makeIC.py
index 1484f60596..01e37c67b6 100644
--- a/examples/UniformBox_3D/makeIC.py
+++ b/examples/UniformBox_3D/makeIC.py
@@ -55,6 +55,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/examples/UniformDMBox/makeIC.py b/examples/UniformDMBox/makeIC.py
index 2aee89798a..8e03250001 100644
--- a/examples/UniformDMBox/makeIC.py
+++ b/examples/UniformDMBox/makeIC.py
@@ -51,6 +51,7 @@ grp.attrs["Time"] = 0.0
 grp.attrs["NumFilesPerSnapshot"] = 1
 grp.attrs["MassTable"] = [0.0, mass, 0.0, 0.0, 0.0, 0.0]
 grp.attrs["Flag_Entropy_ICs"] = 0
+grp.attrs["Dimension"] = 3
 
 #Runtime parameters
 grp = file.create_group("/RuntimePars")
diff --git a/src/parallel_io.c b/src/parallel_io.c
index 262ab7d9e4..d543aac7e7 100644
--- a/src/parallel_io.c
+++ b/src/parallel_io.c
@@ -37,6 +37,7 @@
 
 /* Local includes. */
 #include "common_io.h"
+#include "dimension.h"
 #include "engine.h"
 #include "error.h"
 #include "gravity_io.h"
@@ -383,6 +384,7 @@ void read_ic_parallel(char* fileName, const struct UnitSystem* internal_units,
   size_t N[NUM_PARTICLE_TYPES] = {0};
   long long N_total[NUM_PARTICLE_TYPES] = {0};
   long long offset[NUM_PARTICLE_TYPES] = {0};
+  int dimension = 3; /* Assume 3D if nothing is specified */
 
   /* Open file */
   /* message("Opening file '%s' as IC.", fileName); */
@@ -409,6 +411,15 @@ void read_ic_parallel(char* fileName, const struct UnitSystem* internal_units,
   h_grp = H5Gopen(h_file, "/Header", H5P_DEFAULT);
   if (h_grp < 0) error("Error while opening file header\n");
 
+  /* Check the dimensionality of the ICs (if the info exists) */
+  const hid_t hid_dim = H5Aexists(h_grp, "Dimension");
+  if (hid_dim < 0)
+    error("Error while testing existance of 'Dimension' attribute");
+  if (hid_dim > 0) readAttribute(h_grp, "Dimension", INT, &dimension);
+  if (dimension != hydro_dimension)
+    error("ICs dimensionality (%dD) does not match code dimensionality (%dD)",
+          dimension, (int)hydro_dimension);
+
   /* Read the relevant information and print status */
   int flag_entropy_temp[6];
   readAttribute(h_grp, "Flag_Entropy_ICs", INT, flag_entropy_temp);
diff --git a/src/serial_io.c b/src/serial_io.c
index c981e5e31d..5081682827 100644
--- a/src/serial_io.c
+++ b/src/serial_io.c
@@ -37,6 +37,7 @@
 
 /* Local includes. */
 #include "common_io.h"
+#include "dimension.h"
 #include "engine.h"
 #include "error.h"
 #include "gravity_io.h"
@@ -426,6 +427,7 @@ void read_ic_serial(char* fileName, const struct UnitSystem* internal_units,
   size_t N[NUM_PARTICLE_TYPES] = {0};
   long long N_total[NUM_PARTICLE_TYPES] = {0};
   long long offset[NUM_PARTICLE_TYPES] = {0};
+  int dimension = 3; /* Assume 3D if nothing is specified */
   struct UnitSystem* ic_units = malloc(sizeof(struct UnitSystem));
 
   /* First read some information about the content */
@@ -453,6 +455,15 @@ void read_ic_serial(char* fileName, const struct UnitSystem* internal_units,
     h_grp = H5Gopen(h_file, "/Header", H5P_DEFAULT);
     if (h_grp < 0) error("Error while opening file header\n");
 
+    /* Check the dimensionality of the ICs (if the info exists) */
+    const hid_t hid_dim = H5Aexists(h_grp, "Dimension");
+    if (hid_dim < 0)
+      error("Error while testing existance of 'Dimension' attribute");
+    if (hid_dim > 0) readAttribute(h_grp, "Dimension", INT, &dimension);
+    if (dimension != hydro_dimension)
+      error("ICs dimensionality (%dD) does not match code dimensionality (%dD)",
+            dimension, (int)hydro_dimension);
+
     /* Read the relevant information and print status */
     int flag_entropy_temp[6];
     readAttribute(h_grp, "Flag_Entropy_ICs", INT, flag_entropy_temp);
diff --git a/src/single_io.c b/src/single_io.c
index 93faab6717..1aab1b846d 100644
--- a/src/single_io.c
+++ b/src/single_io.c
@@ -36,6 +36,7 @@
 
 /* Local includes. */
 #include "common_io.h"
+#include "dimension.h"
 #include "engine.h"
 #include "error.h"
 #include "gravity_io.h"
@@ -339,6 +340,7 @@ void read_ic_single(char* fileName, const struct UnitSystem* internal_units,
   int numParticles[NUM_PARTICLE_TYPES] = {0};
   int numParticles_highWord[NUM_PARTICLE_TYPES] = {0};
   size_t N[NUM_PARTICLE_TYPES] = {0};
+  int dimension = 3; /* Assume 3D if nothing is specified */
   size_t Ndm;
 
   /* Open file */
@@ -364,6 +366,15 @@ void read_ic_single(char* fileName, const struct UnitSystem* internal_units,
   h_grp = H5Gopen(h_file, "/Header", H5P_DEFAULT);
   if (h_grp < 0) error("Error while opening file header\n");
 
+  /* Check the dimensionality of the ICs (if the info exists) */
+  const hid_t hid_dim = H5Aexists(h_grp, "Dimension");
+  if (hid_dim < 0)
+    error("Error while testing existance of 'Dimension' attribute");
+  if (hid_dim > 0) readAttribute(h_grp, "Dimension", INT, &dimension);
+  if (dimension != hydro_dimension)
+    error("ICs dimensionality (%dD) does not match code dimensionality (%dD)",
+          dimension, (int)hydro_dimension);
+
   /* Read the relevant information and print status */
   int flag_entropy_temp[6];
   readAttribute(h_grp, "Flag_Entropy_ICs", INT, flag_entropy_temp);
-- 
GitLab