libMesh
Loading...
Searching...
No Matches
fe_amr_constraint_test.C
Go to the documentation of this file.
1// On an adaptively refined mesh, a conforming basis constrains the degrees of freedom on a
2// hanging node to the coarse side that contains the node. A conforming mesh does not exercise
3// these constraints. This test checks the property that the constraints must preserve. The
4// constrained space contains the polynomials of its order, so the projection of such a
5// polynomial reproduces it exactly on both sides of the interface.
6//
7// A wrong constraint moves the hanging degrees of freedom away from the values that
8// interpolate the polynomial. The solution then differs from the polynomial.
9
10#include <libmesh/dof_map.h>
11#include <libmesh/elem.h>
12#include <libmesh/enum_fe_family.h>
13#include <libmesh/enum_order.h>
14#include <libmesh/equation_systems.h>
15#include <libmesh/fe_type.h>
16#include <libmesh/int_range.h>
17#include <libmesh/mesh.h>
18#include <libmesh/mesh_generation.h>
19#include <libmesh/mesh_refinement.h>
20#include <libmesh/system.h>
21#include <libmesh/utility.h>
22
23#include "test_comm.h"
24#include "libmesh_cppunit.h"
25
26#include <cmath>
27#include <memory>
28#include <vector>
29
30using namespace libMesh;
31
32namespace {
33
38template <unsigned int D>
39Number amr_test_poly(const Point & p,
40 const Parameters &,
41 const std::string &,
42 const std::string &)
43{
44 return Utility::pow<D>(1. + p(0) + 2.*p(1) + 3.*p(2));
45}
46
47}
48
49#ifdef LIBMESH_ENABLE_AMR
50
51template <ElemType elem_type, Order order, FEFamily family>
52class FEAMRConstraintTest : public CppUnit::TestCase
53{
54protected:
55 std::unique_ptr<Mesh> _mesh;
56 std::string libmesh_suite_name;
57
58public:
59 void setUp()
60 {
61 _mesh = std::make_unique<Mesh>(*TestCommWorld);
62
63 const unsigned int dim = Elem::type_to_dim_map[elem_type];
64
65 // For a second-order type, build the first-order mesh and then convert it. Families above
66 // order one need the mid-edge and face nodes of the second-order type.
67 ElemType base = elem_type;
68 if (elem_type == QUAD9) base = QUAD4;
69 if (elem_type == HEX27) base = HEX8;
70 if (elem_type == EDGE3) base = EDGE2;
71
72 if (dim == 1)
74 else if (dim == 2)
75 MeshTools::Generation::build_square(*_mesh, 2, 2, 0., 1., 0., 1., base);
76 else
77 MeshTools::Generation::build_cube(*_mesh, 2, 2, 2, 0., 1., 0., 1., 0., 1., base);
78
79 if (base != elem_type)
80 _mesh->all_second_order();
81
82 // Refine part of the mesh. This puts hanging nodes on the interface between the refined
83 // elements and their unrefined neighbors.
84 MeshRefinement refine(*_mesh);
85
86 for (auto * elem : _mesh->active_element_ptr_range())
87 if (elem->id() % 4 == 0)
88 elem->set_refinement_flag(Elem::REFINE);
89
91 }
92
93 void tearDown() { _mesh.reset(); }
94
96 {
97 LOG_UNIT_TEST;
98
100 System & sys = es.add_system<System>("amr");
101 sys.add_variable("u", FEType(order, family));
102 es.init();
103
104 sys.project_solution(amr_test_poly<static_cast<unsigned int>(order)>,
105 nullptr, es.parameters);
106
107 // The projection enforced the constraints. Check that the solution still equals the
108 // polynomial at points in the refined and the unrefined parts of the mesh.
109 Parameters dummy;
110 unsigned int n_checked = 0;
111
112 for (const auto a : make_range(1, 8))
113 for (const auto b : make_range(1, 8))
114 {
115 const unsigned int dim = Elem::type_to_dim_map[elem_type];
116 const Point q(a/8., (dim > 1) ? b/8. : 0., (dim > 2) ? (a + b)/16. : 0.);
117
118 const Number expected =
119 amr_test_poly<static_cast<unsigned int>(order)>(q, dummy, "", "");
120 const Number actual = sys.point_value(0, q);
121
122 LIBMESH_ASSERT_NUMBERS_EQUAL(expected, actual, TOLERANCE*std::sqrt(TOLERANCE));
123 ++n_checked;
124 }
125
126 CPPUNIT_ASSERT(n_checked > 0);
127 }
128};
129
130#define INSTANTIATE_FEAMRCONSTRAINTTEST(elemtype, order, family) \
131 class FEAMRConstraintTest_##family##_##order##_##elemtype : \
132 public FEAMRConstraintTest<elemtype, order, family> { \
133 public: \
134 FEAMRConstraintTest_##family##_##order##_##elemtype() : \
135 FEAMRConstraintTest<elemtype, order, family>() { \
136 if (unitlog->summarized_logs_enabled()) \
137 this->libmesh_suite_name = "FEAMRConstraintTest"; \
138 else \
139 this->libmesh_suite_name = \
140 "FEAMRConstraintTest_" #family "_" #order "_" #elemtype; \
141 } \
142 CPPUNIT_TEST_SUITE( FEAMRConstraintTest_##family##_##order##_##elemtype ); \
143 CPPUNIT_TEST( testConstrainedSpaceHoldsPolynomials ); \
144 CPPUNIT_TEST_SUITE_END(); \
145 }; \
146 \
147 CPPUNIT_TEST_SUITE_REGISTRATION( FEAMRConstraintTest_##family##_##order##_##elemtype )
148
149// LAGRANGE_GLL is the family under test. LAGRANGE is the control, because its constraints
150// are well established. A LAGRANGE failure thus indicates a defect in the test. HIERARCHIC is
151// the other family that adds a separate degree of freedom at a hanging node.
159
160#if LIBMESH_DIM > 2
168#endif
169
170#endif // LIBMESH_ENABLE_AMR
unsigned int dim
std::unique_ptr< Mesh > _mesh
static const unsigned int type_to_dim_map[INVALID_ELEM]
This array maps the integer representation of the ElemType enum to the geometric dimension of the ele...
Definition elem.h:649
This is the EquationSystems class.
Parameters parameters
Data structure holding arbitrary parameters.
virtual void init()
Initialize all the systems.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition fe_type.h:197
Implements (adaptive) mesh refinement algorithms for a MeshBase.
bool refine_and_coarsen_elements()
Refines and coarsens user-requested elements.
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition parameters.h:75
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:41
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr, std::optional< ConstElemRange > active_local_range=std::nullopt, std::optional< std::vector< unsigned int > > variable_numbers=std::nullopt) const
Projects arbitrary functions onto the current solution.
Number point_value(unsigned int var, const Point &p, const bool insist_on_success=true, const NumericVector< Number > *sol=nullptr) const
Definition system.C:2274
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition system.C:1400
static const Real b
Communicator * TestCommWorld
INSTANTIATE_FEAMRCONSTRAINTTEST(QUAD9, SECOND, LAGRANGE)
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.
The libMesh namespace provides an interface to certain functionality in the library.
ElemType
Defines an enum for geometric element types.
static constexpr Real TOLERANCE
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