Debian Codegraph @

python/test/unit/mesh/test_mesh_function.py

raw
1 """Unit tests for MeshFunctions"""
2
3 # Copyright (C) 2011 Garth N. Wells
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.random
22 from dolfin import *
23 from dolfin_utils.test import fixture, skip_in_parallel
24
25
26 @pytest.fixture(params=range(4))
27 def name(request):
28 names = [3, 0, 1, 2]
29 return names[request.param]
30
31
32 @pytest.fixture(params=range(4))
33 def tp(request):
34 tps = ['int', 'size_t', 'bool', 'double']
35 return tps[request.param]
36
37
38 @fixture
39 def mesh():
40 return UnitCubeMesh(3, 3, 3)
41
42
43 @fixture
44 def funcs(mesh):
45 names = [3, 0, 1, 2]
46 tps = ['int', 'size_t', 'bool', 'double']
47 funcs = {}
48 for tp in tps:
49 for name in names:
50 funcs[(tp, name)] = eval("MeshFunction('%s', mesh, %d)" % (tp, name))
51 return funcs
52
53
54 @fixture
55 def cube():
56 return UnitCubeMesh(8, 8, 8)
57
58
59 @fixture
60 def f(cube):
61 return MeshFunction('int', cube, 0)
62
63
64 def test_size(tp, name, funcs, mesh):
65 if name == 0:
66 a = len(funcs[(tp, name)])
67 b = mesh.num_vertices()
68 assert a == b
69 else:
70 a = len(funcs[(tp, name)])
71 b = mesh.num_entities(name)
72 assert a == b
73
74
75 def test_access_type(tp, name, funcs):
76 type_dict = dict(int=int, size_t=int, double=float, bool=bool)
77 assert isinstance(funcs[(tp, name)][0], type_dict[tp])
78
79
80 def test_numpy_access(funcs, tp, name):
81 values = funcs[(tp, name)].array()
82 values[:] = numpy.random.rand(len(values))
83 assert all(values[i] == funcs[(tp, name)][i] for i in range(len(values)))
84
85
86 def test_setvalues(tp, funcs, name):
87 if tp != 'bool':
88 with pytest.raises(TypeError):
89 funcs[(tp, name)].__setitem__(len(funcs[(tp, name)])-1, "jada")
90
91
92 def test_Create(cube):
93 """Create MeshFunctions."""
94
95 v = MeshFunction("size_t", cube, 0)
96 assert v.size() == cube.num_vertices()
97
98 v = MeshFunction("size_t", cube, 1)
99 assert v.size() == cube.num_edges()
100
101 v = MeshFunction("size_t", cube, 2)
102 assert v.size() == cube.num_facets()
103
104 v = MeshFunction("size_t", cube, 3)
105 assert v.size() == cube.num_cells()
106
107
108 def test_CreateAssign(cube):
109 """Create MeshFunctions with value."""
110 i = 10
111 v = MeshFunction("size_t", cube, 0, i)
112 assert v.size() == cube.num_vertices()
113 assert v[0] == i
114
115 v = MeshFunction("size_t", cube, 1, i)
116 assert v.size() == cube.num_edges()
117 assert v[0] == i
118
119 v = MeshFunction("size_t", cube, 2, i)
120 assert v.size() == cube.num_facets()
121 assert v[0] == i
122
123 v = MeshFunction("size_t", cube, 3, i)
124 assert v.size() == cube.num_cells()
125 assert v[0] == i
126
127
128 def test_Assign(f, cube):
129 f = f
130 f[3] = 10
131 v = Vertex(cube, 3)
132 assert f[v] == 10
133
134
135 @skip_in_parallel
136 def test_meshfunction_where_equal():
137 mesh = UnitSquareMesh(2, 2)
138
139 cf = MeshFunction("size_t", mesh, mesh.topology().dim())
140 cf.set_all(1)
141 cf[0] = 3
142 cf[3] = 3
143 assert list(cf.where_equal(3)) == [0, 3]
144 assert list(cf.where_equal(1)) == [1, 2, 4, 5, 6, 7]
145
146 ff = MeshFunction("size_t", mesh, mesh.topology().dim()-1)
147 ff.set_all(0)
148 ff[0] = 1
149 ff[2] = 3
150 ff[3] = 3
151 assert list(ff.where_equal(1)) == [0]
152 assert list(ff.where_equal(3)) == [2, 3]
153 assert list(ff.where_equal(0)) == [1] + list(range(4, ff.size()))
154
155 vf = MeshFunction("size_t", mesh, 0)
156 vf.set_all(3)
157 vf[1] = 1
158 vf[2] = 1
159 assert list(vf.where_equal(1)) == [1, 2]
160 assert list(vf.where_equal(3)) == [0] + list(range(3, vf.size()))