AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
GridFunctionOperator.hpp
1 #pragma once
2 
3 #include <cassert>
4 #include <type_traits>
5 
6 #include <amdis/GridFunctions.hpp>
7 #include <amdis/LocalOperator.hpp>
8 #include <amdis/common/Transposed.hpp>
9 #include <amdis/common/TypeTraits.hpp>
10 #include <amdis/typetree/FiniteElementType.hpp>
11 #include <amdis/utility/QuadratureFactory.hpp>
12 
13 namespace AMDiS
14 {
20 
38  template <class Derived, class LC, class GF>
40  : public LocalOperator<Derived, LC>
41  {
42  template <class, class>
43  friend class LocalOperator;
44 
45  using ContextType = Impl::ContextTypes<LC>;
47 
48  private:
49  using GridFunction = GF;
50 
52  using LocalFunction = decltype(localFunction(std::declval<GF>()));
53 
55  using Element = typename ContextType::Entity;
56 
58  using Geometry = typename Element::Geometry;
59 
61  using LocalCoordinate = typename GF::EntitySet::LocalCoordinate;
62 
65 
66  public:
68 
73  template <class GridFct>
74  GridFunctionOperatorBase(GridFct&& gridFct, int termOrder)
75  : gridFct_(FWD(gridFct))
76  , termOrder_(termOrder)
77  {}
78 
81  template <class PQF>
82  void setQuadFactory(PQF&& pre)
83  {
84  using ctype = typename Geometry::ctype;
85  quadFactory_ = makeUniquePtr(
86  makeQuadratureFactory<ctype, LC::mydimension, LocalFunction>(FWD(pre)));
87  }
88 
89  protected:
91  auto coefficient(LocalCoordinate const& local) const
92  {
93  assert( this->bound_ );
94  return (*localFct_)(local);
95  }
96 
99  template <class... Nodes>
100  auto const& getQuadratureRule(Dune::GeometryType type, Nodes const&... nodes) const
101  {
102  assert( bool(quadFactory_) );
103  int quadDegree = this->getDegree(termOrder_, quadFactory_->order(), nodes...);
104  return quadFactory_->rule(type, quadDegree);
105  }
106 
107  private:
109 
117  void bind_impl(Element const& element, Geometry const& geometry)
118  {
119  assert( bool(quadFactory_) );
120  localFct_.emplace(localFunction(gridFct_));
121  localFct_->bind(element);
122  quadFactory_->bind(localFct_.value());
123  }
124 
126  void unbind_impl()
127  {
128  localFct_->unbind();
129  localFct_.reset();
130  }
131 
132  private:
134  GridFunction gridFct_;
135 
137  std::optional<LocalFunction> localFct_;
138 
140  std::shared_ptr<QuadFactory> quadFactory_;
141 
143  int termOrder_ = 0;
144  };
145 
146 
149  template <class Derived, class Transposed>
151  : public LocalOperator<Derived, typename Transposed::LocalContext>
152  {
153  template <class, class>
154  friend class LocalOperator;
155 
156  template <class T, class... Args>
157  using Constructable = decltype( new T(std::declval<Args>()...) );
158 
159  public:
160  template <class... Args,
161  std::enable_if_t<Dune::Std::is_detected<Constructable, Transposed, Args...>::value, int> = 0>
162  GridFunctionOperatorTransposed(Args&&... args)
163  : transposedOp_(FWD(args)...)
164  {}
165 
168  template <class PQF>
169  void setQuadFactory(PQF&& pre)
170  {
171  transposedOp_.setQuadFactory(FWD(pre));
172  }
173 
174  private:
176  template <class Element, class Geometry>
177  void bind_impl(Element const& element, Geometry const& geometry)
178  {
179  transposedOp_.bind(element, geometry);
180  }
181 
183  void unbind_impl()
184  {
185  transposedOp_.unbind();
186  }
187 
189 
195  template <class CG, class RN, class CN, class Mat>
196  void getElementMatrix(CG const& contextGeometry, RN const& rowNode, CN const& colNode, Mat& elementMatrix)
197  {
198  auto elementMatrixTransposed = transposed(elementMatrix);
199  transposedOp_.getElementMatrix(
200  contextGeometry, colNode, rowNode, elementMatrixTransposed);
201  }
202 
203  private:
204  Transposed transposedOp_;
205  };
206 
207 
208 #ifndef DOXYGEN
209  template <class Tag, class PreGridFct, class PQF>
211  {
212  Tag tag;
213  PreGridFct expr;
214  PQF quadFactory;
215  };
216 #endif
217 
219  template <class Tag, class Expr, class... QuadratureArgs>
220  auto makeOperator(Tag tag, Expr&& expr, QuadratureArgs&&... args)
221  {
222  auto pqf = makePreQuadratureFactory(FWD(args)...);
223  return PreGridFunctionOperator<Tag, TYPEOF(expr), TYPEOF(pqf)>{tag, FWD(expr), std::move(pqf)};
224  }
225 
228 #ifndef DOXYGEN
229 
231 
241  template <class Tag, class LC, class GridFct>
243  : public GridFunctionOperatorBase<GridFunctionOperator<Tag, LC, GridFct>, LC, GridFct>
244  {};
245 
246  template <class Context, class Tag, class GF, class QF>
247  auto makeGridFunctionOperator(Tag tag, GF&& gf, QF&& qf)
248  {
250  gfo.setQuadFactory(FWD(qf));
251  return gfo;
252  }
253 
254 
257  template <class Context, class... Args, class GridView>
258  auto makeLocalOperator(PreGridFunctionOperator<Args...> op, GridView const& gridView)
259  {
260  auto gf = makeGridFunction(std::move(op.expr), gridView);
261  return makeGridFunctionOperator<Context>(op.tag, std::move(gf), std::move(op.quadFactory));
262  }
263 
264  template <class Context, class... Args, class GridView>
265  auto makeLocalOperator(std::reference_wrapper<PreGridFunctionOperator<Args...>> op_ref, GridView const& gridView)
266  {
267  PreGridFunctionOperator<Args...> const& op = op_ref;
268  auto gf = makeGridFunction(std::ref(op.expr), gridView);
269  return makeGridFunctionOperator<Context>(op.tag, std::move(gf), op.quadFactory);
270  }
272 
273 #endif // DOXYGEN
274 
275 } // end namespace AMDiS
auto makeOperator(Tag tag, Expr &&expr, QuadratureArgs &&... args)
Store tag and expression into a PreGridFunctionOperator to create a GridFunctionOperator.
Definition: GridFunctionOperator.hpp:220
Definition: GridFunctionOperator.hpp:210
The base-template for GridFunctionOperators.
Definition: GridFunctionOperator.hpp:242
void setQuadFactory(PQF &&pre)
Definition: GridFunctionOperator.hpp:82
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
auto coefficient(LocalCoordinate const &local) const
Return expression value at LocalCoordinates.
Definition: GridFunctionOperator.hpp:91
int getDegree(int derivOrder, int coeffDegree, RN const &rowNode, CN const &colNode) const
Return the quadrature degree for a matrix operator.
Definition: LocalOperator.hpp:130
auto makeLocalOperator(PreGridFunctionOperator< Args... > op, GridView const &gridView)
Definition: GridFunctionOperator.hpp:258
The main implementation of an operator to be used in a Assembler.
Definition: LocalOperator.hpp:28
The transposed operator, implemented in term of its transposed by calling getElementMatrix with inver...
Definition: GridFunctionOperator.hpp:150
auto const & getQuadratureRule(Dune::GeometryType type, Nodes const &... nodes) const
Definition: GridFunctionOperator.hpp:100
void setQuadFactory(PQF &&pre)
Definition: GridFunctionOperator.hpp:169
GridFunctionOperatorBase(GridFct &&gridFct, int termOrder)
Constructor. Stores a copy of gridFct.
Definition: GridFunctionOperator.hpp:74
Base class for quadrature factories for localFunctions.
Definition: QuadratureFactory.hpp:14
auto makeUniquePtr(Obj &&obj)
Create a unique_ptr by copy/move construction.
Definition: TypeTraits.hpp:107
The main implementation of an CRTP-base class for operators using a grid-function coefficient to be u...
Definition: GridFunctionOperator.hpp:39