Debian Codegraph @

dune/istl/overlappingschwarz.hh

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_ISTL_OVERLAPPINGSCHWARZ_HH
6 #define DUNE_ISTL_OVERLAPPINGSCHWARZ_HH
7 #include <cassert>
8 #include <algorithm>
9 #include <functional>
10 #include <memory>
11 #include <vector>
12 #include <set>
13 #include <dune/common/dynmatrix.hh>
14 #include <dune/common/sllist.hh>
15
16 #include <dune/istl/bccsmatrixinitializer.hh>
17 #include "preconditioners.hh"
18 #include "superlu.hh"
19 #include "umfpack.hh"
20 #include "bvector.hh"
21 #include "bcrsmatrix.hh"
22 #include "ilusubdomainsolver.hh"
23 #include <dune/istl/solvertype.hh>
24
25 namespace Dune
26 {
27
28 /**
29 * @addtogroup ISTL_Prec
30 *
31 * @{
32 */
33 /**
34 * @file
35 * @author Markus Blatt
36 * @brief Contains one level overlapping Schwarz preconditioners
37 */
38
39 template<class M, class X, class TM, class TD, class TA>
40 class SeqOverlappingSchwarz;
41
42 /**
43 * @brief Initializer for SuperLU Matrices representing the subdomains.
44 */
45 template<class I, class S, class D>
46 class OverlappingSchwarzInitializer
47 {
48 public:
49 /** @brief The vector type containing the subdomain to row index mapping. */
50 typedef D subdomain_vector;
51
52 typedef I InitializerList;
53 typedef typename InitializerList::value_type AtomInitializer;
54 typedef typename AtomInitializer::Matrix Matrix;
55 typedef typename Matrix::const_iterator Iter;
56 typedef typename Matrix::row_type::const_iterator CIter;
57
58 typedef S IndexSet;
59 typedef typename IndexSet::size_type size_type;
60
61 OverlappingSchwarzInitializer(InitializerList& il,
62 const IndexSet& indices,
63 const subdomain_vector& domains);
64
65
66 void addRowNnz(const Iter& row);
67
68 void allocate();
69
70 void countEntries(const Iter& row, const CIter& col) const;
71
72 void calcColstart() const;
73
74 void copyValue(const Iter& row, const CIter& col) const;
75
76 void createMatrix() const;
77 private:
78 class IndexMap
79 {
80 public:
81 typedef typename S::size_type size_type;
82 typedef std::map<size_type,size_type> Map;
83 typedef typename Map::iterator iterator;
84 typedef typename Map::const_iterator const_iterator;
85
86 IndexMap();
87
88 void insert(size_type grow);
89
90 const_iterator find(size_type grow) const;
91
92 iterator find(size_type grow);
93
94 iterator begin();
95
96 const_iterator begin() const;
97
98 iterator end();
99
100 const_iterator end() const;
101
102 private:
103 std::map<size_type,size_type> map_;
104 size_type row;
105 };
106
107
108 typedef typename InitializerList::iterator InitIterator;
109 typedef typename IndexSet::const_iterator IndexIteratur;
110 InitializerList* initializers;
111 const IndexSet *indices;
112 mutable std::vector<IndexMap> indexMaps;
113 const subdomain_vector& domains;
114 };
115
116 /**
117 * @brief Tag that the tells the Schwarz method to be additive.
118 */
119 struct AdditiveSchwarzMode
120 {};
121
122 /**
123 * @brief Tag that tells the Schwarz method to be multiplicative.
124 */
125 struct MultiplicativeSchwarzMode
126 {};
127
128 /**
129 * @brief Tag that tells the Schwarz method to be multiplicative
130 * and symmetric.
131 */
132 struct SymmetricMultiplicativeSchwarzMode
133 {};
134
135 /**
136 * @brief Exact subdomain solver using Dune::DynamicMatrix<T>::solve
137 * @tparam M The type of the matrix.
138 */
139 template<class M, class X, class Y>
140 class DynamicMatrixSubdomainSolver;
141
142 // Specialization for BCRSMatrix
143 template<class K, class Al, class X, class Y>
144 class DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >
145 {
146 typedef BCRSMatrix< K, Al> M;
147 public:
148 //! \brief The matrix type the preconditioner is for.
149 typedef typename std::remove_const<M>::type matrix_type;
150 typedef typename X::field_type field_type;
151 typedef typename std::remove_const<M>::type rilu_type;
152 //! \brief The domain type of the preconditioner.
153 typedef X domain_type;
154 //! \brief The range type of the preconditioner.
155 typedef Y range_type;
156 static constexpr size_t n = std::decay_t<decltype(Impl::asMatrix(std::declval<K>()))>::rows;
157
158 /**
159 * @brief Apply the subdomain solver.
160 * @copydoc ILUSubdomainSolver::apply
161 */
162 void apply (DynamicVector<field_type>& v, DynamicVector<field_type>& d)
163 {
164 assert(v.size() > 0);
165 assert(v.size() == d.size());
166 assert(A.rows() <= v.size());
167 assert(A.cols() <= v.size());
168 size_t sz = A.rows();
169 v.resize(sz);
170 d.resize(sz);
171 A.solve(v,d);
172 v.resize(v.capacity());
173 d.resize(d.capacity());
174 }
175
176 /**
177 * @brief Set the data of the local problem.
178 *
179 * @param BCRS The global matrix.
180 * @param rowset The global indices of the local problem.
181 * @tparam S The type of the set with the indices.
182 */
183 template<class S>
184 void setSubMatrix(const M& BCRS, S& rowset)
185 {
186 size_t sz = rowset.size();
187 A.resize(sz*n,sz*n);
188 typedef typename S::const_iterator SIter;
189 size_t r = 0;
190 for(SIter rowIdx = rowset.begin(), rowEnd=rowset.end();
191 rowIdx!= rowEnd; ++rowIdx, r++)
192 {
193 size_t c = 0;
194 for(SIter colIdx = rowset.begin(), colEnd=rowset.end();
195 colIdx!= colEnd; ++colIdx, c++)
196 {
197 if (BCRS[*rowIdx].find(*colIdx) == BCRS[*rowIdx].end())
198 continue;
199 for (size_t i=0; i<n; i++)
200 {
201 for (size_t j=0; j<n; j++)
202 {
203 A[r*n+i][c*n+j] = Impl::asMatrix(BCRS[*rowIdx][*colIdx])[i][j];
204 }
205 }
206 }
207 }
208 }
209 private:
210 DynamicMatrix<K> A;
211 };
212
213 template<typename T, bool tag>
214 class OverlappingAssignerHelper
215 {};
216
217 template<typename T>
218 using OverlappingAssigner = OverlappingAssignerHelper<T, Dune::StoresColumnCompressed<T>::value>;
219
220 // specialization for DynamicMatrix
221 template<class K, class Al, class X, class Y>
222 class OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix<K, Al>, X, Y >,false>
223 {
224 public:
225 typedef BCRSMatrix< K, Al> matrix_type;
226 typedef typename X::field_type field_type;
227 typedef Y range_type;
228 typedef typename range_type::block_type block_type;
229 typedef typename matrix_type::size_type size_type;
230 static constexpr size_t n = std::decay_t<decltype(Impl::asMatrix(std::declval<K>()))>::rows;
231 /**
232 * @brief Constructor.
233 * @param maxlength The maximum entries over all subdomains.
234 * @param mat_ The global matrix.
235 * @param b_ the global right hand side.
236 * @param x_ the global left hand side.
237 */
238 OverlappingAssignerHelper(std::size_t maxlength, const BCRSMatrix<K, Al>& mat_, const X& b_, Y& x_);
239
240 /**
241 * @brief Deallocates memory of the local vector.
242 */
243 inline
244 void deallocate();
245
246 /**
247 * @brief Resets the local index to zero.
248 */
249 inline
250 void resetIndexForNextDomain();
251
252 /**
253 * @brief Get the local left hand side.
254 * @return The local left hand side.
255 */
256 inline
257 DynamicVector<field_type> & lhs();
258
259 /**
260 * @brief Get the local right hand side.
261 * @return The local right hand side.
262 */
263 inline
264 DynamicVector<field_type> & rhs();
265
266 /**
267 * @brief relax the result.
268 * @param relax The relaxation parameter.
269 */
270 inline
271 void relaxResult(field_type relax);
272
273 /**
274 * @brief calculate one entry of the local defect.
275 * @param domainIndex One index of the domain.
276 */
277 void operator()(const size_type& domainIndex);
278
279 /**
280 * @brief Assigns the block to the current local
281 * index.
282 * At the same time the local defect is calculated
283 * for the index and stored in the rhs.
284 * Afterwards the is incremented for the next block.
285 */
286 inline
287 void assignResult(block_type& res);
288
289 private:
290 /**
291 * @brief The global matrix for the defect calculation.
292 */
293 const matrix_type* mat;
294 /** @brief The local right hand side. */
295 // we need a pointer, because we have to avoid deep copies
296 DynamicVector<field_type> * rhs_;
297 /** @brief The local left hand side. */
298 // we need a pointer, because we have to avoid deep copies
299 DynamicVector<field_type> * lhs_;
300 /** @brief The global right hand side for the defect calculation. */
301 const range_type* b;
302 /** @brief The global right hand side for adding the local update to. */
303 range_type* x;
304 /** @brief The current local index. */
305 std::size_t i;
306 /** @brief The maximum entries over all subdomains. */
307 std::size_t maxlength_;
308 };
309
310 #if HAVE_SUPERLU || HAVE_SUITESPARSE_UMFPACK
311 template<template<class> class S, typename T, typename A>
312 struct OverlappingAssignerHelper<S<BCRSMatrix<T, A>>, true>
313 {
314 typedef BCRSMatrix<T, A> matrix_type;
315 typedef typename S<BCRSMatrix<T, A>>::range_type range_type;
316 typedef typename range_type::field_type field_type;
317 typedef typename range_type::block_type block_type;
318
319 typedef typename matrix_type::size_type size_type;
320
321 static constexpr size_t n = std::decay_t<decltype(Impl::asMatrix(std::declval<T>()))>::rows;
322 static constexpr size_t m = std::decay_t<decltype(Impl::asMatrix(std::declval<T>()))>::cols;
323 /**
324 * @brief Constructor.
325 * @param maxlength The maximum entries over all subdomains.
326 * @param mat The global matrix.
327 * @param b the global right hand side.
328 * @param x the global left hand side.
329 */
330 OverlappingAssignerHelper(std::size_t maxlength, const matrix_type& mat,
331 const range_type& b, range_type& x);
332 /**
333 * @brief Deallocates memory of the local vector.
334 * @warning memory is released by the destructor as this Functor
335 * is copied and the copy needs to still have the data.
336 */
337 void deallocate();
338
339 /*
340 * @brief Resets the local index to zero.
341 */
342 void resetIndexForNextDomain();
343
344 /**
345 * @brief Get the local left hand side.
346 * @return The local left hand side.
347 */
348 field_type* lhs();
349
350 /**
351 * @brief Get the local right hand side.
352 * @return The local right hand side.
353 */
354 field_type* rhs();
355
356 /**
357 * @brief relax the result.
358 * @param relax The relaxation parameter.
359 */
360 void relaxResult(field_type relax);
361
362 /**
363 * @brief calculate one entry of the local defect.
364 * @param domain One index of the domain.
365 */
366 void operator()(const size_type& domain);
367
368 /**
369 * @brief Assigns the block to the current local
370 * index.
371 * At the same time the local defect is calculated
372 * for the index and stored in the rhs.
373 * Afterwards the is incremented for the next block.
374 */
375 void assignResult(block_type& res);
376
377 private:
378 /**
379 * @brief The global matrix for the defect calculation.
380 */
381 const matrix_type* mat;
382 /** @brief The local right hand side. */
383 field_type* rhs_;
384 /** @brief The local left hand side. */
385 field_type* lhs_;
386 /** @brief The global right hand side for the defect calculation. */
387 const range_type* b;
388 /** @brief The global right hand side for adding the local update to. */
389 range_type* x;
390 /** @brief The current local index. */
391 std::size_t i;
392 /** @brief The maximum entries over all subdomains. */
393 std::size_t maxlength_;
394 };
395
396 #endif // HAVE_SUPERLU || HAVE_SUITESPARSE_UMFPACK
397
398 template<class M, class X, class Y>
399 class OverlappingAssignerILUBase
400 {
401 public:
402 typedef M matrix_type;
403
404 typedef typename Y::field_type field_type;
405
406 typedef typename Y::block_type block_type;
407
408 typedef typename matrix_type::size_type size_type;
409 /**
410 * @brief Constructor.
411 * @param maxlength The maximum entries over all subdomains.
412 * @param mat The global matrix.
413 * @param b the global right hand side.
414 * @param x the global left hand side.
415 */
416 OverlappingAssignerILUBase(std::size_t maxlength, const M& mat,
417 const Y& b, X& x);
418 /**
419 * @brief Deallocates memory of the local vector.
420 * @warning memory is released by the destructor as this Functor
421 * is copied and the copy needs to still have the data.
422 */
423 void deallocate();
424
425 /**
426 * @brief Resets the local index to zero.
427 */
428 void resetIndexForNextDomain();
429
430 /**
431 * @brief Get the local left hand side.
432 * @return The local left hand side.
433 */
434 X& lhs();
435
436 /**
437 * @brief Get the local right hand side.
438 * @return The local right hand side.
439 */
440 Y& rhs();
441
442 /**
443 * @brief relax the result.
444 * @param relax The relaxation parameter.
445 */
446 void relaxResult(field_type relax);
447
448 /**
449 * @brief calculate one entry of the local defect.
450 * @param domain One index of the domain.
451 */
452 void operator()(const size_type& domain);
453
454 /**
455 * @brief Assigns the block to the current local
456 * index.
457 * At the same time the local defect is calculated
458 * for the index and stored in the rhs.
459 * Afterwards the is incremented for the next block.
460 */
461 void assignResult(block_type& res);
462
463 private:
464 /**
465 * @brief The global matrix for the defect calculation.
466 */
467 const M* mat;
468 /** @brief The local left hand side. */
469 X* lhs_;
470 /** @brief The local right hand side. */
471 Y* rhs_;
472 /** @brief The global right hand side for the defect calculation. */
473 const Y* b;
474 /** @brief The global left hand side for adding the local update to. */
475 X* x;
476 /** @brief The maximum entries over all subdomains. */
477 size_type i;
478 };
479
480 // specialization for ILU0
481 template<class M, class X, class Y>
482 class OverlappingAssignerHelper<ILU0SubdomainSolver<M,X,Y>, false>
483 : public OverlappingAssignerILUBase<M,X,Y>
484 {
485 public:
486 /**
487 * @brief Constructor.
488 * @param maxlength The maximum entries over all subdomains.
489 * @param mat The global matrix.
490 * @param b the global right hand side.
491 * @param x the global left hand side.
492 */
493 OverlappingAssignerHelper(std::size_t maxlength, const M& mat,
494 const Y& b, X& x)
495 : OverlappingAssignerILUBase<M,X,Y>(maxlength, mat,b,x)
496 {}
497 };
498
499 // specialization for ILUN
500 template<class M, class X, class Y>
501 class OverlappingAssignerHelper<ILUNSubdomainSolver<M,X,Y>,false>
502 : public OverlappingAssignerILUBase<M,X,Y>
503 {
504 public:
505 /**
506 * @brief Constructor.
507 * @param maxlength The maximum entries over all subdomains.
508 * @param mat The global matrix.
509 * @param b the global right hand side.
510 * @param x the global left hand side.
511 */
512 OverlappingAssignerHelper(std::size_t maxlength, const M& mat,
513 const Y& b, X& x)
514 : OverlappingAssignerILUBase<M,X,Y>(maxlength, mat,b,x)
515 {}
516 };
517
518 template<typename S, typename T>
519 struct AdditiveAdder
520 {};
521
522 template<typename S, typename T, typename A>
523 struct AdditiveAdder<S, BlockVector<T,A> >
524 {
525 typedef typename A::size_type size_type;
526 typedef typename std::decay_t<decltype(Impl::asVector(std::declval<T>()))>::field_type field_type;
527 AdditiveAdder(BlockVector<T,A>& v, BlockVector<T,A>& x,
528 OverlappingAssigner<S>& assigner, const field_type& relax_);
529 void operator()(const size_type& domain);
530 void axpy();
531 static constexpr size_t n = std::decay_t<decltype(Impl::asVector(std::declval<T>()))>::dimension;
532
533 private:
534 BlockVector<T,A>* v;
535 BlockVector<T,A>* x;
536 OverlappingAssigner<S>* assigner;
537 field_type relax;
538 };
539
540 template<typename S,typename T>
541 struct MultiplicativeAdder
542 {};
543
544 template<typename S, typename T, typename A>
545 struct MultiplicativeAdder<S, BlockVector<T,A> >
546 {
547 typedef typename A::size_type size_type;
548 typedef typename std::decay_t<decltype(Impl::asVector(std::declval<T>()))>::field_type field_type;
549 MultiplicativeAdder(BlockVector<T,A>& v, BlockVector<T,A>& x,
550 OverlappingAssigner<S>& assigner_, const field_type& relax_);
551 void operator()(const size_type& domain);
552 void axpy();
553 static constexpr size_t n = std::decay_t<decltype(Impl::asVector(std::declval<T>()))>::dimension;
554
555 private:
556 BlockVector<T,A>* x;
557 OverlappingAssigner<S>* assigner;
558 field_type relax;
559 };
560
561 /**
562 * @brief template meta program for choosing how to add the correction.
563 *
564 * There are specialization for the additive, the multiplicative, and the symmetric multiplicative mode.
565 *
566 * \tparam T The Schwarz mode (either AdditiveSchwarzMode or MuliplicativeSchwarzMode or
567 * SymmetricMultiplicativeSchwarzMode)
568 * \tparam X The vector field type
569 */
570 template<typename T, class X, class S>
571 struct AdderSelector
572 {};
573
574 template<class X, class S>
575 struct AdderSelector<AdditiveSchwarzMode,X,S>
576 {
577 typedef AdditiveAdder<S,X> Adder;
578 };
579
580 template<class X, class S>
581 struct AdderSelector<MultiplicativeSchwarzMode,X,S>
582 {
583 typedef MultiplicativeAdder<S,X> Adder;
584 };
585
586 template<class X, class S>
587 struct AdderSelector<SymmetricMultiplicativeSchwarzMode,X,S>
588 {
589 typedef MultiplicativeAdder<S,X> Adder;
590 };
591
592 /**
593 * @brief Helper template meta program for application of overlapping Schwarz.
594 *
595 * The is needed because when using the multiplicative Schwarz version one
596 * might still want to make multigrid symmetric, i.e. forward sweep when pre-
597 * and backward sweep when post-smoothing.
598 *
599 * @tparam T1 type of the vector with the subdomain solvers.
600 * @tparam T2 type of the vector with the subdomain vector fields.
601 * @tparam forward If true apply in a forward sweep.
602 */
603 template<typename T1, typename T2, bool forward>
604 struct IteratorDirectionSelector
605 {
606 typedef T1 solver_vector;
607 typedef typename solver_vector::iterator solver_iterator;
608 typedef T2 subdomain_vector;
609 typedef typename subdomain_vector::const_iterator domain_iterator;
610
611 static solver_iterator begin(solver_vector& sv)
612 {
613 return sv.begin();
614 }
615
616 static solver_iterator end(solver_vector& sv)
617 {
618 return sv.end();
619 }
620 static domain_iterator begin(const subdomain_vector& sv)
621 {
622 return sv.begin();
623 }
624
625 static domain_iterator end(const subdomain_vector& sv)
626 {
627 return sv.end();
628 }
629 };
630
631 template<typename T1, typename T2>
632 struct IteratorDirectionSelector<T1,T2,false>
633 {
634 typedef T1 solver_vector;
635 typedef typename solver_vector::reverse_iterator solver_iterator;
636 typedef T2 subdomain_vector;
637 typedef typename subdomain_vector::const_reverse_iterator domain_iterator;
638
639 static solver_iterator begin(solver_vector& sv)
640 {
641 return sv.rbegin();
642 }
643
644 static solver_iterator end(solver_vector& sv)
645 {
646 return sv.rend();
647 }
648 static domain_iterator begin(const subdomain_vector& sv)
649 {
650 return sv.rbegin();
651 }
652
653 static domain_iterator end(const subdomain_vector& sv)
654 {
655 return sv.rend();
656 }
657 };
658
659 /**
660 * @brief Helper template meta program for application of overlapping Schwarz.
661 *
662 * The is needed because when using the multiplicative Schwarz version one
663 * might still want to make multigrid symmetric, i.e. forward sweep when pre-
664 * and backward sweep when post-smoothing.
665 * @tparam T The smoother to apply.
666 */
667 template<class T>
668 struct SeqOverlappingSchwarzApplier
669 {
670 typedef T smoother;
671 typedef typename smoother::range_type range_type;
672
673 static void apply(smoother& sm, range_type& v, const range_type& b)
674 {
675 sm.template apply<true>(v, b);
676 }
677 };
678
679 template<class M, class X, class TD, class TA>
680 struct SeqOverlappingSchwarzApplier<SeqOverlappingSchwarz<M,X,SymmetricMultiplicativeSchwarzMode,TD,TA> >
681 {
682 typedef SeqOverlappingSchwarz<M,X,SymmetricMultiplicativeSchwarzMode,TD,TA> smoother;
683 typedef typename smoother::range_type range_type;
684
685 static void apply(smoother& sm, range_type& v, const range_type& b)
686 {
687 sm.template apply<true>(v, b);
688 sm.template apply<false>(v, b);
689 }
690 };
691
692 template<class T, bool tag>
693 struct SeqOverlappingSchwarzAssemblerHelper
694 {};
695
696 template<class T>
697 using SeqOverlappingSchwarzAssembler = SeqOverlappingSchwarzAssemblerHelper<T,Dune::StoresColumnCompressed<T>::value>;
698
699 template<class K, class Al, class X, class Y>
700 struct SeqOverlappingSchwarzAssemblerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
701 {
702 typedef BCRSMatrix< K, Al> matrix_type;
703 static constexpr size_t n = std::decay_t<decltype(Impl::asMatrix(std::declval<K>()))>::rows;
704 template<class RowToDomain, class Solvers, class SubDomains>
705 static std::size_t assembleLocalProblems(const RowToDomain& rowToDomain, const matrix_type& mat,
706 Solvers& solvers, const SubDomains& domains,
707 bool onTheFly);
708 };
709
710 template<template<class> class S, typename T, typename A>
711 struct SeqOverlappingSchwarzAssemblerHelper<S<BCRSMatrix<T,A>>,true>
712 {
713 typedef BCRSMatrix<T,A> matrix_type;
714 static constexpr size_t n = std::decay_t<decltype(Impl::asMatrix(std::declval<T>()))>::rows;
715 template<class RowToDomain, class Solvers, class SubDomains>
716 static std::size_t assembleLocalProblems(const RowToDomain& rowToDomain, const matrix_type& mat,
717 Solvers& solvers, const SubDomains& domains,
718 bool onTheFly);
719 };
720
721 template<class M,class X, class Y>
722 struct SeqOverlappingSchwarzAssemblerILUBase
723 {
724 typedef M matrix_type;
725 template<class RowToDomain, class Solvers, class SubDomains>
726 static std::size_t assembleLocalProblems(const RowToDomain& rowToDomain, const matrix_type& mat,
727 Solvers& solvers, const SubDomains& domains,
728 bool onTheFly);
729 };
730
731 template<class M,class X, class Y>
732 struct SeqOverlappingSchwarzAssemblerHelper<ILU0SubdomainSolver<M,X,Y>,false>
733 : public SeqOverlappingSchwarzAssemblerILUBase<M,X,Y>
734 {};
735
736 template<class M,class X, class Y>
737 struct SeqOverlappingSchwarzAssemblerHelper<ILUNSubdomainSolver<M,X,Y>,false>
738 : public SeqOverlappingSchwarzAssemblerILUBase<M,X,Y>
739 {};
740
741 /**
742 * @brief Sequential overlapping Schwarz preconditioner
743 *
744 * @tparam M The matrix type.
745 * @tparam X The range and domain type.
746 * @tparam TM The Schwarz mode. Currently supported modes are AdditiveSchwarzMode,
747 * MultiplicativeSchwarzMode, and SymmetricMultiplicativeSchwarzMode. (Default values is AdditiveSchwarzMode)
748 * @tparam TD The type of the local subdomain solver to be used.
749 * @tparam TA The type of the allocator to use.
750 */
751 template<class M, class X, class TM=AdditiveSchwarzMode,
752 class TD=ILU0SubdomainSolver<M,X,X>, class TA=std::allocator<X> >
753 class SeqOverlappingSchwarz
754 : public Preconditioner<X,X>
755 {
756 public:
757 /**
758 * @brief The type of the matrix to precondition.
759 */
760 typedef M matrix_type;
761
762 /**
763 * @brief The domain type of the preconditioner
764 */
765 typedef X domain_type;
766
767 /**
768 * @brief The range type of the preconditioner.
769 */
770 typedef X range_type;
771
772 /**
773 * @brief The mode (additive or multiplicative) of the Schwarz
774 * method.
775 *
776 * Either AdditiveSchwarzMode or MultiplicativeSchwarzMode
777 */
778 typedef TM Mode;
779
780 /**
781 * @brief The field type of the preconditioner.
782 */
783 typedef typename X::field_type field_type;
784
785 /** @brief The return type of the size method. */
786 typedef typename matrix_type::size_type size_type;
787
788 /** @brief The allocator to use. */
789 typedef TA allocator;
790
791 /** @brief The type for the subdomain to row index mapping. */
792 typedef std::set<size_type, std::less<size_type>,
793 typename std::allocator_traits<TA>::template rebind_alloc<size_type> >
794 subdomain_type;
795
796 /** @brief The vector type containing the subdomain to row index mapping. */
797 typedef std::vector<subdomain_type, typename std::allocator_traits<TA>::template rebind_alloc<subdomain_type> > subdomain_vector;
798
799 /** @brief The type for the row to subdomain mapping. */
800 typedef SLList<size_type, typename std::allocator_traits<TA>::template rebind_alloc<size_type> > subdomain_list;
801
802 /** @brief The vector type containing the row index to subdomain mapping. */
803 typedef std::vector<subdomain_list, typename std::allocator_traits<TA>::template rebind_alloc<subdomain_list> > rowtodomain_vector;
804
805 /** @brief The type for the subdomain solver in use. */
806 typedef TD slu;
807
808 /** @brief The vector type containing subdomain solvers. */
809 typedef std::vector<slu, typename std::allocator_traits<TA>::template rebind_alloc<slu> > slu_vector;
810
811 /**
812 * @brief Construct the overlapping Schwarz method.
813 * @param mat The matrix to precondition.
814 * @param subDomains Array of sets of rowindices belonging to an overlapping
815 * subdomain
816 * @param relaxationFactor relaxation factor
817 * @param onTheFly_ If true the decomposition of the exact local solvers is
818 * computed on the fly for each subdomain and
819 * iteration step. If false all decompositions are computed in pre and
820 * only forward and backward substitution takes place
821 * in the iteration steps.
822 * @warning Each rowindex should be part of at least one subdomain!
823 */
824 SeqOverlappingSchwarz(const matrix_type& mat, const subdomain_vector& subDomains,
825 field_type relaxationFactor=1, bool onTheFly_=true);
826
827 /**
828 * Construct the overlapping Schwarz method
829 * @param mat The matrix to precondition.
830 * @param rowToDomain The mapping of the rows onto the domains.
831 * @param relaxationFactor relaxation factor
832 * @param onTheFly_ If true the decomposition of the exact local solvers is
833 * computed on the fly for each subdomain and
834 * iteration step. If false all decompositions are computed in pre and
835 * only forward and backward substitution takes place
836 * in the iteration steps.
837 */
838 SeqOverlappingSchwarz(const matrix_type& mat, const rowtodomain_vector& rowToDomain,
839 field_type relaxationFactor=1, bool onTheFly_=true);
840
841 /*!
842 \brief Prepare the preconditioner.
843
844 \copydoc Preconditioner::pre(X&,Y&)
845 */
846 void pre ([[maybe_unused]] X& x, [[maybe_unused]] X& b) override
847 {}
848
849 /*!
850 \brief Apply the preconditioner
851
852 \copydoc Preconditioner::apply(X&,const Y&)
853 */
854 void apply (X& v, const X& d) override;
855
856 /*!
857 \brief Postprocess the preconditioner.
858
859 \copydoc Preconditioner::post(X&)
860 */
861 void post ([[maybe_unused]] X& x) override
862 {}
863
864 template<bool forward>
865 void apply(X& v, const X& d);
866
867 //! Category of the preconditioner (see SolverCategory::Category)
868 SolverCategory::Category category() const override
869 {
870 return SolverCategory::sequential;
871 }
872
873 private:
874 const M& mat;
875 slu_vector solvers;
876 subdomain_vector subDomains;
877 field_type relax;
878
879 typename M::size_type maxlength;
880
881 bool onTheFly;
882 };
883
884
885
886 template<class I, class S, class D>
887 OverlappingSchwarzInitializer<I,S,D>::OverlappingSchwarzInitializer(InitializerList& il,
888 const IndexSet& idx,
889 const subdomain_vector& domains_)
890 : initializers(&il), indices(&idx), indexMaps(il.size()), domains(domains_)
891 {}
892
893
894 template<class I, class S, class D>
895 void OverlappingSchwarzInitializer<I,S,D>::addRowNnz(const Iter& row)
896 {
897 typedef typename IndexSet::value_type::const_iterator iterator;
898 for(iterator domain=(*indices)[row.index()].begin(); domain != (*indices)[row.index()].end(); ++domain) {
899 (*initializers)[*domain].addRowNnz(row, domains[*domain]);
900 indexMaps[*domain].insert(row.index());
901 }
902 }
903
904 template<class I, class S, class D>
905 void OverlappingSchwarzInitializer<I,S,D>::allocate()
906 {
907 for(auto&& i: *initializers)
908 i.allocateMatrixStorage();
909 for(auto&& i: *initializers)
910 i.allocateMarker();
911 }
912
913 template<class I, class S, class D>
914 void OverlappingSchwarzInitializer<I,S,D>::countEntries(const Iter& row, const CIter& col) const
915 {
916 typedef typename IndexSet::value_type::const_iterator iterator;
917 for(iterator domain=(*indices)[row.index()].begin(); domain != (*indices)[row.index()].end(); ++domain) {
918 typename std::map<size_type,size_type>::const_iterator v = indexMaps[*domain].find(col.index());
919 if(v!= indexMaps[*domain].end()) {
920 (*initializers)[*domain].countEntries(indexMaps[*domain].find(col.index())->second);
921 }
922 }
923 }
924
925 template<class I, class S, class D>
926 void OverlappingSchwarzInitializer<I,S,D>::calcColstart() const
927 {
928 for(auto&& i : *initializers)
929 i.calcColstart();
930 }
931
932 template<class I, class S, class D>
933 void OverlappingSchwarzInitializer<I,S,D>::copyValue(const Iter& row, const CIter& col) const
934 {
935 typedef typename IndexSet::value_type::const_iterator iterator;
936 for(iterator domain=(*indices)[row.index()].begin(); domain!= (*indices)[row.index()].end(); ++domain) {
937 typename std::map<size_type,size_type>::const_iterator v = indexMaps[*domain].find(col.index());
938 if(v!= indexMaps[*domain].end()) {
939 assert(indexMaps[*domain].end()!=indexMaps[*domain].find(row.index()));
940 (*initializers)[*domain].copyValue(col, indexMaps[*domain].find(row.index())->second,
941 v->second);
942 }
943 }
944 }
945
946 template<class I, class S, class D>
947 void OverlappingSchwarzInitializer<I,S,D>::createMatrix() const
948 {
949 std::vector<IndexMap>().swap(indexMaps);
950 for(auto&& i: *initializers)
951 i.createMatrix();
952 }
953
954 template<class I, class S, class D>
955 OverlappingSchwarzInitializer<I,S,D>::IndexMap::IndexMap()
956 : row(0)
957 {}
958
959 template<class I, class S, class D>
960 void OverlappingSchwarzInitializer<I,S,D>::IndexMap::insert(size_type grow)
961 {
962 assert(map_.find(grow)==map_.end());
963 map_.insert(std::make_pair(grow, row++));
964 }
965
966 template<class I, class S, class D>
967 typename OverlappingSchwarzInitializer<I,S,D>::IndexMap::const_iterator
968 OverlappingSchwarzInitializer<I,S,D>::IndexMap::find(size_type grow) const
969 {
970 return map_.find(grow);
971 }
972
973 template<class I, class S, class D>
974 typename OverlappingSchwarzInitializer<I,S,D>::IndexMap::iterator
975 OverlappingSchwarzInitializer<I,S,D>::IndexMap::find(size_type grow)
976 {
977 return map_.find(grow);
978 }
979
980 template<class I, class S, class D>
981 typename OverlappingSchwarzInitializer<I,S,D>::IndexMap::const_iterator
982 OverlappingSchwarzInitializer<I,S,D>::IndexMap::end() const
983 {
984 return map_.end();
985 }
986
987 template<class I, class S, class D>
988 typename OverlappingSchwarzInitializer<I,S,D>::IndexMap::iterator
989 OverlappingSchwarzInitializer<I,S,D>::IndexMap::end()
990 {
991 return map_.end();
992 }
993
994 template<class I, class S, class D>
995 typename OverlappingSchwarzInitializer<I,S,D>::IndexMap::const_iterator
996 OverlappingSchwarzInitializer<I,S,D>::IndexMap::begin() const
997 {
998 return map_.begin();
999 }
1000
1001 template<class I, class S, class D>
1002 typename OverlappingSchwarzInitializer<I,S,D>::IndexMap::iterator
1003 OverlappingSchwarzInitializer<I,S,D>::IndexMap::begin()
1004 {
1005 return map_.begin();
1006 }
1007
1008 template<class M, class X, class TM, class TD, class TA>
1009 SeqOverlappingSchwarz<M,X,TM,TD,TA>::SeqOverlappingSchwarz(const matrix_type& mat_, const rowtodomain_vector& rowToDomain,
1010 field_type relaxationFactor, bool fly)
1011 : mat(mat_), relax(relaxationFactor), onTheFly(fly)
1012 {
1013 typedef typename rowtodomain_vector::const_iterator RowDomainIterator;
1014 typedef typename subdomain_list::const_iterator DomainIterator;
1015 #ifdef DUNE_ISTL_WITH_CHECKING
1016 assert(rowToDomain.size()==mat.N());
1017 assert(rowToDomain.size()==mat.M());
1018
1019 for(RowDomainIterator iter=rowToDomain.begin(); iter != rowToDomain.end(); ++iter)
1020 assert(iter->size()>0);
1021
1022 #endif
1023 // calculate the number of domains
1024 size_type domains=0;
1025 for(RowDomainIterator iter=rowToDomain.begin(); iter != rowToDomain.end(); ++iter)
1026 for(DomainIterator d=iter->begin(); d != iter->end(); ++d)
1027 domains=std::max(domains, *d);
1028 ++domains;
1029
1030 solvers.resize(domains);
1031 subDomains.resize(domains);
1032
1033 // initialize subdomains to row mapping from row to subdomain mapping
1034 size_type row=0;
1035 for(RowDomainIterator iter=rowToDomain.begin(); iter != rowToDomain.end(); ++iter, ++row)
1036 for(DomainIterator d=iter->begin(); d != iter->end(); ++d)
1037 subDomains[*d].insert(row);
1038
1039 #ifdef DUNE_ISTL_WITH_CHECKING
1040 size_type i=0;
1041 typedef typename subdomain_vector::const_iterator iterator;
1042 for(iterator iter=subDomains.begin(); iter != subDomains.end(); ++iter) {
1043 typedef typename subdomain_type::const_iterator entry_iterator;
1044 Dune::dvverb<<"domain "<<i++<<":";
1045 for(entry_iterator entry = iter->begin(); entry != iter->end(); ++entry) {
1046 Dune::dvverb<<" "<<*entry;
1047 }
1048 Dune::dvverb<<std::endl;
1049 }
1050 #endif
1051 maxlength = SeqOverlappingSchwarzAssembler<slu>
1052 ::assembleLocalProblems(rowToDomain, mat, solvers, subDomains, onTheFly);
1053 }
1054
1055 template<class M, class X, class TM, class TD, class TA>
1056 SeqOverlappingSchwarz<M,X,TM,TD,TA>::SeqOverlappingSchwarz(const matrix_type& mat_,
1057 const subdomain_vector& sd,
1058 field_type relaxationFactor,
1059 bool fly)
1060 : mat(mat_), solvers(sd.size()), subDomains(sd), relax(relaxationFactor),
1061 onTheFly(fly)
1062 {
1063 typedef typename subdomain_vector::const_iterator DomainIterator;
1064
1065 #ifdef DUNE_ISTL_WITH_CHECKING
1066 size_type i=0;
1067
1068 for(DomainIterator d=sd.begin(); d != sd.end(); ++d,++i) {
1069 //std::cout<<i<<": "<<d->size()<<std::endl;
1070 assert(d->size()>0);
1071 typedef typename DomainIterator::value_type::const_iterator entry_iterator;
1072 Dune::dvverb<<"domain "<<i<<":";
1073 for(entry_iterator entry = d->begin(); entry != d->end(); ++entry) {
1074 Dune::dvverb<<" "<<*entry;
1075 }
1076 Dune::dvverb<<std::endl;
1077 }
1078
1079 #endif
1080
1081 // Create a row to subdomain mapping
1082 rowtodomain_vector rowToDomain(mat.N());
1083
1084 size_type domainId=0;
1085
1086 for(DomainIterator domain=sd.begin(); domain != sd.end(); ++domain, ++domainId) {
1087 typedef typename subdomain_type::const_iterator iterator;
1088 for(iterator row=domain->begin(); row != domain->end(); ++row)
1089 rowToDomain[*row].push_back(domainId);
1090 }
1091
1092 maxlength = SeqOverlappingSchwarzAssembler<slu>
1093 ::assembleLocalProblems(rowToDomain, mat, solvers, subDomains, onTheFly);
1094 }
1095
1096 /**
1097 template helper struct to determine the size of a domain for the
1098 SeqOverlappingSchwarz solver
1099
1100 only implemented for BCRSMatrix<T>
1101 */
1102 template<class M>
1103 struct SeqOverlappingSchwarzDomainSize {};
1104
1105 template<typename T, typename A>
1106 struct SeqOverlappingSchwarzDomainSize<BCRSMatrix<T,A > >
1107 {
1108 static constexpr size_t n = std::decay_t<decltype(Impl::asMatrix(std::declval<T>()))>::rows;
1109 static constexpr size_t m = std::decay_t<decltype(Impl::asMatrix(std::declval<T>()))>::cols;
1110 template<class Domain>
1111 static int size(const Domain & d)
1112 {
1113 assert(n==m);
1114 return m*d.size();
1115 }
1116 };
1117
1118 template<class K, class Al, class X, class Y>
1119 template<class RowToDomain, class Solvers, class SubDomains>
1120 std::size_t
1121 SeqOverlappingSchwarzAssemblerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>::
1122 assembleLocalProblems([[maybe_unused]] const RowToDomain& rowToDomain,
1123 [[maybe_unused]] const matrix_type& mat,
1124 [[maybe_unused]] Solvers& solvers,
1125 const SubDomains& subDomains,
1126 [[maybe_unused]] bool onTheFly)
1127 {
1128 typedef typename SubDomains::const_iterator DomainIterator;
1129 std::size_t maxlength = 0;
1130
1131 assert(onTheFly);
1132
1133 for(DomainIterator domain=subDomains.begin(); domain!=subDomains.end(); ++domain)
1134 maxlength=std::max(maxlength, domain->size());
1135 maxlength*=n;
1136
1137 return maxlength;
1138 }
1139
1140 #if HAVE_SUPERLU || HAVE_SUITESPARSE_UMFPACK
1141 template<template<class> class S, typename T, typename A>
1142 template<class RowToDomain, class Solvers, class SubDomains>
1143 std::size_t SeqOverlappingSchwarzAssemblerHelper<S<BCRSMatrix<T,A>>,true>::assembleLocalProblems(const RowToDomain& rowToDomain,
1144 const matrix_type& mat,
1145 Solvers& solvers,
1146 const SubDomains& subDomains,
1147 bool onTheFly)
1148 {
1149 typedef typename S<BCRSMatrix<T,A>>::MatrixInitializer MatrixInitializer;
1150 typedef typename std::vector<MatrixInitializer>::iterator InitializerIterator;
1151 typedef typename SubDomains::const_iterator DomainIterator;
1152 typedef typename Solvers::iterator SolverIterator;
1153 std::size_t maxlength = 0;
1154
1155 if(onTheFly) {
1156 for(DomainIterator domain=subDomains.begin(); domain!=subDomains.end(); ++domain)
1157 maxlength=std::max(maxlength, domain->size());
1158 maxlength*=Impl::asMatrix(*mat[0].begin()).N();
1159 }else{
1160 // initialize the initializers
1161 DomainIterator domain=subDomains.begin();
1162
1163 // Create the initializers list.
1164 std::vector<MatrixInitializer> initializers(subDomains.size());
1165
1166 SolverIterator solver=solvers.begin();
1167 for(InitializerIterator initializer=initializers.begin(); initializer!=initializers.end();
1168 ++initializer, ++solver, ++domain) {
1169 solver->getInternalMatrix().N_=SeqOverlappingSchwarzDomainSize<matrix_type>::size(*domain);
1170 solver->getInternalMatrix().M_=SeqOverlappingSchwarzDomainSize<matrix_type>::size(*domain);
1171 //solver->setVerbosity(true);
1172 *initializer=MatrixInitializer(solver->getInternalMatrix());
1173 }
1174
1175 // Set up the supermatrices according to the subdomains
1176 typedef OverlappingSchwarzInitializer<std::vector<MatrixInitializer>,
1177 RowToDomain, SubDomains> Initializer;
1178
1179 Initializer initializer(initializers, rowToDomain, subDomains);
1180 copyToBCCSMatrix(initializer, mat);
1181
1182 // Calculate the LU decompositions
1183 for(auto&& s: solvers)
1184 s.decompose();
1185 for (SolverIterator solverIt = solvers.begin(); solverIt != solvers.end(); ++solverIt)
1186 {
1187 assert(solverIt->getInternalMatrix().N() == solverIt->getInternalMatrix().M());
1188 maxlength = std::max<std::size_t>(maxlength, solverIt->getInternalMatrix().N());
1189 }
1190 }
1191 return maxlength;
1192 }
1193
1194 #endif // HAVE_SUPERLU || HAVE_SUITESPARSE_UMFPACK
1195
1196 template<class M,class X,class Y>
1197 template<class RowToDomain, class Solvers, class SubDomains>
1198 std::size_t SeqOverlappingSchwarzAssemblerILUBase<M,X,Y>::assembleLocalProblems([[maybe_unused]] const RowToDomain& rowToDomain,
1199 const matrix_type& mat,
1200 Solvers& solvers,
1201 const SubDomains& subDomains,
1202 bool onTheFly)
1203 {
1204 typedef typename SubDomains::const_iterator DomainIterator;
1205 typedef typename Solvers::iterator SolverIterator;
1206 std::size_t maxlength = 0;
1207
1208 if(onTheFly) {
1209 for(DomainIterator domain=subDomains.begin(); domain!=subDomains.end(); ++domain)
1210 maxlength=std::max(maxlength, domain->size());
1211 }else{
1212 // initialize the solvers of the local problems.
1213 SolverIterator solver=solvers.begin();
1214 for(DomainIterator domain=subDomains.begin(); domain!=subDomains.end();
1215 ++domain, ++solver) {
1216 solver->setSubMatrix(mat, *domain);
1217 maxlength=std::max(maxlength, domain->size());
1218 }
1219 }
1220
1221 return maxlength;
1222
1223 }
1224
1225
1226 template<class M, class X, class TM, class TD, class TA>
1227 void SeqOverlappingSchwarz<M,X,TM,TD,TA>::apply(X& x, const X& b)
1228 {
1229 SeqOverlappingSchwarzApplier<SeqOverlappingSchwarz>::apply(*this, x, b);
1230 }
1231
1232 template<class M, class X, class TM, class TD, class TA>
1233 template<bool forward>
1234 void SeqOverlappingSchwarz<M,X,TM,TD,TA>::apply(X& x, const X& b)
1235 {
1236 typedef slu_vector solver_vector;
1237 typedef typename IteratorDirectionSelector<solver_vector,subdomain_vector,forward>::solver_iterator iterator;
1238 typedef typename IteratorDirectionSelector<solver_vector,subdomain_vector,forward>::domain_iterator
1239 domain_iterator;
1240
1241 OverlappingAssigner<TD> assigner(maxlength, mat, b, x);
1242
1243 domain_iterator domain=IteratorDirectionSelector<solver_vector,subdomain_vector,forward>::begin(subDomains);
1244 iterator solver = IteratorDirectionSelector<solver_vector,subdomain_vector,forward>::begin(solvers);
1245 X v(x); // temporary for the update
1246 v=0;
1247
1248 typedef typename AdderSelector<TM,X,TD >::Adder Adder;
1249 Adder adder(v, x, assigner, relax);
1250
1251 for(; domain != IteratorDirectionSelector<solver_vector,subdomain_vector,forward>::end(subDomains); ++domain) {
1252 //Copy rhs to C-array for SuperLU
1253 std::for_each(domain->begin(), domain->end(), assigner);
1254 assigner.resetIndexForNextDomain();
1255 if(onTheFly) {
1256 // Create the subdomain solver
1257 slu sdsolver;
1258 sdsolver.setSubMatrix(mat, *domain);
1259 // Apply
1260 sdsolver.apply(assigner.lhs(), assigner.rhs());
1261 }else{
1262 solver->apply(assigner.lhs(), assigner.rhs());
1263 ++solver;
1264 }
1265
1266 //Add relaxed correction to from SuperLU to v
1267 std::for_each(domain->begin(), domain->end(), adder);
1268 assigner.resetIndexForNextDomain();
1269
1270 }
1271
1272 adder.axpy();
1273 assigner.deallocate();
1274 }
1275
1276 template<class K, class Al, class X, class Y>
1277 OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
1278 ::OverlappingAssignerHelper(std::size_t maxlength, const BCRSMatrix<K, Al>& mat_,
1279 const X& b_, Y& x_) :
1280 mat(&mat_),
1281 rhs_( new DynamicVector<field_type>(maxlength, 42) ),
1282 lhs_( new DynamicVector<field_type>(maxlength, -42) ),
1283 b(&b_),
1284 x(&x_),
1285 i(0),
1286 maxlength_(maxlength)
1287 {}
1288
1289 template<class K, class Al, class X, class Y>
1290 void
1291 OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
1292 ::deallocate()
1293 {
1294 delete rhs_;
1295 delete lhs_;
1296 }
1297
1298 template<class K, class Al, class X, class Y>
1299 void
1300 OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
1301 ::resetIndexForNextDomain()
1302 {
1303 i=0;
1304 }
1305
1306 template<class K, class Al, class X, class Y>
1307 DynamicVector<typename X::field_type> &
1308 OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
1309 ::lhs()
1310 {
1311 return *lhs_;
1312 }
1313
1314 template<class K, class Al, class X, class Y>
1315 DynamicVector<typename X::field_type> &
1316 OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
1317 ::rhs()
1318 {
1319 return *rhs_;
1320 }
1321
1322 template<class K, class Al, class X, class Y>
1323 void
1324 OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
1325 ::relaxResult(field_type relax)
1326 {
1327 lhs() *= relax;
1328 }
1329
1330 template<class K, class Al, class X, class Y>
1331 void
1332 OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
1333 ::operator()(const size_type& domainIndex)
1334 {
1335 lhs() = 0.0;
1336 #if 0
1337 //assign right hand side of current domainindex block
1338 for(size_type j=0; j<n; ++j, ++i) {
1339 assert(i<maxlength_);
1340 rhs()[i]=(*b)[domainIndex][j];
1341 }
1342
1343 // loop over all Matrix row entries and calculate defect.
1344 typedef typename matrix_type::ConstColIterator col_iterator;
1345
1346 // calculate defect for current row index block
1347 for(col_iterator col=(*mat)[domainIndex].begin(); col!=(*mat)[domainIndex].end(); ++col) {
1348 block_type tmp(0.0);
1349 (*col).mv((*x)[col.index()], tmp);
1350 i-=n;
1351 for(size_type j=0; j<n; ++j, ++i) {
1352 assert(i<maxlength_);
1353 rhs()[i]-=tmp[j];
1354 }
1355 }
1356 #else
1357 //assign right hand side of current domainindex block
1358 for(size_type j=0; j<n; ++j, ++i) {
1359 assert(i<maxlength_);
1360 rhs()[i]=Impl::asVector((*b)[domainIndex])[j];
1361
1362 // loop over all Matrix row entries and calculate defect.
1363 typedef typename matrix_type::ConstColIterator col_iterator;
1364
1365 // calculate defect for current row index block
1366 for(col_iterator col=(*mat)[domainIndex].begin(); col!=(*mat)[domainIndex].end(); ++col) {
1367 for(size_type k=0; k<n; ++k) {
1368 rhs()[i]-=Impl::asMatrix(*col)[j][k] * Impl::asVector((*x)[col.index()])[k];
1369 }
1370 }
1371 }
1372 #endif
1373 }
1374
1375 template<class K, class Al, class X, class Y>
1376 void
1377 OverlappingAssignerHelper< DynamicMatrixSubdomainSolver< BCRSMatrix< K, Al>, X, Y >,false>
1378 ::assignResult(block_type& res)
1379 {
1380 // assign the result of the local solve to the global vector
1381 for(size_type j=0; j<n; ++j, ++i) {
1382 assert(i<maxlength_);
1383 Impl::asVector(res)[j]+=lhs()[i];
1384 }
1385 }
1386
1387 #if HAVE_SUPERLU || HAVE_SUITESPARSE_UMFPACK
1388
1389 template<template<class> class S, typename T, typename A>
1390 OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>
1391 ::OverlappingAssignerHelper(std::size_t maxlength,
1392 const BCRSMatrix<T,A>& mat_,
1393 const range_type& b_,
1394 range_type& x_)
1395 : mat(&mat_),
1396 b(&b_),
1397 x(&x_), i(0), maxlength_(maxlength)
1398 {
1399 rhs_ = new field_type[maxlength];
1400 lhs_ = new field_type[maxlength];
1401
1402 }
1403
1404 template<template<class> class S, typename T, typename A>
1405 void OverlappingAssignerHelper<S<BCRSMatrix<T,A> >,true>::deallocate()
1406 {
1407 delete[] rhs_;
1408 delete[] lhs_;
1409 }
1410
1411 template<template<class> class S, typename T, typename A>
1412 void OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>::operator()(const size_type& domainIndex)
1413 {
1414 //assign right hand side of current domainindex block
1415 // rhs is an array of doubles!
1416 // rhs[starti] = b[domainindex]
1417 for(size_type j=0; j<n; ++j, ++i) {
1418 assert(i<maxlength_);
1419 rhs_[i]=Impl::asVector((*b)[domainIndex])[j];
1420 }
1421
1422
1423 // loop over all Matrix row entries and calculate defect.
1424 typedef typename matrix_type::ConstColIterator col_iterator;
1425
1426 // calculate defect for current row index block
1427 for(col_iterator col=(*mat)[domainIndex].begin(); col!=(*mat)[domainIndex].end(); ++col) {
1428 block_type tmp;
1429 Impl::asMatrix(*col).mv((*x)[col.index()], tmp);
1430 i-=n;
1431 for(size_type j=0; j<n; ++j, ++i) {
1432 assert(i<maxlength_);
1433 rhs_[i]-=Impl::asVector(tmp)[j];
1434 }
1435
1436 }
1437
1438 }
1439
1440 template<template<class> class S, typename T, typename A>
1441 void OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>::relaxResult(field_type relax)
1442 {
1443 for(size_type j=i+n; i<j; ++i) {
1444 assert(i<maxlength_);
1445 lhs_[i]*=relax;
1446 }
1447 i-=n;
1448 }
1449
1450 template<template<class> class S, typename T, typename A>
1451 void OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>::assignResult(block_type& res)
1452 {
1453 // assign the result of the local solve to the global vector
1454 for(size_type j=0; j<n; ++j, ++i) {
1455 assert(i<maxlength_);
1456 Impl::asVector(res)[j]+=lhs_[i];
1457 }
1458 }
1459
1460 template<template<class> class S, typename T, typename A>
1461 void OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>::resetIndexForNextDomain()
1462 {
1463 i=0;
1464 }
1465
1466 template<template<class> class S, typename T, typename A>
1467 typename OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>::field_type*
1468 OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>::lhs()
1469 {
1470 return lhs_;
1471 }
1472
1473 template<template<class> class S, typename T, typename A>
1474 typename OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>::field_type*
1475 OverlappingAssignerHelper<S<BCRSMatrix<T,A>>,true>::rhs()
1476 {
1477 return rhs_;
1478 }
1479
1480 #endif // HAVE_SUPERLU || HAVE_SUITESPARSE_UMFPACK
1481
1482 template<class M, class X, class Y>
1483 OverlappingAssignerILUBase<M,X,Y>::OverlappingAssignerILUBase(std::size_t maxlength,
1484 const M& mat_,
1485 const Y& b_,
1486 X& x_)
1487 : mat(&mat_),
1488 b(&b_),
1489 x(&x_), i(0)
1490 {
1491 rhs_= new Y(maxlength);
1492 lhs_ = new X(maxlength);
1493 }
1494
1495 template<class M, class X, class Y>
1496 void OverlappingAssignerILUBase<M,X,Y>::deallocate()
1497 {
1498 delete rhs_;
1499 delete lhs_;
1500 }
1501
1502 template<class M, class X, class Y>
1503 void OverlappingAssignerILUBase<M,X,Y>::operator()(const size_type& domainIndex)
1504 {
1505 (*rhs_)[i]=(*b)[domainIndex];
1506
1507 // loop over all Matrix row entries and calculate defect.
1508 typedef typename matrix_type::ConstColIterator col_iterator;
1509
1510 // calculate defect for current row index block
1511 for(col_iterator col=(*mat)[domainIndex].begin(); col!=(*mat)[domainIndex].end(); ++col) {
1512 Impl::asMatrix(*col).mmv((*x)[col.index()], (*rhs_)[i]);
1513 }
1514 // Goto next local index
1515 ++i;
1516 }
1517
1518 template<class M, class X, class Y>
1519 void OverlappingAssignerILUBase<M,X,Y>::relaxResult(field_type relax)
1520 {
1521 (*lhs_)[i]*=relax;
1522 }
1523
1524 template<class M, class X, class Y>
1525 void OverlappingAssignerILUBase<M,X,Y>::assignResult(block_type& res)
1526 {
1527 res+=(*lhs_)[i++];
1528 }
1529
1530 template<class M, class X, class Y>
1531 X& OverlappingAssignerILUBase<M,X,Y>::lhs()
1532 {
1533 return *lhs_;
1534 }
1535
1536 template<class M, class X, class Y>
1537 Y& OverlappingAssignerILUBase<M,X,Y>::rhs()
1538 {
1539 return *rhs_;
1540 }
1541
1542 template<class M, class X, class Y>
1543 void OverlappingAssignerILUBase<M,X,Y>::resetIndexForNextDomain()
1544 {
1545 i=0;
1546 }
1547
1548 template<typename S, typename T, typename A>
1549 AdditiveAdder<S,BlockVector<T,A> >::AdditiveAdder(BlockVector<T,A>& v_,
1550 BlockVector<T,A>& x_,
1551 OverlappingAssigner<S>& assigner_,
1552 const field_type& relax_)
1553 : v(&v_), x(&x_), assigner(&assigner_), relax(relax_)
1554 {}
1555
1556 template<typename S, typename T, typename A>
1557 void AdditiveAdder<S,BlockVector<T,A> >::operator()(const size_type& domainIndex)
1558 {
1559 // add the result of the local solve to the current update
1560 assigner->assignResult((*v)[domainIndex]);
1561 }
1562
1563
1564 template<typename S, typename T, typename A>
1565 void AdditiveAdder<S,BlockVector<T,A> >::axpy()
1566 {
1567 // relax the update and add it to the current guess.
1568 x->axpy(relax,*v);
1569 }
1570
1571
1572 template<typename S, typename T, typename A>
1573 MultiplicativeAdder<S,BlockVector<T,A> >
1574 ::MultiplicativeAdder([[maybe_unused]] BlockVector<T,A>& v_,
1575 BlockVector<T,A>& x_,
1576 OverlappingAssigner<S>& assigner_, const field_type& relax_)
1577 : x(&x_), assigner(&assigner_), relax(relax_)
1578 {}
1579
1580
1581 template<typename S,typename T, typename A>
1582 void MultiplicativeAdder<S,BlockVector<T,A> >::operator()(const size_type& domainIndex)
1583 {
1584 // add the result of the local solve to the current guess
1585 assigner->relaxResult(relax);
1586 assigner->assignResult((*x)[domainIndex]);
1587 }
1588
1589
1590 template<typename S,typename T, typename A>
1591 void MultiplicativeAdder<S,BlockVector<T,A> >::axpy()
1592 {
1593 // nothing to do, as the corrections already relaxed and added in operator()
1594 }
1595
1596
1597 /** @} */
1598 }
1599
1600 #endif