dune-pdelab  2.5-dev
interpolate.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 #ifndef DUNE_PDELAB_GRIDFUNCTIONSPACE_INTERPOLATE_HH
5 #define DUNE_PDELAB_GRIDFUNCTIONSPACE_INTERPOLATE_HH
6 
7 #include<vector>
8 
9 #include<dune/common/exceptions.hh>
10 
11 #include <dune/localfunctions/common/interfaceswitch.hh>
12 
13 #include <dune/typetree/typetree.hh>
14 #include <dune/typetree/pairtraversal.hh>
15 
19 
20 namespace Dune {
21  namespace PDELab {
22 
26 
27  // Backend for standard local interpolation
29  {
30  template<typename FE, typename ElemFunction, typename XL>
31  void interpolate(const FE &fe, const ElemFunction &elemFunction,
32  XL &xl) const
33  {
34  FiniteElementInterfaceSwitch<FE>::interpolation(fe).
35  interpolate(elemFunction,xl);
36  }
37  };
38 
39  namespace {
40 
41  template<typename IB, typename LF, typename XG>
42  struct InterpolateLeafFromScalarVisitor
43  : public TypeTree::TreeVisitor
44  , public TypeTree::DynamicTraversal
45  {
46 
47  template<typename LFS, typename TreePath>
48  void leaf(const LFS& lfs, TreePath treePath) const
49  {
50  std::vector<typename XG::ElementType> xl(lfs.size());
51 
52  // call interpolate for the basis
53  ib.interpolate(lfs.finiteElement(), lf, xl);
54 
55  // write coefficients into local vector
56  xg.write_sub_container(lfs,xl);
57  }
58 
59  InterpolateLeafFromScalarVisitor(const IB& ib_, const LF& lf_, XG& xg_)
60  : ib(ib_)
61  , lf(lf_)
62  , xg(xg_)
63  {}
64 
65  const IB& ib;
66  const LF& lf;
67  XG& xg;
68 
69  };
70 
71 
72  template<typename IB, typename LF, typename XG>
73  struct InterpolateLeafFromVectorVisitor
74  : public TypeTree::TreeVisitor
75  , public TypeTree::DynamicTraversal
76  {
77 
78  template<typename LFS, typename TreePath>
79  void leaf(const LFS& lfs, TreePath treePath) const
80  {
81  std::vector<typename XG::ElementType> xl(lfs.size());
82 
83  // call interpolate for the basis
84 
85  typedef SelectComponentAdapter<LF> LFCOMP;
86 
87  LFCOMP localfcomp(lf,treePath.back());
88  ib.interpolate(lfs.finiteElement(), localfcomp, xl);
89 
90  // write coefficients into local vector
91  xg.write_sub_container(lfs,xl);
92  }
93 
94  InterpolateLeafFromVectorVisitor(const IB& ib_, const LF& lf_, XG& xg_)
95  : ib(ib_)
96  , lf(lf_)
97  , xg(xg_)
98  {}
99 
100  const IB& ib;
101  const LF& lf;
102  XG& xg;
103 
104  };
105 
106 
107  template<typename IB, typename E, typename XG>
108  struct InterpolateVisitor
109  : public TypeTree::TreePairVisitor
110  , public TypeTree::DynamicTraversal
111  {
112 
113  template<typename F, typename LFS, typename TreePath>
114  typename std::enable_if<F::isLeaf && LFS::isLeaf>::type
115  leaf(const F& f, const LFS& lfs, TreePath treePath) const
116  {
117  std::vector<typename XG::ElementType> xl(lfs.size());
118 
119  // call interpolate for the basis
120  ib.interpolate(lfs.finiteElement(),
121  GridFunctionToLocalFunctionAdapter<F>(f,e), xl);
122 
123  // write coefficients into local vector
124  xg.write_sub_container(lfs,xl);
125  }
126 
127  // interpolate PowerLFS from vector-valued function
128  template<typename F, typename LFS, typename TreePath>
129  typename std::enable_if<F::isLeaf && F::Traits::dimRange == 1
130  && (!LFS::isLeaf)>::type
131  leaf(const F& f, const LFS& lfs, TreePath treePath) const
132  {
133  static_assert((TypeTree::TreeInfo<LFS>::depth == 2),
134  "Automatic interpolation of vector-valued function " \
135  "is restricted to trees of depth 1");
136 
137  typedef GridFunctionToLocalFunctionAdapter<F> LF;
138  LF localf(f,e);
139 
140  TypeTree::applyToTree(lfs,InterpolateLeafFromScalarVisitor<IB,LF,XG>(ib,localf,xg));
141 
142  }
143 
144  // interpolate PowerLFS from vector-valued function
145  template<typename F, typename LFS, typename TreePath>
146  typename std::enable_if<F::isLeaf && (F::Traits::dimRange > 1) &&
147  (!LFS::isLeaf)>::type
148  leaf(const F& f, const LFS& lfs, TreePath treePath) const
149  {
150  static_assert((TypeTree::TreeInfo<LFS>::depth == 2),
151  "Automatic interpolation of vector-valued function " \
152  "is restricted to trees of depth 1");
153  static_assert(TypeTree::StaticDegree<LFS>::value == F::Traits::dimRange,
154  "Number of children and dimension of range type " \
155  "must match for automatic interpolation of " \
156  "vector-valued function");
157 
158  typedef GridFunctionToLocalFunctionAdapter<F> LF;
159  LF localf(f,e);
160 
161  TypeTree::applyToTree(lfs,InterpolateLeafFromVectorVisitor<IB,LF,XG>(ib,localf,xg));
162  }
163 
164  InterpolateVisitor(IB ib_, const E& e_, XG& xg_)
165  : ib(ib_)
166  , e(e_)
167  , xg(xg_)
168  {}
169 
170  private:
171  IB ib;
172  const E& e;
173  XG& xg;
174  };
175 
176  } // anonymous namespace
177 
179 
190  template<typename F, typename GFS, typename XG>
191  void interpolate (const F& f, const GFS& gfs, XG& xg)
192  {
193  // this is the leaf version now
194 
195  // get some types
196  using EntitySet = typename GFS::Traits::EntitySet;
197  using Element = typename EntitySet::Element;
198 
199  auto entity_set = gfs.entitySet();
200 
201  // make local function space
202  typedef LocalFunctionSpace<GFS> LFS;
203  LFS lfs(gfs);
204  typedef LFSIndexCache<LFS> LFSCache;
205  LFSCache lfs_cache(lfs);
206  typedef typename XG::template LocalView<LFSCache> XView;
207 
208  XView x_view(xg);
209 
210  // loop once over the grid
211  for (const auto& element : elements(entity_set))
212  {
213  // bind local function space to element
214  lfs.bind(element);
215  lfs_cache.update();
216  x_view.bind(lfs_cache);
217 
218  // call interpolate
219  TypeTree::applyToTreePair(f,lfs,InterpolateVisitor<InterpolateBackendStandard,Element,XView>(InterpolateBackendStandard(),element,x_view));
220 
221  x_view.unbind();
222  }
223 
224  x_view.detach();
225  }
226 
228  } // namespace PDELab
229 } // namespace Dune
230 
231 #endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_INTERPOLATE_HH
const LF & lf
Definition: interpolate.hh:66
Definition: interpolate.hh:28
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
void interpolate(const FE &fe, const ElemFunction &elemFunction, XL &xl) const
Definition: interpolate.hh:31
const IB & ib
Definition: interpolate.hh:65
const Entity & e
Definition: localfunctionspace.hh:111
static const unsigned int value
Definition: gridfunctionspace/tags.hh:139
XG & xg
Definition: interpolate.hh:67