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

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

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

9
#include <dune/amdis/FiniteElementSpaces.hpp>
10
11
#include <dune/amdis/LinearAlgebra.hpp>
#include <dune/amdis/common/Mpl.hpp>
12
#include <dune/amdis/common/TypeDefs.hpp>
13
14
15

namespace AMDiS
{
16
  template <class FeSpaces>
17
18
  class Assembler
  {
19
20
21
22
23
24
25
26
    /// 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>;
27

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

31
32
33
    using ElementVector = Impl::ElementVector;
    using ElementMatrix = Impl::ElementMatrix;

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

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

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

  private:
58
    /// Sets the system to zero and initializes all operators and boundary conditions
59
    template <class Operators>
60
61
62
63
64
65
66
    void initMatrixVector(
        SystemMatrixType& matrix,
        SystemVectorType& solution,
        SystemVectorType& rhs,
        MatrixEntries<Operators>& matrix_operators,
        VectorEntries<Operators>& rhs_operators,
        bool asmMatrix_, bool asmVector_);
67
68


69
70
71
    /// Assemble a block-matrix of element-matrices and return a matrix of flags, whether
    /// a given block has received some entries.
    template <class Operators>
72
73
74
75
76
    MatrixEntries<bool> assembleElementMatrices(
        MatrixEntries<Operators>& operators,
        MatrixEntries<ElementMatrix>& elementMatrix,
        FiniteElementSpaces<FeSpaces> const& elementBases,
        bool asmMatrix_);
77
78
79
80

    /// Assemble a block-vector of element-vectors and return a vector of flags, whether
    /// a given block has received some entries.
    template <class Operators>
81
82
83
84
85
    VectorEntries<bool> assembleElementVectors(
        VectorEntries<Operators>& operators,
        VectorEntries<ElementVector>& elementVector,
        FiniteElementSpaces<FeSpaces> const& elementBases,
        bool asmVector_);
86
87


88
89
90
    /// Assemble one block of the block-element-matrix
    // The MatrixData argument stores all matrix-operators
    template <class Operators, class RowView, class ColView>
91
92
93
94
95
    bool assembleElementMatrix(
        Operators& operators,
        ElementMatrix& elementMatrix,
        RowView const& rowLocalView,
        ColView const& colLocalView);
96
97
98
99

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


    /// Add the block-element-matrix to the system-matrix
107
108
109
110
111
    void addElementMatrices(
        SystemMatrixType& dofmatrix,
        FiniteElementSpaces<FeSpaces> const& elementBases,
        MatrixEntries<bool> const& addMat,
        MatrixEntries<ElementMatrix> const& elementMatrix);
112
113

    /// Add the block-element-vector to the system-vector
114
115
116
117
118
    void addElementVectors(
        SystemVectorType& dofvector,
        FiniteElementSpaces<FeSpaces> const& elementBases,
        VectorEntries<bool> const& addVec,
        VectorEntries<ElementVector> const& elementVector);
119
120
121
122

    /// Finish insertion into the matrix and assembles boundary conditions
    /// Return the number of nonzeros assembled into the matrix
    template <class Operators>
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    std::size_t finishMatrixVector(
        SystemMatrixType& matrix,
        SystemVectorType& solution,
        SystemVectorType& rhs,
        MatrixEntries<Operators>& matrix_operators,
        VectorEntries<Operators>& rhs_operators,
        bool asmMatrix_, bool asmVector_);


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

    /// Return whether the vector-block needs to be assembled
    template <std::size_t R, class Operators>
    bool assembleVector(const index_t<R>, VectorEntries<Operators> const& rhs_operators, bool asmVector_) const
    {
      return asmVector_ && (!rhs_operators[R].assembled || rhs_operators[R].changing);
    }
146
147
148

  private:
    std::shared_ptr<FeSpaces> feSpaces;
149
150
151
  };

} // end namespace AMDiS
152
153

#include "Assembler.inc.hpp"