dune-localfunctions  2.2.1
meta/power/interpolation.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_LOCALFUNCTIONS_META_POWER_INTERPOLATION_HH
5 #define DUNE_LOCALFUNCTIONS_META_POWER_INTERPOLATION_HH
6 
7 #include <algorithm>
8 #include <cassert>
9 #include <cstddef>
10 #include <vector>
11 
12 #include <dune/common/static_assert.hh>
13 
14 namespace Dune {
15 
18 
24  template<class Backend, class BasisTraits>
26  dune_static_assert(Backend::Traits::dimRange == 1, "PowerInterpolation "
27  "works only with scalar backends");
28 
29  const Backend *backend;
30 
31  public:
33  typedef BasisTraits Traits;
34 
36 
42  PowerInterpolation(const Backend &backend_) : backend(&backend_) { }
43 
44  private:
45  template<class F>
46  class ComponentEvaluator {
47  const F &f;
48  std::size_t comp;
49 
50  public:
51  ComponentEvaluator(const F &f_, std::size_t comp_) :
52  f(f_), comp(comp_)
53  { }
54 
55  void evaluate(const typename Backend::Traits::DomainLocal &x,
56  typename Backend::Traits::Range &y) const
57  {
58  typename Traits::Range fy;
59  f.evaluate(x, fy);
60  y[0] = fy[comp];
61  }
62  };
63 
64  public:
66 
75  template<typename F, typename C>
76  void interpolate(const F& f, std::vector<C>& out) const {
77  out.clear();
78  std::vector<C> cout;
79  for(std::size_t d = 0; d < Traits::dimRange; ++d) {
80  backend->interpolate(ComponentEvaluator<F>(f, d), cout);
81  if(d == 0)
82  out.resize(cout.size()*Traits::dimRange);
83  // make sure the size of cout does not change surprisingly
84  assert(out.size() == cout.size()*Traits::dimRange);
85  std::copy(cout.begin(), cout.end(), out.begin() + d*cout.size());
86  }
87  }
88  };
89 
90 } // namespace Dune
91 
92 #endif // DUNE_LOCALFUNCTIONS_META_POWER_INTERPOLATION_HH