Assembler.inc.hpp 10.5 KB
Newer Older
1
2
3
4
5
6
#pragma once

namespace AMDiS {

template <class FeSpaces>
  template <class Operators>
7
void Assembler<FeSpaces>::assemble(
8
    GridView const& gv,
9
10
11
12
    SystemMatrixType& matrix,
    SystemVectorType& solution,
    SystemVectorType& rhs,
    MatrixEntries<Operators>& matrix_operators,
13
    VectorEntries<Operators>& rhs_operators)
14
{
15
16
  // 1. Update global bases
  globalBases_.update(gv);
17

18
19
  // 2. init matrix and rhs vector and initialize dirichlet boundary conditions
  initMatrixVector(matrix, solution, rhs, matrix_operators, rhs_operators);
20
21

  // 3. traverse grid and assemble operators on the elements
22
  for (auto const& element : elements(globalBases_.gridView()))
23
  {
24
25
    globalBases_.bind(element);

26
27
28
    MatrixEntries<ElementMatrix> elementMatrix;
    VectorEntries<ElementVector> elementVector;

29
30
31
32
33
    MatrixEntries<bool> addMat = assembleElementMatrices(matrix_operators, elementMatrix);
    VectorEntries<bool> addVec = assembleElementVectors(rhs_operators, elementVector);

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

35
    globalBases_.unbind();
36
37
38
  }

  // 4. finish matrix insertion and apply dirichlet boundary conditions
39
  std::size_t nnz = finishMatrixVector(matrix, solution, rhs, matrix_operators, rhs_operators);
40
41
42
43
44
45
46

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


template <class FeSpaces>
  template <class Operators>
47
48
49
50
51
void Assembler<FeSpaces>::initMatrixVector(
    SystemMatrixType& matrix,
    SystemVectorType& solution,
    SystemVectorType& rhs,
    MatrixEntries<Operators>& matrix_operators,
52
    VectorEntries<Operators>& rhs_operators) const
53
54
55
56
{
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const int R = decltype(_r)::value;
57
    auto const& rowFeSpace = globalBases_.basis(_r);
58
59

    msg(rowFeSpace.size(), " DOFs for FeSpace[", R, "]");
60

61
    if (this->assembleVector(_r, rhs_operators)) {
62
63
64
65
      rhs.compress(_r);
      rhs.getVector(_r) = 0.0;

      // init vector operators
66
67
68
69
70
71
      for (auto& scaled : rhs_operators[R].element)
        scaled.op->init(rowFeSpace);
      for (auto& scaled : rhs_operators[R].boundary)
        scaled.op->init(rowFeSpace);
      for (auto& scaled : rhs_operators[R].intersection)
        scaled.op->init(rowFeSpace);
72
73
74
75
76
    }

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

79
      bool asmMatrix = this->assembleMatrix(_r, _c, matrix_operators);
80
81
82
83
      matrix(_r, _c).init(asmMatrix);

      if (asmMatrix) {
        // init matrix operators
84
85
86
87
88
89
        for (auto& scaled : matrix_operators[R][C].element)
          scaled.op->init(rowFeSpace, colFeSpace);
        for (auto& scaled : matrix_operators[R][C].boundary)
          scaled.op->init(rowFeSpace, colFeSpace);
        for (auto& scaled : matrix_operators[R][C].intersection)
          scaled.op->init(rowFeSpace, colFeSpace);
90
91
92

        // init boundary condition
        for (int c = 0; c < nComponents; ++c)
93
          for (auto bc : matrix_operators[R][c].dirichlet)
94
95
96
97
98
99
100
101
102
            bc->init(c == C, matrix(_r, _c), solution[_c], rhs[_r]);
      }
    });
  });
}


template <class FeSpaces>
  template <class Operators>
103
104
105
typename Assembler<FeSpaces>::template MatrixEntries<bool>
Assembler<FeSpaces>::assembleElementMatrices(
    MatrixEntries<Operators>& operators,
106
    MatrixEntries<ElementMatrix>& elementMatrix) const
