diff --git a/include/mesh/mesh_smoother_vsmoother.h b/include/mesh/mesh_smoother_vsmoother.h index 576265c7d87..0ef12dd9eb6 100644 --- a/include/mesh/mesh_smoother_vsmoother.h +++ b/include/mesh/mesh_smoother_vsmoother.h @@ -129,6 +129,19 @@ class VariationalMeshSmoother : public MeshSmoother */ const MeshQualityInfo & get_mesh_info() const; + /** + * Set the quadrature rule type used by the underlying + * VariationalSmootherSystem to integrate the distortion-dilation metric. + * The default (\p QGAUSS) samples the element interior only, which can miss + * degeneracies localized at element corners (e.g. an element collapsing + * toward one of its nodes). A vertex-sampling rule such as \p QTRAP, + * \p QSIMPSON, \p QNODAL, or \p QGAUSS_LOBATTO evaluates the metric at the + * element nodes so a folding corner is penalized (and flagged as tangled) as + * it should be. Must be called before smooth()/setup(). See + * VariationalSmootherSystem::set_quadrature_type() for details and caveats. + */ + void set_quadrature_type(QuadratureType qt) { _quadrature_type = qt; } + private: /** @@ -199,6 +212,12 @@ class VariationalMeshSmoother : public MeshSmoother * Solver absolute residual tolerance */ Real _absolute_residual_tolerance; + + /** + * Quadrature rule type used to integrate the distortion-dilation metric. + * See set_quadrature_type(). + */ + QuadratureType _quadrature_type = QGAUSS; }; } // namespace libMesh diff --git a/include/systems/fem_context.h b/include/systems/fem_context.h index c19a5699e4e..3c1cc74533a 100644 --- a/include/systems/fem_context.h +++ b/include/systems/fem_context.h @@ -23,6 +23,7 @@ // Local Includes #include "libmesh/diff_context.h" #include "libmesh/id_types.h" +#include "libmesh/enum_quadrature_type.h" #include "libmesh/fe_type.h" #include "libmesh/fe_base.h" #include "libmesh/vector_value.h" @@ -107,6 +108,23 @@ class FEMContext : public DiffContext */ void use_unweighted_quadrature_rules(int extra_quadrature_order=0); + /** + * Use a specified quadrature rule type \p qt for every element, + * side, and edge dimension, at the default quadrature order for the + * hardest FE type plus \p extra_quadrature_order. + * + * This is useful when the default Gauss rules (whose points lie in + * the element interior) are inadequate. For example, rules whose + * points include the element vertices (\p QTRAP, \p QSIMPSON, \p + * QNODAL, \p QGAUSS_LOBATTO) can detect integrand behavior that is + * localized at element corners --- a feature interior Gauss points + * can miss entirely. Note that not every quadrature type is defined + * for every element type (e.g. \p QGAUSS_LOBATTO is only available for + * tensor-product elements); it is the caller's responsibility to pick + * a type compatible with the mesh. + */ + void use_quadrature_rules(QuadratureType qt, int extra_quadrature_order=0); + /** * Reports if the boundary id is found on the current side */ diff --git a/include/systems/variational_smoother_system.h b/include/systems/variational_smoother_system.h index f9e698ad550..abdc3253b00 100644 --- a/include/systems/variational_smoother_system.h +++ b/include/systems/variational_smoother_system.h @@ -20,6 +20,7 @@ // libMesh includes #include "libmesh/enum_fe_family.h" +#include "libmesh/enum_quadrature_type.h" #include "libmesh/fem_function_base.h" #include "libmesh/fem_system.h" #include "libmesh/libmesh_common.h" @@ -94,7 +95,8 @@ class VariationalSmootherSystem : public libMesh::FEMSystem _epsilon_squared_assembly(0.), _ref_vol(0.), _dilation_weight(0.5), - _untangling_solve(false) + _untangling_solve(false), + _quadrature_type(QGAUSS) {} // Default destructor @@ -110,6 +112,27 @@ class VariationalSmootherSystem : public libMesh::FEMSystem Real & get_dilation_weight() { return _dilation_weight; } + /** + * Set the quadrature rule type used to integrate the distortion-dilation + * metric over each element. The default (\p QGAUSS) samples the element + * interior only, which can miss degeneracies localized at element corners + * (e.g. an element collapsing toward one of its nodes). A vertex-sampling + * rule such as \p QTRAP, \p QSIMPSON, \p QNODAL, or \p QGAUSS_LOBATTO + * evaluates the metric at the element nodes, so a folding corner drives the + * metric up (and is flagged as tangled) as it should be. The number of + * points is set from the default order for the mesh's FE type plus the + * System's \p extra_quadrature_order. Not every quadrature type is defined + * for every element type (e.g. \p QGAUSS_LOBATTO is only available for + * tensor-product elements), so choose a type compatible with the mesh. + */ + void set_quadrature_type(QuadratureType qt) { _quadrature_type = qt; } + + /** + * Get the quadrature rule type used to integrate the distortion-dilation + * metric. See set_quadrature_type(). + */ + QuadratureType get_quadrature_type() const { return _quadrature_type; } + /** * Solves the system to smooth the mesh. If the mesh is initially tangled, * a solve is first performed to untangle the mesh, followed by a solve to @@ -241,6 +264,12 @@ class VariationalSmootherSystem : public libMesh::FEMSystem * Flag to indicate if the current solve is to untangle or smooth */ bool _untangling_solve; + + /** + * The quadrature rule type used to integrate the distortion-dilation metric. + * See set_quadrature_type(). + */ + QuadratureType _quadrature_type; }; } // namespace libMesh diff --git a/src/mesh/mesh_smoother_vsmoother.C b/src/mesh/mesh_smoother_vsmoother.C index 854b8aa4fd2..6c5bf29d050 100644 --- a/src/mesh/mesh_smoother_vsmoother.C +++ b/src/mesh/mesh_smoother_vsmoother.C @@ -111,6 +111,13 @@ void VariationalMeshSmoother::setup() // Using higher quadrature orders has not had a significant effect on observed solutions. //system()->extra_quadrature_order = 0; + // The default Gauss rule samples the element interior only, so it can miss + // degeneracies localized at element corners (e.g. an element collapsing + // toward one of its nodes). Selecting a vertex-sampling rule (QTRAP, + // QSIMPSON, QNODAL, QGAUSS_LOBATTO) evaluates the metric at the element + // nodes instead. Defaults to QGAUSS. + system()->set_quadrature_type(_quadrature_type); + // Uncomment these to debug //system()->print_element_solutions=true; //system()->print_element_residuals=true; diff --git a/src/systems/fem_context.C b/src/systems/fem_context.C index 79ddf6d4e4a..675cad5d6bc 100644 --- a/src/systems/fem_context.C +++ b/src/systems/fem_context.C @@ -195,6 +195,32 @@ void FEMContext::use_unweighted_quadrature_rules(int extra_quadrature_order) } +void FEMContext::use_quadrature_rules(QuadratureType qt, int extra_quadrature_order) +{ + _extra_quadrature_order = extra_quadrature_order; + + const FEType hardest_fe_type = this->find_hardest_fe_type(); + + // Match the accuracy of the default rule (which over-integrates a mass + // matrix), then add any requested extra order on top. + const Order order = static_cast(hardest_fe_type.default_quadrature_order() + + extra_quadrature_order); + + for (const auto & dim : _elem_dims) + { + // Create the requested quadrature rule for the element and its + // lower-dimensional sides/edges + _element_qrule[dim] = QBase::build(qt, dim, order); + if (dim) + _side_qrule[dim] = QBase::build(qt, cast_int(dim - 1), order); + if (dim == 3) + _edge_qrule = QBase::build(qt, 1, order); + } + + this->attach_quadrature_rules(); +} + + void FEMContext::init_internal_data(const System & sys) { // Reserve space for the FEAbstract and QBase objects for each diff --git a/src/systems/variational_smoother_system.C b/src/systems/variational_smoother_system.C index 03f1a3ecd40..00cd3e63f55 100644 --- a/src/systems/variational_smoother_system.C +++ b/src/systems/variational_smoother_system.C @@ -310,6 +310,15 @@ void VariationalSmootherSystem::init_context(DiffContext & context) } FEMSystem::init_context(context); + + // Override the default (interior-only) Gauss rule if a different quadrature + // type has been requested. A vertex-sampling rule (e.g. QTRAP, QSIMPSON, + // QNODAL, QGAUSS_LOBATTO) lets the metric "see" degeneracies localized at + // element corners that interior Gauss points miss. Do this after + // FEMSystem::init_context() so it is not overwritten. The System's + // extra_quadrature_order is respected for the point count. + if (_quadrature_type != QGAUSS) + c.use_quadrature_rules(_quadrature_type, this->extra_quadrature_order); } diff --git a/tests/Makefile.am b/tests/Makefile.am index 64273621205..7b30acbbafe 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -229,6 +229,7 @@ data = matrices/geom_1_extraction_op.h5 \ meshes/Cluster_34.stl \ meshes/engraving.stl \ meshes/quad4_tri3_smoothed.xda.gz \ + meshes/quad4_tri3_hourglass.xda.gz \ meshes/hex8_prism6_smoothed.xda.gz \ solutions/lagrange_vec_solution_mesh.xda \ solutions/lagrange_vec_solution.xda \ diff --git a/tests/mesh/mesh_smoother_test.C b/tests/mesh/mesh_smoother_test.C index 7f499c97617..18f2e3165d1 100644 --- a/tests/mesh/mesh_smoother_test.C +++ b/tests/mesh/mesh_smoother_test.C @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -273,6 +274,7 @@ public: #if defined(LIBMESH_HAVE_GZSTREAM) CPPUNIT_TEST(testVariationalMixed2D); CPPUNIT_TEST(testVariationalMixed3D); + CPPUNIT_TEST(testVariationalQuadratureHourglass); #endif #endif // LIBMESH_ENABLE_VSMOOTHER @@ -1367,6 +1369,62 @@ public: testVariationalSmootherRegression(mesh); } + // Verify that a vertex-sampling quadrature rule prevents the "hourglass" + // failure mode of the variational smoother. + // + // The fixture is a mixed QUAD4/TRI3 patch built around a hub node that is + // pinned as a subdomain-boundary node. With the default (interior-only) Gauss + // rule, minimizing the distortion-dilation metric folds one of the quads + // toward a node---it collapses into a near-triangle (an hourglass)---while the + // interior quadrature points still report positive Jacobians, so the mesh is + // never flagged as tangled. Switching to a quadrature rule whose points + // include the element vertices (here QNODAL) evaluates the metric at the + // corners, so the fold is penalized and does not form. + // + // We detect the fold with Elem::has_invertible_map(), whose default + // implementation evaluates the physical-to-reference map Jacobian at the + // element nodes (nodal quadrature) and returns false if any nodal Jacobian is + // non-positive. A quad folded toward one of its nodes has a non-positive + // Jacobian at that corner, so its map is not invertible. + void testVariationalQuadratureHourglass() + { + LOG_UNIT_TEST; + + auto count_non_invertible = [](const MeshBase & m) { + unsigned int n_bad = 0; + for (const auto * elem : m.active_element_ptr_range()) + if (!elem->has_invertible_map()) + ++n_bad; + return n_bad; + }; + + // (1) Default Gauss rule: characterizes the bug. At least one element folds + // into an hourglass and loses its invertible map. (If a future change to the + // default smoother fixes this on its own, this assertion should be revisited.) + { + ReplicatedMesh mesh(*TestCommWorld); + mesh.read("meshes/quad4_tri3_hourglass.xda.gz"); + + VariationalMeshSmoother smoother(mesh, 0.5, true); + smoother.smooth(); + + CPPUNIT_ASSERT(count_non_invertible(mesh) > 0); + } + + // (2) Vertex-sampling rule (QNODAL): the metric now "sees" the corners, so + // every element keeps an invertible map. + { + ReplicatedMesh mesh(*TestCommWorld); + mesh.read("meshes/quad4_tri3_hourglass.xda.gz"); + + VariationalMeshSmoother smoother(mesh, 0.5, true); + smoother.set_quadrature_type(QNODAL); + smoother.smooth(); + + CPPUNIT_ASSERT_EQUAL(0u, count_non_invertible(mesh)); + } + } + void testVariationalMixed3D() { ReplicatedMesh mesh(*TestCommWorld); diff --git a/tests/meshes/quad4_tri3_hourglass.xda.gz b/tests/meshes/quad4_tri3_hourglass.xda.gz new file mode 100644 index 00000000000..4027c89b285 Binary files /dev/null and b/tests/meshes/quad4_tri3_hourglass.xda.gz differ