libMesh
Loading...
Searching...
No Matches
fe_collocation_test.C
Go to the documentation of this file.
1// A nodal basis whose interpolation points are the points of a quadrature rule is
2// collocated with that rule. Each shape function is one at its own point and zero at the
3// others. The matrix of shape function values at the quadrature points is thus a
4// permutation matrix. The mass matrix assembled with the rule is then diagonal. This holds for any mapping
5// and element shape, because the Jacobian only scales the diagonal entries.
6//
7// The Gauss-Lobatto families are collocated with their default Gauss-Lobatto rule.
8// L2_LAGRANGE_GLL numbers its shape functions in the order of the rule points, so its
9// permutation is the identity. LAGRANGE_GLL numbers them by the entity that owns them, so its
10// permutation is general. Both families read their points from QGaussLobatto, so the
11// property holds exactly. These cases compare against 1 and 0 without a tolerance.
12//
13// LAGRANGE and L2_LAGRANGE are collocated with QNODAL. The QNODAL points are the element
14// nodes, in an order that can differ from the node numbering. The shape functions of these
15// families are evaluated in floating point at the nodes, so these cases use a tolerance.
16
17#include <libmesh/elem.h>
18#include <libmesh/enum_fe_family.h>
19#include <libmesh/enum_order.h>
20#include <libmesh/fe_base.h>
21#include <libmesh/fe_type.h>
22#include <libmesh/int_range.h>
23#include <libmesh/libmesh_common.h>
24#include <libmesh/mesh.h>
25#include <libmesh/mesh_generation.h>
26#include <libmesh/node.h>
27#include <libmesh/parallel_implementation.h>
28#include <libmesh/quadrature.h>
29#include <libmesh/quadrature_nodal.h>
30
31#include "test_comm.h"
32#include "libmesh_cppunit.h"
33
34#include <cmath>
35#include <memory>
36#include <vector>
37
38using namespace libMesh;
39
40template <FEFamily family, ElemType elem_type, Order order>
41class FECollocationTest : public CppUnit::TestCase
42{
43protected:
44 std::unique_ptr<Mesh> _mesh;
45 std::string libmesh_suite_name;
46
47public:
48 void setUp()
49 {
50 _mesh = std::make_unique<Mesh>(*TestCommWorld);
51
52 const unsigned int dim = Elem::type_to_dim_map[elem_type];
53
54 if (dim == 1)
55 MeshTools::Generation::build_line(*_mesh, 2, 0., 1., elem_type);
56 else if (dim == 2)
57 MeshTools::Generation::build_square(*_mesh, 2, 2, 0., 1., 0., 1., elem_type);
58 else
59 MeshTools::Generation::build_cube(*_mesh, 2, 2, 2, 0., 1., 0., 1., 0., 1., elem_type);
60
61 // Shear the mesh so that the mapping to each element has off-diagonal Jacobian terms
62 for (auto * node : _mesh->node_ptr_range())
63 {
64 Node & p = *node;
65 const Real y = (LIBMESH_DIM > 1) ? p(1) : 0.;
66 const Real z = (LIBMESH_DIM > 2) ? p(2) : 0.;
67
68 p(0) += 0.3 * y + 0.17 * z;
69 if (LIBMESH_DIM > 1)
70 p(1) += 0.23 * z;
71 }
72 }
73
74 void tearDown() { _mesh.reset(); }
75
77 std::unique_ptr<QBase> collocated_rule(const FEType & fe_type, const unsigned int dim)
78 {
79 if (gll)
80 return fe_type.default_quadrature_rule(dim);
81
82 return std::make_unique<QNodal>(dim);
83 }
84
86 static constexpr bool gll = (family == L2_LAGRANGE_GLL || family == LAGRANGE_GLL);
87
89 static constexpr Real tol = gll ? Real(0) : TOLERANCE*TOLERANCE;
90
96 std::vector<unsigned int>
97 collocation_points(const std::vector<std::vector<Real>> & phi)
98 {
99 std::vector<unsigned int> shape_qp(phi.size(), invalid_uint);
100
101 // Whether some shape function is already one at each quadrature point. A second shape
102 // function that is one at the same point would leave another point without one.
103 std::vector<bool> qp_claimed(phi.size(), false);
104
105 for (const auto i : index_range(phi))
106 {
107 // The collocated rule has one point per degree of freedom
108 CPPUNIT_ASSERT_EQUAL(phi.size(), phi[i].size());
109
110 for (const auto q : index_range(phi[i]))
111 if (std::abs(phi[i][q] - 1) <= tol)
112 {
113 CPPUNIT_ASSERT_EQUAL(invalid_uint, shape_qp[i]);
114 shape_qp[i] = q;
115 }
116 else
117 LIBMESH_ASSERT_FP_EQUAL(Real(0), phi[i][q], tol);
118
119 CPPUNIT_ASSERT(shape_qp[i] != invalid_uint);
120 CPPUNIT_ASSERT(!qp_claimed[shape_qp[i]]);
121 qp_claimed[shape_qp[i]] = true;
122 }
123
124 return shape_qp;
125 }
126
128 {
129 LOG_UNIT_TEST;
130
131 const FEType fe_type(order, family);
132 const unsigned int dim = Elem::type_to_dim_map[elem_type];
133
134 std::unique_ptr<FEBase> fe = FEBase::build(dim, fe_type);
135 std::unique_ptr<QBase> qrule = collocated_rule(fe_type, dim);
136 fe->attach_quadrature_rule(qrule.get());
137
138 // Request phi before reinit so that reinit computes it
139 const std::vector<std::vector<Real>> & phi = fe->get_phi();
140
141 unsigned int n_elem = 0;
142
143 for (const auto * elem : _mesh->active_local_element_ptr_range())
144 {
145 fe->reinit(elem);
146
147 const std::vector<unsigned int> shape_qp = collocation_points(phi);
148
149 if (family == L2_LAGRANGE_GLL)
150 for (const auto i : index_range(shape_qp))
151 CPPUNIT_ASSERT_EQUAL(cast_int<unsigned int>(i), shape_qp[i]);
152
153 ++n_elem;
154 }
155
156 _mesh->comm().sum(n_elem);
157 CPPUNIT_ASSERT(n_elem > 0);
158 }
159
161 {
162 LOG_UNIT_TEST;
163
164 const FEType fe_type(order, family);
165 const unsigned int dim = Elem::type_to_dim_map[elem_type];
166
167 std::unique_ptr<FEBase> fe = FEBase::build(dim, fe_type);
168 std::unique_ptr<QBase> qrule = collocated_rule(fe_type, dim);
169 fe->attach_quadrature_rule(qrule.get());
170
171 const std::vector<std::vector<Real>> & phi = fe->get_phi();
172 const std::vector<Real> & JxW = fe->get_JxW();
173
174 for (const auto * elem : _mesh->active_local_element_ptr_range())
175 {
176 fe->reinit(elem);
177
178 const std::size_t n_dofs = phi.size();
179 const std::vector<unsigned int> shape_qp = collocation_points(phi);
180
181 for (const auto i : make_range(n_dofs))
182 for (const auto j : make_range(n_dofs))
183 {
184 Real m = 0.;
185 for (const auto q : index_range(JxW))
186 m += JxW[q] * phi[i][q] * phi[j][q];
187
188 if (i == j)
189 {
190 // Each diagonal entry equals JxW at its own point, which is positive on
191 // a valid element with positive quadrature weights
192 CPPUNIT_ASSERT(m > 0.);
193 LIBMESH_ASSERT_FP_EQUAL(JxW[shape_qp[i]], m, TOLERANCE*TOLERANCE);
194 }
195 else
196 LIBMESH_ASSERT_FP_EQUAL(Real(0), m, tol);
197 }
198 }
199 }
200};
201
202#define INSTANTIATE_FECOLLOCATIONTEST(family, elemtype, order) \
203 class FECollocationTest_##family##_##order##_##elemtype : \
204 public FECollocationTest<family, elemtype, order> { \
205 public: \
206 FECollocationTest_##family##_##order##_##elemtype() : \
207 FECollocationTest<family, elemtype, order>() { \
208 if (unitlog->summarized_logs_enabled()) \
209 this->libmesh_suite_name = "FECollocationTest"; \
210 else \
211 this->libmesh_suite_name = \
212 "FECollocationTest_" #family "_" #order "_" #elemtype; \
213 } \
214 CPPUNIT_TEST_SUITE( FECollocationTest_##family##_##order##_##elemtype ); \
215 CPPUNIT_TEST( testShapesArePermutation ); \
216 CPPUNIT_TEST( testMassMatrixIsDiagonal ); \
217 CPPUNIT_TEST_SUITE_END(); \
218 }; \
219 \
220 CPPUNIT_TEST_SUITE_REGISTRATION( FECollocationTest_##family##_##order##_##elemtype )
221
232
233#if LIBMESH_DIM > 1
250#endif
251
252#if LIBMESH_DIM > 2
271#endif
unsigned int dim
std::vector< unsigned int > collocation_points(const std::vector< std::vector< Real > > &phi)
std::unique_ptr< Mesh > _mesh
static constexpr bool gll
Whether the family is one of the Gauss-Lobatto nodal families.
static constexpr Real tol
The tolerance on shape function values at the collocation points.
std::string libmesh_suite_name
std::unique_ptr< QBase > collocated_rule(const FEType &fe_type, const unsigned int dim)
The quadrature rule collocated with the basis.
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
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition fe_type.h:197
std::unique_ptr< QBase > default_quadrature_rule(const unsigned int dim, const int extraorder=0) const
Definition fe_type.C:35
A Node is like a Point, but with more information.
Definition node.h:55
Communicator * TestCommWorld
INSTANTIATE_FECOLLOCATIONTEST(L2_LAGRANGE_GLL, EDGE2, FIRST)
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.
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
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition libmesh.h:303
static constexpr Real TOLERANCE
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