dune-functions 2.8.0
Loading...
Searching...
No Matches
discreteglobalbasisfunction.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_FUNCTIONS_GRIDFUNCTIONS_DISCRETEGLOBALBASISFUNCTIONS_HH
4#define DUNE_FUNCTIONS_GRIDFUNCTIONS_DISCRETEGLOBALBASISFUNCTIONS_HH
5
6#include <memory>
7#include <optional>
8
10
12
19
20namespace Dune {
21namespace Functions {
22
23
24
67template<typename B, typename V,
68 typename NTRE = HierarchicNodeToRangeMap,
69 typename R = typename V::value_type>
71{
72public:
73 using Basis = B;
74 using Vector = V;
75
76 // In order to make the cache work for proxy-references
77 // we have to use AutonomousValue<T> instead of std::decay_t<T>
79
80 using GridView = typename Basis::GridView;
82 using Tree = typename Basis::LocalView::Tree;
83 using NodeToRangeEntry = NTRE;
84
86 using Range = R;
87
89 using Element = typename EntitySet::Element;
90
91 using Traits = Imp::GridFunctionTraits<Range(Domain), EntitySet, DefaultDerivativeTraits, 16>;
92
94 {
95 using LocalView = typename Basis::LocalView;
96 using size_type = typename Tree::size_type;
97
98 template<class Node>
99 using LocalBasisRange = typename Node::FiniteElement::Traits::LocalBasisType::Traits::RangeType;
100
101 template<class Node>
102 using NodeData = typename std::vector<LocalBasisRange<Node>>;
103
104 using PerNodeEvaluationBuffer = typename TypeTree::TreeContainer<NodeData,Tree>;
105
106 public:
107
112
114 : globalFunction_(&globalFunction)
115 , localView_(globalFunction.basis().localView())
116 , evaluationBuffer_(localView_.tree())
117 , bound_(false)
118 {
119 localDoFs_.reserve(localView_.maxSize());
120 }
121
123 : globalFunction_(other.globalFunction_)
124 , localView_(other.localView_)
125 , evaluationBuffer_(localView_.tree())
126 , bound_(other.bound_)
127 {
128 localDoFs_.reserve(localView_.maxSize());
129 if (bound())
130 localDoFs_ = other.localDoFs_;
131 }
132
134 {
135 globalFunction_ = other.globalFunction_;
136 localView_ = other.localView_;
137 bound_ = other.bound_;
138 if (bound())
139 localDoFs_ = other.localDoFs_;
140 return *this;
141 }
142
149 void bind(const Element& element)
150 {
151 localView_.bind(element);
152 bound_ = true;
153 // Use cache of full local view size. For a subspace basis,
154 // this may be larger than the number of local DOFs in the
155 // tree. In this case only cache entries associated to local
156 // DOFs in the subspace are filled. Cache entries associated
157 // to local DOFs which not contained in the subspace will not
158 // be touched.
159 //
160 // Alternatively one could use a cache that exactly fits
161 // the size of the tree. However, this would require to
162 // subtract an offset from localIndex(i) on each cache
163 // access in operator().
164 localDoFs_.resize(localView_.size());
165 for (size_type i = 0; i < localView_.tree().size(); ++i)
166 {
167 // For a subspace basis the index-within-tree i
168 // is not the same as the localIndex withn the
169 // full local view.
170 size_t localIndex = localView_.tree().localIndex(i);
171 localDoFs_[localIndex] = globalFunction_->dofs()[localView_.index(localIndex)];
172 }
173 }
174
175 void unbind()
176 {
177 localView_.unbind();
178 bound_ = false;
179 }
180
184 bool bound() const {
185 return bound_;
186 }
187
197 Range operator()(const Domain& x) const
198 {
199 auto y = Range(0);
200
201 TypeTree::forEachLeafNode(localView_.tree(), [&](auto&& node, auto&& treePath) {
202 const auto& nodeToRangeEntry = globalFunction_->nodeToRangeEntry();
203 const auto& fe = node.finiteElement();
204 const auto& localBasis = fe.localBasis();
205 auto& shapeFunctionValues = evaluationBuffer_[treePath];
206
207 localBasis.evaluateFunction(x, shapeFunctionValues);
208
209 // Get range entry associated to this node
210 auto re = flatVectorView(nodeToRangeEntry(node, treePath, y));
211
212 for (size_type i = 0; i < localBasis.size(); ++i)
213 {
214 // Get coefficient associated to i-th shape function
215 auto c = flatVectorView(localDoFs_[node.localIndex(i)]);
216
217 // Get value of i-th shape function
218 auto v = flatVectorView(shapeFunctionValues[i]);
219
220 // Notice that the range entry re, the coefficient c, and the shape functions
221 // value v may all be scalar, vector, matrix, or general container valued.
222 // The matching of their entries is done via the multistage procedure described
223 // in the class documentation of DiscreteGlobalBasisFunction.
224 auto&& dimC = c.size();
225 auto dimV = v.size();
226 assert(dimC*dimV == re.size());
227 for(size_type j=0; j<dimC; ++j)
228 {
229 auto&& c_j = c[j];
230 for(size_type k=0; k<dimV; ++k)
231 re[j*dimV + k] += c_j*v[k];
232 }
233 }
234 });
235
236 return y;
237 }
238
239 const Element& localContext() const
240 {
241 return localView_.element();
242 }
243
245 {
246 DUNE_THROW(NotImplemented,"not implemented");
247 }
248
249 private:
250
251 const DiscreteGlobalBasisFunction* globalFunction_;
252 LocalView localView_;
253 mutable PerNodeEvaluationBuffer evaluationBuffer_;
254 bool bound_ = false;
255 std::vector<Coefficient> localDoFs_;
256 };
257
258 template<class B_T, class V_T, class NTRE_T>
259 DiscreteGlobalBasisFunction(B_T && basis, V_T && coefficients, NTRE_T&& nodeToRangeEntry) :
260 entitySet_(basis.gridView()),
261 basis_(wrap_or_move(std::forward<B_T>(basis))),
262 coefficients_(wrap_or_move(std::forward<V_T>(coefficients))),
263 nodeToRangeEntry_(wrap_or_move(std::forward<NTRE_T>(nodeToRangeEntry)))
264 {}
265
267 entitySet_(basis->gridView()),
268 basis_(basis),
269 coefficients_(coefficients),
270 nodeToRangeEntry_(nodeToRangeEntry)
271 {}
272
273 const Basis& basis() const
274 {
275 return *basis_;
276 }
277
278 const V& dofs() const
279 {
280 return *coefficients_;
281 }
282
284 {
285 return *nodeToRangeEntry_;
286 }
287
288 // TODO: Implement this using hierarchic search
289 Range operator() (const Domain& x) const
290 {
291 DUNE_THROW(NotImplemented,"not implemented");
292 }
293
295 {
296 DUNE_THROW(NotImplemented,"not implemented");
297 }
298
315 {
316 return LocalFunction(t);
317 }
318
322 const EntitySet& entitySet() const
323 {
324 return entitySet_;
325 }
326
327private:
328
329 EntitySet entitySet_;
331 std::shared_ptr<const V> coefficients_;
333};
334
335
336
347template<typename... TT>
349
350
351
352template<typename R, typename B, typename V>
353auto makeDiscreteGlobalBasisFunction(B&& basis, V&& vector)
354{
355 using Basis = std::decay_t<B>;
356 using NTREM = HierarchicNodeToRangeMap;
357
358 // Small helper functions to wrap vectors using istlVectorBackend
359 // if they do not already satisfy the VectorBackend interface.
360 auto toConstVectorBackend = [&](auto&& v) -> decltype(auto) {
361 if constexpr (models<Concept::ConstVectorBackend<Basis>, decltype(v)>()) {
363 } else {
364 return istlVectorBackend(v);
365 }
366 };
367
370 std::forward<B>(basis),
371 toConstVectorBackend(std::forward<V>(vector)),
373}
374
375
376
377} // namespace Functions
378} // namespace Dune
379
380#endif // DUNE_FUNCTIONS_GRIDFUNCTIONS_DISCRETEGLOBALBASISFUNCTIONS_HH
void forEachLeafNode(Tree &&tree, LeafFunc &&leafFunc)
friend LocalFunction localFunction(const DiscreteGlobalBasisFunction &t)
Construct local function from a DiscreteGlobalBasisFunction.
Definition discreteglobalbasisfunction.hh:314
void localFunction(DiscreteGlobalBasisFunction< TT... > &&t)=delete
Construction of local functions from a temporary DiscreteGlobalBasisFunction (forbidden)
auto istlVectorBackend(Vector &v)
Return a vector backend wrapping non-const ISTL like containers.
Definition istlvectorbackend.hh:345
STL namespace.
auto makeDiscreteGlobalBasisFunction(B &&basis, V &&vector)
Definition discreteglobalbasisfunction.hh:353
int size() const
typename AutonomousValueType< T >::type AutonomousValue
virtual void operator()()=0
#define DUNE_THROW(E, m)
auto wrap_or_move(T &&t)
Default implementation for derivative traits.
Definition defaultderivativetraits.hh:37
A simple node to range map using the nested tree indices.
Definition hierarchicnodetorangemap.hh:30
A grid function induced by a global basis and a coefficient vector.
Definition discreteglobalbasisfunction.hh:71
Dune::AutonomousValue< decltype(std::declval< Vector >()[std::declval< typename Basis::MultiIndex >()])> Coefficient
Definition discreteglobalbasisfunction.hh:78
Imp::GridFunctionTraits< Range(Domain), EntitySet, DefaultDerivativeTraits, 16 > Traits
Definition discreteglobalbasisfunction.hh:91
DiscreteGlobalBasisFunction(B_T &&basis, V_T &&coefficients, NTRE_T &&nodeToRangeEntry)
Definition discreteglobalbasisfunction.hh:259
friend Traits::DerivativeInterface derivative(const DiscreteGlobalBasisFunction &t)
Definition discreteglobalbasisfunction.hh:294
const EntitySet & entitySet() const
Get associated EntitySet.
Definition discreteglobalbasisfunction.hh:322
DiscreteGlobalBasisFunction(std::shared_ptr< const Basis > basis, std::shared_ptr< const V > coefficients, std::shared_ptr< const NodeToRangeEntry > nodeToRangeEntry)
Definition discreteglobalbasisfunction.hh:266
typename Basis::GridView GridView
Definition discreteglobalbasisfunction.hh:80
const Basis & basis() const
Definition discreteglobalbasisfunction.hh:273
GridViewEntitySet< GridView, 0 > EntitySet
Definition discreteglobalbasisfunction.hh:81
typename EntitySet::LocalCoordinate LocalDomain
Definition discreteglobalbasisfunction.hh:88
typename EntitySet::Element Element
Definition discreteglobalbasisfunction.hh:89
B Basis
Definition discreteglobalbasisfunction.hh:73
typename EntitySet::GlobalCoordinate Domain
Definition discreteglobalbasisfunction.hh:85
const NodeToRangeEntry & nodeToRangeEntry() const
Definition discreteglobalbasisfunction.hh:283
R Range
Definition discreteglobalbasisfunction.hh:86
V Vector
Definition discreteglobalbasisfunction.hh:74
typename Basis::LocalView::Tree Tree
Definition discreteglobalbasisfunction.hh:82
NTRE NodeToRangeEntry
Definition discreteglobalbasisfunction.hh:83
const V & dofs() const
Definition discreteglobalbasisfunction.hh:278
Definition discreteglobalbasisfunction.hh:94
LocalFunction & operator=(const LocalFunction &other)
Definition discreteglobalbasisfunction.hh:133
GlobalFunction::Element Element
Definition discreteglobalbasisfunction.hh:111
void bind(const Element &element)
Bind LocalFunction to grid element.
Definition discreteglobalbasisfunction.hh:149
bool bound() const
Check if LocalFunction is already bound to an element.
Definition discreteglobalbasisfunction.hh:184
LocalDomain Domain
Definition discreteglobalbasisfunction.hh:109
GlobalFunction::Range Range
Definition discreteglobalbasisfunction.hh:110
friend Traits::LocalFunctionTraits::DerivativeInterface derivative(const LocalFunction &t)
Definition discreteglobalbasisfunction.hh:244
void unbind()
Definition discreteglobalbasisfunction.hh:175
LocalFunction(const DiscreteGlobalBasisFunction &globalFunction)
Definition discreteglobalbasisfunction.hh:113
Range operator()(const Domain &x) const
Evaluate LocalFunction at bound element.
Definition discreteglobalbasisfunction.hh:197
LocalFunction(const LocalFunction &other)
Definition discreteglobalbasisfunction.hh:122
const Element & localContext() const
Definition discreteglobalbasisfunction.hh:239
Definition gridfunction.hh:32
GridView::template Codim< codim >::Entity Element
Type of Elements contained in this EntitySet.
Definition gridviewentityset.hh:32
Element::Geometry::LocalCoordinate LocalCoordinate
Type of local coordinates with respect to the Element.
Definition gridviewentityset.hh:35
Element::Geometry::GlobalCoordinate GlobalCoordinate
Definition gridviewentityset.hh:36
T reserve(T... args)
T resize(T... args)