From a20694b0de0cfaff3e47a151e78344b52d887318 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Thu, 16 Mar 2017 16:35:59 +0800
Subject: [PATCH] Set the unnecessary dimensions of the box in 1D or 2D to safe
 values to prevent unnecessary crashes due to lack of cells along this
 particular axis.

---
 src/parallel_io.c | 6 ++++++
 src/serial_io.c   | 7 +++++++
 src/single_io.c   | 7 +++++++
 3 files changed, 20 insertions(+)

diff --git a/src/parallel_io.c b/src/parallel_io.c
index 89945b58d9..dbd029569e 100644
--- a/src/parallel_io.c
+++ b/src/parallel_io.c
@@ -445,6 +445,12 @@ void read_ic_parallel(char* fileName, const struct unit_system* internal_units,
   dim[1] = (boxSize[1] < 0) ? boxSize[0] : boxSize[1];
   dim[2] = (boxSize[2] < 0) ? boxSize[0] : boxSize[2];
 
+  /* Change box size in the 1D and 2D case */
+  if (hydro_dimension == 2)
+    dim[2] = min(dim[0], dim[1]);
+  else if (hydro_dimension == 1)
+    dim[2] = dim[1] = dim[0];
+
   /* message("Found %lld particles in a %speriodic box of size [%f %f %f].", */
   /* 	  N_total[0], (periodic ? "": "non-"), dim[0], */
   /* 	  dim[1], dim[2]); */
diff --git a/src/serial_io.c b/src/serial_io.c
index 52c52ff24c..a7e342f0a9 100644
--- a/src/serial_io.c
+++ b/src/serial_io.c
@@ -493,10 +493,17 @@ void read_ic_serial(char* fileName, const struct unit_system* internal_units,
       N_total[ptype] =
           (numParticles[ptype]) + (numParticles_highWord[ptype] << 32);
 
+    /* Get the box size if not cubic */
     dim[0] = boxSize[0];
     dim[1] = (boxSize[1] < 0) ? boxSize[0] : boxSize[1];
     dim[2] = (boxSize[2] < 0) ? boxSize[0] : boxSize[2];
 
+    /* Change box size in the 1D and 2D case */
+    if (hydro_dimension == 2)
+      dim[2] = min(dim[0], dim[1]);
+    else if (hydro_dimension == 1)
+      dim[2] = dim[1] = dim[0];
+
     /* message("Found %lld particles in a %speriodic box of size [%f %f %f].",
      */
     /* 	    N_total, (periodic ? "": "non-"), dim[0], dim[1], dim[2]); */
diff --git a/src/single_io.c b/src/single_io.c
index 85c1286f7f..0b091a5997 100644
--- a/src/single_io.c
+++ b/src/single_io.c
@@ -400,10 +400,17 @@ void read_ic_single(char* fileName, const struct unit_system* internal_units,
     N[ptype] = ((long long)numParticles[ptype]) +
                ((long long)numParticles_highWord[ptype] << 32);
 
+  /* Get the box size if not cubic */
   dim[0] = boxSize[0];
   dim[1] = (boxSize[1] < 0) ? boxSize[0] : boxSize[1];
   dim[2] = (boxSize[2] < 0) ? boxSize[0] : boxSize[2];
 
+  /* Change box size in the 1D and 2D case */
+  if (hydro_dimension == 2)
+    dim[2] = min(dim[0], dim[1]);
+  else if (hydro_dimension == 1)
+    dim[2] = dim[1] = dim[0];
+
   /* message("Found %d particles in a %speriodic box of size [%f %f %f].",  */
   /* 	  *N, (periodic ? "": "non-"), dim[0], dim[1], dim[2]);  */
 
-- 
GitLab