defaultvtkfunction.hh 2.5 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
#pragma once

3
4
5
6
#include <type_traits>

#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>
7
#include <dune/common/typetraits.hh>
8

Stenger, Florian's avatar
Stenger, Florian committed
9
#include "localfunctioninterface.hh"
Praetorius, Simon's avatar
Praetorius, Simon committed
10

11
namespace Dune
Praetorius, Simon's avatar
Praetorius, Simon committed
12
{
Stenger, Florian's avatar
Stenger, Florian committed
13
  namespace Vtk
Praetorius, Simon's avatar
Praetorius, Simon committed
14
  {
Stenger, Florian's avatar
Stenger, Florian committed
15
16
17
18
    /// Type erasure for dune-functions LocalFunction interface
    template <class GridView, class LocalFunction>
    class LocalFunctionWrapper final
        : public LocalFunctionInterface<GridView>
Praetorius, Simon's avatar
Praetorius, Simon committed
19
    {
Stenger, Florian's avatar
Stenger, Florian committed
20
21
22
23
      using Self = LocalFunctionWrapper;
      using Interface = LocalFunctionInterface<GridView>;
      using Entity = typename Interface::Entity;
      using LocalCoordinate = typename Interface::LocalCoordinate;
Praetorius, Simon's avatar
Praetorius, Simon committed
24

Stenger, Florian's avatar
Stenger, Florian committed
25
26
27
28
    public:
      /// Constructor. Stores a copy of the passed `localFct` in a local variable.
      template <class LocalFct,
        disableCopyMove<Self, LocalFct> = 0>
29
      explicit LocalFunctionWrapper (LocalFct&& localFct)
Stenger, Florian's avatar
Stenger, Florian committed
30
31
        : localFct_(std::forward<LocalFct>(localFct))
      {}
Praetorius, Simon's avatar
Praetorius, Simon committed
32

Stenger, Florian's avatar
Stenger, Florian committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
      /// Bind the LocalFunction to the Entity
      virtual void bind (Entity const& entity) override
      {
        localFct_.bind(entity);
      }

      /// Unbind the LocalFunction from the Entity
      virtual void unbind () override
      {
        localFct_.unbind();
      }

      /// Evaluate the LocalFunction in LocalCoordinates
      virtual double evaluate (int comp, LocalCoordinate const& xi) const override
      {
        return evaluateImpl(comp, localFct_(xi));
      }

    private:
      // Evaluate a component of a vector valued data
      template <class T, int N, int M>
      double evaluateImpl (int comp, FieldMatrix<T,N,M> const& mat) const
      {
        int r = comp / 3;
        int c = comp % 3;
        return r < N && c < M ? mat[r][c] : 0.0;
      }

      // Evaluate a component of a vector valued data
      template <class T, int N>
      double evaluateImpl (int comp, FieldVector<T,N> const& vec) const
      {
        return comp < N ? vec[comp] : 0.0;
      }

68
69
      // Evaluate a component of a vector valued data
      template <class T,
70
        std::enable_if_t<Dune::is_indexable<T,int>::value, int> = 0>
71
72
73
74
      double evaluateImpl (int comp, T const& value) const
      {
        return value[comp];
      }
75

76
77
      // Return the scalar values
      template <class T,
78
        std::enable_if_t<not Dune::is_indexable<T,int>::value, int> = 0>
79
80
81
82
83
      double evaluateImpl (int comp, T const& value) const
      {
        assert(comp == 0);
        return value;
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
84

Stenger, Florian's avatar
Stenger, Florian committed
85
86
87
    private:
      LocalFunction localFct_;
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
88

Stenger, Florian's avatar
Stenger, Florian committed
89
  } // end namespace Vtk
90
} // end namespace Dune