dune-functions  2.5-dev
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 
8 #include <dune/common/shared_ptr.hh>
9 
15 
16 namespace Dune {
17 namespace Functions {
18 
19 
20 
66 template<typename B, typename TP, typename V,
67  typename NTRE = DefaultNodeToRangeMap<typename std::decay<decltype(std::declval<B>().localView().tree().child(std::declval<TP>()))>::type>,
68  typename R = typename V::value_type>
70 {
71 public:
72  using Basis = B;
73  using TreePath = TP;
74  using Vector = V;
75 
76  using GridView = typename Basis::GridView;
78  using Tree = typename Basis::LocalView::Tree;
79  using SubTree = typename TypeTree::ChildForTreePath<Tree, TreePath>;
80  using NodeToRangeEntry = NTRE;
81 
83  using Range = R;
84 
86  using Element = typename EntitySet::Element;
87 
89 
91  {
92  using LocalBasisView = typename Basis::LocalView;
93  using LocalIndexSet = typename Basis::LocalIndexSet;
94  using size_type = typename SubTree::size_type;
95 
96  template<class Node>
97  using LocalBasisRange = typename Node::FiniteElement::Traits::LocalBasisType::Traits::RangeType;
98 
99  template<class Node>
100  using NodeData = typename std::vector<LocalBasisRange<Node>>;
101 
103 
104  struct LocalEvaluateVisitor
105  : public TypeTree::TreeVisitor
106  , public TypeTree::DynamicTraversal
107  {
108  LocalEvaluateVisitor(const LocalDomain& x, Range& y, const LocalIndexSet& localIndexSet, const Vector& coefficients, const NodeToRangeEntry& nodeToRangeEntry, ShapeFunctionValueContainer& shapeFunctionValueContainer):
109  x_(x),
110  y_(y),
111  localIndexSet_(localIndexSet),
112  coefficients_(coefficients),
113  nodeToRangeEntry_(nodeToRangeEntry),
114  shapeFunctionValueContainer_(shapeFunctionValueContainer)
115  {}
116 
117  template<typename Node, typename TreePath>
118  void leaf(Node& node, TreePath treePath)
119  {
120  using LocalBasisRange = typename Node::FiniteElement::Traits::LocalBasisType::Traits::RangeType;
121  using MultiIndex = typename LocalIndexSet::MultiIndex;
122  using CoefficientBlock = typename std::decay<decltype(std::declval<Vector>()[std::declval<MultiIndex>()])>::type;
123  using RangeBlock = typename std::decay<decltype(nodeToRangeEntry_(node, y_))>::type;
124 
125  auto&& fe = node.finiteElement();
126  auto&& localBasis = fe.localBasis();
127 
128  auto&& shapeFunctionValues = shapeFunctionValueContainer_[node];
129  localBasis.evaluateFunction(x_, shapeFunctionValues);
130 
131  // Get range entry associated to this node
132  auto&& re = nodeToRangeEntry_(node, y_);
133 
134 
135  for (size_type i = 0; i < localBasis.size(); ++i)
136  {
137  auto&& multiIndex = localIndexSet_.index(node.localIndex(i));
138 
139  // Get coefficient associated to i-th shape function
140  auto&& c = coefficients_[multiIndex];
141 
142  // Get value of i-th shape function
143  auto&& v = shapeFunctionValues[i];
144 
145  // Notice that the range entry re, the coefficient c, and the shape functions
146  // value v may all be scalar, vector, matrix, or general container valued.
147  // The matching of their entries is done via the multistage procedure described
148  // in the class documentation of DiscreteGlobalBasisFunction.
151  assert(dimC*dimV == FlatVectorBackend<RangeBlock>::size(re));
152  for(size_type j=0; j<dimC; ++j)
153  {
155  for(size_type k=0; k<dimV; ++k)
156  {
158  FlatVectorBackend<RangeBlock>::getEntry(re, j*dimV + k) += c_j*v_k;
159  }
160  }
161  }
162  }
163 
164  const LocalDomain& x_;
165  Range& y_;
166  const LocalIndexSet& localIndexSet_;
167  const Vector& coefficients_;
168  const NodeToRangeEntry& nodeToRangeEntry_;
169  ShapeFunctionValueContainer& shapeFunctionValueContainer_;
170  };
171 
172 
173  public:
174 
179 
181  : globalFunction_(&globalFunction)
182  , localBasisView_(globalFunction.basis().localView())
183  , localIndexSet_(globalFunction.basis().localIndexSet())
184  {
185  // Here we assume that the tree can be accessed, traversed,
186  // and queried for tree indices even in unbound state.
187  subTree_ = &TypeTree::child(localBasisView_.tree(), globalFunction_->treePath());
188  shapeFunctionValueContainer_.init(*subTree_);
189 // localDoFs_.reserve(localBasisView_.maxSize());
190  }
191 
193  : globalFunction_(other.globalFunction_)
194  , localBasisView_(globalFunction_->basis().localView())
195  , localIndexSet_(globalFunction_->basis().localIndexSet())
196  {
197  // Here we assume that the tree can be accessed, traversed,
198  // and queried for tree indices even in unbound state.
199  subTree_ = &TypeTree::child(localBasisView_.tree(), globalFunction_->treePath());
200  shapeFunctionValueContainer_.init(*subTree_);
201  }
202 
204  {
205  globalFunction_ = other.globalFunction_;
206  localBasisView_ = other.localBasisView_;
207  localIndexSet_ = other.localIndexSet_;
208  subTree_ = &TypeTree::child(localBasisView_.tree(), globalFunction_->treePath());
209 
210  // Here we assume that the tree can be accessed, traversed,
211  // and queried for tree indices even in unbound state.
212  shapeFunctionValueContainer_.init(*subTree_);
213  }
214 
221  void bind(const Element& element)
222  {
223  localBasisView_.bind(element);
224  localIndexSet_.bind(localBasisView_);
225 
226  // Read dofs associated to bound element
227 // localDoFs_.resize(subTree.size());
228 // for (size_type i = 0; i < subTree.size(); ++i)
229 // localDoFs_[i] = globalFunction_->dofs()[localIndexSet_.index(i)];
230  }
231 
232  void unbind()
233  {
234  localIndexSet_.unbind();
235  localBasisView_.unbind();
236  }
237 
247  Range operator()(const Domain& x) const
248  {
249  auto y = Range(0);
250 
251  LocalEvaluateVisitor localEvaluateVisitor(x, y, localIndexSet_, globalFunction_->dofs(), globalFunction_->nodeToRangeEntry(), shapeFunctionValueContainer_);
252  TypeTree::applyToTree(*subTree_, localEvaluateVisitor);
253 
254  return y;
255  }
256 
257  const Element& localContext() const
258  {
259  return localBasisView_.element();
260  }
261 
263  {
264  DUNE_THROW(NotImplemented,"not implemented");
265  }
266 
267  private:
268 
269  const DiscreteGlobalBasisFunction* globalFunction_;
270  LocalBasisView localBasisView_;
271  LocalIndexSet localIndexSet_;
272 
273  mutable ShapeFunctionValueContainer shapeFunctionValueContainer_;
274 // std::vector<typename V::value_type> localDoFs_;
275  const SubTree* subTree_;
276  };
277 
278  DiscreteGlobalBasisFunction(const Basis & basis, const TreePath& treePath, const V & coefficients, const NodeToRangeEntry& nodeToRangeEntry) :
279  entitySet_(basis.gridView()),
280  basis_(stackobject_to_shared_ptr(basis)),
281  treePath_(treePath),
282  coefficients_(stackobject_to_shared_ptr(coefficients)),
283  nodeToRangeEntry_(stackobject_to_shared_ptr(nodeToRangeEntry))
284  {}
285 
286  DiscreteGlobalBasisFunction(std::shared_ptr<const Basis> basis, const TreePath& treePath, std::shared_ptr<const V> coefficients, std::shared_ptr<const NodeToRangeEntry> nodeToRangeEntry) :
287  entitySet_(basis->gridView()),
288  basis_(basis),
289  treePath_(treePath),
290  coefficients_(coefficients),
291  nodeToRangeEntry_(nodeToRangeEntry)
292  {}
293 
294  const Basis& basis() const
295  {
296  return *basis_;
297  }
298 
299  const TreePath& treePath() const
300  {
301  return treePath_;
302  }
303 
304  const V& dofs() const
305  {
306  return *coefficients_;
307  }
308 
310  {
311  return *nodeToRangeEntry_;
312  }
313 
314  // TODO: Implement this using hierarchic search
315  Range operator() (const Domain& x) const
316  {
317  DUNE_THROW(NotImplemented,"not implemented");
318  }
319 
321  {
322  DUNE_THROW(NotImplemented,"not implemented");
323  }
324 
326  {
327  return LocalFunction(t);
328  }
329 
333  const EntitySet& entitySet() const
334  {
335  return entitySet_;
336  }
337 
338 private:
339 
340  EntitySet entitySet_;
341  std::shared_ptr<const Basis> basis_;
342  const TreePath treePath_;
343  std::shared_ptr<const V> coefficients_;
344  std::shared_ptr<const NodeToRangeEntry> nodeToRangeEntry_;
345 };
346 
347 
348 
349 template<typename R, typename B, typename TP, typename V>
350 auto makeDiscreteGlobalBasisFunction(B&& basis, const TP& treePath, V&& vector)
351 {
352  using Basis = std::decay_t<B>;
353  using Vector = std::decay_t<V>;
355  auto nodeToRangeEntryPtr = std::make_shared<NTREM>(makeDefaultNodeToRangeMap(basis, treePath));
356  auto basisPtr = Dune::wrap_or_move(std::forward<B>(basis));
357  auto vectorPtr = Dune::wrap_or_move(std::forward<V>(vector));
358  return DiscreteGlobalBasisFunction<Basis, TP, Vector, NTREM, R>(basisPtr, treePath, vectorPtr, nodeToRangeEntryPtr);
359 }
360 
361 
362 
363 template<typename R, typename B, typename V>
364 auto makeDiscreteGlobalBasisFunction(B&& basis, V&& vector)
365 {
366  return makeDiscreteGlobalBasisFunction<R>(std::forward<B>(basis), TypeTree::hybridTreePath(), std::forward<V>(vector));
367 }
368 
369 
370 
371 } // namespace Functions
372 } // namespace Dune
373 
374 #endif // DUNE_FUNCTIONS_GRIDFUNCTIONS_DISCRETEGLOBALBASISFUNCTIONS_HH
void bind(const Element &element)
Bind LocalFunction to grid element.
Definition: discreteglobalbasisfunction.hh:221
LocalFunction(const LocalFunction &other)
Definition: discreteglobalbasisfunction.hh:192
const Basis & basis() const
Definition: discreteglobalbasisfunction.hh:294
B Basis
Definition: discreteglobalbasisfunction.hh:72
void unbind()
Definition: discreteglobalbasisfunction.hh:232
friend Traits::LocalFunctionTraits::DerivativeInterface derivative(const LocalFunction &t)
Definition: discreteglobalbasisfunction.hh:262
GridView::template Codim< codim >::Entity Element
Type of Elements contained in this EntitySet.
Definition: gridviewentityset.hh:32
auto makeDiscreteGlobalBasisFunction(B &&basis, const TP &treePath, V &&vector)
Definition: discreteglobalbasisfunction.hh:350
Definition: polynomial.hh:7
Definition: discreteglobalbasisfunction.hh:90
Traits class providing type information for DifferentiableFunction.
Definition: gridfunction.hh:41
typename EntitySet::Element Element
Definition: discreteglobalbasisfunction.hh:86
V Vector
Definition: discreteglobalbasisfunction.hh:74
Element::Geometry::LocalCoordinate LocalCoordinate
Type of local coordinates with respect to the Element.
Definition: gridviewentityset.hh:35
static auto size(VV &&v) -> decltype(v.size())
Definition: flatvectorbackend.hh:41
const V & dofs() const
Definition: discreteglobalbasisfunction.hh:304
typename TypeTree::ChildForTreePath< Tree, TreePath > SubTree
Definition: discreteglobalbasisfunction.hh:79
Range operator()(const Domain &x) const
Evaluate LocalFunction at bound element.
Definition: discreteglobalbasisfunction.hh:247
Definition: flatvectorbackend.hh:20
LocalDomain Domain
Definition: discreteglobalbasisfunction.hh:176
A simple node to range map using lexicographic ordering.
Definition: defaultnodetorangemap.hh:38
R Range
Definition: discreteglobalbasisfunction.hh:83
const EntitySet & entitySet() const
Get associated EntitySet.
Definition: discreteglobalbasisfunction.hh:333
NTRE NodeToRangeEntry
Definition: discreteglobalbasisfunction.hh:80
DiscreteGlobalBasisFunction(const Basis &basis, const TreePath &treePath, const V &coefficients, const NodeToRangeEntry &nodeToRangeEntry)
Definition: discreteglobalbasisfunction.hh:278
DefaultNodeToRangeMap< Tree > makeDefaultNodeToRangeMap(const Tree &tree)
Definition: defaultnodetorangemap.hh:107
typename EntitySet::GlobalCoordinate Domain
Definition: discreteglobalbasisfunction.hh:82
LocalFunction(const DiscreteGlobalBasisFunction &globalFunction)
Definition: discreteglobalbasisfunction.hh:180
typename Basis::LocalView::Tree Tree
Definition: discreteglobalbasisfunction.hh:78
GlobalFunction::Range Range
Definition: discreteglobalbasisfunction.hh:177
const TreePath & treePath() const
Definition: discreteglobalbasisfunction.hh:299
LocalFunction operator=(const LocalFunction &other)
Definition: discreteglobalbasisfunction.hh:203
TP TreePath
Definition: discreteglobalbasisfunction.hh:73
typename Basis::GridView GridView
Definition: discreteglobalbasisfunction.hh:76
A grid function induced by a global basis and a coefficient vector.
Definition: discreteglobalbasisfunction.hh:69
friend Traits::DerivativeInterface derivative(const DiscreteGlobalBasisFunction &t)
Definition: discreteglobalbasisfunction.hh:320
const NodeToRangeEntry & nodeToRangeEntry() const
Definition: discreteglobalbasisfunction.hh:309
Element::Geometry::GlobalCoordinate GlobalCoordinate
Definition: gridviewentityset.hh:36
typename EntitySet::LocalCoordinate LocalDomain
Definition: discreteglobalbasisfunction.hh:85
static auto getEntry(VV &&v, const Index &i) -> decltype(v[i])
Definition: flatvectorbackend.hh:25
GlobalFunction::Element Element
Definition: discreteglobalbasisfunction.hh:178
DiscreteGlobalBasisFunction(std::shared_ptr< const Basis > basis, const TreePath &treePath, std::shared_ptr< const V > coefficients, std::shared_ptr< const NodeToRangeEntry > nodeToRangeEntry)
Definition: discreteglobalbasisfunction.hh:286
const Element & localContext() const
Definition: discreteglobalbasisfunction.hh:257
friend LocalFunction localFunction(const DiscreteGlobalBasisFunction &t)
Definition: discreteglobalbasisfunction.hh:325
Definition: gridfunction.hh:31