DUNE PDELab (2.8)

discreteglobalbasisfunction.hh
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
11#include <dune/typetree/treecontainer.hh>
12
13#include <dune/functions/functionspacebases/hierarchicnodetorangemap.hh>
14#include <dune/functions/functionspacebases/flatvectorview.hh>
15#include <dune/functions/gridfunctions/gridviewentityset.hh>
16#include <dune/functions/gridfunctions/gridfunction.hh>
17#include <dune/functions/backends/concepts.hh>
18#include <dune/functions/backends/istlvectorbackend.hh>
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>
78 using Coefficient = Dune::AutonomousValue<decltype(std::declval<Vector>()[std::declval<typename Basis::MultiIndex>()])>;
79
80 using GridView = typename Basis::GridView;
82 using Tree = typename Basis::LocalView::Tree;
83 using NodeToRangeEntry = NTRE;
84
85 using Domain = typename EntitySet::GlobalCoordinate;
86 using Range = R;
87
88 using LocalDomain = typename EntitySet::LocalCoordinate;
89 using Element = typename EntitySet::Element;
90
91 using Traits = Imp::GridFunctionTraits<Range(Domain), EntitySet, DefaultDerivativeTraits, 16>;
92
93 class LocalFunction
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
108 using GlobalFunction = DiscreteGlobalBasisFunction;
109 using Domain = LocalDomain;
110 using Range = GlobalFunction::Range;
111 using Element = GlobalFunction::Element;
112
113 LocalFunction(const DiscreteGlobalBasisFunction& globalFunction)
114 : globalFunction_(&globalFunction)
115 , localView_(globalFunction.basis().localView())
116 , evaluationBuffer_(localView_.tree())
117 , bound_(false)
118 {
119 localDoFs_.reserve(localView_.maxSize());
120 }
121
122 LocalFunction(const LocalFunction& other)
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
133 LocalFunction& operator=(const LocalFunction& other)
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
244 friend typename Traits::LocalFunctionTraits::DerivativeInterface derivative(const LocalFunction& t)
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
266 DiscreteGlobalBasisFunction(std::shared_ptr<const Basis> basis, std::shared_ptr<const V> coefficients, std::shared_ptr<const NodeToRangeEntry> nodeToRangeEntry) :
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
283 const NodeToRangeEntry& nodeToRangeEntry() const
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
294 friend typename Traits::DerivativeInterface derivative(const DiscreteGlobalBasisFunction& t)
295 {
296 DUNE_THROW(NotImplemented,"not implemented");
297 }
298
314 friend LocalFunction localFunction(const DiscreteGlobalBasisFunction& t)
315 {
316 return LocalFunction(t);
317 }
318
322 const EntitySet& entitySet() const
323 {
324 return entitySet_;
325 }
326
327private:
328
329 EntitySet entitySet_;
330 std::shared_ptr<const Basis> basis_;
331 std::shared_ptr<const V> coefficients_;
332 std::shared_ptr<const NodeToRangeEntry> nodeToRangeEntry_;
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)>()) {
362 return std::forward<decltype(v)>(v);
363 } else {
364 return istlVectorBackend(v);
365 }
366 };
367
368 using Vector = std::decay_t<decltype(toConstVectorBackend(std::forward<V>(vector)))>;
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
A grid function induced by a global basis and a coefficient vector.
Definition: discreteglobalbasisfunction.hh:71
const EntitySet & entitySet() const
Get associated EntitySet.
Definition: discreteglobalbasisfunction.hh:322
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
Default exception for dummy implementations.
Definition: exceptions.hh:261
Traits for type conversions and type information.
typename AutonomousValueType< T >::type AutonomousValue
Type free of internal references that T can be converted to.
Definition: typetraits.hh:558
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
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)
TrigonometricFunction< K, -cosFactor, sinFactor > derivative(const TrigonometricFunction< K, sinFactor, cosFactor > &f)
Obtain derivative of TrigonometricFunction function.
Definition: trigonometricfunction.hh:39
auto istlVectorBackend(Vector &v)
Return a vector backend wrapping non-const ISTL like containers.
Definition: istlvectorbackend.hh:345
constexpr HybridTreePath< T... > treePath(const T &... t)
Constructs a new HybridTreePath from the given indices.
Definition: treepath.hh:188
void forEachLeafNode(Tree &&tree, LeafFunc &&leafFunc)
Traverse tree and visit each leaf node.
Definition: traversal.hh:304
std::decay_t< decltype(makeTreeContainer(std::declval< const Tree & >(), std::declval< Detail::LeafToDefaultConstructibleValue< LeafToValue > >()))> TreeContainer
Alias to container type generated by makeTreeContainer for give tree type and when using LeafToValue ...
Definition: treecontainer.hh:304
Dune namespace.
Definition: alignedallocator.hh:11
auto wrap_or_move(T &&t)
Capture R-value reference to shared_ptr.
Definition: shared_ptr.hh:94
STL namespace.
Default implementation for derivative traits.
Definition: defaultderivativetraits.hh:37
A simple node to range map using the nested tree indices.
Definition: hierarchicnodetorangemap.hh:30
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)