-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_geometry.py
342 lines (290 loc) · 14 KB
/
test_geometry.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""Tests for geometry objects"""
import pytest
import pickle
import os
import numpy as np
import sympy as sp
from schism import BoundaryGeometry
from devito.tools.data_structures import frozendict
def read_sdf(surface, dims):
"""Unpickle an sdf"""
path = os.path.dirname(os.path.abspath(__file__))
fname = path + '/sdfs/' + surface + '_' + str(dims) + 'd.dat'
print(fname)
with open(fname, 'rb') as f:
sdf = pickle.load(f)
return sdf
class TestBoundaryGeometry:
"""Tests for the BoundaryGeometry object"""
@pytest.mark.parametrize('surface', ['45', '45_mirror',
'horizontal', 'vertical'])
@pytest.mark.parametrize('dims', [2, 3])
def test_unit_normal_magnitude(self, surface, dims):
"""Check that unit normals have correct magnitude"""
rtol = 0.1 # Allow up to 10% deviation
sdf = read_sdf(surface, dims)
bg = BoundaryGeometry(sdf)
# Check that boundary normal magnitude == 1 where the sdf<=0.5*spacing
spacing = sdf.grid.spacing
max_dist = np.sqrt(sum([(inc/2)**2 for inc in spacing]))
# Trim edges off data, as normal calculation in corners is imperfect
slices = tuple([slice(1, -1) for dim in sdf.grid.dimensions])
data = sdf.data[slices]
mask = np.abs(data) <= max_dist
normals = [bg.n[i].data[slices][mask] for i in range(len(spacing))]
n_mag = np.sqrt(sum([normals[i]**2 for i in range(len(spacing))]))
assert np.all(np.isclose(n_mag, 1, rtol=rtol))
r2o2 = np.sqrt(2)/2 # Used repeatedly in next test
@pytest.mark.parametrize('surface, dims, answer',
[('45', 2, (-r2o2, r2o2)),
('45', 3, (-r2o2, 0, r2o2)),
('45_mirror', 2, (r2o2, r2o2)),
('45_mirror', 3, (r2o2, 0, r2o2)),
('horizontal', 2, (0., 1.)),
('horizontal', 3, (0., 0., 1.)),
('vertical', 2, (1., 0.)),
('vertical', 3, (1., 0., 0.))])
def test_unit_normal_direction(self, surface, dims, answer):
"""Check that unit normals point in the correct direction"""
rtol = 0.1 # Allow up to 10% deviation
sdf = read_sdf(surface, dims)
bg = BoundaryGeometry(sdf)
# Check boundary normals where the sdf<=0.5*spacing
spacing = sdf.grid.spacing
max_dist = np.sqrt(sum([(inc/2)**2 for inc in spacing]))
# Trim edges off data, as normal calculation in corners is imperfect
slices = tuple([slice(1, -1) for dim in sdf.grid.dimensions])
data = sdf.data[slices]
mask = np.abs(data) <= max_dist
normals = [bg.n[i].data[slices][mask] for i in range(len(spacing))]
for i in range(len(normals)):
assert np.all(np.isclose(normals[i], answer[i], rtol=rtol))
@pytest.mark.parametrize('surface, dims', [('45', 2),
('45_mirror', 2),
('horizontal', 2),
('vertical', 2),
('45', 3),
('45_mirror', 3),
('horizontal', 3),
('vertical', 3)])
def test_boundary_mask(self, surface, dims):
"""Check that the boundary points are correctly identified"""
sdf = read_sdf(surface, dims)
bg = BoundaryGeometry(sdf)
# Check boundary mask size
assert bg.boundary_mask.shape == bg.grid.shape
# Trim edges off data, as normal calculation in corners is imperfect
slices = tuple([slice(2, -2) for dim in sdf.grid.dimensions])
data = bg.boundary_mask[slices]
check_mask = np.zeros(data.shape, dtype=bool)
if surface == '45':
# Diagonal indices
diag_indices = np.arange(data.shape[0])
# Below the diagonal
diag_indices_n1 = np.arange(1, data.shape[0])
# Above the diagonal
diag_indices_1 = np.arange(0, data.shape[0]-1)
# Fill the diagonals
if dims == 2:
check_mask[diag_indices, diag_indices] = True
check_mask[diag_indices_n1, diag_indices_n1-1] = True
check_mask[diag_indices_1, diag_indices_1+1] = True
elif dims == 3:
check_mask[diag_indices, :, diag_indices] = True
check_mask[diag_indices_n1, :, diag_indices_n1-1] = True
check_mask[diag_indices_1, :, diag_indices_1+1] = True
elif surface == '45_mirror':
# Diagonal indices
diag_indices = np.arange(data.shape[0])
# Above the diagonal
diag_indices_1 = np.arange(0, data.shape[0]-1)
# Two above the diagonal
diag_indices_2 = np.arange(0, data.shape[0]-2)
# Fill the diagonals
if dims == 2:
check_mask[diag_indices, diag_indices] = True
check_mask[diag_indices_1, diag_indices_1+1] = True
check_mask[diag_indices_2, diag_indices_2+2] = True
elif dims == 3:
check_mask[diag_indices, :, diag_indices] = True
check_mask[diag_indices_1, :, diag_indices_1+1] = True
check_mask[diag_indices_2, :, diag_indices_2+2] = True
check_mask = check_mask[::-1]
elif surface == 'horizontal':
if dims == 2:
check_mask[:, 48:50] = True
elif dims == 3:
check_mask[:, :, 48:50] = True
elif surface == 'vertical':
if dims == 2:
check_mask[48:50, :] = True
elif dims == 3:
check_mask[48:50, :, :] = True
assert np.all(data == check_mask)
@pytest.mark.parametrize('surface, dims', [('45', 2),
('45_mirror', 2),
('horizontal', 2),
('vertical', 2),
('45', 3),
('45_mirror', 3),
('horizontal', 3),
('vertical', 3)])
def test_boundary_points(self, surface, dims):
"""Check that boundary points are correctly identified"""
# Create the SDF
sdf = read_sdf(surface, dims)
bg = BoundaryGeometry(sdf)
# Check that the boundary points recovers the mask for that sdf
check_mask = np.zeros(bg.grid.shape, dtype=bool)
check_mask[bg.boundary_points] = True
assert np.all(check_mask == bg.boundary_mask)
# TODO: Wants to test some other surfaces too
# TODO: Like sines, eggboxes, etc
@pytest.mark.parametrize('surface', ['45', 'horizontal'])
def test_dense_pos(self, surface):
"""
Check that the dense version of the point positions is correctly
constructed.
"""
sdf = read_sdf(surface, 2)
bg = BoundaryGeometry(sdf)
# Trim edges off data, as normal calculation in corners is imperfect
slices = tuple([slice(2, -2) for dim in sdf.grid.dimensions])
data = tuple([bg.dense_pos[dim][slices] for dim in range(2)])
if surface == '45':
assert np.all(np.isclose(np.diagonal(data[0], offset=1), 0.5))
assert np.all(np.isclose(np.diagonal(data[0], offset=0), 0))
assert np.all(np.isclose(np.diagonal(data[0], offset=-1), -0.5))
assert np.all(np.isclose(np.diagonal(data[1], offset=1), -0.5))
assert np.all(np.isclose(np.diagonal(data[1], offset=0), 0))
assert np.all(np.isclose(np.diagonal(data[1], offset=-1), 0.5))
elif surface == 'horizontal':
assert np.all(np.isclose(data[0][:, 48], 0))
assert np.all(np.isclose(data[0][:, 49], 0))
assert np.all(np.isclose(data[1][:, 48], 0.5))
assert np.all(np.isclose(data[1][:, 49], -0.5))
@pytest.mark.parametrize('surface, dims', [('45_mirror', 2),
('horizontal', 2),
('horizontal', 3)])
def test_interior_mask_unstaggered(self, surface, dims):
"""Check that the interior mask is correct in unstaggered case"""
sdf = read_sdf(surface, dims)
bg = BoundaryGeometry(sdf)
origin = tuple([sp.core.numbers.Zero() for dim in range(dims)])
# Trim edges off data, as normal calculation in corners is imperfect
slices = tuple([slice(4, -4) for dim in sdf.grid.dimensions])
# Create a meshgrid of indices
if dims == 2:
x, z = np.meshgrid(np.arange(bg.grid.shape[0]),
np.arange(bg.grid.shape[1]), indexing='ij')
elif dims == 3:
x, y, z = np.meshgrid(np.arange(bg.grid.shape[0]),
np.arange(bg.grid.shape[1]),
np.arange(bg.grid.shape[2]), indexing='ij')
if surface == '45_mirror':
check_mask = x + z < 100
elif surface == 'horizontal':
check_mask = z < 50
assert np.all(bg.interior_mask[origin][slices] == check_mask[slices])
@pytest.mark.parametrize('surface', ['45_mirror', 'horizontal'])
@pytest.mark.parametrize('setup', [0, 1])
def test_interior_mask_staggered(self, surface, setup):
"""Check that the interior masks are correct in the staggered case"""
sdf = read_sdf(surface, 2)
sdf_x = read_sdf(surface + '_x', 2)
sdf_y = read_sdf(surface + '_y', 2)
sdfs = (sdf, sdf_x, sdf_y)
# Trim edges off data, as normal calculation in corners is imperfect
slices = tuple([slice(4, -4) for dim in sdf.grid.dimensions])
x, y = sdf.grid.dimensions
h_x = x.spacing
h_y = y.spacing
zero = sp.core.numbers.Zero()
if setup == 0:
cutoff = None
elif setup == 1:
cutoff = cutoff = {(h_x/2, zero): 0, (zero, h_y/2): 0}
bg = BoundaryGeometry(sdfs, cutoff=cutoff)
xmsh, ymsh = np.meshgrid(np.arange(bg.grid.shape[0]),
np.arange(bg.grid.shape[1]), indexing='ij')
if surface == '45_mirror' and setup == 0:
check_mask = xmsh + ymsh < 100
for origin in bg.interior_mask:
check = bg.interior_mask[origin][slices] == check_mask[slices]
assert np.all(check)
elif surface == '45_mirror' and setup == 1:
check_mask = xmsh + ymsh < 100
check_mask_stagger = xmsh + ymsh < 101
for origin in bg.interior_mask:
if origin == (zero, zero):
check = bg.interior_mask[origin][slices] \
== check_mask[slices]
else:
check = bg.interior_mask[origin][slices] \
== check_mask_stagger[slices]
assert np.all(check)
elif surface == 'horizontal' and setup == 0:
check_mask = ymsh < 50
for origin in bg.interior_mask:
check = bg.interior_mask[origin][slices] == check_mask[slices]
assert np.all(check)
elif surface == 'horizontal' and setup == 1:
check_mask = ymsh < 50
check_mask_stagger = ymsh < 51
for origin in bg.interior_mask:
if origin == (zero, zero) or origin == (zero, h_y/2):
check = bg.interior_mask[origin][slices] \
== check_mask[slices]
else:
check = bg.interior_mask[origin][slices] \
== check_mask_stagger[slices]
assert np.all(check)
def test_1d_boundary_masks(self):
"""Test the 1D versions of the boundary masks"""
sdf = read_sdf('45_mirror', 2)
bg = BoundaryGeometry(sdf)
# Trim edges as geometry calculation gets weird here
slices = tuple([slice(2, -2) for dim in sdf.grid.dimensions])
# In the 45 degree cases, both masks should be the same
assert np.all(bg.b_mask_1D[0][slices] == bg.b_mask_1D[1][slices])
assert np.count_nonzero(bg.b_mask_1D[0][slices]) == 97
@pytest.mark.parametrize('setup',
[0, 1, 2, 3, 4])
def test_cutoff(self, setup):
"""Test that the dictionary of cutoffs is correctly constructed"""
sdf = read_sdf('45_mirror', 2)
staggered_setups = [2, 3, 4]
if setup in staggered_setups:
sdf_x = read_sdf('45_mirror_x', 2)
sdf_y = read_sdf('45_mirror_y', 2)
sdfs = (sdf, sdf_x, sdf_y)
else:
sdfs = sdf
grid = sdf.grid
x, y = grid.dimensions
h_x = x.spacing
h_y = y.spacing
zero = sp.core.numbers.Zero()
# Need to have the dimensions, so done inside test rather than
# parameterization
if setup == 0:
cutoff = None
answer = {(zero, zero): 0.5}
elif setup == 1:
cutoff = {(zero, zero): 0.5, (h_x/2, zero): 0, (zero, h_y/2): 0}
answer = {(zero, zero): 0.5, (h_x/2, zero): 0, (zero, h_y/2): 0}
elif setup == 2:
cutoff = None
answer = {(zero, zero): 0.5, (h_x/2, zero): 0.5,
(zero, h_y/2): 0.5}
elif setup == 3:
cutoff = {(h_x/2, zero): 0, (zero, h_y/2): 0}
answer = {(zero, zero): 0.5, (h_x/2, zero): 0,
(zero, h_y/2): 0}
elif setup == 4:
cutoff = {(zero, zero): 0, (h_x/2, zero): 0,
(zero, h_y/2): 0}
answer = {(zero, zero): 0, (h_x/2, zero): 0,
(zero, h_y/2): 0}
bg = BoundaryGeometry(sdfs, cutoff=cutoff)
assert bg.cutoff == frozendict(answer)