AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
CoarsenedGridFunction.hpp
1#pragma once
2
3#include <functional>
4#include <type_traits>
5
6#include <dune/common/referencehelper.hh>
7#include <dune/functions/gridfunctions/gridviewentityset.hh>
8
9#include <amdis/common/TypeTraits.hpp>
10#include <amdis/Output.hpp>
11
12// backport of dune/functions/gridfunctions/coarsenedgridfunction.hh
13
14namespace AMDiS {
15
17
28template <class GV, class GF>
29class [[deprecated("Use Dune::Functions::FineFunctionOnCoarseGridView instead")]]
31{
32public:
34 using EntitySet = Dune::Functions::GridViewEntitySet<GV,0>;
35
37 using Domain = typename EntitySet::GlobalCoordinate;
38
40 using Range = std::invoke_result_t<GF, Domain>;
41
43 using LocalDomain = typename EntitySet::LocalCoordinate;
44
46 using Element = typename EntitySet::Element;
47
48 using Grid = typename GV::Grid;
49 using Geometry = typename Element::Geometry;
50
51 template <class ES, class LF>
53 {
54 public:
55 using Domain = LocalDomain;
56 using Range = CoarsenedGridFunction::Range;
57 using Element = CoarsenedGridFunction::Element;
58
59 using Mapping = std::function<Domain(Domain const&)>;
60 struct ChildElement {
61 typename Element::HierarchicIterator it;
62 Mapping local;
63 };
64
65 public:
67 CoarsenedLocalFunction(ES const& entitySet, LF const& localFct, int maxLevel)
68 : entitySet_(entitySet)
69 , localFct_(localFct)
70 , maxLevel_(maxLevel)
71 {}
72
73 CoarsenedLocalFunction(ES const& entitySet, LF&& localFct, int maxLevel)
74 : entitySet_(entitySet)
75 , localFct_(std::move(localFct))
76 , maxLevel_(maxLevel)
77 {}
78
80
85 void bind(Element const& element)
86 {
87 element_.emplace(element);
88
89 childs_.clear();
90 if (entitySet_.contains(*element_)) {
91 localFct_.bind(*element_);
92 return;
93 }
94
95 test_exit_dbg(!element_->isLeaf(), "Element is leaf. Cannot traverse its children.");
96 for (auto it = element_->hbegin(maxLevel_); it != element_->hend(maxLevel_); ++it) {
97 if (entitySet_.contains(*it))
98 childs_.emplace_back(ChildElement{.it=it, .local=makeMapping(*element_,*it)});
99 }
100
101 test_exit_dbg(!childs_.empty(), "No child element in entitySet found!");
102 }
103
105 void unbind()
106 {
107 element_.reset();
108 }
109
111 bool bound() const
112 {
113 return !!element_;
114 }
115
117 Range operator()(Domain const& x) const
118 {
119 if (childs_.empty())
120 return localFct_(x);
121
122 // calculate checkInsideTolerance
123 typename Grid::ctype const checkInsideTolerance = std::sqrt(std::numeric_limits<typename Grid::ctype>::epsilon());
124 for (auto const& child : childs_) {
125 auto refElem = referenceElement(*child.it);
126 auto local = child.local(x);
127 auto refTypeId = refElem.type().id();
128 bool isInside = Dune::Geo::Impl::checkInside(refTypeId, Geometry::mydimension, local, checkInsideTolerance);
129 if (isInside) {
130 localFct_.bind(*child.it);
131 return localFct_(local);
132 }
133 }
134
135 error_exit("No child element with x in child found!");
136 return Range{};
137 }
138
140 Element const& localContext() const
141 {
142 assert(!!element_);
143 return *element_;
144 }
145
147 template <class LF_ = LF>
148 auto makeDerivative() const
149 -> CoarsenedLocalFunction<ES,TYPEOF(derivative(std::declval<LF_ const&>()))>
150 {
152 entitySet_, derivative(localFct_)};
153
154 // bind derivative if this is bound
155 if (bound())
156 df.bind(*element_);
157 return df;
158 }
159
160 protected:
161 // Construct the coordinate mapping
162 Mapping makeMapping(Element const& coarse, Element fine) const
163 {
164 Mapping map = [](Domain const& x) { return x; };
165 while (coarse != fine && fine.hasFather()) {
166 map = [map, geo=fine.geometryInFather()](Domain const& x) {
167 return map(geo.local(x));
168 };
169 fine = fine.father();
170 }
171 return map;
172 }
173
174 private:
175 ES const& entitySet_;
176 mutable LF localFct_;
177 int maxLevel_;
178 std::optional<Element> element_;
179
180 std::vector<ChildElement> childs_;
181 };
182
183
185
189 CoarsenedGridFunction(GV const& gridView, GF const& gridFct)
190 : gridView_{gridView}
191 , entitySet_{gridView}
192 , gridFct_{gridFct}
193 {}
194
195 CoarsenedGridFunction(GV const& gridView, GF&& gridFct)
196 : gridView_{gridView}
197 , entitySet_{gridView}
198 , gridFct_{std::move(gridFct)}
199 {}
200
202 Range operator()(Domain const& x) const
203 {
204 return gridFct_(x);
205 }
206
208 template <class GF_ = GF>
209 auto makeDerivative() const
211 TYPEOF(derivative(Dune::resolveRef(std::declval<GF_ const&>())))>
212 {
213 return {gridView_, derivative(Dune::resolveRef(gridFct_))};
214 }
215
217 auto makeLocalFunction() const
218 {
219 using LF = TYPEOF(localFunction(Dune::resolveRef(gridFct_)));
220 using LocalFunction = CoarsenedLocalFunction<EntitySet,LF>;
221 return LocalFunction{gridFct_.entitySet(),
222 localFunction(Dune::resolveRef(gridFct_)),
223 gridView_.grid().maxLevel()};
224 }
225
227 EntitySet const& entitySet () const
228 {
229 return entitySet_;
230 }
231
232private:
233 GV gridView_;
234 EntitySet entitySet_;
235 GF gridFct_;
236};
237
238} // end namespace AMDiS
Definition CoarsenedGridFunction.hpp:53
auto makeDerivative() const -> CoarsenedLocalFunction< ES, TYPEOF(derivative(std::declval< LF_ const & >()))>
Construct a derivative by wrapping the derivative of the wrapped local-function.
Definition CoarsenedGridFunction.hpp:148
bool bound() const
Check whether the LocalFunction is bound to an element.
Definition CoarsenedGridFunction.hpp:111
Range operator()(Domain const &x) const
Evaluate LocalFunction at bound element.
Definition CoarsenedGridFunction.hpp:117
void bind(Element const &element)
Bind the wrapped local-function to grid element.
Definition CoarsenedGridFunction.hpp:85
CoarsenedLocalFunction(ES const &entitySet, LF const &localFct, int maxLevel)
Constructor. Stores the localFct by value.
Definition CoarsenedGridFunction.hpp:67
Element const & localContext() const
Return the element this LocalFunction is bound to.
Definition CoarsenedGridFunction.hpp:140
void unbind()
Unbind the wrapped local-function.
Definition CoarsenedGridFunction.hpp:105
A grid function defining data on a coarser entity level than it can be bound to.
Definition CoarsenedGridFunction.hpp:31
std::invoke_result_t< GF, Domain > Range
The result type of the function.
Definition CoarsenedGridFunction.hpp:40
auto makeDerivative() const -> CoarsenedGridFunction< GV, TYPEOF(derivative(Dune::resolveRef(std::declval< GF_ const & >())))>
Construct a derivative by wrapping the derivative of the wrapped grid-function.
Definition CoarsenedGridFunction.hpp:209
typename EntitySet::GlobalCoordinate Domain
The global coordinates.
Definition CoarsenedGridFunction.hpp:37
Range operator()(Domain const &x) const
Evaluate the wrapped grid-function.
Definition CoarsenedGridFunction.hpp:202
typename EntitySet::Element Element
Type of the grid element the LocalFunction can be bound to.
Definition CoarsenedGridFunction.hpp:46
auto makeLocalFunction() const
Construct local function from a DiscreteGlobalBasisFunction.
Definition CoarsenedGridFunction.hpp:217
EntitySet const & entitySet() const
Get associated EntitySet.
Definition CoarsenedGridFunction.hpp:227
typename EntitySet::LocalCoordinate LocalDomain
The local coordinates.
Definition CoarsenedGridFunction.hpp:43
CoarsenedGridFunction(GV const &gridView, GF const &gridFct)
Constructor.
Definition CoarsenedGridFunction.hpp:189
Dune::Functions::GridViewEntitySet< GV, 0 > EntitySet
The set of entities this function can be evaluated on.
Definition CoarsenedGridFunction.hpp:34