forked from devitocodes/devito
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_roundoff.py
More file actions
158 lines (120 loc) · 5.78 KB
/
Copy pathtest_roundoff.py
File metadata and controls
158 lines (120 loc) · 5.78 KB
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
import pytest
import numpy as np
from devito import Grid, Constant, TimeFunction, Eq, Operator, switchconfig
class TestRoundoff(object):
"""
Class for checking round-off errors are not unexpectedly creeping in to certain
stencil types.
"""
@pytest.mark.parametrize('dat', [0.5, 0.624, 1.0, 1.5, 2.0, 3.0, 3.6767, 4.0])
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
@switchconfig(log_level='DEBUG', safe_math=True)
def test_lm_forward(self, dat, dtype):
"""
Test logistic map with forward term that should cancel.
"""
iterations = 10000
r = Constant(name='r', dtype=dtype)
r.data = dtype(dat)
s = dtype(0.1)
grid = Grid(shape=(2, 2), extent=(1, 1), dtype=dtype)
dt = grid.stepping_dim.spacing
f0 = TimeFunction(name='f0', grid=grid, time_order=2, dtype=dtype)
f1 = TimeFunction(name='f1', grid=grid, time_order=2, save=iterations+2,
dtype=dtype)
lmap0 = Eq(f0.forward, r*f0*(1.0-f0+(1.0/s)*dt*f0.forward-f0.forward))
lmap1 = Eq(f1.forward, r*f1*(1.0-f1+(1.0/s)*dt*f1.forward-f1.forward))
initial_condition = dtype(0.7235)
f0.data[1, :, :] = initial_condition
f1.data[1, :, :] = initial_condition
op0 = Operator([Eq(f0.forward, dtype(0.0)), lmap0])
op1 = Operator(lmap1)
op0(time_m=1, time_M=iterations, dt=s)
op1(time_m=1, time_M=iterations, dt=s)
assert np.allclose(f0.data[np.mod(iterations+1, 3)], f1.data[iterations+1],
atol=0, rtol=0)
@pytest.mark.parametrize('dat', [0.5, 0.624, 1.0, 1.5, 2.0, 3.0, 3.6767, 4.0])
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
@switchconfig(log_level='DEBUG', safe_math=True)
def test_lm_backward(self, dat, dtype):
"""
Test logistic map with backward term that should cancel.
"""
iterations = 10000
r = Constant(name='r', dtype=dtype)
r.data = dtype(dat)
s = dtype(0.1)
grid = Grid(shape=(2, 2), extent=(1, 1), dtype=dtype)
dt = grid.stepping_dim.spacing
f0 = TimeFunction(name='f0', grid=grid, time_order=2, dtype=dtype)
f1 = TimeFunction(name='f1', grid=grid, time_order=2, save=iterations+2,
dtype=dtype)
lmap0 = Eq(f0.forward, r*f0*(1.0-f0+(1.0/s)*dt*f0.backward-f0.backward))
lmap1 = Eq(f1.forward, r*f1*(1.0-f1+(1.0/s)*dt*f1.backward-f1.backward))
initial_condition = dtype(0.7235)
f0.data[1, :, :] = initial_condition
f1.data[1, :, :] = initial_condition
op0 = Operator([Eq(f0.forward, dtype(0.0)), lmap0])
op1 = Operator(lmap1)
op0(time_m=1, time_M=iterations, dt=s)
op1(time_m=1, time_M=iterations, dt=s)
assert np.allclose(f0.data[np.mod(iterations+1, 3)], f1.data[iterations+1],
atol=0, rtol=0)
@pytest.mark.parametrize('dat', [0.5, 0.624, 1.0, 1.5, 2.0, 3.0, 3.6767, 4.0])
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
@switchconfig(log_level='DEBUG', safe_math=True)
def test_lm_fb(self, dat, dtype):
"""
Test logistic map with forward and backward terms that should cancel.
"""
iterations = 10000
r = Constant(name='r', dtype=dtype)
r.data = dtype(dat)
s = dtype(0.1)
grid = Grid(shape=(2, 2), extent=(1, 1), dtype=dtype)
dt = grid.stepping_dim.spacing
print("dt = ", dt)
f0 = TimeFunction(name='f0', grid=grid, time_order=2, dtype=dtype)
f1 = TimeFunction(name='f1', grid=grid, time_order=2, save=iterations+2,
dtype=dtype)
initial_condition = dtype(0.7235)
lmap0 = Eq(f0.forward, r*f0*(1.0-f0+(1.0/s)*dt*f0.backward
- f0.backward+(1.0/s)*dt*f0.forward-f0.forward))
lmap1 = Eq(f1.forward, r*f1*(1.0-f1+(1.0/s)*dt*f1.backward
- f1.backward+(1.0/s)*dt*f1.forward-f1.forward))
f0.data[1, :, :] = initial_condition
f1.data[1, :, :] = initial_condition
op0 = Operator([Eq(f0.forward, dtype(0.0)), lmap0])
op1 = Operator(lmap1)
op0(time_m=1, time_M=iterations, dt=s)
op1(time_m=1, time_M=iterations, dt=s)
assert np.allclose(f0.data[np.mod(iterations+1, 3)], f1.data[iterations+1],
atol=0, rtol=0)
@pytest.mark.parametrize('dat', [0.5, 0.624, 1.0, 1.5, 2.0, 3.0, 3.6767, 4.0])
@pytest.mark.parametrize('dtype', [np.float32, np.float64])
@switchconfig(log_level='DEBUG', safe_math=True)
def test_lm_ds(self, dat, dtype):
"""
Test logistic map with 2nd derivative term that should cancel.
"""
iterations = 10000
r = Constant(name='r', dtype=dtype)
r.data = dtype(0.5*dat)
s = dtype(0.1)
grid = Grid(shape=(2, 2), extent=(1, 1), dtype=dtype)
f0 = TimeFunction(name='f0', grid=grid, time_order=2, dtype=dtype)
f1 = TimeFunction(name='f1', grid=grid, time_order=2, save=iterations+2,
dtype=dtype)
initial_condition = dtype(0.7235)
lmap0 = Eq(f0.forward, -r*f0.dt2*s**2*(1.0-f0) +
r*(1.0-f0)*(f0.backward+f0.forward))
lmap1 = Eq(f1.forward, -r*f1.dt2*s**2*(1.0-f1) +
r*(1.0-f1)*(f1.backward+f1.forward))
f0.data[1, :, :] = initial_condition
f1.data[1, :, :] = initial_condition
op0 = Operator([Eq(f0.forward, dtype(0.0)), lmap0])
op1 = Operator(lmap1)
op0(time_m=1, time_M=iterations, dt=s)
op1(time_m=1, time_M=iterations, dt=s)
assert np.allclose(f0.data[np.mod(iterations+1, 3)], f1.data[iterations+1],
atol=0, rtol=0)