Debian Codegraph @

packages/stk/stk_unit_tests/stk_middle_mesh/test_mesh_quality_improver.cpp

raw
1 #include "gtest/gtest.h"
2
3 #include "stk_middle_mesh/boundary_fixture.hpp"
4 #include "stk_middle_mesh/create_mesh.hpp"
5 #include "stk_middle_mesh/create_mesh_quality_improver.hpp"
6 #include "stk_middle_mesh/mesh_boundary_snapper.hpp"
7 #include "stk_middle_mesh/mesh_quality_improver.hpp"
8 #include "stk_middle_mesh/regularized_distortion_metric.hpp"
9 #include "util/meshes.hpp"
10
11 #include "stk_middle_mesh/mesh_io.hpp" //TODO: DEBUGGING
12
13 namespace stk {
14 namespace middle_mesh {
15 namespace impl {
16
17 using namespace mesh;
18 using namespace mesh::impl;
19
20 namespace {
21
22 MeshEntityPtr find_closest_vert(std::shared_ptr<Mesh> mesh, const utils::Point& pt, double tol=10000)
23 {
24 double minDist = std::numeric_limits<double>::max();
25 MeshEntityPtr minVert = nullptr;
26 for (auto& vert : mesh->get_vertices())
27 if (vert)
28 {
29 auto pt2 = vert->get_point_orig(0);
30 auto disp = pt - pt2;
31 double dist = dot(disp, disp);
32 if (dist < minDist && dist < tol)
33 {
34 minDist = dist;
35 minVert = vert;
36 }
37 }
38
39 return minVert;
40 }
41
42 void test_meshes_identical(std::shared_ptr<Mesh> mesh1, std::shared_ptr<Mesh> mesh2, const double tol)
43 {
44 auto& verts1 = mesh1->get_vertices();
45 auto& verts2 = mesh2->get_vertices();
46
47 EXPECT_EQ(verts1.size(), verts2.size());
48
49 for (unsigned int i = 0; i < verts1.size(); ++i)
50 {
51 auto v1 = verts1[i];
52 auto v2 = verts2[i];
53
54 EXPECT_NEAR(v1->get_point_orig(0).x, v2->get_point_orig(0).x, tol);
55 EXPECT_NEAR(v1->get_point_orig(0).y, v2->get_point_orig(0).y, tol);
56 }
57 }
58
59 } // namespace
60
61 TEST(MeshQualityImprover, Ideal)
62 {
63 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
64 GTEST_SKIP();
65
66 MeshSpec spec;
67 spec.numelX = 3;
68 spec.numelY = 3;
69 spec.xmin = 0;
70 spec.xmax = 1;
71 spec.ymin = 0;
72 spec.ymax = 1;
73
74 auto func = [&](const utils::Point& pt) { return pt; };
75
76 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
77 std::shared_ptr<Mesh> mesh2 = create_mesh(spec, func);
78
79 BoundaryFixture filter(mesh);
80
81 auto fixer = make_standard_improver(mesh, filter, {.nlayers = -1});
82 EXPECT_EQ(fixer->count_invalid_points(), 0);
83 fixer->run();
84
85 test_meshes_identical(mesh, mesh2, 1e-13);
86 }
87
88 TEST(MeshQualityImprover, SinglePoint)
89 {
90 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4 || utils::impl::comm_size(MPI_COMM_WORLD) == 3)
91 GTEST_SKIP();
92
93 MeshSpec spec;
94 spec.numelX = 2;
95 spec.numelY = 2;
96 spec.xmin = 0;
97 spec.xmax = 1;
98 spec.ymin = 0;
99 spec.ymax = 1;
100
101 auto func = [&](const utils::Point& pt) { return pt; };
102
103 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
104 std::shared_ptr<Mesh> mesh2 = create_mesh(spec, func);
105 MeshEntityPtr vert = find_closest_vert(mesh, utils::Point(0.5, 0.5));
106
107 auto metric = std::make_shared<RegularizedDistortionMetric<double>>();
108
109 BoundaryFixture filter(mesh);
110
111 auto fixer = make_standard_improver(mesh, filter, {.nlayers = -1, .maxDeltaX = 1e-13});
112 EXPECT_EQ(fixer->count_invalid_points(), 0);
113
114 // move the vertex
115 vert->set_point_orig(0, utils::Point(0.55, 0.65));
116
117 // the fixer should return the vertex to its original location
118 fixer->run();
119
120 test_meshes_identical(mesh, mesh2, 1e-13);
121 }
122
123 TEST(MeshQualityImprover, FourPoint)
124 {
125 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
126 GTEST_SKIP();
127
128 // in this case, there are 4 dofs, but only one of them is moved from
129 // the initial position
130 MeshSpec spec;
131 spec.numelX = 3;
132 spec.numelY = 3;
133 spec.xmin = 0;
134 spec.xmax = 1;
135 spec.ymin = 0;
136 spec.ymax = 1;
137
138 auto func = [&](const utils::Point& pt) { return pt; };
139
140 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
141 std::shared_ptr<Mesh> mesh2 = create_mesh(spec, func);
142 MeshEntityPtr vert = find_closest_vert(mesh, utils::Point(1.0 / 3.0, 1.0 / 3.0), 0.1);
143
144 auto metric = std::make_shared<RegularizedDistortionMetric<double>>();
145
146 BoundaryFixture filter(mesh);
147
148 auto fixer = make_standard_improver(mesh, filter, {.nlayers = -1, .maxDeltaX = 1e-15, .itermax = 100});
149
150 // move the vertex
151 if (vert)
152 vert->set_point_orig(0, utils::Point(0.4, 0.45));
153 EXPECT_EQ(fixer->count_invalid_points(), 0);
154
155 // the fixer should return the vertex to its original location
156 fixer->run();
157
158 test_meshes_identical(mesh, mesh2, 1e-11);
159 }
160
161 TEST(MeshQualityImprover, FourPointInvalid)
162 {
163 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
164 GTEST_SKIP();
165
166 // in this case, there are 4 dofs, but only one of them is moved from
167 // the initial position
168 MeshSpec spec;
169 spec.numelX = 3;
170 spec.numelY = 3;
171 spec.xmin = 0;
172 spec.xmax = 1;
173 spec.ymin = 0;
174 spec.ymax = 1;
175
176 auto func = [&](const utils::Point& pt) { return pt; };
177
178 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
179 std::shared_ptr<Mesh> mesh2 = create_mesh(spec, func);
180 MeshEntityPtr vert = find_closest_vert(mesh, utils::Point(1.0 / 3.0, 1.0 / 3.0), 0.1);
181
182 auto metric = std::make_shared<RegularizedDistortionMetric<double>>(1e-3);
183
184 BoundaryFixture filter(mesh);
185
186 auto fixer =
187 make_standard_improver(mesh, filter, {.nlayers = -1, .maxDeltaX = 1e-14, .itermax = 100, .delta = 1e-3});
188
189 // move the vertex to an invalid location
190 if (vert)
191 vert->set_point_orig(0, utils::Point(0.75, 0.75));
192 EXPECT_EQ(fixer->count_invalid_points(), 4);
193
194 // the fixer should return the vertex to its original location
195 fixer->run();
196
197 EXPECT_EQ(fixer->count_invalid_points(), 0);
198 test_meshes_identical(mesh, mesh2, 1e-10);
199 }
200
201 TEST(MeshQualityImprover, FourPointAllInvalid)
202 {
203 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
204 GTEST_SKIP();
205
206 // in this case, there are 4 dofs, but only one of them is moved from
207 // the initial position
208 MeshSpec spec;
209 spec.numelX = 3;
210 spec.numelY = 3;
211 spec.xmin = 0;
212 spec.xmax = 1;
213 spec.ymin = 0;
214 spec.ymax = 1;
215
216 auto func = [&](const utils::Point& pt) { return utils::Point(pt.x, 0, pt.y); };
217
218 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
219 std::shared_ptr<Mesh> mesh2 = create_mesh(spec, func);
220 MeshEntityPtr vert1 = find_closest_vert(mesh, utils::Point(1.0 / 3.0, 0, 1.0 / 3.0), 0.1);
221 MeshEntityPtr vert2 = find_closest_vert(mesh, utils::Point(2.0 / 3.0, 0, 1.0 / 3.0), 0.1);
222 MeshEntityPtr vert3 = find_closest_vert(mesh, utils::Point(2.0 / 3.0, 0, 2.0 / 3.0), 0.1);
223 MeshEntityPtr vert4 = find_closest_vert(mesh, utils::Point(1.0 / 3.0, 0, 2.0 / 3.0), 0.1);
224
225 BoundaryFixture filter(mesh);
226
227 auto fixer =
228 make_standard_improver(mesh, filter, {.nlayers = -1, .maxDeltaX = 1e-14, .itermax = 100, .delta = 1e-3});
229
230 // move all vertices to an invalid location
231 if (vert1)
232 vert1->set_point_orig(0, utils::Point(0.75, 0, 0.75));
233 if (vert2)
234 vert2->set_point_orig(0, utils::Point(0.25, 0, 0.75));
235 if (vert3)
236 vert3->set_point_orig(0, utils::Point(0.25, 0, 0.25));
237 if (vert4)
238 vert4->set_point_orig(0, utils::Point(0.75, 0, 0.25));
239
240 EXPECT_EQ(fixer->count_invalid_points(), 4);
241
242 // the fixer should return the vertex to its original location
243 fixer->run();
244
245 EXPECT_EQ(fixer->count_invalid_points(), 0);
246 test_meshes_identical(mesh, mesh2, 1e-8);
247 }
248
249 TEST(MeshQualityImprover, 4By4)
250 {
251 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
252 GTEST_SKIP();
253
254 // in this case, there are 4 dofs, but only one of them is moved from
255 // the initial position
256 MeshSpec spec;
257 spec.numelX = 4;
258 spec.numelY = 4;
259 spec.xmin = 0;
260 spec.xmax = 1;
261 spec.ymin = 0;
262 spec.ymax = 1;
263
264 auto func = [&](const utils::Point& pt) { return pt; };
265
266 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
267 std::shared_ptr<Mesh> mesh2 = create_mesh(spec, func);
268
269 BoundaryFixture filter(mesh);
270
271 auto fixer =
272 make_standard_improver(mesh, filter, {.nlayers = -1, .maxDeltaX = 1e-10, .itermax = 1000, .delta = 1e-3});
273
274 double delta = 0.25;
275 for (int i=1; i <= 3; ++i)
276 for (int j=1; j <= 3; ++j)
277 {
278 MeshEntityPtr vert = find_closest_vert(mesh, utils::Point(i*delta, j*delta), 0.01);
279 if (vert)
280 {
281 utils::Point pt = vert->get_point_orig(0);
282 std::cout << "perturbing vert at " << pt << std::endl;
283 vert->set_point_orig(0, pt + utils::Point(0.125, 0.125));
284 }
285 }
286
287 // the fixer should return the vertex to its original location
288 fixer->run();
289
290 EXPECT_EQ(fixer->count_invalid_points(), 0);
291 test_meshes_identical(mesh, mesh2, 1e-8);
292 }
293
294 // TODO: do a rotating partial sphere
295
296 } // namespace impl
297 } // namespace middle_mesh
298 } // namespace stk