libMesh
Loading...
Searching...
No Matches
mesh_triangulation.C
Go to the documentation of this file.
1#include <libmesh/boundary_info.h>
2#include <libmesh/elem.h>
3#include <libmesh/mesh.h>
4#include <libmesh/mesh_generation.h>
5#include <libmesh/mesh_modification.h>
6#include <libmesh/mesh_triangle_holes.h>
7#include <libmesh/mesh_triangle_interface.h>
8#include <libmesh/parallel_implementation.h> // max()
9#include <libmesh/parsed_function.h>
10#include <libmesh/point.h>
11#include <libmesh/poly2tri_triangulator.h>
12
13#include "test_comm.h"
14#include "libmesh_cppunit.h"
15
16#include <algorithm>
17#include <cmath>
18#include <regex>
19
20
21using namespace libMesh;
22
23class MeshTriangulationTest : public CppUnit::TestCase
24{
29public:
31
35
36#ifdef LIBMESH_HAVE_POLY2TRI
47# ifdef LIBMESH_ENABLE_AMR
49# endif
64 // This covers an old poly2tri collinearity-tolerance bug
66
67 // These cover more recent tolerance issues when verifying holes
71
74#endif
75
76#ifdef LIBMESH_HAVE_TRIANGLE
83# ifdef LIBMESH_ENABLE_AMR
85# endif
89#endif
90
92
93public:
94 void setUp() {}
95
96 void tearDown() {}
97
99 {
100 // Use the point order to define the boundary, because our
101 // Poly2Tri implementation doesn't do convex hulls yet, even when
102 // that would give the same answer.
104
105 // Don't try to insert points unless we're requested to later
106 triangulator.desired_area() = 1000;
107 triangulator.minimum_angle() = 0;
108 triangulator.smooth_after_generating() = false;
109 triangulator.set_verify_hole_boundaries(true);
110 }
111
113 TriangulatorInterface & triangulator,
114 const char * re)
115 {
116#ifdef LIBMESH_ENABLE_EXCEPTIONS
117 // We can't just CPPUNIT_ASSERT_THROW, because we want to make
118 // sure we were thrown from the right place with the right error
119 // message!
120 bool threw_desired_exception = false;
121 try {
122 this->testTriangulatorBase(mesh, triangulator);
123 }
124 catch (libMesh::LogicError & e) {
125 std::regex msg_regex(re);
126 CPPUNIT_ASSERT(std::regex_search(e.what(), msg_regex));
127 threw_desired_exception = true;
128 }
129 catch (CppUnit::Exception & e) {
130 throw e;
131 }
132 catch (...) {
133 CPPUNIT_ASSERT_MESSAGE("Unexpected exception type thrown", false);
134 }
135 CPPUNIT_ASSERT(threw_desired_exception);
136#endif
137 }
138
139
141 {
142 LOG_UNIT_TEST;
143
144 // Using center=(1,0), radius=2 for the heck of it
145 Point center{1};
146 Real radius = 2;
147 std::vector<TriangulatorInterface::PolygonHole> polyholes;
148
149 // Line
150 polyholes.emplace_back(center, radius, 2);
151 // Triangle
152 polyholes.emplace_back(center, radius, 3);
153 // Square
154 polyholes.emplace_back(center, radius, 4);
155 // Pentagon
156 polyholes.emplace_back(center, radius, 5);
157 // Hexagon
158 polyholes.emplace_back(center, radius, 6);
159
160 for (int i=0; i != 5; ++i)
161 {
162 const int n_sides = i+2;
163 const TriangulatorInterface::Hole & hole = polyholes[i];
164
165 const Real computed_area = hole.area();
166 const Real theta = pi/n_sides;
167 const Real half_side_length = radius*std::cos(theta);
168 const Real apothem = radius*std::sin(theta);
169 const Real area = n_sides * apothem * half_side_length;
170
171 LIBMESH_ASSERT_FP_EQUAL(computed_area, area, TOLERANCE*TOLERANCE);
172 }
173
174 TriangulatorInterface::ArbitraryHole arbhole {center, {{0,-1},{2,-1},{2,1},{0,2}}};
175 LIBMESH_ASSERT_FP_EQUAL(arbhole.area(), Real(5), TOLERANCE*TOLERANCE);
176
177#ifdef LIBMESH_HAVE_TRIANGLE
178 // Make sure we're compatible with the old naming structure too
179 TriangleInterface::PolygonHole square(center, radius, 4);
180 LIBMESH_ASSERT_FP_EQUAL(square.area(), 2*radius*radius, TOLERANCE*TOLERANCE);
181#endif
182 }
183
184
186 {
187 LOG_UNIT_TEST;
188
189 // Using center=(1,2), radius=2 for the heck of it
190 Point center{1,2};
191 Real radius = 2;
192 std::vector<TriangulatorInterface::PolygonHole> polyholes;
193
194 auto check_corners = [center, radius]
195 (const TriangulatorInterface::Hole & hole,
196 unsigned int np)
197 {
198 CPPUNIT_ASSERT_EQUAL(np, hole.n_points());
199 for (auto i : make_range(np))
200 {
201 const Real theta = i * 2 * libMesh::pi / np;
202 const Real xin = center(0) + radius * .99 * std::cos(theta);
203 const Real xout = center(0) + radius * 1.01 * std::cos(theta);
204 const Real yin = center(1) + radius * .99 * std::sin(theta);
205 const Real yout = center(1) + radius * 1.01 * std::sin(theta);
206
207 CPPUNIT_ASSERT(hole.contains(Point(xin, yin)));
208 CPPUNIT_ASSERT(!hole.contains(Point(xout, yout)));
209 }
210 };
211
212 const TriangulatorInterface::PolygonHole triangle(center, radius, 3);
213 check_corners(triangle, 3);
214 const TriangulatorInterface::PolygonHole diamond(center, radius, 4);
215 check_corners(diamond, 4);
216 const TriangulatorInterface::PolygonHole hexagon(center, radius, 6);
217 check_corners(hexagon, 6);
218
220 {{1,0}, {{0,-1},{2,-1},{2,1},{1.75,-.5},{1.5,1},{1.25,-.5},
221 {1,1},{.75,-.5},{.5,1},{.25,-.5},{0,1}}};
222
223 CPPUNIT_ASSERT(jaggy.contains({.1,-.3}));
224 CPPUNIT_ASSERT(jaggy.contains({.5,.9}));
225 CPPUNIT_ASSERT(jaggy.contains({.9,-.3}));
226 CPPUNIT_ASSERT(jaggy.contains({1,0}));
227 CPPUNIT_ASSERT(jaggy.contains({1.1,-.4}));
228 CPPUNIT_ASSERT(jaggy.contains({1.5,.9}));
229 CPPUNIT_ASSERT(jaggy.contains({1.9,-.3}));
230
231 CPPUNIT_ASSERT(jaggy.contains({1.9,-.5}));
232 CPPUNIT_ASSERT(jaggy.contains({1.6,-.5}));
233 CPPUNIT_ASSERT(jaggy.contains({1.1,-.5}));
234 CPPUNIT_ASSERT(jaggy.contains({.5,-.5}));
235 CPPUNIT_ASSERT(jaggy.contains({.2,-.5}));
236
237 CPPUNIT_ASSERT(!jaggy.contains({.1,.7}));
238 CPPUNIT_ASSERT(!jaggy.contains({.5,1.1}));
239 CPPUNIT_ASSERT(!jaggy.contains({.9,.7}));
240 CPPUNIT_ASSERT(!jaggy.contains({1,-1.1}));
241 CPPUNIT_ASSERT(!jaggy.contains({1.1,.8}));
242 CPPUNIT_ASSERT(!jaggy.contains({1.5,1.1}));
243 CPPUNIT_ASSERT(!jaggy.contains({1.9,.9}));
244
245 CPPUNIT_ASSERT(!jaggy.contains({1.9,1}));
246 CPPUNIT_ASSERT(!jaggy.contains({1.4,1}));
247 CPPUNIT_ASSERT(!jaggy.contains({.9,1}));
248 CPPUNIT_ASSERT(!jaggy.contains({.4,1}));
249 CPPUNIT_ASSERT(!jaggy.contains({-.2,1}));
250 CPPUNIT_ASSERT(!jaggy.contains({-.2,0}));
251 CPPUNIT_ASSERT(!jaggy.contains({1.2,0}));
252
254 {{1,0}, {{-.25,-1},{2,-1},{2,1},{1.75,1},{1.75,-.5},{1.5,-.5},
255 {1.5,1},{1.25,1},{1.25,-.5},{1,-.5},{1,1},{.75,1},
256 {.75,-.5},{.5,-.5},{.5,1},{.25,1},{.25,-.5},{0,-.5},
257 {0,1},{-.25,1}}};
258
259 CPPUNIT_ASSERT(square_jaggy.contains({-.1,-.3}));
260 CPPUNIT_ASSERT(square_jaggy.contains({.4,.9}));
261 CPPUNIT_ASSERT(square_jaggy.contains({.9,-.3}));
262 CPPUNIT_ASSERT(square_jaggy.contains({.9,0}));
263 CPPUNIT_ASSERT(square_jaggy.contains({1.1,-.6}));
264 CPPUNIT_ASSERT(square_jaggy.contains({1.4,.9}));
265 CPPUNIT_ASSERT(square_jaggy.contains({1.9,-.3}));
266
267 CPPUNIT_ASSERT(!square_jaggy.contains({.1,-.3}));
268 CPPUNIT_ASSERT(!square_jaggy.contains({.6,.9}));
269 CPPUNIT_ASSERT(!square_jaggy.contains({1.1,-.3}));
270 CPPUNIT_ASSERT(!square_jaggy.contains({1.1,0}));
271 CPPUNIT_ASSERT(!square_jaggy.contains({1.1,-1.6}));
272 CPPUNIT_ASSERT(!square_jaggy.contains({1.6,.9}));
273 CPPUNIT_ASSERT(!square_jaggy.contains({2.1,-.3}));
274
275 CPPUNIT_ASSERT(square_jaggy.contains({-.1,-.5}));
276 CPPUNIT_ASSERT(square_jaggy.contains({.3,-.5}));
277 CPPUNIT_ASSERT(square_jaggy.contains({.9,-.5}));
278 CPPUNIT_ASSERT(square_jaggy.contains({1.3,-.5}));
279 CPPUNIT_ASSERT(square_jaggy.contains({1.9,-.5}));
280
281 CPPUNIT_ASSERT(!square_jaggy.contains({-.3,1}));
282 CPPUNIT_ASSERT(!square_jaggy.contains({.2,1}));
283 CPPUNIT_ASSERT(!square_jaggy.contains({.6,1}));
284 CPPUNIT_ASSERT(!square_jaggy.contains({1.1,1}));
285 CPPUNIT_ASSERT(!square_jaggy.contains({1.6,1}));
286 CPPUNIT_ASSERT(!square_jaggy.contains({2.1,1}));
287 }
288
289
290 // The case from libMesh issue #3496, with no triangulator in the way.
291 // contains() casts its ray in the +x direction, so a hole vertex that
292 // very nearly shares the query point's y is the degenerate case.
293 //
294 // Rays that hit a vertex exactly - passing through one, grazing one,
295 // or running along a whole edge - are already covered by the holes in
296 // testTriangleHoleContains. What is special here is the scale: the
297 // ray misses the boundary's vertices by a few ulps, which is where
298 // comparing a parametric edge coordinate against a tolerance used to
299 // classify the same vertex differently from each of its two edges.
301 {
302 LOG_UNIT_TEST;
303
304 // Every vertex of a hole has to test as inside a boundary that
305 // encloses it, however nearly collinear the ray from it is with the
306 // boundary's own vertices.
308 perturbed(Point(0,4.e-16), std::sqrt(2_R)/2, 4);
310 {{0,0}, {{100,0},{100,100},{0,100},{-100,100},
311 {-100,0},{-100,-100},{0,-100},{100,-100}}};
312
313 for (auto i : make_range(perturbed.n_points()))
314 CPPUNIT_ASSERT(outer_bdy.contains(perturbed.point(i)));
315 }
316
317
319 TriangulatorInterface & triangulator)
320 {
321 commonSettings(triangulator);
322
323 triangulator.triangulate();
324
325 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), static_cast<dof_id_type>(2));
326 for (const auto & elem : mesh.element_ptr_range())
327 {
328 CPPUNIT_ASSERT_EQUAL(elem->type(), TRI3);
329
330 // Make sure we're not getting any inverted elements
331 auto cross_prod =
332 (elem->point(1) - elem->point(0)).cross
333 (elem->point(2) - elem->point(0));
334
335 CPPUNIT_ASSERT_GREATER(Real(0), cross_prod(2));
336
337 bool found_triangle = false;
338 for (const auto & node : elem->node_ref_range())
339 {
340 const Point & point = node;
341 if (point == Point(0,0))
342 {
343 found_triangle = true;
344 CPPUNIT_ASSERT((elem->point(0) == Point(0,0) &&
345 elem->point(1) == Point(1,0) &&
346 elem->point(2) == Point(0,1)) ||
347 (elem->point(1) == Point(0,0) &&
348 elem->point(2) == Point(1,0) &&
349 elem->point(0) == Point(0,1)) ||
350 (elem->point(2) == Point(0,0) &&
351 elem->point(0) == Point(1,0) &&
352 elem->point(1) == Point(0,1)));
353 }
354 if (point == Point(1,2))
355 {
356 found_triangle = true;
357 CPPUNIT_ASSERT((elem->point(0) == Point(0,1) &&
358 elem->point(1) == Point(1,0) &&
359 elem->point(2) == Point(1,2)) ||
360 (elem->point(1) == Point(0,1) &&
361 elem->point(2) == Point(1,0) &&
362 elem->point(0) == Point(1,2)) ||
363 (elem->point(2) == Point(0,1) &&
364 elem->point(0) == Point(1,0) &&
365 elem->point(1) == Point(1,2)));
366 }
367 }
368 CPPUNIT_ASSERT(found_triangle);
369 }
370 }
371
373 TriangulatorInterface & triangulator)
374 {
375 commonSettings(triangulator);
376
377 triangulator.elem_type() = TRI6;
378 triangulator.triangulate();
379 const Real hsq2 = std::sqrt(2) / 2.0;
380
381 // Check element number
382 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), (dof_id_type)2);
383 for (const auto & elem : mesh.element_ptr_range())
384 {
385 // Check element type
386 CPPUNIT_ASSERT_EQUAL(elem->type(), TRI6);
387
388 for (const auto & i_side : make_range(elem->n_sides()))
389 {
390 // We only want to check the sides on the external boundary
391 if (elem->neighbor_ptr(i_side) == nullptr)
392 {
393 const Point & side_pt_0 = *(elem->node_ptr(i_side));
394 const Point & side_pt_1 = *(elem->node_ptr((i_side + 1) % 3));
395 const Point & side_pt_2 = *(elem->node_ptr(i_side + 3));
396 const bool x_sign = (side_pt_0(0) == 1 || side_pt_1(0) == 1);
397 const bool y_sign = (side_pt_0(1) == 1 || side_pt_1(1) == 1);
398 const Point ref_side_pt_2 = Point (x_sign ? hsq2 : -hsq2, y_sign ? hsq2 : -hsq2);
399 CPPUNIT_ASSERT_EQUAL(ref_side_pt_2, side_pt_2);
400 }
401 }
402 }
403 }
404
406 TriangulatorInterface & triangulator)
407 {
408 // A non-square quad, so we don't have ambiguity about which
409 // diagonal a Delaunay algorithm will pick.
410 // Manually-numbered points, so we can use the point numbering as
411 // a segment ordering even on DistributedMesh.
412 mesh.add_point(Point(0,0), 0);
413 mesh.add_point(Point(1,0), 1);
414 mesh.add_point(Point(1,2), 2);
415 mesh.add_point(Point(0,1), 3);
416
417 this->testTriangulatorBase(mesh, triangulator);
418 }
419
420
422 {
423 // A non-square quad, so we don't have ambiguity about which
424 // diagonal a Delaunay algorithm will pick.
425 // Manually-numbered points, so we can use the point numbering as
426 // a segment ordering even on DistributedMesh.
427 mesh.add_point(Point(0,0), 0);
428 mesh.add_point(Point(1,0), 1);
429 mesh.add_point(Point(1,2), 2);
430 mesh.add_point(Point(0,1), 3);
431 }
432
433
435 TriangulatorInterface & triangulator,
436 int interpolate_boundary_points,
437 dof_id_type n_expected_elem,
438 Real expected_total_area = 1.5,
439 Real desired_area = 1000)
440 {
441 commonSettings(triangulator);
442
443 if (!mesh.n_nodes())
445
446 // Interpolate points!
447 triangulator.set_interpolate_boundary_points(interpolate_boundary_points);
448
449 // Try to insert points?
450 triangulator.desired_area() = desired_area;
451
452 triangulator.triangulate();
453
454 if (n_expected_elem)
455 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), n_expected_elem);
456
457 Real area = 0;
458 for (const auto & elem : mesh.active_local_element_ptr_range())
459 {
460 CPPUNIT_ASSERT_EQUAL(elem->level(), 0u);
461 CPPUNIT_ASSERT_EQUAL(elem->type(), TRI3);
462
463 area += elem->volume();
464 }
465
466 mesh.comm().sum(area);
467
468 LIBMESH_ASSERT_FP_EQUAL(area, expected_total_area, TOLERANCE*TOLERANCE);
469 }
470
471
473 const std::vector<Point> & expected_centers)
474 {
475 std::vector<bool> found_centers(expected_centers.size(), false);
476
477 for (const auto & elem : mesh.element_ptr_range())
478 {
479 CPPUNIT_ASSERT_EQUAL(elem->type(), TRI3);
480
481 // Make sure we're not getting any inverted elements
482 auto cross_prod =
483 (elem->point(1) - elem->point(0)).cross
484 (elem->point(2) - elem->point(0));
485
486 CPPUNIT_ASSERT_GREATER(Real(0), cross_prod(2));
487
488 // Make sure we're finding all the elements we expect
489 Point center = elem->vertex_average();
490
491 bool found_mine = false;
492 for (auto i : index_range(expected_centers))
493 {
494 Point possible = expected_centers[i];
495
496 if (possible.absolute_fuzzy_equals(center, TOLERANCE*TOLERANCE))
497 {
498 found_mine = true;
499 found_centers[i] = true;
500 }
501 }
502 CPPUNIT_ASSERT(found_mine);
503 }
504
505 mesh.comm().max(found_centers);
506
507 for (auto found_it : found_centers)
508 CPPUNIT_ASSERT(found_it);
509 }
510
511
513 TriangulatorInterface & triangulator)
514 {
515 // A square quad; we'll put a diamond hole in the middle to make
516 // the Delaunay selection unambiguous.
517 mesh.add_point(Point(-1,-1), 0);
518 mesh.add_point(Point(1,-1), 1);
519 mesh.add_point(Point(1,1), 2);
520 mesh.add_point(Point(-1,1), 3);
521
522 commonSettings(triangulator);
523
524 // Add a diamond hole in the center
525 TriangulatorInterface::PolygonHole diamond(Point(0), std::sqrt(2_R)/2, 4);
526 const std::vector<TriangulatorInterface::Hole*> holes { &diamond };
527 triangulator.attach_hole_list(&holes);
528
529 triangulator.triangulate();
530
531 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), static_cast<dof_id_type>(8));
532
533 // Center coordinates for all the elements we expect
534 Real r2p2o6 = (std::sqrt(2_R)+2)/6;
535 Real r2p4o6 = (std::sqrt(2_R)+4)/6;
536
537 std::vector <Point> expected_centers
538 { {r2p2o6,r2p2o6}, {r2p2o6,-r2p2o6},
539 {-r2p2o6,r2p2o6}, {-r2p2o6,-r2p2o6},
540 {0,r2p4o6}, {r2p4o6, 0},
541 {0,-r2p4o6}, {-r2p4o6, 0}
542 };
543
544 testFoundCenters(mesh, expected_centers);
545 }
546
547
549 TriangulatorInterface & triangulator)
550 {
551 // Points based on a simplification of a hole verification failure
552 // case
553 mesh.add_point(Point(100,0), 0);
554 mesh.add_point(Point(100,100), 1);
555 mesh.add_point(Point(0,100), 2);
556 mesh.add_point(Point(-100,100), 3);
557 mesh.add_point(Point(-100,0), 4);
558 mesh.add_point(Point(-100,-100), 5);
559 mesh.add_point(Point(0,-100), 6);
560 mesh.add_point(Point(100,-100), 7);
561
562 commonSettings(triangulator);
563
564 // Don't insert points here; we want the triangulation determined
565 // purely by the boundary and hole points
566 triangulator.desired_area() = 1e16;
567
568 // Add a diamond hole in *almost* the center; the tiny perturbation
569 // puts a hole vertex ray-collinear with a boundary vertex, which
570 // used to trip up the hole verification
572 diamond(Point(0,4.e-16),
573 std::sqrt(2_R)/2, 4);
574 const std::vector<TriangulatorInterface::Hole*> holes { &diamond };
575 triangulator.attach_hole_list(&holes);
576
577 triangulator.triangulate();
578
579 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), dof_id_type(12));
580
581 // Center coordinates for all the elements we expect
582 const Real r2p200o6 = (std::sqrt(2_R)+200)/6,
583 r2p400o6 = (std::sqrt(2_R)+400)/6;
584
585 std::vector <Point> expected_centers
586 { {r2p400o6,100./3}, {-r2p400o6,100./3},
587 {r2p400o6,-100./3}, {-r2p400o6,-100./3},
588 {100./3,r2p400o6}, {-100./3,r2p400o6},
589 {100./3,-r2p400o6}, {-100./3,-r2p400o6},
590 {r2p200o6,r2p200o6}, {-r2p200o6,r2p200o6},
591 {r2p200o6,-r2p200o6}, {-r2p200o6,-r2p200o6},
592 };
593
594 testFoundCenters(mesh, expected_centers);
595 }
596
597
599 TriangulatorInterface & triangulator)
600 {
601 // Points based on a simplification of a hole verification failure
602 // case
603 mesh.add_point(Point(200,0), 0);
604 mesh.add_point(Point(200,100), 1);
605 mesh.add_point(Point(100,100), 2);
606 mesh.add_point(Point(0,100), 3);
607 mesh.add_point(Point(-100,100), 4);
608 mesh.add_point(Point(-200,100), 5);
609 mesh.add_point(Point(-200,0), 6);
610 mesh.add_point(Point(-200,-100), 7);
611 mesh.add_point(Point(-100,-100), 8);
612 mesh.add_point(Point(0,-100), 9);
613 mesh.add_point(Point(100,-100), 10);
614 mesh.add_point(Point(200,-100), 11);
615
616 commonSettings(triangulator);
617
618 // Don't insert points here; we want the triangulation determined
619 // purely by the boundary and hole points
620 triangulator.desired_area() = 1e16;
621
622 // Two diamond holes, in *almost* the center of each half of that
623 // rectangle. The tiny perturbations make a ray from one hole
624 // vertex "tangent" to a vertex of the other hole, which used to
625 // trip up the hole verification double-intersection check.
627 left_diamond(Point(-100,4.e-16),
628 std::sqrt(2_R)/2, 4),
629 right_diamond(Point(100,-4.e-16),
630 std::sqrt(2_R)/2, 4);
631 const std::vector<TriangulatorInterface::Hole*> holes
632 { &left_diamond, &right_diamond };
633 triangulator.attach_hole_list(&holes);
634
635 triangulator.triangulate();
636
637 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), dof_id_type(22));
638
639 // Center coordinates for all the elements we expect
640 const Real r2p200o6 = (std::sqrt(2_R)+200)/6,
641 r2p400o6 = (std::sqrt(2_R)+400)/6;
642
643 std::vector <Point> expected_centers
644 { // Right half, around the diamond hole near x = 100
645 {100+r2p400o6,100./3}, {100+r2p400o6,-100./3},
646 {100+100./3,r2p400o6}, {100-100./3,r2p400o6},
647 {100+100./3,-r2p400o6}, {100-100./3,-r2p400o6},
648 {100+r2p200o6,r2p200o6}, {100-r2p200o6,r2p200o6},
649 {100+r2p200o6,-r2p200o6}, {100-r2p200o6,-r2p200o6},
650 // Left half, mirror of the right around the hole near x = -100
651 {-100-r2p400o6,100./3}, {-100-r2p400o6,-100./3},
652 {-100-100./3,r2p400o6}, {-100+100./3,r2p400o6},
653 {-100-100./3,-r2p400o6}, {-100+100./3,-r2p400o6},
654 {-100-r2p200o6,r2p200o6}, {-100+r2p200o6,r2p200o6},
655 {-100-r2p200o6,-r2p200o6}, {-100+r2p200o6,-r2p200o6},
656 // The two elements meeting the shared wall at x = 0
657 {0,100./3}, {0,-100./3}
658 };
659
660 testFoundCenters(mesh, expected_centers);
661 }
662
663
665 TriangulatorInterface & triangulator)
666 {
667 // A square quad; we'll put a square hole in the middle. Offset
668 // this to catch a potential bug I missed on the first try.
669 mesh.add_point(Point(19,19), 0);
670 mesh.add_point(Point(21,19), 1);
671 mesh.add_point(Point(21,21), 2);
672 mesh.add_point(Point(19,21), 3);
673
674 commonSettings(triangulator);
675
676 // Add a square meshed hole in the center
677 Mesh centermesh { mesh.comm() };
678 MeshTools::Generation::build_square (centermesh, 2, 2, 19.5, 20.5, 19.5, 20.5, QUAD4);
679
680 TriangulatorInterface::MeshedHole centerhole { centermesh };
681
682 CPPUNIT_ASSERT_EQUAL(centerhole.n_points(), 8u);
683 CPPUNIT_ASSERT_EQUAL(centerhole.area(), Real(1));
684 Point inside = centerhole.inside();
685 CPPUNIT_ASSERT_EQUAL(inside(0), Real(20));
686 CPPUNIT_ASSERT_EQUAL(inside(1), Real(20));
687
688 const std::vector<TriangulatorInterface::Hole*> holes { &centerhole };
689 triangulator.attach_hole_list(&holes);
690
691 triangulator.triangulate();
692
693 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), static_cast<dof_id_type>(12));
694
695 // Center coordinates for all the elements we expect
696 std::vector <Point> expected_centers
697 { {-0.5, Real(2)/3}, {0, Real(5)/6},
698 {0.5, Real(2)/3},
699 {Real(2)/3, -0.5}, {Real(5)/6, 0},
700 {Real(2)/3, 0.5},
701 {-0.5, -Real(2)/3}, {0, -Real(5)/6},
702 {0.5, -Real(2)/3},
703 {-Real(2)/3, -0.5}, {-Real(5)/6, 0},
704 {-Real(2)/3, 0.5} };
705
706 // With the same offset
707 for (auto & p : expected_centers)
708 p += Point(20,20);
709
710 testFoundCenters(mesh, expected_centers);
711 }
712
713
714#ifdef LIBMESH_ENABLE_AMR
716 TriangulatorInterface & triangulator)
717 {
718 // A square quad; we'll put a round hole in the middle.
719 mesh.add_point(Point(19,19), 0);
720 mesh.add_point(Point(21,19), 1);
721 mesh.add_point(Point(21,21), 2);
722 mesh.add_point(Point(19,21), 3);
723
724 commonSettings(triangulator);
725
726 // Add a square meshed hole in the center
727 const Real radius = 0.5;
728 const Point center{20,20};
729
730 Mesh centermesh { mesh.comm() };
732 MeshTools::Modification::translate(centermesh, center(0), center(1));
733
734 TriangulatorInterface::MeshedHole centerhole { centermesh };
735
736 CPPUNIT_ASSERT_EQUAL(centerhole.n_points(), 8u);
737 Point inside = centerhole.inside();
738 CPPUNIT_ASSERT_EQUAL(inside(0), Real(20));
739 CPPUNIT_ASSERT_EQUAL(inside(1), Real(20));
740
741 const std::vector<TriangulatorInterface::Hole*> holes { &centerhole };
742 triangulator.attach_hole_list(&holes);
743 triangulator.elem_type() = TRI6;
744
745 triangulator.triangulate();
746
747 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), static_cast<dof_id_type>(12));
748 CPPUNIT_ASSERT_EQUAL(mesh.n_nodes(), static_cast<dof_id_type>(36));
749
750 // Make sure we didn't screw up the outer sides. We should
751 // have exact values for the outer vertices, so we can use
752 // those for a map.
753 std::map<std::pair<Real, Real>, Point> outer_midpoints
754 {{{19, 19}, {20, 19}},
755 {{21, 19}, {21, 20}},
756 {{21, 21}, {20, 21}},
757 {{19, 21}, {19, 20}}
758 };
759
760 for (const auto & elem : mesh.active_local_element_ptr_range())
761 for (const auto n : make_range(elem->n_sides()))
762 {
763 if (elem->neighbor_ptr(n))
764 continue;
765
766 auto it =
767 outer_midpoints.find(std::make_pair(elem->point(n)(0),
768 elem->point(n)(1)));
769 if (it != outer_midpoints.end())
770 {
771 const Point error = it->second - elem->point(n+3);
772 CPPUNIT_ASSERT_LESS(TOLERANCE*TOLERANCE,
773 error.norm_sq());
774 }
775 else
776 {
777 const Point radius1 = elem->point(n) - center;
778 CPPUNIT_ASSERT_LESS(TOLERANCE*TOLERANCE,
779 std::abs(radius1.norm()-radius));
780
781 const Point radius2 = elem->point((n+1)%3) - center;
782 CPPUNIT_ASSERT_LESS(TOLERANCE*TOLERANCE,
783 std::abs(radius2.norm()-radius));
784
785 const Point radius3 = elem->point(n+3) - center;
786 CPPUNIT_ASSERT_LESS(TOLERANCE*TOLERANCE,
787 std::abs(radius3.norm()-radius));
788 }
789 }
790 }
791#endif // LIBMESH_ENABLE_AMR
792
793
795 TriangulatorInterface & triangulator)
796 {
797 // The same quad as testTriangulator, but out of order
798 mesh.add_point(Point(0,0), 0);
799 mesh.add_point(Point(1,2), 1);
800 mesh.add_point(Point(1,0), 2);
801 mesh.add_point(Point(0,1), 3);
802
803 // Segments to put them in order
804 triangulator.segments = {{0,2},{2,1},{1,3},{3,0}};
805
806 this->testTriangulatorBase(mesh, triangulator);
807 }
808
809
811 {
812 // The same quad as testTriangulator, but out of order
813 auto node0 = mesh.add_point(Point(0,0), 0);
814 auto node1 = mesh.add_point(Point(1,2), 1);
815 auto node2 = mesh.add_point(Point(1,0), 2);
816 auto node3 = mesh.add_point(Point(0,1), 3);
817
818 // Edges, also out of order, but enough to put them in order
819 auto edge13 = mesh.add_elem(Elem::build(elem_type));
820 edge13->set_node(0, node1);
821 edge13->set_node(1, node3);
822 auto edge02 = mesh.add_elem(Elem::build(elem_type));
823 edge02->set_node(0, node0);
824 edge02->set_node(1, node2);
825 auto edge30 = mesh.add_elem(Elem::build(elem_type));
826 edge30->set_node(0, node3);
827 edge30->set_node(1, node0);
828 auto edge21 = mesh.add_elem(Elem::build(elem_type));
829 edge21->set_node(0, node2);
830 edge21->set_node(1, node1);
831
832 // Add mid-edge nodes if asked to
833 if (elem_type == EDGE3)
834 {
835 auto node4 = mesh.add_point(Point(.5,1.5), 4);
836 edge13->set_node(2, node4);
837 auto node5 = mesh.add_point(Point(.5,0), 5);
838 edge02->set_node(2, node5);
839 auto node6 = mesh.add_point(Point(0,.5), 6);
840 edge30->set_node(2, node6);
841 auto node7 = mesh.add_point(Point(1,1), 7);
842 edge21->set_node(2, node7);
843 }
844 else
845 libmesh_assert(elem_type == EDGE2);
846
848 }
849
851 {
852 const Real hsq2 = std::sqrt(2) / 2.0;
853 auto node0 = mesh.add_point(Point(1,0), 0);
854 auto node1 = mesh.add_point(Point(hsq2,hsq2), 1);
855 auto node2 = mesh.add_point(Point(0,1), 2);
856 auto node3 = mesh.add_point(Point(-hsq2,hsq2), 3);
857 auto node4 = mesh.add_point(Point(-1,0), 4);
858 auto node5 = mesh.add_point(Point(-hsq2,-hsq2), 5);
859 auto node6 = mesh.add_point(Point(0,-1), 6);
860 auto node7 = mesh.add_point(Point(hsq2,-hsq2), 7);
861
862 auto edge021 = mesh.add_elem(Elem::build(EDGE3));
863 edge021->set_node(0, node0);
864 edge021->set_node(1, node2);
865 edge021->set_node(2, node1);
866 auto edge243 = mesh.add_elem(Elem::build(EDGE3));
867 edge243->set_node(0, node2);
868 edge243->set_node(1, node4);
869 edge243->set_node(2, node3);
870 auto edge465 = mesh.add_elem(Elem::build(EDGE3));
871 edge465->set_node(0, node4);
872 edge465->set_node(1, node6);
873 edge465->set_node(2, node5);
874 auto edge607 = mesh.add_elem(Elem::build(EDGE3));
875 edge607->set_node(0, node6);
876 edge607->set_node(1, node0);
877 edge607->set_node(2, node7);
878
880 }
881
882
884 TriangulatorInterface & triangulator,
885 ElemType elem_type)
886 {
887 // We might have 4 or 8 nodes on the outer boundary, depending on
888 // whether it has mid-edge nodes
889 const dof_id_type off = (elem_type == EDGE3)*4;
890
891 // A pentagon we'll avoid via subdomain ids
892 auto node4 = mesh.add_point(Point(2,0), 4+off);
893 auto node5 = mesh.add_point(Point(3,0), 5+off);
894 auto node6 = mesh.add_point(Point(3,2), 6+off);
895 auto node7 = mesh.add_point(Point(2,2), 7+off);
896 auto node8 = mesh.add_point(Point(2,1), 8+off);
897
898 auto edge45 = mesh.add_elem(Elem::build(elem_type));
899 edge45->set_node(0, node4);
900 edge45->set_node(1, node5);
901 edge45->subdomain_id() = 1;
902 auto edge56 = mesh.add_elem(Elem::build(elem_type));
903 edge56->set_node(0, node5);
904 edge56->set_node(1, node6);
905 edge56->subdomain_id() = 1;
906 auto edge67 = mesh.add_elem(Elem::build(elem_type));
907 edge67->set_node(0, node6);
908 edge67->set_node(1, node7);
909 edge67->subdomain_id() = 1;
910 auto edge78 = mesh.add_elem(Elem::build(elem_type));
911 edge78->set_node(0, node7);
912 edge78->set_node(1, node8);
913 edge78->subdomain_id() = 1;
914 auto edge84 = mesh.add_elem(Elem::build(elem_type));
915 edge84->set_node(0, node8);
916 edge84->set_node(1, node4);
917 edge84->subdomain_id() = 1;
918
919 if (elem_type == EDGE3)
920 {
921 auto node9 = mesh.add_point(Point(2.5,0), 9+off);
922 edge45->set_node(2, node9);
923 auto node10 = mesh.add_point(Point(3,1), 10+off);
924 edge56->set_node(2, node10);
925 auto node11 = mesh.add_point(Point(2.5,2), 11+off);
926 edge67->set_node(2, node11);
927 auto node12 = mesh.add_point(Point(2,1.5), 12+off);
928 edge78->set_node(2, node12);
929 auto node13 = mesh.add_point(Point(2,.5), 13+off);
930 edge84->set_node(2, node13);
931 }
932 else
933 libmesh_assert(elem_type == EDGE2);
934
935 testEdgesMesh(mesh, elem_type);
936
937 std::set<std::size_t> bdy_ids {0};
938 triangulator.set_outer_boundary_ids(bdy_ids);
939
940 this->testTriangulatorBase(mesh, triangulator);
941 }
942
943
944#ifdef LIBMESH_HAVE_TRIANGLE
946 {
947 LOG_UNIT_TEST;
948
950 TriangleInterface triangle(mesh);
951 testTriangulator(mesh, triangle);
952 }
953
954
956 {
957 LOG_UNIT_TEST;
958
960 TriangleInterface triangle(mesh);
961 testHalfDomain(mesh, triangle, EDGE2);
962 }
963
964
966 {
967 LOG_UNIT_TEST;
968
970 TriangleInterface triangle(mesh);
971 testTriangulatorInterp(mesh, triangle, 1, 6);
972 }
973
974
976 {
977 LOG_UNIT_TEST;
978
980 TriangleInterface triangle(mesh);
981 testTriangulatorInterp(mesh, triangle, 2, 10);
982 }
983
984
986 {
987 LOG_UNIT_TEST;
988
990 TriangleInterface triangle(mesh);
991 testTriangulatorHoles(mesh, triangle);
992 }
993
994
996 {
997 LOG_UNIT_TEST;
998
1000 TriangleInterface triangle(mesh);
1002 }
1003
1004
1005#ifdef LIBMESH_ENABLE_AMR
1007 {
1008 LOG_UNIT_TEST;
1009
1011 TriangleInterface triangle(mesh);
1013 }
1014#endif // LIBMESH_ENABLE_AMR
1015
1016
1018 {
1019 LOG_UNIT_TEST;
1020
1022 TriangleInterface triangulator(mesh);
1024
1025 this->testTriangulatorBase(mesh, triangulator);
1026 }
1027
1029 {
1030 LOG_UNIT_TEST;
1031
1033 TriangleInterface triangle(mesh);
1034 testTriangulatorSegments(mesh, triangle);
1035 }
1036
1038 {
1039 LOG_UNIT_TEST;
1040
1042 TriangleInterface triangulator(mesh);
1043
1045
1046 this->testEdge3ToTri6Base(mesh, triangulator);
1047 }
1048
1049#endif // LIBMESH_HAVE_TRIANGLE
1050
1051
1052#ifdef LIBMESH_HAVE_POLY2TRI
1054 {
1055 LOG_UNIT_TEST;
1056
1058 Poly2TriTriangulator p2t_tri(mesh);
1059 testTriangulator(mesh, p2t_tri);
1060 }
1061
1062
1064 {
1065 LOG_UNIT_TEST;
1066
1068 Poly2TriTriangulator p2t_tri(mesh);
1069 testHalfDomain(mesh, p2t_tri, EDGE2);
1070 }
1071
1072
1074 {
1075 LOG_UNIT_TEST;
1076
1078 Poly2TriTriangulator p2t_tri(mesh);
1079 testHalfDomain(mesh, p2t_tri, EDGE3);
1080 }
1081
1082
1084 {
1085 LOG_UNIT_TEST;
1086
1088 Poly2TriTriangulator p2t_tri(mesh);
1089 testTriangulatorInterp(mesh, p2t_tri, 1, 6);
1090 }
1091
1092
1094 {
1095 LOG_UNIT_TEST;
1096
1098 Poly2TriTriangulator p2t_tri(mesh);
1099 testTriangulatorInterp(mesh, p2t_tri, 2, 10);
1100 }
1101
1102
1104 {
1105 LOG_UNIT_TEST;
1106
1108 Poly2TriTriangulator p2t_tri(mesh);
1109 testTriangulatorHoles(mesh, p2t_tri);
1110 }
1111
1112
1114 {
1115 LOG_UNIT_TEST;
1116
1118 Poly2TriTriangulator p2t_tri(mesh);
1120 }
1121
1122
1124 {
1125 LOG_UNIT_TEST;
1126
1128 Poly2TriTriangulator p2t_tri(mesh);
1130 }
1131
1132
1134 {
1135 LOG_UNIT_TEST;
1136
1137 // The four vertices of a rectangle lie on one circle, so either
1138 // diagonal is Delaunay. With a circumradius of order 1e5 the
1139 // rounding error in the distance from a circumcircle exceeds
1140 // TOLERANCE*TOLERANCE, which lets both diagonals call for a swap
1141 // if the swap tolerance is fixed. The vertex coordinates are
1142 // exact in binary, so the rounding does not depend on how they are
1143 // computed.
1144 const Real scale = 1e5;
1146 mesh.add_point(scale * Point(-0.5, -0.7), 0);
1147 mesh.add_point(scale * Point(1, -0.7), 1);
1148 mesh.add_point(scale * Point(1, 1), 2);
1149 mesh.add_point(scale * Point(-0.5, 1), 3);
1150
1151 Poly2TriTriangulator triangulator(mesh);
1152 commonSettings(triangulator);
1153 triangulator.desired_area() = 0;
1154 triangulator.triangulate();
1155
1156 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), static_cast<dof_id_type>(2));
1157
1158 Real area = 0;
1159 for (const auto & elem : mesh.active_local_element_ptr_range())
1160 area += elem->volume();
1161 mesh.comm().sum(area);
1162 LIBMESH_ASSERT_FP_EQUAL(area, 1.5 * 1.7 * scale * scale, TOLERANCE * scale * scale);
1163 }
1164
1165
1167 {
1168 LOG_UNIT_TEST;
1169
1171 Poly2TriTriangulator p2t_tri(mesh);
1173 }
1174
1176 {
1177 LOG_UNIT_TEST;
1178
1180 Poly2TriTriangulator triangulator(mesh);
1181
1183
1184 this->testEdge3ToTri6Base(mesh, triangulator);
1185 }
1186
1187
1189 {
1190 LOG_UNIT_TEST;
1191
1193 Poly2TriTriangulator triangulator(mesh);
1194
1195 // Re-use the curved (unit-circle) Edge3 boundary from testEdge3Mesh:
1196 // four arcs whose midpoints lie on the unit circle. After
1197 // triangulation with TRI7 + boundary-midpoint snap, every TRI7
1198 // element's interior node should land at the curved Tri6 image of
1199 // the reference centroid (1/3, 1/3) -- the formula applied by
1200 // TriangulatorInterface::fixup_tri7_center_nodes() -- *not* at the
1201 // straight-edge vertex centroid (P0+P1+P2)/3 that
1202 // MeshBase::all_complete_order() initially placed there.
1204
1205 commonSettings(triangulator);
1206 triangulator.elem_type() = TRI7;
1207 // The interior-node relocation is opt-in (off by default), so
1208 // enable it here -- this is the behavior under test.
1209 triangulator.set_fixup_tri7_center_nodes(true);
1210 triangulator.triangulate();
1211
1212 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), (dof_id_type)2);
1213
1214 bool saw_curved_element = false;
1215 for (const auto & elem : mesh.element_ptr_range())
1216 {
1217 CPPUNIT_ASSERT_EQUAL(elem->type(), TRI7);
1218
1219 const Point & x0 = elem->point(0);
1220 const Point & x1 = elem->point(1);
1221 const Point & x2 = elem->point(2);
1222 const Point & x3 = elem->point(3);
1223 const Point & x4 = elem->point(4);
1224 const Point & x5 = elem->point(5);
1225 const Point & x6 = elem->point(6);
1226
1227 const Point expected =
1228 (Real(-1)/9) * (x0 + x1 + x2) +
1229 (Real( 4)/9) * (x3 + x4 + x5);
1230 const Point straight = (x0 + x1 + x2) / Real(3);
1231
1232 // Centroid must match the curved-mapping formula exactly.
1233 CPPUNIT_ASSERT(x6.relative_fuzzy_equals(expected, TOLERANCE));
1234
1235 // On the inscribed-square triangulation of the unit circle
1236 // each triangle has at least one curved external side, so the
1237 // curved-mapping centroid is strictly different from the
1238 // straight-edge vertex average -- otherwise the fixup would
1239 // be a no-op and we wouldn't actually be testing it.
1240 if (!x6.relative_fuzzy_equals(straight, TOLERANCE))
1241 saw_curved_element = true;
1242 }
1243
1244 mesh.comm().max(saw_curved_element);
1245 CPPUNIT_ASSERT(saw_curved_element);
1246 }
1247
1248
1250 {
1251#ifdef LIBMESH_ENABLE_EXCEPTIONS
1252 LOG_UNIT_TEST;
1253
1255
1256 // Build a triangular domain (0,0)-(4,0)-(2,2) using three Edge3
1257 // segments. The bottom segment's mid-edge node is placed at
1258 // (2, 1.95) -- almost on top of the apex (2, 2) -- so that
1259 // snapping the resulting TRI6 mid-edge node to that location
1260 // tangles the element (the curved bottom edge folds across the
1261 // apex). TriangulatorInterface::verify_quadratic_elements()
1262 // should detect the non-positive Jacobian and throw.
1263 auto vA = mesh.add_point(Point(0, 0), 0);
1264 auto vB = mesh.add_point(Point(4, 0), 1);
1265 auto vC = mesh.add_point(Point(2, 2), 2);
1266 auto mAB = mesh.add_point(Point(2, 1.95), 3); // pathological
1267 auto mBC = mesh.add_point(Point(3, 1), 4);
1268 auto mCA = mesh.add_point(Point(1, 1), 5);
1269
1270 auto edgeAB = mesh.add_elem(Elem::build(EDGE3));
1271 edgeAB->set_node(0, vA);
1272 edgeAB->set_node(1, vB);
1273 edgeAB->set_node(2, mAB);
1274
1275 auto edgeBC = mesh.add_elem(Elem::build(EDGE3));
1276 edgeBC->set_node(0, vB);
1277 edgeBC->set_node(1, vC);
1278 edgeBC->set_node(2, mBC);
1279
1280 auto edgeCA = mesh.add_elem(Elem::build(EDGE3));
1281 edgeCA->set_node(0, vC);
1282 edgeCA->set_node(1, vA);
1283 edgeCA->set_node(2, mCA);
1284
1286
1287 Poly2TriTriangulator triangulator(mesh);
1288 commonSettings(triangulator);
1289 triangulator.elem_type() = TRI6;
1290
1291 bool threw_desired_exception = false;
1292 try {
1293 triangulator.triangulate();
1294 }
1295 catch (libMesh::LogicError & e) {
1296 const std::regex msg_regex("tangled quadratic triangle");
1297 CPPUNIT_ASSERT(std::regex_search(e.what(), msg_regex));
1298 threw_desired_exception = true;
1299 }
1300 catch (CppUnit::Exception & e) {
1301 throw e;
1302 }
1303 catch (...) {
1304 CPPUNIT_ASSERT_MESSAGE("Unexpected exception type thrown", false);
1305 }
1306 CPPUNIT_ASSERT(threw_desired_exception);
1307#endif
1308 }
1309
1310
1311#ifdef LIBMESH_ENABLE_AMR
1313 {
1314 LOG_UNIT_TEST;
1315
1317 Poly2TriTriangulator p2t_tri(mesh);
1319 }
1320#endif // LIBMESH_ENABLE_AMR
1321
1322
1324 {
1325 LOG_UNIT_TEST;
1326
1328 Poly2TriTriangulator triangulator(mesh);
1330
1331 this->testTriangulatorBase(mesh, triangulator);
1332 }
1333
1334
1336 {
1337 LOG_UNIT_TEST;
1338
1340 Poly2TriTriangulator triangulator(mesh);
1342
1343 this->testTriangulatorBase(mesh, triangulator);
1344 }
1345
1346
1348 {
1349 LOG_UNIT_TEST;
1350
1352 Poly2TriTriangulator triangulator(mesh);
1353
1354 // The same quad as testTriangulator, but out of order
1355 auto node0 = mesh.add_point(Point(0,0), 0);
1356 auto node1 = mesh.add_point(Point(1,2), 1);
1357 auto node2 = mesh.add_point(Point(1,0), 2);
1358 auto node3 = mesh.add_point(Point(0,1), 3);
1359
1360 // Edges, but not enough to complete the quad
1361 auto edge13 = mesh.add_elem(Elem::build(EDGE2));
1362 edge13->set_node(0, node1);
1363 edge13->set_node(1, node3);
1364 auto edge02 = mesh.add_elem(Elem::build(EDGE2));
1365 edge02->set_node(0, node0);
1366 edge02->set_node(1, node2);
1367 auto edge30 = mesh.add_elem(Elem::build(EDGE2));
1368 edge30->set_node(0, node3);
1369 edge30->set_node(1, node0);
1370
1372
1373 testExceptionBase(mesh, triangulator, "Bad edge topology");
1374 }
1375
1376
1378 {
1379 LOG_UNIT_TEST;
1380
1382 Poly2TriTriangulator triangulator(mesh);
1383
1384 // Two separate triangles
1385 auto node0 = mesh.add_point(Point(0,0), 0);
1386 auto node1 = mesh.add_point(Point(0,1), 1);
1387 auto node2 = mesh.add_point(Point(1,0), 2);
1388
1389 auto node3 = mesh.add_point(Point(2,0), 3);
1390 auto node4 = mesh.add_point(Point(2,1), 4);
1391 auto node5 = mesh.add_point(Point(3,0), 5);
1392
1393 auto edge01 = mesh.add_elem(Elem::build(EDGE2));
1394 edge01->set_node(0, node0);
1395 edge01->set_node(1, node1);
1396 auto edge12 = mesh.add_elem(Elem::build(EDGE2));
1397 edge12->set_node(0, node1);
1398 edge12->set_node(1, node2);
1399 auto edge20 = mesh.add_elem(Elem::build(EDGE2));
1400 edge20->set_node(0, node2);
1401 edge20->set_node(1, node0);
1402
1403 auto edge34 = mesh.add_elem(Elem::build(EDGE2));
1404 edge34->set_node(0, node3);
1405 edge34->set_node(1, node4);
1406 auto edge45 = mesh.add_elem(Elem::build(EDGE2));
1407 edge45->set_node(0, node4);
1408 edge45->set_node(1, node5);
1409 auto edge53 = mesh.add_elem(Elem::build(EDGE2));
1410 edge53->set_node(0, node5);
1411 edge53->set_node(1, node3);
1412
1414
1415 testExceptionBase(mesh, triangulator, "multiple loops of Edge");
1416 }
1417
1419 {
1420 LOG_UNIT_TEST;
1421
1423 Poly2TriTriangulator triangulator(mesh);
1424
1425 // Two separate triangles
1426 auto node0 = mesh.add_point(Point(0,0), 0);
1427 auto node1 = mesh.add_point(Point(1,0), 1);
1428 auto node2 = mesh.add_point(Point(0,1), 2);
1429
1430 auto node3 = mesh.add_point(Point(2,0), 3);
1431 auto node4 = mesh.add_point(Point(3,0), 4);
1432 auto node5 = mesh.add_point(Point(2,1), 5);
1433
1434 auto tri012 = mesh.add_elem(Elem::build(TRI3));
1435 tri012->set_node(0, node0);
1436 tri012->set_node(1, node1);
1437 tri012->set_node(2, node2);
1438 auto tri345 = mesh.add_elem(Elem::build(TRI3));
1439 tri345->set_node(0, node3);
1440 tri345->set_node(1, node4);
1441 tri345->set_node(2, node5);
1442
1444
1445 testExceptionBase(mesh, triangulator, "cannot choose one");
1446 }
1447
1448
1450 {
1451 LOG_UNIT_TEST;
1452
1454 Poly2TriTriangulator triangulator(mesh);
1456 testPoly2TriRefinementBase(mesh, nullptr, 1.5, 14);
1457 }
1458
1459
1461 {
1462 LOG_UNIT_TEST;
1463
1465 Poly2TriTriangulator p2t_tri(mesh);
1467 }
1468
1471 const std::vector<TriangulatorInterface::Hole*> * holes,
1472 Real expected_total_area,
1473 dof_id_type n_original_elem,
1474 Real desired_area = 0.1,
1475 FunctionBase<Real> * area_func = nullptr)
1476 {
1477 Poly2TriTriangulator triangulator(mesh);
1478
1479 commonSettings(triangulator);
1480
1481 if (holes)
1482 triangulator.attach_hole_list(holes);
1483
1484 // Try to insert points!
1485 triangulator.desired_area() = desired_area;
1486 triangulator.set_desired_area_function(area_func);
1487
1488 triangulator.triangulate();
1489
1490 // If refinement should have increased our element count, check it
1491 if (desired_area || area_func)
1492 CPPUNIT_ASSERT_GREATER(n_original_elem, mesh.n_elem()); // n_elem+++
1493 else
1494 CPPUNIT_ASSERT_EQUAL(mesh.n_elem(), n_original_elem);
1495
1496 Real area = 0;
1497 for (const auto & elem : mesh.active_local_element_ptr_range())
1498 {
1499 CPPUNIT_ASSERT_EQUAL(elem->level(), 0u);
1500 CPPUNIT_ASSERT_EQUAL(elem->type(), TRI3);
1501
1502 const Real my_area = elem->volume();
1503
1504 // my_area <= desired_area, wow this macro ordering hurts
1505 if (desired_area != 0)
1506 CPPUNIT_ASSERT_LESSEQUAL(desired_area, my_area);
1507
1508 if (area_func != nullptr)
1509 for (auto v : make_range(elem->n_vertices()))
1510 {
1511 const Real local_desired_area =
1512 (*area_func)(elem->point(v));
1513 CPPUNIT_ASSERT_LESSEQUAL(local_desired_area, my_area);
1514 }
1515
1516 area += my_area;
1517 }
1518
1519 mesh.comm().sum(area);
1520
1521 LIBMESH_ASSERT_FP_EQUAL(area, expected_total_area, TOLERANCE*TOLERANCE);
1522 }
1523
1525 {
1526 LOG_UNIT_TEST;
1527
1530 testPoly2TriRefinementBase(mesh, nullptr, 1.5, 15);
1531 }
1532
1534 {
1535 LOG_UNIT_TEST;
1536
1539 // Make sure we see 0 as "don't refine", not "infinitely refine"
1540 testPoly2TriRefinementBase(mesh, nullptr, 1.5, 2, 0);
1541 }
1542
1543
1545 {
1546 LOG_UNIT_TEST;
1547
1550 testPoly2TriRefinementBase(mesh, nullptr, 1.5, 150, 0.01);
1551 }
1552
1554 {
1555#ifdef LIBMESH_HAVE_FPARSER
1556 ParsedFunction<Real> var_area {"0.002*(1+2*x)*(1+2*y)"};
1559 testPoly2TriRefinementBase(mesh, nullptr, 1.5, 150, 0, &var_area);
1560#endif // LIBMESH_HAVE_FPARSER
1561 }
1562
1564 {
1565 LOG_UNIT_TEST;
1566
1567 // Add a diamond hole
1568 TriangulatorInterface::PolygonHole diamond(Point(0.5,0.5), std::sqrt(2_R)/4, 4);
1569 const std::vector<TriangulatorInterface::Hole*> holes { &diamond };
1570
1573 testPoly2TriRefinementBase(mesh, &holes, 1.25, 13);
1574 }
1575
1577 {
1578 LOG_UNIT_TEST;
1579
1582 Poly2TriTriangulator p2t_tri(mesh);
1583
1584 Real total_area = 1.5;
1585
1586 // Try a narrower point
1587 mesh.node_ref(2)(1) = 3;
1588 total_area += 0.5;
1589
1590 // Add a bunch of tiny diamond holes
1591 const int N=3, M=3;
1592 TriangulatorInterface::PolygonHole diamond(Point(), std::sqrt(2_R)/20, 4);
1593
1594 std::vector<TriangulatorInterface::AffineHole> hole_data;
1595 // Reserve so we don't invalidate pointers
1596 hole_data.reserve(M*N);
1597 std::vector<TriangulatorInterface::Hole*> holes;
1598 for (int i : make_range(M))
1599 for (int j : make_range(N))
1600 {
1601 Point shift(Real(i+1)/(M+1),Real(j+1)/(N+1));
1602 hole_data.emplace_back(diamond, 0, shift);
1603 holes.push_back(&hole_data.back());
1604 total_area -= hole_data.back().area();
1605 }
1606
1607 p2t_tri.attach_hole_list(&holes);
1608
1609 p2t_tri.set_refine_boundary_allowed(false);
1610
1611 testTriangulatorInterp(mesh, p2t_tri, 4, 0, total_area, 0.03_R);
1612 }
1613
1615 (dof_id_type n_original_elem,
1616 Real desired_area)
1617 {
1618 // Add a diamond hole, disallowing refinement of it
1619 TriangulatorInterface::PolygonHole diamond(Point(0.5,0.5), std::sqrt(2_R)/4, 4);
1620
1621 CPPUNIT_ASSERT_EQUAL(diamond.refine_boundary_allowed(), true);
1622
1623 diamond.set_refine_boundary_allowed(false);
1624
1625 const std::vector<TriangulatorInterface::Hole*> holes { &diamond };
1626
1627 // Doing extra refinement here to ensure that we had the
1628 // *opportunity* to refine the hole boundaries.
1631 testPoly2TriRefinementBase(mesh, &holes, 1.25, n_original_elem, desired_area);
1632
1633 // Checking that we have more outer boundary sides than we started
1634 // with, and exactly the 4 hole boundary sides we started with.
1635 auto side_bcs = mesh.get_boundary_info().build_side_list();
1636
1637 int n_outer_sides = std::count_if(side_bcs.begin(), side_bcs.end(),
1638 [](auto t){return std::get<2>(t) == 0;});
1639 CPPUNIT_ASSERT_GREATER(4, n_outer_sides); // n_outer_sides > 4
1640 int n_hole_sides = std::count_if(side_bcs.begin(), side_bcs.end(),
1641 [](auto t){return std::get<2>(t) == 1;});
1642 CPPUNIT_ASSERT_EQUAL(n_hole_sides, 4);
1643 }
1644
1645
1647 {
1648 LOG_UNIT_TEST;
1650 }
1651
1652
1654 {
1655 LOG_UNIT_TEST;
1656 // 0.01 creates slivers triggering a poly2tri exception for me - RHS
1658 }
1659
1660
1662 {
1663 // Add a diamond hole
1664 TriangulatorInterface::PolygonHole diamond(Point(0.5,0.5), std::sqrt(2_R)/4, 4);
1665 const std::vector<TriangulatorInterface::Hole*> holes { &diamond };
1666
1669 testPoly2TriRefinementBase(mesh, &holes, 1.25, 125, 0.01);
1670 }
1671
1673 {
1674#ifdef LIBMESH_HAVE_FPARSER
1675 // Add a diamond hole
1676 TriangulatorInterface::PolygonHole diamond(Point(0.5,0.5), std::sqrt(2_R)/4, 4);
1677 const std::vector<TriangulatorInterface::Hole*> holes { &diamond };
1678
1679 ParsedFunction<Real> var_area {"0.002*(0.25+2*x)*(0.25+2*y)"};
1682 testPoly2TriRefinementBase(mesh, &holes, 1.25, 150, 0, &var_area);
1683#endif // LIBMESH_HAVE_FPARSER
1684 }
1685
1686
1687#endif // LIBMESH_HAVE_POLY2TRI
1688
1689};
1690
1691
CPPUNIT_TEST(testPoly2TriNonUniformRefined)
LIBMESH_CPPUNIT_TEST_SUITE(MeshTriangulationTest)
The goal of this test is to verify proper operation of the interfaces to triangulation libraries.
CPPUNIT_TEST(testTriangleHalfDomain)
CPPUNIT_TEST(testPoly2TriEdge3s)
void testEdge3Mesh(MeshBase &mesh)
void testTriangulatorInterp(UnstructuredMesh &mesh, TriangulatorInterface &triangulator, int interpolate_boundary_points, dof_id_type n_expected_elem, Real expected_total_area=1.5, Real desired_area=1000)
CPPUNIT_TEST(testTriangleEdges)
CPPUNIT_TEST(testPoly2TriHolePerturbed)
void testEdge3ToTri6Base(MeshBase &mesh, TriangulatorInterface &triangulator)
CPPUNIT_TEST(testPoly2TriEdge3ToTri6)
CPPUNIT_TEST(testPoly2TriInterp)
CPPUNIT_TEST(testPoly2TriLargeCocircular)
CPPUNIT_TEST(testPoly2TriHolesInteriorRefined)
CPPUNIT_TEST(testPoly2TriNonRefined)
CPPUNIT_TEST(testTriangleInterp)
void testEdgesMesh(MeshBase &mesh, ElemType elem_type)
CPPUNIT_TEST(testPoly2TriEdge3ToTri6FlipThrows)
CPPUNIT_TEST(testPoly2TriHolesInteriorExtraRefined)
CPPUNIT_TEST(testPoly2TriBadEdges)
CPPUNIT_TEST(testPoly2TriBad1DMultiBoundary)
void testTriangulatorHoles(MeshBase &mesh, TriangulatorInterface &triangulator)
void testTriangulatorBase(MeshBase &mesh, TriangulatorInterface &triangulator)
void testFoundCenters(const MeshBase &mesh, const std::vector< Point > &expected_centers)
CPPUNIT_TEST(testPoly2TriBad2DMultiBoundary)
void testTriangulatorSegments(MeshBase &mesh, TriangulatorInterface &triangulator)
CPPUNIT_TEST(testTriangleRoundHole)
void testTriangulatorRoundHole(MeshBase &mesh, TriangulatorInterface &triangulator)
CPPUNIT_TEST(testPoly2TriHoles)
void testExceptionBase(MeshBase &mesh, TriangulatorInterface &triangulator, const char *re)
CPPUNIT_TEST(testTriangleSegments)
CPPUNIT_TEST(testPoly2TriHolesInterpRefined)
CPPUNIT_TEST(testTriangleEdge3ToTri6)
void testPoly2TriRefinementBase(UnstructuredMesh &mesh, const std::vector< TriangulatorInterface::Hole * > *holes, Real expected_total_area, dof_id_type n_original_elem, Real desired_area=0.1, FunctionBase< Real > *area_func=nullptr)
CPPUNIT_TEST(testTriangleMeshedHoles)
CPPUNIT_TEST(testPoly2TriHolesExtraRefined)
void testHalfDomain(MeshBase &mesh, TriangulatorInterface &triangulator, ElemType elem_type)
void testTriangulator(MeshBase &mesh, TriangulatorInterface &triangulator)
CPPUNIT_TEST(testTriangleInterp2)
CPPUNIT_TEST(testPoly2TriRefined)
void testTriangulatorTrapMesh(UnstructuredMesh &mesh)
void testTriangulatorHolePerturbed(MeshBase &mesh, TriangulatorInterface &triangulator)
void testPoly2TriHolesInteriorRefinedBase(dof_id_type n_original_elem, Real desired_area)
CPPUNIT_TEST(testPoly2TriSegments)
CPPUNIT_TEST(testPoly2Tri)
void testTriangulatorHoleTangentPerturbed(MeshBase &mesh, TriangulatorInterface &triangulator)
CPPUNIT_TEST(testTriangleHoleContains)
CPPUNIT_TEST(testTriangle)
void commonSettings(TriangulatorInterface &triangulator)
CPPUNIT_TEST(testPoly2TriHolesRefined)
CPPUNIT_TEST(testPoly2TriEdgesRefined)
CPPUNIT_TEST(testTriangleHoleArea)
CPPUNIT_TEST(testPoly2TriMeshedHoles)
CPPUNIT_TEST(testHoleContainsRayDegeneracies)
CPPUNIT_TEST(testPoly2TriHolesNonUniformRefined)
CPPUNIT_TEST(testPoly2TriHoleTangentPerturbed)
CPPUNIT_TEST(testPoly2TriEdge3ToTri7CenterFixup)
CPPUNIT_TEST(testTriangleHoles)
CPPUNIT_TEST(testPoly2TriEdges)
CPPUNIT_TEST(testPoly2TriExtraRefined)
CPPUNIT_TEST(testPoly2TriHalfDomainEdge3)
CPPUNIT_TEST(testPoly2TriRoundHole)
CPPUNIT_TEST(testPoly2TriInterp2)
void testTriangulatorMeshedHoles(MeshBase &mesh, TriangulatorInterface &triangulator)
CPPUNIT_TEST(testPoly2TriHalfDomain)
void max(const T &r, T &o, Request &req) const
std::vector< BCTuple > build_side_list(BCTupleSortBy sort_by=BCTupleSortBy::ELEM_ID) const
virtual Node *& set_node(const unsigned int i)
Definition elem.h:2688
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition elem.C:382
Base class for functors that can be evaluated at a point and (optionally) time.
A class to represent the internal "this should never happen" errors, to be thrown by "libmesh_error()...
This is the MeshBase class.
Definition mesh_base.h:81
virtual const Node & node_ref(const dof_id_type i) const
Definition mesh_base.h:750
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition mesh_base.h:178
virtual dof_id_type n_elem() const =0
void prepare_for_use(const bool skip_renumber_nodes_and_elements, const bool skip_find_neighbors)
Prepare a newly created (or read) mesh for use.
Definition mesh_base.C:901
virtual dof_id_type n_nodes() const =0
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
virtual Elem * add_elem(Elem *e)=0
Add elem e to the end of the element array.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
const Parallel::Communicator & comm() const
A Function generated (via FParser) by parsing a mathematical expression.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:41
A C++ interface between LibMesh and the poly2tri library, with custom code for Steiner point insertio...
virtual void set_refine_boundary_allowed(bool refine_bdy_allowed) override
Set whether or not the triangulation is allowed to refine the mesh boundary when refining the interio...
virtual void set_desired_area_function(FunctionBase< Real > *desired) override
Set a function giving desired triangle area as a function of position.
virtual void triangulate() override
Internally, this calls the poly2tri triangulation code in a loop, inserting our owner Steiner points ...
A C++ interface between LibMesh and the Triangle library written by J.R.
Another concrete instantiation of the hole, this one should be sufficiently general for most non-poly...
An abstract class for defining a 2-dimensional hole.
virtual bool refine_boundary_allowed() const
Get whether or not the triangulation is allowed to refine the mesh boundary when refining the interio...
Real area() const
Return the area of the hole.
virtual void set_refine_boundary_allowed(bool refine_bdy_allowed)
Set whether or not a triangulator is allowed to refine the hole boundary when refining the mesh inter...
Another concrete instantiation of the hole, as general as ArbitraryHole, but based on an existing 1D ...
A concrete instantiation of the Hole class that describes polygonal (triangular, square,...
virtual Point point(const unsigned int n) const override
Return the nth point defining the hole.
virtual unsigned int n_points() const override
The number of geometric points which define the hole.
void set_outer_boundary_ids(std::set< std::size_t > bdy_ids)
A set of ids to allow on the outer boundary loop: interpreted as boundary ids of 2D elements and/or s...
Real & minimum_angle()
Sets and/or gets the minimum desired angle.
TriangulationType & triangulation_type()
Sets and/or gets the desired triangulation type.
virtual void triangulate()=0
This is the main public interface for this function.
@ PSLG
Triangulate the interior of a Planar Straight Line Graph, which is defined implicitly by the order of...
bool & smooth_after_generating()
Sets/gets flag which tells whether to do two steps of Laplace mesh smoothing after generating the gri...
void set_verify_hole_boundaries(bool v)
Verifying that hole boundaries don't cross the outer boundary or each other is something like O(N_bdy...
std::vector< std::pair< unsigned int, unsigned int > > segments
When constructing a PSLG, if the node numbers do not define the desired boundary segments implicitly ...
void attach_hole_list(const std::vector< Hole * > *holes)
Attaches a vector of Hole* pointers which will be meshed around.
Real & desired_area()
Sets and/or gets the desired triangle area.
ElemType & elem_type()
Sets and/or gets the desired element type.
void set_fixup_tri7_center_nodes(bool v)
For TRI7 elements, after boundary mid-edge nodes have been snapped to a curved boundary in increase_t...
void set_interpolate_boundary_points(int n_points)
Complicated setter, for compatibility with insert_extra_points()
bool absolute_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
bool relative_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
auto norm_sq() const
auto norm() const
The UnstructuredMesh class is derived from the MeshBase class.
Communicator * TestCommWorld
MeshBase & mesh
CPPUNIT_TEST_SUITE_REGISTRATION(MeshTriangulationTest)
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
void build_sphere(UnstructuredMesh &mesh, const Real radius=1, const unsigned int n_refinements=2, const ElemType type=INVALID_ELEM, const unsigned int n_smooth=2, const bool flat=true)
Fills mesh with a mesh discretizing a ball (||x||<= radius) or sphere (||x|| = radius) domain.
void translate(MeshBase &mesh, const Real xt=0., const Real yt=0., const Real zt=0.)
Translates the mesh.
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
ElemType
Defines an enum for geometric element types.
libmesh_assert(ctx)
const Real pi
.
Definition libmesh.h:292
static constexpr Real TOLERANCE
uint8_t dof_id_type
Definition id_types.h:67
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition int_range.h:176
const Real radius