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
8
9
#include <dune/common/typeutilities.hh>

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

    /// A functor that evaluates to the global coordinates
    struct CoordsFunction
Praetorius, Simon's avatar
Praetorius, Simon committed
18
    {
19
20
21
22
23
24
25
26
27
28
29
      template <class T, int N>
      Dune::FieldVector<T, N> const& operator()(Dune::FieldVector<T, N> const& x) const
      {
        return x;
      }

      friend int order(CoordsFunction const& /*f*/, int /*d*/)
      {
        return 1;
      }

30
31
32
33
34
35
36
37
38
39
40
41
      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
42
43
44
    };


45
46
    /// A functor that evaluates to a component of the global coordinates
    struct CoordsCompFunction
Praetorius, Simon's avatar
Praetorius, Simon committed
47
    {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      /// 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_];
      }

      friend int order(CoordsCompFunction const& /*f*/, int /*d*/)
      {
        return 1;
      }

64
65
66
67
68
      struct Derivative
      {
        explicit Derivative(int comp)
          : comp_(comp)
        {}
Praetorius, Simon's avatar
Praetorius, Simon committed
69

70
71
72
73
74
        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
75

76
77
          return result;
        }
Praetorius, Simon's avatar
Praetorius, Simon committed
78

79
80
81
82
83
      private:
        int comp_;
      };

      friend Derivative partial(CoordsCompFunction const& f, index_t<0>)
Praetorius, Simon's avatar
Praetorius, Simon committed
84
      {
85
86
        return Derivative{f.comp_};
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
87
88
89
90
91

    private:
      int comp_;
    };

92
    /** @} **/
Praetorius, Simon's avatar
Praetorius, Simon committed
93

94
  } // end namespace Operation
Praetorius, Simon's avatar
Praetorius, Simon committed
95
96
97
98

  /// Generator for \ref CoordsFunction
  inline auto X()
  {
99
    return Operation::CoordsFunction{};
Praetorius, Simon's avatar
Praetorius, Simon committed
100
101
102
103
104
  }

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

108
109
110
111
112
113
114
115
116
117
118
  namespace Traits
  {
    template <>
    struct IsPreGridFunction<Operation::CoordsFunction>
      : std::true_type {};

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

119
  namespace Impl
Praetorius, Simon's avatar
Praetorius, Simon committed
120
  {
121
122
123
124
125
126
    /// Generator for GridFunction from the pre-GridFunction \ref CoordsFunction
    template <class GridView>
    auto makeGridFunctionImpl(Operation::CoordsFunction const& f, GridView const& gridView, Dune::PriorityTag<1>)
    {
      return makeAnalyticGridFunction(f, gridView);
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
127

128
129
130
131
132
133
    /// Generator for GridFunction from the pre-GridFunction \ref CoordsCompFunction
    template <class GridView>
    auto makeGridFunctionImpl(Operation::CoordsCompFunction const& f, GridView const& gridView, Dune::PriorityTag<1>)
    {
      return makeAnalyticGridFunction(f, gridView);
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
134

135
  } // end namespace Impl
Praetorius, Simon's avatar
Praetorius, Simon committed
136
} // end namespace AMDiS