From 05d59e6b396976acea27bd1fc8bdf50eac0ff56f Mon Sep 17 00:00:00 2001
From: Ulrich Noebauer <unoebauer@mpa-garching.mpg.de>
Date: Wed, 9 Dec 2015 14:30:50 +0100
Subject: [PATCH 1/7] added check for zero-volume of first cell

---
 tardis/io/model_reader.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/tardis/io/model_reader.py b/tardis/io/model_reader.py
index 27cdc48efe7..044dd70fbf0 100644
--- a/tardis/io/model_reader.py
+++ b/tardis/io/model_reader.py
@@ -43,7 +43,7 @@ def read_density_file(density_filename, density_filetype, time_explosion,
         raise ConfigurationError('v_inner_boundary > v_outer_boundary '
                                  '({0:s} > {1:s}). unphysical!'.format(
             v_inner_boundary, v_outer_boundary))
-    
+
     if (not np.isclose(v_inner_boundary, 0.0 * u.km / u.s,
                        atol=1e-8 * u.km / u.s)
         and v_inner_boundary > v_inner[0]):
@@ -52,6 +52,11 @@ def read_density_file(density_filename, density_filetype, time_explosion,
             raise ConfigurationError('Inner boundary selected outside of model')
 
         inner_boundary_index = v_inner.searchsorted(v_inner_boundary) - 1
+        # check for zero volume cell
+        if np.isclose(v_inner_boundary, v_inner[inner_boundary_index + 1],
+                      atol=1e-8 * u.km / u.s) and (v_inner_boundary <=
+                                                   v_inner[inner_boundary_index + 1]):
+            inner_boundary_index += 1
 
     else:
         inner_boundary_index = None
@@ -66,7 +71,7 @@ def read_density_file(density_filename, density_filetype, time_explosion,
         v_outer_boundary = v_outer[-1]
         logger.warning("v_outer_boundary requested too large for readin file. Boundary shifted to match file.")
 
-        
+
     v_inner = v_inner[inner_boundary_index:outer_boundary_index]
     v_inner[0] = v_inner_boundary
 
@@ -107,7 +112,7 @@ def read_abundances_file(abundance_filename, abundance_filetype,
 
     index, abundances = file_parsers[abundance_filetype](abundance_filename)
     if outer_boundary_index is not None:
-        outer_boundary_index_m1 = outer_boundary_index - 1 
+        outer_boundary_index_m1 = outer_boundary_index - 1
     else:
         outer_boundary_index_m1 = None
     index = index[inner_boundary_index:outer_boundary_index]

From 15a626ebe4dae25025839db68b2d1be95bad042e Mon Sep 17 00:00:00 2001
From: Ulrich Noebauer <unoebauer@mpa-garching.mpg.de>
Date: Wed, 9 Dec 2015 14:52:20 +0100
Subject: [PATCH 2/7] found much simpler formulation of zero-volume check

---
 tardis/io/model_reader.py | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/tardis/io/model_reader.py b/tardis/io/model_reader.py
index 044dd70fbf0..334d559319a 100644
--- a/tardis/io/model_reader.py
+++ b/tardis/io/model_reader.py
@@ -51,12 +51,7 @@ def read_density_file(density_filename, density_filetype, time_explosion,
         if v_inner_boundary > v_outer[-1]:
             raise ConfigurationError('Inner boundary selected outside of model')
 
-        inner_boundary_index = v_inner.searchsorted(v_inner_boundary) - 1
-        # check for zero volume cell
-        if np.isclose(v_inner_boundary, v_inner[inner_boundary_index + 1],
-                      atol=1e-8 * u.km / u.s) and (v_inner_boundary <=
-                                                   v_inner[inner_boundary_index + 1]):
-            inner_boundary_index += 1
+        inner_boundary_index = v_inner.searchsorted(v_inner_boundary, side="right")
 
     else:
         inner_boundary_index = None

From 9acb06cfabc8bab31916ef9c47baf85a6b05b8f2 Mon Sep 17 00:00:00 2001
From: Ulrich Noebauer <unoebauer@mpa-garching.mpg.de>
Date: Wed, 9 Dec 2015 15:13:02 +0100
Subject: [PATCH 3/7] Revert "found much simpler formulation of zero-volume
 check"

This reverts commit 15a626ebe4dae25025839db68b2d1be95bad042e.
---
 tardis/io/model_reader.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tardis/io/model_reader.py b/tardis/io/model_reader.py
index 334d559319a..044dd70fbf0 100644
--- a/tardis/io/model_reader.py
+++ b/tardis/io/model_reader.py
@@ -51,7 +51,12 @@ def read_density_file(density_filename, density_filetype, time_explosion,
         if v_inner_boundary > v_outer[-1]:
             raise ConfigurationError('Inner boundary selected outside of model')
 
-        inner_boundary_index = v_inner.searchsorted(v_inner_boundary, side="right")
+        inner_boundary_index = v_inner.searchsorted(v_inner_boundary) - 1
+        # check for zero volume cell
+        if np.isclose(v_inner_boundary, v_inner[inner_boundary_index + 1],
+                      atol=1e-8 * u.km / u.s) and (v_inner_boundary <=
+                                                   v_inner[inner_boundary_index + 1]):
+            inner_boundary_index += 1
 
     else:
         inner_boundary_index = None

From acd72ea944dd85b44db7c8e9757320fc4d3b9a5a Mon Sep 17 00:00:00 2001
From: Ulrich Noebauer <unoebauer@mpa-garching.mpg.de>
Date: Wed, 9 Dec 2015 15:36:22 +0100
Subject: [PATCH 4/7] -fixed astropy.units.Quantity issue in ascii_reader test
 (strangely did only trigger now)

---
 tardis/io/tests/test_ascii_readers.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tardis/io/tests/test_ascii_readers.py b/tardis/io/tests/test_ascii_readers.py
index 87f251ea824..0f7013c81b8 100644
--- a/tardis/io/tests/test_ascii_readers.py
+++ b/tardis/io/tests/test_ascii_readers.py
@@ -55,6 +55,6 @@ def test_ascii_reader_density_boundaries(v_inner_boundary, v_outer_boundary, act
                             actual_v_inner.to(v_inner[0].unit).value)
 
     if not np.isnan(actual_v_outer):
-        npt.assert_allclose(v_outer[-1],
+        npt.assert_allclose(v_outer[-1].value,
                             actual_v_outer.to(v_outer[-1].unit).value)
 

From c5fbfff43511a92d8b40a4ae7f625884df30fbcd Mon Sep 17 00:00:00 2001
From: Ulrich Noebauer <unoebauer@mpa-garching.mpg.de>
Date: Wed, 9 Dec 2015 15:52:51 +0100
Subject: [PATCH 5/7] added another test scenario for the ascii density reader

---
 tardis/io/tests/test_ascii_readers.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tardis/io/tests/test_ascii_readers.py b/tardis/io/tests/test_ascii_readers.py
index 0f7013c81b8..c280b3fada2 100644
--- a/tardis/io/tests/test_ascii_readers.py
+++ b/tardis/io/tests/test_ascii_readers.py
@@ -40,7 +40,8 @@ def test_simple_ascii_abundance_reader():
     (0.0 * u.km/u.s, np.inf * u.km/u.s, np.nan, np.nan, None, None),
     (500 * u.km/u.s, 6000 * u.km/u.s, np.nan, 6000 * u.km/u.s, None, 16),
     (1300 * u.km/u.s, 6000 * u.km/u.s, 1300 * u.km/u.s, 6000 * u.km/u.s, 0, 16),
-    (1600 * u.km/u.s, 6000 * u.km/u.s, 1600 * u.km/u.s, 6000 * u.km/u.s, 1, 16)
+    (1600 * u.km/u.s, 6000 * u.km/u.s, 1600 * u.km/u.s, 6000 * u.km/u.s, 1, 16),
+    (1889.063 * u.km/u.s, np.inf * u.km/u.s, 1889.063 * u.km/u.s, np.nan, 2, None)
 ])
 def test_ascii_reader_density_boundaries(v_inner_boundary, v_outer_boundary, actual_v_inner, actual_v_outer,
                                          inner_index, outer_index):

From cfad0e0f74217d294fce5e7dbbf4524a15ee400a Mon Sep 17 00:00:00 2001
From: Ulrich Noebauer <unoebauer@mpa-garching.mpg.de>
Date: Wed, 9 Dec 2015 16:20:58 +0100
Subject: [PATCH 6/7] added general check for invalid cell volumes

---
 tardis/io/model_reader.py | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tardis/io/model_reader.py b/tardis/io/model_reader.py
index 044dd70fbf0..5cdb19687c6 100644
--- a/tardis/io/model_reader.py
+++ b/tardis/io/model_reader.py
@@ -52,7 +52,7 @@ def read_density_file(density_filename, density_filetype, time_explosion,
             raise ConfigurationError('Inner boundary selected outside of model')
 
         inner_boundary_index = v_inner.searchsorted(v_inner_boundary) - 1
-        # check for zero volume cell
+        # check for zero volume of designated first cell
         if np.isclose(v_inner_boundary, v_inner[inner_boundary_index + 1],
                       atol=1e-8 * u.km / u.s) and (v_inner_boundary <=
                                                    v_inner[inner_boundary_index + 1]):
@@ -80,6 +80,16 @@ def read_density_file(density_filename, density_filetype, time_explosion,
 
     mean_densities = mean_densities[inner_boundary_index:outer_boundary_index]
 
+    invalid_volume_mask = (v_outer - v_inner) <= 0
+    if invalid_volume_mask.sum() > 0:
+        message = "\n".join(["cell {0:d}: v_inner {1:s}, v_outer "
+                             "{2:s}".format(i, v_inner_i, v_outer_i) for i,
+                             v_inner_i, v_outer_i in
+                             zip(np.arange(len(v_outer))[invalid_volume_mask],
+                                 v_inner[invalid_volume_mask],
+                                 v_outer[invalid_volume_mask])])
+        raise ConfigurationError("Invalid volume of following cell(s):\n"
+                                 "{:s}".format(message))
 
     return (v_inner, v_outer, mean_densities,
             inner_boundary_index, outer_boundary_index)

From 7ff44f4f194863854dcf73850d786f348edc82d9 Mon Sep 17 00:00:00 2001
From: Ulrich Noebauer <unoebauer@mpa-garching.mpg.de>
Date: Wed, 9 Dec 2015 17:17:38 +0100
Subject: [PATCH 7/7] added test specifically triggering the invalid volume
 exception in the ascii_density reader

---
 tardis/io/tests/data/invalid_artis_model.dat | 72 ++++++++++++++++++++
 tardis/io/tests/test_ascii_readers.py        |  6 ++
 2 files changed, 78 insertions(+)
 create mode 100644 tardis/io/tests/data/invalid_artis_model.dat

diff --git a/tardis/io/tests/data/invalid_artis_model.dat b/tardis/io/tests/data/invalid_artis_model.dat
new file mode 100644
index 00000000000..bef85553ce8
--- /dev/null
+++ b/tardis/io/tests/data/invalid_artis_model.dat
@@ -0,0 +1,72 @@
+      70
+   0.00114661
+     1    1.259375e+03   -5.449497e-05    9.969498e-01    9.787696e-01    6.973734e-06    5.052319e-06    6.168110e-07
+     2    1.574219e+03    3.013383e-04    9.969307e-01    9.788986e-01    6.845718e-06    5.013408e-06    6.140697e-07
+     3    1.889063e+03   -1.982630e-02    9.968976e-01    9.789760e-01    6.720018e-06    4.915389e-06    6.130545e-07
+     4    2.203907e+03   -4.429480e-02    9.968509e-01    9.790147e-01    6.608687e-06    4.839975e-06    6.060005e-07
+     5    2.518750e+03   -5.767948e-02    9.967775e-01    9.789697e-01    6.505750e-06    4.727071e-06    6.067441e-07
+     6    2.833594e+03   -7.370731e-02    9.966485e-01    9.787804e-01    6.395329e-06    4.580685e-06    6.268937e-07
+     7    3.148438e+03   -1.013941e-01    9.964382e-01    9.783643e-01    6.259683e-06    4.443940e-06    6.797306e-07
+     8    3.463282e+03   -1.223770e-01    9.961101e-01    9.776204e-01    6.090588e-06    4.411024e-06    7.830217e-07
+     9    3.778126e+03   -1.441526e-01    9.956658e-01    9.765878e-01    5.894967e-06    4.667291e-06    9.375288e-07
+    10    4.092969e+03   -1.710595e-01    9.951726e-01    9.754603e-01    5.696173e-06    5.264160e-06    1.113761e-06
+    11    4.407813e+03   -1.921165e-01    9.946904e-01    9.743882e-01    5.505227e-06    6.087066e-06    1.275082e-06
+    12    4.722657e+03   -2.190449e-01    9.942578e-01    9.734612e-01    5.318957e-06    6.965121e-06    1.406783e-06
+    13    5.037501e+03   -2.503739e-01    9.938884e-01    9.727036e-01    5.138882e-06    7.761285e-06    1.503072e-06
+    14    5.352345e+03   -2.796168e-01    9.936060e-01    9.721567e-01    4.966839e-06    8.340429e-06    1.549395e-06
+    15    5.667188e+03   -3.105920e-01    9.934865e-01    9.719984e-01    4.797945e-06    8.454966e-06    1.503797e-06
+    16    5.667188e+03   -3.413327e-01    9.937069e-01    9.726593e-01    4.642989e-06    7.660500e-06    1.276586e-06
+    17    6.296876e+03   -3.751951e-01    9.946118e-01    9.750052e-01    4.504424e-06    5.799124e-06    7.959853e-07
+    18    6.611720e+03   -4.087162e-01    9.968127e-01    9.809170e-01    4.377842e-06    1.154036e-05    2.120706e-07
+    19    6.926563e+03   -4.452069e-01    9.993206e-01    9.838294e-01    4.787479e-06    7.603537e-03    1.054282e-04
+    20    7.241407e+03   -4.782779e-01    9.934882e-01    9.623342e-01    7.808212e-06    2.142014e-02    4.893291e-04
+    21    7.556251e+03   -5.144547e-01    9.801450e-01    9.390609e-01    7.891220e-06    2.628785e-02    7.470313e-04
+    22    7.871095e+03   -5.506459e-01    9.570934e-01    9.065100e-01    7.418816e-06    2.935965e-02    9.643208e-04
+    23    8.185939e+03   -5.838073e-01    9.166762e-01    8.554363e-01    6.813265e-06    3.148066e-02    1.174981e-03
+    24    8.500782e+03   -6.187967e-01    8.444416e-01    7.711242e-01    6.021313e-06    3.228287e-02    1.373482e-03
+    25    8.815626e+03   -6.478082e-01    7.205625e-01    6.360884e-01    4.926903e-06    3.042671e-02    1.489358e-03
+    26    9.130470e+03   -6.772585e-01    5.522046e-01    4.633726e-01    3.580056e-06    2.523080e-02    1.421018e-03
+    27    9.445314e+03   -7.025316e-01    3.798727e-01    2.967333e-01    2.290327e-06    1.821502e-02    1.174456e-03
+    28    9.360158e+03   -7.393099e-01    2.434208e-01    1.729650e-01    1.321915e-06    1.181660e-02    8.653664e-04
+    29    1.007500e+04   -7.769093e-01    1.593954e-01    1.020666e-01    7.627219e-07    7.574566e-03    6.170831e-04
+    30    1.038985e+04   -8.221853e-01    1.137370e-01    6.635599e-02    4.797101e-07    5.202758e-03    4.581936e-04
+    31    1.070469e+04   -8.745196e-01    8.964338e-02    4.874161e-02    3.390994e-07    3.947096e-03    3.661284e-04
+    32    1.101953e+04   -9.296468e-01    7.628418e-02    3.940266e-02    2.635893e-07    3.241346e-03    3.114475e-04
+    33    1.133438e+04   -9.903432e-01    6.722356e-02    3.232460e-02    2.087902e-07    2.646639e-03    2.642194e-04
+    34    1.164922e+04   -1.044582e+00    5.923151e-02    2.301299e-02    1.443657e-07    1.793031e-03    1.973346e-04
+    35    1.196406e+04   -1.107672e+00    5.226578e-02    1.259155e-02    7.754713e-08    8.991098e-04    1.268152e-04
+    36    1.227891e+04   -1.166106e+00    4.718720e-02    5.081296e-03    3.147591e-08    3.251325e-04    7.181194e-05
+    37    1.259375e+04   -1.224244e+00    3.832284e-02    1.308496e-03    1.028649e-08    6.508490e-05    2.581188e-05
+    38    1.290860e+04   -1.278808e+00    2.325070e-02    1.854372e-04    8.404720e-09    5.333218e-06    3.988807e-06
+    39    1.322344e+04   -1.319016e+00    9.104333e-03    1.132432e-05    2.479127e-09    3.431589e-07    4.391149e-07
+    40    1.353828e+04   -1.339697e+00    9.852053e-04    1.299599e-07    7.783476e-11    1.265313e-08    2.057230e-08
+    41    1.385313e+04   -1.350281e+00    3.185364e-05    9.492013e-10    1.632555e-12    1.190779e-10    2.088784e-10
+    42    1.416797e+04   -1.366863e+00    1.474822e-05    2.220123e-10    5.309134e-13    4.447397e-11    7.551816e-11
+    43    1.448281e+04   -1.408773e+00    8.973184e-06    8.011853e-11    2.082280e-13    3.914228e-11    5.869264e-11
+    44    1.479766e+04   -1.484229e+00    6.154459e-06    2.021216e-11    5.137364e-14    2.829258e-11    6.464542e-11
+    45    1.511250e+04   -1.576636e+00    5.280028e-06    6.933790e-12    1.465119e-14    1.530388e-11    6.442062e-11
+    46    1.542735e+04   -1.673018e+00    5.236783e-06    3.220037e-12    6.011255e-15    8.828028e-12    6.008495e-11
+    47    1.574219e+04   -1.779685e+00    5.475689e-06    1.656054e-12    3.293192e-15    5.715768e-12    5.706669e-11
+    48    1.605703e+04   -1.876238e+00    5.822625e-06    8.090158e-13    1.990751e-15    3.956454e-12    5.687796e-11
+    49    1.637188e+04   -1.990179e+00    6.233115e-06    4.442047e-13    1.473151e-15    3.072241e-12    5.915856e-11
+    50    1.668672e+04   -2.082134e+00    6.693559e-06    2.602742e-13    1.196462e-15    2.628631e-12    6.268807e-11
+    51    1.700156e+04   -2.189603e+00    7.080294e-06    1.937764e-13    1.302169e-15    2.424756e-12    6.402162e-11
+    52    1.731641e+04   -2.251445e+00    7.367846e-06    1.874260e-13    2.002987e-15    2.292432e-12    6.198017e-11
+    53    1.763125e+04   -2.336874e+00    7.487331e-06    2.089537e-13    3.862363e-15    2.145276e-12    5.615828e-11
+    54    1.794610e+04   -2.413082e+00    7.481771e-06    2.284368e-13    7.610089e-15    1.892516e-12    4.869019e-11
+    55    1.826094e+04   -2.525231e+00    7.307651e-06    2.426003e-13    1.494172e-14    1.604724e-12    4.093251e-11
+    56    1.857578e+04   -2.598198e+00    6.904268e-06    2.392776e-13    2.741366e-14    1.286481e-12    3.355300e-11
+    57    1.889063e+04   -2.582957e+00    6.180129e-06    2.125336e-13    4.712860e-14    9.528658e-13    2.601973e-11
+    58    1.920547e+04   -2.732132e+00    5.130247e-06    1.782245e-13    7.413013e-14    6.634504e-13    1.942738e-11
+    59    1.952032e+04   -2.787764e+00    4.160961e-06    1.395227e-13    9.871988e-14    4.585879e-13    1.521031e-11
+    60    1.983516e+04   -2.847531e+00    3.126174e-06    9.689468e-14    1.121511e-13    2.803738e-13    1.103460e-11
+    61    2.015000e+04   -2.861318e+00    2.165275e-06    6.563661e-14    1.260716e-13    1.659489e-13    7.817914e-12
+    62    2.046485e+04   -2.984034e+00    1.489794e-06    4.275136e-14    1.315134e-13    9.863714e-14    5.815667e-12
+    63    2.077969e+04   -2.888253e+00    8.864073e-07    2.107301e-14    1.053168e-13    4.595312e-14    3.655108e-12
+    64    2.109453e+04   -3.077504e+00    5.232797e-07    8.675422e-15    6.905988e-14    1.760052e-14    2.341958e-12
+    65    2.140938e+04   -3.169650e+00    3.201423e-07    3.365656e-15    3.595022e-14    6.162002e-15    1.394190e-12
+    66    2.172422e+04   -3.103242e+00    1.856889e-07    1.293778e-15    1.860925e-14    2.033915e-15    8.039162e-13
+    67    2.203907e+04   -3.195011e+00    1.027709e-07    3.343978e-16    5.949844e-15    5.858914e-16    4.132950e-13
+    68    2.235391e+04   -3.304330e+00    5.443506e-08    7.937542e-17    1.640208e-15    1.396395e-16    1.943107e-13
+    69    2.266875e+04   -3.441502e+00    2.939781e-08    1.475479e-17    3.430180e-16    4.586563e-17    9.063403e-14
+    70    2.298360e+04   -3.453566e+00    1.464506e-08    2.619737e-18    6.785903e-17    1.021336e-17    3.857130e-14
diff --git a/tardis/io/tests/test_ascii_readers.py b/tardis/io/tests/test_ascii_readers.py
index c280b3fada2..bb6861ec62b 100644
--- a/tardis/io/tests/test_ascii_readers.py
+++ b/tardis/io/tests/test_ascii_readers.py
@@ -59,3 +59,9 @@ def test_ascii_reader_density_boundaries(v_inner_boundary, v_outer_boundary, act
         npt.assert_allclose(v_outer[-1].value,
                             actual_v_outer.to(v_outer[-1].unit).value)
 
+def test_ascii_reader_invalid_volumes():
+
+    with pytest.raises(io.model_reader.ConfigurationError):
+        v_inner, v_outer, mean_densities, inner_boundary_index, outer_boundary_index = \
+            io.read_density_file(data_path('invalid_artis_model.dat'), 'artis', 19 *
+                                 u.day, 0.0 * u.km/u.s, np.inf * u.km/u.s)