3#ifndef DUNE_FUNCTIONS_FUNCTIONSPACEBASES_TEST_BASISTEST_HH
4#define DUNE_FUNCTIONS_FUNCTIONSPACEBASES_TEST_BASISTEST_HH
11#include <dune/common/test/testsuite.hh>
14#include <dune/common/hybridutilities.hh>
18#include <dune/functions/functionspacebases/concepts.hh>
20struct CheckBasisFlag {};
21struct AllowZeroBasisFunctions {};
23template<
class T,
class... S>
24struct IsContained :
public std::disjunction<std::is_same<T,S>...>
32template<
class Element,
class Gr
idView>
33std::string elementStr(
const Element& element,
const GridView& gridView)
36 s << element.type() <<
"#" << gridView.indexSet().index(element);
44template<
class MultiIndex>
45bool multiIndicesConsecutive(
const MultiIndex& a,
const MultiIndex& b)
50 for (; (i<a.size()) and (i<b.size()) and (a[i] == b[i]); ++i)
54 if ((i<a.size()) and (i==b.size()))
58 if ((i<a.size()) and (i<b.size()))
68 for (; i<b.size(); ++i)
84template<
class MultiIndexSet>
85Dune::TestSuite checkBasisIndexTreeConsistency(
const MultiIndexSet& multiIndexSet)
87 Dune::TestSuite test(
"index tree consistency check");
91 auto it = multiIndexSet.begin();
92 auto end = multiIndexSet.end();
95 auto lastMultiIndex = *it;
98 test.require(lastMultiIndex.size()>0,
"multi-index size check")
99 <<
"empty multi-index found";
102 for (
decltype(lastMultiIndex.size()) i = 0; i<lastMultiIndex.size(); ++i)
104 test.require(lastMultiIndex[i] == 0,
"smallest index check")
105 <<
"smallest index contains non-zero entry " << lastMultiIndex[i] <<
" in position " << i;
109 for(; it != end; ++it)
111 auto multiIndex = *it;
114 test.require(multiIndex.size()>0,
"multi-index size check")
115 <<
"empty multi-index found";
118 test.check(multiIndicesConsecutive(lastMultiIndex, multiIndex),
"consecutive index check")
119 <<
"multi-indices " << lastMultiIndex <<
" and " << multiIndex <<
" are subsequent but not consecutive";
121 lastMultiIndex = multiIndex;
132template<
class Basis,
class MultiIndexSet>
133Dune::TestSuite checkBasisSizeConsistency(
const Basis& basis,
const MultiIndexSet& multiIndexSet)
135 Dune::TestSuite test(
"index size consistency check");
137 auto prefix =
typename Basis::SizePrefix{};
139 for(
const auto& index : multiIndexSet)
142 for (
const auto& i: index)
148 auto prefixSize = basis.size(prefix);
149 test.require(prefixSize > i,
"basis.size(prefix) subtree check")
150 <<
"basis.size(" << prefix <<
")=" << prefixSize <<
" but index " << index <<
" exists";
155 auto prefixSize = basis.size(prefix);
156 test.require(prefixSize == 0,
"basis.size(prefix) leaf check")
157 <<
"basis.size(" << prefix <<
")=" << prefixSize <<
" but the prefix exists as index";
175Dune::TestSuite checkBasisIndices(
const Basis& basis)
177 Dune::TestSuite test(
"basis index check");
179 using MultiIndex =
typename Basis::MultiIndex;
183 auto compare = [](
const auto& a,
const auto& b) {
184 return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
187 auto multiIndexSet = std::set<MultiIndex,
decltype(compare)>{compare};
189 auto localView = basis.localView();
190 for (
const auto& e :
elements(basis.gridView()))
194 test.require(localView.size() <= localView.maxSize(),
"localView.size() check")
195 <<
"localView.size() is " << localView.size() <<
" but localView.maxSize() is " << localView.maxSize();
197 for (
decltype(localView.size()) i=0; i< localView.size(); ++i)
199 auto multiIndex = localView.index(i);
200 for(
auto mi: multiIndex)
202 <<
"Global multi-index containes negative entry for shape function " << i
203 <<
" in element " << elementStr(localView.element(), basis.gridView());
204 multiIndexSet.insert(multiIndex);
208 test.subTest(checkBasisIndexTreeConsistency(multiIndexSet));
209 test.subTest(checkBasisSizeConsistency(basis, multiIndexSet));
210 test.check(basis.dimension() == multiIndexSet.size())
211 <<
"basis.dimension() does not equal the total number of basis functions.";
222template<
class LocalFiniteElement>
223Dune::TestSuite checkNonZeroShapeFunctions(
const LocalFiniteElement& fe, std::size_t order = 5,
double tol = 1e-10)
225 Dune::TestSuite test;
226 static const int dimension = LocalFiniteElement::Traits::LocalBasisType::Traits::dimDomain;
230 std::vector<typename LocalFiniteElement::Traits::LocalBasisType::Traits::RangeType> values;
231 std::vector<bool> isNonZero;
232 isNonZero.resize(fe.size(),
false);
233 for (
const auto& qp : quadRule)
235 fe.localBasis().evaluateFunction(qp.position(), values);
236 for(std::size_t i=0; i<fe.size(); ++i)
237 isNonZero[i] = (isNonZero[i] or (values[i].infinity_norm() > tol));
239 for(std::size_t i=0; i<fe.size(); ++i)
240 test.check(isNonZero[i])
241 <<
"Found a constant zero basis function";
251template<
class Basis,
class LocalView,
class... Flags>
252Dune::TestSuite checkLocalView(
const Basis& basis,
const LocalView& localView, Flags... flags)
254 Dune::TestSuite test(std::string(
"LocalView on ") + elementStr(localView.element(), basis.gridView()));
256 test.check(localView.size() <= localView.maxSize(),
"localView.size() check")
257 <<
"localView.size() is " << localView.size() <<
" but localView.maxSize() is " << localView.maxSize();
260 std::vector<std::size_t> localIndices;
261 localIndices.resize(localView.size(), 0);
263 test.check(node.size() == node.finiteElement().size())
264 <<
"Size of leaf node and finite element are different.";
265 for(std::size_t i=0; i<node.size(); ++i)
267 test.check(node.localIndex(i) < localView.size())
268 <<
"Local index exceeds localView.size().";
269 if (node.localIndex(i) < localView.size())
270 ++(localIndices[node.localIndex(i)]);
275 for(std::size_t i=0; i<localView.size(); ++i)
278 test.check(localIndices[i]>=1)
279 <<
"Local index " << i <<
" did not appear";
280 test.check(localIndices[i]<=1)
281 <<
"Local index " << i <<
" appears multiple times";
285 if (not IsContained<AllowZeroBasisFunctions, Flags...>::value)
288 test.subTest(checkNonZeroShapeFunctions(node.finiteElement()));
302struct EnableContinuityCheck
304 std::size_t order_ = 5;
307 template<
class JumpEvaluator>
308 auto localJumpContinuityCheck(
const JumpEvaluator& jumpEvaluator, std::size_t order,
double tol)
const
310 return [=](
const auto& intersection,
const auto&
treePath,
const auto& insideNode,
const auto& outsideNode,
const auto& insideToOutside) {
311 using Intersection = std::decay_t<
decltype(intersection)>;
312 using Node = std::decay_t<
decltype(insideNode)>;
314 std::vector<int> isContinuous(insideNode.size(),
true);
317 using Range =
typename Node::FiniteElement::Traits::LocalBasisType::Traits::RangeType;
318 std::vector<std::vector<Range>> values;
319 std::vector<std::vector<Range>> neighborValues;
322 values.resize(quadRule.size());
323 neighborValues.resize(quadRule.size());
324 for(std::size_t k=0; k<quadRule.size(); ++k)
326 auto pointInElement = intersection.geometryInInside().global(quadRule[k].position());
327 auto pointInNeighbor = intersection.geometryInOutside().global(quadRule[k].position());
328 insideNode.finiteElement().localBasis().evaluateFunction(pointInElement, values[k]);
329 outsideNode.finiteElement().localBasis().evaluateFunction(pointInNeighbor, neighborValues[k]);
333 for(std::size_t i=0; i<insideNode.size(); ++i)
335 for(std::size_t k=0; k<quadRule.size(); ++k)
337 auto jump = values[k][i];
338 if (insideToOutside[i].has_value())
339 jump -= neighborValues[k][insideToOutside[i].value()];
340 isContinuous[i] = isContinuous[i] and (jumpEvaluator(jump, intersection, quadRule[k].position()) < tol);
347 auto localContinuityCheck()
const {
348 auto jumpNorm = [](
auto&&jump,
auto&& intersection,
auto&& x) ->
double {
349 return jump.infinity_norm();
351 return localJumpContinuityCheck(jumpNorm, order_, tol_);
361struct EnableNormalContinuityCheck :
public EnableContinuityCheck
363 auto localContinuityCheck()
const {
364 auto normalJump = [](
auto&&jump,
auto&& intersection,
auto&& x) ->
double {
365 return jump * intersection.unitOuterNormal(x);
367 return localJumpContinuityCheck(normalJump, order_, tol_);
378struct EnableTangentialContinuityCheck :
public EnableContinuityCheck
380 auto localContinuityCheck()
const {
381 auto tangentialJumpNorm = [](
auto&&jump,
auto&& intersection,
auto&& x) ->
double {
382 auto tangentialJump = jump - (jump * intersection.unitOuterNormal(x)) * intersection.unitOuterNormal(x);
383 return tangentialJump.two_norm();
385 return localJumpContinuityCheck(tangentialJumpNorm, order_, tol_);
396struct EnableCenterContinuityCheck :
public EnableContinuityCheck
398 template<
class JumpEvaluator>
399 auto localJumpCenterContinuityCheck(
const JumpEvaluator& jumpEvaluator,
double tol)
const
401 return [=](
const auto& intersection,
const auto&
treePath,
const auto& insideNode,
const auto& outsideNode,
const auto& insideToOutside) {
402 using Node = std::decay_t<
decltype(insideNode)>;
403 using Range =
typename Node::FiniteElement::Traits::LocalBasisType::Traits::RangeType;
405 std::vector<int> isContinuous(insideNode.size(),
true);
406 std::vector<Range> insideValues;
407 std::vector<Range> outsideValues;
409 insideNode.finiteElement().localBasis().evaluateFunction(intersection.geometryInInside().center(), insideValues);
410 outsideNode.finiteElement().localBasis().evaluateFunction(intersection.geometryInOutside().center(), outsideValues);
412 auto centerLocal = intersection.geometry().local(intersection.geometry().center());
415 for(std::size_t i=0; i<insideNode.size(); ++i)
417 auto jump = insideValues[i];
418 if (insideToOutside[i].has_value())
419 jump -= outsideValues[insideToOutside[i].value()];
420 isContinuous[i] = isContinuous[i] and (jumpEvaluator(jump, intersection, centerLocal) < tol);
426 auto localContinuityCheck()
const {
427 auto jumpNorm = [](
auto&&jump,
auto&& intersection,
auto&& x) ->
double {
428 return jump.infinity_norm();
430 return localJumpCenterContinuityCheck(jumpNorm, tol_);
446template<
class Basis,
class LocalCheck>
447Dune::TestSuite checkBasisContinuity(
const Basis& basis,
const LocalCheck& localCheck)
449 Dune::TestSuite test(
"Global continuity check of basis functions");
452 auto localView = basis.localView();
453 auto neighborLocalView = basis.localView();
455 for (
const auto& e :
elements(basis.gridView()))
458 for(
const auto& intersection :
intersections(basis.gridView(), e))
460 if (intersection.neighbor())
462 neighborLocalView.bind(intersection.outside());
465 const auto& outsideNode = Dune::TypeTree::child(neighborLocalView.tree(), treePath);
467 std::vector<std::optional<int>> insideToOutside;
468 insideToOutside.resize(insideNode.size());
471 for(std::size_t i=0; i<insideNode.size(); ++i)
473 for(std::size_t j=0; j<outsideNode.size(); ++j)
475 if (localView.index(insideNode.localIndex(i)) == neighborLocalView.index(outsideNode.localIndex(j)))
478 test.check(not insideToOutside[i].has_value())
479 <<
"Basis function " << localView.index(insideNode.localIndex(i))
480 <<
" appears twice in element " << elementStr(neighborLocalView.element(), basis.gridView());
481 insideToOutside[i] = j;
487 auto isContinuous = localCheck(intersection,
treePath, insideNode, outsideNode, insideToOutside);
489 for(std::size_t i=0; i<insideNode.size(); ++i)
491 test.check(isContinuous[i])
492 <<
"Basis function " << localView.index(insideNode.localIndex(i))
493 <<
" is discontinuous across intersection of elements "
494 << elementStr(localView.element(), basis.gridView())
495 <<
" and " << elementStr(neighborLocalView.element(), basis.gridView());
504template<
class Basis,
class... Flags>
505Dune::TestSuite checkConstBasis(
const Basis& basis, Flags... flags)
507 Dune::TestSuite test(
"const basis check");
509 using GridView =
typename Basis::GridView;
512 test.check(
Dune::models<Dune::Functions::Concept::GlobalBasis<GridView>, Basis>(),
"global basis concept check")
513 <<
"type passed to checkBasis() does not model the GlobalBasis concept";
516 auto localView = basis.localView();
517 for (
const auto& e :
elements(basis.gridView()))
520 test.subTest(checkLocalView(basis, localView, flags...));
524 test.subTest(checkBasisIndices(basis));
528 auto flagTuple = std::tie(flags...);
530 using Flag = std::decay_t<
decltype(flag)>;
531 if constexpr (std::is_base_of_v<EnableContinuityCheck, Flag>)
532 test.subTest(checkBasisContinuity(basis, flag.localContinuityCheck()));
539template<
class Basis,
class... Flags>
540Dune::TestSuite checkBasis(Basis& basis, Flags... flags)
542 Dune::TestSuite test(
"basis check");
545 test.subTest(checkConstBasis(basis,flags...));
548 auto gridView = basis.gridView();
549 basis.update(gridView);
Grid view abstract base class.
Definition: gridview.hh:63
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: intersection.hh:162
static const QuadratureRule & rule(const GeometryType &t, int p, QuadratureType::Enum qt=QuadratureType::GaussLegendre)
select the appropriate QuadratureRule for GeometryType t and order p
Definition: quadraturerules.hh:280
Infrastructure for concepts.
Traits for type conversions and type information.
constexpr auto models()
Check if concept is modeled by given types.
Definition: concept.hh:182
IteratorRange<... > intersections(const GV &gv, const Entity &e)
Iterates over all Intersections of an Entity with respect to the given GridView.
IteratorRange<... > elements(const GV &gv)
Iterates over all elements / cells (entities with codimension 0) of a GridView.
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:266
constexpr HybridTreePath< T... > treePath(const T &... t)
Constructs a new HybridTreePath from the given indices.
Definition: treepath.hh:188
void forEachLeafNode(Tree &&tree, LeafFunc &&leafFunc)
Traverse tree and visit each leaf node.
Definition: traversal.hh:304
Dune namespace.
Definition: alignedallocator.hh:11
Type trait to determine whether an instance of T has an operator[](I), i.e. whether it can be indexed...
Definition: typetraits.hh:250