CoordsGridFunction.hpp 3.11 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
      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)};
        }
      };

Praetorius, Simon's avatar
Praetorius, Simon committed
28
      template <class T, int N>
29
30
31
32
33
34
35
36
37
38
39
      Dune::FieldVector<T, N> const& operator()(Dune::FieldVector<T, N> const& x) const
      {
        return x;
      }

      friend Derivative partial(CoordsFunction const& /*f*/, index_t<0>)
      {
        return Derivative{};
      }

      friend int order(CoordsFunction const& /*f*/, int /*d*/)
Praetorius, Simon's avatar
Praetorius, Simon committed
40
      {
41
        return 1;
Praetorius, Simon's avatar
Praetorius, Simon committed
42
43
44
45
      }
    };


46
47
    /// A functor that evaluates to a component of the global coordinates
    struct CoordsCompFunction
Praetorius, Simon's avatar
Praetorius, Simon committed
48
    {
49
50
51
52
53
      struct Derivative
      {
        explicit Derivative(int comp)
          : comp_(comp)
        {}
Praetorius, Simon's avatar
Praetorius, Simon committed
54

55
56
57
58
59
        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
60

61
62
          return result;
        }
Praetorius, Simon's avatar
Praetorius, Simon committed
63

64
65
66
67
68
69
      private:
        int comp_;
      };

      /// Constructor. Stores the component `comp` of the coordinates.
      explicit CoordsCompFunction(int comp)
Praetorius, Simon's avatar
Praetorius, Simon committed
70
71
72
73
        : comp_(comp)
      {}

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

      friend Derivative partial(CoordsCompFunction const& f, index_t<0>)
Praetorius, Simon's avatar
Praetorius, Simon committed
80
      {
81
82
        return Derivative{f.comp_};
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
83

84
85
86
      friend int order(CoordsCompFunction const& /*f*/, int /*d*/)
      {
        return 1;
Praetorius, Simon's avatar
Praetorius, Simon committed
87
88
89
90
91
92
      }

    private:
      int comp_;
    };

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

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

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

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

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

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

  /**
    * \addtogroup GridFunctions
    * @{
    **/
Praetorius, Simon's avatar
Praetorius, Simon committed
124

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

132
  /// Generator for GridFunction from the pre-GridFunction \ref CoordsCompFunction
Praetorius, Simon's avatar
Praetorius, Simon committed
133
  template <class GridView>
134
  auto makeGridFunction(Operation::CoordsCompFunction const& f, GridView const& gridView, Dune::PriorityTag<1>)
Praetorius, Simon's avatar
Praetorius, Simon committed
135
136
137
138
139
140
141
  {
    return makeAnalyticGridFunction(f, gridView);
  }

  /** @} **/

} // end namespace AMDiS