Debian Codegraph @

test/unit/cpp/mesh/Mesh.cpp

raw
1 // Copyright (C) 2007 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Unit tests for the mesh library
19
20 #include <dolfin.h>
21 #include <catch.hpp>
22
23 using namespace dolfin;
24
25 //-----------------------------------------------------------------------------
26 TEST_CASE("Simple shapes test")
27 {
28 SECTION("Test UnitSquareMesh")
29 {
30 // Create mesh of unit square
31 UnitSquareMesh mesh(5, 7);
32 CHECK(mesh.num_vertices() == (std::size_t) 48);
33 CHECK(mesh.num_cells() == (std::size_t) 70);
34
35 // Create mesh of unit square
36 auto mesh1 = UnitSquareMesh::create({{5, 7}}, CellType::Type::triangle);
37 CHECK(mesh1.num_vertices() == (std::size_t) 48);
38 CHECK(mesh1.num_cells() == (std::size_t) 70);
39 }
40
41 SECTION("Test UnitCubeMesh")
42 {
43 // Create mesh of unit cube
44 UnitCubeMesh mesh(5, 7, 9);
45 CHECK(mesh.num_vertices() == (std::size_t) 480);
46 CHECK(mesh.num_cells() == (std::size_t) 1890);
47
48 // Create mesh of unit cube
49 auto mesh1 = UnitCubeMesh::create({{5, 7, 9}}, CellType::Type::tetrahedron);
50 CHECK(mesh1.num_vertices() == (std::size_t) 480);
51 CHECK(mesh1.num_cells() == (std::size_t) 1890);
52 }
53 }
54
55 TEST_CASE("Mesh refinement")
56 {
57 SECTION("Test refine UnitSquareMesh")
58 {
59 // Refine mesh of unit square
60 UnitSquareMesh mesh0(5, 7);
61 Mesh mesh1 = refine(mesh0);
62 CHECK(mesh1.num_vertices() == (std::size_t) 165);
63 CHECK(mesh1.num_cells() == (std::size_t) 280);
64 }
65
66 SECTION("Test refine UnitCubeMesh")
67 {
68 // Refine mesh of unit cube
69 UnitCubeMesh mesh0(5, 7, 9);
70 Mesh mesh1 = refine(mesh0);
71 CHECK(mesh1.num_vertices() == (std::size_t) 3135);
72 CHECK(mesh1.num_cells() == (std::size_t) 15120);
73 }
74 }
75
76 TEST_CASE("Mesh iterators")
77 {
78 SECTION("Test vertex iterators")
79 {
80 // Iterate over vertices
81 UnitCubeMesh mesh(5, 5, 5);
82 unsigned int n = 0;
83 for (VertexIterator v(mesh); !v.end(); ++v)
84 n++;
85
86 CHECK(n == mesh.num_vertices());
87 }
88
89 SECTION("Test edge iterators")
90 {
91 // Iterate over edges
92 UnitCubeMesh mesh(5, 5, 5);
93 unsigned int n = 0;
94 for (EdgeIterator e(mesh); !e.end(); ++e)
95 n++;
96
97 CHECK(n == mesh.num_edges());
98 }
99
100 SECTION("Test face iterators")
101 {
102 // Iterate over faces
103 UnitCubeMesh mesh(5, 5, 5);
104 unsigned int n = 0;
105 for (FaceIterator f(mesh); !f.end(); ++f)
106 n++;
107
108 CHECK(n == mesh.num_faces());
109 }
110
111 SECTION("Test facet iterators")
112 {
113 // Iterate over facets
114 UnitCubeMesh mesh(5, 5, 5);
115 unsigned int n = 0;
116 for (FacetIterator f(mesh); !f.end(); ++f)
117 n++;
118
119 CHECK(n == mesh.num_facets());
120 }
121
122 SECTION("Test cell iterators")
123 {
124 // Iterate over cells
125 UnitCubeMesh mesh(5, 5, 5);
126 unsigned int n = 0;
127 for (CellIterator c(mesh); !c.end(); ++c)
128 n++;
129
130 CHECK(n == mesh.num_cells());
131 }
132
133 SECTION("Test mixed iterators")
134 {
135 // Iterate over vertices of cells
136 UnitCubeMesh mesh(5, 5, 5);
137 unsigned int n = 0;
138 for (CellIterator c(mesh); !c.end(); ++c)
139 for (VertexIterator v(*c); !v.end(); ++v)
140 n++;
141
142 CHECK(n == 4*mesh.num_cells());
143 }
144
145 SECTION("Test boundary computation")
146 {
147 // Compute boundary of mesh
148 UnitCubeMesh mesh(2, 2, 2);
149 BoundaryMesh boundary(mesh, "exterior");
150 CHECK(boundary.num_vertices() == (std::size_t) 26);
151 CHECK(boundary.num_cells() == (std::size_t) 48);
152 }
153 }
154
155 TEST_CASE("Boundary extraction")
156 {
157 SECTION("Test boundary of boundary")
158 {
159 // Compute boundary of boundary
160 //
161 // Note that we can't do
162 //
163 // BoundaryMesh b0(mesh);
164 // BoundaryMesh b1(b0);
165 //
166 // since b1 would then be a copy of b0 (copy
167 // constructor in Mesh will be used).
168
169 UnitCubeMesh mesh(2, 2, 2);
170 BoundaryMesh b0(mesh, "exterior");
171 b0.order();
172 BoundaryMesh b1(b0, "exterior");
173 CHECK(b1.num_vertices() == (std::size_t) 0);
174 CHECK(b1.num_cells() == (std::size_t) 0);
175 }
176
177 SECTION("Test assign")
178 {
179 /// Assign value of mesh function
180 auto mesh = std::make_shared<UnitSquareMesh>(3, 3);
181 MeshFunction<int> f(mesh, 0);
182 f[3] = 10;
183 Vertex v(*mesh, 3);
184 CHECK(f[v] == 10);
185 }
186 }
187
188 TEST_CASE("InputOutput")
189 {
190 SECTION("Test mesh XML 2D")
191 {
192 // Write and read 2D mesh to/from file
193 UnitSquareMesh mesh_out(3, 3);
194 Mesh mesh_in;
195 File file("unitsquare.xml");
196 file << mesh_out;
197 file >> mesh_in;
198 CHECK(mesh_in.num_vertices() == (std::size_t) 16);
199 }
200
201 SECTION("Test mesh XML 3D")
202 {
203 // Write and read 3D mesh to/from file
204 UnitCubeMesh mesh_out(3, 3, 3);
205 Mesh mesh_in;
206 File file("unitcube.xml");
207 file << mesh_out;
208 file >> mesh_in;
209 CHECK(mesh_in.num_vertices() == (std::size_t) 64);
210 }
211
212 SECTION("Test MeshFunction")
213 {
214 // Write and read mesh function to/from file
215 auto mesh = std::make_shared<UnitSquareMesh>(1, 1);
216 MeshFunction<int> f(mesh, 0);
217 f[0] = 2;
218 f[1] = 4;
219 f[2] = 6;
220 f[3] = 8;
221 File file("meshfunction.xml");
222 file << f;
223 MeshFunction<int> g(mesh, 0);
224 file >> g;
225 for (VertexIterator v(*mesh); !v.end(); ++v)
226 CHECK(f[*v] == g[*v]);
227 }
228 }
229
230 TEST_CASE("PyCCInterface")
231 {
232 SECTION("Test get geometrical dimension")
233 {
234 // Get geometrical dimension of mesh
235 UnitSquareMesh mesh(5, 5);
236 CHECK(mesh.geometry().dim() == (std::size_t) 2);
237 }
238
239 SECTION("Test get coordinates")
240 {
241 // Get coordinates of vertices
242 UnitSquareMesh mesh(5, 5);
243 CHECK(mesh.geometry().num_vertices() == (std::size_t) 36);
244 }
245
246 SECTION("Test get cells")
247 {
248 // Get cells of mesh
249 UnitSquareMesh mesh(5, 5);
250 CHECK(mesh.topology().size(2) == (std::size_t) 50);
251 }
252 }