AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
ConstantGridFunction.hpp
1#pragma once
2
3#include <functional>
4#include <type_traits>
5
6#include <dune/common/diagonalmatrix.hh>
7#include <dune/common/fmatrix.hh>
8#include <dune/common/fvector.hh>
9
10#include <amdis/common/DerivativeTraits.hpp>
11#include <amdis/common/TypeTraits.hpp>
12#include <amdis/gridfunctions/AnalyticGridFunction.hpp>
13#include <amdis/gridfunctions/GridFunction.hpp>
14
15namespace AMDiS
16{
17#ifndef DOXYGEN
18 template <class Signature, class LocalContext, class Function>
20#endif
21
23 template <class R, class D, class LC, class T>
24 class ConstantLocalFunction<R(D), LC, T>
25 {
26 public:
28 using Domain = D;
29
31 using Range = R;
32
34 enum { hasDerivative = true };
35
36 private:
37 using LocalContext = LC;
38 using Geometry = typename LC::Geometry;
39
40 public:
42 ConstantLocalFunction(T const& value)
43 : value_(value)
44 {}
45
46 void bind(LocalContext const& element)
47 {
48 localContext_.emplace(element);
49 }
50
51 void unbind() { /* do nothing */ }
52
54 bool bound() const
55 {
56 return !!localContext_;
57 }
58
59 LocalContext const& localContext() const
60 {
61 assert( !!localContext_ );
62 return *localContext_;
63 }
64
66 Range const& operator()(Domain const& /*local*/) const
67 {
68 return value_;
69 }
70
73 template <class Type>
74 auto makeDerivative(Type const& /*type*/) const
75 {
76 using RawSignature = typename Dune::Functions::SignatureTraits<R(D)>::RawSignature;
77 using DerivativeRange = typename DerivativeTraits<RawSignature,Type>::Range;
78 DerivativeRange diff(0);
79 auto df = ConstantLocalFunction<DerivativeRange(D), LC, DerivativeRange>{diff};
80 // bind derivative if this is bound
81 if (bound())
82 df.bind(*localContext_);
83 return df;
84 }
85
87 int order() const
88 {
89 return 0;
90 }
91
92 private:
93 T value_;
94 std::optional<LocalContext> localContext_;
95 };
96
97
99
107 template <class T, class GridView>
109 {
110 public:
111 using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
112 using Domain = typename EntitySet::GlobalCoordinate;
113 using Range = Underlying_t<T>;
114
115 enum { hasDerivative = false };
116
117 private:
118 using Element = typename EntitySet::Element;
119 using LocalDomain = typename EntitySet::LocalCoordinate;
120 using LocalFunction = ConstantLocalFunction<Range(LocalDomain), Element, T>;
121
122 public:
124 ConstantGridFunction(T const& value, GridView const& gridView)
125 : value_(value)
126 , entitySet_(gridView)
127 {}
128
130 Range const& operator()(Domain const& /*x*/) const
131 {
132 return value_;
133 }
134
135 EntitySet const& entitySet() const
136 {
137 return entitySet_;
138 }
139
142 {
143 return {value_};
144 }
145
146 private:
147 T value_;
148 EntitySet entitySet_;
149 };
150
151
152 namespace Concepts
153 {
158 namespace Definition
159 {
160 template <class T>
162 : std::is_arithmetic<T> {};
163
164 template <class T>
165 struct ConstantToGridFunction<std::reference_wrapper<T>>
167
168 template <class T, int N>
169 struct ConstantToGridFunction<Dune::FieldVector<T, N>>
171
172 template <class T, int N, int M>
173 struct ConstantToGridFunction<Dune::FieldMatrix<T, N, M>>
175
176 template <class T, int N>
177 struct ConstantToGridFunction<Dune::DiagonalMatrix<T, N>>
179
180 } // end namespace Definition
181
182
186 template <class T>
187 constexpr bool ConstantToGridFunction =
189
192 } // end namespace Concepts
193
194
195 template <class Value>
196 struct GridFunctionCreator<Value, std::enable_if_t<Concepts::ConstantToGridFunction<Value>>>
197 {
198 template <class GridView>
199 static auto create(Value const& value, GridView const& gridView)
200 {
201 return ConstantGridFunction<Value,GridView>{value, gridView};
202 }
203 };
204
205} // end namespace AMDiS
Gridfunction returning a constant value.
Definition: ConstantGridFunction.hpp:109
LocalFunction makeLocalFunction() const
Create an ConstantLocalFunction with the stores value_.
Definition: ConstantGridFunction.hpp:141
Range const & operator()(Domain const &) const
Return the constant value_
Definition: ConstantGridFunction.hpp:130
ConstantGridFunction(T const &value, GridView const &gridView)
Constructor. Stores the function fct and creates an EntitySet.
Definition: ConstantGridFunction.hpp:124
D Domain
The LocalDomain this LocalFunction can be evaluated in.
Definition: ConstantGridFunction.hpp:28
bool bound() const
Check whether this localfunction is bound to an element.
Definition: ConstantGridFunction.hpp:54
Range const & operator()(Domain const &) const
Return the constant value_.
Definition: ConstantGridFunction.hpp:66
R Range
The range type of the LocalFunction.
Definition: ConstantGridFunction.hpp:31
int order() const
Return the constant polynomial order 0.
Definition: ConstantGridFunction.hpp:87
auto makeDerivative(Type const &) const
Create a ConstantLocalFunction representing the derivative of a constant function,...
Definition: ConstantGridFunction.hpp:74
ConstantLocalFunction(T const &value)
Constructor. Stores the constant value.
Definition: ConstantGridFunction.hpp:42
Definition: ConstantGridFunction.hpp:19
constexpr bool ConstantToGridFunction
Concepts that is true for all ''simple'' types that can be converted automatically to a GridFunction,...
Definition: ConstantGridFunction.hpp:187
Definition: ConstantGridFunction.hpp:162
Definition: DerivativeTraits.hpp:29
Definition: GridFunction.hpp:97