AMDiS  0.1
The Adaptive Multi-Dimensional Simulation Toolbox
DiscreteFunction.hpp
1 #pragma once
2 
3 #include <optional>
4 #include <vector>
5 
6 #include <dune/functions/common/defaultderivativetraits.hh>
7 #include <dune/functions/functionspacebases/defaultnodetorangemap.hh>
8 #include <dune/functions/functionspacebases/flatvectorview.hh>
9 #include <dune/functions/gridfunctions/gridviewentityset.hh>
10 #include <dune/typetree/childextraction.hh>
11 
12 #include <amdis/common/Tags.hpp>
13 #include <amdis/functions/NodeCache.hpp>
14 #include <amdis/typetree/FiniteElementType.hpp>
15 #include <amdis/typetree/RangeType.hpp>
16 #include <amdis/typetree/TreePath.hpp>
17 
18 namespace AMDiS
19 {
22 
32  template <class Coeff, class GB, class TreePath>
34 
35 
37  template <class Coeff, class GB, class TreePath>
38  class DiscreteFunction
39  : public DiscreteFunction<Coeff const,GB,TreePath>
40  {
43 
44  using Coefficients = Coeff;
45  using GlobalBasis = GB;
46  using ValueType = typename Coeff::value_type;
47 
48  public:
50  template <class... Path>
51  DiscreteFunction(Coefficients& dofVector, GlobalBasis const& basis, Path... path)
52  : Super(dofVector, basis, path...)
53  , mutableCoeff_(&dofVector)
54  {}
55 
57  template <class DV, class... Path,
58  class Coeff_ = TYPEOF(std::declval<DV>().coefficients()),
59  class GB_ = TYPEOF(*std::declval<DV>().basis())>
60  DiscreteFunction(DV&& dofVector, Path... path)
61  : DiscreteFunction(dofVector.coefficients(), *dofVector.basis(), path...)
62  {}
63 
64  public:
67 
74  template <class Expr, class Tag = tag::average>
75  void interpolate_noalias(Expr&& expr, Tag strategy = {});
76 
78 
87  template <class Expr, class Tag = tag::average>
88  void interpolate(Expr&& expr, Tag strategy = {});
89 
91  template <class Expr>
92  Self& operator<<(Expr&& expr)
93  {
94  interpolate(FWD(expr));
95  return *this;
96  }
97 
99  template <class Expr>
100  Self& operator+=(Expr&& expr)
101  {
102  interpolate((*this) + 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  Coefficients& coefficients()
116  {
117  return *mutableCoeff_;
118  }
119 
121  using Super::coefficients;
122 
123  template <class... Indices>
124  auto child(Indices... ii)
125  {
126  auto tp = cat(this->treePath_, makeTreePath(ii...));
127  return DiscreteFunction<Coeff, GB, TYPEOF(makeTreePath(tp))>{*mutableCoeff_, this->basis(), tp};
128  }
129 
130  using Super::child;
131 
132  protected:
133  Coefficients* mutableCoeff_;
134  };
135 
137  template <class Coeff, class GB, class TreePath>
138  class DiscreteFunction<Coeff const,GB,TreePath>
139  {
140  private:
141  using Coefficients = std::remove_const_t<Coeff>;
142  using GlobalBasis = GB;
143  using GridView = typename GlobalBasis::GridView;
144  using ValueType = typename Coefficients::value_type;
145 
146  using Tree = typename GlobalBasis::LocalView::Tree;
147  using SubTree = typename Dune::TypeTree::ChildForTreePath<Tree, TreePath>;
148 
149  using TreeCache = typename GlobalBasis::LocalView::TreeCache;
150  using SubTreeCache = typename Dune::TypeTree::ChildForTreePath<TreeCache, TreePath>;
151 
152  using NodeToRangeEntry = Dune::Functions::DefaultNodeToRangeMap<SubTree>;
153 
154  public:
156  using EntitySet = Dune::Functions::GridViewEntitySet<GridView, 0>;
157 
159  using Domain = typename EntitySet::GlobalCoordinate;
160 
163 
166  enum { hasDerivative = false };
167 
168  public:
170  template <class Type>
171  class DerivativeLocalFunctionBase;
172 
173  class GradientLocalFunction;
174  class PartialLocalFunction;
175  class DivergenceLocalFunction;
176 
178  class LocalFunction;
179 
180  public:
182  template <class... Path>
183  DiscreteFunction(Coefficients const& coefficients, GlobalBasis const& basis, Path... path)
184  : coefficients_(&coefficients)
185  , basis_(&basis)
186  , treePath_(makeTreePath(path...))
187  , entitySet_(basis_->gridView())
188  , nodeToRangeEntry_(Dune::Functions::makeDefaultNodeToRangeMap(*basis_, treePath_))
189  {}
190 
192  template <class DV, class... Path,
193  class Coeff_ = TYPEOF(std::declval<DV>().coefficients()),
194  class GB_ = TYPEOF(*std::declval<DV>().basis())>
195  DiscreteFunction(DV const& dofVector, Path... path)
196  : DiscreteFunction(dofVector.coefficients(), *dofVector.basis(), path...)
197  {}
198 
199 
201  Range operator()(Domain const& x) const;
202 
204  LocalFunction makeLocalFunction() const
205  {
206  return LocalFunction{*this};
207  }
208 
210  EntitySet const& entitySet() const
211  {
212  return entitySet_;
213  }
214 
216  GlobalBasis const& basis() const
217  {
218  return *basis_;
219  }
220 
222  TreePath const& treePath() const
223  {
224  return treePath_;
225  }
226 
228  Coefficients const& coefficients() const
229  {
230  return *coefficients_;
231  }
232 
233  template <class... Indices>
234  auto child(Indices... ii) const
235  {
236  auto tp = cat(this->treePath_, makeTreePath(ii...));
237  return DiscreteFunction<Coeff const, GB, TYPEOF(makeTreePath(tp))>{*coefficients_, *basis_, tp};
238  }
239 
240  protected:
241  Coefficients const* coefficients_;
242  GlobalBasis const* basis_;
243  TreePath treePath_;
244  EntitySet entitySet_;
245  NodeToRangeEntry nodeToRangeEntry_;
246  };
247 
248  // deduction guides
249 
250  template <class Coeff, class GB, class... Path,
251  class TP = TYPEOF(makeTreePath(std::declval<Path>()...)),
252  REQUIRES(Concepts::GlobalBasis<GB>)>
253  DiscreteFunction(Coeff&, GB const&, Path...)
255 
256  template <class DV, class... Path,
257  class Coeff = decltype(std::declval<DV>().coefficients()),
258  class GB = decltype(*std::declval<DV>().basis()),
259  class TP = TYPEOF(makeTreePath(std::declval<Path>()...))>
260  DiscreteFunction(DV&, Path...)
261  -> DiscreteFunction<std::remove_reference_t<Coeff>,std::decay_t<GB>,TP>;
262 
263 
264  // grid functions representing the DOFVector
265  // -----------------------------------------
266 
268  template <class Coeff, class GB, class... Path, class... Indices>
270  {
271  return df.child(ii...);
272  }
273 
275  template <class Coeff, class GB, class... Path, class... Indices>
277  {
278  return df.child(ii...);
279  }
280 
282  template <class DV, class... Indices,
283  class = decltype(std::declval<DV>().coefficients()),
284  class = decltype(std::declval<DV>().basis())>
285  auto valueOf(DV& dofVec, Indices... ii)
286  {
287  return DiscreteFunction{dofVec.coefficients(), *dofVec.basis(), makeTreePath(ii...)};
288  }
289 
290 } // end namespace AMDiS
291 
292 #include "DiscreteLocalFunction.inc.hpp"
293 #include "DiscreteFunction.inc.hpp"
Self & operator<<(Expr &&expr)
Interpolation of GridFunction to DOFVector, alias to interpolate()
Definition: DiscreteFunction.hpp:92
TreePath const & treePath() const
Return treePath associated with this discrete function.
Definition: DiscreteFunction.hpp:222
RangeType_t< SubTree, ValueType > Range
Range type of this DiscreteFunction.
Definition: DiscreteFunction.hpp:162
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:183
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:108
Definition: AdaptiveGrid.hpp:373
void interpolate(Expr &&expr, Tag strategy={})
Interpolation of GridFunction to DOFVector.
Definition: DiscreteFunction.inc.hpp:61
typename EntitySet::GlobalCoordinate Domain
Global coordinates of the EntitySet.
Definition: DiscreteFunction.hpp:159
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
A mutable view on the subspace of a DOFVector,.
Definition: DiscreteFunction.hpp:33
Node_t< typename GlobalBasis::PreBasis, PrefixPath > Tree
Tree of local finite elements / local shape function sets.
Definition: LocalView.hpp:40
Coefficients const & coefficients() const
Return const coefficient vector.
Definition: DiscreteFunction.hpp:228
A Const DiscreteFunction.
Definition: DiscreteFunction.hpp:138
DiscreteFunction(Coefficients &dofVector, GlobalBasis const &basis, Path... path)
Constructor. Stores a pointer to the mutable dofvector.
Definition: DiscreteFunction.hpp:51
Dune::Functions::GridViewEntitySet< GridView, 0 > EntitySet
Set of entities the DiscreteFunction is defined on.
Definition: DiscreteFunction.hpp:156
auto valueOf(DiscreteFunction< Coeff, GB, Path... > &df, Indices... ii)
A Generator for the childs of a mutable DiscreteFunction.
Definition: DiscreteFunction.hpp:269
typename PreBasis::GridView GridView
The grid view that the FE space is defined on.
Definition: GlobalBasis.hpp:66
GlobalBasis const & basis() const
Return global basis bound to the DOFVector.
Definition: DiscreteFunction.hpp:216
Coefficients & coefficients()
Return the mutable DOFVector.
Definition: DiscreteFunction.hpp:115
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(DV const &dofVector, Path... path)
Construct a DiscreteFunction directly from a DOFVector.
Definition: DiscreteFunction.hpp:195
DiscreteFunction(DV &&dofVector, Path... path)
Construct a DiscreteFunction directly from a DOFVector.
Definition: DiscreteFunction.hpp:60
EntitySet const & entitySet() const
Return a Dune::Functions::GridViewEntitySet.
Definition: DiscreteFunction.hpp:210
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
LocalFunction makeLocalFunction() const
Create a local function for this view on the DOFVector.
Definition: DiscreteFunction.hpp:204
Self & operator+=(Expr &&expr)
interpolate (*this) + expr to DOFVector
Definition: DiscreteFunction.hpp:100
NodeCache_t< Tree > TreeCache
Cached basis-tree.
Definition: LocalView.hpp:43