libMesh
Loading...
Searching...
No Matches
dual_shape_verification_test.C
Go to the documentation of this file.
1// libmesh includes
2#include "libmesh/libmesh.h"
3#include "libmesh/dense_matrix.h"
4#include "libmesh/edge_edge2.h"
5#include "libmesh/fe.h"
6#include "libmesh/quadrature_gauss.h"
7#include "libmesh/mesh_generation.h"
8#include "libmesh/mesh.h"
9
10// unit test includes
11#include "test_comm.h"
12
13#include "libmesh_cppunit.h"
14
15using namespace libMesh;
16
20class DualShapeTest : public CppUnit::TestCase
21{
22public:
30
31private:
32 std::unique_ptr<Mesh> _mesh;
33 std::unique_ptr<QGauss> _qrule;
34 std::unique_ptr<FEBase> _fe;
36
37public:
38
40 {
41 LOG_UNIT_TEST;
42 if (!_elem)
43 return;
44
45 _fe->reinit(_elem);
46
47 const DenseMatrix<Real> & dual_coeff = _fe->get_dual_coeff();
48
49 CPPUNIT_ASSERT_EQUAL(dual_coeff.m(), unsigned(2));
50 CPPUNIT_ASSERT_EQUAL(dual_coeff.n(), unsigned(2));
51
52 // TOLERANCE*TOLERANCE works with double but not float128
53 Real my_tol = TOLERANCE*std::sqrt(TOLERANCE);
54
55 LIBMESH_ASSERT_FP_EQUAL(2, dual_coeff(0,0), my_tol);
56 LIBMESH_ASSERT_FP_EQUAL(-1, dual_coeff(0,1), my_tol);
57 LIBMESH_ASSERT_FP_EQUAL(-1, dual_coeff(1,0), my_tol);
58 LIBMESH_ASSERT_FP_EQUAL(2, dual_coeff(1,1), my_tol);
59
60 const auto & dual_phi = _fe->get_dual_phi();
61
62 CPPUNIT_ASSERT_EQUAL(std::size_t(2), dual_phi.size());
63
64 const auto & qpoints = _qrule->get_points();
65
66 CPPUNIT_ASSERT_EQUAL(qpoints.size(), dual_phi[0].size());
67
68 for (auto qp : index_range(dual_phi[0]))
69 LIBMESH_ASSERT_FP_EQUAL(1./2. * (1. - 3.*qpoints[qp](0)), dual_phi[0][qp],
70 my_tol);
71
72 CPPUNIT_ASSERT_EQUAL(qpoints.size(), dual_phi[1].size());
73
74 for (auto qp : index_range(dual_phi[1]))
75 LIBMESH_ASSERT_FP_EQUAL(1./2. * (1. + 3.*qpoints[qp](0)), dual_phi[1][qp],
76 my_tol);
77 }
78
85 void testTransformedDual (const ElemType elem_type, const unsigned int n,
86 const Real vertex_over_mid, const FEFamily family = LAGRANGE)
87 {
89 MeshTools::Generation::build_square(mesh, 1, 1, -1., 1., -1., 1., elem_type);
90
91 auto rng = mesh.active_local_element_ptr_range();
92 if (rng.begin() == rng.end())
93 return;
94 const Elem * elem = *(rng.begin());
95
96 FEType fe_type(SECOND, family);
97 std::unique_ptr<FEBase> fe = FEBase::build(2, fe_type);
98 const auto & JxW = fe->get_JxW();
99 const auto & phi = fe->get_phi();
100 const auto & dual_phi = fe->get_dual_phi();
101 QGauss qrule(2, fe_type.default_quadrature_order());
102 fe->attach_quadrature_rule(&qrule);
103 fe->reinit(elem);
104
105 CPPUNIT_ASSERT_EQUAL(std::size_t(n), phi.size());
106 CPPUNIT_ASSERT_EQUAL(std::size_t(n), dual_phi.size());
107
108 // TOLERANCE*TOLERANCE works with double but not float128
109 const Real my_tol = TOLERANCE*std::sqrt(TOLERANCE);
110
111 // dtilde_j = \int dual_phi_j; these are the weights that must be positive.
112 std::vector<Real> dtilde(n, 0);
113 for (const auto j : make_range(n))
114 for (const auto qp : index_range(JxW))
115 dtilde[j] += JxW[qp]*dual_phi[j][qp];
116
117 Real sum_dtilde = 0, volume = 0;
118 for (const auto j : make_range(n))
119 {
120 CPPUNIT_ASSERT(dtilde[j] > 0);
121 sum_dtilde += dtilde[j];
122 }
123 for (const auto qp : index_range(JxW))
124 volume += JxW[qp];
125
126 // Partition of unity is preserved, so constants are reproduced and the weights sum to the volume.
127 LIBMESH_ASSERT_FP_EQUAL(volume, sum_dtilde, my_tol);
128 for (const auto qp : index_range(JxW))
129 {
130 Real sum = 0;
131 for (const auto j : make_range(n))
132 sum += dual_phi[j][qp];
133 LIBMESH_ASSERT_FP_EQUAL(1., sum, my_tol);
134 }
135
136 // Scale-free signature of alpha.
137 LIBMESH_ASSERT_FP_EQUAL(vertex_over_mid,
138 dtilde[0]/dtilde[elem->n_vertices()], my_tol);
139
140 // T from the element's own adjacency, not an assumed index convention.
141 const Real alpha = Real(1)/5;
142 DenseMatrix<Real> T(n, n);
143 for (const auto i : make_range(n))
144 T(i,i) = 1.;
145 for (const auto m : make_range(elem->n_vertices(), n))
146 {
147 T(m,m) = 1. - 2.*alpha;
148 for (const auto v : make_range(elem->n_second_order_adjacent_vertices(m)))
149 T(elem->second_order_adjacent_vertex(m,v), m) += alpha;
150 }
151
152 for (const auto k : make_range(n))
153 for (const auto j : make_range(n))
154 {
155 Real integral = 0;
156 for (const auto qp : index_range(JxW))
157 {
158 Real ntilde_k = 0;
159 for (const auto l : make_range(n))
160 ntilde_k += T(k,l)*phi[l][qp];
161 integral += JxW[qp]*dual_phi[j][qp]*ntilde_k;
162 }
163 LIBMESH_ASSERT_FP_EQUAL(k == j ? dtilde[j] : 0., integral, my_tol);
164 }
165 }
166
168 {
169 LOG_UNIT_TEST;
170 // QUAD8 weights 1/5 (corner) and 4/5 (mid-edge) on the reference element
172 }
173
175 {
176 LOG_UNIT_TEST;
177 // TRI6 weights 1/15 (vertex) and 1/10 (mid-edge) on the reference element
178 testTransformedDual(TRI6, 6, Real(2)/3);
179 }
180
182 {
183 LOG_UNIT_TEST;
184 // SECOND order on TRI7 is the TRI6 basis, with no degree of freedom at the centroid node
185 testTransformedDual(TRI7, 6, Real(2)/3);
186 }
187
189 {
190 LOG_UNIT_TEST;
191 // L2_LAGRANGE shares the LAGRANGE shape functions
195 }
196
197 void setUp()
198 {
199 FEType fe_type(FIRST, LAGRANGE);
200 _fe = FEBase::build(1, fe_type);
201 _fe->get_phi();
202 _fe->get_dual_phi();
203
204 _mesh = std::make_unique<Mesh>(*TestCommWorld);
205
207
208 auto rng = _mesh->active_local_element_ptr_range();
209 _elem = rng.begin() == rng.end() ? nullptr : *(rng.begin());
210
211 _qrule = std::make_unique<QGauss>(1, fe_type.default_quadrature_order());
212 _fe->attach_quadrature_rule(_qrule.get());
213 }
214
215 void tearDown() {}
216
217};
218
This class is for unit testing dual coefficient and shape function values.
LIBMESH_CPPUNIT_TEST_SUITE(DualShapeTest)
std::unique_ptr< FEBase > _fe
CPPUNIT_TEST(testTri6Lagrange)
std::unique_ptr< QGauss > _qrule
CPPUNIT_TEST(testQuad8Lagrange)
std::unique_ptr< Mesh > _mesh
void testTransformedDual(const ElemType elem_type, const unsigned int n, const Real vertex_over_mid, const FEFamily family=LAGRANGE)
Check what the transformed dual basis on QUAD8/TRI6/TRI7 guarantees, none of which the untransformed ...
CPPUNIT_TEST(testL2Lagrange)
CPPUNIT_TEST(testEdge2Lagrange)
CPPUNIT_TEST(testTri7Lagrange)
Defines a dense matrix for use in Finite Element-type computations.
This is the base class from which all geometric element types are derived.
Definition elem.h:96
virtual unsigned int n_vertices() const =0
virtual unsigned int n_second_order_adjacent_vertices(const unsigned int n) const
Definition elem.C:3175
virtual unsigned short int second_order_adjacent_vertex(const unsigned int n, const unsigned int v) const
Definition elem.C:3183
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
Order default_quadrature_order() const
Definition fe_type.h:415
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
This class implements specific orders of Gauss quadrature.
Communicator * TestCommWorld
CPPUNIT_TEST_SUITE_REGISTRATION(DualShapeTest)
MeshBase & mesh
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.
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.
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