Debian Codegraph @

test/unit/cpp/mesh/MeshValueCollection.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 TEST_CASE("MeshValueCollections")
26 {
27 SECTION("Test assign 2D cells")
28 {
29 auto mesh = std::make_shared<UnitSquareMesh>(3, 3);
30 const std::size_t ncells = mesh->num_cells();
31 MeshValueCollection<int> f(mesh, 2);
32 bool all_new = true;
33 for (CellIterator cell(*mesh); !cell.end(); ++cell)
34 {
35 bool this_new;
36 const int value = ncells - cell->index();
37 this_new = f.set_value(cell->index(), value);
38 all_new = all_new && this_new;
39 }
40 MeshValueCollection<int> g(mesh, 2);
41 g = f;
42 CHECK(ncells == f.size());
43 CHECK(ncells == g.size());
44 CHECK(all_new);
45 for (CellIterator cell(*mesh); !cell.end(); ++cell)
46 {
47 const int value = ncells - cell->index();
48 CHECK(value == g.get_value(cell->index(), 0));
49 }
50 }
51
52 SECTION("Test assign 2D facets")
53 {
54 auto mesh = std::make_shared<UnitSquareMesh>(3, 3);
55 mesh->init(2,1);
56 const std::size_t ncells = mesh->num_cells();
57 MeshValueCollection<int> f(mesh, 1);
58 bool all_new = true;
59 for (CellIterator cell(*mesh); !cell.end(); ++cell)
60 {
61 const int value = ncells - cell->index();
62 for (std::size_t i = 0; i < cell->num_entities(1); ++i)
63 {
64 bool this_new;
65 this_new = f.set_value(cell->index(), i, value + i);
66 all_new = all_new && this_new;
67 }
68 }
69 MeshValueCollection<int> g(mesh, 1);
70 g = f;
71 CHECK(ncells*3 == f.size());
72 CHECK(ncells*3 == g.size());
73 CHECK(all_new);
74 for (CellIterator cell(*mesh); !cell.end(); ++cell)
75 {
76 for (std::size_t i = 0; i < cell->num_entities(1); ++i)
77 {
78 const int value = ncells - cell->index() + i;
79 CHECK(value == g.get_value(cell->index(), i));
80 }
81 }
82 }
83
84 SECTION("Test assign 2D vertices")
85 {
86 auto mesh = std::make_shared<UnitSquareMesh>(3, 3);
87 mesh->init(2, 0);
88 const std::size_t ncells = mesh->num_cells();
89 MeshValueCollection<int> f(mesh, 0);
90 bool all_new = true;
91 for (CellIterator cell(*mesh); !cell.end(); ++cell)
92 {
93 const int value = ncells - cell->index();
94 for (std::size_t i = 0; i < cell->num_entities(0); ++i)
95 {
96 bool this_new;
97 this_new = f.set_value(cell->index(), i, value+i);
98 all_new = all_new && this_new;
99 }
100 }
101 MeshValueCollection<int> g(mesh, 0);
102 g = f;
103 CHECK(ncells*3 == f.size());
104 CHECK(ncells*3 == g.size());
105 CHECK(all_new);
106 for (CellIterator cell(*mesh); !cell.end(); ++cell)
107 {
108 for (std::size_t i = 0; i < cell->num_entities(0); ++i)
109 {
110 const int value = ncells - cell->index() + i;
111 CHECK(value == g.get_value(cell->index(), i));
112 }
113 }
114 }
115
116 SECTION("Test MeshFunction assign 2D cells")
117 {
118 auto mesh = std::make_shared<UnitSquareMesh>(3, 3);
119 const std::size_t ncells = mesh->num_cells();
120 MeshFunction<int> f(mesh, 2, 0);
121 for (CellIterator cell(*mesh); !cell.end(); ++cell)
122 f[cell->index()] = ncells - cell->index();
123 MeshValueCollection<int> g(mesh, 2);
124 g = f;
125 CHECK(ncells == f.size());
126 CHECK(ncells == g.size());
127 for (CellIterator cell(*mesh); !cell.end(); ++cell)
128 {
129 const int value = ncells - cell->index();
130 CHECK(value == g.get_value(cell->index(), 0));
131 }
132 }
133
134 SECTION("Test MeshFunction assign 2D facets")
135 {
136 auto mesh = std::make_shared<UnitSquareMesh>(3, 3);
137 mesh->init(1);
138 MeshFunction<int> f(mesh, 1, 25);
139 MeshValueCollection<int> g(mesh, 1);
140 g = f;
141 CHECK(mesh->num_facets() == f.size());
142 CHECK(mesh->num_cells()*3 == g.size());
143 for (CellIterator cell(*mesh); !cell.end(); ++cell)
144 {
145 for (std::size_t i = 0; i < cell->num_entities(1); ++i)
146 CHECK(25 == g.get_value(cell->index(), i));
147 }
148 }
149
150 SECTION("Test MeshFunction assign 2D vertices")
151 {
152 auto mesh = std::make_shared<UnitSquareMesh>(3, 3);
153 mesh->init(0);
154 MeshFunction<int> f(mesh, 0, 25);
155 MeshValueCollection<int> g(mesh, 0);
156 g = f;
157 CHECK(mesh->num_vertices() == f.size());
158 CHECK(mesh->num_cells()*3 == g.size());
159 for (CellIterator cell(*mesh); !cell.end(); ++cell)
160 {
161 for (std::size_t i = 0; i < cell->num_entities(0); ++i)
162 CHECK(25 == g.get_value(cell->index(), i));
163 }
164 }
165 }