Debian Codegraph @

python/test/unit/mesh/test_mesh_iterator.py

raw
1 "Unit tests for MeshIterator and subclasses"
2
3 # Copyright (C) 2006-2011 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: 2006-08-08
21 # Last changed: 2011-08-21
22
23 import pytest
24 import numpy
25 from dolfin import *
26
27
28 def test_vertex_iterators():
29 "Iterate over vertices"
30
31 mesh = UnitCubeMesh(5, 5, 5)
32 for i in range(4):
33 mesh.init(0, i)
34
35 # Test connectivity
36 cons = [(i, mesh.topology()(0,i)) for i in range(4)]
37
38 # Test writability
39 for i, con in cons:
40 def assign(con, i):
41 con(i)[0] = 1
42 with pytest.raises(Exception):
43 assign(con, i)
44
45 n = 0
46 for i, v in enumerate(vertices(mesh)):
47 n += 1
48 for j, con in cons:
49 assert numpy.all(con(i) == v.entities(j))
50
51 assert n == mesh.num_vertices()
52
53 # Check coordinate assignment
54 # FIXME: Outcomment to hopefully please Mac-buildbot
55 #end_point = numpy.array([v.x(0), v.x(1), v.x(2)])
56 #mesh.coordinates()[:] += 2
57 #assert end_point[0] + 2 == mesh.coordinates()[-1,0]
58 #assert end_point[1] + 2 == mesh.coordinates()[-1,1]
59 #assert end_point[2] + 2 == mesh.coordinates()[-1,2]
60
61 def test_edge_iterators():
62 "Iterate over edges"
63
64 mesh = UnitCubeMesh(5, 5, 5)
65 for i in range(4):
66 mesh.init(1, i)
67
68 # Test connectivity
69 cons = [(i, mesh.topology()(1,i)) for i in range(4)]
70
71 # Test writability
72 for i, con in cons:
73 def assign(con, i):
74 con(i)[0] = 1
75 with pytest.raises(Exception):
76 assign(con, i)
77
78 n = 0
79 for i, e in enumerate(edges(mesh)):
80 n += 1
81 for j, con in cons:
82 assert numpy.all(con(i) == e.entities(j))
83
84 assert n == mesh.num_edges()
85
86 def test_face_iterator():
87 "Iterate over faces"
88
89 mesh = UnitCubeMesh(5, 5, 5)
90 for i in range(4):
91 mesh.init(2, i)
92
93 # Test connectivity
94 cons = [(i, mesh.topology()(2,i)) for i in range(4)]
95
96 # Test writability
97 for i, con in cons:
98 def assign(con, i):
99 con(i)[0] = 1
100 with pytest.raises(Exception):
101 assign(con, i)
102
103 n = 0
104 for i, f in enumerate(faces(mesh)):
105 n += 1
106 for j, con in cons:
107 assert numpy.all(con(i) == f.entities(j))
108
109 assert n == mesh.num_faces()
110
111 def test_facet_iterators():
112 "Iterate over facets"
113 mesh = UnitCubeMesh(5, 5, 5)
114 n = 0
115 for f in facets(mesh):
116 n += 1
117 assert n == mesh.num_facets()
118
119 def test_cell_iterators():
120 "Iterate over cells"
121 mesh = UnitCubeMesh(5, 5, 5)
122 for i in range(4):
123 mesh.init(3, i)
124
125 # Test connectivity
126 cons = [(i, mesh.topology()(3,i)) for i in range(4)]
127
128 # Test writability
129 for i, con in cons:
130 def assign(con, i):
131 con(i)[0] = 1
132 with pytest.raises(Exception):
133 assign(con, i)
134
135 n = 0
136 for i, c in enumerate(cells(mesh)):
137 n += 1
138 for j, con in cons:
139 assert numpy.all(con(i) == c.entities(j))
140
141 assert n == mesh.num_cells()
142
143 # Test non destruction of MeshEntities
144 cell_list = [c for c in cells(mesh)]
145 assert sum(c.volume() for c in cell_list) == sum(c.volume() for c in cells(mesh))
146
147 def test_mixed_iterators():
148 "Iterate over vertices of cells"
149
150 mesh = UnitCubeMesh(5, 5, 5)
151 n = 0
152 for c in cells(mesh):
153 for v in vertices(c):
154 n += 1
155 assert n == 4*mesh.num_cells()