Debian Codegraph @

packages/stk/stk_unit_tests/stk_middle_mesh/test_boundary_snapper.cpp

raw
1 #include "gtest/gtest.h"
2
3 #include "stk_middle_mesh/create_mesh.hpp"
4 #include "stk_middle_mesh/element_operations_2d.hpp"
5 #include "stk_middle_mesh/mesh_boundary_snapper.hpp"
6 #include "stk_middle_mesh/mesh_io.hpp" //TODO: DEBUGGING
7 #include "stk_middle_mesh/field.hpp"
8
9 #include "util/meshes.hpp"
10
11 namespace stk {
12 namespace middle_mesh {
13 namespace impl {
14
15 using namespace stk::middle_mesh::mesh;
16 using namespace stk::middle_mesh::mesh::impl;
17
18 namespace {
19
20 std::shared_ptr<Mesh> make_square_boundary(std::vector<double>& xCoords)
21 {
22 std::sort(xCoords.begin(), xCoords.end());
23
24 auto mesh = make_empty_mesh();
25 std::vector<MeshEntityPtr> vertsBottom, vertsTop;
26 for (auto& xCoord : xCoords)
27 vertsBottom.push_back(mesh->create_vertex(xCoord, 0, 0));
28
29 for (auto& xCoord : xCoords)
30 vertsTop.push_back(mesh->create_vertex(xCoord, 1, 0));
31
32 for (size_t i = 1; i < vertsBottom.size(); ++i)
33 mesh->create_quad_from_verts(vertsBottom[i - 1], vertsBottom[i], vertsTop[i], vertsTop[i - 1]);
34
35 return mesh;
36 }
37
38 void test_edge_lengths_positive(std::shared_ptr<Mesh> mesh, int nverts)
39 {
40 auto& verts = mesh->get_vertices();
41 for (int i = 1; i < nverts; ++i)
42 EXPECT_GE(verts[i]->get_point_orig(0).x, verts[i - 1]->get_point_orig(0).x);
43 }
44
45 void test_edges_only(std::vector<double> verts1, std::vector<double> verts2)
46 {
47 auto mesh1 = make_square_boundary(verts1);
48 auto mesh2 = make_square_boundary(verts2);
49
50 MeshBoundarySnapper snapper;
51 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
52
53 test_edge_lengths_positive(mesh1, verts1.size());
54 test_edge_lengths_positive(mesh2, verts2.size());
55
56 mesh1 = make_square_boundary(verts2);
57 mesh2 = make_square_boundary(verts1);
58 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
59
60 test_edge_lengths_positive(mesh1, verts2.size());
61 test_edge_lengths_positive(mesh2, verts1.size());
62 }
63
64 void test_areas_positive(std::shared_ptr<Mesh> mesh)
65 {
66 mesh::impl::ElementOperations2D elemOps;
67 for (auto& e : mesh->get_elements())
68 if (e)
69 {
70 EXPECT_GE(elemOps.compute_area(e), 0);
71 }
72 }
73
74 void test_areas_equal(std::shared_ptr<Mesh> mesh1, std::shared_ptr<Mesh> mesh2, MPI_Comm unionComm = MPI_COMM_WORLD)
75 {
76 mesh::impl::ElementOperations2D elemOps;
77 double area1Local = 0.0, area2Local = 0.0;
78
79 if (mesh1)
80 area1Local = elemOps.compute_area(mesh1);
81
82 if (mesh2)
83 area2Local = elemOps.compute_area(mesh2);
84
85 const int root = 0;
86 int nprocs = utils::impl::comm_size(unionComm);
87 std::vector<double> areas1(nprocs), areas2(nprocs);
88 MPI_Gather(&area1Local, 1, MPI_DOUBLE, areas1.data(), 1, MPI_DOUBLE, root, unionComm);
89 MPI_Gather(&area2Local, 1, MPI_DOUBLE, areas2.data(), 1, MPI_DOUBLE, root, unionComm);
90
91 double area1Sum = 0.0, area2Sum = 0.0;
92 for (int i=0; i < nprocs; ++i)
93 {
94 area1Sum += areas1[i];
95 area2Sum += areas2[i];
96 }
97
98 EXPECT_FLOAT_EQ(area1Sum, area2Sum);
99 }
100
101 } // namespace
102
103 TEST(MeshBoundarySnapper, Identical1D)
104 {
105 if (utils::impl::comm_size(MPI_COMM_WORLD) > 1)
106 GTEST_SKIP();
107
108 test_edges_only({0, 0.2, 0.4, 1}, {0, 0.2, 0.4, 1});
109 }
110
111 TEST(MeshBoundarySnapper, Subdivision1D)
112 {
113 if (utils::impl::comm_size(MPI_COMM_WORLD) > 1)
114 GTEST_SKIP();
115
116 test_edges_only({0, 0.2, 0.4, 1}, {0, 0.1, 0.2, 0.3, 0.4, 0.7, 1});
117 }
118
119 TEST(MeshBoundarySnapper, Dense1D)
120 {
121 if (utils::impl::comm_size(MPI_COMM_WORLD) > 1)
122 GTEST_SKIP();
123
124 test_edges_only({0, 0.2, 0.4, 1}, {0, 0.01, 0.02, 0.03, 0.04, 0.1, 0.2, 0.3, 0.4, 0.7, 1});
125 // testEdgesOnly({0, 0.01, 0.02, 0.03, 0.04, 0.1, 0.2, 0.3, 0.4, 0.7, 1}, {0, 0.2, 0.4, 1});
126 // testEdgesOnly({0, 0.03, 0.06, 0.09, 0.012, 0.3, 0.6, 0.9, 1.2, 2.1, 3}, {0, 0.6, 1.2, 3});
127 }
128
129 TEST(MeshBoundarySnapper, Identical)
130 {
131 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
132 GTEST_SKIP();
133
134 MeshSpec spec;
135 spec.numelX = 3;
136 spec.numelY = 4;
137 spec.xmin = 0;
138 spec.xmax = 1;
139 spec.ymin = 0;
140 spec.ymax = 1;
141
142 auto func = [&](const utils::Point& pt) { return pt; };
143
144 std::shared_ptr<Mesh> mesh1 = create_mesh(spec, func);
145 std::shared_ptr<Mesh> mesh2 = create_mesh(spec, func);
146
147 MeshBoundarySnapper snapper;
148 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
149
150 test_areas_equal(mesh1, mesh2);
151 }
152
153 TEST(MeshBoundarySnapper, Identical2)
154 {
155 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
156 GTEST_SKIP();
157
158 MeshSpec spec;
159 spec.numelX = 3;
160 spec.numelY = 4;
161 spec.xmin = 0;
162 spec.xmax = 1;
163 spec.ymin = 0;
164 spec.ymax = 1;
165
166 MeshSpec spec2;
167 spec2.numelX = 3;
168 spec2.numelY = 4;
169 spec2.xmin = 0;
170 spec2.xmax = 1;
171 spec2.ymin = 0;
172 spec2.ymax = 1;
173
174 auto func = [&](const utils::Point& pt) { return pt; };
175
176 std::shared_ptr<Mesh> mesh1 = create_mesh(spec, func);
177 std::shared_ptr<Mesh> mesh2 = create_mesh(spec2, func);
178
179 MeshBoundarySnapper snapper;
180 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
181
182 test_areas_equal(mesh1, mesh2);
183 }
184
185 TEST(MeshBoundarySnapper, Subdivision)
186 {
187 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
188 GTEST_SKIP();
189
190 MeshSpec spec, spec2;
191 spec.numelX = 3;
192 spec.numelY = 4;
193 spec.xmin = 0;
194 spec.xmax = 1;
195 spec.ymin = 0;
196 spec.ymax = 1;
197
198 spec2.numelX = 6;
199 spec2.numelY = 8;
200 spec2.xmin = 0;
201 spec2.xmax = 1;
202 spec2.ymin = 0;
203 spec2.ymax = 1;
204
205 auto func = [&](const utils::Point& pt) { return pt; };
206
207 std::shared_ptr<Mesh> mesh1 = create_mesh(spec, func);
208 std::shared_ptr<Mesh> mesh2 = create_mesh(spec2, func);
209
210 MeshBoundarySnapper snapper;
211 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
212
213 test_areas_equal(mesh1, mesh2);
214 }
215
216 TEST(MeshBoundarySnapper, QuarterAnnulus)
217 {
218 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
219 GTEST_SKIP();
220
221 // std::cout << std::setprecision(16) << std::endl;
222 double pi = std::atan(1) * 4;
223 int nmeshes = 30;
224
225 for (int i = 0; i < nmeshes; ++i)
226 {
227 // std::cout << "mesh " << i + 1 << " / " << nmeshes << std::endl;
228 MeshSpec spec, spec2;
229 spec.numelX = 5;
230 spec.numelY = 5;
231 spec.xmin = 0.5;
232 spec.xmax = 1.5;
233 spec.ymin = 0;
234 spec.ymax = pi / 2;
235
236 spec2.numelX = 5 + i;
237 spec2.numelY = 5 + i;
238 spec2.xmin = 0.5;
239 spec2.xmax = 1.5;
240 spec2.ymin = 0;
241 spec2.ymax = pi / 2;
242
243 // remap coordinates to annulus
244
245 // auto func = [&](const utils::Point& pt) { return pt; };
246
247 auto func = [&](const utils::Point& pt) {
248 // interpret x and y as r and theta
249 double r = pt.x;
250 double theta = pt.y;
251
252 double x = r * std::cos(theta);
253 double y = r * std::sin(theta);
254 double z = 0.0;
255 utils::Point pt2(x, y, z);
256 return pt2;
257 };
258
259 // std::cout << "creating mesh1" << std::endl;
260 std::shared_ptr<Mesh> mesh1 = create_mesh(spec, func);
261
262 // std::cout << "\ncreating mesh2" << std::endl;
263 std::shared_ptr<Mesh> mesh2 = create_mesh(spec2, func);
264
265 // printVertEdges("mesh1_initial", mesh1);
266 // printVertEdges("mesh2_initial", mesh2);
267
268 MeshBoundarySnapper snapper;
269 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
270
271 // printVertEdges("mesh1_final", mesh1);
272 // printVertEdges("mesh2_final", mesh2);
273
274 test_areas_positive(mesh1);
275 test_areas_positive(mesh2);
276 test_areas_equal(mesh1, mesh2);
277 }
278 }
279
280
281 TEST(MeshBoundarySnapper, PeriodicAnnulus)
282 {
283 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
284 GTEST_SKIP();
285
286 // std::cout << std::setprecision(16) << std::endl;
287 double pi = std::atan(1) * 4;
288 int nmeshes = 640;
289 double dtheta = pi / (16 * nmeshes);
290
291 for (int i = 0; i < nmeshes; ++i)
292 {
293 // std::cout << "mesh " << i + 1 << " / " << nmeshes << std::endl;
294
295 // std::cout << "creating mesh1" << std::endl;
296 std::shared_ptr<Mesh> mesh1 = make_annulus_mesh(10, 10, 0.5, 1.5, 0);
297
298 // std::cout << "\ncreating mesh2" << std::endl;
299 std::shared_ptr<Mesh> mesh2 = make_annulus_mesh(13, 13, 0.5, 1.5, i * dtheta);
300
301 /*
302 printVertEdges("mesh1_initial", mesh1);
303 printVertEdges("mesh2_initial", mesh2);
304 */
305
306 MeshBoundarySnapper snapper;
307 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
308 /*
309 printVertEdges("mesh1_final", mesh1);
310 printVertEdges("mesh2_final", mesh2);
311 */
312 test_areas_positive(mesh1);
313 test_areas_positive(mesh2);
314 test_areas_equal(mesh1, mesh2);
315 }
316 }
317
318 TEST(MeshBoundarySnapper, PeriodicAnnulusMPMD)
319 {
320 if (utils::impl::comm_size(MPI_COMM_WORLD) > 8 || utils::impl::comm_size(MPI_COMM_WORLD) <= 1)
321 GTEST_SKIP();
322
323 int myRank = utils::impl::comm_rank(MPI_COMM_WORLD);
324 int commSize = utils::impl::comm_size(MPI_COMM_WORLD);
325 int commSizeUsed = commSize - (commSize % 2);
326 int color;
327 if (myRank < commSizeUsed)
328 color = myRank % 2 == 0;
329 else
330 color = MPI_UNDEFINED;
331
332 MPI_Comm meshComm;
333 MPI_Comm_split(MPI_COMM_WORLD, color, 0, &meshComm);
334
335 double pi = std::atan(1) * 4;
336 int nmeshes = 640;
337 double dtheta = pi / (16 * nmeshes);
338
339 for (int i = 0; i < nmeshes; ++i)
340 {
341 std::shared_ptr<Mesh> mesh1, mesh2;
342
343 if (color == 0)
344 mesh1 = make_annulus_mesh(10, 10, 0.5, 1.5, 0, meshComm);
345
346 if (color == 1)
347 mesh2 = make_annulus_mesh(13, 13, 0.5, 1.5, i * dtheta, meshComm);
348
349 /*
350 printVertEdges("mesh1_initial", mesh1);
351 printVertEdges("mesh2_initial", mesh2);
352 */
353
354 MeshBoundarySnapper snapper;
355 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
356 /*
357 printVertEdges("mesh1_final", mesh1);
358 printVertEdges("mesh2_final", mesh2);
359 */
360
361 if (mesh1 != nullptr)
362 test_areas_positive(mesh1);
363 if (mesh2 != nullptr)
364 test_areas_positive(mesh2);
365
366 test_areas_equal(mesh1, mesh2, MPI_COMM_WORLD);
367 }
368
369 if (meshComm != MPI_COMM_NULL)
370 MPI_Comm_free(&meshComm);
371 }
372
373 TEST(MeshBoundarySnapper, TestMeshExchangeBoundaryEdgesGather)
374 {
375 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4 || utils::impl::comm_size(MPI_COMM_WORLD) <= 1)
376 GTEST_SKIP();
377
378 int myRank = utils::impl::comm_rank(MPI_COMM_WORLD);
379
380 MeshSpec spec;
381 spec.numelX = 3;
382 spec.numelY = 4;
383 spec.xmin = 0;
384 spec.xmax = 1;
385 spec.ymin = 0;
386 spec.ymax = 1;
387
388 auto func = [&](const utils::Point& pt) { return pt; };
389
390 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
391
392 MeshExchangeBoundaryEdges exchange(mesh, mesh->get_comm(), 0);
393 auto boundaryEdgeMesh = exchange.get_boundary_edge_mesh();
394
395 if (myRank == 0) {
396 const unsigned numExpectedGatheredEdges = 14;
397 const unsigned numExpectedGatheredVertices = 14;
398
399 EXPECT_EQ(numExpectedGatheredEdges, boundaryEdgeMesh->get_edges().size());
400 EXPECT_EQ(numExpectedGatheredVertices, boundaryEdgeMesh->get_vertices().size());
401 }
402 }
403
404 TEST(MeshBoundarySnapper, TestMeshExchangeBoundaryEdgesRemoteUpdate)
405 {
406 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4 || utils::impl::comm_size(MPI_COMM_WORLD) <= 1)
407 GTEST_SKIP();
408
409 MeshSpec spec;
410 spec.numelX = 3;
411 spec.numelY = 4;
412 spec.xmin = 0;
413 spec.xmax = 1;
414 spec.ymin = 0;
415 spec.ymax = 1;
416
417 auto func = [&](const utils::Point& pt) { return pt; };
418
419 std::shared_ptr<Mesh> mesh = create_mesh(spec, func);
420 auto myRank = utils::impl::comm_rank(MPI_COMM_WORLD);
421
422 auto origVertices = mesh->get_vertices();
423 auto field = create_field<utils::Point>(mesh, FieldShape(1, 0, 0), 1);
424 auto& fieldRef = *field;
425
426 for (auto vertex : origVertices) {
427 fieldRef(vertex, 0, 0) = vertex->get_point_orig(0);
428 }
429
430 MeshExchangeBoundaryEdges exchange(mesh, mesh->get_comm(), 0);
431 auto boundaryEdgeMesh = exchange.get_boundary_edge_mesh();
432
433 if (myRank == 0) {
434 auto vertices = boundaryEdgeMesh->get_vertices();
435 for (auto vertex : vertices) {
436 utils::Point pt = vertex->get_point_orig(0);
437 pt = pt * 2;
438 vertex->set_point_orig(0, pt);
439 }
440 }
441 exchange.update_remote_vertices();
442
443 for (auto vertex : origVertices) {
444 std::vector<MeshEntityPtr> edges;
445 get_upward(vertex, 1, edges);
446
447 for (auto edge : edges)
448 if (edge->count_up() == 1 && edge->count_remote_shared_entities() == 0) {
449 for (auto i = 0; i < 3; ++i)
450 EXPECT_DOUBLE_EQ(vertex->get_point_orig(0)[i], fieldRef(vertex, 0, 0)[i] * 2);
451 }
452 }
453 }
454
455 TEST(MeshBoundarySnapper, SimpleMeshesFinerMesh1)
456 {
457 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
458 GTEST_SKIP();
459
460 MeshSpec spec, spec2;
461 spec.numelX = 5;
462 spec.numelY = 5;
463 spec.xmin = 0;
464 spec.xmax = 1;
465 spec.ymin = 0;
466 spec.ymax = 1;
467
468 spec2.numelX = 4;
469 spec2.numelY = 4;
470 spec2.xmin = 0;
471 spec2.xmax = 1;
472 spec2.ymin = 0;
473 spec2.ymax = 1;
474
475 auto func = [&](const utils::Point& pt) { return pt; };
476
477 std::shared_ptr<Mesh> mesh1 = create_mesh(spec, func);
478 std::shared_ptr<Mesh> mesh2 = create_mesh(spec2, func);
479
480 MeshBoundarySnapper snapper;
481 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
482
483 test_areas_positive(mesh1);
484 test_areas_positive(mesh2);
485 test_areas_equal(mesh1, mesh2);
486 }
487
488 TEST(MeshBoundarySnapper, SimpleMeshesFinerMesh2)
489 {
490 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
491 GTEST_SKIP();
492
493 MeshSpec spec, spec2;
494 spec.numelX = 4;
495 spec.numelY = 4;
496 spec.xmin = 0;
497 spec.xmax = 1;
498 spec.ymin = 0;
499 spec.ymax = 1;
500
501 spec2.numelX = 5;
502 spec2.numelY = 5;
503 spec2.xmin = 0;
504 spec2.xmax = 1;
505 spec2.ymin = 0;
506 spec2.ymax = 1;
507
508 auto func = [&](const utils::Point& pt) { return pt; };
509
510 std::shared_ptr<Mesh> mesh1 = create_mesh(spec, func);
511 std::shared_ptr<Mesh> mesh2 = create_mesh(spec2, func);
512
513 MeshBoundarySnapper snapper;
514 snapper.snap(mesh1, mesh2, MPI_COMM_WORLD);
515
516 test_areas_positive(mesh1);
517 test_areas_positive(mesh2);
518 test_areas_equal(mesh1, mesh2);
519 }
520
521 } // namespace impl
522 } // namespace middle_mesh
523 } // namespace stk