CoordsGridFunction.hpp 3.06 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
9
#include <amdis/gridfunctions/AnalyticGridFunction.hpp>

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

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

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

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

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

43
44
45
46
47
48
49
50
51
52
53
54
      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)};
        }
      };
      friend Derivative partial(CoordsFunction const& /*f*/, index_t<0>)
      {
        return Derivative{};
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
55
56
57
    };


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

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

    public:
73
74
75
76
77
78
79
80
81
82
83
      /// 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_];
      }

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

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

95
96
97
98
99
        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
100

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

104
105
106
107
      private:
        int comp_;
      };

108
      friend Derivative partial(Self const& f, index_t<0>)
Praetorius, Simon's avatar
Praetorius, Simon committed
109
      {
110
111
        return Derivative{f.comp_};
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
112
113
114
115
116

    private:
      int comp_;
    };

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

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

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

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

133
134
135
136
137
138
139
140
141
142
143
  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
144
} // end namespace AMDiS