Basic.hpp 3.75 KB
Newer Older
1
2
3
4
#pragma once

#include <algorithm>

5
6
7
8
9
#include <amdis/common/IndexSeq.hpp>
#include <amdis/common/Math.hpp>
#include <amdis/common/Mpl.hpp>
#include <amdis/common/Concepts.hpp>
#include <amdis/common/ScalarTypes.hpp>
10
11
12

namespace AMDiS
{
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  namespace Concepts
  {
    namespace Definition
    {
      struct HasFunctorOrder
      {
        template <class F, std::size_t... I>
        auto requires_(F&& f, Indices<I...>) -> decltype( order(f, int(I)...) );
      };
    }

    template <class F, std::size_t N>
    constexpr bool HasFunctorOrder = models<Definition::HasFunctorOrder(F, MakeSeq_t<N>)>;
  }

28
29
  namespace Operation
  {
30
    /** \addtogroup operations
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
     *  @{
     **/

    /// Functor representing a static constant value
    template <class T, T value>
    struct StaticConstant
    {
      template <class... Ts>
      constexpr T operator()(Ts const&... /*args*/) const
      {
        return value;
      }
    };

    using Zero = StaticConstant<int, 0>;
    using One = StaticConstant<int, 1>;

    template <class T, T value, class... Int>
49
    constexpr int order(StaticConstant<T,value> const&, Int... /*orders*/)
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    {
      return 0;
    }

    template <class T, T value, std::size_t J>
    constexpr auto partial(StaticConstant<T,value>, index_t<J>)
    {
      return Zero{};
    }

    // -------------------------------------------------------------------------

    /// (Unary-)Functor representing the identity
    struct Id
    {
      template <class T>
      constexpr T const& operator()(T const& x) const
      {
        return x;
      }

      friend constexpr int order(Id, int d)
      {
        return d;
      }

      friend auto partial(Id, index_t<0>)
      {
        return Zero{};
      }
    };

    // -------------------------------------------------------------------------

    /// Functor representing a constant value
    template <class T>
    struct Constant
    {
      constexpr Constant(T value)
        : value_(value)
      {}

      template <class... Ts>
      constexpr T const& operator()(Ts const&... /*args*/) const
      {
        return value_;
      }

    private:
      T value_;
    };

    template <class T, class... Int>
103
    constexpr int order(Constant<T> const&, Int... /*orders*/)
104
105
106
107
108
109
110
111
112
113
114
115
116
    {
      return 0;
    }

    template <class T, std::size_t J>
    constexpr auto partial(Constant<T> const&, index_t<J>)
    {
      return Zero{};
    }

    // -------------------------------------------------------------------------

    template <std::size_t I>
117
    struct Arg
118
119
120
121
122
123
124
125
126
    {
      template <class... Ts>
      constexpr auto&& operator()(Ts&&... args) const
      {
        return std::get<I>(std::forward_as_tuple(std::forward<Ts>(args)...));
      }
    };

    template <std::size_t I, class... Int>
127
    constexpr int order(Arg<I> const&, Int... orders)
128
    {
129
      return std::get<I>(std::tie(orders...));
130
131
132
    }

    template <std::size_t I, std::size_t J>
133
    constexpr auto partial(Arg<I>, index_t<J>)
134
135
136
137
138
139
    {
      return StaticConstant<int,(I==J ? 1 : 0)>{};
    }

    // -------------------------------------------------------------------------

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    template <std::size_t I>
    struct Get
    {
      template <class T, int N>
      constexpr T const& operator()(Dune::FieldVector<T,N> const& vec) const
      {
        return vec[I];
      }

      friend constexpr int order(Get, int d)
      {
        return d;
      }
    };

    struct Get_
    {
      explicit constexpr Get_(std::size_t i)
        : i_(i)
      {}

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

      friend constexpr int order(Get_, int d)
      {
        return d;
      }

      std::size_t i_;
    };

175
176
177
178
    /** @} **/

  } // end namespace Operation
} // end namespace AMDiS