Debian Codegraph @

packages/stk/stk_unit_tests/stk_middle_mesh/test_mesh_layers.cpp

raw
1 #include "gtest/gtest.h"
2
3 #include "stk_middle_mesh/create_mesh.hpp"
4 #include "stk_middle_mesh/mesh_layers.hpp"
5
6 namespace stk {
7 namespace middle_mesh {
8 namespace impl {
9
10 using namespace mesh;
11 using namespace mesh::impl;
12
13 namespace {
14
15 MeshEntityPtr find_closest_el(const std::vector<MeshEntityPtr>& entities, const utils::Point& pt, double tol)
16 {
17 MeshEntityPtr entity = nullptr;
18
19 double min_dist = tol;
20 for (auto& e : entities)
21 {
22 auto disp = e->get_point_orig(0) - pt;
23 double dist = std::sqrt(dot(disp, disp));
24 if (dist < min_dist)
25 {
26 min_dist = dist;
27 entity = e;
28 }
29 }
30
31 return entity;
32 }
33
34 class AlwaysTrue
35 {
36 public:
37 bool operator()(MeshEntityPtr) { return true; }
38 };
39
40 class MeshLayersSerialNoFilterTester : public ::testing::Test
41 {
42 protected:
43 void SetUp() override
44 {
45 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
46 GTEST_SKIP();
47
48 MeshSpec spec;
49 spec.numelX = 3;
50 spec.numelY = 3;
51 spec.xmin = 0;
52 spec.xmax = 1;
53 spec.ymin = 0;
54 spec.ymax = 1;
55
56 auto func = [&](const utils::Point& pt) { return pt; };
57
58 mesh = create_mesh(spec, func);
59
60 for (auto& v : mesh->get_vertices())
61 if (v)
62 {
63 auto pt = v->get_point_orig(0);
64 if (std::abs(pt.x) < 1e-13)
65 roots.push_back(v);
66 }
67 }
68
69 std::shared_ptr<Mesh> mesh;
70 std::vector<MeshEntityPtr> roots;
71 AlwaysTrue filter;
72 };
73
74
75 class ExcludedVertFilter
76 {
77 public:
78 ExcludedVertFilter(MeshEntityPtr vert = nullptr) :
79 m_entity(vert)
80 {}
81
82 bool operator()(MeshEntityPtr v) { return v != m_entity; }
83
84 private:
85 MeshEntityPtr m_entity;
86 };
87
88 class MeshLayersSerialFilterTester : public ::testing::Test
89 {
90 protected:
91 void SetUp() override
92 {
93 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
94 GTEST_SKIP();
95
96 MeshSpec spec;
97 spec.numelX = 3;
98 spec.numelY = 3;
99 spec.xmin = 0;
100 spec.xmax = 1;
101 spec.ymin = 0;
102 spec.ymax = 1;
103
104 auto func = [&](const utils::Point& pt) { return pt; };
105
106 mesh = create_mesh(spec, func);
107
108 std::vector<MeshEntityPtr> output;
109 MeshEntityPtr vExclude = nullptr;
110 for (auto& v : mesh->get_vertices())
111 if (v)
112 {
113 auto pt = v->get_point_orig(0);
114 if (std::abs(pt.x) < 1e-13)
115 roots.push_back(v);
116
117 if (std::abs(pt.x - 1.0 / 3.0) < 1e-13 && std::abs(pt.y - 1.0 / 3.0) < 1e-13)
118 vExclude = v;
119 }
120
121 filter = ExcludedVertFilter(vExclude);
122 }
123
124 std::shared_ptr<Mesh> mesh;
125 std::vector<MeshEntityPtr> roots;
126 ExcludedVertFilter filter;
127 };
128
129
130 class MeshLayersParallelNoFilterTester : public ::testing::Test
131 {
132 protected:
133 void SetUp() override
134 {
135 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
136 GTEST_SKIP();
137
138 MeshSpec spec;
139 spec.numelX = 4;
140 spec.numelY = 4;
141 spec.xmin = 0;
142 spec.xmax = 1;
143 spec.ymin = 0;
144 spec.ymax = 1;
145
146 auto func = [&](const utils::Point& pt) { return pt; };
147
148 mesh = create_mesh(spec, func);
149
150 MeshLayers layers(mesh);
151
152 myRank = utils::impl::comm_rank(MPI_COMM_WORLD);
153 if (myRank == 0)
154 for (auto& v : mesh->get_vertices())
155 if (v)
156 {
157 auto pt = v->get_point_orig(0);
158 if (std::abs(pt.y) < 1e-13)
159 roots.push_back(v);
160 }
161 }
162
163 std::shared_ptr<Mesh> mesh;
164 std::vector<MeshEntityPtr> roots;
165 AlwaysTrue filter;
166 int myRank = utils::impl::comm_rank(MPI_COMM_WORLD);
167 };
168
169 class MeshLayersParallelFilterTester : public ::testing::Test
170 {
171 protected:
172 void SetUp() override
173 {
174 if (utils::impl::comm_size(MPI_COMM_WORLD) > 4)
175 GTEST_SKIP();
176
177 MeshSpec spec;
178 spec.numelX = 4;
179 spec.numelY = 4;
180 spec.xmin = 0;
181 spec.xmax = 1;
182 spec.ymin = 0;
183 spec.ymax = 1;
184
185 auto func = [&](const utils::Point& pt) { return pt; };
186
187 mesh = create_mesh(spec, func);
188
189 MeshLayers layers(mesh);
190
191 myRank = utils::impl::comm_rank(MPI_COMM_WORLD);
192 if (myRank == 0)
193 for (auto& v : mesh->get_vertices())
194 if (v)
195 {
196 auto pt = v->get_point_orig(0);
197 if (std::abs(pt.y) < 1e-13)
198 roots.push_back(v);
199 }
200
201 filter = ExcludedVertFilter(find_closest_el(mesh->get_vertices(), {0.5, 0.5}, 1e-10));
202 }
203
204 std::shared_ptr<Mesh> mesh;
205 std::vector<MeshEntityPtr> roots;
206 ExcludedVertFilter filter;
207 int myRank = utils::impl::comm_rank(MPI_COMM_WORLD);
208 };
209
210 }
211
212 TEST_F(MeshLayersSerialNoFilterTester, OneLayer)
213 {
214 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
215 GTEST_SKIP();
216
217 MeshLayers layers(mesh);
218 std::vector<MeshEntityPtr> output;
219 layers.get_layers(filter, roots, 1, output);
220
221 EXPECT_EQ(output.size(), static_cast<unsigned int>(8));
222 for (auto v : output)
223 {
224 double xCoord = v->get_point_orig(0).x;
225 bool isCorrect = std::abs(xCoord) < 1e-13 || std::abs(xCoord - 1.0 / 3.0) < 1e-13;
226 EXPECT_TRUE(isCorrect);
227 }
228 }
229
230
231 TEST_F(MeshLayersSerialNoFilterTester, TwoLayers)
232 {
233 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
234 GTEST_SKIP();
235
236 MeshLayers layers(mesh);
237 std::vector<MeshEntityPtr> output;
238 layers.get_layers(filter, roots, 2, output);
239
240 EXPECT_EQ(output.size(), static_cast<unsigned int>(12));
241 for (auto v : output)
242 {
243 double xCoord = v->get_point_orig(0).x;
244 bool isCorrect =
245 std::abs(xCoord) < 1e-13 || std::abs(xCoord - 1.0 / 3.0) < 1e-13 || std::abs(xCoord - 2.0 / 3.0) < 1e-13;
246 EXPECT_TRUE(isCorrect);
247 }
248 }
249
250
251 TEST_F(MeshLayersSerialNoFilterTester, ThreeLayers)
252 {
253 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
254 GTEST_SKIP();
255
256 MeshLayers layers(mesh);
257 std::vector<MeshEntityPtr> output;
258 layers.get_layers(filter, roots, 3, output);
259 EXPECT_EQ(output.size(), static_cast<unsigned int>(16));
260 EXPECT_TRUE(is_unique(output));
261 }
262
263 TEST_F(MeshLayersSerialFilterTester, OneLayer)
264 {
265 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
266 GTEST_SKIP();
267
268 MeshLayers layers(mesh);
269 std::vector<MeshEntityPtr> output;
270 layers.get_layers(filter, roots, 1, output);
271
272 EXPECT_TRUE(is_unique(output));
273 EXPECT_EQ(output.size(), static_cast<unsigned int>(7));
274 for (auto v : output)
275 {
276 double xCoord = v->get_point_orig(0).x;
277 double yCoord = v->get_point_orig(0).y;
278 bool isCorrect = std::abs(xCoord) < 1e-13 || std::abs(xCoord - 1.0 / 3.0) < 1e-13;
279
280 if (std::abs(xCoord - 1.0 / 3.0) < 1e-13 && std::abs(yCoord - 1.0 / 3.0) < 1e-13)
281 EXPECT_FALSE(true);
282 else
283 EXPECT_TRUE(isCorrect);
284 }
285 }
286
287 TEST_F(MeshLayersSerialFilterTester, TwoLayers)
288 {
289 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
290 GTEST_SKIP();
291
292 MeshLayers layers(mesh);
293 std::vector<MeshEntityPtr> output;
294 layers.get_layers(filter, roots, 2, output);
295
296 EXPECT_TRUE(is_unique(output));
297 EXPECT_EQ(output.size(), static_cast<unsigned int>(10));
298 for (auto v : output)
299 {
300 double xCoord = v->get_point_orig(0).x;
301 double yCoord = v->get_point_orig(0).y;
302 bool isCorrect =
303 std::abs(xCoord) < 1e-13 || std::abs(xCoord - 1.0 / 3.0) < 1e-13 || std::abs(xCoord - 2.0 / 3.0) < 1e-13;
304
305 bool isExcluded = (std::abs(xCoord - 1.0 / 3.0) < 1e-13 && std::abs(yCoord - 1.0 / 3.0) < 1e-13) ||
306 (std::abs(xCoord - 2.0 / 3.0) < 1e-13 && std::abs(yCoord - 1.0 / 3.0) < 1e-13);
307
308 if (isExcluded)
309 EXPECT_FALSE(true);
310 else
311 EXPECT_TRUE(isCorrect);
312 }
313 }
314
315 TEST_F(MeshLayersSerialFilterTester, ThreeLayers)
316 {
317 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
318 GTEST_SKIP();
319
320 MeshLayers layers(mesh);
321 std::vector<MeshEntityPtr> output;
322 layers.get_layers(filter, roots, 3, output);
323
324 EXPECT_TRUE(is_unique(output));
325 EXPECT_EQ(output.size(), static_cast<unsigned int>(14));
326
327 std::set<MeshEntityPtr> verts;
328 for (auto v : mesh->get_vertices())
329 {
330 auto pt = v->get_point_orig(0);
331 bool isExcluded = (std::abs(pt.x - 1.0 / 3.0) < 1e-13 && std::abs(pt.y - 1.0 / 3.0) < 1e-13) ||
332 (std::abs(pt.x - 1.0) < 1e-13 && std::abs(pt.y - 1.0 / 3.0) < 1e-13);
333 if (!isExcluded)
334 verts.insert(v);
335 }
336
337 for (auto v : output)
338 {
339 EXPECT_TRUE(verts.count(v) == 1);
340 }
341 }
342
343 TEST_F(MeshLayersSerialFilterTester, FourLayers)
344 {
345 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
346 GTEST_SKIP();
347
348 MeshLayers layers(mesh);
349 std::vector<MeshEntityPtr> output;
350 layers.get_layers(filter, roots, 4, output);
351 EXPECT_TRUE(is_unique(output));
352 EXPECT_EQ(output.size(), static_cast<unsigned int>(15));
353
354 std::set<MeshEntityPtr> verts;
355 for (auto v : mesh->get_vertices())
356 {
357 auto pt = v->get_point_orig(0);
358 bool isExcluded = (std::abs(pt.x - 1.0 / 3.0) < 1e-13 && std::abs(pt.y - 1.0 / 3.0) < 1e-13);
359 if (!isExcluded)
360 verts.insert(v);
361 }
362
363 for (auto v : output)
364 {
365 EXPECT_TRUE(verts.count(v) == 1);
366 }
367 }
368
369
370 TEST_F(MeshLayersSerialFilterTester, AllLayers)
371 {
372 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
373 GTEST_SKIP();
374
375 MeshLayers layers(mesh);
376 std::vector<MeshEntityPtr> output;
377 layers.get_all_layers(filter, roots, output);
378 EXPECT_TRUE(is_unique(output));
379 EXPECT_EQ(output.size(), static_cast<unsigned int>(15));
380 }
381
382
383 TEST_F(MeshLayersParallelNoFilterTester, OneLayerNP2)
384 {
385 if (utils::impl::comm_size(MPI_COMM_WORLD) != 2)
386 GTEST_SKIP();
387
388 MeshLayers layers(mesh);
389 std::vector<MeshEntityPtr> output;
390 layers.get_layers(filter, roots, 1, output);
391
392 std::vector<utils::Point> expectedPts;
393 if (myRank == 0)
394 {
395 expectedPts = { {0, 0}, {0.25, 0}, {0.5, 0},
396 {0, 0.25}, {0.25, 0.25}, {0.5, 0.25} };
397 } else
398 {
399 expectedPts = { {0.5, 0}, {0.75, 0}, {0.5, 0.25}};
400 }
401
402 EXPECT_EQ(output.size(), expectedPts.size());
403 for (auto& pt : expectedPts)
404 EXPECT_TRUE(find_closest_el(output, pt, 1e-10));
405 }
406
407 TEST_F(MeshLayersParallelNoFilterTester, TwoLayersNP2)
408 {
409 if (utils::impl::comm_size(MPI_COMM_WORLD) != 2)
410 GTEST_SKIP();
411
412 MeshLayers layers(mesh);
413 std::vector<MeshEntityPtr> output;
414 layers.get_layers(filter, roots, 2, output);
415
416 std::vector<utils::Point> expectedPts;
417 if (myRank == 0)
418 {
419 expectedPts = { {0, 0}, {0.25, 0}, {0.5, 0},
420 {0, 0.25}, {0.25, 0.25}, {0.5, 0.25},
421 {0, 0.50}, {0.25, 0.50}, {0.5, 0.50} };
422 } else
423 {
424 expectedPts = { {0.5, 0}, {0.75, 0}, {0.5, 0.25},
425 {1, 0}, {0.75, 0.25}, {0.5, 0.5}};
426 }
427
428 EXPECT_EQ(output.size(), expectedPts.size());
429 for (auto& pt : expectedPts)
430 EXPECT_TRUE(find_closest_el(output, pt, 1e-10));
431 }
432
433 TEST_F(MeshLayersParallelNoFilterTester, ThreeLayersNP2)
434 {
435 if (utils::impl::comm_size(MPI_COMM_WORLD) != 2)
436 GTEST_SKIP();
437
438 MeshLayers layers(mesh);
439 std::vector<MeshEntityPtr> output;
440 layers.get_layers(filter, roots, 3, output);
441
442 std::vector<utils::Point> expectedPts;
443 if (myRank == 0)
444 {
445 expectedPts = { {0, 0}, {0.25, 0}, {0.5, 0},
446 {0, 0.25}, {0.25, 0.25}, {0.5, 0.25},
447 {0, 0.50}, {0.25, 0.50}, {0.5, 0.50},
448 {0, 0.75}, {0.25, 0.75}, {0.5, 0.75} };
449 } else
450 {
451 expectedPts = { {0.5, 0}, {0.75, 0}, {0.5, 0.25},
452 {1, 0}, {0.75, 0.25}, {0.5, 0.5},
453 {1, 0.25}, {0.75, 0.50}, {0.5, 0.75}};
454 }
455
456 EXPECT_EQ(output.size(), expectedPts.size());
457 for (auto& pt : expectedPts)
458 EXPECT_TRUE(find_closest_el(output, pt, 1e-10));
459 }
460
461 TEST_F(MeshLayersParallelNoFilterTester, AllLayersNP2)
462 {
463 if (utils::impl::comm_size(MPI_COMM_WORLD) != 2)
464 GTEST_SKIP();
465
466 MeshLayers layers(mesh);
467 std::vector<MeshEntityPtr> output;
468 layers.get_all_layers(filter, roots, output);
469
470
471 std::vector<utils::Point> expectedPts;
472 if (myRank == 0)
473 {
474 expectedPts = { {0, 0}, {0.25, 0}, {0.5, 0},
475 {0, 0.25}, {0.25, 0.25}, {0.5, 0.25},
476 {0, 0.50}, {0.25, 0.50}, {0.5, 0.50},
477 {0, 0.75}, {0.25, 0.75}, {0.5, 0.75},
478 {0, 1.00}, {0.25, 1.00}, {0.5, 1.00} };
479 } else
480 {
481 expectedPts = { {0.5, 0}, {0.75, 0}, {1.00, 0},
482 {0.5, 0.25}, {0.75, 0.25}, {1.00, 0.25},
483 {0.5, 0.50}, {0.75, 0.50}, {1.00, 0.50},
484 {0.5, 0.75}, {0.75, 0.75}, {1.00, 0.75},
485 {0.5, 1.00}, {0.75, 1.00}, {1.00, 1.00}};
486 }
487
488 EXPECT_EQ(output.size(), expectedPts.size());
489 for (auto& pt : expectedPts)
490 EXPECT_TRUE(find_closest_el(output, pt, 1e-10));
491 }
492
493 TEST_F(MeshLayersParallelNoFilterTester, TwoLayersNP4)
494 {
495 if (utils::impl::comm_size(MPI_COMM_WORLD) != 4)
496 GTEST_SKIP();
497
498 MeshLayers layers(mesh);
499 std::vector<MeshEntityPtr> output;
500 layers.get_layers(filter, roots, 2, output);
501
502 std::vector<utils::Point> expectedPts;
503 if (myRank == 0)
504 {
505 expectedPts = { {0, 0}, {0.25, 0}, {0.5, 0},
506 {0, 0.25}, {0.25, 0.25}, {0.5, 0.25},
507 {0, 0.50}, {0.25, 0.50}, {0.5, 0.50} };
508 } else if (myRank == 1)
509 {
510 expectedPts = { {0.5, 0}, {0.75, 0}, {0.5, 0.25},
511 {1, 0}, {0.75, 0.25}, {0.5, 0.5}};
512 } else if (myRank == 2)
513 {
514 expectedPts = { {0, 0.5}, {0.25, 0.5}, {0.5, 0.5}};
515 } else if (myRank == 3)
516 {
517 expectedPts = { {0.5, 0.5}};
518 }
519
520 EXPECT_EQ(output.size(), expectedPts.size());
521 for (auto& pt : expectedPts)
522 EXPECT_TRUE(find_closest_el(output, pt, 1e-10));
523 }
524
525 TEST_F(MeshLayersParallelNoFilterTester, ThreeLayersNP4)
526 {
527 if (utils::impl::comm_size(MPI_COMM_WORLD) != 4)
528 GTEST_SKIP();
529
530 MeshLayers layers(mesh);
531 std::vector<MeshEntityPtr> output;
532 layers.get_layers(filter, roots, 3, output);
533
534 std::vector<utils::Point> expectedPts;
535 if (myRank == 0)
536 {
537 expectedPts = { {0, 0}, {0.25, 0}, {0.5, 0},
538 {0, 0.25}, {0.25, 0.25}, {0.5, 0.25},
539 {0, 0.50}, {0.25, 0.50}, {0.5, 0.50} };
540 } else if (myRank == 1)
541 {
542 expectedPts = { {0.5, 0}, {0.75, 0}, {0.5, 0.25},
543 {1, 0}, {0.75, 0.25}, {0.5, 0.5},
544 {0.75, 0.5}, {1.0, 0.25}};
545 } else if (myRank == 2)
546 {
547 expectedPts = { {0, 0.5}, {0.25, 0.5}, {0.5, 0.5},
548 {0, 0.75}, {0.25, 0.75}, {0.5, 0.75}};
549 } else if (myRank == 3)
550 {
551 expectedPts = { {0.5, 0.5}, {0.75, 0.5}, {0.5, 0.75}};
552 }
553
554 EXPECT_EQ(output.size(), expectedPts.size());
555 for (auto& pt : expectedPts)
556 EXPECT_TRUE(find_closest_el(output, pt, 1e-10));
557 }
558
559 TEST_F(MeshLayersParallelFilterTester, ThreeLayersNP4)
560 {
561 if (utils::impl::comm_size(MPI_COMM_WORLD) != 4)
562 GTEST_SKIP();
563
564 MeshLayers layers(mesh);
565 std::vector<MeshEntityPtr> output;
566 layers.get_layers(filter, roots, 3, output);
567
568 std::vector<utils::Point> expectedPts;
569 if (myRank == 0)
570 {
571 expectedPts = { {0, 0}, {0.25, 0}, {0.5, 0},
572 {0, 0.25}, {0.25, 0.25}, {0.5, 0.25},
573 {0, 0.50}, {0.25, 0.50} };
574 } else if (myRank == 1)
575 {
576 expectedPts = { {0.5, 0}, {0.75, 0}, {0.5, 0.25},
577 {1, 0}, {0.75, 0.25},
578 {0.75, 0.5}, {1.0, 0.25}};
579 } else if (myRank == 2)
580 {
581 expectedPts = { {0, 0.5}, {0.25, 0.5},
582 {0, 0.75}, {0.25, 0.75} };
583 } else if (myRank == 3)
584 {
585 expectedPts = {{0.75, 0.5}};
586 }
587
588 EXPECT_EQ(output.size(), expectedPts.size());
589 for (auto& pt : expectedPts)
590 EXPECT_TRUE(find_closest_el(output, pt, 1e-10));
591 }
592
593
594 } // namespace impl
595 } // namespace middle_mesh
596 } // namespace stk