Repository: devitocodes/devito
Issue: ConditionalDimension indexing does not follow SteppingDimension

A ConditionalDimension that uses a stepping DImension in its condition does not generate the code expected.
The Conditional is indexed using `t` instead of `t0` or `t1`.

Related culprit:
https://github.com/devitocodes/devito/blob/master/devito/ir/iet/scheduler.py#L76

Slack discussion:
https://devitocodes.slack.com/archives/CHRKMGAE4/p1588667937209500

MFE:
```
from devito import Dimension, ConditionalDimension, Function, TimeFunction, Eq, Operator, Grid
from sympy import And
from devito.symbolics import CondEq
import pdb
nx, ny = 4 , 4

shape = (nx, ny)
grid = Grid(shape=shape)

x, y = grid.dimensions
t = grid.stepping_dim
time = grid.time_dim 

g = TimeFunction(name='g', shape=(2,nx,ny), dimensions=(t,x,y))

ci = ConditionalDimension(name='ci', parent=y, condition=CondEq(g[t,x,y], 0))
op = Operator(Eq(g.forward, g + 1, implicit_dims=ci))
print(op.ccode)
op.apply()
```

Changed files:
devito/ir/iet/visitors.py, tests/test_dimension.py

diff --git a/devito/ir/iet/visitors.py b/devito/ir/iet/visitors.py
index cf7fea534..d3a85dbde 100644
--- a/devito/ir/iet/visitors.py
+++ b/devito/ir/iet/visitors.py
@@ -743,6 +743,12 @@ class XSubs(Transformer):
         super(XSubs, self).__init__()
         self.replacer = replacer or (lambda i: uxreplace(i, mapper))
 
+    def visit_Conditional(self, o):
+        condition = self.replacer(o.condition)
+        then_body = self._visit(o.then_body)
+        else_body = self._visit(o.else_body)
+        return o._rebuild(condition=condition, then_body=then_body, else_body=else_body)
+
     def visit_Expression(self, o):
         return o._rebuild(expr=self.replacer(o.expr))
 
diff --git a/tests/test_dimension.py b/tests/test_dimension.py
index 52de106ca..7835284d1 100644
--- a/tests/test_dimension.py
+++ b/tests/test_dimension.py
@@ -831,6 +831,34 @@ class TestConditionalDimension(object):
 
         assert np.all(f.data == F)
 
+    def test_stepping_dim_in_condition_lowering(self):
+        """
+        Check that the compiler performs lowering on conditions
+        with TimeDimensions and generates the expected code::
+
+        if (g[t][x + 1][y + 1] <= 10){          if (g[t0][x + 1][y + 1] <= 10){
+            ...                          -->       ...
+        }                                       }
+
+        This test increments a function by one at every timestep until it is
+        less-or-equal to 10 (g<=10) while although operator runs for 13 timesteps.
+        """
+        grid = Grid(shape=(4, 4))
+        _, y = grid.dimensions
+
+        ths = 10
+        g = TimeFunction(name='g', grid=grid)
+
+        ci = ConditionalDimension(name='ci', parent=y, condition=Le(g, ths))
+
+        op = Operator(Eq(g.forward, g + 1, implicit_dims=ci))
+
+        op.apply(time_M=ths+3)
+        assert np.all(g.data[0, :, :] == ths)
+        assert np.all(g.data[1, :, :] == ths + 1)
+        assert 'if (g[t0][x + 1][y + 1] <= 10)\\n'
+        '{\\n g[t1][x + 1][y + 1] = g[t0][x + 1][y + 1] + 1' in str(op.ccode)
+
     def test_expr_like_lowering(self):
         """
         Test the lowering of an expr-like ConditionalDimension's condition.