DUNE PDELab (git)

mapperutilities.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUNCTIONS_COMMON_MAPPERUTILITIES_HH
8#define DUNE_FUNCTIONS_COMMON_MAPPERUTILITIES_HH
9
10#include <vector>
11
13
14#include <dune/grid/common/rangegenerators.hh>
16
17namespace Dune::Functions {
18
19// All utilities are in Impl:: and thus considered implementation
20// details for now. However, one may want to think about making
21// them public. Then they could also be put into dune-grid,
22// since there's nothing dune-function specific about them.
23namespace Impl {
24
25
26
27 // Helper class providing an unordered range
28 // of global indices associated to the element
29 // within a MultipleCodimMultipleGeomTypeMapper.
30 // This has to be bound to an element, before
31 // it can be used.
32 template<class GV>
33 class MapperElementSubIndices
34 {
35 using IndexContainer = std::vector<typename Dune::MultipleCodimMultipleGeomTypeMapper<GV>::Index>;
36 public:
37 using GridView = GV;
39 using Index = typename Mapper::Index;
40 using Element = typename GridView::template Codim<0>::Entity;
41
42 MapperElementSubIndices(const Mapper& mapper)
43 : mapper_(mapper)
44 {}
45
46 // Bind to given element and precompute all indices.
47 void bind(const Element& element)
48 {
49 constexpr auto dimension = GridView::dimension;
50 indices_.clear();
51 auto referenceElement = Dune::referenceElement<double, dimension>(element.type());
52 for (auto codim : Dune::range(dimension + 1)) {
53 for (auto subEntity : Dune::range(referenceElement.size(codim))) {
54 std::size_t c = mapper_.layout()(referenceElement.type(subEntity, codim), dimension);
55 if (c > 0) {
56 std::size_t firstIndex = mapper_.subIndex(element, subEntity, codim);
57 for (auto j : Dune::range(firstIndex, firstIndex + c)) {
58 indices_.push_back(j);
59 }
60 }
61 }
62 }
63 }
64
65 auto begin() const
66 {
67 return indices_.begin();
68 }
69
70 auto end() const
71 {
72 return indices_.end();
73 }
74
75 private:
76 const Mapper mapper_;
77 IndexContainer indices_;
78 };
79
80
81
82 // Helper function computing an average mesh size per subentity
83 // by averaging over the adjacent elements. This only considers
84 // the subentities handled by the given mapper and returns a
85 // std::vector<FieldType> of mesh sizes indexed according to the mapper.
86 //
87 // The average is determined by first computing the average volume
88 // of adjacent elements and then taking the d-th root for a d-dimensional
89 // grid.
90 //
91 // This operation has O(n) runtime (with n=mapper.size()),
92 // allocates O(n) memory for the returned vector and additional
93 // O(n) temporary memory.
94 template<class FieldType = double, class Mapper>
95 auto computeAverageSubEntityMeshSize(const Mapper& mapper)
96 {
97 constexpr auto dimension = Mapper::GridView::dimension;
98 std::vector<unsigned int> adjacentElements(mapper.size(), 0);
99 std::vector<FieldType> subEntityMeshSize(mapper.size(), 0.0);
100 auto subIndices = Impl::MapperElementSubIndices(mapper);
101 for(const auto& element : Dune::elements(mapper.gridView()))
102 {
103 auto A = element.geometry().volume();
104 subIndices.bind(element);
105 for(auto i : subIndices)
106 {
107 subEntityMeshSize[i] += A;
108 ++(adjacentElements[i]);
109 }
110 }
111 for(auto i : Dune::range(mapper.size()))
112 subEntityMeshSize[i] = std::pow(subEntityMeshSize[i]/adjacentElements[i], 1./dimension);
113 return subEntityMeshSize;
114 }
115
116
117
118} // end namespace Impl
119
120} // end namespace Dune::Functions
121
122
123#endif // end namespace DUNE_FUNCTIONS_COMMON_MAPPERUTILITIES_HH
GV::IndexSet::IndexType Index
Number type used for indices.
Definition: mcmgmapper.hh:136
static constexpr int dimension
The dimension of the grid.
Definition: gridview.hh:134
unspecified value type referenceElement(T &&... t)
Returns a reference element for the objects t....
Mapper for multiple codim and multiple geometry types.
Utilities for reduction like operations on ranges.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)