Debian Codegraph @

python/test/unit/mesh/test_cell.py

raw
1 """Unit tests for the Cell class"""
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 import pytest
21 import numpy
22
23 from dolfin import *
24 from dolfin_utils.test import skip_in_parallel, skip_in_release
25
26 def test_unit_interval():
27 mesh = UnitIntervalMesh.create(MPI.comm_self, 1)
28
29 @skip_in_parallel
30 def test_distance_interval():
31
32 mesh = UnitIntervalMesh.create(1)
33 cell = Cell(mesh, 0)
34
35 assert round(cell.distance(Point(-1.0)) - 1.0, 7) == 0
36 assert round(cell.distance(Point(0.5)) - 0.0, 7) == 0
37
38
39 @skip_in_parallel
40 def test_distance_triangle():
41
42 mesh = UnitSquareMesh(1, 1)
43 cell = Cell(mesh, 1)
44
45 assert round(cell.distance(Point(-1.0, -1.0)) - numpy.sqrt(2), 7) == 0
46 assert round(cell.distance(Point(-1.0, 0.5)) - 1, 7) == 0
47 assert round(cell.distance(Point(0.5, 0.5)) - 0.0, 7) == 0
48
49
50 @skip_in_parallel
51 def test_distance_tetrahedron():
52
53 mesh = UnitCubeMesh(1, 1, 1)
54 cell = Cell(mesh, 5)
55
56 assert round(cell.distance(Point(-1.0, -1.0, -1.0))-numpy.sqrt(3), 7) == 0
57 assert round(cell.distance(Point(-1.0, 0.5, 0.5)) - 1, 7) == 0
58 assert round(cell.distance(Point(0.5, 0.5, 0.5)) - 0.0, 7) == 0
59
60
61 @skip_in_release
62 @skip_in_parallel
63 def test_issue_568():
64 mesh = UnitSquareMesh(4, 4)
65 cell = Cell(mesh, 0)
66
67 # Should throw an error, not just segfault (only works in DEBUG mode!)
68 with pytest.raises(RuntimeError):
69 cell.facet_area(0)
70
71 # Should work after initializing the connectivity
72 mesh.init(2, 1)
73 cell.facet_area(0)
74
75
76 def test_volume_quadrilateralR2():
77
78 mesh = UnitSquareMesh.create(MPI.comm_self, 1, 1, CellType.Type.quadrilateral)
79 cell = Cell(mesh, 0)
80
81 assert cell.volume() == 1.0
82
83
84 @pytest.mark.parametrize('coordinates', [[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0]],
85 [[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 1.0]]])
86 def test_volume_quadrilateralR3(coordinates):
87
88 mesh = Mesh()
89 editor = MeshEditor()
90 editor.open(mesh, "quadrilateral", 2, 3)
91 editor.init_vertices(4)
92 editor.init_cells(1)
93 editor.add_vertex(0, Point(numpy.array(coordinates[0])))
94 editor.add_vertex(1, Point(numpy.array(coordinates[1])))
95 editor.add_vertex(2, Point(numpy.array(coordinates[2])))
96 editor.add_vertex(3, Point(numpy.array(coordinates[3])))
97 editor.add_cell(0,numpy.array([0, 1, 2, 3],dtype=numpy.uintp))
98 editor.close()
99 mesh.init()
100 cell = Cell(mesh, 0)
101
102 assert cell.volume() == 1.0
103
104
105 @pytest.mark.parametrize('scaling', [1e0, 1e-5, 1e-10, 1e-15, 1e-20, 1e-30,
106 1e5, 1e10, 1e15, 1e20, 1e30])
107 def test_volume_quadrilateral_coplanarity_check_1(scaling):
108
109 with pytest.raises(RuntimeError) as error:
110 mesh = Mesh()
111 editor = MeshEditor()
112 editor.open(mesh, "quadrilateral", 2, 3)
113 editor.init_vertices(4)
114 editor.init_cells(1)
115 # Unit square cell scaled down by 'scaling' and the first vertex is distorted so that the vertices are clearly non coplanar
116 editor.add_vertex(0, Point(scaling, 0.5 * scaling, 0.6 * scaling))
117 editor.add_vertex(1, Point(0.0, scaling, 0.0))
118 editor.add_vertex(2, Point(0.0, 0.0, scaling))
119 editor.add_vertex(3, Point(0.0, scaling, scaling))
120 editor.add_cell(0,numpy.array([0, 1, 2, 3],dtype=numpy.uintp))
121 editor.close()
122 mesh.init()
123 cell = Cell(mesh, 0)
124 volume = cell.volume()
125
126 assert "are not coplanar" in str(error.value)
127
128
129 # Test when |p0-p3| is ~ 1 but |p1-p2| is small
130 # The cell is degenerate when scale is below 1e-17, it is expected to fail the test.
131 @pytest.mark.parametrize('scaling', [1e0, 1e-5, 1e-10, 1e-15])
132 def test_volume_quadrilateral_coplanarity_check_2(scaling):
133
134 with pytest.raises(RuntimeError) as error:
135 mesh = Mesh()
136 editor = MeshEditor()
137 editor.open(mesh, "quadrilateral", 2, 3)
138 editor.init_vertices(4)
139 editor.init_cells(1)
140 # Unit square cell scaled down by 'scaling' and the first vertex is distorted so that the vertices are clearly non coplanar
141 editor.add_vertex(0, Point(1.0, 0.5, 0.6))
142 editor.add_vertex(1, Point(0.0, scaling, 0.0))
143 editor.add_vertex(2, Point(0.0, 0.0, scaling))
144 editor.add_vertex(3, Point(0.0, 1.0, 1.0))
145 editor.add_cell(0,numpy.array([0, 1, 2, 3],dtype=numpy.uintp))
146 editor.close()
147 mesh.init()
148 cell = Cell(mesh, 0)
149 volume = cell.volume()
150
151 assert "are not coplanar" in str(error.value)