Assembler.hpp 5.64 KB
Newer Older
1
2
3
#pragma once

#include <memory>
4
#include <tuple>
5

6
7
8
9
10
11
#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>

#include <dune/amdis/LinearAlgebra.hpp>
#include <dune/amdis/LocalFiniteElemenet.hpp>
#include <dune/amdis/common/Mpl.hpp>
12
13
14

namespace AMDiS
{
15
  template <class FeSpaces>
16
17
  class Assembler
  {
18
19
20
21
22
23
24
25
    /// Number of problem components
    static constexpr int nComponents = std::tuple_size<FeSpaces>::value;

    using SystemMatrixType = SystemMatrix<FeSpaces>;
    using SystemVectorType = SystemVector<FeSpaces>;

    template <class T>
    using MatrixEntries = Dune::FieldMatrix<T, nComponents, nComponents>;
26

27
28
    template <class T>
    using VectorEntries = Dune::FieldVector<T, nComponents>;
29
30

  public:
31
32
33
    /// Constructor, stores a shared-pointer to the feSpaces
    Assembler(std::shared_ptr<FeSpaces> const& feSpaces_)
      : feSpaces(feSpaces_)
34
    {
35
      auto gridView = std::get<0>(*feSpaces).gridView();
36

37
38
39
40
      forEach(range_<0, nComponents>, [&,this](auto const _r) {
        static const std::size_t R = decltype(_r)::value;
        std::get<R>(*feSpaces).update(gridView);
      });
41
42
    }

43
44
45
46
47
48
49
50
51
52
53
54
55
56
    /// Assemble the linear system
    template <class Operators>
    void assemble(SystemMatrixType& matrix,
                  SystemVectorType& solution,
                  SystemVectorType& rhs,
                  MatrixEntries<Operators>& matrix_operators,
                  VectorEntries<Operators>& rhs_operators,
                  bool asmMatrix_, bool asmVector_);

  private:
    // Return whether the matrix-block needs to be assembled
    template <int R, int C, class Operators>
    bool assembleMatrix(const index_t<R>, const index_t<C>,
                        MatrixEntries<Operators> const& matrix_operators, bool asmMatrix_) const
57
    {
58
      return asmMatrix_ && (!matrix_operators[R][C].assembled || matrix_operators[R][C].changing);
59
60
    }

61
62
63
    // Return whether the vector-block needs to be assembled
    template <int R, class Operators>
    bool assembleVector(const index_t<R>, VectorEntries<Operators> const& rhs_operators, bool asmVector_) const
64
    {
65
      return asmVector_ && (!rhs_operators[R].assembled || rhs_operators[R].changing);
66
67
    }

68
69
70
71
72
73
74
75
    // Sets the system to zero and initializes all operators and boundary conditions
    template <class Operators>
    void initMatrixVector(SystemMatrixType& matrix,
                          SystemVectorType& solution,
                          SystemVectorType& rhs,
                          MatrixEntries<Operators>& matrix_operators,
                          VectorEntries<Operators>& rhs_operators,
                          bool asmMatrix_, bool asmVector_);
76
77


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    /// Assemble a block-matrix of element-matrices and return a matrix of flags, whether
    /// a given block has received some entries.
    template <class Operators>
    MatrixEntries<bool> assembleElementMatrices(MatrixEntries<Operators>& operators,
                                                MatrixEntries<ElementMatrix>& elementMatrix,
                                                LocalFiniteElement<FeSpaces> const& localFiniteElem,
                                                bool asmMatrix_);

    /// Assemble a block-vector of element-vectors and return a vector of flags, whether
    /// a given block has received some entries.
    template <class Operators>
    VectorEntries<bool> assembleElementVectors(VectorEntries<Operators>& operators,
                                               VectorEntries<ElementVector>& elementVector,
                                               LocalFiniteElement<FeSpaces> const& localFiniteElem,
                                               bool asmVector_);
93
94


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    /// Assemble one block of the block-element-matrix
    // The MatrixData argument stores all matrix-operators
    template <class Operators, class RowView, class ColView>
    bool assembleElementMatrix(Operators& operators,
                               ElementMatrix& elementMatrix,
                               RowView const& rowLocalView, ColView const& colLocalView);

    /// Assemble one block of the block-element-vector
    // The VectorData argument stores all vector-operators
    template <class Operators, class RowView>
    bool assembleElementVector(Operators& operators,
                               ElementVector& elementVector,
                               RowView const& rowLocalView);


    /// Add the block-element-matrix to the system-matrix
    void addElementMatrices(SystemMatrixType& dofmatrix,
                            LocalFiniteElement<FeSpaces> const& localFiniteElem,
                            MatrixEntries<bool> const& addMat,
                            MatrixEntries<ElementMatrix> const& elementMatrix);

    /// Add the block-element-vector to the system-vector
    void addElementVectors(SystemVectorType& dofvector,
                           LocalFiniteElement<FeSpaces> const& localFiniteElem,
                           VectorEntries<bool> const& addVec,
                           VectorEntries<ElementVector> const& elementVector);

    /// Finish insertion into the matrix and assembles boundary conditions
    /// Return the number of nonzeros assembled into the matrix
    template <class Operators>
    std::size_t finishMatrixVector(SystemMatrixType& matrix,
                                   SystemVectorType& solution,
                                   SystemVectorType& rhs,
                                   MatrixEntries<Operators>& matrix_operators,
                                   VectorEntries<Operators>& rhs_operators,
                                   bool asmMatrix_, bool asmVector_);

  private:
    std::shared_ptr<FeSpaces> feSpaces;
134
135
136
  };

} // end namespace AMDiS
137
138

#include "Assembler.inc.hpp"