| 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 <cstddef> |
| 7 |
#include <iostream> |
| 8 |
#include <typeinfo> |
| 9 |
#include <cstdlib> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include <dune/common/gmpfield.hh> |
| 13 |
|
| 14 |
// Lagrange type elements |
| 15 |
#include <dune/localfunctions/lagrange.hh> |
| 16 |
#include <dune/localfunctions/lagrange/equidistantpoints.hh> |
| 17 |
|
| 18 |
// DG type elements |
| 19 |
#include <dune/localfunctions/orthonormal.hh> |
| 20 |
|
| 21 |
// Raviart Thomas type elements |
| 22 |
#include <dune/localfunctions/raviartthomas.hh> |
| 23 |
|
| 24 |
#include "test-localfe.hh" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
// Check if the traces of the basis functions on all |
| 29 |
// faces have the desired polynomial order by comparing |
| 30 |
// them to the interpolation into an appropriate trace |
| 31 |
// FE space at a set of sample points. |
| 32 |
template<class FE> |
| 33 |
bool checkTraceOrder(const FE& fe, int order, double tol) |
| 34 |
{ |
| 35 |
bool success = true; |
| 36 |
|
| 37 |
using namespace Dune::Indices; |
| 38 |
using T = typename FE::Traits::LocalBasisType::Traits::RangeFieldType; |
| 39 |
using Range = typename FE::Traits::LocalBasisType::Traits::RangeType; |
| 40 |
constexpr auto dim = FE::Traits::LocalBasisType::Traits::dimDomain; |
| 41 |
|
| 42 |
auto evaluationBuffer = std::vector<Range>(); |
| 43 |
auto coefficients = std::vector<Range>(); |
| 44 |
|
| 45 |
// Loop over all faces with 1<=codim<dim |
| 46 |
const auto& re = Dune::referenceElement<T, dim>(fe.type()); |
| 47 |
Dune::Hybrid::forEach(Dune::range(_1, Dune::index_constant<dim>{}), [&](auto codim) { |
| 48 |
for(auto face : Dune::range(re.size(codim))) |
| 49 |
{ |
| 50 |
// Check if the trace on the face is in the appropriate |
| 51 |
// polynomial space by interpolating each basis function |
| 52 |
// into a corresponding polynomial trace space and comparing |
| 53 |
// the results. |
| 54 |
using TraceFE = typename Dune::LagrangeLocalFiniteElement<Dune::EquidistantPointSet, dim-codim, T, T>; |
| 55 |
using std::fabs; |
| 56 |
auto faceGeometry = re.template geometry<codim>(face); |
| 57 |
auto traceFE = TraceFE(re.type(face, codim), order); |
| 58 |
const auto& sampleRule = Dune::QuadratureRules<T, dim-codim>::rule(re.type(face, codim), 2*order); |
| 59 |
|
| 60 |
for(auto i : Dune::range(fe.size())) |
| 61 |
{ |
| 62 |
auto fe_i = [&](auto x) { |
| 63 |
fe.localBasis().evaluateFunction(faceGeometry.global(x), evaluationBuffer); |
| 64 |
return evaluationBuffer[i]; |
| 65 |
}; |
| 66 |
|
| 67 |
traceFE.localInterpolation().interpolate(fe_i, coefficients); |
| 68 |
auto fe_i_interpolated = [&](auto x) { |
| 69 |
traceFE.localBasis().evaluateFunction(x, evaluationBuffer); |
| 70 |
auto y = Range(0); |
| 71 |
for(auto j : Dune::range(traceFE.size())) |
| 72 |
y += evaluationBuffer[j]*coefficients[j]; |
| 73 |
return y; |
| 74 |
}; |
| 75 |
double mismatch = 0; |
| 76 |
for(auto [x, dummy] : sampleRule) |
| 77 |
mismatch = std::max(mismatch, double(fabs(fe_i(x) - fe_i_interpolated(x)))); |
| 78 |
if (mismatch > tol) |
| 79 |
{ |
| 80 |
std::cout |
| 81 |
<< "The trace of basis function " << i |
| 82 |
<< " on face " << face |
| 83 |
<< " with codim " << codim |
| 84 |
<< " of " << fe.type() |
| 85 |
<< " is not in the desired polynomial space." |
| 86 |
<< " Maximal mismatch to interpolation is " << mismatch |
| 87 |
<< std::endl; |
| 88 |
success = false; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
}); |
| 93 |
return success; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
int main(int argc, char** argv) try |
| 99 |
{ |
| 100 |
bool success = true; |
| 101 |
|
| 102 |
{ |
| 103 |
double tol = 1e-7; |
| 104 |
using FE2D = Dune::LagrangeLocalFiniteElement<Dune::EquidistantPointSet, 2, double, double>; |
| 105 |
using FE3D = Dune::LagrangeLocalFiniteElement<Dune::EquidistantPointSet, 3, double, double>; |
| 106 |
for(auto order : {1,2,3,4}) |
| 107 |
{ |
| 108 |
std::cout << "Checking traces for Lagrange bases of order " << order << std::endl; |
| 109 |
success &= checkTraceOrder(FE2D(Dune::GeometryTypes::triangle, order), order, tol); |
| 110 |
success &= checkTraceOrder(FE2D(Dune::GeometryTypes::quadrilateral, order), order, tol); |
| 111 |
success &= checkTraceOrder(FE3D(Dune::GeometryTypes::tetrahedron, order), order, tol); |
| 112 |
success &= checkTraceOrder(FE3D(Dune::GeometryTypes::hexahedron, order), order, tol); |
| 113 |
success &= checkTraceOrder(FE3D(Dune::GeometryTypes::prism, order), order, tol); |
| 114 |
success &= checkTraceOrder(FE3D(Dune::GeometryTypes::pyramid, order), order, tol); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
std::cout << "Testing LagrangeLocalFiniteElement<EquidistantPointSet> on 3d" |
| 119 |
<< " simplex elements with double precision" << std::endl; |
| 120 |
for (unsigned int order : {1, 2, 4, 6}) |
| 121 |
{ |
| 122 |
std::cout << "order : " << order << std::endl; |
| 123 |
Dune::LagrangeLocalFiniteElement<Dune::EquidistantPointSet,3,double,double> |
| 124 |
lagrangeSimplex(Dune::GeometryTypes::simplex(3), order); |
| 125 |
TEST_FE(lagrangeSimplex); |
| 126 |
} |
| 127 |
std::cout << "Testing LagrangeLocalFiniteElement<EquidistantPointSet> on 2d" |
| 128 |
<< " cube elements with double precision" << std::endl; |
| 129 |
for (unsigned int order : {1, 2, 4}) |
| 130 |
{ |
| 131 |
std::cout << "order : " << order << std::endl; |
| 132 |
Dune::LagrangeLocalFiniteElement<Dune::EquidistantPointSet,2,double,double> |
| 133 |
lagrangeCube(Dune::GeometryTypes::cube(2), order); |
| 134 |
TEST_FE(lagrangeCube); |
| 135 |
} |
| 136 |
#if HAVE_GMP |
| 137 |
std::cout << "Testing LagrangeLocalFiniteElement<EquidistantPointSet> on 2d" |
| 138 |
<< " simplex elements with higher precision" << std::endl; |
| 139 |
for (unsigned int order : {5, 8}) |
| 140 |
{ |
| 141 |
std::cout << "order : " << order << std::endl; |
| 142 |
Dune::LagrangeLocalFiniteElement<Dune::EquidistantPointSet,2,double,double, |
| 143 |
Dune::GMPField<64>,Dune::GMPField<256> > |
| 144 |
lagrangeSimplex(Dune::GeometryTypes::simplex(2), order); |
| 145 |
TEST_FE(lagrangeSimplex); |
| 146 |
} |
| 147 |
#endif |
| 148 |
std::cout << "Testing DGLagrangeLocalFiniteElement<EquidistantPointSet> on 3d" |
| 149 |
<< " cube elements with double precision" << std::endl; |
| 150 |
for (unsigned int order : {1, 2}) |
| 151 |
{ |
| 152 |
std::cout << "order : " << order << std::endl; |
| 153 |
typedef Dune::LagrangeLocalFiniteElement<Dune::EquidistantPointSet,3,double,double> FE; |
| 154 |
Dune::DGLocalFiniteElement<FE> dglagrangeCube(Dune::GeometryTypes::cube(3), order); |
| 155 |
TEST_FE(dglagrangeCube); |
| 156 |
} |
| 157 |
std::cout << "Testing L2LagrangeLocalFiniteElement<EquidistantPointSet> on 3d" |
| 158 |
<< " cube elements with double precision" << std::endl; |
| 159 |
for (unsigned int order : {2, 3}) |
| 160 |
{ |
| 161 |
std::cout << "order : " << order << std::endl; |
| 162 |
typedef Dune::LagrangeLocalFiniteElement<Dune::EquidistantPointSet,3,double,double> FE; |
| 163 |
Dune::L2LocalFiniteElement<FE> dglagrangeCube(Dune::GeometryTypes::cube(3), order); |
| 164 |
TEST_FE(dglagrangeCube); |
| 165 |
} |
| 166 |
#if HAVE_GMP |
| 167 |
std::cout << "Testing OrthonormalFiniteElement on 3d" |
| 168 |
<< " prism elements with higher precision" << std::endl; |
| 169 |
for (unsigned int order : {6}) |
| 170 |
{ |
| 171 |
std::cout << "order : " << order << std::endl; |
| 172 |
Dune::OrthonormalLocalFiniteElement<3,double,double, |
| 173 |
Dune::GMPField<64>,Dune::GMPField<256> > |
| 174 |
onbPrism(Dune::GeometryTypes::prism, order); |
| 175 |
TEST_FE(onbPrism); |
| 176 |
} |
| 177 |
#endif |
| 178 |
std::cout << "Testing OrthonormalFiniteElement on 3d" |
| 179 |
<< " prism elements with double precision" << std::endl; |
| 180 |
for (unsigned int order : {4, 2}) |
| 181 |
{ |
| 182 |
std::cout << "order : " << order << std::endl; |
| 183 |
Dune::OrthonormalLocalFiniteElement<3,double,double> |
| 184 |
onbPrism(Dune::GeometryTypes::prism, order); |
| 185 |
TEST_FE(onbPrism); |
| 186 |
} |
| 187 |
std::cout << "Testing RaviartThomasSimplexFiniteElement on 3d" |
| 188 |
<< " simplex elements with double precision" << std::endl; |
| 189 |
for (unsigned int order : {0, 1, 4}) |
| 190 |
{ |
| 191 |
std::cout << "order : " << order << std::endl; |
| 192 |
Dune::RaviartThomasSimplexLocalFiniteElement<3,double,double> |
| 193 |
rtSimplex(Dune::GeometryTypes::simplex(3), order); |
| 194 |
TEST_FE(rtSimplex); |
| 195 |
} |
| 196 |
return success ? 0 : 1; |
| 197 |
} |
| 198 |
catch (const Dune::Exception &e) |
| 199 |
{ |
| 200 |
std::cout << e << std::endl; |
| 201 |
return 1; |
| 202 |
} |