Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_CURVED_SURFACE_GRID_ANALYTIC_DISCRETEFUNCTION_HH
#define DUNE_CURVED_SURFACE_GRID_ANALYTIC_DISCRETEFUNCTION_HH
#include <optional>
#include <type_traits>
#include <utility>
#include <dune/common/typeutilities.hh>
#include <dune/curvedsurfacegrid/gridfunctions/discretegridviewfunction.hh>
#include <dune/curvedsurfacegrid/gridfunctions/gridentityset.hh>
#include <dune/localfunctions/lagrange/lfecache.hh>
namespace Dune
{
//! LocalFunction associated to the \ref AnalyticDiscreteFunction
/**
* \tparam LocalContext Context this localFunction can be bound to, e.g. a grid element
* \tparam F Type of a function that can be evaluated in global coordinates.
* \tparam derivativeOrder Order of the derivative of the functor to return on evaluation
**/
template< class LocalContext, class F, int derivativeOrder = 0 >
class LocalAnalyticDiscreteFunction;
//! Generator for \ref LocalAnalyticDiscreteFunction
/**
* \param ff Function that can be evaluated at global coordinates of the \ref LocalContext
* \param order Polynomial order of the approximation of ff
* \tparam LocalContext Context this localFunction can be bound to, e.g. a grid element
**/
template< class LocalContext, class FF >
auto localAnalyticDiscreteFunction (FF&& ff, int order)
{
using F = std::decay_t<FF>;
return LocalAnalyticDiscreteFunction<LocalContext, F>{std::forward<FF>(ff), order};
}
template< class LC, class Functor, int derivativeOrder >
class LocalAnalyticDiscreteFunction
{
public:
using LocalContext = LC;
using Geometry = typename LocalContext::Geometry;
using ctype = typename Geometry::ctype;
using Domain = typename Geometry::GlobalCoordinate;
using RangeType = std::result_of_t<Functor(Domain)>;
template <int degree>
using DerivativeRange = typename Impl::DerivativeRangeType<RangeType(Domain), degree, Functions::DefaultDerivativeTraits>::type;
using LocalDomain = typename Geometry::LocalCoordinate;
using Range = DerivativeRange<derivativeOrder>;
using Signature = Range(LocalDomain);
public:
//! Constructor. Stores the functor f by value
template< class FF >
LocalAnalyticDiscreteFunction (FF&& ff, int order)
: f_(std::forward<FF>(ff))
, order_(order)
, cache_(order)
{}
//! bind the LocalFunction to the local context
/**
* Stores the localContext and its geometry in a cache variable
**/
void bind (const LocalContext& localContext)
{
localContext_.emplace(localContext);
geometry_.emplace(localContext.geometry());
lfe_ = &cache_.get(localContext_->type());
lfe_->localInterpolation().interpolate([&](auto const& local)
{
return f_(geometry_->global(local));
}, coefficients_);
}
//! unbind the localContext from the localFunction
/**
* Reset the geometry
**/
void unbind ()
{
lfe_ = nullptr;
geometry_.reset();
localContext_.reset();
}
//! evaluate the stored function in local coordinates
/**
* Transform the local coordinates to global coordinates first,
* then evalute the stored functor.
**/
Range operator() (const LocalDomain& local) const
{
static_assert(derivativeOrder < 2, "Higher-order derivatives not implemented");
if constexpr (derivativeOrder == 0)
return evaluateFunction(local);
else if constexpr (derivativeOrder == 1)
return evaluateJacobian(local);
}
friend LocalAnalyticDiscreteFunction<LC,Functor,derivativeOrder+1> derivative (LocalAnalyticDiscreteFunction const& lf)
{
return {lf.f_, lf.order_};
}
//! return the bound localContext.
const LocalContext& localContext () const
{
assert(!!localContext_);
return *localContext_;
}
//! obtain the functor
const Functor& f () const
{
return f_;
}
private:
DerivativeRange<0> evaluateFunction (const LocalDomain& local) const
{
assert(!!lfe_);
auto const& localBasis = lfe_->localBasis();
localBasis.evaluateFunction(local, shapeValues_);
assert(coefficients_.size() == shapeValues_.size());
DerivativeRange<0> nh(0);
for (std::size_t i = 0; i < shapeValues_.size(); ++i)
nh.axpy(shapeValues_[i], coefficients_[i]);
return nh;
}
DerivativeRange<1> evaluateJacobian (const LocalDomain& local) const
{
assert(!!geometry_);
assert(!!lfe_);
auto const& localBasis = lfe_->localBasis();
// evaluate basis functions in local coordinate
localBasis.evaluateJacobian(local, shapeGradients_);
assert(coefficients_.size() == shapeGradients_.size());
// transform gradients to global coordinates
auto jit = geometry_->jacobianInverseTransposed(local);
gradients_.resize(shapeGradients_.size());
for (std::size_t i = 0; i < shapeGradients_.size(); ++i)
jit.mv(shapeGradients_[i][0], gradients_[i]);
DerivativeRange<1> J(0);
for (std::size_t i = 0; i < coefficients_.size(); ++i)
for (std::size_t j = 0; j < J.N(); ++j)
J[j].axpy(coefficients_[i][j], gradients_[i]);
return J;
}
private:
Functor f_;
int order_;
using LFECache = LagrangeLFECache<ctype,ctype,Geometry::mydimension>;
LFECache cache_;
using LFE = typename LFECache::FiniteElementType;
LFE const* lfe_ = nullptr;
std::vector<FieldVector<ctype,Geometry::coorddimension>> coefficients_;
using LB = typename LFE::Traits::LocalBasisType;
mutable std::vector<typename LB::Traits::RangeType> shapeValues_;
mutable std::vector<typename LB::Traits::JacobianType> shapeGradients_;
mutable std::vector<FieldVector<ctype,Geometry::coorddimension>> gradients_;
// some caches
std::optional<LocalContext> localContext_;
std::optional<Geometry> geometry_;
};
//! GridFunction associated to the mapping F
/**
* \tparam Grid The grid type with elements the corresponding LocalFunction can be bound to
* \tparam F Type of a function that can be evaluated in global coordinates.
* \tparam ORDER Polynomial order of the local lagrange basis functions (optional)
**/
template< class Grid, class F, int ORDER = -1 >
class AnalyticDiscreteFunction;
//! Generator for \ref AnalyticDiscreteFunction
/**
* \param ff Function that can be evaluated at global coordinates of the \ref Grid
* \tparam Grid The grid type with elements the corresponding LocalFunction can be bound to
**/
template< class FF, class Grid >
auto analyticDiscreteFunction (FF&& ff, Grid const& /*grid*/, int order)
{
using F = std::decay_t<FF>;
return AnalyticDiscreteFunction<Grid, F>{std::forward<FF>(ff), order};
}
template< int ORDER, class FF, class Grid >
auto analyticDiscreteFunction (FF&& ff, Grid const& /*grid*/)
{
using F = std::decay_t<FF>;
return AnalyticDiscreteFunction<Grid, F, ORDER>{std::forward<FF>(ff)};
}
template< class GridType, class Functor, int ORDER >
class AnalyticDiscreteFunction
{
public:
using Grid = GridType;
using EntitySet = GridEntitySet<Grid,0>;
using Domain = typename EntitySet::GlobalCoordinate;
using Range = std::result_of_t<Functor(Domain)>;
using Signature = Range(Domain);
public:
//! Constructor. Stores the functor f by value
template< class FF,
disableCopyMove<AnalyticDiscreteFunction, FF> = 0>
AnalyticDiscreteFunction (FF&& ff, int order = ORDER)
: f_(std::forward<FF>(ff))
, order_(order)
{}
//! evaluate the stored function in global coordinates
Range operator() (const Domain& x) const
{
return f_(x);
}
//! construct the \ref LocalAnalyticDiscreteFunction
using LocalFunction = LocalAnalyticDiscreteFunction<typename EntitySet::Element, Functor>;
friend LocalFunction localFunction (const AnalyticDiscreteFunction& t)
{
return LocalFunction(t.f_, t.order_);
}
//! obtain the stored \ref GridEntitySet
const EntitySet& entitySet () const
{
return entitySet_;
}
//! obtain the functor
const Functor& f () const
{
return f_;
}
private:
Functor f_;
int order_;
EntitySet entitySet_;
};
} // end namespace Dune
#endif // DUNE_CURVED_SURFACE_GRID_ANALYTIC_DISCRETEFUNCTION_HH