dune-pdelab  2.4.1
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 
9 namespace Dune {
10  namespace PDELab {
11 
12  /************************
13  * Grid function adapters
14  ************************/
15 
17  template<typename GV, typename RF, int n, typename F>
19  : public Dune::PDELab::GridFunctionBase<Dune::PDELab::
20  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> >,
21  GlobalCallableToGridFunctionAdapter<GV,RF,n,F> >
22  {
23  GV gv;
24  F f;
25  public:
26  typedef Dune::PDELab::
28 
30  GlobalCallableToGridFunctionAdapter (const GV& gv_, const F& f_) : gv(gv_), f(f_) {}
31 
33  inline const GV& getGridView () {return gv;}
34 
36  inline void evaluate (const typename Traits::ElementType& e,
37  const typename Traits::DomainType& xl,
38  typename Traits::RangeType& y) const
39  {
40  typename Traits::DomainType xg = e.geometry().global(xl);
41  y = f(xg);
42  }
43  };
44 
45  template<typename T>
47  enum {dim=1};
48  };
49 
50  template<typename T, int n>
51  struct CallableAdapterGetDim< FieldVector<T,n> > {
52  enum {dim=n};
53  };
54 
55  template<typename T>
57  typedef T Type;
58  };
59 
60  template<typename T, int n>
61  struct CallableAdapterGetRangeFieldType< FieldVector<T,n> > {
62  typedef T Type;
63  };
64 
65 
67  template<typename GV, typename RF, int n, typename F>
69  : public Dune::PDELab::GridFunctionBase<Dune::PDELab::
70  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> >,
71  LocalCallableToGridFunctionAdapter<GV,RF,n,F> >
72  {
73  GV gv;
74  F f;
75  public:
76  typedef Dune::PDELab::
78 
80  LocalCallableToGridFunctionAdapter (const GV& gv_, const F& f_) : gv(gv_), f(f_) {}
81 
83  inline const GV& getGridView () {return gv;}
84 
86  inline void evaluate (const typename Traits::ElementType& e,
87  const typename Traits::DomainType& xl,
88  typename Traits::RangeType& y) const
89  {
90  y = f(e,xl);
91  }
92  };
93 
94 #ifdef DOXYGEN
95 
106  template <typename GV, typename F>
107  WrapperConformingToGridFunctionInterface makeGridFunctionFromCallable (const GV& gv, const F& f)
108  {}
109 #endif
110 
111 #ifndef DOXYGEN
112 
113  template <typename GV, typename F>
114  auto makeGridFunctionFromCallable (const GV& gv, const F& f)
115  -> typename std::enable_if<
116  AlwaysTrue <
117  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
118  >::value,
120  GV,
122  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
123  >::Type,
125  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
126  >::dim,
127  F>
128  >::type
129  {
130  typedef typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate X;
131  X x;
132  typedef decltype(f(x)) ReturnType;
136  return TheType(gv,f);
137  }
138 
141  template <typename GV, typename F>
142  auto makeGridFunctionFromCallable (const GV& gv, const F& f)
143  -> typename std::enable_if<
144  AlwaysTrue <
145  decltype(f(
146  std::declval<typename GV::template Codim<0>::Entity>(),
147  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
148  ))
149  >::value,
151  GV,
153  decltype(f(
154  std::declval<typename GV::template Codim<0>::Entity>(),
155  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
156  ))
157  >::Type,
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  >::dim,
164  F>
165  >::type
166  {
167  typedef typename GV::template Codim<0>::Entity E;
168  E e;
169  typedef typename E::Geometry::LocalCoordinate X;
170  X x;
171  typedef decltype(f(e,x)) ReturnType;
175  return TheType(gv,f);
176  }
177 #endif // DOXYGEN
178 
179 
180  /*************************************
181  * Instationary grid function adapters
182  *************************************/
183 
184 
187  template<typename GV, typename RF, int n, typename F, typename P>
189  : public Dune::PDELab::GridFunctionBase<Dune::PDELab::
190  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> >,
191  GlobalCallableToInstationaryGridFunctionAdapter<GV,RF,n,F,P> >
192  {
193  GV gv;
194  F f;
195  P& p;
196  public:
197  typedef Dune::PDELab::
199 
201  GlobalCallableToInstationaryGridFunctionAdapter (const GV& gv_, const F& f_, P& p_)
202  : gv(gv_), f(f_), p(p_)
203  {}
204 
206  inline const GV& getGridView () {return gv;}
207 
209  inline void evaluate (const typename Traits::ElementType& e,
210  const typename Traits::DomainType& xl,
211  typename Traits::RangeType& y) const
212  {
213  typename Traits::DomainType xg = e.geometry().global(xl);
214  y = f(xg);
215  }
216 
217  // pass time to parameter object
218  void setTime (RF t) {
219  p.setTime(t);
220  }
221  };
222 
225  template<typename GV, typename RF, int n, typename F, typename P>
227  : public Dune::PDELab::GridFunctionBase<Dune::PDELab::
228  GridFunctionTraits<GV,RF,n,Dune::FieldVector<RF,n> >,
229  LocalCallableToInstationaryGridFunctionAdapter<GV,RF,n,F,P> >
230  {
231  GV gv;
232  F f;
233  P& p;
234  public:
235  typedef Dune::PDELab::
237 
239  LocalCallableToInstationaryGridFunctionAdapter (const GV& gv_, const F& f_, P& p_) : gv(gv_), f(f_), p(p_) {}
240 
242  inline const GV& getGridView () {return gv;}
243 
245  inline void evaluate (const typename Traits::ElementType& e,
246  const typename Traits::DomainType& xl,
247  typename Traits::RangeType& y) const
248  {
249  y = f(e,xl);
250  }
251 
252  // pass time to parameter object
253  void setTime (RF t) {
254  p.setTime(t);
255  }
256  };
257 
258 #ifdef DOXYGEN
259 
272  template <typename GV, typename F>
273  WrapperConformingToGridFunctionInterface makeInstationaryGridFunctionFromCallable (const GV& gv, const F& f)
274  {}
275 #endif
276 
277 #ifndef DOXYGEN
278 
280  template <typename GV, typename F, typename PARAM>
281  auto makeInstationaryGridFunctionFromCallable (const GV& gv, const F& f, PARAM& param)
282  -> typename std::enable_if<
283  AlwaysTrue <
284  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
285  >::value,
287  GV,
289  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
290  >::Type,
292  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
293  >::dim,
294  F,
295  PARAM>
296  >::type
297  {
298  typedef typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate X;
299  X x;
300  typedef decltype(f(x)) ReturnType;
304  return TheType(gv,f,param);
305  }
306 
309  template <typename GV, typename F, typename PARAM>
310  auto makeInstationaryGridFunctionFromCallable (const GV& gv, const F& f, PARAM& param)
311  -> typename std::enable_if<
312  AlwaysTrue <
313  decltype(f(
314  std::declval<typename GV::template Codim<0>::Entity>(),
315  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
316  ))
317  >::value,
319  GV,
321  decltype(f(
322  std::declval<typename GV::template Codim<0>::Entity>(),
323  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
324  ))
325  >::Type,
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  >::dim,
332  F,
333  PARAM>
334  >::type
335  {
336  typedef typename GV::template Codim<0>::Entity E;
337  E e;
338  typedef typename E::Geometry::LocalCoordinate X;
339  X x;
340  typedef decltype(f(e,x)) ReturnType;
344  return TheType(gv,f,param);
345  }
346 #endif // DOXYGEN
347 
348 
349  /*****************************
350  * Boundary condition adapters
351  *****************************/
352 
354  template<typename F>
357  {
358  F f;
359 
360  public:
363 
365  template<typename I>
366  bool isDirichlet(const I & intersection,
367  const Dune::FieldVector<typename I::ctype, I::dimension-1> & coord
368  ) const
369  {
370  Dune::FieldVector<typename I::ctype, I::dimension> xg = intersection.geometry().global(coord);
371  return f(xg);
372  }
373 
374  template<typename I>
375  bool isNeumann(const I & ig,
376  const Dune::FieldVector<typename I::ctype, I::dimension-1> & coord
377  ) const
378  {
379  return !isDirichlet( ig, coord );
380  }
381 
382  };
383 
385  template<typename F>
389  {
390  const F f;
391 
392  public:
393 
395  : f( f_ )
396  {}
397 
398  template<typename I>
399  bool isDirichlet(const I & ig, const Dune::FieldVector<typename I::ctype, I::dimension-1> & coord
400  ) const
401  {
402  return(f(ig.intersection(),coord));
403  }
404 
405  template<typename I>
406  bool isNeumann(const I & ig,
407  const Dune::FieldVector<typename I::ctype, I::dimension-1> & coord
408  ) const
409  {
410  return !isDirichlet( ig, coord );
411  }
412  };
413 
414 #ifdef DOXYGEN
415 
427  template <typename GV, typename F>
428  BoundaryConditionAdapter makebBoundaryConditionFromCallable (const GV& gv, const F& f)
429 #endif
430 
431 #ifndef DOXYGEN
432 
433  template<typename GV, typename F>
434  auto makeBoundaryConditionFromCallable (const GV& gv, const F& f)
435  -> typename std::enable_if<
436  AlwaysTrue <
437  decltype(f(std::declval<typename GV::template Codim<0>::Entity::Geometry::GlobalCoordinate>()))
438  >::value,
440  >::type
441  {
443  }
444 
447  template<typename GV, typename F>
448  auto makeBoundaryConditionFromCallable (const GV& gv, const F& f)
449  -> typename std::enable_if<
450  AlwaysTrue <
451  decltype(f(
452  std::declval<typename GV::template Codim<0>::Entity>(),
453  std::declval<typename GV::template Codim<0>::Entity::Geometry::LocalCoordinate>()
454  ))
455  >::value,
457  >::type
458  {
460  }
461 #endif
462 
463 
464  }
465 }
466 #endif
Dune::PDELab::GridFunctionTraits< GV, RF, n, Dune::FieldVector< RF, n > > Traits
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:86
static const unsigned int value
Definition: gridfunctionspace/tags.hh:177
Definition: callableadapter.hh:46
bool isNeumann(const I &ig, const Dune::FieldVector< typename I::ctype, I::dimension-1 > &coord) const
Definition: callableadapter.hh:375
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:36
Dune::PDELab::GridFunctionTraits< GV, RF, n, Dune::FieldVector< RF, n > > Traits
Definition: callableadapter.hh:198
WrapperConformingToGridFunctionInterface makeGridFunctionFromCallable(const GV &gv, const F &f)
Create a GridFunction adapter from a callable.
Definition: callableadapter.hh:107
const IG & ig
Definition: constraints.hh:148
static const int dim
Definition: adaptivity.hh:83
Dune::PDELab::GridFunctionTraits< GV, RF, n, Dune::FieldVector< RF, n > > Traits
Definition: callableadapter.hh:27
void setTime(RF t)
Definition: callableadapter.hh:253
Adapter for callables f(e,x) expecting an entity e and a global coordinate x.
Definition: callableadapter.hh:68
Adapter for boundary cond from a callable taking global coordinates.
Definition: callableadapter.hh:355
Definition: adaptivity.hh:27
GV::Traits::template Codim< 0 >::Entity ElementType
codim 0 entity
Definition: function.hh:117
LocalCallableToGridFunctionAdapter(const GV &gv_, const F &f_)
construct from grid view
Definition: callableadapter.hh:80
GlobalCallableToBoundaryConditionAdapter(F f_)
construct from functor
Definition: callableadapter.hh:362
WrapperConformingToGridFunctionInterface makeInstationaryGridFunctionFromCallable(const GV &gv, const F &f)
Create a GridFunction from callable and parameter class with setTime method.
Definition: callableadapter.hh:273
const GV & getGridView()
get a reference to the grid view
Definition: callableadapter.hh:83
Definition: callableadapter.hh:56
LocalCallableToBoundaryConditionAdapter(const F &f_)
Definition: callableadapter.hh:394
const Entity & e
Definition: localfunctionspace.hh:111
Dune::FieldVector< GV::Grid::ctype, GV::dimension > DomainType
domain type in dim-size coordinates
Definition: function.hh:48
const GV & getGridView()
get a reference to the grid view
Definition: callableadapter.hh:242
Dune::PDELab::GridFunctionTraits< GV, RF, n, Dune::FieldVector< RF, n > > Traits
Definition: callableadapter.hh:77
LocalCallableToInstationaryGridFunctionAdapter(const GV &gv_, const F &f_, P &p_)
construct from grid view
Definition: callableadapter.hh:239
Adapter for boundary cond from a callable taking an entity and local coordinates. ...
Definition: callableadapter.hh:386
bool isDirichlet(const I &ig, const Dune::FieldVector< typename I::ctype, I::dimension-1 > &coord) const
Definition: callableadapter.hh:399
T Type
Definition: callableadapter.hh:57
Definition: constraintsparameters.hh:24
return a PDELab GridFunction (with setTime method) defined by a parameter class and a callable f(x) g...
Definition: callableadapter.hh:188
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:209
const P & p
Definition: constraints.hh:147
XG & xg
Definition: interpolate.hh:67
void setTime(RF t)
Definition: callableadapter.hh:218
return a PDELab GridFunction (with setTime method) defined by a parameter class and a callable f(e...
Definition: callableadapter.hh:226
bool isNeumann(const I &ig, const Dune::FieldVector< typename I::ctype, I::dimension-1 > &coord) const
Definition: callableadapter.hh:406
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:245
bool isDirichlet(const I &intersection, const Dune::FieldVector< typename I::ctype, I::dimension-1 > &coord) const
Test whether boundary is Dirichlet-constrained.
Definition: callableadapter.hh:366
Adapter for callables f(x) expecting a global coordinate x.
Definition: callableadapter.hh:18
GlobalCallableToGridFunctionAdapter(const GV &gv_, const F &f_)
construct from grid view
Definition: callableadapter.hh:30
leaf of a function tree
Definition: function.hh:576
const GV & getGridView()
get a reference to the grid view
Definition: callableadapter.hh:206
const GV & getGridView()
get a reference to the grid view
Definition: callableadapter.hh:33
traits class holding the function signature, same as in local function
Definition: function.hh:175
GlobalCallableToInstationaryGridFunctionAdapter(const GV &gv_, const F &f_, P &p_)
construct from grid view
Definition: callableadapter.hh:201
Definition: constraintsparameters.hh:120