dune-pdelab  2.0.0
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 
9 #include <dune/typetree/nodetags.hh>
10 
11 #include "function.hh"
12 
13 namespace Dune {
14  namespace PDELab {
15 
18 
20  //
21  // PointwiseGridFunctionAdapter
22  //
23 
26 
31  template<typename Engine, typename F0,
32  typename F1 = TypeTree::EmptyNode,
33  typename F2 = TypeTree::EmptyNode,
34  typename F3 = TypeTree::EmptyNode,
35  typename F4 = TypeTree::EmptyNode,
36  typename F5 = TypeTree::EmptyNode,
37  typename F6 = TypeTree::EmptyNode,
38  typename F7 = TypeTree::EmptyNode,
39  typename F8 = TypeTree::EmptyNode,
40  typename F9 = TypeTree::EmptyNode>
42  public GridFunctionInterface<
43  typename F0::Traits,
44  PointwiseGridFunctionAdapter<
45  Engine,
46  F0, F1, F2, F3, F4, F5, F6, F7, F8, F9> >
47  {
48  public:
49  typedef typename F0::Traits Traits;
50 
51  private:
52  const Engine& engine;
53  const F0& f0;
54  const F1& f1;
55  const F2& f2;
56  const F3& f3;
57  const F4& f4;
58  const F5& f5;
59  const F6& f6;
60  const F7& f7;
61  const F8& f8;
62  const F9& f9;
64  unsigned size;
65 
67 
74  template<typename F>
75  static void inc(unsigned &sum, const F* f)
76  { ++sum; }
78 
83  static void inc(unsigned &sum, const TypeTree::EmptyNode* f)
84  { }
86 
89  static unsigned count() {
90  unsigned sum = 0;
91  inc(sum, (F0*)0);
92  inc(sum, (F1*)0);
93  inc(sum, (F2*)0);
94  inc(sum, (F3*)0);
95  inc(sum, (F4*)0);
96  inc(sum, (F5*)0);
97  inc(sum, (F6*)0);
98  inc(sum, (F7*)0);
99  inc(sum, (F8*)0);
100  inc(sum, (F9*)0);
101  return sum;
102  }
103 
106 
116  template<typename F>
117  static void evaluate(const F &f, unsigned &index,
118  const typename Traits::ElementType& e,
119  const typename Traits::DomainType& x,
120  std::vector<typename Traits::RangeType>& y) {
121  f.evaluate(e, x, y[index]);
122  ++index;
123  }
125 
136  static void evaluate(const TypeTree::EmptyNode &f, unsigned &index,
137  const typename Traits::ElementType& e,
138  const typename Traits::DomainType& x,
139  std::vector<typename Traits::RangeType>& y)
140  { }
141 
142  public:
143 #ifdef DOXYGEN
144 
155  PointwiseGridFunctionAdapter(const Engine& engine_, const F0& f0_, ...)
156  {}
157 #else
158  PointwiseGridFunctionAdapter(const Engine& engine_, const F0& f0_,
159  const F1& f1_ = TypeTree::EmptyNode(),
160  const F2& f2_ = TypeTree::EmptyNode(),
161  const F3& f3_ = TypeTree::EmptyNode(),
162  const F4& f4_ = TypeTree::EmptyNode(),
163  const F5& f5_ = TypeTree::EmptyNode(),
164  const F6& f6_ = TypeTree::EmptyNode(),
165  const F7& f7_ = TypeTree::EmptyNode(),
166  const F8& f8_ = TypeTree::EmptyNode(),
167  const F9& f9_ = TypeTree::EmptyNode())
168  : engine(engine_), f0(f0_)
169  , f1(f1_), f2(f2_), f3(f3_)
170  , f4(f4_), f5(f5_), f6(f6_)
171  , f7(f7_), f8(f8_), f9(f9_)
172  , size(count())
173  {}
174 #endif // DOXYGEN
175 
176  inline void evaluate (const typename Traits::ElementType& e,
177  const typename Traits::DomainType& x,
178  typename Traits::RangeType& y) const
179  {
180  std::vector<typename Traits::RangeType> in(size);
181  unsigned index = 0;
182  evaluate(f0, index, e, x, in);
183  evaluate(f1, index, e, x, in);
184  evaluate(f2, index, e, x, in);
185  evaluate(f3, index, e, x, in);
186  evaluate(f4, index, e, x, in);
187  evaluate(f5, index, e, x, in);
188  evaluate(f6, index, e, x, in);
189  evaluate(f7, index, e, x, in);
190  evaluate(f8, index, e, x, in);
191  evaluate(f9, index, e, x, in);
192  engine.evaluate(y, in);
193  }
194 
195  inline const typename Traits::GridViewType& getGridView () const
196  {
197  return f0.getGridView();
198  }
199  };
200 
203  template<typename Engine, typename F0>
204  PointwiseGridFunctionAdapter<Engine, F0>
205  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0)
206  {
208  <Engine,F0>
209  (engine,f0);
210  }
211 
214  template<typename Engine, typename F0, typename F1>
215  PointwiseGridFunctionAdapter<Engine, F0, F1>
216  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
217  const F1& f1)
218  {
220  <Engine,F0,F1>
221  (engine,f0,f1);
222  }
223 
226  template<typename Engine, typename F0, typename F1, typename F2>
227  PointwiseGridFunctionAdapter<Engine, F0, F1, F2>
228  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
229  const F1& f1, const F2& f2)
230  {
232  <Engine,F0,F1,F2>
233  (engine,f0,f1,f2);
234  }
235 
238  template<typename Engine, typename F0, typename F1, typename F2,
239  typename F3>
240  PointwiseGridFunctionAdapter<Engine, F0, F1, F2, F3>
241  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
242  const F1& f1, const F2& f2, const F3& f3)
243  {
245  <Engine,F0,F1,F2,F3>
246  (engine,f0,f1,f2,f3);
247  }
248 
251  template<typename Engine, typename F0, typename F1, typename F2,
252  typename F3, typename F4>
253  PointwiseGridFunctionAdapter<Engine, F0, F1, F2, F3, F4>
254  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
255  const F1& f1, const F2& f2, const F3& f3,
256  const F4& f4)
257  {
259  <Engine,F0,F1,F2,F3,F4>
260  (engine,f0,f1,f2,f3,f4);
261  }
262 
265  template<typename Engine, typename F0, typename F1, typename F2,
266  typename F3, typename F4, typename F5>
267  PointwiseGridFunctionAdapter<Engine, F0, F1, F2, F3, F4, F5>
268  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
269  const F1& f1, const F2& f2, const F3& f3,
270  const F4& f4, const F5& f5)
271  {
273  <Engine,F0,F1,F2,F3,F4,F5>
274  (engine,f0,f1,f2,f3,f4,f5);
275  }
276 
279  template<typename Engine, typename F0, typename F1, typename F2,
280  typename F3, typename F4, typename F5, typename F6>
281  PointwiseGridFunctionAdapter<Engine, F0, F1, F2, F3, F4, F5, F6>
282  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
283  const F1& f1, const F2& f2, const F3& f3,
284  const F4& f4, const F5& f5, const F6& f6)
285  {
287  <Engine,F0,F1,F2,F3,F4,F5,F6>
288  (engine,f0,f1,f2,f3,f4,f5,f6);
289  }
290 
293  template<typename Engine, typename F0, typename F1, typename F2,
294  typename F3, typename F4, typename F5, typename F6, typename F7>
295  PointwiseGridFunctionAdapter<Engine, F0, F1, F2, F3, F4, F5, F6, F7>
296  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
297  const F1& f1, const F2& f2, const F3& f3,
298  const F4& f4, const F5& f5, const F6& f6,
299  const F7& f7)
300  {
302  <Engine,F0,F1,F2,F3,F4,F5,F6,F7>
303  (engine,f0,f1,f2,f3,f4,f5,f6,f7);
304  }
305 
308  template<typename Engine, typename F0, typename F1, typename F2,
309  typename F3, typename F4, typename F5, typename F6, typename F7,
310  typename F8>
311  PointwiseGridFunctionAdapter<Engine, F0, F1, F2, F3, F4, F5, F6, F7, F8>
312  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
313  const F1& f1, const F2& f2, const F3& f3,
314  const F4& f4, const F5& f5, const F6& f6,
315  const F7& f7, const F8& f8)
316  {
318  <Engine,F0,F1,F2,F3,F4,F5,F6,F7,F8>
319  (engine,f0,f1,f2,f3,f4,f5,f6,f7,f8);
320  }
321 
324  template<typename Engine, typename F0, typename F1, typename F2,
325  typename F3, typename F4, typename F5, typename F6, typename F7,
326  typename F8, typename F9>
327  PointwiseGridFunctionAdapter<Engine, F0, F1, F2, F3, F4, F5, F6, F7, F8,
328  F9>
329  makePointwiseGridFunctionAdapter(const Engine& engine, const F0& f0,
330  const F1& f1, const F2& f2, const F3& f3,
331  const F4& f4, const F5& f5, const F6& f6,
332  const F7& f7, const F8& f8, const F9& f9)
333  {
335  <Engine,F0,F1,F2,F3,F4,F5,F6,F7,F8,F9>
336  (engine,f0,f1,f2,f3,f4,f5,f6,f7,f8,f9);
337  }
338 
340  //
341  // AdapterEngines
342  //
343 
346  {
347  public:
348 
350 
357  template<typename Domain, typename Range>
358  void evaluate(Range& out,
359  const std::vector<Domain>& in) const;
360  };
361 
363 
368  template<typename S>
370  {
371  S scale;
372 
373  public:
374 
376 
380  : scale(scale_)
381  {}
382 
384  template<typename Domain, typename Range>
385  void evaluate(Range& out,
386  const std::vector<Domain>& in) const {
387  assert(in.size() == 1);
388  out = in[0];
389  out *= scale;
390  }
391  };
393 
396  template<typename S>
397  PointwiseScaleAdapterEngine<S>
399  return PointwiseScaleAdapterEngine<S>(scale);
400  }
401 
403 
409  {
410  public:
411 
413  template<typename Domain, typename Range>
414  void evaluate(Range& out,
415  const std::vector<Domain>& in) const {
416  out = 0;
417  for(unsigned i = 0; i < in.size(); ++i)
418  out += in[i];
419  }
420  };
421 
423 
424  } // namespace PDELab
425 } // namespace Dune
426 
427 #endif // DUNE_PDELAB_FUNCTIONWRAPPERS_HH
Interface of a pointwise adapter engine.
Definition: functionwrappers.hh:345
void evaluate(Range &out, const std::vector< Domain > &in) const
calculate the adapted value from a set of input values
Definition: functionwrappers.hh:385
F0::Traits Traits
Definition: functionwrappers.hh:49
void evaluate(Range &out, const std::vector< Domain > &in) const
calculate the adapted value from a set of input values
PointwiseGridFunctionAdapter(const Engine &engine_, const F0 &f0_,...)
construct a PointwiseGridFunctionAdapter
Definition: functionwrappers.hh:155
Sum all terms.
Definition: functionwrappers.hh:408
PointwiseGridFunctionAdapter< Engine, F0 > makePointwiseGridFunctionAdapter(const Engine &engine, const F0 &f0)
Definition: functionwrappers.hh:205
PointwiseScaleAdapterEngine(const S scale_)
create a PointwiseScaleAdapterEngine
Definition: functionwrappers.hh:379
PointwiseScaleAdapterEngine< S > makePointwiseScaleAdapterEngine(const S scale)
syntactic sugar to create a PointwiseScaleAdapterEngine
Definition: functionwrappers.hh:398
Scale the output value.
Definition: functionwrappers.hh:369
a GridFunction maps x in DomainType to y in RangeType
Definition: function.hh:187
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Definition: functionwrappers.hh:176
Definition: functionwrappers.hh:41
const F & f
Definition: common/constraints.hh:145
const Traits::GridViewType & getGridView() const
Definition: functionwrappers.hh:195
void evaluate(Range &out, const std::vector< Domain > &in) const
calculate the adapted value from a set of input values
Definition: functionwrappers.hh:414
const E & e
Definition: interpolate.hh:172