From 5f68e60a726a539cb48723c1cedcd111966dff93 Mon Sep 17 00:00:00 2001 From: Rhodri Nelson Date: Wed, 7 Aug 2019 15:08:50 +0100 Subject: [PATCH] data.utils: Fix for when is negative. test_data: Add corresponding test. test_data: skip test_negative_start if yask. --- devito/data/utils.py | 3 +++ tests/test_data.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/devito/data/utils.py b/devito/data/utils.py index 0e9730dc7d..d59c92ac4e 100644 --- a/devito/data/utils.py +++ b/devito/data/utils.py @@ -65,6 +65,9 @@ def index_dist_to_repl(idx, decomposition): elif not is_integer(value): raise ValueError("Cannot derive shift value from type `%s`" % type(value)) + if value < 0: + value += decomposition.glb_max + 1 + # Convert into absolute local index idx = decomposition.index_glb_to_loc(idx, rel=False) diff --git a/tests/test_data.py b/tests/test_data.py index 5d1fc4634e..b8a106cf5e 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -75,6 +75,16 @@ def test_negative_step(self): assert (np.array(u.data[0, 3::-1, 0, 0]) == dat[3::-1]).all() assert (np.array(u.data[0, 5:1:-1, 0, 0]) == dat[5:1:-1]).all() + @skipif('yask') + def test_negative_start(self): + """Test slicing with a negative start.""" + grid = Grid(shape=(13,)) + f = Function(name='f', grid=grid) + idx = slice(-4, None, 1) + dat = np.array([1, 2, 3, 4]) + f.data[idx] = dat + assert np.all(np.array(f.data[9:]) == dat) + def test_halo_indexing(self): """Test data packing/unpacking in presence of a halo region.""" domain_shape = (16, 16, 16)