DerivativeGridFunction.hpp 3.44 KB
Newer Older
1
2
3
4
5
6
#pragma once

#include <type_traits>

#include <dune/functions/common/defaultderivativetraits.hh>

Praetorius, Simon's avatar
Praetorius, Simon committed
7
8
#include <dune/amdis/gridfunctions/GridFunctionConcepts.hpp>

9
10
11
namespace AMDiS
{
  /**
Praetorius, Simon's avatar
Praetorius, Simon committed
12
    * \addtogroup GridFunctions
13
14
15
    * @{
    **/

16
17
18
19
20
21
22
23
24
25
26
  /// \brief A Gridfunction that returns the derivative when calling localFunction.
  /**
   * Wrapps the GridFunction so that \ref localFunction returns a LocalFunction
   * representing the derivative of the LocalFunction of the GridFunction.
   *
   * \tparam GridFunction The GridFunction that is wrapped.
   *
   * **Requirements:**
   * - `GridFunction` models \ref Concepts::GridFunction and the LocalFunction has a derivative.
   **/
  template <class GridFunction>
27
28
  class DerivativeGridFunction
  {
29
30
31
32
    using GridFctRange = typename GridFunction::Range;
    using GridFctDomain = typename GridFunction::Domain;
    using RawSignature = typename Dune::Functions::SignatureTraits<GridFctRange(GridFctDomain)>::RawSignature;
    using LocalFunction = std::decay_t<decltype(derivative(localFunction(std::declval<GridFunction>())))>;
33
34

  public:
35
    /// The Range of the derivative of the GridFunction
36
    using Range = typename Dune::Functions::DefaultDerivativeTraits<RawSignature>::Range;
37
    /// The domain of the GridFunction
38
    using Domain = GridFctDomain;
39
40
    /// The EntitySet of the GridFunction
    using EntitySet = typename GridFunction::EntitySet;
41
42

  public:
43
44
    /// Constructor. Stores a copy of gridFct.
    explicit DerivativeGridFunction(GridFunction const& gridFct)
45
46
47
      : gridFct_(gridFct)
    {}

48
    /// NOTE: no global derivative available
49
50
51
52
53
54
    Range operator()(Domain const& x) const
    {
      error_exit("Not implemented");
      return Range(0);
    }

55
    /// Return the derivative-localFunction of the GridFunction.
56
57
    friend LocalFunction localFunction(DerivativeGridFunction const& gf)
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
58
59
60
      return derivative(localFunction(gf.gridFct_));
    }

61
    /// Return the \ref EntitySet of the \ref GridFunction.
62
63
64
65
66
67
    EntitySet const& entitySet() const
    {
      return gridFct_.entitySet();
    }

  private:
68
    GridFunction gridFct_;
69
70
  };

71
72

#ifndef DOXYGEN
Praetorius, Simon's avatar
Praetorius, Simon committed
73
  template <class GridFct,
74
    REQUIRES(not Concepts::HasDerivative<GridFct> &&
75
                Concepts::HasLocalFunctionDerivative<GridFct>)>
Praetorius, Simon's avatar
Praetorius, Simon committed
76
  auto derivative(GridFct const& gridFct)
77
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
78
    return DerivativeGridFunction<GridFct>{gridFct};
79
80
  }

81
82
83
84
85
86
87
88
89
90
91
92
93

  template <class Expr>
  struct DerivativePreGridFunction
  {
    Expr expr;
  };

  namespace Traits
  {
    template <class Expr>
    struct IsPreGridFunction<DerivativePreGridFunction<Expr>>
      : std::true_type {};
  }
94
#endif
95
96


97
98
99
100
101
102
103
104
  /// \brief Generator function for DerivativeGridFunction expressions. \relates DerivativeGridFunction
  /**
   * Generates a derivative of a GridFunction. See \ref DerivativeGridFunction.
   *
   * **Examples:**
   * - `gradientAtQP(prob.getSolution(_0))`
   * - `gradientAtQP(X(0) + X(1) + prob.getSolution(_0))`
   **/
105
106
107
108
109
110
  template <class Expr>
  auto gradientAtQP(Expr const& expr)
  {
    return DerivativePreGridFunction<Expr>{expr};
  }

111
112
  /** @} **/

113
114
115
116
117
118
119
120
121
122
  namespace Impl
  {
    /// Generator function for \ref AnalyticGridFunction expressions from \ref AnalyticPreGridFunction.
    template <class Expr, class GridView>
    auto makeGridFunctionImpl(DerivativePreGridFunction<Expr> const& pre, GridView const& gridView, Dune::PriorityTag<3>)
    {
      return derivative(Impl::makeGridFunctionImpl(pre.expr, gridView, Dune::PriorityTag<10>{}));
    }

  } // end namespace Impl
123
} // end namespace AMDiS