AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
ElementGridFunction.hpp
1#pragma once
2
3#include <functional>
4#include <type_traits>
5
6#include <dune/common/exceptions.hh>
7#include <dune/functions/gridfunctions/gridviewentityset.hh>
8
9#include <amdis/ElementVector.hpp>
10#include <amdis/common/DerivativeTraits.hpp>
11#include <amdis/common/TypeTraits.hpp>
12#include <amdis/gridfunctions/ConstantGridFunction.hpp>
13#include <amdis/gridfunctions/GridFunction.hpp>
14
15namespace AMDiS
16{
17#ifndef DOXYGEN
18 template <class Signature, class IndexSet, class Container>
20#endif
21
23 template <class R, class D, class IndexSet, class Container>
24 class ElementLocalFunction<R(D), IndexSet, Container>
25 {
26 public:
28 using Domain = D;
29
31 using Range = R;
32
34 enum { hasDerivative = true };
35
37 using Element = typename IndexSet::template Codim<0>::Entity;
38
39 public:
41 ElementLocalFunction(IndexSet const& indexSet, Container const& container)
42 : indexSet_(&indexSet)
43 , container_(&container)
44 {}
45
47 Element const& localContext() const
48 {
49 assert(!!element_);
50 return *element_;
51 }
52
54 void bind(Element const& element)
55 {
56 element_.emplace(element);
57 value_ = (*container_)[indexSet_->index(element)];
58 }
59
60 void unbind() {}
61
63 bool bound() const
64 {
65 return !!element_;
66 }
67
69 Range const& operator()(Domain const& /*local*/) const
70 {
71 return value_;
72 }
73
76 template <class Type>
77 auto makeDerivative(Type const& /*type*/) const
78 {
79 using RawSignature = typename Dune::Functions::SignatureTraits<R(D)>::RawSignature;
80 using DerivativeRange = typename DerivativeTraits<RawSignature,Type>::Range;
81 DerivativeRange diff(0);
82 ConstantLocalFunction<DerivativeRange(D), Element, DerivativeRange> df{diff};
83 if (bound())
84 df.bind(*element_);
85 return df;
86 }
87
89 int order() const
90 {
91 return 0;
92 }
93
94 private:
95 IndexSet const* indexSet_;
96 Container const* container_;
97 std::optional<Element> element_;
98 Range value_ = 0;
99 };
100
101
103
110 template <class GridView, class Container>
112 {
113 public:
114 using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
115 using Domain = typename EntitySet::GlobalCoordinate;
116 using Range = typename Container::value_type;
117
118 enum { hasDerivative = false };
119
120 private:
121 using LocalDomain = typename EntitySet::LocalCoordinate;
122 using LocalFunction = ElementLocalFunction<Range(LocalDomain), typename GridView::IndexSet, Container>;
123
124 public:
126 ElementGridFunction(GridView const& gridView, Container const& container)
127 : entitySet_(gridView)
128 , container_(&container)
129 {}
130
131 template <class G, class T,
132 std::enable_if_t<std::is_same_v<typename G::LeafGridView,GridView>, int> = 0,
133 std::enable_if_t<std::is_same_v<typename ElementVector<G,T>::Data, Container>, int> = 0>
134 ElementGridFunction(ElementVector<G,T> const& elementVector)
135 : entitySet_(elementVector.gridView())
136 , container_(&elementVector.data())
137 {}
138
140 Range operator()(Domain const& /*x*/) const
141 {
142 DUNE_THROW(Dune::NotImplemented, "Global evaluation not yet implemented");
143 return Range(0);
144 }
145
146 EntitySet const& entitySet() const
147 {
148 return entitySet_;
149 }
150
153 {
154 return {entitySet_.gridView().indexSet(), *container_};
155 }
156
157 private:
158 EntitySet entitySet_;
159 Container const* container_;
160 };
161
162 // deduction guide
163 template <class G, class T>
164 ElementGridFunction(ElementVector<G,T> const&)
165 -> ElementGridFunction<typename G::LeafGridView, typename ElementVector<G,T>::Data>;
166
168 template <class G, class T>
169 auto valueOf(ElementVector<G,T> const& ev)
170 {
171 return ElementGridFunction{ev.gridView(), ev.data()};
172 }
173
174} // end namespace AMDiS
Definition: ConstantGridFunction.hpp:19
Gridfunction returning a constant value per element.
Definition: ElementGridFunction.hpp:112
LocalFunction makeLocalFunction() const
Create an ElementLocalFunction.
Definition: ElementGridFunction.hpp:152
ElementGridFunction(GridView const &gridView, Container const &container)
Constructor. Stores the function fct and creates an EntitySet.
Definition: ElementGridFunction.hpp:126
Range operator()(Domain const &) const
Return the constant value_
Definition: ElementGridFunction.hpp:140
D Domain
The LocalDomain this LocalFunction can be evaluated in.
Definition: ElementGridFunction.hpp:28
bool bound() const
Check whether this localfunction is bound to an element.
Definition: ElementGridFunction.hpp:63
typename IndexSet::template Codim< 0 >::Entity Element
Type of the entity to bind.
Definition: ElementGridFunction.hpp:37
Range const & operator()(Domain const &) const
Return the constant value_.
Definition: ElementGridFunction.hpp:69
ElementLocalFunction(IndexSet const &indexSet, Container const &container)
Constructor. Stores the constant value.
Definition: ElementGridFunction.hpp:41
R Range
The range type of the LocalFunction.
Definition: ElementGridFunction.hpp:31
void bind(Element const &element)
Extract the constant data associated to the element.
Definition: ElementGridFunction.hpp:54
int order() const
Return the constant polynomial order 0.
Definition: ElementGridFunction.hpp:89
auto makeDerivative(Type const &) const
Create a ConstantLocalFunction representing the derivative of a constant function,...
Definition: ElementGridFunction.hpp:77
Element const & localContext() const
Return the bound element.
Definition: ElementGridFunction.hpp:47
Definition: ElementGridFunction.hpp:19
An adaptive container that stores a value per grid element.
Definition: ElementVector.hpp:25
Definition: DerivativeTraits.hpp:29