Assembler.inc.hpp 10.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#pragma once

namespace AMDiS {

template <class FeSpaces>
  template <class Operators>
void Assembler<FeSpaces>::
assemble(SystemMatrixType& matrix,
         SystemVectorType& solution,
         SystemVectorType& rhs,
         MatrixEntries<Operators>& matrix_operators,
         VectorEntries<Operators>& rhs_operators,
         bool asmMatrix_, bool asmVector_)
{
  // 1. init matrix and rhs vector and initialize dirichlet boundary conditions
  initMatrixVector(matrix, solution, rhs, matrix_operators, rhs_operators, asmMatrix_, asmVector_);

  // 2. create a localView and localIndexSet object for each global basis
  LocalFiniteElement<FeSpaces> localFiniteElem(*feSpaces);

  // 3. traverse grid and assemble operators on the elements
  for (auto const& element : elements(gridView))
  {
    localFiniteElem.bind(element);

    MatrixEntries<ElementMatrix> elementMatrix;
    VectorEntries<ElementVector> elementVector;

    MatrixEntries<bool> addMat = assembleElementMatrices(matrix_operators, elementMatrix, localFiniteElem, asmMatrix_);
    VectorEntries<bool> addVec = assembleElementVectors(rhs_operators, elementVector, localFiniteElem, asmVector_);

    addElementMatrices(matrix, localFiniteElem, addMat, elementMatrix);
    addElementVectors(rhs, localFiniteElem, addVec, elementVector);
  }

  // 4. finish matrix insertion and apply dirichlet boundary conditions
  std::size_t nnz = finishMatrixVector(asmMatrix_, asmVector_);

  msg("fillin of assembled matrix: ", nnz);
}


template <class FeSpaces>
  template <class Operators>
void Assembler<FeSpaces>::
initMatrixVector(SystemMatrixType& matrix,
                 SystemVectorType& solution,
                 SystemVectorType& rhs,
                 MatrixEntries<Operators>& matrix_operators,
                 VectorEntries<Operators>& rhs_operators,
                 bool asmMatrix_, bool asmVector_)
{
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const int R = decltype(_r)::value;
    msg(this->getFeSpace(_r).size(), " DOFs for FeSpace[", R, "]");

    if (assembleVector(_r, rhs_operators, asmVector_)) {
      rhs.compress(_r);
      rhs.getVector(_r) = 0.0;

      // init vector operators
      for (auto& op : rhs_operators[R].element)
        op.init(this->getFeSpace(_r));
      for (auto& op : rhs_operators[R].boundary)
        op.init(this->getFeSpace(_r));
      for (auto& op : rhs_operators[R].intersection)
        op.init(this->getFeSpace(_r));
    }

    forEach(range_<0, nComponents>, [&,this](auto const _c)
    {
      static const int C = decltype(_c)::value;

      bool asmMatrix = assembleMatrix(_r, _c, matrix_operators, asmMatrix_);
      matrix(_r, _c).init(asmMatrix);

      if (asmMatrix) {
        // init matrix operators
        for (auto& op : matrix_operators[R][C].element)
          op.init(this->getFeSpace(_r), this->getFeSpace(_c));
        for (auto& op : matrix_operators[R][C].boundary)
          op.init(this->getFeSpace(_r), this->getFeSpace(_c));
        for (auto& op : matrix_operators[R][C].intersection)
          op.init(this->getFeSpace(_r), this->getFeSpace(_c));

        // init boundary condition
        for (int c = 0; c < nComponents; ++c)
          for (auto bc : dirichletBc[R][c])
            bc->init(c == C, matrix(_r, _c), solution[_c], rhs[_r]);
      }
    });
  });
}


template <class FeSpaces>
  template <class Operators>
MatrixEntries<bool> Assembler<FeSpaces>::
assembleElementMatrices(MatrixEntries<Operators>& operators,
                        MatrixEntries<ElementMatrix>& elementMatrix,
                        LocalFiniteElement<FeSpaces> const& localFiniteElem, bool asmMatrix_)
{
  MatrixEntries<bool> addMat;
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const std::size_t R = decltype(_r)::value;
    forEach(range_<0, nComponents>, [&,this](auto const _c)
    {
      static const std::size_t C = decltype(_c)::value;

      // assemble block of element matrix
      addMat[R][C] = assembleMatrix(_r, _c, operators, asmMatrix_)
        ? assembleElementMatrix(operators[R][C], elementMatrix[R][C],
                                localFiniteElem.localView(_r), localFiniteElem.localView(_c))
        : false;
    });
  });

  return addMat;
}


template <class FeSpaces>
  template <class Operators>
VectorEntries<bool> Assembler<FeSpaces>::
assembleElementVectors(VectorEntries<Operators>& operators,
                       VectorEntries<ElementVector>& elementVector,
                       LocalFiniteElement<FeSpaces> const& localFiniteElem, bool asmVector_)
{
  VectorEntries<bool> addVec;
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const std::size_t R = decltype(_r)::value;

    // assemble block of element vector
    addVec[R] = assembleVector(_r, operators, asmVector_)
      ? assembleElementVector(operators[R], elementVector[R], localFiniteElem.localView(_r))
      : false;
  });

  return addVec;
}


template <class FeSpaces>
  template <class Operators, class RowView, class ColView>
