Repository: devitocodes/devito
Issue: HaloScheme with time subdimensions

HaloScheme is breaking when a time subdimension is involved

```
from devito import *

grid = Grid(shape=(11, 11))

u = TimeFunction(name="u", grid=grid, time_order=2, space_order=4, save=10)

time_subsampled = ConditionalDimension('t_sub', parent=grid.time_dim, factor=2)

v = TimeFunction(name="v", grid=grid, space_order=4, time_dim=time_subsampled, save=5)
w = Function(name="w", grid=grid, space_order=4)

op = Operator(Eq(w, v.dx))
```

Leads to the error

```
    op = Operator(Eq(w, v.dx))
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/backends.py", line 73, in __call__
    return t(*args, **kwargs)
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/operator/operator.py", line 157, in __new__
    op = cls._build(expressions, **kwargs)
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/operator/operator.py", line 187, in _build
    stree = cls._lower_stree(clusters, **kwargs)
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/tools/decorators.py", line 163, in __call__
    retval = self.func(*args, **kwargs)
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/operator/operator.py", line 345, in _lower_stree
    stree = stree_build(clusters)
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/ir/stree/algorithms.py", line 26, in stree_build
    stree = stree_make_halo(stree)
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/ir/stree/algorithms.py", line 97, in stree_make_halo
    halo_schemes[n] = HaloScheme(n.exprs, n.ispace)
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/mpi/halo_scheme.py", line 71, in __init__
    loc_indices = compute_local_indices(f, local, ispace, scope)
  File "/Users/mathiaslouboutin/research/London/CodeGen/devito/devito/mpi/halo_scheme.py", line 439, in compute_local_indices
    loc_indices[d] = func(candidates, key=lambda i: i-d)
  File "/usr/local/lib/python3.8/site-packages/sympy/core/relational.py", line 376, in __nonzero__
    raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
```



Changed files:
tests/test_dimension.py, devito/mpi/halo_scheme.py

diff --git a/devito/mpi/halo_scheme.py b/devito/mpi/halo_scheme.py
index 28f048be9..00505daef 100644
--- a/devito/mpi/halo_scheme.py
+++ b/devito/mpi/halo_scheme.py
@@ -428,13 +428,15 @@ def compute_local_indices(f, dims, ispace, scope):
     loc_indices = {}
     for d in dims:
         try:
-            func = max if ispace.is_forward(d.root) else min
+            func = Max if ispace.is_forward(d.root) else Min
         except KeyError:
             raise HaloSchemeException("Don't know how to build a HaloScheme as `%s` "
                                       "doesn't appear in `%s`" % (d, ispace))
-        candidates = [i[d] for i in scope.getreads(f) if not is_integer(i[d])]
         if d.is_Stepping:
-            loc_indices[d] = func(candidates, key=lambda i: i.origin - d)
+            candidates = {i[d].origin - d: i[d] for i in scope.getreads(f)
+                          if not is_integer(i[d])}
         else:
-            loc_indices[d] = func(candidates, key=lambda i: i-d)
+            candidates = {i[d] - d: i[d] for i in scope.getreads(f)
+                          if not is_integer(i[d])}
+        loc_indices[d] = candidates[func(*candidates.keys())]
     return loc_indices
diff --git a/tests/test_dimension.py b/tests/test_dimension.py
index f74aad7b7..991b3e5f2 100644
--- a/tests/test_dimension.py
+++ b/tests/test_dimension.py
@@ -9,7 +9,7 @@ from devito import (ConditionalDimension, Grid, Function, TimeFunction, SparseFu
                     Eq, Operator, Constant, Dimension, SubDimension, switchconfig,
                     SubDomain, Lt, Le, Gt, Ge, Ne, Buffer)
 from devito.ir.iet import Expression, Iteration, FindNodes, retrieve_iteration_tree
-from devito.symbolics import indexify, retrieve_functions
+from devito.symbolics import indexify, retrieve_functions, IntDiv
 from devito.types import Array
 
 
@@ -597,6 +597,18 @@ class TestConditionalDimension(object):
                        usave[time_subsampled + 1, x, y],
                        usave[time_subsampled, x, y]]
 
+    def test_issue_1592(self):
+        grid = Grid(shape=(11, 11))
+        time = grid.time_dim
+        time_sub = ConditionalDimension('t_sub', parent=time, factor=2)
+        v = TimeFunction(name="v", grid=grid, space_order=4, time_dim=time_sub, save=5)
+        w = Function(name="w", grid=grid, space_order=4)
+        Operator(Eq(w, v.dx))(time=6)
+        op = Operator(Eq(v.forward, v.dx))
+        op.apply(time=6)
+        exprs = FindNodes(Expression).visit(op)
+        assert exprs[-1].expr.lhs.indices[0] == IntDiv(time, 2) + 1
+
     def test_subsampled_fd(self):
         """
         Test that the FD shortcuts are handled correctly with ConditionalDimensions