Debian Codegraph @

dune/localfunctions/test/virtualshapefunctiontest.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 <array>
7 #include <cstddef>
8 #include <iostream>
9
10 #include <dune/common/deprecated.hh>
11
12 #include <dune/geometry/type.hh>
13 #include <dune/localfunctions/common/virtualinterface.hh>
14 #include <dune/localfunctions/common/virtualwrappers.hh>
15
16 #include <dune/localfunctions/lagrange/p0.hh>
17 #include <dune/localfunctions/lagrange/lagrangesimplex.hh>
18 #include <dune/localfunctions/monomial.hh>
19
20 #define DUNE_DISABLE_DEPRECATION_WARNING_PQ22D
21 #include <dune/localfunctions/lagrange/pq22d.hh>
22 #undef DUNE_DISABLE_DEPRECATION_WARNING_PQ22D
23
24 /** \file
25 \brief Test the dynamically polymorphic shape function interface
26
27 This file mainly tests whether the polymorphic interface can be properly
28 instantiated, compiled and run without crashed. It does _not_ test whether
29 the shape function sets behave correctly.
30 */
31
32 using namespace Dune;
33
34 template <class T>
35 void syntax_check( const T& )
36 {}
37
38 // A test function to test the local interpolation
39 template <class D, class R>
40 struct TestFunction
41 {
42 using DomainType = D;
43 using RangeType = R;
44
45 RangeType operator()(const DomainType& in) const {
46 // May not be flexible enough to compile for all range types
47 RangeType out;
48 out = 1;
49 return out;
50 }
51 };
52
53
54 template <class T>
55 void testLocalBasis(const LocalBasisVirtualInterface<T>* localBasis)
56 {
57 // call each method once to test that it's there
58 syntax_check<unsigned int>( localBasis->order() );
59 [[maybe_unused]] unsigned int size = localBasis->size();
60
61 // evaluate the local basis at (0,...,0)
62 typename T::DomainType in(0);
63 std::vector<typename T::RangeType> out;
64 localBasis->evaluateFunction(in, out);
65 assert(out.size() == size);
66
67 std::vector<typename T::JacobianType> jacobianOut;
68 localBasis->evaluateJacobian(in, jacobianOut);
69 assert(jacobianOut.size() == localBasis->size());
70 }
71
72 void testLocalCoefficients(const LocalCoefficientsVirtualInterface* localCoefficients)
73 {
74 if (!localCoefficients)
75 DUNE_THROW(Dune::Exception, "Received an invalid pointer to LocalCoefficientsVirtualInterface");
76
77 if (localCoefficients->size() < 1)
78 DUNE_THROW(Dune::Exception, "LocalCoefficients does not provide any coefficients!");
79
80 for (std::size_t i=0; i<localCoefficients->size(); i++) {
81
82 // Test the localKey method
83 // We just test whether the interface is there. Correctness is tested elsewhere
84 syntax_check<unsigned int>( localCoefficients->localKey(i).subEntity() );
85 syntax_check<unsigned int>( localCoefficients->localKey(i).codim() );
86 syntax_check<unsigned int>( localCoefficients->localKey(i).index() );
87
88 }
89 }
90
91 template <class DomainType, class RangeType>
92 void testLocalInterpolation(const LocalInterpolationVirtualInterface<DomainType,RangeType>* localInterpolation)
93 {
94 // Test interpolation of a function object derived from VirtualFunction
95 TestFunction<DomainType,RangeType> testFunction;
96 std::vector<typename RangeType::field_type> coefficients;
97
98 //////////////////////////////////////////////////////////////////////////////
99 // Feed the function to the 'interpolate' method in form of a callable.
100 //////////////////////////////////////////////////////////////////////////////
101 localInterpolation->interpolate(testFunction, coefficients);
102 }
103
104 // Test all methods of a local finite element given as a pointer to the abstract base class
105 template <class T>
106 void testLocalFiniteElement(const LocalFiniteElementVirtualInterface<T>* localFiniteElement)
107 {
108 // Test method type()
109 std::cout << "Testing local finite element for a " << localFiniteElement->type() << "." << std::endl;
110
111 typedef LocalFiniteElementVirtualInterface<T> FEType;
112
113 // Test the local basis
114 const typename FEType::Traits::LocalBasisType& basis = localFiniteElement->localBasis();
115 testLocalBasis(&basis);
116
117 // Test the local coefficients
118 const typename FEType::Traits::LocalCoefficientsType& coeffs = localFiniteElement->localCoefficients();
119 testLocalCoefficients(&coeffs);
120
121 // Test the interpolation
122 const typename FEType::Traits::LocalInterpolationType& interp = localFiniteElement->localInterpolation();
123 testLocalInterpolation(&interp);
124
125 // Test cloning
126 const LocalFiniteElementVirtualInterface<T>* other = localFiniteElement->clone();
127
128 // Make sure new object has the same type
129 if (typeid(other).hash_code() != typeid(localFiniteElement).hash_code())
130 DUNE_THROW(Dune::Exception, "'clone' method returns object of wrong type");
131
132 // But it shouldn't be the same object
133 if (other==localFiniteElement)
134 DUNE_THROW(Dune::Exception, "'clone' method returned the object it was called for!");
135
136 delete other;
137
138 }
139
140 int main (int argc, char *argv[])
141 {
142
143 typedef Dune::LagrangeSimplexLocalFiniteElement<double, double, 2, 1>::Traits::LocalBasisType::Traits LBTraits;
144
145 const Dune::P0LocalFiniteElement<double, double, 2> p0FE(Dune::GeometryTypes::cube(2));
146 const Dune::LocalFiniteElementVirtualImp<Dune::P0LocalFiniteElement<double, double, 2> > p0VFE(p0FE);
147 testLocalFiniteElement<LBTraits>(&p0VFE);
148
149 DUNE_NO_DEPRECATED_BEGIN
150 const Dune::PQ22DLocalFiniteElement<double, double> pq2FE(Dune::GeometryTypes::cube(2));
151 const Dune::PQ22DLocalFiniteElement<double, double> pq2FE2(pq2FE);
152
153 const Dune::LocalFiniteElementVirtualImp<Dune::PQ22DLocalFiniteElement<double, double> > pq2VFE(pq2FE);
154 testLocalFiniteElement<LBTraits>(&pq2VFE);
155 DUNE_NO_DEPRECATED_END
156
157 const Dune::LocalFiniteElementVirtualImp<Dune::LagrangeSimplexLocalFiniteElement<double, double, 2, 1>> p1VFE;
158 testLocalFiniteElement<LBTraits>(&p1VFE);
159
160 const Dune::LocalFiniteElementVirtualImp<Dune::LocalFiniteElementVirtualImp<
161 Dune::LagrangeSimplexLocalFiniteElement<double, double, 2, 1>>> p1VVFE;
162 testLocalFiniteElement<LBTraits>(&p1VVFE);
163
164 typedef Dune::MonomialLocalFiniteElement<double, double, 2, 7> Monom7;
165 const Monom7 monom7FE(Dune::GeometryTypes::cube(2));
166 const Dune::LocalFiniteElementVirtualImp<Monom7> monom7VFE(monom7FE);
167 const Dune::LocalFiniteElementVirtualImp<
168 Dune::LocalFiniteElementVirtualImp<Monom7> > monom7VVFE(monom7VFE);
169 testLocalFiniteElement<LBTraits>(&monom7VVFE);
170
171 return 0;
172
173 }