Debian Codegraph @

dune/localfunctions/test/lagrangeshapefunctiontest.cc

raw
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5
6 #include <iostream>
7 #include <typeinfo>
8 #include <fenv.h>
9
10 #include <dune/common/classname.hh>
11 #include <dune/common/deprecated.hh>
12 #include <dune/common/test/testsuite.hh>
13
14 #include <dune/localfunctions/lagrange/p0.hh>
15 #include <dune/localfunctions/lagrange/lagrangelfecache.hh>
16 #include <dune/localfunctions/lagrange/lagrangecube.hh>
17 #include <dune/localfunctions/lagrange/lagrangeprism.hh>
18 #include <dune/localfunctions/lagrange/lagrangepyramid.hh>
19 #include <dune/localfunctions/lagrange/lagrangesimplex.hh>
20
21 #define DUNE_DISABLE_DEPRECATION_WARNING_PQ22D
22 #include <dune/localfunctions/lagrange/pq22d.hh>
23 #undef DUNE_DISABLE_DEPRECATION_WARNING_PQ22D
24
25 #include <dune/localfunctions/test/test-localfe.hh>
26
27 /** \file
28 \brief Performs some tests for the Lagrange shape functions
29 */
30
31 double epsilon = 1e-14;
32 double sqrt_epsilon = std::sqrt(epsilon);
33
34 using namespace Dune;
35
36 // Generate list of Lagrange points for dim-dimensional simplex
37 template <int dim>
38 void getPkTestPoints(unsigned order, unsigned level, std::vector<FieldVector<double,dim> >& test_points)
39 {
40 for (unsigned i = 0; i <= order - level; ++i)
41 {
42 std::vector<FieldVector<double,dim-1> > test_points_lower_dim;
43 getPkTestPoints(order, level + i, test_points_lower_dim);
44 double coord = double(i) / order;
45 for (unsigned j = 0; j < test_points_lower_dim.size(); ++j)
46 {
47 FieldVector<double,dim> pos;
48 for (int k = 0; k < dim-1; ++k)
49 pos[k] = test_points_lower_dim[j][k];
50 pos[dim-1] = coord;
51 test_points.push_back(pos);
52 }
53 }
54 }
55
56 // Template specialization to terminate recursion
57 template <>
58 void getPkTestPoints(unsigned order, unsigned level, std::vector<FieldVector<double,0> >& test_points)
59 {
60 FieldVector<double,0> pos;
61 test_points.push_back(pos);
62 }
63
64 template <class FE>
65 bool testPk(const FE& local_fe)
66 {
67 const int dim = FE::Traits::LocalBasisType::Traits::dimDomain;
68 const unsigned order = local_fe.localBasis().order();
69
70 std::vector<FieldVector<double,1> > values;
71
72 std::vector<FieldVector<double,dim> > test_points;
73 getPkTestPoints(order, 0, test_points);
74
75 for (unsigned n = 0; n < test_points.size(); ++n)
76 {
77 FieldVector<double,dim> pos = test_points[n];
78
79 // ////////////////////////////////////////////////////////////////
80 // Verify that shape functions fulfill \phi_i(x_j) = \delta_{ij}
81 // We assume that the shape functions are ordered corresponding
82 // to the test points returned by getPkTestPoints()
83 // ////////////////////////////////////////////////////////////////
84
85 local_fe.localBasis().evaluateFunction(pos, values);
86 for (unsigned i = 0; i < values.size(); ++i)
87 if (std::abs(values[i] - double(i==n)) > epsilon)
88 {
89 std::cerr << "Bug in shape function in local finite element type"
90 << typeid(FE).name() << std::endl;
91 std::cerr << "Shape function " << n << " has value " << values[i]
92 << " at position " << pos << " while " << double(i==n)
93 << " was expected" << std::endl;
94 return false;
95 }
96
97 }
98
99 return true;
100 }
101
102 int main (int argc, char *argv[])
103 {
104 #if __linux__ \
105 && (!defined __INTEL_COMPILER || __INTEL_COMPILER >= 1010) \
106 && (!defined __clang__)
107 feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
108 #endif
109
110 auto testSuite = Dune::TestSuite();
111
112 // ////////////////////////////////////////////////////////
113 // Test for the Lagrange property
114 // ////////////////////////////////////////////////////////
115
116 Dune::Hybrid::forEach(std::index_sequence<1,2,3>{},[&](auto dim)
117 {
118 Dune::Hybrid::forEach(std::index_sequence<1,2,3,4>{},[&](auto order)
119 {
120 auto lfe = LagrangeSimplexLocalFiniteElement<double,double,dim,order>();
121 testSuite.check(testPk(lfe))
122 << "Lagrange property not satisfied for " << Dune::className(lfe);
123 });
124 });
125
126 // ////////////////////////////////////////////////////////
127 // Run the standard tests
128 // ////////////////////////////////////////////////////////
129
130 // This check extends the generic testFE() by additionally
131 // wrapping the LFE into the virtual interface twice
132 // Check local finite element and its virtualized variants
133 // Notice that testFE add another level of virtualization
134 auto testVirtualLFE = [&](const auto& lfe, auto... args)
135 {
136 auto testSuite = Dune::TestSuite(Dune::className(lfe));
137 using LFE = std::decay_t<decltype(lfe)>;
138 using LocalBasisTraits = typename LFE::Traits::LocalBasisType::Traits;
139 using Interface = Dune::LocalFiniteElementVirtualInterface<LocalBasisTraits>;
140 auto vlfe = Dune::LocalFiniteElementVirtualImp<LFE>(lfe);
141 auto vvlfe = Dune::LocalFiniteElementVirtualImp<decltype(vlfe)>(vlfe);
142 const auto& ilfe = static_cast<const Interface&>(vlfe);
143 testSuite.check(testFE(lfe, args...)) << "Check of raw local finite element";
144 testSuite.check(testFE(vlfe, args...)) << "Check of virtualized local finite element";
145 testSuite.check(testFE(vvlfe, args...)) << "Check of double virtualized local finite element";
146 testSuite.check(testFE(ilfe, args...)) << "Check of virtualized local finite element via interface";
147 return testSuite;
148 };
149
150 // Special implementations
151
152 auto p0LFE = Dune::P0LocalFiniteElement<double,double,2>(GeometryTypes::simplex(2));
153 testSuite.subTest(testVirtualLFE(p0LFE, DisableNone, 0));
154
155 DUNE_NO_DEPRECATED_BEGIN
156 auto mixedPQ22DLFE = Dune::PQ22DLocalFiniteElement<double,double>(GeometryTypes::simplex(2));
157 testSuite.subTest(testVirtualLFE(mixedPQ22DLFE, DisableNone, 0));
158 DUNE_NO_DEPRECATED_END
159
160 auto prismP1LFE = LagrangePrismLocalFiniteElement<double,double,1>();
161 testSuite.subTest(testVirtualLFE(prismP1LFE, DisableNone, 2));
162
163 auto prismP2LFE = LagrangePrismLocalFiniteElement<double,double,2>();
164 testSuite.subTest(testVirtualLFE(prismP2LFE, DisableNone, 1));
165
166 // Pyramid shapefunctions are not differentiable on the plane where xi[0]=xi[1].
167 // So let's skip test points on this plane
168 auto xySkip = [](const FieldVector<double,3>& xi){return std::abs(xi[0]-xi[1]) < 1e-8;};
169
170 auto pyramidP1LFE = LagrangePyramidLocalFiniteElement<double,double,1>();
171 testSuite.subTest(testVirtualLFE(pyramidP1LFE, DisableNone, 1, xySkip));
172
173 auto pyramidP2LFE = LagrangePyramidLocalFiniteElement<double,double,2>();
174 testSuite.subTest(testVirtualLFE(pyramidP2LFE, DisableNone, 1, xySkip));
175
176 // Simplex implementations
177 Dune::Hybrid::forEach(std::index_sequence<1,2,3>{},[&](auto dim)
178 {
179 Dune::Hybrid::forEach(std::make_index_sequence<5>{},[&](auto order)
180 {
181 auto diffOrder = 2;
182 auto lfe = LagrangeSimplexLocalFiniteElement<double,double,dim,order>();
183 testSuite.subTest(testVirtualLFE(lfe, DisableNone, diffOrder));
184 });
185 });
186
187 // Cube implementations
188 Dune::Hybrid::forEach(std::index_sequence<1,2,3>{},[&](auto dim)
189 {
190 Dune::Hybrid::forEach(std::make_index_sequence<5>{},[&](auto order)
191 {
192 auto diffOrder = 2;
193 auto lfe = LagrangeCubeLocalFiniteElement<double,double,dim,order>();
194 testSuite.subTest(testVirtualLFE(lfe, DisableNone, diffOrder));
195 });
196 });
197
198 // Test the LagrangeLocalFiniteElementCache
199 auto lagrangeLFECache = LagrangeLocalFiniteElementCache<double,double,2,2>();
200 testSuite.subTest(testVirtualLFE(lagrangeLFECache.get(GeometryTypes::simplex(2))));
201 testSuite.subTest(testVirtualLFE(lagrangeLFECache.get(GeometryTypes::cube(2))));
202
203 // Test whether asking the cache for an element of the wrong dimension throws an exception
204 testSuite.checkThrow([&]{
205 lagrangeLFECache.get(GeometryTypes::simplex(1));
206 }) << "LagrangeLocalFiniteElementCache does not throw for non-existing element";
207
208 return testSuite.exit();
209 }