AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
DiscreteFunction.hpp
1 #pragma once
2 
3 #include <optional>
4 #include <vector>
5 
6 #include <dune/common/ftraits.hh>
7 #include <dune/common/typeutilities.hh>
8 #include <dune/functions/common/defaultderivativetraits.hh>
9 #include <dune/functions/functionspacebases/flatvectorview.hh>
10 #include <dune/functions/gridfunctions/gridviewentityset.hh>
11 #include <dune/typetree/childextraction.hh>
12 
13 #include <amdis/common/Tags.hpp>
14 #include <amdis/functions/NodeCache.hpp>
15 #include <amdis/linearalgebra/Access.hpp>
16 #include <amdis/typetree/FiniteElementType.hpp>
17 #include <amdis/typetree/RangeType.hpp>
18 #include <amdis/typetree/TreePath.hpp>
19 
20 namespace AMDiS
21 {
24 
35  template <class Coeff, class GB, class TreePath, class Range = void>
37 
38 
40  template <class Coeff, class GB, class TreePath, class R>
41  class DiscreteFunction
42  : public DiscreteFunction<Coeff const,GB,TreePath,R>
43  {
46 
47  using Coefficients = Coeff;
48  using GlobalBasis = GB;
49 
50  public:
52  template <class... Path>
53  DiscreteFunction(Coefficients& coefficients, GlobalBasis const& basis, Path... path)
54  : Super(coefficients, basis, path...)
55  , mutableCoeff_(&coefficients)
56  {}
57 
58  template <class... Path>
59  DiscreteFunction(Coefficients& coefficients, std::shared_ptr<GlobalBasis const> const& basis, Path... path)
60  : DiscreteFunction(coefficients, *basis, path...)
61  {}
62 
64  template <class DV, class... Path,
65  Dune::disableCopyMove<DiscreteFunction,DV> = 0,
66  class Coeff_ = TYPEOF(std::declval<DV>().coefficients()),
67  class GB_ = TYPEOF(std::declval<DV>().basis())>
68  explicit DiscreteFunction(DV&& dofVector, Path... path)
69  : DiscreteFunction(dofVector.coefficients(), dofVector.basis(), path...)
70  {}
71 
72  public:
75 
82  template <class Expr, class Tag = tag::average>
83  void interpolate_noalias(Expr&& expr, Tag strategy = {});
84 
86 
95  template <class Expr, class Tag = tag::average>
96  void interpolate(Expr&& expr, Tag strategy = {});
97 
99  template <class Expr>
100  Self& operator<<(Expr&& expr)
101  {
102  interpolate(FWD(expr));
103  return *this;
104  }
105 
107  template <class Expr>
108  Self& operator+=(Expr&& expr)
109  {
110  interpolate((*this) + expr);
111  return *this;
112  }
113 
115  template <class Expr>
116  Self& operator-=(Expr&& expr)
117  {
118  interpolate((*this) - expr);
119  return *this;
120  }
121 
123  Coefficients& coefficients()
124  {
125  return *mutableCoeff_;
126  }
127 
129  using Super::coefficients;
130 
131  template <class Range = void, class... Indices>
132  auto child(Indices... ii)
133  {
134  auto tp = cat(this->treePath_, makeTreePath(ii...));
135  return DiscreteFunction<Coeff, GB, TYPEOF(makeTreePath(tp)), Range>{*mutableCoeff_, this->basis(), tp};
136  }
137 
138  using Super::child;
139 
140  protected:
141  Coefficients* mutableCoeff_;
142  };
143 
145  template <class Coeff, class GB, class TreePath, class R>
146  class DiscreteFunction<Coeff const,GB,TreePath,R>
147  {
148  private:
149  using Coefficients = std::remove_const_t<Coeff>;
150  using GlobalBasis = GB;
151  using LocalView = typename GlobalBasis::LocalView;
152  using GridView = typename GlobalBasis::GridView;
153 
154  using Coefficient = TYPEOF(std::declval<Traits::Access>()(std::declval<Coeff const>(), std::declval<typename GB::MultiIndex>()));
155 
156  using Tree = typename GlobalBasis::LocalView::Tree;
157  using SubTree = typename Dune::TypeTree::ChildForTreePath<Tree, TreePath>;
158 
159  public:
161  using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
162 
164  using Domain = typename EntitySet::GlobalCoordinate;
165 
167  using Range = std::conditional_t<std::is_same_v<R,void>, RangeType_t<SubTree,Coefficient>, R>;
168 
171  enum { hasDerivative = false };
172 
173  private:
175  template <class Type>
176  class LocalFunction;
177 
178  public:
180  template <class... Path>
181  DiscreteFunction(Coefficients const& coefficients, GlobalBasis const& basis, Path... path)
182  : coefficients_(&coefficients)
183  , basis_(&basis)
184  , treePath_(makeTreePath(path...))
185  , entitySet_(basis_->gridView())
186  {}
187 
188  template <class... Path>
189  DiscreteFunction(Coefficients const& coefficients, std::shared_ptr<GlobalBasis const> const& basisPtr, Path... path)
190  : DiscreteFunction(coefficients, *basisPtr, path...)
191  {}
192 
194  template <class DV, class... Path,
195  Dune::disableCopyMove<DiscreteFunction,DV> = 0,
196  class Coeff_ = TYPEOF(std::declval<DV>().coefficients()),
197  class GB_ = TYPEOF(std::declval<DV>().basis())>
198  explicit DiscreteFunction(DV const& dofVector, Path... path)
199  : DiscreteFunction(dofVector.coefficients(), dofVector.basis(), path...)
200  {}
201 
203  Range operator()(Domain const& x) const;
204 
206  LocalFunction<tag::value> makeLocalFunction() const
207  {
208  return {std::make_shared<LocalView>(basis().localView()), treePath(), coefficients(), tag::value{}};
209  }
210 
212  EntitySet const& entitySet() const
213  {
214  return entitySet_;
215  }
216 
218  GlobalBasis const& basis() const
219  {
220  return *basis_;
221  }
222 
224  TreePath const& treePath() const
225  {
226  return treePath_;
227  }
228 
230  Coefficients const& coefficients() const
231  {
232  return *coefficients_;
233  }
234 
235  template <class Range = void, class... Indices>
236  auto child(Indices... ii) const
237  {
238  auto tp = cat(this->treePath_, makeTreePath(ii...));
239  return DiscreteFunction<Coeff const, GB, TYPEOF(makeTreePath(tp)), Range>{*coefficients_, *basis_, tp};
240  }
241 
242  protected:
243  Coefficients const* coefficients_;
244  GlobalBasis const* basis_;
245  TreePath treePath_;
246  EntitySet entitySet_;
247  };
248 
249 
250  // deduction guides
251 
252  template <class C, class Basis, class... Indices,
253  class GB = Underlying_t<Basis>,
254  class TP = TYPEOF(makeTreePath(std::declval<Indices>()...)),
255  REQUIRES(Concepts::GlobalBasis<GB>)>
256  DiscreteFunction(C&, Basis const&, Indices...)
258 
259  template <class DV, class... Indices,
260  class C = decltype(std::declval<DV>().coefficients()),
261  class GB = decltype(std::declval<DV>().basis()),
262  class TP = TYPEOF(makeTreePath(std::declval<Indices>()...))>
263  DiscreteFunction(DV&, Indices...)
264  -> DiscreteFunction<std::remove_reference_t<C>,Underlying_t<GB>,TP>;
265 
266 
267  // grid functions representing the DOFVector
268  // -----------------------------------------
269 
271  template <class Range = void, class C, class GB, class TP, class R, class... Indices>
273  {
274  return df.template child<Range>(ii...);
275  }
276 
278  template <class Range = void, class C, class GB, class TP, class R, class... Indices>
280  {
281  return df.template child<Range>(ii...);
282  }
283 
285  template <class Range = void, class DV, class... Indices,
286  class C = decltype(std::declval<DV>().coefficients()),
287  class GB = decltype(std::declval<DV>().basis()),
288  class TP = TYPEOF(makeTreePath(std::declval<Indices>()...))>
289  auto valueOf(DV& dofVec, Indices... ii)
290  {
291  using DF = DiscreteFunction<std::remove_reference_t<C>,Underlying_t<GB>,TP,Range>;
292  return DF{dofVec.coefficients(), dofVec.basis(), makeTreePath(ii...)};
293  }
294 
295 } // end namespace AMDiS
296 
297 #include "DiscreteLocalFunction.inc.hpp"
298 #include "DiscreteFunction.inc.hpp"
Self & operator<<(Expr &&expr)
Interpolation of GridFunction to DOFVector, alias to interpolate()
Definition: DiscreteFunction.hpp:100
DiscreteFunction(Coefficients const &coefficients, GlobalBasis const &basis, Path... path)
Constructor. Stores a pointer to the dofVector and a copy of the treePath.
Definition: DiscreteFunction.hpp:181
std::index_sequence< I... > Indices
class that represents a sequence of indices
Definition: Index.hpp:40
Self & operator-=(Expr &&expr)
interpolate (*this) - expr to DOFVector
Definition: DiscreteFunction.hpp:116
A Const DiscreteFunction.
Definition: DiscreteFunction.hpp:146
void interpolate(Expr &&expr, Tag strategy={})
Interpolation of GridFunction to DOFVector.
Definition: DiscreteFunction.inc.hpp:61
AMDiS::LocalView< Self > LocalView
Type of the local view on the restriction of the basis to a single element.
Definition: GlobalBasis.hpp:70
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
auto cat(Dune::TypeTree::HybridTreePath< S... > const &tp0, Dune::TypeTree::HybridTreePath< T... > const &tp1)
Concatenate two treepaths.
Definition: TreePath.hpp:209
A mutable view on the subspace of a DOFVector,.
Definition: DiscreteFunction.hpp:36
Node_t< typename GlobalBasis::PreBasis, PrefixPath > Tree
Tree of local finite elements / local shape function sets.
Definition: LocalView.hpp:41
Definition: DerivativeTraits.hpp:18
auto valueOf(DiscreteFunction< C, GB, TP, R > &df, Indices... ii)
A Generator for the childs of a mutable DiscreteFunction.
Definition: DiscreteFunction.hpp:272
DiscreteFunction(DV const &dofVector, Path... path)
Construct a DiscreteFunction directly from a DOFVector.
Definition: DiscreteFunction.hpp:198
DiscreteFunction(DV &&dofVector, Path... path)
Construct a DiscreteFunction directly from a DOFVector.
Definition: DiscreteFunction.hpp:68
typename PreBasis::GridView GridView
The grid view that the FE space is defined on.
Definition: GlobalBasis.hpp:66
Coefficients & coefficients()
Return the mutable DOFVector.
Definition: DiscreteFunction.hpp:123
typename Impl::RangeTypeImpl< Node, R, typename Node::NodeTag >::type RangeType_t
Range type of a node in the basis tree, composed of the leaf basis range types.
Definition: RangeType.hpp:47
DiscreteFunction(Coefficients &coefficients, GlobalBasis const &basis, Path... path)
Constructor. Stores a pointer to the mutable dofvector.
Definition: DiscreteFunction.hpp:53
TreePath const & treePath() const
Return treePath associated with this discrete function.
Definition: DiscreteFunction.hpp:224
Dune::Functions::GridViewEntitySet< GridView, 0 > EntitySet
Set of entities the DiscreteFunction is defined on.
Definition: DiscreteFunction.hpp:161
std::conditional_t< std::is_same_v< R, void >, RangeType_t< SubTree, Coefficient >, R > Range
Range type of this DiscreteFunction. If R=void deduce the Range automatically, using RangeType_t...
Definition: DiscreteFunction.hpp:167
Coefficients const & coefficients() const
Return const coefficient vector.
Definition: DiscreteFunction.hpp:230
EntitySet const & entitySet() const
Return a Dune::Functions::GridViewEntitySet.
Definition: DiscreteFunction.hpp:212
LocalFunction< tag::value > makeLocalFunction() const
Create a local function for this view on the DOFVector.
Definition: DiscreteFunction.hpp:206
void interpolate_noalias(Expr &&expr, Tag strategy={})
Interpolation of GridFunction to DOFVector, assuming that there is no reference to this DOFVector in ...
Definition: DiscreteFunction.inc.hpp:36
Self & operator+=(Expr &&expr)
interpolate (*this) + expr to DOFVector
Definition: DiscreteFunction.hpp:108
typename EntitySet::GlobalCoordinate Domain
Global coordinates of the EntitySet.
Definition: DiscreteFunction.hpp:164
GlobalBasis const & basis() const
Return global basis bound to the DOFVector.
Definition: DiscreteFunction.hpp:218