CoordsGridFunction.hpp 3.12 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
#pragma once

#include <dune/common/fvector.hh>
#include <dune/common/fmatrix.hh>
5
#include <dune/common/diagonalmatrix.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
6
7
#include <dune/common/typeutilities.hh>

8
#include <amdis/common/DerivativeTraits.hpp>
9
10
#include <amdis/gridfunctions/AnalyticGridFunction.hpp>

Praetorius, Simon's avatar
Praetorius, Simon committed
11
12
namespace AMDiS
{
13
  namespace Operation
Praetorius, Simon's avatar
Praetorius, Simon committed
14
  {
15
16
17
18
19
20
    /** \addtogroup operations
     *  @{
     **/

    /// A functor that evaluates to the global coordinates
    struct CoordsFunction
Praetorius, Simon's avatar
Praetorius, Simon committed
21
    {
22
23
24
25
26
27
28
29
30
31
32
      using Self = CoordsFunction;

      struct Creator
      {
        template <class GridView>
        static auto create(Self const& f, GridView const& gridView)
        {
          return AnalyticGridFunction<Self, GridView>{f, gridView};
        }
      };

33
34
35
36
37
38
      template <class T, int N>
      Dune::FieldVector<T, N> const& operator()(Dune::FieldVector<T, N> const& x) const
      {
        return x;
      }

39
      friend int order(CoordsFunction /*f*/, int /*d*/)
40
41
42
43
      {
        return 1;
      }

44
45
46
47
48
49
50
51
      struct Derivative
      {
        template <class T, int N>
        Dune::DiagonalMatrix<T, N> const& operator()(Dune::FieldVector<T, N> const& x) const
        {
          return Dune::DiagonalMatrix<T,N>{T(1)};
        }
      };
52
      friend Derivative derivative(CoordsFunction const& /*f*/, tag::gradient)
53
54
55
      {
        return Derivative{};
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
56
57
58
    };


59
60
    /// A functor that evaluates to a component of the global coordinates
    struct CoordsCompFunction
Praetorius, Simon's avatar
Praetorius, Simon committed
61
    {
62
63
64
65
66
67
68
69
70
71
72
73
      using Self = CoordsCompFunction;

      struct Creator
      {
        template <class GridView>
        static auto create(Self const& f, GridView const& gridView)
        {
          return AnalyticGridFunction<Self, GridView>{f, gridView};
        }
      };

    public:
74
75
76
77
78
79
80
81
82
83
84
      /// Constructor. Stores the component `comp` of the coordinates.
      explicit CoordsCompFunction(int comp)
        : comp_(comp)
      {}

      template <class T, int N>
      T const& operator()(Dune::FieldVector<T, N> const& x) const
      {
        return x[comp_];
      }

85
      friend int order(CoordsCompFunction /*f*/, int /*d*/)
86
87
88
89
      {
        return 1;
      }

90
91
92
93
94
      struct Derivative
      {
        explicit Derivative(int comp)
          : comp_(comp)
        {}
Praetorius, Simon's avatar
Praetorius, Simon committed
95

96
97
98
99
100
        template <class T, int N>
        Dune::FieldVector<T, N> operator()(Dune::FieldVector<T, N> const& x) const
        {
          Dune::FieldVector<T, N> result(0);
          result[comp_] = T(1);
Praetorius, Simon's avatar
Praetorius, Simon committed
101

102
103
          return result;
        }
Praetorius, Simon's avatar
Praetorius, Simon committed
104

105
106
107
108
      private:
        int comp_;
      };

109
      friend Derivative derivative(Self const& f, tag::gradient)
Praetorius, Simon's avatar
Praetorius, Simon committed
110
      {
111
112
        return Derivative{f.comp_};
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
113
114
115
116
117

    private:
      int comp_;
    };

118
    /** @} **/
Praetorius, Simon's avatar
Praetorius, Simon committed
119

120
  } // end namespace Operation
Praetorius, Simon's avatar
Praetorius, Simon committed
121
122
123
124

  /// Generator for \ref CoordsFunction
  inline auto X()
  {
125
    return Operation::CoordsFunction{};
Praetorius, Simon's avatar
Praetorius, Simon committed
126
127
128
129
130
  }

  /// Generator for \ref CoordsCompFunction
  inline auto X(int comp)
  {
131
    return Operation::CoordsCompFunction{comp};
Praetorius, Simon's avatar
Praetorius, Simon committed
132
133
  }

134
135
136
137
138
139
140
141
142
143
144
  namespace Traits
  {
    template <>
    struct IsPreGridFunction<Operation::CoordsFunction>
      : std::true_type {};

    template <>
    struct IsPreGridFunction<Operation::CoordsCompFunction>
      : std::true_type {};
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
145
} // end namespace AMDiS