AMDiS  0.1
The Adaptive Multi-Dimensional Simulation Toolbox
DerivativeGridFunction.hpp
1 #pragma once
2 
3 #include <type_traits>
4 
5 #include <dune/grid/utility/hierarchicsearch.hh>
6 
7 #include <amdis/common/Concepts.hpp>
8 #include <amdis/common/DerivativeTraits.hpp>
9 #include <amdis/gridfunctions/Derivative.hpp>
10 #include <amdis/gridfunctions/GridFunction.hpp>
11 
12 namespace AMDiS
13 {
14  namespace Impl
15  {
16  template <class LF, class Sig>
17  struct CheckFunctorConcept
18  {
19  static_assert(Concepts::Functor<LF, Sig>, "Derivative of LocalFunction can not be called as a functor.");
20  };
21 
22  template <class Traits>
23  struct CheckValidRange
24  {
25  static_assert(!std::is_same_v<typename Traits::Range, Dune::Functions::InvalidRange>, "Invalid Range.");
26  };
27  }
28 
31 
43  template <class GridFunction, class Type>
45  {
46  using GridFctRange = typename GridFunction::Range;
47  using GridFctDomain = typename GridFunction::Domain;
48  using RawSignature = typename Dune::Functions::SignatureTraits<GridFctRange(GridFctDomain)>::RawSignature;
49 
51  using LocalFunction = TYPEOF( derivativeOf(localFunction(std::declval<GridFunction>()), std::declval<Type>()) ) ;
52 
53  using LocalFctRange = typename Traits::Range;
54  using LocalFctDomain = typename GridFunction::EntitySet::LocalCoordinate;
55 
56  CHECK_CONCEPT(Impl::CheckValidRange<Traits>);
57  CHECK_CONCEPT(Impl::CheckFunctorConcept<LocalFunction, LocalFctRange(LocalFctDomain)>);
58 
59  enum { hasDerivative = false };
60 
61  public:
63  using Range = typename Traits::Range;
64 
66  using Domain = GridFctDomain;
67 
69  using EntitySet = typename GridFunction::EntitySet;
70 
71  public:
73  explicit DerivativeGridFunction(GridFunction const& gridFct, Type const& type)
74  : gridFct_{gridFct}
75  , type_{type}
76  {}
77 
79  Range operator()(Domain const& x) const
80  {
81  auto gv = entitySet().gridView();
82 
83  using GridView = decltype(gv);
84  using Grid = typename GridView::Grid;
85  using IS = typename GridView::IndexSet;
86 
87  Dune::HierarchicSearch<Grid,IS> hsearch{gv.grid(), gv.indexSet()};
88 
89  auto element = hsearch.findEntity(x);
90  auto geometry = element.geometry();
91  auto localFct = makeLocalFunction();
92  localFct.bind(element);
93  return localFct(geometry.local(x));
94  }
95 
97  LocalFunction makeLocalFunction() const
98  {
99  return derivativeOf(localFunction(gridFct_), type_);
100  }
101 
103  EntitySet const& entitySet() const
104  {
105  return gridFct_.entitySet();
106  }
107 
108  private:
109  GridFunction gridFct_;
110  Type type_;
111  };
112 
113 
117 
127  template <class GridFct, class Type,
128  class LocalFct = decltype( localFunction(std::declval<GridFct>()) ),
129  REQUIRES(not GridFct::hasDerivative)>
130  auto derivativeOf(GridFct const& gridFct, Type const& type)
131  {
132  static_assert(Concepts::HasDerivative<LocalFct,Type>,
133  "derivativeOf(LocalFunction,type) not defined!");
134  return DerivativeGridFunction<GridFct,Type>{gridFct, type};
135  }
136 
137 
138 #ifndef DOXYGEN
139  template <class Expr, class Type>
141  {
143 
144  struct Creator
145  {
146  template <class GridView>
147  static auto create(Self const& self, GridView const& gridView)
148  {
149  return derivativeOf(makeGridFunction(self.expr_, gridView), self.type_);
150  }
151  };
152 
153  Expr expr_;
154  Type type_ = {};
155  };
156 
157  namespace Traits
158  {
159  template <class Expr,class Type>
161  : std::true_type {};
162  }
163 #endif
164 
165 
169 
178  template <class Expr>
179  auto gradientOf(Expr const& expr)
180  {
182  }
183 
185  template <class Expr>
186  auto divergenceOf(Expr const& expr)
187  {
189  }
190 
192  template <class Expr>
193  auto partialDerivativeOf(Expr const& expr, std::size_t i)
194  {
196  }
197 
200 } // end namespace AMDiS
constexpr bool GridFunction
GridFunction GF is a Type that has LocalFunction and provides some typedefs for Domain, Range, and EntitySet.
Definition: GridFunction.hpp:72
auto divergenceOf(Expr const &expr)
Generates a Gridfunction representing the divergence of a vector-valued GridFunction.
Definition: DerivativeGridFunction.hpp:186
Definition: GridFunction.hpp:26
Definition: DerivativeTraits.hpp:21
decltype(auto) makeGridFunction(PreGridFct const &preGridFct, GridView const &gridView)
Generator for Gridfunctions from Expressions (PreGridfunctions)
Definition: GridFunction.hpp:154
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
EntitySet const & entitySet() const
Return the EntitySet of the GridFunction.
Definition: DerivativeGridFunction.hpp:103
auto partialDerivativeOf(Expr const &expr, std::size_t i)
Generates a Gridfunction representing the partial derivative of a GridFunction.
Definition: DerivativeGridFunction.hpp:193
auto gradientOf(Expr const &expr)
Definition: DerivativeGridFunction.hpp:179
typename Traits::Range Range
The Range of the derivative of the GridFunction.
Definition: DerivativeGridFunction.hpp:63
auto derivativeOf(AnalyticLocalFunction< R(D), LC, F > const &lf, Type const &type)
Definition: AnalyticGridFunction.hpp:103
A Gridfunction that returns the derivative when calling localFunction.
Definition: DerivativeGridFunction.hpp:44
Definition: DerivativeTraits.hpp:28
Definition: DerivativeGridFunction.hpp:140
GridFctDomain Domain
The domain of the GridFunction.
Definition: DerivativeGridFunction.hpp:66
LocalFunction makeLocalFunction() const
Return the derivative-localFunction of the GridFunction.
Definition: DerivativeGridFunction.hpp:97
DerivativeGridFunction(GridFunction const &gridFct, Type const &type)
Constructor. Stores a copy of gridFct.
Definition: DerivativeGridFunction.hpp:73
Range operator()(Domain const &x) const
Evaluate derivative in global coordinates. NOTE: expensive.
Definition: DerivativeGridFunction.hpp:79
Definition: DerivativeGridFunction.hpp:144
typename GridFunction::EntitySet EntitySet
The EntitySet of the GridFunction.
Definition: DerivativeGridFunction.hpp:69