bool Assembler<FeSpaces>::
assembleElementMatrix(Operators& operators,
                      ElementMatrix& elementMatrix,
                      RowView const& rowLocalView,
                      ColView const& colLocalView)
{
  if (operators.element.empty() && operators.boundary.empty() && operators.intersection.empty())
    return false; // nothing to do

  auto const nRows = rowLocalView.tree().finiteElement().size();
  auto const nCols = colLocalView.tree().finiteElement().size();

  auto const& element = rowLocalView.element();
  auto const& gridView = rowLocalView.globalBasis().gridView();

  // fills the entire matrix with zeroes
  elementMatrix.change_dim(nRows, nCols);
  set_to_zero(elementMatrix);

  bool add = false;

  auto assemble_operators = [&](auto& e, auto& operator_list) {
    for (auto scaled : operator_list) {
      bool add_op = scaled.op->getElementMatrix(e, rowLocalView, colLocalView, elementMatrix, scaled.factor);
      add = add || add_op;
    }
  };

  // assemble element operators
  assemble_operators(element, operators.element);

  // assemble intersection operators
  if (!operators.intersection.empty()
      || (!operators.boundary.empty() && element.hasBoundaryIntersections()))
  {
    for (auto const& intersection : intersections(gridView, element)) {
      if (intersection.boundary())
        assemble_operators(intersection, operators.boundary);
      else
        assemble_operators(intersection, operators.intersection);
    }
  }

  return add;
}


template <class FeSpaces>
  template <class Operators, class RowView>
bool Assembler<FeSpaces>::
assembleElementVector(Operators& operators, ElementVector& elementVector, RowView const& rowLocalView)
{

  if (operators.element.empty() && operators.boundary.empty() && operators.intersection.empty())
    return false;

  auto const nRows = rowLocalView.tree().finiteElement().size();

  auto const& element = rowLocalView.element();
  auto const& gridView = rowLocalView.globalBasis().gridView();

  // Set all entries to zero
  elementVector.change_dim(nRows);
  set_to_zero(elementVector);

  bool add = false;

  auto assemble_operators = [&](auto& e, auto& operator_list) {
    for (auto scaled : operator_list) {
      bool add_op = scaled.op->getElementVector(e, rowLocalView, elementVector, scaled.factor);
      add = add || add_op;
    }
  };

  // assemble element operators
  assemble_operators(element, operators.element);

  // assemble intersection operators
  if (!operators.intersection.empty()
      || (!operators.boundary.empty() && element.hasBoundaryIntersections()))
  {
    for (auto const& intersection : intersections(gridView, element)) {
      if (intersection.boundary())
        assemble_operators(intersection, operators.boundary);
      else
        assemble_operators(intersection, operators.intersection);
    }
  }

  return add;
}


template <class FeSpaces>
void Assembler<FeSpaces>::
addElementMatrices(SystemMatrixType& dofmatrix,
                   LocalFiniteElement<FeSpaces> const& localFiniteElem,
                   MatrixEntries<bool> const& addMat,
                   MatrixEntries<ElementMatrix> const& elementMatrix)
{
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const std::size_t R = decltype(_r)::value;
    forEach(range_<0, nComponents>, [&,this](auto const _c)
    {
      static const std::size_t C = decltype(_c)::value;
      if (!addMat[R][C])
        return;

      // NOTE: current implementation does not utilize the multi-indices that we get from localIndexSet.

      for (std::size_t i = 0; i < num_rows(elementMatrix[R][C]); ++i) {
        // The global index of the i−th vertex of the element
        auto const row = rowIndexSet.index(i);
        for (std::size_t j = 0; j < num_cols(elementMatrix[R][C]); ++j) {
          // The global index of the j−th vertex of the element
          auto const col = colIndexSet.index(j);
          dofmatrix[R][C](row,col) += elementMatrix[R][C](i,j);
        }
      }
    });
  });
}


template <class FeSpaces>
void Assembler<FeSpaces>::
addElementVectors(SystemVectorType& dofvector,
                  LocalFiniteElement<FeSpaces> const& localFiniteElem,
                  VectorEntries<bool> const& addVec,
                  VectorEntries<ElementVector> const& elementVector)
{
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const std::size_t R = decltype(_r)::value;
    if (!addVec[R])
      return;

    for (std::size_t i = 0; i < size(elementVector[R]); ++i) {
      // The global index of the i-th vertex
      auto const row = indexSet.index(i);
      dofvector[R][row] += elementVector[R][i];
    }
  });
}


template <class FeSpaces>
  template <class Operators>
std::size_t Assembler<FeSpaces>::
finishMatrixVector(SystemMatrixType& matrix, SystemVectorType& solution, SystemVectorType& rhs,
                   MatrixEntries<Operators>& matrix_operators, VectorEntries<Operators>& rhs_operators,
                   bool asmMatrix_, bool asmVector_)
{
  std::size_t nnz = 0;
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const int R = decltype(_r)::value;
    if (assembleVector(asmVector_, _r))
      rhs_operators[R].assembled = true;

    forEach(range_<0, nComponents>, [&,this](auto const _c)
    {
      static const int C = decltype(_c)::value;
      bool asmMatrix = assembleMatrix(asmMatrix_, _r, _c);

      matrix(_r, _c).finish();

      if (asmMatrix)
        matrix_operators[R][C].assembled = true;

      if (asmMatrix) {
        // finish boundary condition
        for (int c = 0; c < nComponents; ++c) {
          for (int r = 0; r < nComponents; ++r) {
            if (r != R && c != C)
              continue;
            for (auto bc : matrix_operators[r][c].dirichlet)
              bc->finish(r == R, c == C, matrix(_r, _c), solution[_c], rhs[_r]);
          }
        }

        nnz += matrix(_r, _c).getNnz();
    });
  });

  return nnz;
}

} // end namespace AMDiS