dune-assembler 2.12
Loading...
Searching...
No Matches
defaultglobalassembler.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef DUNE_ASSEMBLER_GLOBALASSEMBLER_HH
5#define DUNE_ASSEMBLER_GLOBALASSEMBLER_HH
6
11
15
17#include <dune/functions/functionspacebases/concepts.hh>
18
19#include <dune/grid/common/capabilities.hh>
21
23
24namespace Dune::Assembler {
25
26
41template <Concepts::GlobalBasis RowBasis, Concepts::GlobalBasis ColBasis = RowBasis, class Executor = SequentialExecutor>
43{
44 using RowLocalView = typename RowBasis::LocalView;
45 using ColLocalView = typename ColBasis::LocalView;
46
47 template <class LA>
48 static constexpr bool isLocalElementMatrixAssembler =
50
51 template <class LA>
52 static constexpr bool isLocalElementVectorAssembler =
54
55 template <class LA>
56 static constexpr bool isLocalElementSystemAssembler =
58
59 template <class LA>
62
63 template <class LA>
66
67 template <class LA>
70
71 template <class LA>
74
75 template <class LA>
78
79 template <class LA>
82
83private:
84 const RowBasis& rowBasis_;
85 const ColBasis& colBasis_;
86 Executor executor_;
87
88public:
89
104 Assembler (const RowBasis& rowBasis, const ColBasis& colBasis, Executor executor)
105 : rowBasis_(rowBasis)
106 , colBasis_(colBasis)
107 , executor_(executor)
108 {
110 {
111 constexpr bool threadSafeViews =
114 static_assert(threadSafeViews, "Trying to use Assembler with parallel Executor, but grid view is not viewThreadSafe.");
115 }
116 }
117
128 Assembler (const RowBasis& rowBasis, const ColBasis& colBasis)
129 requires std::is_default_constructible_v<Executor>
131 {}
132
142 Assembler (const RowBasis& rowBasis)
143 requires std::same_as<RowBasis, ColBasis> && std::is_default_constructible_v<Executor>
145 {}
146
147 // ....
148 template <Concepts::LocalMatrixAssembler<RowLocalView,ColLocalView> LocalAssembler,
149 class Matrix>
151 {
152 executor_([&, la](auto context) mutable
153 {
154 auto localViews = LocalViewPair{rowBasis_, colBasis_};
155 la.bindLocalViews(get<0>(localViews), get<1>(localViews));
156
157 auto outsideLocalViews = LocalViewPair{rowBasis_, colBasis_};
159 la.bindOutsideLocalViews(get<0>(outsideLocalViews), get<1>(outsideLocalViews));
160
161 // local matrices for local assembling
162 using LocalMatrix = typename Matrix::LocalMatrix;
163 FlatArray<LocalMatrix,2,2> localMatrices{LocalMatrix(get<0>(localViews).maxSize(), get<1>(localViews).maxSize())};
164
165 using LocalPattern = typename Matrix::LocalPattern;
166 FlatArray<LocalPattern,2,2> localPattern{LocalPattern(get<0>(localViews).maxSize(), get<1>(localViews).maxSize())};
167
169 context.forEach(elements(rowBasis_.gridView()), [&](const auto& e)
170 {
171 const auto insideIndex = mapper.index(e);
172 localViews.bind(e);
173
174 // initialize the matrix to zero
175 localMatrices[0][0].resize(get<0>(localViews).size(), get<1>(localViews).size());
176 localPattern[0][0].resize(get<0>(localViews).size(), get<1>(localViews).size());
177 localMatrices[0][0] = 0;
178 localPattern[0][0].clear();
179
180 // Whenever any contribution was computed on this
181 // element, we need to scatter the local element data.
182 bool elementContributionsComputed = false;
183
184 const bool hasBoundaryIntersections = e.hasBoundaryIntersections();
185
186 // only bind to an element if necessary
187 if (isLocalElementMatrixAssembler<LocalAssembler> ||
188 isLocalInteriorIntersectionMatrixAssembler<LocalAssembler> || (isLocalBoundaryIntersectionMatrixAssembler<LocalAssembler> && hasBoundaryIntersections))
189 la.bindElement(e);
190
191 if constexpr (isLocalElementMatrixAssembler<LocalAssembler>)
192 {
193 la.assembleElementMatrix(e, localMatrices[0][0]);
194 la.assembleElementMatrixPattern(e, localPattern[0][0]);
195 elementContributionsComputed = true;
196 }
197
198 // Only assemble on inner or boundary intersections if the assembler
199 // supports this.
202 {
203 // We only need to traverse the intersections if the assembler
204 // supports interior intersections or if there are boundary intersections.
206 hasBoundaryIntersections)
207 {
208 for (auto const& is : intersections(rowBasis_.gridView(), e))
209 {
210 // the intersection is on a domain boundary
211 if (is.boundary() && !is.neighbor())
212 {
214 {
215 la.assembleBoundaryIntersectionMatrix(is, localMatrices[0][0]);
216 la.assembleBoundaryIntersectionMatrixPattern(is, localPattern[0][0]);
218 }
219 }
220
221 // the intersection is a regular inner boundary
222 if (!is.boundary() && is.neighbor())
223 {
225 {
226 const auto outsideIndex = mapper.index(is.outside());
227 // visit each intersection only once
229 {
230 outsideLocalViews.bind(is.outside());
231
232 // update the sizes of the local matrices and patterns
233 localMatrices[0][1].resize(get<0>(localViews).size(), get<1>(outsideLocalViews).size());
234 localMatrices[1][0].resize(get<0>(outsideLocalViews).size(), get<1>(localViews).size());
235 localMatrices[1][1].resize(get<0>(outsideLocalViews).size(), get<1>(outsideLocalViews).size());
236 localPattern[0][1].resize(get<0>(localViews).size(), get<1>(outsideLocalViews).size());
237 localPattern[1][0].resize(get<0>(outsideLocalViews).size(), get<1>(localViews).size());
238 localPattern[1][1].resize(get<0>(outsideLocalViews).size(), get<1>(outsideLocalViews).size());
239
240 // initialize the coupling matrices to zero
241 localMatrices[0][1] = 0;
242 localMatrices[1][0] = 0;
243 localMatrices[1][1] = 0;
244 localPattern[0][1].clear();
245 localPattern[1][0].clear();
246 localPattern[1][1].clear();
247
248 la.assembleInteriorIntersectionMatrix(is, localMatrices);
249 la.assembleInteriorIntersectionMatrixPattern(is, localPattern);
251
252 matrix.scatter(get<0>(localViews),get<1>(outsideLocalViews), localMatrices[0][1], localPattern[0][1]);
253 matrix.scatter(get<0>(outsideLocalViews),get<1>(localViews), localMatrices[1][0], localPattern[1][0]);
255 }
256 }
257 }
258 }
259 }
260 }
262 matrix.scatter(get<0>(localViews),get<1>(localViews), localMatrices[0][0], localPattern[0][0]);
263 });
264 });
265 }
266
284 template <Concepts::LocalMatrixAssembler<RowLocalView,ColLocalView> LocalAssembler,
285 class Matrix, class Vector>
286 void assembleSystemEntries (LocalAssembler& la, Matrix& matrix, Vector& vector)
287 {
288 executor_([&, la](auto context) mutable
289 {
290 auto localViews = LocalViewPair{rowBasis_, colBasis_};
291 la.bindLocalViews(get<0>(localViews), get<1>(localViews));
292
293 auto outsideLocalViews = LocalViewPair{rowBasis_, colBasis_};
295 la.bindOutsideLocalViews(get<0>(outsideLocalViews), get<1>(outsideLocalViews));
296
297 // local matrices for local assembling
298 using LocalMatrix = typename Matrix::LocalMatrix;
299 FlatArray<LocalMatrix,2,2> localMatrices{LocalMatrix(get<0>(localViews).maxSize(), get<1>(localViews).maxSize())};
300
301 using LocalPattern = typename Matrix::LocalPattern;
302 FlatArray<LocalPattern,2,2> localPattern{LocalPattern(get<0>(localViews).maxSize(), get<1>(localViews).maxSize())};
303
304 // local vectors for local assembling
305 using LocalVector = typename Vector::LocalVector;
307
309 context.forEach(elements(rowBasis_.gridView()), [&](const auto& e)
310 {
311 const auto insideIndex = mapper.index(e);
312 localViews.bind(e);
313
314 // initialize the matrix to zero
315 localMatrices[0][0].resize(get<0>(localViews).size(), get<1>(localViews).size());
316 localPattern[0][0].resize(get<0>(localViews).size(), get<1>(localViews).size());
317 localMatrices[0][0] = 0;
318 localPattern[0][0].clear();
319
320 // initialize the matrix to zero
321 localVectors[0].resize(get<0>(localViews).size());
322 localVectors[0] = 0;
323
324 // Whenever any contribution was computed on this
325 // element, we need to scatter the local element data.
326 bool elementContributionsComputed = false;
327
328 const bool hasBoundaryIntersections = e.hasBoundaryIntersections();
329
330 // only bind to an element if necessary
331 if (isLocalElementSystemAssembler<LocalAssembler> ||
332 isLocalElementMatrixAssembler<LocalAssembler> ||
333 isLocalElementVectorAssembler<LocalAssembler> ||
334 isLocalInteriorIntersectionSystemAssembler<LocalAssembler> ||
335 isLocalInteriorIntersectionMatrixAssembler<LocalAssembler> ||
336 isLocalInteriorIntersectionVectorAssembler<LocalAssembler> ||
337 ((isLocalBoundaryIntersectionSystemAssembler<LocalAssembler> ||
338 isLocalBoundaryIntersectionSystemAssembler<LocalAssembler> ||
339 isLocalBoundaryIntersectionSystemAssembler<LocalAssembler>)
340 && hasBoundaryIntersections))
341 la.bindElement(e);
342
343 if constexpr (isLocalElementSystemAssembler<LocalAssembler>)
344 {
345 la.assembleElementSystem(e, localMatrices[0][0], localVectors[0]);
346 la.assembleElementMatrixPattern(e, localPattern[0][0]);
347 elementContributionsComputed = true;
348 }
349 else
350 {
351 if constexpr (isLocalElementMatrixAssembler<LocalAssembler>)
352 {
353 la.assembleElementMatrix(e, localMatrices[0][0]);
354 la.assembleElementMatrixPattern(e, localPattern[0][0]);
355 elementContributionsComputed = true;
356 }
358 {
359 la.assembleElementVector(e, localVectors[0]);
361 }
362 }
363
364 // Only assemble on inner or boundary intersections if the assembler
365 // supports this.
372 {
373 // We only need to traverse the intersections if the assembler
374 // supports interior intersections or if there are boundary intersections.
378 hasBoundaryIntersections)
379 {
380 for (auto const& is : intersections(rowBasis_.gridView(), e))
381 {
382 // the intersection is on a domain boundary
383 if (is.boundary() && !is.neighbor())
384 {
386 {
387 la.assembleBoundaryIntersectionSystem(is, localMatrices[0][0], localVectors[0]);
388 la.assembleBoundaryIntersectionMatrixPattern(is, localPattern[0][0]);
390 }
391 else
392 {
394 {
395 la.assembleBoundaryIntersectionMatrix(is, localMatrices[0][0]);
396 la.assembleBoundaryIntersectionMatrixPattern(is, localPattern[0][0]);
398 }
400 {
401 la.assembleBoundaryIntersection(is, localVectors[0]);
403 }
404 }
405 }
406
407 // the intersection is a regular inner boundary
408 if (!is.boundary() && is.neighbor())
409 {
413 {
414 const auto outsideIndex = mapper.index(is.outside());
415 // visit each intersection only once
417 {
418 outsideLocalViews.bind(is.outside());
419
420 // update the sizes of the local matrices and patterns
421 localMatrices[0][1].resize(get<0>(localViews).size(), get<1>(outsideLocalViews).size());
422 localMatrices[1][0].resize(get<0>(outsideLocalViews).size(), get<1>(localViews).size());
423 localMatrices[1][1].resize(get<0>(outsideLocalViews).size(), get<1>(outsideLocalViews).size());
424 localPattern[0][1].resize(get<0>(localViews).size(), get<1>(outsideLocalViews).size());
425 localPattern[1][0].resize(get<0>(outsideLocalViews).size(), get<1>(localViews).size());
426 localPattern[1][1].resize(get<0>(outsideLocalViews).size(), get<1>(outsideLocalViews).size());
427
428 // initialize the coupling matrices to zero
429 localMatrices[0][1] = 0;
430 localMatrices[1][0] = 0;
431 localMatrices[1][1] = 0;
432 localPattern[0][1].clear();
433 localPattern[1][0].clear();
434 localPattern[1][1].clear();
435
436 // initialize the coupling matrices to zero
437 localVectors[1].resize(get<0>(outsideLocalViews).size());
438 localVectors[1] = 0;
439
441 {
442 la.assembleInteriorIntersectionSystem(is, localMatrices, localVectors);
443 la.assembleInteriorIntersectionMatrixPattern(is, localPattern);
444 }
445 else
446 {
448 {
449 la.assembleInteriorIntersectionMatrixSystem(is, localMatrices);
450 la.assembleInteriorIntersectionMatrixPattern(is, localPattern);
451 }
453 {
454 la.assembleInteriorIntersectionVector(is, localVectors);
455 }
456 }
458
459 matrix.scatter(get<0>(localViews),get<1>(outsideLocalViews), localMatrices[0][1], localPattern[0][1]);
460 matrix.scatter(get<0>(outsideLocalViews),get<1>(localViews), localMatrices[1][0], localPattern[1][0]);
462
463 vector.scatter(get<0>(outsideLocalViews), localVectors[1]);
464 }
465 }
466 }
467 }
468 }
469 }
471 {
472 matrix.scatter(get<0>(localViews),get<1>(localViews), localMatrices[0][0], localPattern[0][0]);
473 vector.scatter(get<0>(localViews), localVectors[0]);
474 }
475 });
476 });
477 }
478
479 template <Concepts::LocalMatrixAssembler<RowLocalView,ColLocalView> LocalAssembler,
480 class MatrixPattern>
482 {
483 executor_([&, la](auto context) mutable
484 {
485 auto localViews = LocalViewPair{rowBasis_, colBasis_};
486 la.bindLocalViews(get<0>(localViews), get<1>(localViews));
487
488 auto outsideLocalViews = LocalViewPair{rowBasis_, colBasis_};
490 la.bindOutsideLocalViews(get<0>(outsideLocalViews), get<1>(outsideLocalViews));
491
492 using LocalPattern = typename MatrixPattern::LocalPattern;
493 FlatArray<LocalPattern,2,2> localPattern{LocalPattern(get<0>(localViews).maxSize(), get<1>(localViews).maxSize())};
494
496 context.forEach(elements(rowBasis_.gridView()), [&](const auto& e)
497 {
498 const auto insideIndex = mapper.index(e);
499 localViews.bind(e);
500
501 localPattern[0][0].resize(get<0>(localViews).size(), get<1>(localViews).size());
502 localPattern[0][0].clear();
503
504 // Whenever any contribution was computed on this
505 // element, we need to scatter the local element data.
506 bool elementContributionsComputed = false;
507
508 const bool hasBoundaryIntersections = e.hasBoundaryIntersections();
509
510 // only bind to an element if necessary
511 if (isLocalElementMatrixAssembler<LocalAssembler> ||
512 isLocalInteriorIntersectionMatrixAssembler<LocalAssembler> || (isLocalBoundaryIntersectionMatrixAssembler<LocalAssembler> && hasBoundaryIntersections))
513 la.bindElement(e);
514
515 if constexpr (isLocalElementMatrixAssembler<LocalAssembler>)
516 {
517 la.assembleElementMatrixPattern(e, localPattern[0][0]);
518 elementContributionsComputed = true;
519 }
520
521 // Only assemble on inner or boundary intersections if the assembler
522 // supports this.
525 {
526 // We only need to traverse the intersections if the assembler
527 // supports interior intersections or if there are boundary intersections.
529 hasBoundaryIntersections)
530 {
531 for (auto const& is : intersections(rowBasis_.gridView(), e))
532 {
533 // the intersection is on a domain boundary
534 if (is.boundary() && !is.neighbor())
535 {
537 {
538 la.assembleBoundaryIntersectionMatrixPattern(is, localPattern[0][0]);
540 }
541 }
542
543 // the intersection is a regular inner boundary
544 if (!is.boundary() && is.neighbor())
545 {
547 {
548 const auto outsideIndex = mapper.index(is.outside());
549 // visit each intersection only once
551 {
552 outsideLocalViews.bind(is.outside());
553
554 localPattern[0][1].resize(get<0>(localViews).size(), get<1>(outsideLocalViews).size());
555 localPattern[1][0].resize(get<0>(outsideLocalViews).size(), get<1>(localViews).size());
556 localPattern[1][1].resize(get<0>(outsideLocalViews).size(), get<1>(outsideLocalViews).size());
557
558 localPattern[0][1].clear();
559 localPattern[1][0].clear();
560 localPattern[1][1].clear();
561
562 la.assembleInteriorIntersectionMatrixPattern(is, localPattern);
564
568 }
569 }
570 }
571 }
572 }
573 }
576 });
577 });
578 }
579
580 template <Concepts::LocalVectorAssembler<typename RowBasis::LocalView> LocalAssembler, class Vector>
582 {
583 executor_([&, la](auto context) mutable
584 {
585 auto localView = rowBasis_.localView();
586 la.bindLocalView(localView);
587
588 auto outsideLocalView = rowBasis_.localView();
590 la.bindOutsideLocalView(outsideLocalView);
591
592 // local vectors for local assembling
593 using LocalVector = typename Vector::LocalVector;
594 std::array<LocalVector,2> localVectors{LocalVector(localView.maxSize())};
595
597 context.forEach(elements(rowBasis_.gridView()), [&](const auto& e)
598 {
599 const auto insideIndex = mapper.index(e);
600 localView.bind(e);
601
602 // initialize the matrix to zero
603 localVectors[0].resize(localView.size());
604 localVectors[0] = 0;
605
606 // Whenever any contribution was computed on this
607 // element, we need to scatter the local element data.
608 bool elementContributionsComputed = false;
609
610 const bool hasBoundaryIntersections = e.hasBoundaryIntersections();
611
612 // only bind to an element if necessary
613 if (isLocalElementVectorAssembler<LocalAssembler> ||
614 isLocalInteriorIntersectionVectorAssembler<LocalAssembler> || (isLocalBoundaryIntersectionVectorAssembler<LocalAssembler> && hasBoundaryIntersections))
615 la.bindElement(e);
616
617 if constexpr (isLocalElementVectorAssembler<LocalAssembler>)
618 {
619 la.assembleElementVector(e, localVectors[0]);
620 elementContributionsComputed = true;
621 }
622
623 // Only assemble on inner or boundary intersections if the assembler
624 // supports this.
627 {
628 // We only need to traverse the intersections if the assembler
629 // supports interior intersections or if there are boundary intersections.
631 hasBoundaryIntersections)
632 {
633 for (auto const& is : intersections(rowBasis_.gridView(), e))
634 {
635 // the intersection is on a domain boundary
636 if (is.boundary() && !is.neighbor())
637 {
639 {
640 la.assembleBoundaryIntersectionVector(is, localVectors[0]);
642 }
643 }
644
645 // the intersection is a regular inner boundary
646 if (!is.boundary() && is.neighbor())
647 {
649 {
650 const auto outsideIndex = mapper.index(is.outside());
651 // visit each intersection only once
653 {
654 outsideLocalView.bind(is.outside());
655
656 // initialize the coupling matrices to zero
657 localVectors[1].resize(outsideLocalView.size());
658 localVectors[1] = 0;
659
660 la.assembleInteriorIntersectionVector(is, localVectors);
662
663 vector.scatter(outsideLocalView, localVectors[1]);
664 }
665 }
666 }
667 }
668 }
669 }
671 vector.scatter(localView, localVectors[0]);
672 });
673 });
674 }
675
676};
677
678
679
680} // end namespace Dune::Assembler
681
682#endif // DUNE_ASSEMBLER_GLOBALASSEMBLER_HH
Definition localboundaryintersectionmatrixassembler.hh:9
MCMGLayout mcmgElementLayout()
A global assembler for assembling matrices and vectors.
Definition defaultglobalassembler.hh:43
static constexpr bool isLocalBoundaryIntersectionSystemAssembler
Definition defaultglobalassembler.hh:80
Assembler(const RowBasis &rowBasis, const ColBasis &colBasis)
Construct global assembler.
Definition defaultglobalassembler.hh:128
static constexpr bool isLocalInteriorIntersectionVectorAssembler
Definition defaultglobalassembler.hh:64
void assembleVectorEntries(LocalAssembler &la, Vector &vector)
Definition defaultglobalassembler.hh:581
void assembleSystemEntries(LocalAssembler &la, Matrix &matrix, Vector &vector)
Construct matrix and vector entries in a single loop.
Definition defaultglobalassembler.hh:286
Assembler(const RowBasis &rowBasis)
Construct global assembler.
Definition defaultglobalassembler.hh:142
typename ColBasis::LocalView ColLocalView
Definition defaultglobalassembler.hh:45
typename RowBasis::LocalView RowLocalView
Definition defaultglobalassembler.hh:44
static constexpr bool isLocalInteriorIntersectionMatrixAssembler
Definition defaultglobalassembler.hh:60
static constexpr bool isLocalBoundaryIntersectionVectorAssembler
Definition defaultglobalassembler.hh:76
static constexpr bool isLocalInteriorIntersectionSystemAssembler
Definition defaultglobalassembler.hh:68
Assembler(const RowBasis &rowBasis, const ColBasis &colBasis, Executor executor)
Construct global assembler.
Definition defaultglobalassembler.hh:104
static constexpr bool isLocalElementMatrixAssembler
Definition defaultglobalassembler.hh:48
void assembleMatrixPattern(LocalAssembler &la, MatrixPattern &matrixPattern)
Definition defaultglobalassembler.hh:481
static constexpr bool isLocalElementSystemAssembler
Definition defaultglobalassembler.hh:56
static constexpr bool isLocalElementVectorAssembler
Definition defaultglobalassembler.hh:52
void assembleMatrixEntries(LocalAssembler &la, Matrix &matrix)
Definition defaultglobalassembler.hh:150
static constexpr bool isLocalBoundaryIntersectionMatrixAssembler
Definition defaultglobalassembler.hh:72
A pair of local views constructed from a pair of global bases.
Definition localviewpair.hh:24