dune-vtk  0.2
localfunction.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <type_traits>
5 
6 #include <dune/common/typetraits.hh>
7 
9 #include "legacyvtkfunction.hh"
10 #include "defaultvtkfunction.hh"
11 
12 namespace Dune
13 {
14  namespace Vtk
15  {
18 
21  template <class GridView>
23  {
24  using Self = LocalFunction;
25  using Entity = typename GridView::template Codim<0>::Entity;
26  using LocalCoordinate = typename Entity::Geometry::LocalCoordinate;
27 
28  template <class LF, class E>
29  using HasBind = decltype((std::declval<LF>().bind(std::declval<E>()), true));
30 
31  private:
32  struct RangeProxy
33  {
34  using value_type = double;
35  using field_type = double;
36 
37  RangeProxy (LocalFunctionInterface<GridView> const& localFct,
38  std::vector<int> const& components,
39  LocalCoordinate const& local)
40  : localFct_(localFct)
41  , components_(components)
42  , local_(local)
43  {}
44 
45  std::size_t size () const
46  {
47  return components_.size();
48  }
49 
50  double operator[] (std::size_t i) const
51  {
52  return i < size() ? localFct_.evaluate(components_[i], local_) : 0.0;
53  }
54 
55  private:
56  LocalFunctionInterface<GridView> const& localFct_;
57  std::vector<int> const& components_;
58  LocalCoordinate local_;
59  };
60 
61  public:
63  template <class LF,
64  disableCopyMove<Self, LF> = 0,
65  HasBind<LF,Entity> = true>
66  explicit LocalFunction (LF&& lf)
67  : localFct_(std::make_shared<LocalFunctionWrapper<GridView,LF>>(std::forward<LF>(lf)))
68  {}
69 
71  explicit LocalFunction (std::shared_ptr<VTKFunction<GridView> const> const& lf)
72  : localFct_(std::make_shared<VTKLocalFunctionWrapper<GridView>>(lf))
73  {}
74 
77  LocalFunction () = default;
78 
80  void bind (Entity const& entity)
81  {
82  assert(bool(localFct_));
83  localFct_->bind(entity);
84  }
85 
87  void unbind ()
88  {
89  assert(bool(localFct_));
90  localFct_->unbind();
91  }
92 
94  RangeProxy operator() (LocalCoordinate const& xi) const
95  {
96  assert(bool(localFct_));
97  return {*localFct_, components_, xi};
98  }
99 
101  double evaluate (int c, LocalCoordinate const& xi) const
102  {
103  assert(bool(localFct_));
104  return c < int(components_.size()) ? localFct_->evaluate(components_[c], xi) : 0.0;
105  }
106 
107  void setComponents (std::vector<int> components)
108  {
109  components_ = std::move(components);
110  }
111 
112  private:
113  std::shared_ptr<LocalFunctionInterface<GridView>> localFct_ = nullptr;
114  std::vector<int> components_;
115  };
116 
117  } // end namespace Vtk
118 } // end namespace Dune
Definition: writer.hh:13
Type erasure for dune-functions LocalFunction interface.
Definition: defaultvtkfunction.hh:19
Type erasure for Legacy VTKFunction.
Definition: legacyvtkfunction.hh:17
A Vtk::LocalFunction is a function-like object that can be bound to a grid element an that provides a...
Definition: localfunction.hh:23
LocalFunction(LF &&lf)
Construct the Vtk::LocalFunction from any function object that has a bind(element) method.
Definition: localfunction.hh:66
LocalFunction(std::shared_ptr< VTKFunction< GridView > const > const &lf)
Construct a Vtk::LocalFunction from a legacy VTKFunction.
Definition: localfunction.hh:71
void unbind()
Unbind from the currently bound entity.
Definition: localfunction.hh:87
RangeProxy operator()(LocalCoordinate const &xi) const
Return a proxy object to access the components of the range vector.
Definition: localfunction.hh:94
double evaluate(int c, LocalCoordinate const &xi) const
Evaluate the cth component of the Range value at local coordinate xi
Definition: localfunction.hh:101
void setComponents(std::vector< int > components)
Definition: localfunction.hh:107
void bind(Entity const &entity)
Bind the function to the grid entity.
Definition: localfunction.hh:80
An abstract base class for LocalFunctions that can be bound to an element and evaluated in local coor...
Definition: localfunctioninterface.hh:11