File: test_bounding_box_tree.py

package info (click to toggle)
dolfin 2019.2.0~legacy20240219.1c52e83-18
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 31,700 kB
  • sloc: xml: 104,040; cpp: 102,227; python: 24,356; sh: 460; makefile: 330; javascript: 226
file content (483 lines) | stat: -rwxr-xr-x 13,456 bytes parent folder | download | duplicates (6)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
"""Unit tests for BoundingBoxTree"""

# Copyright (C) 2013-2014 Anders Logg
#
# This file is part of DOLFIN.
#
# DOLFIN is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DOLFIN is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.

import pytest
import numpy

from dolfin import BoundingBoxTree
from dolfin import UnitIntervalMesh, UnitSquareMesh, UnitCubeMesh
from dolfin import Point
from dolfin import MeshEntity
from dolfin import MPI
from dolfin_utils.test import skip_in_parallel


#--- compute_collisions with point ---

@skip_in_parallel
def test_compute_collisions_point_1d():

    reference = {1: set([4])}

    p = Point(0.3)
    mesh = UnitIntervalMesh(16)
    for dim in range(1, 2):
        tree = BoundingBoxTree()
        tree.build(mesh, dim)
        entities = tree.compute_collisions(p)
        assert set(entities) == reference[dim]

@skip_in_parallel
def test_compute_collisions_point_2d():

    reference = {1: set([226]),
                  2: set([136, 137])}

    p = Point(0.3, 0.3)
    mesh = UnitSquareMesh(16, 16)
    for dim in range(1, 3):
        tree = BoundingBoxTree()
        tree.build(mesh, dim)
        entities = tree.compute_collisions(p)
        for e in entities:
            ent = MeshEntity(mesh, dim, e)
            mp = ent.midpoint()
            x = (mp.x(), mp.y())
            print("test: {}".format(x))
        #assert set(entities) == reference[dim]

@skip_in_parallel
def test_compute_collisions_point_3d():

    reference = {1: set([1364]),
                  2: set([1967, 1968, 1970, 1972, 1974, 1976]),
                  3: set([876, 877, 878, 879, 880, 881])}

    p = Point(0.3, 0.3, 0.3)
    mesh = UnitCubeMesh(8, 8, 8)
    for dim in range(1, 4):
        tree = BoundingBoxTree()
        tree.build(mesh, dim)
        entities = tree.compute_collisions(p)

        # FIXME: Face and edges tests are excluded because test
        # mistakingly relies on the face and edge indices
        tdim = mesh.topology().dim()
        if dim != tdim - 1 and dim != tdim - 2:
            assert set(entities) == reference[dim]

#--- compute_collisions with tree ---

@skip_in_parallel
def test_compute_collisions_tree_1d():

    references = [[set([8, 9, 10, 11, 12, 13, 14, 15]),
                    set([0, 1, 2, 3, 4, 5, 6, 7])],
                  [set([14, 15]),
                    set([0, 1])]]

    points = [Point(0.52), Point(0.9)]

    for i, point in enumerate(points):

        mesh_A = UnitIntervalMesh(16)
        mesh_B = UnitIntervalMesh(16)

        mesh_B.translate(point)

        tree_A = BoundingBoxTree()
        tree_A.build(mesh_A)

        tree_B = BoundingBoxTree()
        tree_B.build(mesh_B)

        entities_A, entities_B = tree_A.compute_collisions(tree_B)

        assert set(entities_A) == references[i][0]
        assert set(entities_B) == references[i][1]

@skip_in_parallel
def test_compute_collisions_tree_2d():

    references = [[set([20, 21, 22, 23, 28, 29, 30, 31]),
                    set([0, 1, 2, 3, 8, 9, 10, 11])],
                  [set([6, 7]),
                    set([24, 25])]]

    points = [Point(0.52, 0.51), Point(0.9, -0.9)]

    for i, point in enumerate(points):

        mesh_A = UnitSquareMesh(4, 4)
        mesh_B = UnitSquareMesh(4, 4)

        mesh_B.translate(point)

        tree_A = BoundingBoxTree()
        tree_A.build(mesh_A)

        tree_B = BoundingBoxTree()
        tree_B.build(mesh_B)

        entities_A, entities_B = tree_A.compute_collisions(tree_B)

        assert set(entities_A) == references[i][0]
        assert set(entities_B) == references[i][1]

@skip_in_parallel
def test_compute_collisions_tree_3d():

    references = [[set([18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]),
                    set([0, 1, 2, 3, 4, 5, 24, 25, 26, 27, 28, 29])],
                  [set([6, 7, 8, 9, 10, 11, 30, 31, 32, 33, 34, 35]),
                    set([12, 13, 14, 15, 16, 17, 36, 37, 38, 39, 40, 41])]]

    points = [Point(0.52, 0.51, 0.3), Point(0.9, -0.9, 0.3)]

    for i, point in enumerate(points):

        mesh_A = UnitCubeMesh(2, 2, 2)
        mesh_B = UnitCubeMesh(2, 2, 2)

        mesh_B.translate(point)

        tree_A = BoundingBoxTree()
        tree_A.build(mesh_A)

        tree_B = BoundingBoxTree()
        tree_B.build(mesh_B)

        entities_A, entities_B = tree_A.compute_collisions(tree_B)

        assert set(entities_A) == references[i][0]
        assert set(entities_B) == references[i][1]

