AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
BiLinearForm.hpp
1 #pragma once
2 
3 #include <cmath>
4 
5 #include <amdis/LinearAlgebra.hpp>
6 #include <amdis/Observer.hpp>
7 #include <amdis/OperatorList.hpp>
8 #include <amdis/common/Concepts.hpp>
9 #include <amdis/common/ConceptsBase.hpp>
10 #include <amdis/common/FlatMatrix.hpp>
11 #include <amdis/common/TypeTraits.hpp>
12 #include <amdis/linearalgebra/SymmetryStructure.hpp>
13 #include <amdis/typetree/TreePath.hpp>
14 
15 namespace AMDiS
16 {
26  template <class RB, class CB, class T = double, class Traits = BackendTraits<RB>>
28  : public MatrixFacade<T, Traits::template MatrixImpl>
29  , private ObserverSequence<event::adapt,2>
30  {
31  using Self = BiLinearForm;
33 
34  public:
36  using RowBasis = RB;
37 
39  using ColBasis = CB;
40 
42  using CoefficientType = T;
43 
46 
48  using SparsityPattern = typename Traits::SparsityPattern;
49 
50  public:
52  BiLinearForm(std::shared_ptr<RB> const& rowBasis, std::shared_ptr<CB> const& colBasis)
53  : Super(*rowBasis, *colBasis)
54  , ObserverSequence<event::adapt,2>(*rowBasis, *colBasis)
55  , symmetry_(SymmetryStructure::unknown)
56  , rowBasis_(rowBasis)
57  , colBasis_(colBasis)
58  , updatePattern_(true)
59  {
60  auto const rowSize = rowBasis_->localView().maxSize();
61  auto const colSize = colBasis_->localView().maxSize();
62  elementMatrix_.resize(rowSize, colSize);
63  }
64 
66  template <class RB_, class CB_,
67  REQUIRES(Concepts::Similar<RB_,RB>),
68  REQUIRES(Concepts::Similar<CB_,CB>)>
69  BiLinearForm(RB_&& rowBasis, CB_&& colBasis)
70  : BiLinearForm(Dune::wrap_or_move(FWD(rowBasis)), Dune::wrap_or_move(FWD(colBasis)))
71  {}
72 
74  template <class RB_ = RB, class CB_ = CB,
75  REQUIRES(std::is_same_v<RB_,CB_>)>
76  explicit BiLinearForm(std::shared_ptr<RB> const& rowBasis)
77  : BiLinearForm(rowBasis, rowBasis)
78  {}
79 
81  template <class RB_,
82  REQUIRES(Concepts::Similar<RB_,RB>)>
83  explicit BiLinearForm(RB_&& rowBasis)
84  : BiLinearForm(Dune::wrap_or_move(FWD(rowBasis)))
85  {}
86 
87  RowBasis const& rowBasis() const { return *rowBasis_; }
88  ColBasis const& colBasis() const { return *colBasis_; }
89 
91 
110  template <class ContextTag, class Operator, class Row, class Col>
111  void addOperator(ContextTag contextTag, Operator const& op, Row row, Col col);
112 
113  // Add an operator to be assembled on the elements of the grid
114  template <class Operator, class Row = RootTreePath, class Col = RootTreePath>
115  void addOperator(Operator const& op, Row row = {}, Col col = {})
116  {
117  using E = typename RowBasis::LocalView::Element;
118  addOperator(tag::element_operator<E>{}, op, row, col);
119  }
120 
121  // Add an operator to be assembled on the intersections of the grid
122  template <class Operator, class Row = RootTreePath, class Col = RootTreePath>
123  void addIntersectionOperator(Operator const& op, Row row = {}, Col col = {})
124  {
125  using I = typename RowBasis::LocalView::GridView::Intersection;
127  }
129 
131 
135  void setSymmetryStructure(SymmetryStructure symm)
136  {
137  updatePattern_ = updatePattern_ || (symmetry_ != symm);
138  symmetry_ = symm;
139  }
140 
142  void setSymmetryStructure(std::string str)
143  {
144  setSymmetryStructure(symmetryStructure(str));
145  }
146 
148  template <class RowLocalView, class ColLocalView, class LocalOperators,
149  REQUIRES(Concepts::LocalView<RowLocalView>),
150  REQUIRES(Concepts::LocalView<ColLocalView>)>
151  void assemble(RowLocalView const& rowLocalView,
152  ColLocalView const& colLocalView,
153  LocalOperators& localOperators);
154 
156  void assemble();
157 
160 
168  void init(bool forcePatternRebuild = false)
169  {
170  if (updatePattern_ || forcePatternRebuild)
171  {
172  Super::init(SparsityPattern{*rowBasis_, *colBasis_, symmetry_});
173  updatePattern_ = false;
174  }
175  else
176  {
177  Super::init();
178  }
179  }
180 
183  void updateImpl(event::adapt e, index_t<0> i) override { updateImpl2(*rowBasis_); }
184  void updateImpl(event::adapt e, index_t<1> i) override { updateImpl2(*colBasis_); }
185 
186  auto& operators() { return operators_; }
187 
188  private:
189  template <class Basis>
190  void updateImpl2(Basis const& basis)
191  {
192  if (!updatePattern_)
193  Recursive::forEach(operators_, [&](auto& op) { op.update(basis.gridView()); });
194  updatePattern_ = true;
195  }
196 
197  protected:
199  SymmetryStructure symmetry_;
200 
203 
205  MatrixOperators<RowBasis,ColBasis,ElementMatrix> operators_;
206 
207  std::shared_ptr<RowBasis const> rowBasis_;
208  std::shared_ptr<ColBasis const> colBasis_;
209 
210  private:
211  // The pattern is rebuilt on calling init if this flag is set
212  bool updatePattern_;
213  };
214 
215 
216  // deduction guides
217  template <class RB, class CB>
218  BiLinearForm(RB&&, CB&&)
219  -> BiLinearForm<Underlying_t<RB>, Underlying_t<CB>>;
220 
221  template <class RB>
222  BiLinearForm(RB&&)
223  -> BiLinearForm<Underlying_t<RB>, Underlying_t<RB>>;
224 
225 
226  template <class T = double, class RB, class CB>
227  auto makeBiLinearForm(RB&& rowBasis, CB&& colBasis)
228  {
229  using BLF = BiLinearForm<Underlying_t<RB>, Underlying_t<CB>, T>;
230  return BLF{FWD(rowBasis), FWD(colBasis)};
231  }
232 
233  template <class T = double, class RB>
234  auto makeBiLinearForm(RB&& rowBasis)
235  {
236  using BLF = BiLinearForm<Underlying_t<RB>, Underlying_t<RB>, T>;
237  return BLF{FWD(rowBasis)};
238  }
239 
240 } // end namespace AMDiS
241 
242 #include <amdis/BiLinearForm.inc.hpp>
void setSymmetryStructure(std::string str)
Set the symmetry structure using a string.
Definition: BiLinearForm.hpp:142
auto localOperators(Container const &c)
Definition: Operator.hpp:125
The base class for an operator to be used in an Assembler.
Definition: Operator.hpp:79
Definition: AdaptiveGrid.hpp:373
MatrixOperators< RowBasis, ColBasis, ElementMatrix > operators_
List of operators associated to row/col node.
Definition: BiLinearForm.hpp:205
BiLinearForm(std::shared_ptr< RB > const &rowBasis)
Constructor for rowBasis == colBasis.
Definition: BiLinearForm.hpp:76
Definition: AdaptBase.hpp:6
void resize(size_type r, size_type c, value_type v={})
Resizes the container to contain r x c elements.
Definition: FlatMatrix.hpp:70
RB RowBasis
The type of the finite element space / basis of the row.
Definition: BiLinearForm.hpp:36
BiLinearForm(std::shared_ptr< RB > const &rowBasis, std::shared_ptr< CB > const &colBasis)
Constructor. Stores the row and column basis in a local shared_ptr to const.
Definition: BiLinearForm.hpp:52
BiLinearForm(RB_ &&rowBasis)
Wraps the passed row-basis into a (non-destroying) shared_ptr.
Definition: BiLinearForm.hpp:83
void setSymmetryStructure(SymmetryStructure symm)
Provide some additional information about the structure of the matrix pattern.
Definition: BiLinearForm.hpp:135
void assemble()
Assemble all matrix operators, TODO: incorporate boundary conditions.
Definition: BiLinearForm.inc.hpp:66
T CoefficientType
The type of the elements of the DOFVector.
Definition: BiLinearForm.hpp:42
void addOperator(ContextTag contextTag, Operator const &op, Row row, Col col)
Associate a local operator with this BiLinearForm.
Definition: BiLinearForm.hpp:27
void init(bool forcePatternRebuild=false)
Definition: BiLinearForm.hpp:168
Definition: Observer.hpp:25
BiLinearForm(RB_ &&rowBasis, CB_ &&colBasis)
Wraps the passed global bases into (non-destroying) shared_ptr.
Definition: BiLinearForm.hpp:69
CB ColBasis
The type of the finite element space / basis of the column.
Definition: BiLinearForm.hpp:39
void updateImpl(event::adapt e, index_t< 0 > i) override
Definition: BiLinearForm.hpp:183
Definition: OperatorList.hpp:15
void update(GridView const &gv)
Update the operator data on a GridView.
Definition: Operator.hpp:103
Definition: OperatorList.hpp:16
void init()
Initialize the matrix for insertion while keeping the pattern unchanged.
Definition: MatrixFacade.hpp:49
Definition: MatrixFacade.hpp:24
ElementMatrix elementMatrix_
Dense matrix to store coefficients during assemble()
Definition: BiLinearForm.hpp:202
SymmetryStructure symmetry_
The symmetry property if the bilinear form.
Definition: BiLinearForm.hpp:199
typename Traits::SparsityPattern SparsityPattern
Type of the sparsity pattern of the backend.
Definition: BiLinearForm.hpp:48