libMesh
Loading...
Searching...
No Matches
mesh_smoother_test.C
Go to the documentation of this file.
1#include <libmesh/boundary_info.h>
2#include <libmesh/elem.h>
3#include <libmesh/enum_elem_type.h>
4#include <libmesh/enum_quadrature_type.h>
5#include <libmesh/function_base.h>
6#include <libmesh/libmesh.h>
7#include <libmesh/mesh_generation.h>
8#include <libmesh/mesh_modification.h>
9#include <libmesh/mesh_smoother_laplace.h>
10#include <libmesh/mesh_smoother_vsmoother.h>
11#include <libmesh/mesh_tools.h>
12#include <libmesh/node.h>
13#include <libmesh/reference_elem.h>
14#include <libmesh/replicated_mesh.h>
15#include <libmesh/mesh.h>
16#include <libmesh/system.h> // LIBMESH_HAVE_SOLVER define
17#include <libmesh/variational_smoother_constraint.h>
18#include "libmesh/face_tri.h"
19#include "libmesh/cell_prism.h"
20#include "libmesh/utility.h"
21#include "libmesh/enum_to_string.h"
22#include "libmesh/parallel_ghost_sync.h"
23
24#include "test_comm.h"
25#include "libmesh_cppunit.h"
26
27#include <random>
28
29namespace
30{
31using namespace libMesh;
32using Utility::pow;
33
34// Distortion function that doesn't distort boundary nodes
35// 2D only, use for LaplaceMeshSmoother
36class DistortSquare : public FunctionBase<Real>
37{
38 std::unique_ptr<FunctionBase<Real>> clone() const override
39 {
40 return std::make_unique<DistortSquare>();
41 }
42
43 Real operator()(const Point &, const Real = 0.) override
44 {
45 libmesh_not_implemented();
46 } // scalar-only API
47
48 // Skew inward based on a cubic function
49 void operator()(const Point & p, const Real, DenseVector<Real> & output)
50 {
51 output.resize(3);
52 const Real eta = 2 * p(0) - 1;
53 const Real zeta = 2 * p(1) - 1;
54 output(0) = p(0) + (pow<3>(eta) - eta) * p(1) * (1 - p(1));
55 output(1) = p(1) + (pow<3>(zeta) - zeta) * p(0) * (1 - p(0));
56 output(2) = 0;
57 }
58};
59
60// Distortion function supporting 1D, 2D, and 3D
61// Use for VariationalMeshSmoother
62class DistortHyperCube : public FunctionBase<Real>
63{
64public:
65 DistortHyperCube(const unsigned int dim) : _dim(dim) {}
66
67private:
68 std::unique_ptr<FunctionBase<Real>> clone() const override
69 {
70 return std::make_unique<DistortHyperCube>(_dim);
71 }
72
73 Real operator()(const Point &, const Real = 0.) override { libmesh_not_implemented(); }
74
75 void operator()(const Point & p, const Real, DenseVector<Real> & output) override
76 {
77 output.resize(3);
78 output.zero();
79
80 // Count how many coordinates are exactly on the boundary (0 or 1)
81 std::array<bool, 3> is_on_boundary = {false, false, false};
82 for (unsigned int i = 0; i < _dim; ++i)
83 if (std::abs(p(i)) < TOLERANCE || std::abs(p(i) - 1.) < TOLERANCE)
84 is_on_boundary[i] = true;
85
86 // Distort only those directions not fixed on the boundary
87 for (unsigned int i = 0; i < _dim; ++i)
88 {
89 if (!is_on_boundary[i]) // only distort free dimensions
90 {
91 // This value controls the strength of the distortion
92 Real modulation = 0.3;
93 Real xi = 2. * p(i) - 1.;
94 for (unsigned int j = 0; j < _dim; ++j)
95 {
96 if (j != i)
97 {
98 Real pj = std::clamp(p(j), 0., 1.); // ensure numeric safety
99 modulation *= (pj - 0.5) * (pj - 0.5) * 4.; // quadratic bump centered at 0.5
100 }
101 }
102 const auto delta = (pow<3>(xi) - xi) * modulation;
103 // Check for delta = 0 to make sure we perturb every point
104 output(i) = (delta > TOLERANCE) ? p(i) + delta : 1.03 * p(i);
105 }
106 else
107 output(i) = p(i); // dimension on boundary remains unchanged
108 }
109 }
110
111 const unsigned int _dim;
112};
113
114class SquareToParallelogram : public FunctionBase<Real>
115{
116 std::unique_ptr<FunctionBase<Real>> clone() const override
117 {
118 return std::make_unique<SquareToParallelogram>();
119 }
120
121 Real operator()(const Point &, const Real = 0.) override
122 {
123 libmesh_not_implemented();
124 } // scalar-only API
125
126 // Has the effect that a square, meshed into right triangles with diagonals
127 // rising from lower-left to upper-right, is transformed into a left-leaning
128 // parallelogram of eqilateral triangles.
129 void operator()(const Point & p, const Real, DenseVector<Real> & output)
130 {
131 output.resize(3);
132 output(0) = p(0) - 0.5 * p(1);
133 output(1) = 0.5 * std::sqrt(Real(3)) * p(1);
134 output(2) = p(2);
135 }
136};
137
138class ParallelogramToSquare : public FunctionBase<Real>
139{
140 std::unique_ptr<FunctionBase<Real>> clone() const override
141 {
142 return std::make_unique<ParallelogramToSquare>();
143 }
144
145 Real operator()(const Point &, const Real = 0.) override
146 {
147 libmesh_not_implemented();
148 } // scalar-only API
149
150 // Has the effect that a left-leaning parallelogram of equilateral triangles is transformed
151 // into a square of right triangle with diagonals rising from lower-left to upper-right.
152 // This is the inversion of the SquareToParallelogram mapping.
153 void operator()(const Point & p, const Real, DenseVector<Real> & output)
154 {
155 output.resize(3);
156 output(0) = p(0) + p(1) / std::sqrt(Real(3));
157 output(1) = (2. / std::sqrt(Real(3))) * p(1);
158 output(2) = p(2);
159 }
160};
161
162class CubeToParallelepiped : public FunctionBase<Real>
163{
164 std::unique_ptr<FunctionBase<Real>> clone() const override
165 {
166 return std::make_unique<CubeToParallelepiped>();
167 }
168
169 Real operator()(const Point &, const Real = 0.) override
170 {
171 libmesh_not_implemented();
172 } // scalar-only API
173
174 // Has the effect that a cube, meshed into right triangular prisms with diagonals
175 // rising from lower-right to upper-left, is transformed into a right-leaning
176 // parallelepiped of equilateral triangular prisms. Additionally, the z direction
177 // is scaled to ensure element height to base aspect ratios match the target element.
178 // Without the correct aspect ratio, the smoothed mesh nodes are off and the
179 // distortion_is assertions fail.
180 void operator()(const Point & p, const Real, DenseVector<Real> & output)
181 {
182 output.resize(3);
183 output(0) = p(0) + 0.5 * p(1);
184 output(1) = 0.5 * std::sqrt(3) * p(1);
185 // Adjusting z to get the triangular area to element height aspect ratio To
186 // match the target element
187 output(2) = p(2) * 0.25 * std::sqrt(3.);
188 }
189};
190
191class ParallelepipedToCube : public FunctionBase<Real>
192{
193 std::unique_ptr<FunctionBase<Real>> clone() const override
194 {
195 return std::make_unique<ParallelepipedToCube>();
196 }
197
198 Real operator()(const Point &, const Real = 0.) override
199 {
200 libmesh_not_implemented();
201 } // scalar-only API
202
203 // Has the effect that a right-leaning parallelepiped of equilaterial triangular
204 // prisms with is transformed into a cube of right triangular prisms with
205 // diagonals rising from lower-right to upper-left. Additionally, the z direction
206 // is scaled to ensure element heights align with the values expected by the
207 // distortion_is function.
208 // This is the inversion of the CubeToParallelepiped mapping.
209 void operator()(const Point & p, const Real, DenseVector<Real> & output)
210 {
211 output.resize(3);
212 output(0) = p(0) - p(1) / std::sqrt(3.);
213 output(1) = (2. / std::sqrt(3)) * p(1);
214 // Undoing the aspect ratio adjustment to get the z divisions to work with
215 // distortion_is
216 output(2) = p(2) * 4. / std::sqrt(3.);
217 }
218};
219}
220
221using namespace libMesh;
222
223class MeshSmootherTest : public CppUnit::TestCase
224{
229public:
231
232#if LIBMESH_DIM > 2
237#if defined(LIBMESH_ENABLE_VSMOOTHER) && defined(LIBMESH_HAVE_SOLVER)
241
248
252
258
264
271
276
277#if defined(LIBMESH_HAVE_GZSTREAM)
281#endif
282
283#endif // LIBMESH_ENABLE_VSMOOTHER
284#endif
285
287
288public:
289 void setUp() {}
290
291 void tearDown() {}
292
294 {
295 LOG_UNIT_TEST;
296
297 constexpr unsigned int n_elems_per_side = 4;
298
300 mesh, n_elems_per_side, n_elems_per_side, 0., 1., 0., 1., type);
301
302 // Move it around so we have something that needs smoothing
303 DistortSquare ds;
305
306 // Assert the distortion is as expected
307 auto center_distortion_is =
308 [](const Node & node, int d, bool distortion, Real distortion_tol = TOLERANCE) {
309 const Real r = node(d);
310 const Real R = r * n_elems_per_side;
311 CPPUNIT_ASSERT_GREATER(-TOLERANCE * TOLERANCE, r);
312 CPPUNIT_ASSERT_GREATER(-TOLERANCE * TOLERANCE, 1 - r);
313
314 // If we're at the boundaries we should *never* be distorted
315 if (std::abs(node(0)) < TOLERANCE * TOLERANCE ||
316 std::abs(node(0) - 1) < TOLERANCE * TOLERANCE)
317 {
318 const Real R1 = node(1) * n_elems_per_side;
319 CPPUNIT_ASSERT_LESS(TOLERANCE * TOLERANCE, std::abs(R1 - std::round(R1)));
320 return true;
321 }
322
323 if (std::abs(node(1)) < TOLERANCE * TOLERANCE ||
324 std::abs(node(1) - 1) < TOLERANCE * TOLERANCE)
325 {
326 const Real R0 = node(0) * n_elems_per_side;
327 CPPUNIT_ASSERT_LESS(TOLERANCE * TOLERANCE, std::abs(R0 - std::round(R0)));
328
329 return true;
330 }
331
332 // If we're at the center we're fine
333 if (std::abs(r - 0.5) < TOLERANCE * TOLERANCE)
334 return true;
335
336 return ((std::abs(R - std::round(R)) > distortion_tol) == distortion);
337 };
338
339 for (auto node : mesh.node_ptr_range())
340 {
341 CPPUNIT_ASSERT(center_distortion_is(*node, 0, true));
342 CPPUNIT_ASSERT(center_distortion_is(*node, 1, true));
343 }
344
345 // Enough iterations to mostly fix us up. Laplace seems to be at 1e-3
346 // tolerance by iteration 6, so hopefully everything is there on any
347 // system by 8.
348 for (unsigned int i = 0; i != 8; ++i)
349 smoother.smooth();
350
351 // Make sure we're not too distorted anymore.
352 for (auto node : mesh.node_ptr_range())
353 {
354 CPPUNIT_ASSERT(center_distortion_is(*node, 0, false, 1e-3));
355 CPPUNIT_ASSERT(center_distortion_is(*node, 1, false, 1e-3));
356 }
357 }
358
359 // Helper function to determine how many dimensions of a given point lie at
360 // the center and faces of a sub-cube
361 std::pair<unsigned int, unsigned int>
362 numCenteredAndFacedDimensions(const Point & point, const Real & side_length, const Real & tol)
363 {
364 const auto half_length = 0.5 * side_length;
365 unsigned int num_centered = 0;
366 unsigned int num_face = 0;
367 for (const auto d : make_range(3))
368 {
369 const auto num_half_lengths = point(d) / half_length;
370 if (std::abs(num_half_lengths - std::round(num_half_lengths)) > tol)
371 // This coordinante does not occur at a sub-cube face or center
372 continue;
373
374 if (std::lround(num_half_lengths) % 2)
375 // An odd number of half lengths means point(d) is at a sub-cube center
376 ++num_centered;
377 else
378 // An odd number of half lengths means point(d) is on a sub-cube face
379 ++num_face;
380 }
381
382 return std::make_pair(num_centered, num_face);
383 }
384
385 // Helper function to determine whether a given point lies at the center of
386 // a sub-cube
387 bool pointIsCubeCenter(const Point & point, const Real & side_length, const Real & tol = TOLERANCE)
388 {
389 return numCenteredAndFacedDimensions(point, side_length, tol).first == 3;
390 }
391
392 // Helper function to determine whether a given point lies at the vertex of
393 // a sub-cube
394 bool pointIsCubeVertex(const Point & point, const Real & side_length, const Real & tol = TOLERANCE)
395 {
396 return numCenteredAndFacedDimensions(point, side_length, tol).second == 3;
397 }
398
399 // Helper function to determine whether a given point lies at the center of
400 // a sub-cube face
401 bool pointIsCubeFaceCenter(const Point & point, const Real & side_length, const Real & tol = TOLERANCE)
402 {
403 const auto result = numCenteredAndFacedDimensions(point, side_length, tol);
404 return (result.first == 2) && (result.second == 1);
405 }
406
408 VariationalMeshSmoother &smoother,
409 const ElemType type,
410 const bool multiple_subdomains = false,
411 const bool tangle_mesh = false,
412 const Real tangle_damping_factor = 1.0)
413 {
414 LOG_UNIT_TEST;
415
416 if (multiple_subdomains && tangle_mesh)
417 libmesh_not_implemented_msg(
418 "Arbitrary mesh tangling with multiple subdomains is not supported.");
419
420 // Get mesh dimension, determine whether type is triangular
421 const auto * ref_elem = &(ReferenceElem::get(type));
422 const auto dim = ref_elem->dim();
423 const bool type_is_tri = Utility::enum_to_string(type).compare(0, 3, "TRI") == 0;
424 const bool type_is_pyramid = Utility::enum_to_string(type).compare(0, 7, "PYRAMID") == 0;
425 const bool type_is_prism = Utility::enum_to_string(type).compare(0, 5, "PRISM") == 0;
426 const bool type_is_tet = Utility::enum_to_string(type).compare(0, 3, "TET") == 0;
427
428 // Used fewer elems for higher order types, as extra midpoint nodes will add
429 // enough complexity
430 unsigned int n_elems_per_side = 4 / Elem::type_to_default_order_map[type];
431 const auto side_length = 1.0 / n_elems_per_side;
432
433 switch (dim)
434 {
435 case 1:
436 MeshTools::Generation::build_line(mesh, n_elems_per_side, 0., 1., type);
437 break;
438 case 2:
440 mesh, n_elems_per_side, n_elems_per_side, 0., 1., 0., 1., type);
441 break;
442
443 case 3:
445 n_elems_per_side,
446 n_elems_per_side,
447 n_elems_per_side,
448 0.,
449 1.,
450 0.,
451 1.,
452 0.,
453 1.,
454 type);
455 break;
456
457 default:
458 libmesh_error_msg("Unsupported dimension " << dim);
459 }
460
461 // Move it around so we have something that needs smoothing
462 DistortHyperCube dh(dim);
464
465 const auto & boundary_info = mesh.get_boundary_info();
466 const auto & proc_id = mesh.processor_id();
467
468 if (tangle_mesh)
469 {
470 // We tangle the mesh by (partially) swapping the locations of 2 nodes.
471 // If the n-dimentional hypercube is represented as a grid with
472 // (undistorted) divisions occuring at integer numbers, we will
473 // (partially) swap the nodes nearest p1 = (1,1,1) and p2 = (2,1,2).
474
475 // Define p1 and p2 given the integer indices
476 // Undistorted elem side length
477 const Real dr = 1. / (n_elems_per_side * Elem::type_to_default_order_map[type]);
478 const Point p1 = Point(dr, dim > 1 ? dr : 0, dim > 2 ? dr : 0);
479 const Point p2 = Point(2. * dr, dim > 1 ? dr : 0, dim > 2 ? 2. * dr : 0);
480
481 // We need to find the nodes of the mesh that are closest to p1 and p2.
482 // This will require some parallel communication for distributed meshes.
483
484 // Distances between local mesh nodes and p1
485 std::map<dof_id_type, Real> dist1_map;
486 // Distances between local mesh nodes and p2
487 std::map<dof_id_type, Real> dist2_map;
488
489 for (const auto * node : mesh.local_node_ptr_range())
490 {
491 dist1_map[node->id()] = (*node - p1).norm();
492 dist2_map[node->id()] = (*node - p2).norm();
493 }
494
495 // Helper function to analyze a dist_map accross all processors and
496 // return the node id and spatial coordinates for the node with the
497 // smallest distance
498 auto get_closet_point_accross_all_procs =
499 [&mesh, &proc_id](const std::map<dof_id_type, Real> &dist_map) {
500
501 processor_id_type broadcasting_proc = 0;
502 Real d_min_local = std::numeric_limits<Real>::max();
504
505 // Only execute this if this proc owns any nodes. Otherwise,
506 // use default values defined above
507 if (dist_map.size())
508 {
509 // Iterator to the map entry with the smallest distance
510 auto min_it =
511 std::min_element(dist_map.begin(), dist_map.end(),
512 [](const auto &a, const auto &b) {
513 return a.second < b.second;
514 });
515
516 node_id = min_it->first;
517 d_min_local = min_it->second;
518 }
519
520 // Get the smallest distance accross all procs
521 auto d_min_global = d_min_local;
522 mesh.comm().min(d_min_global);
523
524 // Find the proc id, node_id, and spatial coordinantes for the
525 // node with the smallest distance
526 Point node;
527 if (d_min_local == d_min_global) {
528 broadcasting_proc = proc_id;
529 node = mesh.node_ref(node_id);
530 }
531
532 mesh.comm().max(broadcasting_proc);
533 // Broadcast information about this node to all procs
534 mesh.comm().broadcast(node_id, broadcasting_proc);
535 mesh.comm().broadcast(node, broadcasting_proc);
536
537 return std::pair(node_id, node);
538 };
539
540 const auto [node_id1, node1] =
541 get_closet_point_accross_all_procs(dist1_map);
542 const auto [node_id2, node2] =
543 get_closet_point_accross_all_procs(dist2_map);
544
545 // modify the nodes if they are on this proc
546 const auto displacement = tangle_damping_factor * (node1 - node2);
547 if (mesh.query_node_ptr(node_id1))
548 mesh.node_ref(node_id1) -= displacement;
549 if (mesh.query_node_ptr(node_id2))
550 mesh.node_ref(node_id2) += displacement;
551
552 SyncNodalPositions sync_object(mesh);
554 mesh.nodes_end(), sync_object);
555
556 // Make sure the mesh is tangled
557 // Need this to create the system we are about to access
558 smoother.setup();
559 const auto &unsmoothed_info = smoother.get_mesh_info();
560 CPPUNIT_ASSERT(unsmoothed_info.mesh_is_tangled);
561 }
562
563 // Add multiple subdomains if requested
564 std::unordered_map<subdomain_id_type, Real> distorted_subdomain_volumes;
565 subdomain_id_type highest_subdomain_id = 0;
566 if (multiple_subdomains)
567 {
568 // Modify the subdomain ids in an interesting way
569 for (auto * elem : mesh.active_element_ptr_range())
570 {
571 unsigned int subdomain_id = 0;
572 for (const auto d : make_range(dim))
573 if (elem->vertex_average()(d) > 0.5)
574 ++subdomain_id;
575 elem->subdomain_id() += subdomain_id;
576 }
577
578 // This loop should NOT be combined with the one above because we need to
579 // finish checking and updating subdomain ids for all elements before
580 // recording the final subdomain volumes.
581 for (auto *elem : mesh.active_element_ptr_range()) {
582 const auto sub_id = elem->subdomain_id();
583 // Don't double count an element's volume on multiple procs
584 if (elem->processor_id() != proc_id)
585 continue;
586 distorted_subdomain_volumes[sub_id] += elem->volume();
587 highest_subdomain_id = std::max(sub_id, highest_subdomain_id);
588 }
589
590 // Parallel communication
591 mesh.comm().max(highest_subdomain_id);
592 for (const auto sub_id : make_range(highest_subdomain_id + 1)) {
593 // Make sure sub_id exists in the map on this proc
594 if (distorted_subdomain_volumes.find(sub_id) ==
595 distorted_subdomain_volumes.end())
596 distorted_subdomain_volumes[sub_id] = 0.;
597
598 mesh.comm().sum(distorted_subdomain_volumes[sub_id]);
599 }
600 }
601
602 // We may have just invalidated the get_mesh_subdomains() cache by
603 // adding a new one, and libMesh marked our spatial_dimension()
604 // cache as invalidated in redistribute(). Just rebuild our
605 // caches before we start asking for anything like
606 // elem_default_orders().
608
609 // Get the mesh order
610 const auto & elem_orders = mesh.elem_default_orders();
611 libmesh_error_msg_if(elem_orders.size() != 1,
612 "The variational smoother cannot be used for mixed-order meshes!");
613 // For pyramids, the factor 2 accounts for the account that cubes of side
614 // length s are divided into pyramids of base side length s and height s/2.
615 // The factor 4 lets us catch triangular face midpoint nodes for PYRAMID18 elements.
616 // Similar reasoning for tets.
617 const auto scale_factor = *elem_orders.begin() * ((type_is_pyramid || type_is_tet) ? 2 * 4 : 1);
618
619 // Function to assert the node distortion is as expected
620 auto node_distortion_is = [&n_elems_per_side, &dim, &boundary_info, &scale_factor, &type_is_prism](
621 const Node & node, bool distortion, Real distortion_tol = TOLERANCE) {
622 // Get boundary ids associated with the node
623 std::vector<boundary_id_type> boundary_ids;
624 boundary_info.boundary_ids(&node, boundary_ids);
625
626 // This tells us what type of node we are: internal, sliding, or fixed
627 const auto num_dofs = dim - boundary_ids.size();
628 /*
629 * The following cases of num_dofs are possible, ASSUMING all boundaries
630 * are non-overlapping
631 * 3D: 3-0, 3-1, 3-2, 3-3
632 * = 3 2 1 0
633 * internal sliding, sliding, fixed
634 * 2D: 2-0, 2-1, 2-2
635 * = 2 1 0
636 * internal sliding, fixed
637 * 1D: 1-0, 1-1
638 * = 1 0
639 * internal fixed
640 *
641 * We expect that R is an integer in [0, n_elems_per_side] for
642 * num_dofs of the node's cooridinantes, while the remaining coordinates
643 * are fixed to the boundary with value 0 or 1. In other words, at LEAST
644 * dim - num_dofs coordinantes should be 0 or 1.
645 */
646
647 std::size_t num_zero_or_one = 0;
648
649 bool distorted = false;
650 for (const auto d : make_range(dim))
651 {
652 const Real r = node(d);
653 const Real R = r * n_elems_per_side * scale_factor;
654 CPPUNIT_ASSERT_GREATER(-distortion_tol * distortion_tol, r);
655 CPPUNIT_ASSERT_GREATER(-distortion_tol * distortion_tol, 1 - r);
656
657 bool d_distorted = std::abs(R - std::round(R)) > distortion_tol;
658 if (type_is_prism && (scale_factor == 3))
659 {
660 // Adjustment required for triangular face nodes of PRISM20/21 elements.
661 // These elements have fe_order 3. This takes care of nodes occuring at
662 // thirds, but not halves.
663 const Real R_prism = R / scale_factor * 2;
664 const bool d_distorted_prism =
665 std::abs(R_prism - std::round(R_prism)) > distortion_tol;
666 d_distorted &= d_distorted_prism;
667 }
668 distorted |= d_distorted;
669 num_zero_or_one += (absolute_fuzzy_equals(r, 0.) || absolute_fuzzy_equals(r, 1.));
670 }
671
672 CPPUNIT_ASSERT_GREATEREQUAL(dim - num_dofs, num_zero_or_one);
673
674 // We can never expect a fixed node to be distorted
675 if (num_dofs == 0)
676 // if (num_dofs < dim)
677 return true;
678 return distorted == distortion;
679 };
680
681 // Make sure our DistortHyperCube transformation has distorted the mesh
682 for (auto node : mesh.node_ptr_range())
683 CPPUNIT_ASSERT(node_distortion_is(*node, true));
684
685 // Transform the square mesh of triangles to a parallelogram mesh of
686 // triangles. This will allow the Variational Smoother to smooth the mesh
687 // to the optimal case of equilateral triangles
688 if (type_is_tri)
689 {
690 // Transform the square mesh of triangles to a parallelogram mesh of
691 // triangles. This will allow the Variational Smoother to smooth the mesh
692 // to the optimal case of equilateral triangles
693 SquareToParallelogram stp;
695 }
696 else if (type_is_prism)
697 {
698 // Transform the cube mesh of prisms to a parallelepiped mesh of
699 // prisms. This will allow the Variational Smoother to smooth the mesh
700 // to the optimal case of equilateral triangular prisms
701 CubeToParallelepiped ctp;
703 }
704
705 smoother.smooth();
706
707 if (type_is_tri)
708 {
709 // Transform the parallelogram mesh back to a square mesh. In the case of
710 // the Variational Smoother, equilateral triangules will be transformed
711 // into right triangules that align with the original undistorted mesh.
712 ParallelogramToSquare pts;
714 }
715 else if (type_is_prism)
716 {
717 // Transform the parallelepiped mesh back to a cube mesh. In the case of
718 // the Variational Smoother, equilateral triangular prisms will be transformed
719 // into right triangular prisms that align with the original undistorted mesh.
720 ParallelepipedToCube ptc;
722 }
723
724 if (multiple_subdomains)
725 {
726 // Make sure the subdomain volumes didn't change. Although this doesn't
727 // guarantee that the subdomain didn't change, it is a good indicator
728 // and is friedly to sliding subdomain boundary nodes.
729 std::unordered_map<subdomain_id_type, Real> smoothed_subdomain_volumes;
730 for (auto *elem : mesh.active_element_ptr_range()) {
731 // Don't double count an element's volume on multiple procs
732 if (elem->processor_id() != proc_id)
733 continue;
734 smoothed_subdomain_volumes[elem->subdomain_id()] += elem->volume();
735 }
736
737 // Parallel communication
738 for (const auto sub_id : make_range(highest_subdomain_id + 1)) {
739 // Make sure sub_id exists in the map on this proc
740 if (smoothed_subdomain_volumes.find(sub_id) ==
741 smoothed_subdomain_volumes.end())
742 smoothed_subdomain_volumes[sub_id] = 0.;
743
744 mesh.comm().sum(smoothed_subdomain_volumes[sub_id]);
745 }
746
747 for (const auto sub_id : make_range(highest_subdomain_id + 1))
748 CPPUNIT_ASSERT(relative_fuzzy_equals(
749 libmesh_map_find(distorted_subdomain_volumes, sub_id),
750 libmesh_map_find(smoothed_subdomain_volumes, sub_id), TOLERANCE));
751 }
752 else
753 {
754 std::unordered_map<dof_id_type, std::vector<const Elem *>> nodes_to_elem_map;
755 MeshTools::build_nodes_to_elem_map(mesh, nodes_to_elem_map);
756
757 // Make sure we're not too distorted anymore, using the given
758 // tolerance.
759 std::set<dof_id_type> nodes_checked;
760 const Real tol = TOLERANCE;
761
762 for (const auto * elem : mesh.active_element_ptr_range())
763 {
764 for (const auto local_node_id : make_range(elem->n_nodes()))
765 {
766 const auto & node = elem->node_ref(local_node_id);
767 if (nodes_checked.find(node.id()) != nodes_checked.end())
768 continue;
769
770 nodes_checked.insert(node.id());
771
772 // Check for special case where pyramidal base-to-apex midpoint
773 // nodes are not smoothed to the actual midpoints.
774 if (type_is_pyramid)
775 {
776 if (local_node_id > 8 && local_node_id < 13)
777 {
778 // Base-Apex midpoint nodes
779 //
780 // Due to the nature of the pyramidal target element and the
781 // cubic nature of the test mesh, for a dilation weight o
782 // 0.5, smoothed base-apex midpoint nodes are smoothed to
783 // the point base + x * (apex - base) instead of
784 // base + 0.5 (apex - base), where x is in (0,1) and
785 // depends on the number of nodes in the element.
786 // We hard-code this check below.
787
788 const auto & base = elem->node_ref(local_node_id - 9);
789 const auto & apex = elem->node_ref(4);
790 const Real x = (type == PYRAMID18) ? 0.56646084 : 0.54985875;
791
792 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(base + x * (apex - base), tol));
793 continue;
794 }
795 else if (local_node_id > 13)
796 {
797 // Triangular face midpoint nodes
798 //
799 // Due to the nature of the pyramidal target element and the
800 // cubic nature of the test mesh, for a dilation weight o
801 // 0.5, smoothed triangular face midpoint nodes are
802 // smoothed a weighted average of the constituent
803 // vertices instead of an unweighted average.
804 // We hard-code this check below.
805 const auto & base1 = elem->node_ref(local_node_id - 14);
806 const auto & base2 = elem->node_ref((local_node_id - 13) % 4);
807 const auto & apex = elem->node_ref(4);
808
809 const auto node_approx =
810 (0.31401599 * base1 + 0.31401599 * base2 + 0.37196802 * apex);
811 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(node_approx, tol));
812 continue;
813 }
814 }
815
816 // Check for special case where some tet midpoint nodes are not
817 // smoothed to the actual midpoints.
818 else if (type_is_tet && !elem->is_vertex(local_node_id))
819 {
820 // We have a non-vertex node. Determine what "type" of
821 // midpoint node with respect to the mesh geometry.
822 // First, get the nodes that neighbor this node
823 std::vector<const Node *> neighbors;
825 mesh, node, nodes_to_elem_map, neighbors);
826
827 switch (neighbors.size())
828 {
829 case 2: {
830 // Midpoint node
831 // Determine what "type" of midpoint node
832
833 // Is one of the neighbors at the center of a sub-cube?
834 const auto is_0_cube_center =
835 pointIsCubeCenter(*neighbors[0], side_length);
836 const auto is_1_cube_center =
837 pointIsCubeCenter(*neighbors[1], side_length);
838 libmesh_assert(!(is_0_cube_center && is_1_cube_center));
839
840 if (is_0_cube_center || is_1_cube_center)
841 {
842 const auto & cube_center =
843 is_0_cube_center ? *neighbors[0] : *neighbors[1];
844 const auto & other =
845 is_0_cube_center ? *neighbors[1] : *neighbors[0];
846
847 // Since one neighbor is a sub-cube center, the
848 // other will either be a sub-cube face center or
849 // a sub-cube vertex. Determine which one.
850 if (pointIsCubeFaceCenter(other, side_length))
851 {
852 const Real x = (type == TET10) ? 0.42895041 : 0.41486385;
853 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(
854 other + x * (cube_center - other), tol));
855 }
856
857 else if (pointIsCubeVertex(other, side_length))
858 {
859 const Real x = (type == TET10) ? 0.55388920 : 0.58093516;
860 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(
861 other + x * (cube_center - other), tol));
862 }
863 }
864
865 else
866 {
867 // The remaining possibilities are
868 // cube-vertex-to-cube-vertex midpoints and
869 // cube-vertex-to-cube-face-center midpoints.
870 const auto is_0_cube_vertex =
871 pointIsCubeVertex(*neighbors[0], side_length);
872 const auto is_1_cube_vertex =
873 pointIsCubeVertex(*neighbors[1], side_length);
874 const auto is_0_cube_face_center =
875 pointIsCubeFaceCenter(*neighbors[0], side_length);
876 const auto is_1_cube_face_center =
877 pointIsCubeFaceCenter(*neighbors[1], side_length);
878
879 if (is_0_cube_vertex && is_1_cube_vertex)
880 // Due to symmetry, this type of midpoint is
881 // the geometric midpoint. Let the
882 // node_distortion_is function check it
883 CPPUNIT_ASSERT(node_distortion_is(node, false));
884
885 else
886 {
887 libmesh_error_msg_if(
888 (is_0_cube_center || is_0_cube_face_center) &&
889 (is_1_cube_center || is_1_cube_face_center),
890 "We should never get here!");
891 const auto & cube_vertex =
892 is_0_cube_vertex ? *neighbors[0] : *neighbors[1];
893 const auto & cube_face_center =
894 is_0_cube_face_center ? *neighbors[0] : *neighbors[1];
895 const Real x = (type == TET10) ? 0.61299101 : 0.65125580;
896 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(
897 cube_vertex + x * (cube_face_center - cube_vertex), tol));
898 }
899 }
900
901 continue;
902 break;
903 }
904
905 case 6: {
906 // Face node
907 // Only keep the face vertex nodes
908 neighbors.erase(std::remove_if(neighbors.begin(),
909 neighbors.end(),
910 [&elem](const Node * n) {
911 return !elem->is_vertex(
912 elem->local_node(n->id()));
913 }),
914 neighbors.end());
915
916 // There are three types of faces:
917 //
918 // 1) Isoscelese triangle with vertices at two
919 // sub-cube vertices and the sub-cube center,
920 // 2) Isosceles triangle with vertices at two
921 // sub-cube vertices and a sub-cube face center,
922 // 3) Scalene triangle with vertices at a sub-cube
923 // center, a sub-cube vertex, and a sub-cube face
924 // center.
925 //
926 // Determine which kind of face this node belongs to based on the vertex
927 // neighbors
928
929 unsigned int num_vertices_at_cube_center = 0;
930 unsigned int num_vertices_at_cube_vertices = 0;
931 unsigned int num_vertices_at_cube_face_centers = 0;
932 for (const auto * neighbor : neighbors)
933 {
934 if (pointIsCubeCenter(*neighbor, side_length))
935 num_vertices_at_cube_center += 1;
936 else if (pointIsCubeVertex(*neighbor, side_length))
937 num_vertices_at_cube_vertices += 1;
938 else if (pointIsCubeFaceCenter(*neighbor, side_length))
939 num_vertices_at_cube_face_centers += 1;
940 }
941
942 // We will express the face node at a linear
943 // combination of the face vertices
944 Node node_approx = Node(0, 0, 0);
945
946 // Isosceles triangular face
947 if (num_vertices_at_cube_vertices == 2)
948 {
949 // Isosceles triangular face (type 1)
950 if (num_vertices_at_cube_center)
951 for (const auto * neighbor : neighbors)
952 {
953 Real weight;
954 if (pointIsCubeVertex(*neighbor, side_length))
955 weight = 0.30600747;
956 else if (pointIsCubeCenter(*neighbor, side_length))
957 weight = 0.38798506;
958 else
959 libmesh_error_msg("We should never get here!");
960
961 node_approx += weight * (*neighbor);
962 }
963
964 // Isosceles triangular face (type 2)
965 else if (num_vertices_at_cube_face_centers)
966 for (const auto * neighbor : neighbors)
967 {
968 Real weight;
969 if (pointIsCubeVertex(*neighbor, side_length))
970 weight = 0.28078090;
971 else if (pointIsCubeFaceCenter(*neighbor, side_length))
972 weight = 0.43843820;
973 else
974 libmesh_error_msg("We should never get here!");
975
976 node_approx += weight * (*neighbor);
977 }
978
979 else
980 libmesh_error_msg("We should never get here!");
981 }
982
983 // Scalene triangular face (type 3)
984 else if (num_vertices_at_cube_center && num_vertices_at_cube_vertices &&
985 num_vertices_at_cube_face_centers)
986 for (const auto * neighbor : neighbors)
987 {
988 Real weight;
989 if (pointIsCubeCenter(*neighbor, side_length))
990 weight = 0.33102438;
991 else if (pointIsCubeVertex(*neighbor, side_length))
992 weight = 0.27147230;
993 else if (pointIsCubeFaceCenter(*neighbor, side_length))
994 weight = 0.39750332;
995 else
996 libmesh_error_msg("We should never get here!");
997
998 node_approx += weight * (*neighbor);
999 }
1000
1001 else
1002 libmesh_error_msg("We should never get here!");
1003
1004 CPPUNIT_ASSERT(node.absolute_fuzzy_equals(node_approx, tol));
1005
1006 continue;
1007 break;
1008 }
1009 default: {
1010 libmesh_error_msg(node << " has unexpected number of neighbors ("
1011 << neighbors.size() << ")");
1012 break;
1013 }
1014 } // switch (neighbors.size())
1015 } // if (type_is_tet)
1016
1017 CPPUNIT_ASSERT(node_distortion_is(node, false));
1018
1019 }
1020 }
1021 }
1022 }
1023
1024 // Function that distorts and smooths a mesh and checks that the result is
1025 // equal to the original "gold" mesh
1027 {
1028 // Make a copy of the gold mesh to distort and then smooth
1029 ReplicatedMesh mesh(gold_mesh);
1030
1031 // Move it around so we have something that needs smoothing
1032 DistortHyperCube dh(mesh.mesh_dimension());
1034
1035 // Function to assert the distortion is as expected
1036 auto mesh_distortion_is = [](const ReplicatedMesh & gold_mesh,
1037 const ReplicatedMesh & mesh,
1038 const bool distortion,
1039 Real distortion_tol = TOLERANCE) {
1040
1041 CPPUNIT_ASSERT(gold_mesh.n_nodes() == mesh.n_nodes());
1042 for (const auto node_id : make_range(gold_mesh.n_nodes()))
1043 {
1044 const auto & gold_node = gold_mesh.node_ref(node_id);
1045 const auto & node = mesh.node_ref(node_id);
1046 for (const auto d : make_range(gold_mesh.mesh_dimension()))
1047 {
1048 const bool d_distorted = std::abs(gold_node(d) - node(d)) > distortion_tol;
1049 if (d_distorted)
1050 // mesh is distorted from gold_mesh
1051 return distortion;
1052 }
1053 }
1054
1055 // mesh is not distorted from gold_mesh
1056 return !distortion;
1057 };
1058
1059 // Make sure the mesh has been distorted
1060 CPPUNIT_ASSERT(mesh_distortion_is(gold_mesh, mesh, true));
1061
1062 // Turn off subdomain boundary preservation because DistortHyperCube
1063 // does not preserve subdomain boundaries
1064 // Also set dilation coefficient to 0 because the reference volume of the
1065 // distorted mesh won't be equal to the reference volume of the smoothed
1066 // mesh and will smooth to a slightly different solution.
1067 VariationalMeshSmoother smoother(mesh, 0.0, false);
1068 smoother.smooth();
1069
1070 // Make sure the mesh has been smoothed to the gold mesh
1071 CPPUNIT_ASSERT(mesh_distortion_is(gold_mesh, mesh, false));
1072 }
1073
1075 {
1077 LaplaceMeshSmoother laplace(mesh, 8);
1078
1079 testLaplaceSmoother(mesh, laplace, QUAD4);
1080 }
1081
1083 {
1085 LaplaceMeshSmoother laplace(mesh, 8);
1086
1087 testLaplaceSmoother(mesh, laplace, TRI3);
1088 }
1089
1090 // A line whose (canonically oriented) direction has a negative dot product
1091 // with a plane normal is not parallel to the plane, and must intersect it.
1093 {
1094 LOG_UNIT_TEST;
1095
1096 const PlaneConstraint plane(Point(0, 0, 0), Point(0, 0, 1));
1097 const LineConstraint line(Point(0, 0, 1), Point(1, 0, -1).unit());
1098 CPPUNIT_ASSERT(!line.is_parallel(plane));
1099
1100 for (const auto & intersection :
1101 {intersect_constraints(plane, line), intersect_constraints(line, plane)})
1102 {
1103 CPPUNIT_ASSERT(std::holds_alternative<PointConstraint>(intersection));
1104 CPPUNIT_ASSERT(
1105 std::get<PointConstraint>(intersection).point().absolute_fuzzy_equals(Point(1, 0, 0)));
1106 }
1107 }
1108
1109 // Planes whose normals differ by a small angle are distinct and must
1110 // intersect in a line. The angle used here is between the sine of the angle
1111 // (compared against the tolerance by is_parallel()) and its square
1112 // (the determinant of the intersection system), which previously disagreed.
1114 {
1115 LOG_UNIT_TEST;
1116
1117 // Angle between the plane normals, chosen so that angle > tol > angle^2
1118 // for the default tolerance TOLERANCE^2
1119 const Real angle = TOLERANCE;
1120 const PlaneConstraint plane1(Point(0, 0, 0), Point(0, 0, 1));
1121 const PlaneConstraint plane2(Point(0, 0, 0), Point(std::sin(angle), 0, std::cos(angle)));
1122 CPPUNIT_ASSERT(!(plane1 == plane2));
1123 CPPUNIT_ASSERT(!plane1.is_parallel(plane2));
1124
1125 const auto intersection = intersect_constraints(plane1, plane2);
1126 CPPUNIT_ASSERT(std::holds_alternative<LineConstraint>(intersection));
1127 const auto & line = std::get<LineConstraint>(intersection);
1128 CPPUNIT_ASSERT(line.direction().absolute_fuzzy_equals(Point(0, 1, 0)));
1129 CPPUNIT_ASSERT(line.contains_point(PointConstraint(Point(0, 0, 0))));
1130 }
1131
1132#ifdef LIBMESH_ENABLE_VSMOOTHER
1134 {
1136 VariationalMeshSmoother variational(mesh);
1137
1138 testVariationalSmoother(mesh, variational, EDGE2);
1139 }
1140
1142 {
1144 VariationalMeshSmoother variational(mesh);
1145
1146 testVariationalSmoother(mesh, variational, EDGE3);
1147 }
1148
1150 {
1152 VariationalMeshSmoother variational(mesh);
1153
1154 testVariationalSmoother(mesh, variational, EDGE3, true);
1155 }
1156
1158 {
1160 VariationalMeshSmoother variational(mesh, 0.5, true, TOLERANCE * TOLERANCE, TOLERANCE * TOLERANCE, 100);
1161
1162 // High verbosity for a 3D mesh to increase code coverage, but silence the output
1163 // If we want to save the output for processing later, send it somewhere
1164 // besides nullptr
1165 std::streambuf * out_buf = libMesh::out.rdbuf(nullptr);
1166
1167 testVariationalSmoother(mesh, variational, QUAD4);
1168
1169 // Reset stdout
1170 libMesh::out.rdbuf(out_buf);
1171 }
1172
1174 {
1176 VariationalMeshSmoother variational(mesh);
1177
1178 testVariationalSmoother(mesh, variational, QUAD4, true);
1179 }
1180
1182 {
1184 VariationalMeshSmoother variational(mesh);
1185
1186 testVariationalSmoother(mesh, variational, QUAD8);
1187 }
1188
1190 {
1192 VariationalMeshSmoother variational(mesh);
1193
1194 testVariationalSmoother(mesh, variational, QUAD9);
1195 }
1196
1198 {
1200 VariationalMeshSmoother variational(mesh);
1201
1202 testVariationalSmoother(mesh, variational, QUAD9, true);
1203 }
1204
1206 {
1208 VariationalMeshSmoother variational(mesh);
1209
1210 testVariationalSmoother(mesh, variational, QUAD4, false, true, 0.65);
1211 }
1212
1214 {
1216 VariationalMeshSmoother variational(mesh);
1217
1218 testVariationalSmoother(mesh, variational, TRI3);
1219 }
1220
1222 {
1224 VariationalMeshSmoother variational(mesh);
1225
1226 testVariationalSmoother(mesh, variational, TRI6);
1227 }
1228
1230 {
1232 VariationalMeshSmoother variational(mesh);
1233
1234 testVariationalSmoother(mesh, variational, TRI3, true);
1235 }
1236
1238 {
1240 VariationalMeshSmoother variational(mesh);
1241
1242 testVariationalSmoother(mesh, variational, HEX8);
1243 }
1244
1246 {
1248 VariationalMeshSmoother variational(mesh);
1249
1250 testVariationalSmoother(mesh, variational, HEX20);
1251 }
1252
1254 {
1256 VariationalMeshSmoother variational(mesh);
1257
1258 testVariationalSmoother(mesh, variational, HEX27);
1259 }
1260
1262 {
1264 VariationalMeshSmoother variational(mesh);
1265
1266 testVariationalSmoother(mesh, variational, HEX27, true);
1267 }
1268
1270 {
1272 VariationalMeshSmoother variational(mesh);
1273
1274 testVariationalSmoother(mesh, variational, HEX27, false, true, 0.55);
1275 }
1276
1278 {
1280 VariationalMeshSmoother variational(mesh);
1281
1282 testVariationalSmoother(mesh, variational, PYRAMID5);
1283 }
1284
1286 {
1288 VariationalMeshSmoother variational(mesh);
1289
1290 testVariationalSmoother(mesh, variational, PYRAMID13);
1291 }
1292
1294 {
1296 VariationalMeshSmoother variational(mesh);
1297
1298 testVariationalSmoother(mesh, variational, PYRAMID14);
1299 }
1300
1302 {
1304 VariationalMeshSmoother variational(mesh);
1305
1306 testVariationalSmoother(mesh, variational, PYRAMID18);
1307 }
1308
1310 {
1312 VariationalMeshSmoother variational(mesh, 0.0);
1313
1314 testVariationalSmoother(mesh, variational, PYRAMID18, true);
1315 }
1316
1318 {
1320 VariationalMeshSmoother variational(mesh);
1321
1322 testVariationalSmoother(mesh, variational, PRISM6);
1323 }
1324
1326 {
1328 VariationalMeshSmoother variational(mesh);
1329
1330 testVariationalSmoother(mesh, variational, PRISM15);
1331 }
1332
1334 {
1336 VariationalMeshSmoother variational(
1337 mesh, 0.5, true, TOLERANCE * TOLERANCE, 10 * TOLERANCE * TOLERANCE);
1338
1339 testVariationalSmoother(mesh, variational, PRISM18);
1340 }
1341
1343 {
1345 VariationalMeshSmoother variational(
1346 mesh, 0.5, true, TOLERANCE * TOLERANCE, 10 * TOLERANCE * TOLERANCE);
1347
1348 testVariationalSmoother(mesh, variational, PRISM20);
1349 }
1350
1352 {
1354 VariationalMeshSmoother variational(
1355 mesh, 0.5, true, TOLERANCE * TOLERANCE, 10 * TOLERANCE * TOLERANCE);
1356
1357 testVariationalSmoother(mesh, variational, PRISM21);
1358 }
1359
1361 {
1363 mesh.allow_renumbering(false);
1364 VariationalMeshSmoother variational(mesh);
1365
1366 testVariationalSmoother(mesh, variational, PRISM21, true);
1367 }
1368
1370 {
1372 VariationalMeshSmoother variational(mesh, 0.5, true, TOLERANCE * TOLERANCE, TOLERANCE * TOLERANCE, 100);
1373
1374 // High verbosity for a 3D mesh to increase code coverage, but silence the output
1375 // If we want to save the output for processing later, send it somewhere
1376 // besides nullptr
1377 std::streambuf * out_buf = libMesh::out.rdbuf(nullptr);
1378
1379 testVariationalSmoother(mesh, variational, TET4);
1380
1381 // Reset stdout
1382 libMesh::out.rdbuf(out_buf);
1383 }
1384
1386 {
1388 VariationalMeshSmoother variational(mesh);
1389
1390 testVariationalSmoother(mesh, variational, TET10);
1391 }
1392
1394 {
1396 VariationalMeshSmoother variational(mesh);
1397
1398 testVariationalSmoother(mesh, variational, TET14);
1399 }
1400
1402 {
1404 VariationalMeshSmoother variational(mesh);
1405
1406 testVariationalSmoother(mesh, variational, TET14, true);
1407 }
1408
1410 {
1412 mesh.read("meshes/quad4_tri3_smoothed.xda.gz");
1413
1415 }
1416
1417 // Verify that a vertex-sampling quadrature rule prevents the "hourglass"
1418 // failure mode of the variational smoother.
1419 //
1420 // The fixture is a mixed QUAD4/TRI3 patch built around a hub node that is
1421 // pinned as a subdomain-boundary node. With the default (interior-only) Gauss
1422 // rule, minimizing the distortion-dilation metric folds one of the quads
1423 // toward a node---it collapses into a near-triangle (an hourglass)---while the
1424 // interior quadrature points still report positive Jacobians, so the mesh is
1425 // never flagged as tangled. Switching to a quadrature rule whose points
1426 // include the element vertices (here QNODAL) evaluates the metric at the
1427 // corners, so the fold is penalized and does not form.
1428 //
1429 // We detect the fold with Elem::has_invertible_map(), whose default
1430 // implementation evaluates the physical-to-reference map Jacobian at the
1431 // element nodes (nodal quadrature) and returns false if any nodal Jacobian is
1432 // non-positive. A quad folded toward one of its nodes has a non-positive
1433 // Jacobian at that corner, so its map is not invertible.
1435 {
1436 LOG_UNIT_TEST;
1437
1438 auto count_non_invertible = [](const MeshBase & m) {
1439 unsigned int n_bad = 0;
1440 for (const auto * elem : m.active_element_ptr_range())
1441 if (!elem->has_invertible_map())
1442 ++n_bad;
1443 return n_bad;
1444 };
1445
1446 // (1) Default Gauss rule: characterizes the bug. At least one element folds
1447 // into an hourglass and loses its invertible map. (If a future change to the
1448 // default smoother fixes this on its own, this assertion should be revisited.)
1449 {
1451 mesh.read("meshes/quad4_tri3_hourglass.xda.gz");
1452
1453 VariationalMeshSmoother smoother(mesh, 0.5, true);
1454 smoother.smooth();
1455
1456 CPPUNIT_ASSERT(count_non_invertible(mesh) > 0);
1457 }
1458
1459 // (2) Vertex-sampling rule (QNODAL): the metric now "sees" the corners, so
1460 // every element keeps an invertible map.
1461 {
1463 mesh.read("meshes/quad4_tri3_hourglass.xda.gz");
1464
1465 VariationalMeshSmoother smoother(mesh, 0.5, true);
1466 smoother.set_quadrature_type(QNODAL);
1467 smoother.smooth();
1468
1469 CPPUNIT_ASSERT_EQUAL(0u, count_non_invertible(mesh));
1470 }
1471 }
1472
1474 {
1476 mesh.read("meshes/hex8_prism6_smoothed.xda.gz");
1477
1479 }
1480
1481#endif // LIBMESH_ENABLE_VSMOOTHER
1482};
1483
unsigned int dim
CPPUNIT_TEST(testVariationalHex27MultipleSubdomains)
CPPUNIT_TEST(testVariationalPrism15)
CPPUNIT_TEST(testVariationalTri3)
CPPUNIT_TEST(testVariationalEdge3MultipleSubdomains)
CPPUNIT_TEST(testLaplaceQuad)
CPPUNIT_TEST(testVariationalTet4)
CPPUNIT_TEST(testVariationalPyramid18)
CPPUNIT_TEST(testVariationalHex27Tangled)
void testVariationalQuadratureHourglass()
void testLaplaceSmoother(ReplicatedMesh &mesh, MeshSmoother &smoother, ElemType type)
CPPUNIT_TEST(testVariationalQuadratureHourglass)
CPPUNIT_TEST(testVariationalHex20)
CPPUNIT_TEST(testVariationalMixed2D)
void testVariationalQuad9MultipleSubdomains()
LIBMESH_CPPUNIT_TEST_SUITE(MeshSmootherTest)
The goal of this test is to verify proper operation of the MeshSmoother subclasses.
void testVariationalPrism21MultipleSubdomains()
void testVariationalQuad4MultipleSubdomains()
void testConstraintNearlyParallelPlanes()
CPPUNIT_TEST(testVariationalPyramid18MultipleSubdomains)
CPPUNIT_TEST(testVariationalQuad4MultipleSubdomains)
void testVariationalSmootherRegression(const ReplicatedMesh &gold_mesh)
CPPUNIT_TEST(testVariationalPyramid5)
void testVariationalSmoother(Mesh &mesh, VariationalMeshSmoother &smoother, const ElemType type, const bool multiple_subdomains=false, const bool tangle_mesh=false, const Real tangle_damping_factor=1.0)
std::pair< unsigned int, unsigned int > numCenteredAndFacedDimensions(const Point &point, const Real &side_length, const Real &tol)
CPPUNIT_TEST(testVariationalTet14)
CPPUNIT_TEST(testVariationalPrism18)
CPPUNIT_TEST(testVariationalQuad8)
CPPUNIT_TEST(testVariationalHex27)
void testVariationalTet14MultipleSubdomains()
void testVariationalHex27MultipleSubdomains()
CPPUNIT_TEST(testVariationalPrism21)
bool pointIsCubeFaceCenter(const Point &point, const Real &side_length, const Real &tol=TOLERANCE)
void testVariationalPyramid18MultipleSubdomains()
CPPUNIT_TEST(testVariationalTri6)
CPPUNIT_TEST(testVariationalTet10)
CPPUNIT_TEST(testVariationalQuad4)
CPPUNIT_TEST(testVariationalTet14MultipleSubdomains)
CPPUNIT_TEST(testVariationalMixed3D)
CPPUNIT_TEST(testVariationalQuad9)
bool pointIsCubeCenter(const Point &point, const Real &side_length, const Real &tol=TOLERANCE)
CPPUNIT_TEST(testConstraintNearlyParallelPlanes)
CPPUNIT_TEST(testVariationalPrism20)
CPPUNIT_TEST(testVariationalPyramid14)
CPPUNIT_TEST(testVariationalPyramid13)
CPPUNIT_TEST(testVariationalPrism21MultipleSubdomains)
void testVariationalTri6MultipleSubdomains()
CPPUNIT_TEST(testVariationalHex8)
CPPUNIT_TEST(testVariationalTri6MultipleSubdomains)
CPPUNIT_TEST(testVariationalEdge2)
CPPUNIT_TEST(testVariationalEdge3)
CPPUNIT_TEST(testVariationalQuad4Tangled)
CPPUNIT_TEST(testVariationalQuad9MultipleSubdomains)
void testVariationalEdge3MultipleSubdomains()
bool pointIsCubeVertex(const Point &point, const Real &side_length, const Real &tol=TOLERANCE)
CPPUNIT_TEST(testConstraintLinePlane)
CPPUNIT_TEST(testLaplaceTri)
CPPUNIT_TEST(testVariationalPrism6)
void max(const T &r, T &o, Request &req) const
void min(const T &r, T &o, Request &req) const
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
streambufT * rdbuf() const
Get the associated stream buffer.
Defines a dense vector for use in Finite Element-type computations.
void resize(const unsigned int n)
Resize the vector.
virtual void zero() override final
Set every element in the vector to 0.
static constexpr dof_id_type invalid_id
An invalid id to distinguish an uninitialized DofObject.
Definition dof_object.h:473
static const Order type_to_default_order_map[INVALID_ELEM]
This array maps the integer representation of the ElemType enum to the default approximation order of...
Definition elem.h:1089
Base class for functors that can be evaluated at a point and (optionally) time.
virtual std::unique_ptr< FunctionBase< Output > > clone() const =0
virtual Output operator()(const Point &p, const Real time=0.)=0
This class defines the data structures necessary for Laplace smoothing.
Represents a line constraint defined by a base point and direction vector.
bool is_parallel(const LineConstraint &l) const
Query whether a line is parallel to this line.
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
unsigned int mesh_dimension() const
Definition mesh_base.C:515
void allow_renumbering(bool allow)
If false is passed in then this mesh will no longer be renumbered when being prepared for use.
Definition mesh_base.h:1367
virtual dof_id_type n_nodes() const =0
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false, bool skip_detect_interior_parents=false)=0
Interfaces for reading/writing a mesh to/from a file.
virtual const Node * query_node_ptr(const dof_id_type i) const =0
const std::set< Order > & elem_default_orders() const
Definition mesh_base.h:2809
This class provides the necessary interface for mesh smoothing.
virtual void smooth()=0
Function which actually performs the smoothing operations.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
A Node is like a Point, but with more information.
Definition node.h:55
processor_id_type processor_id() const
const Parallel::Communicator & comm() const
Represents a plane constraint defined by a point and normal vector.
bool is_parallel(const PlaneConstraint &p) const
Query whether a plane is parallel to this plane.
Represents a fixed point constraint.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:41
The ReplicatedMesh class is derived from the MeshBase class, and is used to store identical copies of...
virtual dof_id_type n_nodes() const override final
This is an implementation of Larisa Branets' smoothing algorithms.
virtual void smooth() override
Redefinition of the smooth function from the base class.
void set_quadrature_type(QuadratureType qt)
Set the quadrature rule type used by the underlying VariationalSmootherSystem to integrate the distor...
const MeshQualityInfo & get_mesh_info() const
Getter for the _system's _mesh_info attribute.
virtual void setup()
Setup method that creates equation systems, system, and constraints, to be called just prior to smoot...
static const Real b
Communicator * TestCommWorld
MeshBase & mesh
CPPUNIT_TEST_SUITE_REGISTRATION(MeshSmootherTest)
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_line(UnstructuredMesh &mesh, const unsigned int nx, const Real xmin=0., const Real xmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 1D meshes.
void build_cube(UnstructuredMesh &mesh, const unsigned int nx=0, const unsigned int ny=0, const unsigned int nz=0, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const Real zmin=0., const Real zmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
Builds a (elements) cube.
void redistribute(MeshBase &mesh, const FunctionBase< Real > &mapfunc)
Deterministically perturb the nodal locations.
void build_nodes_to_elem_map(const MeshBase &mesh, std::vector< std::vector< dof_id_type > > &nodes_to_elem_map)
After calling this function the input vector nodes_to_elem_map will contain the node to element conne...
Definition mesh_tools.C:520
void find_nodal_or_face_neighbors(const MeshBase &mesh, const Node &node, const std::unordered_map< dof_id_type, std::vector< const Elem * > > &nodes_to_elem_map, std::vector< const Node * > &neighbors)
Given a mesh and a node in the mesh, the vector will be filled with every node directly attached to t...
void sync_dofobject_data_by_id(const Communicator &comm, const Iterator &range_begin, const Iterator &range_end, SyncFunctor &sync)
Request data about a range of ghost dofobjects uniquely identified by their id.
const Elem & get(const ElemType type_in)
T pow(const T &x)
Definition utility.h:296
std::string enum_to_string(const T e)
The libMesh namespace provides an interface to certain functionality in the library.
bool relative_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
Function to check whether two variables are equal within a relative tolerance.
bool absolute_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
ElemType
Defines an enum for geometric element types.
libmesh_assert(ctx)
OStreamProxy out
static constexpr Real TOLERANCE
uint8_t dof_id_type
Definition id_types.h:67
ConstraintVariant intersect_constraints(const ConstraintVariant &a, const ConstraintVariant &b)
Dispatch intersection between two constraint variants.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t processor_id_type
Definition id_types.h:104
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