dune-pdelab  2.4.1
functionwrappers.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_FUNCTIONWRAPPERS_HH
5 #define DUNE_PDELAB_FUNCTIONWRAPPERS_HH
6 
7 #include <vector>
8 #include <tuple>
9 
10 #include <dune/typetree/nodetags.hh>
11 
12 #include "function.hh"
13 
14 namespace Dune {
15  namespace PDELab {
16 
19 
21  //
22  // PointwiseGridFunctionAdapter
23  //
24 
27 
32  template<typename Engine, typename F0, typename... Functions>
34  public GridFunctionInterface<
35  typename F0::Traits,
36  PointwiseGridFunctionAdapter<Engine, F0, Functions...> >
37  {
38  public:
39  typedef typename F0::Traits Traits;
40 
41  private:
42  const Engine& engine;
43  std::tuple<const F0*, const Functions*...> storage;
44 
46 
54  template<unsigned int I, unsigned int N>
55  void evaluate(
56  const typename Traits::ElementType& e,
57  const typename Traits::DomainType& x,
58  std::vector<typename Traits::RangeType>& y,
59  std::integral_constant<unsigned int, I>,
60  std::integral_constant<unsigned int, N>,
61  std::integral_constant<bool, true>) const {
62  std::get<I>(storage)->evaluate(e, x, y[I]);
63  evaluate(e,x,y,
64  std::integral_constant<unsigned int, I+1>(),
65  std::integral_constant<unsigned int, N>(),
66  std::integral_constant<bool, (I+1<N)>()
67  );
68  }
69 
71  template<unsigned int I>
72  void evaluate(
73  const typename Traits::ElementType& e,
74  const typename Traits::DomainType& x,
75  std::vector<typename Traits::RangeType>& y,
76  std::integral_constant<unsigned int, I>,
77  std::integral_constant<unsigned int, I>,
78  std::integral_constant<bool, false>) const
79  {}
80 
81  public:
83 
93  template<typename... F>
94  PointwiseGridFunctionAdapter(const Engine& engine_, const F&... functions)
95  : engine(engine_), storage(&functions...)
96  {}
97 
98  inline void evaluate (const typename Traits::ElementType& e,
99  const typename Traits::DomainType& x,
100  typename Traits::RangeType& y) const
101  {
102  static const unsigned int N = sizeof...(Functions)+1;
103  std::vector<typename Traits::RangeType> in(N);
104  evaluate(e, x, in,
105  std::integral_constant<unsigned int, 0>(),
106  std::integral_constant<unsigned int, N>(),
107  std::integral_constant<bool, true>()
108  );
109  engine.evaluate(y, in);
110  }
111 
112  inline const typename Traits::GridViewType& getGridView () const
113  {
114  return std::get<0>(storage)->getGridView();
115  }
116 
117  };
118 
121  template<typename Engine, typename... Functions>
122  PointwiseGridFunctionAdapter<Engine, Functions...>
123  makePointwiseGridFunctionAdapter(const Engine& engine, const Functions&... f)
124  {
126  <Engine,Functions...>
127  (engine,f...);
128  }
129 
131  //
132  // AdapterEngines
133  //
134 
137  {
138  public:
139 
141 
148  template<typename Domain, typename Range>
149  void evaluate(Range& out,
150  const std::vector<Domain>& in) const;
151  };
152 
154 
159  template<typename S>
161  {
162  S scale;
163 
164  public:
165 
167 
171  : scale(scale_)
172  {}
173 
175  template<typename Domain, typename Range>
176  void evaluate(Range& out,
177  const std::vector<Domain>& in) const {
178  assert(in.size() == 1);
179  out = in[0];
180  out *= scale;
181  }
182  };
184 
187  template<typename S>
190  return PointwiseScaleAdapterEngine<S>(scale);
191  }
192 
194 
200  {
201  public:
202 
204  template<typename Domain, typename Range>
205  void evaluate(Range& out,
206  const std::vector<Domain>& in) const {
207  out = 0;
208  for(unsigned i = 0; i < in.size(); ++i)
209  out += in[i];
210  }
211  };
212 
214 
215  } // namespace PDELab
216 } // namespace Dune
217 
218 #endif // DUNE_PDELAB_FUNCTIONWRAPPERS_HH
Scale the output value.
Definition: functionwrappers.hh:160
Definition: adaptivity.hh:27
PointwiseScaleAdapterEngine(const S scale_)
create a PointwiseScaleAdapterEngine
Definition: functionwrappers.hh:170
Sum all terms.
Definition: functionwrappers.hh:199
const Entity & e
Definition: localfunctionspace.hh:111
Definition: functionwrappers.hh:33
F0::Traits Traits
Definition: functionwrappers.hh:39
PointwiseScaleAdapterEngine< S > makePointwiseScaleAdapterEngine(const S scale)
syntactic sugar to create a PointwiseScaleAdapterEngine
Definition: functionwrappers.hh:189
PointwiseGridFunctionAdapter< Engine, Functions... > makePointwiseGridFunctionAdapter(const Engine &engine, const Functions &...f)
Definition: functionwrappers.hh:123
void evaluate(Range &out, const std::vector< Domain > &in) const
calculate the adapted value from a set of input values
Definition: functionwrappers.hh:176
const Traits::GridViewType & getGridView() const
Definition: functionwrappers.hh:112
void evaluate(Range &out, const std::vector< Domain > &in) const
calculate the adapted value from a set of input values
Definition: functionwrappers.hh:205
Interface of a pointwise adapter engine.
Definition: functionwrappers.hh:136
PointwiseGridFunctionAdapter(const Engine &engine_, const F &...functions)
construct a PointwiseGridFunctionAdapter
Definition: functionwrappers.hh:94
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Definition: functionwrappers.hh:98
a GridFunction maps x in DomainType to y in RangeType
Definition: function.hh:186