#--- compute_entity_collisions with point ---

@skip_in_parallel
def test_compute_entity_collisions_1d():

    reference = set([4])

    p = Point(0.3)
    mesh = UnitIntervalMesh(16)

    tree = BoundingBoxTree()
    tree.build(mesh)
    entities = tree.compute_entity_collisions(p)
    assert set(entities) == reference

    tree = mesh.bounding_box_tree()
    entities = tree.compute_entity_collisions(p)
    assert set(entities) == reference

@skip_in_parallel
def test_compute_entity_collisions_2d():

    reference = set([136, 137])

    p = Point(0.3, 0.3)
    mesh = UnitSquareMesh(16, 16)

    tree = BoundingBoxTree()
    tree.build(mesh)
    entities = tree.compute_entity_collisions(p)
    assert set(entities) == reference

    tree = mesh.bounding_box_tree()
    entities = tree.compute_entity_collisions(p)
    assert set(entities) == reference

@skip_in_parallel
def test_compute_entity_collisions_3d():

    reference = set([876, 877, 878, 879, 880, 881])

    p = Point(0.3, 0.3, 0.3)
    mesh = UnitCubeMesh(8, 8, 8)

    tree = BoundingBoxTree()
    tree.build(mesh)
    entities = tree.compute_entity_collisions(p)
    assert set(entities) == reference

#--- compute_entity_collisions with tree ---

@skip_in_parallel
def test_compute_entity_collisions_tree_1d():

    references = [[set([8, 9, 10, 11, 12, 13, 14, 15]),
                    set([0, 1, 2, 3, 4, 5, 6, 7])],
                  [set([14, 15]),
                    set([0, 1])]]

    points = [Point(0.52), Point(0.9)]

    for i, point in enumerate(points):

        mesh_A = UnitIntervalMesh(16)
        mesh_B = UnitIntervalMesh(16)

        mesh_B.translate(point)

        tree_A = BoundingBoxTree()
        tree_A.build(mesh_A)

        tree_B = BoundingBoxTree()
        tree_B.build(mesh_B)

        entities_A, entities_B = tree_A.compute_entity_collisions(tree_B)

        assert set(entities_A) == references[i][0]
        assert set(entities_B) == references[i][1]

@skip_in_parallel
def test_compute_entity_collisions_tree_2d():

    references = [[set([20, 21, 22, 23, 28, 29, 30, 31]),
                    set([0, 1, 2, 3, 8, 9, 10, 11])],
                  [set([6]),
                    set([25])]]

    points = [Point(0.52, 0.51), Point(0.9, -0.9)]

    for i, point in enumerate(points):

        mesh_A = UnitSquareMesh(4, 4)
        mesh_B = UnitSquareMesh(4, 4)

        mesh_B.translate(point)

        tree_A = BoundingBoxTree()
        tree_A.build(mesh_A)

        tree_B = BoundingBoxTree()
        tree_B.build(mesh_B)

        entities_A, entities_B = tree_A.compute_entity_collisions(tree_B)

        assert set(entities_A) == references[i][0]
        assert set(entities_B) == references[i][1]

@skip_in_parallel
def test_compute_entity_collisions_tree_3d():

    references = [[set([18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]),
                    set([0, 1, 2, 3, 4, 5, 24, 25, 26, 27, 28, 29])],
                  [set([7, 8, 30, 31, 32]),
                    set([15, 16, 17, 39, 41])]]

    points = [Point(0.52, 0.51, 0.3), Point(0.9, -0.9, 0.3)]

    for i, point in enumerate(points):

        mesh_A = UnitCubeMesh(2, 2, 2)
        mesh_B = UnitCubeMesh(2, 2, 2)

        mesh_B.translate(point)

        tree_A = BoundingBoxTree()
        tree_A.build(mesh_A)

        tree_B = BoundingBoxTree()
        tree_B.build(mesh_B)

        entities_A, entities_B = tree_A.compute_entity_collisions(tree_B)

        assert set(entities_A) == references[i][0]
        assert set(entities_B) == references[i][1]

#--- compute_first_collision with point ---

@skip_in_parallel
def test_compute_first_collision_1d():

    reference = {1: [4]}

    p = Point(0.3)
    mesh = UnitIntervalMesh(16)
    for dim in range(1, 2):
        tree = BoundingBoxTree()
        tree.build(mesh, dim)
        first = tree.compute_first_collision(p)
        assert first in reference[dim]

    tree = mesh.bounding_box_tree()
    first = tree.compute_first_collision(p)
    assert first in reference[mesh.topology().dim()]

