dune-pdelab  2.7-git
callableadapter.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_PDELAB_FUNCTION_CALLABLEADAPTER_HH
5 #define DUNE_PDELAB_FUNCTION_CALLABLEADAPTER_HH
6 
7 #include <utility>
8 #include <type_traits>
9 #include <dune/common/fvector.hh>
10 #include <dune/common/typetraits.hh>
11 
14 
15 namespace Dune {
16  namespace PDELab {
17 
18  /************************
19  * Grid function adapters
20  ************************/
21 
23  template<typename GV, typename RF, int n, typename F>
25  : public Dune::PDELab::GridFunctionBase<Dune::PDELab::
26  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> >,
27  GlobalCallableToGridFunctionAdapter<GV,RF,n,F> >
28  {
29  GV gv;
30  F f;
31  public:
32  typedef Dune::PDELab::
33  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> > Traits;
34 
36  GlobalCallableToGridFunctionAdapter (const GV& gv_, const F& f_) : gv(gv_), f(f_) {}
37 
39  inline const GV& getGridView () const {return gv;}
40 
42  inline void evaluate (const typename Traits::ElementType& e,
43  const typename Traits::DomainType& xl,
44  typename Traits::RangeType& y) const
45  {
46  typename Traits::DomainType xg = e.geometry().global(xl);
47  y = f(xg);
48  }
49  };
50 
51  template<typename T>
53  enum {dim=1};
54  };
55 
56  template<typename T, int n>
57  struct CallableAdapterGetDim< FieldVector<T,n> > {
58  enum {dim=n};
59  };
60 
61  template<typename T>
63  typedef T Type;
64  };
65 
66  template<typename T, int n>
67  struct CallableAdapterGetRangeFieldType< FieldVector<T,n> > {
68  typedef T Type;
69  };
70 
71 
73  template<typename GV, typename RF, int n, typename F>
75  : public Dune::PDELab::GridFunctionBase<Dune::PDELab::
76  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> >,
77  LocalCallableToGridFunctionAdapter<GV,RF,n,F> >
78  {
79  GV gv;
80  F f;
81  public:
82  typedef Dune::PDELab::
83  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> > Traits;
84 
86  LocalCallableToGridFunctionAdapter (const GV& gv_, const F& f_) : gv(gv_), f(f_) {}
87 
89  inline const GV& getGridView () const {return gv;}
90 
92  inline void evaluate (const typename Traits::ElementType& e,
93  const typename Traits::DomainType& xl,
94  typename Traits::RangeType& y) const
95  {
96  y = f(e,xl);
97  }
98  };
99 
100 #ifdef DOXYGEN
102 
112  template <typename GV, typename F>
113  WrapperConformingToGridFunctionInterface makeGridFunctionFromCallable (const GV& gv, const F& f)
114  {}
115 #endif
116 
117 #ifndef DOXYGEN
119  template <typename GV, typename F>
120  auto makeGridFunctionFromCallable (const GV& gv, const F& f)
121  -> typename std::enable_if<
122  AlwaysTrue <
123  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
124  >::value,
125  GlobalCallableToGridFunctionAdapter<
126  GV,
127  typename CallableAdapterGetRangeFieldType<
128  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
129  >::Type,
130  CallableAdapterGetDim<
131  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
132  >::dim,
133  F>
134  >::type
135  {
136  typedef typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate X;
137  X x;
138  typedef decltype(f(x)) ReturnType;
141  typedef GlobalCallableToGridFunctionAdapter<GV,RF,dim,F> TheType;
142  return TheType(gv,f);
143  }
144 
147  template <typename GV, typename F>
148  auto makeGridFunctionFromCallable (const GV& gv, const F& f)
149  -> typename std::enable_if<
150  AlwaysTrue <
151  decltype(f(
152  std::declval<typename GV::template Codim<0>::Entity>(),
153  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
154  ))
155  >::value,
156  LocalCallableToGridFunctionAdapter<
157  GV,
158  typename CallableAdapterGetRangeFieldType<
159  decltype(f(
160  std::declval<typename GV::template Codim<0>::Entity>(),
161  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
162  ))
163  >::Type,
164  CallableAdapterGetDim<
165  decltype(f(
166  std::declval<typename GV::template Codim<0>::Entity>(),
167  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
168  ))
169  >::dim,
170  F>
171  >::type
172  {
173  typedef typename GV::template Codim<0>::Entity E;
174  E e;
175  typedef typename E::Geometry::LocalCoordinate X;
176  X x;
177  typedef decltype(f(e,x)) ReturnType;
180  typedef LocalCallableToGridFunctionAdapter<GV,RF,dim,F> TheType;
181  return TheType(gv,f);
182  }
183 #endif // DOXYGEN
184 
185 
186  /*************************************
187  * Instationary grid function adapters
188  *************************************/
189 
190 
193  template<typename GV, typename RF, int n, typename F, typename P>
195  : public Dune::PDELab::GridFunctionBase<Dune::PDELab::
196  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> >,
197  GlobalCallableToInstationaryGridFunctionAdapter<GV,RF,n,F,P> >
198  {
199  GV gv;
200  F f;
201  P& p;
202  public:
203  typedef Dune::PDELab::
204  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> > Traits;
205 
207  GlobalCallableToInstationaryGridFunctionAdapter (const GV& gv_, const F& f_, P& p_)
208  : gv(gv_), f(f_), p(p_)
209  {}
210 
212  inline const GV& getGridView () const {return gv;}
213 
215  inline void evaluate (const typename Traits::ElementType& e,
216  const typename Traits::DomainType& xl,
217  typename Traits::RangeType& y) const
218  {
219  typename Traits::DomainType xg = e.geometry().global(xl);
220  y = f(xg);
221  }
222 
223  // pass time to parameter object
224  void setTime (RF t) {
225  p.setTime(t);
226  }
227  };
228 
231  template<typename GV, typename RF, int n, typename F, typename P>
233  : public Dune::PDELab::GridFunctionBase<Dune::PDELab::
234  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> >,
235  LocalCallableToInstationaryGridFunctionAdapter<GV,RF,n,F,P> >
236  {
237  GV gv;
238  F f;
239  P& p;
240  public:
241  typedef Dune::PDELab::
242  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> > Traits;
243 
245  LocalCallableToInstationaryGridFunctionAdapter (const GV& gv_, const F& f_, P& p_) : gv(gv_), f(f_), p(p_) {}
246 
248  inline const GV& getGridView () const {return gv;}
249 
251  inline void evaluate (const typename Traits::ElementType& e,
252  const typename Traits::DomainType& xl,
253  typename Traits::RangeType& y) const
254  {
255  y = f(e,xl);
256  }
257 
258  // pass time to parameter object
259  void setTime (RF t) {
260  p.setTime(t);
261  }
262  };
263 
264 #ifdef DOXYGEN
266 
278  template <typename GV, typename F>
279  WrapperConformingToGridFunctionInterface makeInstationaryGridFunctionFromCallable (const GV& gv, const F& f)
280  {}
281 #endif
282 
283 #ifndef DOXYGEN
286  template <typename GV, typename F, typename PARAM>
287  auto makeInstationaryGridFunctionFromCallable (const GV& gv, const F& f, PARAM& param)
288  -> typename std::enable_if<
289  AlwaysTrue <
290  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
291  >::value,
292  GlobalCallableToInstationaryGridFunctionAdapter<
293  GV,
294  typename CallableAdapterGetRangeFieldType<
295  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
296  >::Type,
297  CallableAdapterGetDim<
298  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
299  >::dim,
300  F,
301  PARAM>
302  >::type
303  {
304  typedef typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate X;
305  X x;
306  typedef decltype(f(x)) ReturnType;
309  typedef GlobalCallableToInstationaryGridFunctionAdapter<GV,RF,dim,F,PARAM> TheType;
310  return TheType(gv,f,param);
311  }
312 
315  template <typename GV, typename F, typename PARAM>
316  auto makeInstationaryGridFunctionFromCallable (const GV& gv, const F& f, PARAM& param)
317  -> typename std::enable_if<
318  AlwaysTrue <
319  decltype(f(
320  std::declval<typename GV::template Codim<0>::Entity>(),
321  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
322  ))
323  >::value,
324  LocalCallableToInstationaryGridFunctionAdapter<
325  GV,
326  typename CallableAdapterGetRangeFieldType<
327  decltype(f(
328  std::declval<typename GV::template Codim<0>::Entity>(),
329  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
330  ))
331  >::Type,
332  CallableAdapterGetDim<
333  decltype(f(
334  std::declval<typename GV::template Codim<0>::Entity>(),
335  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
336  ))
337  >::dim,
338  F,
339  PARAM>
340  >::type
341  {
342  typedef typename GV::template Codim<0>::Entity E;
343  E e;
344  typedef typename E::Geometry::LocalCoordinate X;
345  X x;
346  typedef decltype(f(e,x)) ReturnType;
349  typedef LocalCallableToInstationaryGridFunctionAdapter<GV,RF,dim,F,PARAM> TheType;
350  return TheType(gv,f,param);
351  }
352 #endif // DOXYGEN
353 
354 
355  /*****************************
356  * Boundary condition adapters
357  *****************************/
358 
360  template<typename F>
363  {
364  F f;
365 
366  public:
369 
371  template<typename I>
372  bool isDirichlet(const I & intersection,
373  const Dune::FieldVector<typename I::ctype, I::mydimension> & coord
374  ) const
375  {
376  auto xg = intersection.geometry().global(coord);
377  return f(xg);
378  }
379 
380  template<typename I>
381  bool isNeumann(const I & ig,
382  const Dune::FieldVector<typename I::ctype, I::mydimension> & coord
383  ) const
384  {
385  return !isDirichlet( ig, coord );
386  }
387 
388  };
389 
391  template<typename F>
395  {
396  const F f;
397 
398  public:
399 
401  : f( f_ )
402  {}
403 
404  template<typename I>
405  bool isDirichlet(const I & ig, const Dune::FieldVector<typename I::ctype, I::mydimension> & coord
406  ) const
407  {
408  return(f(ig.intersection(),coord));
409  }
410 
411  template<typename I>
412  bool isNeumann(const I & ig,
413  const Dune::FieldVector<typename I::ctype, I::mydimension> & coord
414  ) const
415  {
416  return !isDirichlet( ig, coord );
417  }
418  };
419 
420 #ifdef DOXYGEN
422 
433  template <typename GV, typename F>
434  BoundaryConditionAdapter makebBoundaryConditionFromCallable (const GV& gv, const F& f)
435 #endif
436 
437 #ifndef DOXYGEN
439  template<typename GV, typename F>
440  auto makeBoundaryConditionFromCallable (const GV& gv, const F& f)
441  -> typename std::enable_if<
442  AlwaysTrue <
443  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
444  >::value,
445  GlobalCallableToBoundaryConditionAdapter<F>
446  >::type
447  {
448  return GlobalCallableToBoundaryConditionAdapter<F>(f);
449  }
450 
453  template<typename GV, typename F>
454  auto makeBoundaryConditionFromCallable (const GV& gv, const F& f)
455  -> typename std::enable_if<
456  AlwaysTrue <
457  decltype(f(std::declval<typename GV::Intersection>(),
458  std::declval<typename GV::Intersection::Geometry::LocalCoordinate>()
459  ))
460  >::value
461  ,
462  LocalCallableToBoundaryConditionAdapter<F>
463  >::type
464  {
465  return LocalCallableToBoundaryConditionAdapter<F>(f);
466  }
467 #endif
468 
469 
470  }
471 }
472 #endif // DUNE_PDELAB_FUNCTION_CALLABLEADAPTER_HH
static const int dim
Definition: adaptivity.hh:84
const P & p
Definition: constraints.hh:148
const IG & ig
Definition: constraints.hh:149
XG & xg
Definition: interpolate.hh:55
const Entity & e
Definition: localfunctionspace.hh:121
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
WrapperConformingToGridFunctionInterface makeInstationaryGridFunctionFromCallable(const GV &gv, const F &f)
Create a GridFunction from callable and parameter class with setTime method.
Definition: callableadapter.hh:279
WrapperConformingToGridFunctionInterface makeGridFunctionFromCallable(const GV &gv, const F &f)
Create a GridFunction adapter from a callable.
Definition: callableadapter.hh:113
Dune::FieldVector< GV::Grid::ctype, GV::dimension > DomainType
domain type in dim-size coordinates
Definition: function.hh:50
GV::Traits::template Codim< 0 >::Entity ElementType
codim 0 entity
Definition: function.hh:119
leaf of a function tree
Definition: function.hh:302
Definition: constraintsparameters.hh:26
Definition: constraintsparameters.hh:122
Adapter for callables f(x) expecting a global coordinate x.
Definition: callableadapter.hh:28
Dune::PDELab::GridFunctionTraits< GV, RF, n, Dune::FieldVector< RF, n > > Traits
Definition: callableadapter.hh:33
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &xl, typename Traits::RangeType &y) const
evaluate extended function on element
Definition: callableadapter.hh:42
const GV & getGridView() const
get a reference to the grid view
Definition: callableadapter.hh:39
GlobalCallableToGridFunctionAdapter(const GV &gv_, const F &f_)
construct from grid view
Definition: callableadapter.hh:36
Definition: callableadapter.hh:52
@ dim
Definition: callableadapter.hh:53
Definition: callableadapter.hh:62
T Type
Definition: callableadapter.hh:63
Adapter for callables f(e,x) expecting an entity e and a global coordinate x.
Definition: callableadapter.hh:78
Dune::PDELab::GridFunctionTraits< GV, RF, n, Dune::FieldVector< RF, n > > Traits
Definition: callableadapter.hh:83
LocalCallableToGridFunctionAdapter(const GV &gv_, const F &f_)
construct from grid view
Definition: callableadapter.hh:86
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &xl, typename Traits::RangeType &y) const
evaluate extended function on element
Definition: callableadapter.hh:92
const GV & getGridView() const
get a reference to the grid view
Definition: callableadapter.hh:89
return a PDELab GridFunction (with setTime method) defined by a parameter class and a callable f(x) g...
Definition: callableadapter.hh:198
void setTime(RF t)
Definition: callableadapter.hh:224
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &xl, typename Traits::RangeType &y) const
evaluate extended function on element
Definition: callableadapter.hh:215
const GV & getGridView() const
get a reference to the grid view
Definition: callableadapter.hh:212
GlobalCallableToInstationaryGridFunctionAdapter(const GV &gv_, const F &f_, P &p_)
construct from grid view
Definition: callableadapter.hh:207
Dune::PDELab::GridFunctionTraits< GV, RF, n, Dune::FieldVector< RF, n > > Traits
Definition: callableadapter.hh:204
return a PDELab GridFunction (with setTime method) defined by a parameter class and a callable f(e,...
Definition: callableadapter.hh:236
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &xl, typename Traits::RangeType &y) const
evaluate extended function on element
Definition: callableadapter.hh:251
Dune::PDELab::GridFunctionTraits< GV, RF, n, Dune::FieldVector< RF, n > > Traits
Definition: callableadapter.hh:242
LocalCallableToInstationaryGridFunctionAdapter(const GV &gv_, const F &f_, P &p_)
construct from grid view
Definition: callableadapter.hh:245
void setTime(RF t)
Definition: callableadapter.hh:259
const GV & getGridView() const
get a reference to the grid view
Definition: callableadapter.hh:248
Adapter for boundary cond from a callable taking global coordinates.
Definition: callableadapter.hh:363
bool isDirichlet(const I &intersection, const Dune::FieldVector< typename I::ctype, I::mydimension > &coord) const
Test whether boundary is Dirichlet-constrained.
Definition: callableadapter.hh:372
bool isNeumann(const I &ig, const Dune::FieldVector< typename I::ctype, I::mydimension > &coord) const
Definition: callableadapter.hh:381
GlobalCallableToBoundaryConditionAdapter(F f_)
construct from functor
Definition: callableadapter.hh:368
Adapter for boundary cond from a callable taking an entity and local coordinates.
Definition: callableadapter.hh:395
LocalCallableToBoundaryConditionAdapter(const F &f_)
Definition: callableadapter.hh:400
bool isNeumann(const I &ig, const Dune::FieldVector< typename I::ctype, I::mydimension > &coord) const
Definition: callableadapter.hh:412
bool isDirichlet(const I &ig, const Dune::FieldVector< typename I::ctype, I::mydimension > &coord) const
Definition: callableadapter.hh:405
static const unsigned int value
Definition: gridfunctionspace/tags.hh:139