-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathtest_lower_exprs.py
255 lines (180 loc) · 7.86 KB
/
test_lower_exprs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import numpy as np
import pytest
from devito import (Grid, TimeFunction, Function, Operator, Eq, solve,
DefaultDimension)
from devito.finite_differences import Derivative
from devito.finite_differences.differentiable import diff2sympy
from devito.ir.equations import LoweredEq
from devito.passes.equations.linearity import collect_derivatives
from devito.tools import timed_region
class TestCollectDerivatives(object):
"""
Test collect_derivatives and all mechanisms used by collect_derivatives
indirectly.
"""
def test_nocollection_if_diff_dims(self):
"""
Test that expressions with different time dimensions are not collected.
"""
grid = Grid((10,))
f = TimeFunction(name="f", grid=grid, save=10)
f2 = TimeFunction(name="f2", grid=grid, save=10)
g = TimeFunction(name="g", grid=grid)
g2 = TimeFunction(name="g2", grid=grid)
w = Function(name="w", grid=grid)
with timed_region('x'):
eq = Eq(w, f.dt*g + f2.dt*g2)
# Since all Function are time dependent, there should be no collection
# and produce the same result as with the pre evaluated expression
expr = Operator._lower_exprs([eq], options={})[0]
expr2 = Operator._lower_exprs([eq.evaluate], options={})[0]
assert expr == expr2
def test_numeric_constant(self):
grid = Grid(shape=(10, 10))
u = TimeFunction(name="u", grid=grid, space_order=4, time_order=2)
eq = Eq(u.forward, u.dx.dx + 0.3*u.dy.dx)
leq = collect_derivatives.func([eq])[0]
assert len(leq.find(Derivative)) == 3
def test_symbolic_constant(self):
grid = Grid(shape=(10, 10))
dt = grid.time_dim.spacing
u = TimeFunction(name="u", grid=grid, space_order=4, time_order=2)
eq = Eq(u.forward, u.dx.dx + dt**0.2*u.dy.dx)
leq = collect_derivatives.func([eq])[0]
assert len(leq.find(Derivative)) == 3
def test_symbolic_constant_times_add(self):
grid = Grid(shape=(10, 10))
dt = grid.time_dim.spacing
u = TimeFunction(name="u", grid=grid, space_order=4, time_order=2)
f = Function(name='f', grid=grid)
eq = Eq(u.forward, u.laplace + dt**0.2*u.biharmonic(1/f))
leq = collect_derivatives.func([eq])[0]
assert len(eq.rhs.args) == 3
assert len(leq.rhs.args) == 2
assert all(isinstance(i, Derivative) for i in leq.rhs.args)
def test_solve(self):
"""
By remaining unevaluated until after Operator's collect_derivatives,
the Derivatives after a solve() should be collected.
"""
grid = Grid(shape=(10, 10))
u = TimeFunction(name="u", grid=grid, space_order=4, time_order=2)
pde = u.dt2 - (u.dx.dx + u.dy.dy) - u.dx.dy
eq = Eq(u.forward, solve(pde, u.forward))
leq = collect_derivatives.func([eq])[0]
assert len(eq.rhs.find(Derivative)) == 5
assert len(leq.rhs.find(Derivative)) == 4
assert len(leq.rhs.args[2].find(Derivative)) == 3 # Check factorization
def test_nocollection_if_unworthy(self):
grid = Grid(shape=(10, 10))
dt = grid.time_dim.spacing
u = TimeFunction(name="u", grid=grid)
eq = Eq(u.forward, (0.4 + dt)*(u.dx + u.dy))
leq = collect_derivatives.func([eq])[0]
assert eq == leq
def test_pull_and_collect(self):
grid = Grid(shape=(10, 10))
dt = grid.time_dim.spacing
hx, _ = grid.spacing_symbols
u = TimeFunction(name="u", grid=grid)
v = TimeFunction(name="v", grid=grid)
eq = Eq(u.forward, ((0.4 + dt)*u.dx + 0.3)*hx + v.dx)
leq = collect_derivatives.func([eq])[0]
assert eq != leq
args = leq.rhs.args
assert len(args) == 2
assert diff2sympy(args[0]) == 0.3*hx
assert args[1] == (hx*(dt + 0.4)*u + v).dx
def test_pull_and_collect_nested(self):
grid = Grid(shape=(10, 10))
dt = grid.time_dim.spacing
hx, hy = grid.spacing_symbols
u = TimeFunction(name="u", grid=grid, space_order=2)
v = TimeFunction(name="v", grid=grid, space_order=2)
eq = Eq(u.forward, (((0.4 + dt)*u.dx + 0.3)*hx + v.dx).dy + (0.2 + hy)*v.dy)
leq = collect_derivatives.func([eq])[0]
assert eq != leq
assert leq.rhs == ((v + hx*(0.4 + dt)*u).dx + 0.3*hx + (0.2 + hy)*v).dy
def test_pull_and_collect_nested_v2(self):
grid = Grid(shape=(10, 10))
dt = grid.time_dim.spacing
hx, hy = grid.spacing_symbols
u = TimeFunction(name="u", grid=grid, space_order=2)
v = TimeFunction(name="v", grid=grid, space_order=2)
eq = Eq(u.forward, ((0.4 + dt*(hy + 1. + hx*hy))*u.dx + 0.3)*hx + v.dx)
leq = collect_derivatives.func([eq])[0]
assert eq != leq
assert leq.rhs == 0.3*hx + (hx*(0.4 + dt*(hy + 1. + hx*hy))*u + v).dx
def test_pull_and_collect_nested_v3(self):
grid = Grid(shape=(10, 10))
dt = grid.time_dim.spacing
hx, hy = grid.spacing_symbols
a = Function(name="a", grid=grid, space_order=2)
u = TimeFunction(name="u", grid=grid, space_order=2)
v = TimeFunction(name="v", grid=grid, space_order=2)
eq = Eq(u.forward, 0.4 + a*(hx + dt*(u.dx + v.dx)))
leq = collect_derivatives.func([eq])[0]
assert eq != leq
assert leq.rhs == 0.4 + a*(hx + (dt*u + dt*v).dx)
def test_nocollection_subdims(self):
grid = Grid(shape=(10, 10))
xi, yi = grid.interior.dimensions
u = TimeFunction(name="u", grid=grid)
v = TimeFunction(name="v", grid=grid)
f = Function(name='f', grid=grid)
eq = Eq(u.forward, u.dx + 0.2*f[xi, yi]*v.dx)
leq = collect_derivatives.func([eq])[0]
assert eq == leq
def test_nocollection_staggered(self):
grid = Grid(shape=(10, 10))
x, y = grid.dimensions
u = TimeFunction(name="u", grid=grid)
v = TimeFunction(name="v", grid=grid, staggered=x)
eq = Eq(u.forward, u.dx + v.dx)
leq = collect_derivatives.func([eq])[0]
assert eq == leq
def test_nocollection_mixed_order(self):
grid = Grid(shape=(10, 10))
u = TimeFunction(name="u", grid=grid, space_order=2)
# First case is obvious...
eq = Eq(u.forward, u.dx2 + u.dx.dy + 1.)
leq = collect_derivatives.func([eq])[0]
assert eq == leq
# y-derivative should not get collected!
eq = Eq(u.forward, u.dy2 + u.dx.dy + 1.)
leq = collect_derivatives.func([eq])[0]
assert eq == leq
class TestLowering(object):
"""
Test that expression lowering works as expected.
"""
def test_lower_func_as_ind(self):
grid = Grid((11, 11))
x, y = grid.dimensions
t = grid.stepping_dim
h = DefaultDimension("h", default_value=10)
u = TimeFunction(name='u', grid=grid, time_order=2, space_order=2)
oh = Function(name="ou", dimensions=(h,), shape=(10,), dtype=int)
eq = [Eq(u.forward, u._subs(x, x + oh))]
lowered = LoweredEq(Eq(u[t + 1, x + 2, y + 2], u[t, x + oh[h] + 2, y + 2]))
with timed_region('x'):
leq = Operator._lower_exprs(eq, options={})
assert leq[0] == lowered
class TestUnexpanded(object):
@pytest.mark.parametrize('expr', [
'u.dx',
'u.dx.dy',
'u.dxdy',
'u.dx.dy + u.dy.dx',
'u.dx2 + u.dx.dy',
])
def test_single_eq(self, expr):
grid = Grid(shape=(10, 10))
u = TimeFunction(name="u", grid=grid, space_order=4)
u1 = TimeFunction(name="u", grid=grid, space_order=4)
eq = Eq(u.forward, eval(expr) + 1.)
op0 = Operator(eq)
op1 = Operator(eq, opt=('advanced', {'expand': False}))
op0.apply(time_M=5)
op1.apply(time_M=5, u=u1)
assert np.allclose(u.data, u1.data, rtol=1e-5)