Debian Codegraph @

test/unit/python/mesh/test_meshview.py

raw
1 #!/usr/bin/env py.test
2
3 """Unit tests for MeshViewMapping class"""
4
5 # Copyright (C) 2011 Garth N. Wells
6 #
7 # This file is part of DOLFIN.
8 #
9 # DOLFIN is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # DOLFIN is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
21 #
22 # First added: 2011-02-26
23 # Last changed: 2014-05-30
24
25 import pytest
26 from dolfin import *
27 from dolfin_utils.test import fixture, skip_in_parallel
28
29
30 @fixture
31 def cube():
32 return UnitCubeMesh(5, 5, 5)
33
34
35 @fixture
36 def square():
37 return UnitSquareMesh(5, 5)
38
39
40 @pytest.fixture(scope='module', params=range(2))
41 def meshes(cube, square, request):
42 mesh = [cube, square]
43 return mesh[request.param]
44
45 def test_make_facets_view(square, cube):
46 """Create view of facets"""
47
48 def _check_facets_view(mesh):
49 marker = FacetFunction("size_t", mesh, 0)
50 D = mesh.topology().dim()
51 mesh.init(D-1, D)
52
53 c = 0
54 for f in facets(mesh):
55 if f.num_global_entities(D) == 1:
56 marker[f] = 1
57 c += 1
58
59 m2 = MeshViewMapping.create_from_marker(marker, 1)
60
61 assert m2.num_cells() == c
62 assert m2.num_cells() == len(m2.topology().mapping.cell_map())
63 assert m2.num_vertices() == len(m2.topology().mapping.vertex_map())
64
65 _check_facets_view(square)
66 _check_facets_view(cube)
67
68
69 def test_make_cells_view(square, cube):
70 """Create view of cells"""
71
72 def _check_cells_view(mesh):
73 marker = CellFunction("size_t", mesh, 0)
74
75 ct = 0
76 for c in cells(mesh):
77 if c.midpoint().x() < 0.5:
78 marker[c] = 1
79 ct += 1
80
81 m2 = MeshViewMapping.create_from_marker(marker, 1)
82
83 assert m2.num_cells() == ct
84 assert m2.num_cells() == len(m2.topology().mapping.cell_map())
85 assert m2.num_vertices() == len(m2.topology().mapping.vertex_map())
86
87 _check_cells_view(square)
88 _check_cells_view(cube)
89
90
91 def test_make_edges_view(cube):
92 """Create view of edges"""
93
94 marker = EdgeFunction("size_t", cube, 0)
95 cube.init(1, 3)
96
97 c = 0
98 for e in edges(cube):
99 if (e.midpoint().x() == 0 or e.midpoint().x() == 1):
100 marker[e] = 1
101 c += 1
102 elif (e.midpoint().y() == 0 or e.midpoint().y() == 1):
103 marker[e] = 1
104 c += 1
105 elif (e.midpoint().z() == 0 or e.midpoint().z() == 1):
106 marker[e] = 1
107 c += 1
108
109 m2 = MeshViewMapping.create_from_marker(marker, 1)
110
111 assert m2.num_cells() == c
112 assert m2.num_cells() == len(m2.topology().mapping.cell_map())
113 assert m2.num_vertices() == len(m2.topology().mapping.vertex_map())