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

3
4
5
6
7
#include <type_traits>

#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>

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

10
namespace Dune
Praetorius, Simon's avatar
Praetorius, Simon committed
11
{
Stenger, Florian's avatar
Stenger, Florian committed
12
  namespace Vtk
Praetorius, Simon's avatar
Praetorius, Simon committed
13
  {
Stenger, Florian's avatar
Stenger, Florian committed
14
15
16
17
    /// 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
18
    {
Stenger, Florian's avatar
Stenger, Florian committed
19
20
21
22
      using Self = LocalFunctionWrapper;
      using Interface = LocalFunctionInterface<GridView>;
      using Entity = typename Interface::Entity;
      using LocalCoordinate = typename Interface::LocalCoordinate;
Praetorius, Simon's avatar
Praetorius, Simon committed
23

Stenger, Florian's avatar
Stenger, Florian committed
24
25
      template <class F, class D>
      using Range = std::decay_t<decltype(std::declval<F>()(std::declval<D>()))>;
Praetorius, Simon's avatar
Praetorius, Simon committed
26

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

Stenger, Florian's avatar
Stenger, Florian committed
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
68
69
      /// 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;
      }

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    // Evaluate a component of a vector valued data
    template <class T,
      std::enable_if_t<IsIndexable<T,int>::value, int> = 0>
    double evaluateImpl (int comp, T const& value) const
    {
      return value[comp];
    }

    // Return the scalar values
    template <class T,
      std::enable_if_t<not IsIndexable<T,int>::value, int> = 0>
    double evaluateImpl (int comp, T const& value) const
    {
      assert(comp == 0);
      return value;
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
86

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

Stenger, Florian's avatar
Stenger, Florian committed
91
  } // end namespace Vtk
92
} // end namespace Dune