@skip_in_parallel
def test_compute_first_collision_2d():

    # FIXME: This test should not use facet indices as there are no guarantees
    # on how DOLFIN numbers facets
    reference = {1: [226],
                  2: [136, 137]}

    p = Point(0.3, 0.3)
    mesh = UnitSquareMesh(16, 16)
    for dim in range(1, 3):
        tree = BoundingBoxTree()
        tree.build(mesh, dim)
        first = tree.compute_first_collision(p)

        # FIXME: Facet test is excluded because it mistakingly relies in the
        # facet indices
        if dim != mesh.topology().dim() - 1:
            assert first in reference[dim]

    tree = mesh.bounding_box_tree()
    first = tree.compute_first_collision(p)
    assert first in reference[mesh.topology().dim()]

@skip_in_parallel
def test_compute_first_collision_3d():

    # FIXME: This test should not use facet indices as there are no guarantees
    # on how DOLFIN numbers facets

    reference = {1: [1364],
                  2: [1967, 1968, 1970, 1972, 1974, 1976],
                  3: [876, 877, 878, 879, 880, 881]}

    p = Point(0.3, 0.3, 0.3)
    mesh = UnitCubeMesh(8, 8, 8)
    for dim in range(1, 4):
        tree = BoundingBoxTree()
        tree.build(mesh, dim)
        first = tree.compute_first_collision(p)

        # FIXME: Face and test is excluded because it mistakingly
        # relies in the facet indices
        tdim = mesh.topology().dim()
        if dim != tdim - 1 and dim != tdim - 2:
            assert first in reference[dim]

    tree = mesh.bounding_box_tree()
    first = tree.compute_first_collision(p)
    assert first in reference[mesh.topology().dim()]

#--- compute_first_entity_collision with point ---

@skip_in_parallel
def test_compute_first_entity_collision_1d():

    reference = [4]

    p = Point(0.3)
    mesh = UnitIntervalMesh(16)
    tree = BoundingBoxTree()
    tree.build(mesh)
    first = tree.compute_first_entity_collision(p)
    assert first in reference

    tree = mesh.bounding_box_tree()
    first = tree.compute_first_entity_collision(p)
    assert first in reference

@skip_in_parallel
def test_compute_first_entity_collision_2d():

    reference = [136, 137]

    p = Point(0.3, 0.3)
    mesh = UnitSquareMesh(16, 16)
    tree = BoundingBoxTree()
    tree.build(mesh)
    first = tree.compute_first_entity_collision(p)
    assert first in reference

    tree = mesh.bounding_box_tree()
    first = tree.compute_first_entity_collision(p)
    assert first in reference

@skip_in_parallel
def test_compute_first_entity_collision_3d():

    reference = [876, 877, 878, 879, 880, 881]

    p = Point(0.3, 0.3, 0.3)
    mesh = UnitCubeMesh(8, 8, 8)
    tree = BoundingBoxTree()
    tree.build(mesh)
    first = tree.compute_first_entity_collision(p)
    assert first in reference

    tree = mesh.bounding_box_tree()
    first = tree.compute_first_entity_collision(p)
    assert first in reference

#--- compute_closest_entity with point ---

@skip_in_parallel
def test_compute_closest_entity_1d():

    reference = (0, 1.0)

    p = Point(-1.0)
    mesh = UnitIntervalMesh(16)
    tree = BoundingBoxTree()
    tree.build(mesh)
    entity, distance = tree.compute_closest_entity(p)

    assert entity == reference[0]
    assert round(distance - reference[1], 7) == 0

    tree = mesh.bounding_box_tree()
    entity, distance = tree.compute_closest_entity(p)
    assert entity == reference[0]
    assert round(distance - reference[1], 7) == 0

@skip_in_parallel
def test_compute_closest_entity_2d():

    reference = (1, 1.0)

    p = Point(-1.0, 0.01)
    mesh = UnitSquareMesh(16, 16)
    tree = BoundingBoxTree()
    tree.build(mesh)
    entity, distance = tree.compute_closest_entity(p)

    assert entity == reference[0]
    assert round(distance - reference[1], 7) == 0

    tree = mesh.bounding_box_tree()
    entity, distance = tree.compute_closest_entity(p)
    assert entity == reference[0]
    assert round(distance - reference[1], 7) == 0

@skip_in_parallel
def test_compute_closest_entity_3d():

    reference = (0, 0.1)

    p = Point(0.1, 0.05, -0.1)
    mesh = UnitCubeMesh(8, 8, 8)
    tree = BoundingBoxTree()
    tree.build(mesh)
    entity, distance = tree.compute_closest_entity(p)

    assert entity == reference[0]
    assert round(distance - reference[1], 7) == 0

    tree = mesh.bounding_box_tree()
    entity, distance = tree.compute_closest_entity(p)
    assert entity == reference[0]
    assert round(distance - reference[1], 7) == 0