| 1 |
"""Unit tests for intersection computation""" |
| 2 |
|
| 3 |
# Copyright (C) 2013 Anders Logg |
| 4 |
# |
| 5 |
# This file is part of DOLFIN. |
| 6 |
# |
| 7 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# DOLFIN is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
# First added: 2013-12-09 |
| 21 |
# Last changed: 2017-02-16 |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
from dolfin import * |
| 26 |
from dolfin_utils.test import skip_in_parallel |
| 27 |
import numpy as np |
| 28 |
|
| 29 |
@skip_in_parallel |
| 30 |
def test_issue_97(): |
| 31 |
"Test from Mikael Mortensen (issue #97)" |
| 32 |
|
| 33 |
N = 2 |
| 34 |
L = 1000 |
| 35 |
mesh = BoxMesh(Point(0, 0, 0), Point(L, L, L), N, N, N) |
| 36 |
V = FunctionSpace(mesh, 'CG', 1) |
| 37 |
v = interpolate(Expression('x[0]', degree=1), V) |
| 38 |
x = Point(0.5*L, 0.5*L, 0.5*L) |
| 39 |
vx = v(x) |
| 40 |
|
| 41 |
@skip_in_parallel |
| 42 |
def test_issue_168(): |
| 43 |
"Test from Torsten Wendav (issue #168)" |
| 44 |
|
| 45 |
mesh = UnitCubeMesh(14, 14, 14) |
| 46 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 47 |
v = Function(V) |
| 48 |
x = (0.75, 0.25, 0.125) |
| 49 |
vx = v(x) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.skipif(True, reason="Since cell.contains(point) is doing an exact calculation, we cannot assume that the midpoint is exactly in the cell") |
| 53 |
@skip_in_parallel |
| 54 |
def test_segment_collides_point_3D_2(): |
| 55 |
"""Test case by Oyvind from https://bitbucket.org/fenics-project/dolfin/issue/296 for segment point collision in 3D""" |
| 56 |
mesh = Mesh() |
| 57 |
editor = MeshEditor() |
| 58 |
editor.open(mesh, 1, 3) |
| 59 |
editor.init_vertices(2) |
| 60 |
editor.init_cells(1) |
| 61 |
editor.add_vertex(0, np.array( (41.06309891, 63.74219894, 68.10320282), dtype='float') ) |
| 62 |
editor.add_vertex(1, np.array( (41.45830154, 62.61560059, 66.43019867), dtype='float') ) |
| 63 |
editor.add_cell(0, np.array( (0,1), dtype='uint')) |
| 64 |
editor.close() |
| 65 |
cell = Cell(mesh, 0) |
| 66 |
assert cell.contains(cell.midpoint()) |
| 67 |
|
| 68 |
|
| 69 |
def _test_collision_robustness_2d(aspect, y, step): |
| 70 |
nx = 10 |
| 71 |
ny = int(aspect*nx) |
| 72 |
mesh = UnitSquareMesh(nx, ny, 'crossed') |
| 73 |
bb = mesh.bounding_box_tree() |
| 74 |
|
| 75 |
x = 0.0 |
| 76 |
p = Point(x, y) |
| 77 |
while x <= 1.0: |
| 78 |
c = bb.compute_first_entity_collision(Point(x, y)) |
| 79 |
assert c < np.uintc(-1) |
| 80 |
x += step |
| 81 |
|
| 82 |
#@pytest.mark.skipif(True, reason="Not implemented in 3D") |
| 83 |
@skip_in_parallel |
| 84 |
def _test_collision_robustness_3d(aspect, y, z, step): |
| 85 |
nx = nz = 10 |
| 86 |
ny = int(aspect*nx) |
| 87 |
mesh = UnitCubeMesh(nx, ny, nz) |
| 88 |
bb = mesh.bounding_box_tree() |
| 89 |
|
| 90 |
x = 0.0 |
| 91 |
while x <= 1.0: |
| 92 |
c = bb.compute_first_entity_collision(Point(x, y, z)) |
| 93 |
assert c < np.uintc(-1) |
| 94 |
x += step |
| 95 |
|
| 96 |
@skip_in_parallel |
| 97 |
@pytest.mark.slow |
| 98 |
def test_collision_robustness_slow(): |
| 99 |
"""Test cases from https://bitbucket.org/fenics-project/dolfin/issue/296""" |
| 100 |
_test_collision_robustness_2d( 100, 1e-14, 1e-5) |
| 101 |
_test_collision_robustness_2d( 40, 1e-03, 1e-5) |
| 102 |
_test_collision_robustness_2d( 100, 0.5 + 1e-14, 1e-5) |
| 103 |
_test_collision_robustness_2d(4.43, 0.5, 4.03e-6) |
| 104 |
_test_collision_robustness_3d( 100, 1e-14, 1e-14, 1e-5) |
| 105 |
|
| 106 |
@skip_in_parallel |
| 107 |
@pytest.mark.slow |
| 108 |
@pytest.mark.skipif(True, reason='Very slow test cases') |
| 109 |
def test_collision_robustness_very_slow(): |
| 110 |
"""Test cases from https://bitbucket.org/fenics-project/dolfin/issue/296""" |
| 111 |
_test_collision_robustness_2d( 10, 1e-16, 1e-7) |
| 112 |
_test_collision_robustness_2d(4.43, 1e-17, 4.03e-6) |
| 113 |
_test_collision_robustness_2d( 40, 0.5, 1e-6) |
| 114 |
_test_collision_robustness_2d( 10, 0.5 + 1e-16, 1e-7) |
| 115 |
|
| 116 |
@skip_in_parallel |
| 117 |
def test_points_on_line(): |
| 118 |
"""Test case from https://bitbucket.org/fenics-project/dolfin/issues/790""" |
| 119 |
big = 1e6 |
| 120 |
p1 = np.array((0.1, 0.06), dtype='float') |
| 121 |
p3 = np.array((big*2.1, big*0.1), dtype='float') |
| 122 |
p2 = np.array((0.0, big*3.0), dtype='float') |
| 123 |
p0 = np.array((big*3.0, 0.0), dtype='float') |
| 124 |
|
| 125 |
mesh = Mesh() |
| 126 |
ed = MeshEditor() |
| 127 |
ed.open(mesh, "triangle", 2, 2) |
| 128 |
ed.init_cells(3) |
| 129 |
ed.init_vertices(4) |
| 130 |
ed.add_vertex(0, p0) |
| 131 |
ed.add_vertex(1, p1) |
| 132 |
ed.add_vertex(2, p2) |
| 133 |
ed.add_vertex(3, p3) |
| 134 |
ed.add_cell(0, np.array( (2, 3, 0), dtype='uint')) |
| 135 |
ed.add_cell(1, np.array( (0, 1, 3 ), dtype='uint')) |
| 136 |
ed.add_cell(2, np.array( (1, 2, 3 ), dtype='uint')) |
| 137 |
ed.close() |
| 138 |
|
| 139 |
# xdmf = XDMFFile("a.xdmf") |
| 140 |
# xdmf.write(mesh) |
| 141 |
|
| 142 |
# print mesh.cells() |
| 143 |
# print mesh.coordinates() |
| 144 |
|
| 145 |
bb = mesh.bounding_box_tree() |
| 146 |
|
| 147 |
# Find a point on line somewhere between p3 and p1 |
| 148 |
j = 4 |
| 149 |
pq = (p3*j + p1*(50-j))/50.0 |
| 150 |
c = bb.compute_entity_collisions(Point(pq[0], pq[1])) |
| 151 |
# print pq.str(), c |
| 152 |
|
| 153 |
# Check that the neighbouring points are in the correct cells |
| 154 |
cell_numbers = [1, 2, 2, 1, 1, 2, 1, 1, 2] |
| 155 |
step = 1e-6 |
| 156 |
cnt = 0 |
| 157 |
for i in range(-1, 2): |
| 158 |
for j in range(-1, 2): |
| 159 |
pt = Point(pq[0], pq[1]) + Point(step*i, step*j) |
| 160 |
c = bb.compute_entity_collisions(pt) |
| 161 |
assert c[0] == cell_numbers[cnt] |
| 162 |
cnt += 1 |