Debian Codegraph @

packages/stk/stk_unit_tests/stk_middle_mesh/test_adjacency_search.cpp

raw
1 #include "gtest/gtest.h"
2
3 #include "stk_middle_mesh/adjacency_search.hpp"
4 #include "stk_middle_mesh/create_mesh.hpp"
5 #include "stk_middle_mesh/predicates/intersection.hpp"
6
7 namespace stk {
8 namespace middle_mesh {
9 namespace impl {
10
11 namespace {
12
13 std::shared_ptr<mesh::Mesh> create_mesh(const std::vector<utils::Point>& vertPts, const std::vector<std::array<int, 4>>& elVertIds)
14 {
15 auto mesh = mesh::make_empty_mesh();
16
17 std::vector<mesh::MeshEntityPtr> verts;
18 for (auto& pt : vertPts)
19 verts.push_back(mesh->create_vertex(pt));
20
21 for (auto& vertIds : elVertIds)
22 {
23 if (vertIds[3] == -1)
24 {
25 mesh->create_triangle_from_verts(verts[vertIds[0]], verts[vertIds[1]], verts[vertIds[2]]);
26 } else
27 {
28 mesh->create_quad_from_verts(verts[vertIds[0]], verts[vertIds[1]], verts[vertIds[2]], verts[vertIds[3]]);
29 }
30 }
31
32 return mesh;
33 }
34
35 bool any_contains(const std::vector<mesh::MeshEntityPtr>& entities, mesh::MeshEntityPtr el1)
36 {
37 predicates::impl::FaceContainer container;
38 utils::Point pt = compute_centroid(el1);
39 for (auto e : entities)
40 if (container.contains(e, pt))
41 return true;
42
43 return false;
44 }
45
46 // test that "every" point of el1 is contained in one of entities
47 // In practice, we don't test every point, just a bunch of them
48 void test_contains_inverse(const std::vector<mesh::MeshEntityPtr>& entities, mesh::MeshEntityPtr el1)
49 {
50 int npts = 5; // number of test points
51 double dxi = 1.0 / npts;
52
53 predicates::impl::FaceContainer container(1e-12);
54 for (int i = 0; i < npts; ++i)
55 for (int j = 0; j < npts; ++j)
56 {
57 double xi1 = i * dxi;
58 double xi2 = j * dxi;
59 utils::Point pt = compute_coords_from_xi(el1, utils::Point(xi1, xi2));
60
61 bool found = false;
62 for (auto e : entities)
63 {
64 if (container.contains(e, pt))
65 {
66 found = true;
67 break;
68 }
69 }
70
71 EXPECT_TRUE(found);
72 }
73 }
74
75 }
76
77 TEST(AdjacencySearch, Identical)
78 {
79 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
80 GTEST_SKIP();
81
82 // mesh1 and mesh2 are identical
83 mesh::impl::MeshSpec spec;
84 spec.numelX = 5;
85 spec.numelY = 5;
86 spec.xmin = 0;
87 spec.xmax = 1;
88 spec.ymin = 0;
89 spec.ymax = 1;
90
91 auto func = [&](const utils::Point& pt) { return pt; };
92
93 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
94 std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec, func);
95 mesh::impl::AdjacencySearch search(mesh1, mesh2);
96
97 mesh::MeshEntityPtr el1;
98 std::vector<mesh::MeshEntityPtr> entities;
99 int nentities = 0;
100 while ((el1 = search.get_next(entities)))
101 {
102 // the algorithm could produce either the single element that overlaps
103 // el1, or that element plus all adjacencies, or something in between
104 EXPECT_GE(entities.size(), static_cast<unsigned int>(1));
105 EXPECT_LE(entities.size(), static_cast<unsigned int>(9));
106 EXPECT_TRUE(any_contains(entities, el1));
107 test_contains_inverse(entities, el1);
108 nentities++;
109 }
110 EXPECT_EQ(nentities, spec.numelX * spec.numelY);
111 }
112
113 TEST(AdjacencySearch, Subdivision)
114 {
115 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
116 GTEST_SKIP();
117
118 mesh::impl::MeshSpec spec, spec2;
119 spec.numelX = 5;
120 spec.numelY = 5;
121 spec.xmin = 0;
122 spec.xmax = 1;
123 spec.ymin = 0;
124 spec.ymax = 1;
125
126 spec2.numelX = 10;
127 spec2.numelY = 10;
128 spec2.xmin = 0;
129 spec2.xmax = 1;
130 spec2.ymin = 0;
131 spec2.ymax = 1;
132
133 auto func = [&](const utils::Point& pt) { return pt; };
134
135 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
136 std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func);
137 mesh::impl::AdjacencySearch search(mesh1, mesh2);
138
139 mesh::MeshEntityPtr el1;
140 std::vector<mesh::MeshEntityPtr> entities;
141 int nentities = 0;
142 while ((el1 = search.get_next(entities)))
143 {
144 // the algorithm could produce either the single element that overlaps
145 // el1, or that element plus all adjacencies, or something in between
146 EXPECT_GE(entities.size(), static_cast<unsigned int>(4));
147 EXPECT_LE(entities.size(), static_cast<unsigned int>(16));
148 EXPECT_TRUE(any_contains(entities, el1));
149 test_contains_inverse(entities, el1);
150 nentities++;
151 }
152 EXPECT_EQ(nentities, spec.numelX * spec.numelY);
153 }
154
155 TEST(AdjacencySearch, ContainedElement)
156 {
157 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
158 GTEST_SKIP();
159
160 // mesh1 and mesh2 are identical
161 mesh::impl::MeshSpec spec, spec2;
162 spec.numelX = 5;
163 spec.numelY = 5;
164 spec.xmin = 0;
165 spec.xmax = 1;
166 spec.ymin = 0;
167 spec.ymax = 1;
168
169 spec2.numelX = 15;
170 spec2.numelY = 15;
171 spec2.xmin = 0;
172 spec2.xmax = 1;
173 spec2.ymin = 0;
174 spec2.ymax = 1;
175
176 auto func = [&](const utils::Point& pt) { return pt; };
177
178 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
179 std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func);
180
181 mesh::impl::AdjacencySearch search(mesh1, mesh2);
182
183 mesh::MeshEntityPtr el1;
184 std::vector<mesh::MeshEntityPtr> entities;
185 int nentities = 0;
186 while ((el1 = search.get_next(entities)))
187 {
188 EXPECT_GE(entities.size(), static_cast<unsigned int>(9));
189 EXPECT_LE(entities.size(), static_cast<unsigned int>(9 + 16));
190 EXPECT_TRUE(any_contains(entities, el1));
191 test_contains_inverse(entities, el1);
192 nentities++;
193 }
194 EXPECT_EQ(nentities, spec.numelX * spec.numelY);
195 }
196
197 TEST(AdjacencySearch, ContainedElements)
198 {
199 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
200 GTEST_SKIP();
201
202 // elements on mesh2 are contained within mesh1
203 mesh::impl::MeshSpec spec, spec2;
204 spec.numelX = 5;
205 spec.numelY = 5;
206 spec.xmin = 0;
207 spec.xmax = 1;
208 spec.ymin = 0;
209 spec.ymax = 1;
210
211 spec2.numelX = 20;
212 spec2.numelY = 20;
213 spec2.xmin = 0;
214 spec2.xmax = 1;
215 spec2.ymin = 0;
216 spec2.ymax = 1;
217
218 auto func = [&](const utils::Point& pt) { return pt; };
219
220 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
221 std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func);
222
223 mesh::impl::AdjacencySearch search(mesh1, mesh2);
224
225 mesh::MeshEntityPtr el1;
226 std::vector<mesh::MeshEntityPtr> entities;
227 int nentities = 0;
228 while ((el1 = search.get_next(entities)))
229 {
230 EXPECT_GE(entities.size(), static_cast<unsigned int>(16));
231 EXPECT_LE(entities.size(), static_cast<unsigned int>(16 + 20));
232 EXPECT_TRUE(any_contains(entities, el1));
233 test_contains_inverse(entities, el1);
234 nentities++;
235 }
236 EXPECT_EQ(nentities, spec.numelX * spec.numelY);
237 }
238
239 TEST(AdjacencySearch, TwoToFive)
240 {
241 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
242 GTEST_SKIP();
243
244 // in this case, there is no mesh2 edge coincident with any internal edges,
245 // and there are contained elements
246 mesh::impl::MeshSpec spec, spec2;
247 spec.numelX = 10;
248 spec.numelY = 10;
249 spec.xmin = 0;
250 spec.xmax = 1;
251 spec.ymin = 0;
252 spec.ymax = 1;
253
254 spec2.numelX = 25;
255 spec2.numelY = 25;
256 spec2.xmin = 0;
257 spec2.xmax = 1;
258 spec2.ymin = 0;
259 spec2.ymax = 1;
260
261 auto func = [&](const utils::Point& pt) { return pt; };
262
263 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
264 std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func);
265
266 mesh::impl::AdjacencySearch search(mesh1, mesh2);
267
268 mesh::MeshEntityPtr el1;
269 std::vector<mesh::MeshEntityPtr> entities;
270 int nentities = 0;
271 while ((el1 = search.get_next(entities)))
272 {
273 EXPECT_GE(entities.size(), static_cast<unsigned int>(9));
274 EXPECT_LE(entities.size(), static_cast<unsigned int>(9 + 16));
275 EXPECT_TRUE(any_contains(entities, el1));
276 test_contains_inverse(entities, el1);
277 nentities++;
278 }
279 EXPECT_EQ(nentities, spec.numelX * spec.numelY);
280 }
281
282 TEST(AdjacencySearch, NoVerts)
283 {
284 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
285 GTEST_SKIP();
286
287 // for some elements on mesh1, there are no mesh2 vertices contained
288 mesh::impl::MeshSpec spec, spec2;
289 spec.numelX = 12;
290 spec.numelY = 12;
291 spec.xmin = 0;
292 spec.xmax = 1;
293 spec.ymin = 0;
294 spec.ymax = 1;
295
296 spec2.numelX = 18;
297 spec2.numelY = 4;
298 spec2.xmin = 0;
299 spec2.xmax = 1;
300 spec2.ymin = 0;
301 spec2.ymax = 1;
302
303 auto func = [&](const utils::Point& pt) { return pt; };
304
305 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
306 std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func);
307
308 mesh::impl::AdjacencySearch search(mesh1, mesh2);
309
310 mesh::MeshEntityPtr el1;
311 std::vector<mesh::MeshEntityPtr> entities;
312 int nentities = 0;
313 while ((el1 = search.get_next(entities)))
314 {
315 EXPECT_GE(entities.size(), static_cast<unsigned int>(2));
316 EXPECT_LE(entities.size(), static_cast<unsigned int>(11));
317 EXPECT_TRUE(any_contains(entities, el1));
318 test_contains_inverse(entities, el1);
319 nentities++;
320 }
321 EXPECT_EQ(nentities, spec.numelX * spec.numelY);
322 }
323
324 TEST(AdjacencySearch, CutCorner)
325 {
326 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
327 GTEST_SKIP();
328
329 mesh::impl::MeshSpec spec;
330 spec.numelX = 1;
331 spec.numelY = 1;
332 spec.xmin = 0;
333 spec.xmax = 1;
334 spec.ymin = 0;
335 spec.ymax = 1;
336
337 auto func = [&](const utils::Point& pt) { return pt; };
338
339 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
340
341 auto mesh2 = mesh::make_empty_mesh();
342 auto v1 = mesh2->create_vertex(-0.25, 0.25);
343 auto v2 = mesh2->create_vertex(0.5, 1.25);
344 auto v3 = mesh2->create_vertex(-0.25, 1.25);
345 auto v4 = mesh2->create_vertex(0, 0);
346 auto v5 = mesh2->create_vertex(1, 1);
347 auto v6 = mesh2->create_vertex(1, 0);
348 mesh2->create_triangle_from_verts(v1, v2, v3);
349 mesh2->create_quad_from_verts(v4, v5, v2, v1);
350 mesh2->create_triangle_from_verts(v4, v6, v5);
351
352 mesh::impl::AdjacencySearch search(mesh1, mesh2);
353 std::vector<mesh::MeshEntityPtr> entities;
354 auto el1 = search.get_next(entities);
355 // std::cout << "mesh2 elements:" << std::endl;
356 // for (auto& el : entities)
357 // std::cout << el << std::endl;
358 EXPECT_EQ(entities.size(), static_cast<unsigned int>(3));
359 EXPECT_TRUE(any_contains(entities, el1));
360 test_contains_inverse(entities, el1);
361
362 // TODO: this avoids a memory leak of the Projection created
363 // inside the adjacency search, figure out why
364 while ((el1 = search.get_next(entities)))
365 {}
366 }
367
368 TEST(AdjacencySearch, Contains)
369 {
370 if (utils::impl::comm_size(MPI_COMM_WORLD) != 1)
371 GTEST_SKIP();
372
373 // some elements on mesh1 are completely contained in elements on mesh2
374 mesh::impl::MeshSpec spec, spec2;
375 spec.numelX = 36;
376 spec.numelY = 36;
377 spec.xmin = 0;
378 spec.xmax = 1;
379 spec.ymin = 0;
380 spec.ymax = 1;
381
382 spec2.numelX = 12;
383 spec2.numelY = 12;
384 spec2.xmin = 0;
385 spec2.xmax = 1;
386 spec2.ymin = 0;
387 spec2.ymax = 1;
388
389 auto func = [&](const utils::Point& pt) { return pt; };
390
391 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
392 std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func);
393
394 mesh::impl::AdjacencySearch search(mesh1, mesh2);
395
396 mesh::MeshEntityPtr el1;
397 std::vector<mesh::MeshEntityPtr> entities;
398 int nentities = 0;
399 while ((el1 = search.get_next(entities)))
400 {
401 EXPECT_GE(entities.size(), static_cast<unsigned int>(1));
402 EXPECT_LE(entities.size(), static_cast<unsigned int>(4));
403 EXPECT_TRUE(any_contains(entities, el1));
404 test_contains_inverse(entities, el1);
405 nentities++;
406 }
407 EXPECT_EQ(nentities, spec.numelX * spec.numelY);
408 }
409
410 /*
411 TEST(AdjacencySearch, Contains2)
412 {
413 // some elements on mesh1 are completely contained in elements on mesh2
414 mesh::impl::MeshSpec spec, spec2;
415 spec.numelX = 200;
416 spec.numelY = 200;
417 spec.xmin = 0;
418 spec.xmax = 1;
419 spec.ymin = 0;
420 spec.ymax = 1;
421
422 spec2.numelX = 400;
423 spec2.numelY = 400;
424 spec2.xmin = 0;
425 spec2.xmax = 1;
426 spec2.ymin = 0;
427 spec2.ymax = 1;
428
429 auto func = [&](const utils::Point& pt) { return pt;};
430
431 std::shared_ptr<mesh::Mesh> mesh1 = create_mesh(spec, func);
432 std::shared_ptr<mesh::Mesh> mesh2 = create_mesh(spec2, func);
433
434 mesh::impl::AdjacencySearch search(mesh1, mesh2);
435
436 mesh::MeshEntityPtr el1;
437 std::vector<mesh::MeshEntityPtr> entities;
438 int nentities = 0;
439 while ( (el1 = search.get_next(entities)) )
440 {
441 EXPECT_GE(entities.size(), static_cast<unsigned int>(1));
442 EXPECT_LE(entities.size(), static_cast<unsigned int>(100));
443 //EXPECT_TRUE(anyContains(entities, el1));
444 //testContainsInverse(entities, el1);
445 nentities++;
446 }
447 EXPECT_EQ(nentities, spec.numelX*spec.numelY);
448 }
449 */
450
451 TEST(AdjacencySearch, UnreachableCutCorner)
452 {
453 auto mesh1 = create_mesh( {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}},
454 {{0, 1, 2, 3}});
455
456 auto mesh2 = create_mesh( {{0, 0, 0}, {0.25, 0, 0}, {0.75, 0, 0}, {1.25, 0, 0},
457 {0, 0.75, 0}, {0.25, 0.75, 0}, {0.75, 0.75, 0}, {1.25, 0.75, 0},
458 {0, 1, 0}, {0.25, 1, 0}, {0.5, 1.25, 0}, {0.9, 1.0, 0},
459 {1.1, 1.25, 0}, {1.5, 0.9, 0}},
460 {{0, 1, 5, 4}, {1, 2, 6, 5}, {2, 3, 7, 6},
461 {4, 5, 9, 8}, {5, 6, 10, 9}, {6, 7, 11, 10},
462 {7, 13, 12, 11}});
463
464
465 mesh::impl::AdjacencySearch search(mesh1, mesh2);
466
467 mesh::MeshEntityPtr el1;
468 std::vector<mesh::MeshEntityPtr> entities;
469 while ((el1 = search.get_next(entities)))
470 {
471 EXPECT_EQ(entities.size(), 7u);
472
473 auto pred = [](const mesh::MeshEntityPtr& e) { return e->get_id() == 6; };
474 EXPECT_NE(std::find_if(entities.begin(), entities.end(), pred), entities.end());
475 }
476 }
477
478 } // namespace impl
479 } // namespace middle_mesh
480 } // namespace stk