ConstantGridFunction.hpp 2.61 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once

#include <functional>
#include <type_traits>

#include <dune/common/diagonalmatrix.hh>
#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>
#include <dune/common/identitymatrix.hh>
#include <dune/functions/common/defaultderivativetraits.hh>

#include <dune/amdis/common/Utility.hpp>
#include <dune/amdis/gridfunctions/AnalyticGridFunction.hpp>
#include <dune/amdis/gridfunctions/GridFunctionConcepts.hpp>

namespace AMDiS
{
  /**
    * \addtogroup GridFunctions
    * @{
    **/

  template <class T>
  struct ConstantFunction
  {
    using Range = Underlying_t<T>;

    using DerivativeRange = typename Dune::Functions::DefaultDerivativeTraits<Range>::Range;
    using Derivative = ConstantFunction<DerivativeRange>;

    explicit ConstantFunction(T const& value)
      : value_(value)
    {}

    template <class GlobalCoordinate>
    Range const& operator()(GlobalCoordinate const& /*x*/) const
    {
      return value_;
    }

    friend Derivative derivative(ConstantFunction const& /*f*/)
    {
      return Derivative{DerivativeRange(0)};
    }

    friend int order(ConstantFunction const& /*f*/)
    {
      return 0;
    }

  private:
    T value_;
  };


  /** @} **/

  namespace Concepts
  {
    /** \addtogroup Concepts
     *  @{
     **/

    namespace Definition
    {
      template <class T>
      struct ConstantToGridFunction
        : std::is_arithmetic<T> {};

      template <class T>
      struct ConstantToGridFunction<std::reference_wrapper<T>>
        : ConstantToGridFunction<T> {};

      template <class T, int N>
      struct ConstantToGridFunction<Dune::FieldVector<T, N>>
        : ConstantToGridFunction<T> {};

      template <class T, int N, int M>
      struct ConstantToGridFunction<Dune::FieldMatrix<T, N, M>>
        : ConstantToGridFunction<T> {};

      template <class T, int N>
      struct ConstantToGridFunction<Dune::IdentityMatrix<T, N>>
        : ConstantToGridFunction<T> {};

      template <class T, int N>
      struct ConstantToGridFunction<Dune::DiagonalMatrix<T, N>>
        : ConstantToGridFunction<T> {};

    } // end namespace Definition


    template <class T>
    constexpr bool ConstantToGridFunction =
      Definition::ConstantToGridFunction<T>::value;

    /** @} **/

  } // end namespace Concepts


  /**
    * \addtogroup GridFunctions
    * @{
    **/

  template <class T, class GridView,
    REQUIRES(Concepts::ConstantToGridFunction<T>)>
  auto makeGridFunction(T const& value, GridView const& gridView, Dune::PriorityTag<2>)
  {
    return makeGridFunction(ConstantFunction<T>{value}, gridView, Dune::PriorityTag<3>{});
  }

  /** @} **/

} // end namespace AMDiS