Dune-Functions 2.12-git
Loading...
Searching...
No Matches
nodes.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
4// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
8#define DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
9
10#include <cassert>
11#include <memory>
12#include <vector>
13#include <array>
14#include <optional>
15
19
22
23namespace Dune {
24 namespace Functions {
25
26
27 namespace Impl {
28
29 // This class encapsulates the access to the setOffset()
30 // and setTreeIndex() methods of a node. This way we
31 // can hide the methods from the user but still provide
32 // access where this is needed.
33 struct BasisNodeSetupHelper
34 {
35
36 template<class Node, class size_type>
37 static void setSize(Node& node, const size_type size)
38 {
39 node.setSize(size);
40 }
41
42 template<class Node, class size_type>
43 static void setOffset(Node& node, const size_type offset)
44 {
45 node.setOffset(offset);
46 }
47
48 template<class Node, class size_type>
49 static void setTreeIndex(Node& node, const size_type index)
50 {
51 node.setTreeIndex(index);
52 }
53
54 };
55
56
57
58 // A mixin class for generalized child access from
59 // multiple indices or a tree path. The derived class
60 // only has to provide the child(i) method with
61 // a single index for accessing direct children.
62 template<class Impl>
63 class ChildAccessMixIn
64 {
65
66 Impl& asImpl()
67 {
68 return static_cast<Impl&>(*this);
69 }
70
71 const Impl& asImpl() const
72 {
73 return static_cast<const Impl&>(*this);
74 }
75
76 public:
77
83 template<class... II>
84 const auto& child(II... ii) const
85 requires (sizeof...(II) != 1)
86 {
87 return Dune::TypeTree::child(asImpl(), ii...);
88 }
89
96 template<class... II>
97 auto& child(II... ii)
98 requires (sizeof...(II) != 1)
99 {
100 return Dune::TypeTree::child(asImpl(), ii...);
101 }
102
108 template<class... II>
109 const auto& child(Dune::HybridMultiIndex<II...> treePath) const
110 {
111 return Dune::TypeTree::child(asImpl(), treePath);
112 }
113
119 template<class... II>
121 {
122 return Dune::TypeTree::child(asImpl(), treePath);
123 }
124
125 };
126
127 } // end namespace Impl
128
129
131 {
132
133 friend struct Impl::BasisNodeSetupHelper;
134
135 public:
136
138
140 offset_(0),
141 size_(0),
142 treeIndex_(0)
143 {}
144
146 {
147 assert(i < size_);
148 return offset_ + i;
149 }
150
159 {
160 return size_;
161 }
162
171 bool empty() const
172 {
173 return (size_ == 0);
174 }
175
182 {
183 return treeIndex_;
184 }
185
186 protected:
187
189 {
190 return offset_;
191 }
192
194 {
195 offset_ = offset;
196 }
197
199 {
200 size_ = size;
201 }
202
204 {
205 treeIndex_ = treeIndex;
206 }
207
208 private:
209
210 size_type offset_;
211 size_type size_;
212 size_type treeIndex_;
213
214 };
215
216
218 public BasisNodeMixin
219 {
220 public:
221
222 // Begin of node interface
223
224 static constexpr auto degree()
225 {
227 }
228
229 // End of node interface
230
231 };
232
233
234
235 template<typename Node, typename Element>
237 : public BasisNodeMixin
238 {
239 public:
240
241 void bind(const Element& entity)
242 {
243 Node& self = *static_cast<Node*>(this);
244 std::size_t offset = this->offset();
245 Dune::Hybrid::forEach(Dune::range(self.degree()), [&](auto i) {
246 bindTree(self.child(i), entity, offset);
247 offset += self.child(i).size();
248 });
249 this->setSize(offset - this->offset());
250 }
251
252 };
253
254
255
256 template<typename T, std::size_t n>
258 public InnerBasisNodeMixin<PowerBasisNode<T, n>, typename T::Element>,
259 public Impl::ChildAccessMixIn<PowerBasisNode<T, n>>
260 {
261 public:
262
263 // Begin of node interface
264
265 static constexpr auto degree()
266 {
268 }
269
270 template<class Index>
271 requires (std::is_convertible_v<Index, std::size_t>)
272 const auto& child(Index i) const
273 {
274 return children_[i].value();
275 }
276
277 template<class Index>
278 requires (std::is_convertible_v<Index, std::size_t>)
279 auto& child(Index i)
280 {
281 return children_[i].value();
282 }
283
284 using Impl::ChildAccessMixIn<PowerBasisNode<T, n>>::child;
285
286 // End of node interface
287
288 using Element = typename T::Element;
289
290 PowerBasisNode() = default;
291
292 const Element& element() const
293 {
294 return child(Dune::Indices::_0).element();
295 }
296
297 template<class Index, class TT>
298 void setChild(Index i, TT&& t)
299 {
300 children_[i].emplace(std::forward<TT>(t));
301 }
302
303 private:
304 std::array<std::optional<T>, n> children_;
305 };
306
307
308
309 template<typename T>
311 public InnerBasisNodeMixin<DynamicPowerBasisNode<T>, typename T::Element>,
312 public Impl::ChildAccessMixIn<DynamicPowerBasisNode<T>>
313 {
314 public:
315
316 // Begin of node interface
317
319 {
320 return children_.size();
321 }
322
323 template<class Index>
324 requires (std::is_convertible_v<Index, std::size_t>)
325 const auto& child(Index i) const
326 {
327 return children_[i].value();
328 }
329
330 template<class Index>
331 requires (std::is_convertible_v<Index, std::size_t>)
332 auto& child(Index i)
333 {
334 return children_[i].value();
335 }
336
337 using Impl::ChildAccessMixIn<DynamicPowerBasisNode<T>>::child;
338
339 // End of node interface
340
341 using Element = typename T::Element;
342
344 : children_(children)
345 {}
346
347 const Element& element() const
348 {
349 return child(Dune::Indices::_0).element();
350 }
351
352 template<class Index, class TT>
353 void setChild(Index i, TT&& t)
354 {
355 children_[i].emplace(std::forward<TT>(t));
356 }
357
358 private:
360 };
361
362
363 template<typename... T>
365 public InnerBasisNodeMixin<CompositeBasisNode<T...>, typename TypeListEntry_t<0, TypeList<T...>>::Element>,
366 public Impl::ChildAccessMixIn<CompositeBasisNode<T...>>
367 {
368 public:
369
370 // Begin of node interface
371
372 static constexpr auto degree()
373 {
374 return Dune::index_constant<sizeof...(T)>{};
375 }
376
377 template<std::size_t i>
378 const auto& child(Dune::index_constant<i> ii) const
379 {
380 return children_[ii].value();
381 }
382
383 template<std::size_t i>
385 {
386 return children_[ii].value();
387 }
388
389 using Impl::ChildAccessMixIn<CompositeBasisNode<T...>>::child;
390
391 // End of node interface
392
393 using Element = typename std::tuple_element_t<0, std::tuple<T...>>::Element;
394
396
397 explicit CompositeBasisNode(const T&... children) :
398 children_(children...)
399 {}
400
401 const Element& element() const
402 {
403 return child(Dune::Indices::_0).element();
404 }
405
406 template<std::size_t i, class TT>
407 void setChild (TT&& t, Dune::index_constant<i> ii = {})
408 {
409 children_[ii].emplace(std::forward<TT>(t));
410 }
411
412 private:
414 };
415
416
417 template<typename Tree, typename Entity>
418 void bindTree(Tree& tree, const Entity& entity, std::size_t offset = 0)
419 {
420 Impl::BasisNodeSetupHelper::setOffset(tree, offset);
421 tree.bind(entity);
422 }
423
424 template<typename Tree>
425 void initializeTree(Tree& tree, std::size_t treeIndexOffset = 0)
426 {
427 Dune::TypeTree::forEachNode(tree, [&](auto& node, const auto& treePath) {
428 Impl::BasisNodeSetupHelper::setTreeIndex(node, treeIndexOffset);
429 ++treeIndexOffset;
430 });
431 }
432
433
434 } // namespace Functions
435
436} // namespace Dune
437
438#endif // DUNE_FUNCTIONS_FUNCTIONSPACEBASES_NODES_HH
int size() const
static constexpr IntegralRange< T > range(T from, T to) noexcept
constexpr void forEach(Range &&range, F &&f)
std::ptrdiff_t index() const
decltype(auto) child(Node &&node, TreePath< Indices... > treePath)
void forEachNode(Tree &&tree, PreNodeFunc &&preNodeFunc, LeafNodeFunc &&leafNodeFunc, PostNodeFunc &&postNodeFunc)
constexpr index_constant< 0 > _0
Definition nodes.hh:131
size_type treeIndex() const
Lexicographic index that uniquely identifies the node within a tree.
Definition nodes.hh:181
size_type localIndex(size_type i) const
Definition nodes.hh:145
size_type offset() const
Definition nodes.hh:188
bool empty() const
Check if the node is empty.
Definition nodes.hh:171
size_type size() const
Obtain the number of basis function in the local node.
Definition nodes.hh:158
void setOffset(const size_type offset)
Definition nodes.hh:193
std::size_t size_type
Definition nodes.hh:137
BasisNodeMixin()
Definition nodes.hh:139
void setSize(const size_type size)
Definition nodes.hh:198
void setTreeIndex(size_type treeIndex)
Definition nodes.hh:203
static constexpr auto degree()
Definition nodes.hh:372
typename std::tuple_element_t< 0, std::tuple< T... > >::Element Element
Definition nodes.hh:393
auto & child(Dune::index_constant< i > ii)
Definition nodes.hh:384
const Element & element() const
Definition nodes.hh:401
const auto & child(Dune::index_constant< i > ii) const
Definition nodes.hh:378
void setChild(TT &&t, Dune::index_constant< i > ii={})
Definition nodes.hh:407
CompositeBasisNode(const T &... children)
Definition nodes.hh:397
const Element & element() const
Definition nodes.hh:347
void setChild(Index i, TT &&t)
Definition nodes.hh:353
const auto & child(Index i) const
Definition nodes.hh:325
DynamicPowerBasisNode(std::size_t children)
Definition nodes.hh:343
auto & child(Index i)
Definition nodes.hh:332
std::size_t degree() const
Definition nodes.hh:318
typename T::Element Element
Definition nodes.hh:341
void bind(const Element &entity)
Definition nodes.hh:241
Definition nodes.hh:219
static constexpr auto degree()
Definition nodes.hh:224
Definition nodes.hh:260
auto & child(Index i)
Definition nodes.hh:279
const Element & element() const
Definition nodes.hh:292
typename T::Element Element
Definition nodes.hh:288
const auto & child(Index i) const
Definition nodes.hh:272
static constexpr auto degree()
Definition nodes.hh:265
void setChild(Index i, TT &&t)
Definition nodes.hh:298
T emplace(T... args)
void bindTree(Tree &tree, const Entity &entity, std::size_t offset=0)
Definition nodes.hh:418
void initializeTree(Tree &tree, std::size_t treeIndexOffset=0)
Definition nodes.hh:425
T size(T... args)