dune-localfunctions  2.7.0
meta/power/interpolation.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_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 #include <dune/common/function.hh>
13 
14 namespace Dune {
15 
18 
24  template<class Backend, class BasisTraits>
26  static_assert(Backend::Traits::dimRange == 1,
27  "PowerInterpolation 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  public Function<typename Backend::Traits::DomainLocal, typename Backend::Traits::Range>
48  {
49  const F &f;
50  std::size_t comp;
51 
52  public:
53  ComponentEvaluator(const F &f_, std::size_t comp_) :
54  f(f_), comp(comp_)
55  { }
56 
57  void evaluate(const typename Backend::Traits::DomainLocal &x,
58  typename Backend::Traits::Range &y) const
59  {
60  typename Traits::Range fy = f(x);
61  y[0] = fy[comp];
62  }
63  };
64 
65  public:
67 
76  template<typename F, typename C>
77  void interpolate(const F& ff, std::vector<C>& out) const {
78 
79  auto&& f = Impl::makeFunctionWithCallOperator<typename Backend::Traits::DomainLocal>(ff);
80 
81 
82  out.clear();
83  std::vector<C> cout;
84  for(std::size_t d = 0; d < Traits::dimRange; ++d) {
85  // When dropping support for `evaluate()` we can simply use a lambda
86  // instead of ComponentEvaluator. But changing this now would break
87  // PowerInterpolation for FE-implementation outside of dune-localfunctions
88  // which may not have been adjusted so far.
89  backend->interpolate(ComponentEvaluator<std::decay_t<decltype(f)>>(f, d), cout);
90  if(d == 0)
91  out.resize(cout.size()*Traits::dimRange);
92  // make sure the size of cout does not change surprisingly
93  assert(out.size() == cout.size()*Traits::dimRange);
94  std::copy(cout.begin(), cout.end(), out.begin() + d*cout.size());
95  }
96  }
97  };
98 
99 } // namespace Dune
100 
101 #endif // DUNE_LOCALFUNCTIONS_META_POWER_INTERPOLATION_HH
Dune::PowerInterpolation
Meta-interpolation turning a scalar interpolation into vector-valued interpolation.
Definition: meta/power/interpolation.hh:25
localinterpolation.hh
Dune
Definition: bdfmcube.hh:15
Dune::PowerInterpolation::PowerInterpolation
PowerInterpolation(const Backend &backend_)
Construct a PowerInterpolation.
Definition: meta/power/interpolation.hh:42
Dune::PowerInterpolation::Traits
BasisTraits Traits
Export basis traits.
Definition: meta/power/interpolation.hh:33
Dune::PowerInterpolation::interpolate
void interpolate(const F &ff, std::vector< C > &out) const
Determine coefficients interpolating a given function.
Definition: meta/power/interpolation.hh:77