Dune-Functions 2.12-git
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
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_GRIDFUNCTIONS_DISCRETEGLOBALBASISFUNCTIONS_HH
8#define DUNE_FUNCTIONS_GRIDFUNCTIONS_DISCRETEGLOBALBASISFUNCTIONS_HH
9
10#include <memory>
11#include <optional>
12
15
17
20
27
28namespace Dune {
29namespace Functions {
30
31
32namespace ImplDoc {
33
34template<typename B, typename V, typename NTRE>
36{
37public:
38 using Basis = B;
39 using Vector = V;
40
41 // In order to make the cache work for proxy-references
42 // we have to use AutonomousValue<T> instead of std::decay_t<T>
43 using Coefficient = Dune::AutonomousValue<decltype(std::declval<Vector>()[std::declval<typename Basis::MultiIndex>()])>;
44
45 using GridView = typename Basis::GridView;
47 using Tree = typename Basis::LocalView::Tree;
48 using NodeToRangeEntry = NTRE;
49
51
53 using Element = typename EntitySet::Element;
54
55protected:
56
57 // This collects all data that is shared by all related
58 // global and local functions. This way we don't need to
59 // keep track of it individually.
67
68public:
70 {
71 using LocalView = typename Basis::LocalView;
72 using size_type = typename Tree::size_type;
73
74 public:
76 using Element = typename EntitySet::Element;
77
78 protected:
80 : data_(data)
81 , localView_(data_->basis->localView())
82 {
83 localDoFs_.reserve(localView_.maxSize());
84 }
85
93 : data_(other.data_)
94 , localView_(other.localView_)
95 {
96 localDoFs_.reserve(localView_.maxSize());
97 if (bound())
98 localDoFs_ = other.localDoFs_;
99 }
100
109 {
110 data_ = other.data_;
111 localView_ = other.localView_;
112 if (bound())
113 localDoFs_ = other.localDoFs_;
114 return *this;
115 }
116
117 public:
124 void bind(const Element& element)
125 {
126 localView_.bind(element);
127 // Use cache of full local view size. For a subspace basis,
128 // this may be larger than the number of local DOFs in the
129 // tree. In this case only cache entries associated to local
130 // DOFs in the subspace are filled. Cache entries associated
131 // to local DOFs which are not contained in the subspace will
132 // not be touched.
133 //
134 // Alternatively one could use a cache that exactly fits
135 // the size of the tree. However, this would require to
136 // subtract an offset from localIndex(i) on each cache
137 // access in operator().
139 const auto& dofs = *data_->coefficients;
140 for (size_type i = 0; i < localView_.tree().size(); ++i)
141 {
142 // For a subspace basis the index-within-tree i
143 // is not the same as the localIndex within the
144 // full local view.
145 size_t localIndex = localView_.tree().localIndex(i);
146 localDoFs_[localIndex] = dofs[localView_.index(localIndex)];
147 }
148 }
149
151 void unbind()
152 {
153 localView_.unbind();
154 }
155
157 bool bound() const
158 {
159 return localView_.bound();
160 }
161
163 const Element& localContext() const
164 {
165 return localView_.element();
166 }
167
168 protected:
169
170 template<class To, class From>
171 void assignWith(To& to, const From& from) const
172 {
173 auto from_flat = flatVectorView(from);
174 auto to_flat = flatVectorView(to);
175 assert(from_flat.size() == to_flat.size());
176 for (size_type i = 0; i < to_flat.size(); ++i)
177 to_flat[i] = from_flat[i];
178 }
179
180 template<class Node, class TreePath, class Range>
181 decltype(auto) nodeToRangeEntry(const Node& node, const TreePath& treePath, Range& y) const
182 {
183 return (*data_->nodeToRangeEntry)(node, treePath, y);
184 }
185
187 LocalView localView_;
189 };
190
191protected:
193 : data_(data)
194 {
195 /* Nothing. */
196 }
197
198public:
199
201 const Basis& basis() const
202 {
203 return *data_->basis;
204 }
205
207 const Vector& dofs() const
208 {
209 return *data_->coefficients;
210 }
211
214 {
215 return *data_->nodeToRangeEntry;
216 }
217
219 const EntitySet& entitySet() const
220 {
221 return data_->entitySet;
222 }
223
224protected:
226};
227
228} // namespace ImplDoc
229
230
231
232template<typename DGBF>
234
278template<typename B, typename V,
279 typename NTRE = HierarchicNodeToRangeMap,
280 typename R = typename V::value_type>
283{
285 using Data = typename Base::Data;
286
287public:
288 using Basis = typename Base::Basis;
289 using Vector = typename Base::Vector;
290
291 using Domain = typename Base::Domain;
292 using Range = R;
293
294 using Traits = Imp::GridFunctionTraits<Range(Domain), typename Base::EntitySet, DefaultDerivativeTraits, 16>;
295
296private:
297
298 template<class Node>
299 using LocalBasisRange = typename Node::FiniteElement::Traits::LocalBasisType::Traits::RangeType;
300 template<class Node>
301 using NodeData = typename std::vector<LocalBasisRange<Node>>;
302 using PerNodeEvaluationBuffer = typename TypeTree::TreeContainer<NodeData, typename Base::Tree>;
303
304public:
307 {
308 using LocalBase = typename Base::LocalFunctionBase;
309 using size_type = typename Base::Tree::size_type;
310 using LocalBase::nodeToRangeEntry;
311
312 public:
313
315 using Domain = typename LocalBase::Domain;
317 using Element = typename LocalBase::Element;
318
321 : LocalBase(globalFunction.data_)
322 , evaluationBuffer_(this->localView_.tree())
323 {
324 /* Nothing. */
325 }
326
336 Range operator()(const Domain& x) const
337 {
338 Range y;
339 istlVectorBackend(y) = 0;
340
341 TypeTree::forEachLeafNode(this->localView_.tree(), [&](auto&& node, auto&& treePath) {
342 if (node.empty())
343 return;
344 const auto& fe = node.finiteElement();
345 const auto& localBasis = fe.localBasis();
346 auto& shapeFunctionValues = evaluationBuffer_[treePath];
347
348 localBasis.evaluateFunction(x, shapeFunctionValues);
349
350 // Compute linear combinations of basis function jacobian.
351 // Non-scalar coefficients of dimension coeffDim are handled by
352 // processing the coeffDim linear combinations independently
353 // and storing them as entries of an array.
354 using CoeffType = Dune::AutonomousValue<decltype(flatVectorView(this->localDoFs_[node.localIndex(0)])[0])>;
355 using LFERangeType = LocalBasisRange< std::decay_t<decltype(node)>>;
356 using Value = Dune::AutonomousValue<decltype(std::declval<CoeffType>()*std::declval<LFERangeType>())>;
357 static constexpr auto coeffDim = decltype(flatVectorView(this->localDoFs_[node.localIndex(0)]).size())::value;
358 auto values = std::array<Value, coeffDim>{};
359 istlVectorBackend(values) = 0;
360 for (size_type i = 0; i < localBasis.size(); ++i)
361 {
362 auto c = flatVectorView(this->localDoFs_[node.localIndex(i)]);
363 for (std::size_t j = 0; j < coeffDim; ++j)
364 values[j].axpy(c[j], shapeFunctionValues[i]);
365 }
366
367 // Assign computed values to node entry of range.
368 // Types are matched using the lexicographic ordering provided by flatVectorView.
369 LocalBase::assignWith(nodeToRangeEntry(node, treePath, y), values);
370 });
371
372 return y;
373 }
374
383
384 private:
385 mutable PerNodeEvaluationBuffer evaluationBuffer_;
386 };
387
389 template<class B_T, class V_T, class NTRE_T>
390 DiscreteGlobalBasisFunction(B_T && basis, V_T && coefficients, NTRE_T&& nodeToRangeEntry)
391 : Base(std::make_shared<Data>(Data{{basis.gridView()}, wrap_or_move(std::forward<B_T>(basis)), wrap_or_move(std::forward<V_T>(coefficients)), wrap_or_move(std::forward<NTRE_T>(nodeToRangeEntry))}))
392 {}
393
396 : Base(std::make_shared<Data>(Data{{basis->gridView()}, basis, coefficients, nodeToRangeEntry}))
397 {}
398
404 Range operator() (const Domain& x) const
405 {
406 HierarchicSearch search(this->data_->basis->gridView().grid(), this->data_->basis->gridView().indexSet());
407
408 const auto e = search.findEntity(x);
409 auto localThis = localFunction(*this);
410 localThis.bind(e);
411 return localThis(e.geometry().local(x));
412 }
413
419
429 {
430 return LocalFunction(t);
431 }
432};
433
434
459template<typename R, typename B, typename V>
460auto makeDiscreteGlobalBasisFunction(B&& basis, V&& vector)
461{
462 using Basis = std::decay_t<B>;
463 using NTREM = HierarchicNodeToRangeMap;
464
465 // Small helper functions to wrap vectors using istlVectorBackend
466 // if they do not already satisfy the VectorBackend interface.
467 auto toConstVectorBackend = [&](auto&& v) -> decltype(auto) {
468 if constexpr (models<Concept::ConstVectorBackend<Basis>, decltype(v)>()) {
469 return std::forward<decltype(v)>(v);
470 } else {
471 return istlVectorBackend(v);
472 }
473 };
474
477 std::forward<B>(basis),
478 toConstVectorBackend(std::forward<V>(vector)),
480}
481
482
497template<typename DGBF>
499 : public ImplDoc::DiscreteGlobalBasisFunctionBase<typename DGBF::Basis, typename DGBF::Vector, typename DGBF::NodeToRangeEntry>
500{
502 using Data = typename Base::Data;
503
504public:
506
507 using Basis = typename Base::Basis;
508 using Vector = typename Base::Vector;
509
510 using Domain = typename Base::Domain;
512
513 using Traits = Imp::GridFunctionTraits<Range(Domain), typename Base::EntitySet, DefaultDerivativeTraits, 16>;
514
515private:
516
517 template<class Node>
518 using LocalBasisRange = typename Node::FiniteElement::Traits::LocalBasisType::Traits::JacobianType;
519 template<class Node>
520 using NodeData = typename std::vector< LocalBasisRange<Node> >;
521 using PerNodeEvaluationBuffer = typename TypeTree::TreeContainer<NodeData, typename Base::Tree>;
522
523public:
524
534 {
535 using LocalBase = typename Base::LocalFunctionBase;
536 using size_type = typename Base::Tree::size_type;
537 using LocalBase::nodeToRangeEntry;
538
539 public:
541 using Domain = typename LocalBase::Domain;
543 using Element = typename LocalBase::Element;
544
546 LocalFunction(const GlobalFunction& globalFunction)
547 : LocalBase(globalFunction.data_)
548 , evaluationBuffer_(this->localView_.tree())
549 {
550 /* Nothing. */
551 }
552
559 void bind(const Element& element)
560 {
561 LocalBase::bind(element);
562 geometry_.emplace(element.geometry());
563 }
564
566 void unbind()
567 {
568 geometry_.reset();
569 LocalBase::unbind();
570 }
571
585 Range operator()(const Domain& x) const
586 {
587 Range y;
588 istlVectorBackend(y) = 0;
589
590 const auto& jacobianInverse = geometry_->jacobianInverse(x);
591
592 TypeTree::forEachLeafNode(this->localView_.tree(), [&](auto&& node, auto&& treePath) {
593 if (node.empty())
594 return;
595 const auto& fe = node.finiteElement();
596 const auto& localBasis = fe.localBasis();
597 auto& shapeFunctionJacobians = evaluationBuffer_[treePath];
598
599 localBasis.evaluateJacobian(x, shapeFunctionJacobians);
600
601 // Compute linear combinations of basis function jacobian.
602 // Non-scalar coefficients of dimension coeffDim are handled by
603 // processing the coeffDim linear combinations independently
604 // and storing them as entries of an array.
605 using RefJacobian = LocalBasisRange< std::decay_t<decltype(node)> >;
606 static constexpr auto coeffDim = decltype(flatVectorView(this->localDoFs_[node.localIndex(0)]).size())::value;
607 auto refJacobians = std::array<RefJacobian, coeffDim>{};
608 istlVectorBackend(refJacobians) = 0;
609 for (size_type i = 0; i < localBasis.size(); ++i)
610 {
611 auto c = flatVectorView(this->localDoFs_[node.localIndex(i)]);
612 for (std::size_t j = 0; j < coeffDim; ++j)
613 refJacobians[j].axpy(c[j], shapeFunctionJacobians[i]);
614 }
615
616 // Transform Jacobians form local to global coordinates.
617 using Jacobian = decltype(refJacobians[0] * jacobianInverse);
618 auto jacobians = std::array<Jacobian, coeffDim>{};
620 refJacobians.begin(), refJacobians.end(), jacobians.begin(),
621 [&](const auto& refJacobian) { return refJacobian * jacobianInverse; });
622
623 // Assign computed Jacobians to node entry of range.
624 // Types are matched using the lexicographic ordering provided by flatVectorView.
625 LocalBase::assignWith(nodeToRangeEntry(node, treePath, y), jacobians);
626 });
627
628 return y;
629 }
630
633 {
634 DUNE_THROW(NotImplemented, "derivative of derivative is not implemented");
635 }
636
637 private:
638 mutable PerNodeEvaluationBuffer evaluationBuffer_;
640 };
641
649 : Base(data)
650 {
651 /* Nothing. */
652 }
653
659 Range operator()(const Domain& x) const
660 {
661 HierarchicSearch search(this->data_->basis->gridView().grid(), this->data_->basis->gridView().indexSet());
662
663 const auto e = search.findEntity(x);
664 auto localThis = localFunction(*this);
665 localThis.bind(e);
666 return localThis(e.geometry().local(x));
667 }
668
670 {
671 DUNE_THROW(NotImplemented, "derivative of derivative is not implemented");
672 }
673
679};
680
681
682} // namespace Functions
683} // namespace Dune
684
685#endif // DUNE_FUNCTIONS_GRIDFUNCTIONS_DISCRETEGLOBALBASISFUNCTIONS_HH
void axpy(const Ta &a, const type &y)
auto makeDiscreteGlobalBasisFunction(B &&basis, V &&vector)
Generate a DiscreteGlobalBasisFunction.
Definition discreteglobalbasisfunction.hh:460
auto istlVectorBackend(Vector &v)
Return a vector backend wrapping non-const ISTL like containers.
Definition istlvectorbackend.hh:352
STL namespace.
auto flatVectorView(T &t)
Create flat vector view of passed mutable container.
Definition flatvectorview.hh:183
int size() const
typename AutonomousValueType< T >::type AutonomousValue
virtual void operator()()=0
#define DUNE_THROW(E,...)
void forEachLeafNode(Tree &&tree, LeafFunc &&leafFunc)
auto wrap_or_move(T &&t)
Default implementation for derivative traits.
Definition defaultderivativetraits.hh:41
Helper class to deduce the signature of a callable.
Definition signature.hh:60
A simple node to range map using the nested tree indices.
Definition hierarchicnodetorangemap.hh:34
Definition discreteglobalbasisfunction.hh:36
std::shared_ptr< const Data > data_
Definition discreteglobalbasisfunction.hh:225
const Vector & dofs() const
Return the coefficients of this discrete function by reference.
Definition discreteglobalbasisfunction.hh:207
B Basis
Definition discreteglobalbasisfunction.hh:38
typename Basis::LocalView::Tree Tree
Definition discreteglobalbasisfunction.hh:47
V Vector
Definition discreteglobalbasisfunction.hh:39
typename EntitySet::Element Element
Definition discreteglobalbasisfunction.hh:53
typename EntitySet::LocalCoordinate LocalDomain
Definition discreteglobalbasisfunction.hh:52
const NodeToRangeEntry & nodeToRangeEntry() const
Return the stored node-to-range map.
Definition discreteglobalbasisfunction.hh:213
NTRE NodeToRangeEntry
Definition discreteglobalbasisfunction.hh:48
DiscreteGlobalBasisFunctionBase(const std::shared_ptr< const Data > &data)
Definition discreteglobalbasisfunction.hh:192
typename Basis::GridView GridView
Definition discreteglobalbasisfunction.hh:45
const Basis & basis() const
Return a const reference to the stored basis.
Definition discreteglobalbasisfunction.hh:201
GridViewEntitySet< GridView, 0 > EntitySet
Definition discreteglobalbasisfunction.hh:46
const EntitySet & entitySet() const
Get associated set of entities the local-function can be bound to.
Definition discreteglobalbasisfunction.hh:219
typename EntitySet::GlobalCoordinate Domain
Definition discreteglobalbasisfunction.hh:50
Dune::AutonomousValue< decltype(std::declval< Vector >()[std::declval< typename Basis::MultiIndex >()])> Coefficient
Definition discreteglobalbasisfunction.hh:43
Definition discreteglobalbasisfunction.hh:61
EntitySet entitySet
Definition discreteglobalbasisfunction.hh:62
std::shared_ptr< const NodeToRangeEntry > nodeToRangeEntry
Definition discreteglobalbasisfunction.hh:65
std::shared_ptr< const Basis > basis
Definition discreteglobalbasisfunction.hh:63
std::shared_ptr< const Vector > coefficients
Definition discreteglobalbasisfunction.hh:64
LocalFunctionBase & operator=(const LocalFunctionBase &other)
Copy-assignment of the local-function.
Definition discreteglobalbasisfunction.hh:108
bool bound() const
Check if LocalFunction is already bound to an element.
Definition discreteglobalbasisfunction.hh:157
void bind(const Element &element)
Bind LocalFunction to grid element.
Definition discreteglobalbasisfunction.hh:124
typename EntitySet::Element Element
Definition discreteglobalbasisfunction.hh:76
const Element & localContext() const
Return the element the local-function is bound to.
Definition discreteglobalbasisfunction.hh:163
LocalFunctionBase(const LocalFunctionBase &other)
Copy-construct the local-function.
Definition discreteglobalbasisfunction.hh:92
std::vector< Coefficient > localDoFs_
Definition discreteglobalbasisfunction.hh:188
void assignWith(To &to, const From &from) const
Definition discreteglobalbasisfunction.hh:171
std::shared_ptr< const Data > data_
Definition discreteglobalbasisfunction.hh:186
decltype(auto) nodeToRangeEntry(const Node &node, const TreePath &treePath, Range &y) const
Definition discreteglobalbasisfunction.hh:181
LocalView localView_
Definition discreteglobalbasisfunction.hh:187
void unbind()
Unbind the local-function.
Definition discreteglobalbasisfunction.hh:151
LocalDomain Domain
Definition discreteglobalbasisfunction.hh:75
LocalFunctionBase(const std::shared_ptr< const Data > &data)
Definition discreteglobalbasisfunction.hh:79
Derivative of a DiscreteGlobalBasisFunction
Definition discreteglobalbasisfunction.hh:500
typename Base::Basis Basis
Definition discreteglobalbasisfunction.hh:507
friend Traits::DerivativeInterface derivative(const DiscreteGlobalBasisFunctionDerivative &f)
Definition discreteglobalbasisfunction.hh:669
Range operator()(const Domain &x) const
Evaluate the discrete grid-function derivative in global coordinates.
Definition discreteglobalbasisfunction.hh:659
typename SignatureTraits< typename DiscreteGlobalBasisFunction::Traits::DerivativeInterface >::Range Range
Definition discreteglobalbasisfunction.hh:511
friend LocalFunction localFunction(const DiscreteGlobalBasisFunctionDerivative &f)
Construct local function from a DiscreteGlobalBasisFunctionDerivative
Definition discreteglobalbasisfunction.hh:675
DiscreteGlobalBasisFunctionDerivative(const std::shared_ptr< const Data > &data)
create object from DiscreateGlobalBasisFunction data
Definition discreteglobalbasisfunction.hh:648
DGBF DiscreteGlobalBasisFunction
Definition discreteglobalbasisfunction.hh:505
typename Base::Vector Vector
Definition discreteglobalbasisfunction.hh:508
Imp::GridFunctionTraits< Range(Domain), typename Base::EntitySet, DefaultDerivativeTraits, 16 > Traits
Definition discreteglobalbasisfunction.hh:513
typename Base::Domain Domain
Definition discreteglobalbasisfunction.hh:510
A grid function induced by a global basis and a coefficient vector.
Definition discreteglobalbasisfunction.hh:283
friend DiscreteGlobalBasisFunctionDerivative< DiscreteGlobalBasisFunction > derivative(const DiscreteGlobalBasisFunction &f)
Derivative of the DiscreteGlobalBasisFunction
Definition discreteglobalbasisfunction.hh:415
DiscreteGlobalBasisFunction(B_T &&basis, V_T &&coefficients, NTRE_T &&nodeToRangeEntry)
Create a grid-function, by wrapping the arguments in std::shared_ptr.
Definition discreteglobalbasisfunction.hh:390
DiscreteGlobalBasisFunction(std::shared_ptr< const Basis > basis, std::shared_ptr< const V > coefficients, std::shared_ptr< const typename Base::NodeToRangeEntry > nodeToRangeEntry)
Create a grid-function, by moving the arguments in std::shared_ptr.
Definition discreteglobalbasisfunction.hh:395
friend LocalFunction localFunction(const DiscreteGlobalBasisFunction &t)
Construct local function from a DiscreteGlobalBasisFunction.
Definition discreteglobalbasisfunction.hh:428
typename Base::Basis Basis
Definition discreteglobalbasisfunction.hh:288
Imp::GridFunctionTraits< Range(Domain), typename Base::EntitySet, DefaultDerivativeTraits, 16 > Traits
Definition discreteglobalbasisfunction.hh:294
R Range
Definition discreteglobalbasisfunction.hh:292
typename Base::Vector Vector
Definition discreteglobalbasisfunction.hh:289
typename Base::Domain Domain
Definition discreteglobalbasisfunction.hh:291
Definition discreteglobalbasisfunction.hh:307
GlobalFunction::Range Range
Definition discreteglobalbasisfunction.hh:316
typename LocalBase::Domain Domain
Definition discreteglobalbasisfunction.hh:315
LocalFunction(const DiscreteGlobalBasisFunction &globalFunction)
Create a local-function from the associated grid-function.
Definition discreteglobalbasisfunction.hh:320
friend DiscreteGlobalBasisFunctionDerivative< DiscreteGlobalBasisFunction >::LocalFunction derivative(const LocalFunction &lf)
Local function of the derivative.
Definition discreteglobalbasisfunction.hh:376
Range operator()(const Domain &x) const
Evaluate this local-function in coordinates x in the bound element.
Definition discreteglobalbasisfunction.hh:336
typename LocalBase::Element Element
Definition discreteglobalbasisfunction.hh:317
local function evaluating the derivative in reference coordinates
Definition discreteglobalbasisfunction.hh:534
Range operator()(const Domain &x) const
Evaluate this local-function in coordinates x in the bound element.
Definition discreteglobalbasisfunction.hh:585
typename LocalBase::Domain Domain
Definition discreteglobalbasisfunction.hh:541
typename LocalBase::Element Element
Definition discreteglobalbasisfunction.hh:543
friend Traits::LocalFunctionTraits::DerivativeInterface derivative(const LocalFunction &)
Not implemented.
Definition discreteglobalbasisfunction.hh:632
GlobalFunction::Range Range
Definition discreteglobalbasisfunction.hh:542
void unbind()
Unbind the local-function.
Definition discreteglobalbasisfunction.hh:566
LocalFunction(const GlobalFunction &globalFunction)
Create a local function from the associated grid function.
Definition discreteglobalbasisfunction.hh:546
void bind(const Element &element)
Bind LocalFunction to grid element.
Definition discreteglobalbasisfunction.hh:559
Definition gridfunction.hh:36
GridView::template Codim< codim >::Entity Element
Type of Elements contained in this EntitySet.
Definition gridviewentityset.hh:36
Element::Geometry::LocalCoordinate LocalCoordinate
Type of local coordinates with respect to the Element.
Definition gridviewentityset.hh:39
Element::Geometry::GlobalCoordinate GlobalCoordinate
Definition gridviewentityset.hh:40
T reserve(T... args)
T resize(T... args)
T transform(T... args)