// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // This header is not part of the official Dune API and might be subject // to change. You can use this header to test external finite element // implementations, but be warned that your tests might break with future // Dune versions. #ifndef DUNE_LOCALFUNCTIONS_TEST_TEST_FE_HH #define DUNE_LOCALFUNCTIONS_TEST_TEST_FE_HH #include #include #include #include #include #include #include #include #include #include #include // This class defines a local finite element function. // It is determined by a local finite element and // representing the local basis and a coefficient vector. // This provides the evaluate method needed by the interpolate() // method. template class FEFunction { const FE& fe; public: using RangeType = typename FE::Traits::Basis::Traits::Range; using DomainType = typename FE::Traits::Basis::Traits::DomainLocal; typedef typename FE::Traits::Basis::Traits::RangeField CT; std::vector coeff; FEFunction(const FE& fe_) : fe(fe_) { resetCoefficients(); } void resetCoefficients() { coeff.resize(fe.basis().size()); for(std::size_t i=0; i yy; fe.basis().evaluateFunction(x, yy); y = 0.0; for (std::size_t i=0; i bool testInterpolation(const FE& fe, double eps, int n=5) { bool success = true; FEFunction f(fe); std::vector::CT> coeff; for(int i=0; i() << ":" << std::endl; std::cout << " Interpolation vector has size " << coeff.size() << std::endl; std::cout << " Basis has size " << fe.basis().size() << std::endl; std::cout << std::endl; success = false; // skip rest of loop since that depends on matching sizes continue; } // Check if interpolation weights are equal to coefficients for(std::size_t j=0; j (2*coeff.size()*eps)*(std::max(std::abs(f.coeff[j]), 1.0)) ) { std::cout << std::setprecision(16); std::cout << "Bug in LocalInterpolation for finite element type " << Dune::className() << ":" << std::endl; std::cout << " Interpolation weight " << j << " differs by " << std::abs(coeff[j]-f.coeff[j]) << " from coefficient of " << "linear combination." << std::endl; std::cout << std::endl; success = false; } } } return success; } // check whether Jacobian agrees with FD approximation /** * \param geo The geometry the finite element is tested on. * \param fe The finite element to test. * \param eps Tolerance for comparing floating-point values. When comparing * numerical derivatives, this is divided by \c delta to yield an * even bigger tolerance. * \param delta Stepsize to use when doing numerical derivatives. * \param order The Jacobian is checked at a number of quadrature points. * This parameter determines the order of the quatrature rule * used to obtain the quadrature points. */ template bool testJacobian(const Geo &geo, const FE& fe, double eps, double delta, std::size_t order = 2) { typedef typename FE::Traits::Basis Basis; typedef typename Basis::Traits::DomainField DF; static const std::size_t dimDLocal = Basis::Traits::dimDomainLocal; typedef typename Basis::Traits::DomainLocal DomainLocal; static const std::size_t dimDGlobal = Basis::Traits::dimDomainGlobal; static const std::size_t dimR = Basis::Traits::dimRange; typedef typename Basis::Traits::Range Range; typedef typename Basis::Traits::Jacobian Jacobian; bool success = true; // //////////////////////////////////////////////////////////// // Check the partial derivatives by comparing them // to finite difference approximations // //////////////////////////////////////////////////////////// // A set of test points const Dune::QuadratureRule quad = Dune::QuadratureRules::rule(fe.type(),order); // Loop over all quadrature points for (std::size_t i=0; i < quad.size(); i++) { // Get a test point const DomainLocal& testPoint = quad[i].position(); // Get the shape function derivatives there std::vector jacobians; fe.basis().evaluateJacobian(testPoint, jacobians); if(jacobians.size() != fe.basis().size()) { std::cout << "Bug in evaluateJacobianGlobal() for finite element type " << Dune::className() << ":" << std::endl; std::cout << " Jacobian vector has size " << jacobians.size() << std::endl; std::cout << " Basis has size " << fe.basis().size() << std::endl; std::cout << std::endl; return false; } Dune::FieldMatrix geoJT = geo.jacobianTransposed(testPoint); // Loop over all shape functions in this set for (std::size_t j=0; j localJacobian(0); for(std::size_t k = 0; k < dimR; ++k) for(std::size_t l = 0; l < dimDGlobal; ++l) for(std::size_t m = 0; m < dimDLocal; ++m) localJacobian[k][m] += jacobians[j][k][l] * geoJT[m][l]; // Loop over all local directions for (std::size_t m = 0; m < dimDLocal; ++m) { // Compute an approximation to the derivative by finite differences DomainLocal upPos = testPoint; DomainLocal downPos = testPoint; upPos[m] += delta; downPos[m] -= delta; std::vector upValues, downValues; fe.basis().evaluateFunction(upPos, upValues); fe.basis().evaluateFunction(downPos, downValues); //Loop over all components for(std::size_t k = 0; k < dimR; ++k) { // The current partial derivative, just for ease of notation double derivative = localJacobian[k][m]; double finiteDiff = (upValues[j][k] - downValues[j][k]) / (2*delta); // Check if ( std::abs(derivative-finiteDiff) > eps/delta*(std::max(std::abs(finiteDiff), 1.0)) ) { std::cout << std::setprecision(16); std::cout << "Bug in evaluateJacobian() for finite element type " << Dune::className() << ":" << std::endl; std::cout << " Shape function derivative does not agree with " << "FD approximation" << std::endl; std::cout << " Shape function " << j << " component " << k << " at position " << testPoint << ": derivative in " << "local direction " << m << " is " << derivative << ", but " << finiteDiff << " is " << "expected." << std::endl; std::cout << std::endl; success = false; } } //Loop over all components } // Loop over all local directions } // Loop over all shape functions in this set } // Loop over all quadrature points return success; } // call tests for given finite element template bool testFE(const Geo &geo, const FE& fe, double eps, double delta, unsigned order = 2) { bool success = true; success = testInterpolation(fe, eps) and success; success = testJacobian(geo, fe, eps, delta, order) and success; return success; } #endif // DUNE_LOCALFUNCTIONS_TEST_TEST_FE_HH