Debian Codegraph @

python/test/unit/mesh/test_mesh_value_collection.py

raw
1 """Unit tests for MeshValueCollection"""
2
3 # Copyright (C) 2011 Johan Hake
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: 2011-03-10
21 # Last changed: 2011-03-10
22
23 import numpy.random
24 from dolfin import *
25
26
27 def test_assign_2D_cells():
28 mesh = UnitSquareMesh(3, 3)
29 ncells = mesh.num_cells()
30 f = MeshValueCollection("int", mesh, 2)
31 all_new = True
32 for cell in cells(mesh):
33 value = ncells - cell.index()
34 all_new = all_new and f.set_value(cell.index(), value)
35 g = MeshValueCollection("int", mesh, 2)
36 g.assign(f)
37 assert ncells == f.size()
38 assert ncells == g.size()
39 assert all_new
40
41 for cell in cells(mesh):
42 value = ncells - cell.index()
43 assert value, g.get_value(cell.index() == 0)
44
45 old_value = g.get_value(0, 0)
46 g.set_value(0, 0, old_value + 1)
47 assert old_value + 1 == g.get_value(0, 0)
48
49
50 def test_assign_2D_facets():
51 mesh = UnitSquareMesh(3, 3)
52 mesh.init(2, 1)
53 ncells = mesh.num_cells()
54 f = MeshValueCollection("int", mesh, 1)
55 all_new = True
56 for cell in cells(mesh):
57 value = ncells - cell.index()
58 for i, facet in enumerate(facets(cell)):
59 all_new = all_new and f.set_value(cell.index(), i, value + i)
60
61 g = MeshValueCollection("int", mesh, 1)
62 g.assign(f)
63 assert ncells*3 == f.size()
64 assert ncells*3 == g.size()
65 assert all_new
66
67 for cell in cells(mesh):
68 value = ncells - cell.index()
69 for i, facet in enumerate(facets(cell)):
70 assert value+i == g.get_value(cell.index(), i)
71
72
73 def test_assign_2D_vertices():
74 mesh = UnitSquareMesh(3, 3)
75 mesh.init(2, 0)
76 ncells = mesh.num_cells()
77 f = MeshValueCollection("int", mesh, 0)
78 all_new = True
79 for cell in cells(mesh):
80 value = ncells - cell.index()
81 for i, vert in enumerate(vertices(cell)):
82 all_new = all_new and f.set_value(cell.index(), i, value+i)
83
84 g = MeshValueCollection("int", mesh, 0)
85 g.assign(f)
86 assert ncells*3 == f.size()
87 assert ncells*3 == g.size()
88 assert all_new
89
90 for cell in cells(mesh):
91 value = ncells - cell.index()
92 for i, vert in enumerate(vertices(cell)):
93 assert value+i == g.get_value(cell.index(), i)
94
95
96 def test_mesh_function_assign_2D_cells():
97 mesh = UnitSquareMesh(3, 3)
98 ncells = mesh.num_cells()
99 f = MeshFunction("int", mesh, mesh.topology().dim())
100 for cell in cells(mesh):
101 f[cell] = ncells - cell.index()
102
103 g = MeshValueCollection("int", mesh, 2)
104 g.assign(f)
105 assert ncells == f.size()
106 assert ncells == g.size()
107
108 f2 = MeshFunction("int", mesh, g)
109
110 for cell in cells(mesh):
111 value = ncells - cell.index()
112 assert value == g.get_value(cell.index(), 0)
113 assert f2[cell] == g.get_value(cell.index(), 0)
114
115 h = MeshValueCollection("int", mesh, 2)
116 global_indices = mesh.topology().global_indices(2)
117 ncells_global = mesh.num_entities_global(2)
118 for cell in cells(mesh):
119 if global_indices[cell.index()] in [5, 8, 10]:
120 continue
121 value = ncells_global - global_indices[cell.index()]
122 h.set_value(cell.index(), int(value))
123
124 f3 = MeshFunction("int", mesh, h)
125
126 values = f3.array()
127 values[values > ncells_global] = 0.
128
129 info(str(values))
130 info(str(values.sum()))
131
132 assert MPI.sum(mesh.mpi_comm(), values.sum()*1.0) == 140.
133
134
135 def test_mesh_function_assign_2D_facets():
136 mesh = UnitSquareMesh(3, 3)
137 mesh.init(1)
138 f = MeshFunction("int", mesh, mesh.topology().dim()-1, 25)
139 for cell in cells(mesh):
140 for i, facet in enumerate(facets(cell)):
141 assert 25 == f[facet]
142
143 g = MeshValueCollection("int", mesh, 1)
144 g.assign(f)
145 assert mesh.num_facets() == f.size()
146 assert mesh.num_cells()*3 == g.size()
147 for cell in cells(mesh):
148 for i, facet in enumerate(facets(cell)):
149 assert 25 == g.get_value(cell.index(), i)
150
151 f2 = MeshFunction("int", mesh, g)
152
153 for cell in cells(mesh):
154 for i, facet in enumerate(facets(cell)):
155 assert f2[facet] == g.get_value(cell.index(), i)
156
157
158 def test_mesh_function_assign_2D_vertices():
159 mesh = UnitSquareMesh(3, 3)
160 mesh.init(0)
161 f = MeshFunction("int", mesh, 0, 25)
162 g = MeshValueCollection("int", mesh, 0)
163 g.assign(f)
164 assert mesh.num_vertices() == f.size()
165 assert mesh.num_cells()*3 == g.size()
166
167 f2 = MeshFunction("int", mesh, g)
168
169 for cell in cells(mesh):
170 for i, vert in enumerate(vertices(cell)):
171 assert 25 == g.get_value(cell.index(), i)
172 assert f2[vert] == g.get_value(cell.index(), i)