107
108
109
110
111
112
113
114
115
116
{
  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
117
      addMat[R][C] = this->assembleMatrix(_r, _c, operators)
118
        ? this->assembleElementMatrix(operators[R][C], elementMatrix[R][C],
119
                                      globalBases_.localView(_r), globalBases_.localView(_c))
120
121
122
123
124
125
126
127
128
129
        : false;
    });
  });

  return addMat;
}


template <class FeSpaces>
  template <class Operators>
130
131
132
typename Assembler<FeSpaces>::template VectorEntries<bool>
  Assembler<FeSpaces>::assembleElementVectors(
    VectorEntries<Operators>& operators,
133
    VectorEntries<ElementVector>& elementVector) const
134
135
136
137
138
139
140
{
  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
141
142
    addVec[R] = this->assembleVector(_r, operators)
      ? this->assembleElementVector(operators[R], elementVector[R], globalBases_.localView(_r))
143
144
145
146
147
148
149
150
151
      : false;
  });

  return addVec;
}


template <class FeSpaces>
  template <class Operators, class RowView, class ColView>
152
153
154
155
bool Assembler<FeSpaces>::assembleElementMatrix(
    Operators& operators,
    ElementMatrix& elementMatrix,
    RowView const& rowLocalView,
156
    ColView const& colLocalView) const
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
{
  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>
201
202
203
bool Assembler<FeSpaces>::assembleElementVector(
    Operators& operators,
    ElementVector& elementVector,
204
    RowView const& rowLocalView) const
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
{
  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>
247
248
249
void Assembler<FeSpaces>::addElementMatrices(
    SystemMatrixType& dofmatrix,
    MatrixEntries<bool> const& addMat,
250
    MatrixEntries<ElementMatrix> const& elementMatrix) const
251
252
253
254
255
256
257
258
259
260
{
  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;

261
262
      auto const& rowIndexSet = globalBases_.localIndexSet(_r);
      auto const& colIndexSet = globalBases_.localIndexSet(_c);
263

264
265
266
267
268
269
270
271
      // 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);
272
          dofmatrix(_r,_c)(row,col) += elementMatrix[R][C](i,j);
273
274
275
276
277
278
279
280
        }
      }
    });
  });
}


template <class FeSpaces>
281
282
283
void Assembler<FeSpaces>::addElementVectors(
    SystemVectorType& dofvector,
    VectorEntries<bool> const& addVec,
284
    VectorEntries<ElementVector> const& elementVector) const
285
286
287
288
289
290
291
{
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const std::size_t R = decltype(_r)::value;
    if (!addVec[R])
      return;

292
    auto const& localIndexSet = globalBases_.localIndexSet(_r);
293

294
295
    for (std::size_t i = 0; i < size(elementVector[R]); ++i) {
      // The global index of the i-th vertex
296
297
      auto const row = localIndexSet.index(i);
      dofvector[_r][row] += elementVector[R][i];
298
299
300
301
302
303
304
    }
  });
}


template <class FeSpaces>
  template <class Operators>
305
306
307
308
309
std::size_t Assembler<FeSpaces>::finishMatrixVector(
    SystemMatrixType& matrix,
    SystemVectorType& solution,
    SystemVectorType& rhs,
    MatrixEntries<Operators>& matrix_operators,
310
    VectorEntries<Operators>& rhs_operators) const
311
312
313
314
315
{
  std::size_t nnz = 0;
  forEach(range_<0, nComponents>, [&,this](auto const _r)
  {
    static const int R = decltype(_r)::value;
316
    if (this->assembleVector(_r, rhs_operators))
317
318
319
320
321
      rhs_operators[R].assembled = true;

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

      matrix(_r, _c).finish();

326
      if (asmMatrix) {
327
328
329
330
331
332
333
334
335
336
337
338
339
        matrix_operators[R][C].assembled = true;

        // 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();
340
      }
341
342
343
344
345
346
347
    });
  });

  return nnz;
}

} // end namespace AMDiS