DUNE-FEM (unstable)

gsetc.hh
Go to the documentation of this file.
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_GSETC_HH
6#define DUNE_ISTL_GSETC_HH
7
8#include <cmath>
9#include <complex>
10#include <iostream>
11#include <iomanip>
12#include <string>
13
14#include <dune/common/hybridutilities.hh>
15
16#include "multitypeblockvector.hh"
17#include "multitypeblockmatrix.hh"
18
19#include "istlexception.hh"
20
21
27namespace Dune {
28
39 //============================================================
40 // parameter types
41 //============================================================
42
44 template<int l>
45 struct BL {
46 enum {recursion_level = l};
47 };
48
49 enum WithDiagType {
50 withdiag=1,
51 nodiag=0
52 };
53
54 enum WithRelaxType {
55 withrelax=1,
56 norelax=0
57 };
58
59 //============================================================
60 // generic triangular solves
61 // consider block decomposition A = L + D + U
62 // we can invert L, L+D, U, U+D
63 // we can apply relaxation or not
64 // we can recurse over a fixed number of levels
65 //============================================================
66
67 // template meta program for triangular solves
68 template<int I, WithDiagType diag, WithRelaxType relax>
69 struct algmeta_btsolve {
70 template<class M, class X, class Y, class K>
71 static void bltsolve (const M& A, X& v, const Y& d, const K& w)
72 {
73 // iterator types
74 typedef typename M::ConstRowIterator rowiterator;
75 typedef typename M::ConstColIterator coliterator;
76 typedef typename Y::block_type bblock;
77
78 // local solve at each block and immediate update
79 rowiterator endi=A.end();
80 for (rowiterator i=A.begin(); i!=endi; ++i)
81 {
82 bblock rhs(d[i.index()]);
83 coliterator j;
84 for (j=(*i).begin(); j.index()<i.index(); ++j)
85 (*j).mmv(v[j.index()],rhs);
86 algmeta_btsolve<I-1,diag,relax>::bltsolve(*j,v[i.index()],rhs,w);
87 }
88 }
89 template<class M, class X, class Y, class K>
90 static void butsolve (const M& A, X& v, const Y& d, const K& w)
91 {
92 // iterator types
93 typedef typename M::ConstRowIterator rowiterator;
94 typedef typename M::ConstColIterator coliterator;
95 typedef typename Y::block_type bblock;
96
97 // local solve at each block and immediate update
98 rowiterator rendi=A.beforeBegin();
99 for (rowiterator i=A.beforeEnd(); i!=rendi; --i)
100 {
101 bblock rhs(d[i.index()]);
102 coliterator j;
103 for (j=(*i).beforeEnd(); j.index()>i.index(); --j)
104 (*j).mmv(v[j.index()],rhs);
105 algmeta_btsolve<I-1,diag,relax>::butsolve(*j,v[i.index()],rhs,w);
106 }
107 }
108 };
109
110 // recursion end ...
111 template<>
112 struct algmeta_btsolve<0,withdiag,withrelax> {
113 template<class M, class X, class Y, class K>
114 static void bltsolve (const M& A, X& v, const Y& d, const K& w)
115 {
116 A.solve(v,d);
117 v *= w;
118 }
119 template<class M, class X, class Y, class K>
120 static void butsolve (const M& A, X& v, const Y& d, const K& w)
121 {
122 A.solve(v,d);
123 v *= w;
124 }
125 };
126 template<>
127 struct algmeta_btsolve<0,withdiag,norelax> {
128 template<class M, class X, class Y, class K>
129 static void bltsolve (const M& A, X& v, const Y& d, const K& /*w*/)
130 {
131 A.solve(v,d);
132 }
133 template<class M, class X, class Y, class K>
134 static void butsolve (const M& A, X& v, const Y& d, const K& /*w*/)
135 {
136 A.solve(v,d);
137 }
138 };
139 template<>
140 struct algmeta_btsolve<0,nodiag,withrelax> {
141 template<class M, class X, class Y, class K>
142 static void bltsolve (const M& /*A*/, X& v, const Y& d, const K& w)
143 {
144 v = d;
145 v *= w;
146 }
147 template<class M, class X, class Y, class K>
148 static void butsolve (const M& /*A*/, X& v, const Y& d, const K& w)
149 {
150 v = d;
151 v *= w;
152 }
153 };
154 template<>
155 struct algmeta_btsolve<0,nodiag,norelax> {
156 template<class M, class X, class Y, class K>
157 static void bltsolve (const M& /*A*/, X& v, const Y& d, const K& /*w*/)
158 {
159 v = d;
160 }
161 template<class M, class X, class Y, class K>
162 static void butsolve (const M& /*A*/, X& v, const Y& d, const K& /*w*/)
163 {
164 v = d;
165 }
166 };
167
168
169 // user calls
170
171 // default block recursion level = 1
172
174 template<class M, class X, class Y>
175 void bltsolve (const M& A, X& v, const Y& d)
176 {
177 typename X::field_type w=1;
179 }
181 template<class M, class X, class Y, class K>
182 void bltsolve (const M& A, X& v, const Y& d, const K& w)
183 {
185 }
187 template<class M, class X, class Y>
188 void ubltsolve (const M& A, X& v, const Y& d)
189 {
190 typename X::field_type w=1;
192 }
194 template<class M, class X, class Y, class K>
195 void ubltsolve (const M& A, X& v, const Y& d, const K& w)
196 {
198 }
199
201 template<class M, class X, class Y>
202 void butsolve (const M& A, X& v, const Y& d)
203 {
204 typename X::field_type w=1;
206 }
208 template<class M, class X, class Y, class K>
209 void butsolve (const M& A, X& v, const Y& d, const K& w)
210 {
212 }
214 template<class M, class X, class Y>
215 void ubutsolve (const M& A, X& v, const Y& d)
216 {
217 typename X::field_type w=1;
219 }
221 template<class M, class X, class Y, class K>
222 void ubutsolve (const M& A, X& v, const Y& d, const K& w)
223 {
225 }
226
227 // general block recursion level >= 0
228
230 template<class M, class X, class Y, int l>
231 void bltsolve (const M& A, X& v, const Y& d, BL<l> /*bl*/)
232 {
233 typename X::field_type w=1;
235 }
237 template<class M, class X, class Y, class K, int l>
238 void bltsolve (const M& A, X& v, const Y& d, const K& w, BL<l> /*bl*/)
239 {
241 }
243 template<class M, class X, class Y, int l>
244 void ubltsolve (const M& A, X& v, const Y& d, BL<l> /*bl*/)
245 {
246 typename X::field_type w=1;
248 }
250 template<class M, class X, class Y, class K, int l>
251 void ubltsolve (const M& A, X& v, const Y& d, const K& w, BL<l> /*bl*/)
252 {
254 }
255
257 template<class M, class X, class Y, int l>
258 void butsolve (const M& A, X& v, const Y& d, BL<l> /*bl*/)
259 {
260 typename X::field_type w=1;
262 }
264 template<class M, class X, class Y, class K, int l>
265 void butsolve (const M& A, X& v, const Y& d, const K& w, BL<l> /*bl*/)
266 {
268 }
270 template<class M, class X, class Y, int l>
271 void ubutsolve (const M& A, X& v, const Y& d, BL<l> /*bl*/)
272 {
273 typename X::field_type w=1;
275 }
277 template<class M, class X, class Y, class K, int l>
278 void ubutsolve (const M& A, X& v, const Y& d, const K& w, BL<l> /*bl*/)
279 {
281 }
282
283
284
285 //============================================================
286 // generic block diagonal solves
287 // consider block decomposition A = L + D + U
288 // we can apply relaxation or not
289 // we can recurse over a fixed number of levels
290 //============================================================
291
292 // template meta program for diagonal solves
293 template<int I, WithRelaxType relax>
294 struct algmeta_bdsolve {
295 template<class M, class X, class Y, class K>
296 static void bdsolve (const M& A, X& v, const Y& d, const K& w)
297 {
298 // iterator types
299 typedef typename M::ConstRowIterator rowiterator;
300 typedef typename M::ConstColIterator coliterator;
301
302 // local solve at each block and immediate update
303 rowiterator rendi=A.beforeBegin();
304 for (rowiterator i=A.beforeEnd(); i!=rendi; --i)
305 {
306 coliterator ii=(*i).find(i.index());
307 algmeta_bdsolve<I-1,relax>::bdsolve(*ii,v[i.index()],d[i.index()],w);
308 }
309 }
310 };
311
312 // recursion end ...
313 template<>
314 struct algmeta_bdsolve<0,withrelax> {
315 template<class M, class X, class Y, class K>
316 static void bdsolve (const M& A, X& v, const Y& d, const K& w)
317 {
318 A.solve(v,d);
319 v *= w;
320 }
321 };
322 template<>
323 struct algmeta_bdsolve<0,norelax> {
324 template<class M, class X, class Y, class K>
325 static void bdsolve (const M& A, X& v, const Y& d, const K& /*w*/)
326 {
327 A.solve(v,d);
328 }
329 };
330
331 // user calls
332
333 // default block recursion level = 1
334
336 template<class M, class X, class Y>
337 void bdsolve (const M& A, X& v, const Y& d)
338 {
339 typename X::field_type w=1;
341 }
343 template<class M, class X, class Y, class K>
344 void bdsolve (const M& A, X& v, const Y& d, const K& w)
345 {
347 }
348
349 // general block recursion level >= 0
350
352 template<class M, class X, class Y, int l>
353 void bdsolve (const M& A, X& v, const Y& d, BL<l> /*bl*/)
354 {
355 typename X::field_type w=1;
357 }
359 template<class M, class X, class Y, class K, int l>
360 void bdsolve (const M& A, X& v, const Y& d, const K& w, BL<l> /*bl*/)
361 {
363 }
364
365
366 //============================================================
367 // generic steps of iteration methods
368 // Jacobi, Gauss-Seidel, SOR, SSOR
369 // work directly on Ax=b, ie solve M(x^{i+1}-x^i) = w (b-Ax^i)
370 // we can recurse over a fixed number of levels
371 //============================================================
372
373 // template meta program for iterative solver steps
374 template<int I, typename M>
375 struct algmeta_itsteps {
376
377 template<class X, class Y, class K>
378 static void dbgs (const M& A, X& x, const Y& b, const K& w)
379 {
380 typedef typename M::ConstRowIterator rowiterator;
381 typedef typename M::ConstColIterator coliterator;
382 typedef typename Y::block_type bblock;
383 bblock rhs;
384
385 X xold(x); // remember old x
386
387 rowiterator endi=A.end();
388 for (rowiterator i=A.begin(); i!=endi; ++i)
389 {
390 rhs = b[i.index()]; // rhs = b_i
391 coliterator endj=(*i).end();
392 coliterator j=(*i).begin();
393 if constexpr (IsNumber<typename M::block_type>())
394 {
395 for (; j.index()<i.index(); ++j)
396 rhs -= (*j) * x[j.index()];
397 coliterator diag=j++; // *diag = a_ii and increment coliterator j from a_ii to a_i+1,i to skip diagonal
398 for (; j != endj; ++j)
399 rhs -= (*j) * x[j.index()];
400 x[i.index()] = rhs / (*diag);
401 }
402 else
403 {
404 for (; j.index()<i.index(); ++j) // iterate over a_ij with j < i
405 (*j).mmv(x[j.index()],rhs); // rhs -= sum_{j<i} a_ij * xnew_j
406 coliterator diag=j++; // *diag = a_ii and increment coliterator j from a_ii to a_i+1,i to skip diagonal
407 for (; j != endj; ++j) // iterate over a_ij with j > i
408 (*j).mmv(x[j.index()],rhs); // rhs -= sum_{j>i} a_ij * xold_j
409 algmeta_itsteps<I-1,typename M::block_type>::dbgs(*diag,x[i.index()],rhs,w); // if I==1: xnew_i = rhs/a_ii
410 }
411 }
412 // next two lines: xnew_i = w / a_ii * (b_i - sum_{j<i} a_ij * xnew_j - sum_{j>=i} a_ij * xold_j) + (1-w)*xold;
413 x *= w;
414 x.axpy(K(1)-w,xold);
415 }
416
417 template<class X, class Y, class K>
418 static void bsorf (const M& A, X& x, const Y& b, const K& w)
419 {
420 typedef typename M::ConstRowIterator rowiterator;
421 typedef typename M::ConstColIterator coliterator;
422 typedef typename Y::block_type bblock;
423 typedef typename X::block_type xblock;
424 bblock rhs;
425 xblock v;
426
427 rowiterator endi=A.end();
428 for (rowiterator i=A.begin(); i!=endi; ++i)
429 {
430 rhs = b[i.index()]; // rhs = b_i
431 coliterator endj=(*i).end(); // iterate over a_ij with j < i
432 coliterator j=(*i).begin();
433 if constexpr (IsNumber<typename M::block_type>())
434 {
435 for (; j.index()<i.index(); ++j)
436 rhs -= (*j) * x[j.index()]; // rhs -= sum_{j<i} a_ij * xnew_j
437 coliterator diag=j; // *diag = a_ii
438 for (; j!=endj; ++j)
439 rhs -= (*j) * x[j.index()]; // rhs -= sum_{j<i} a_ij * xnew_j
440 v = rhs / (*diag);
441 x[i.index()] += w*v; // x_i = w / a_ii * (b_i - sum_{j<i} a_ij * xnew_j - sum_{j>=i} a_ij * xold_j)
442 }
443 else
444 {
445 for (; j.index()<i.index(); ++j)
446 (*j).mmv(x[j.index()],rhs); // rhs -= sum_{j<i} a_ij * xnew_j
447 coliterator diag=j; // *diag = a_ii
448 for (; j!=endj; ++j)
449 (*j).mmv(x[j.index()],rhs); // rhs -= sum_{j<i} a_ij * xnew_j
450 v=x[i.index()]; // Initialize nested data structure if there are entries
451 algmeta_itsteps<I-1,typename M::block_type>::bsorf(*diag,v,rhs,w); // if blocksize I==1: v = rhs/a_ii
452 x[i.index()].axpy(w,v); // x_i = w / a_ii * (b_i - sum_{j<i} a_ij * xnew_j - sum_{j>=i} a_ij * xold_j)
453 }
454 }
455 }
456
457 template<class X, class Y, class K>
458 static void bsorb (const M& A, X& x, const Y& b, const K& w)
459 {
460 typedef typename M::ConstRowIterator rowiterator;
461 typedef typename M::ConstColIterator coliterator;
462 typedef typename Y::block_type bblock;
463 typedef typename X::block_type xblock;
464 bblock rhs;
465 xblock v;
466
467 rowiterator endi=A.beforeBegin();
468 for (rowiterator i=A.beforeEnd(); i!=endi; --i)
469 {
470 rhs = b[i.index()];
471 coliterator endj=(*i).end();
472 coliterator j=(*i).begin();
473 if constexpr (IsNumber<typename M::block_type>())
474 {
475 for (; j.index()<i.index(); ++j)
476 rhs -= (*j) * x[j.index()];
477 coliterator diag=j;
478 for (; j!=endj; ++j)
479 rhs -= (*j) * x[j.index()];
480 v = rhs / (*diag);
481 x[i.index()] += w*v;
482 }
483 else
484 {
485 for (; j.index()<i.index(); ++j)
486 j->mmv(x[j.index()],rhs);
487 coliterator diag=j;
488 for (; j!=endj; ++j)
489 j->mmv(x[j.index()],rhs);
490 v = x[i.index()]; // Initialize nested data structure if there are entries
492 x[i.index()].axpy(w,v);
493 }
494 }
495 }
496
497 template<class X, class Y, class K>
498 static void dbjac (const M& A, X& x, const Y& b, const K& w)
499 {
500 typedef typename M::ConstRowIterator rowiterator;
501 typedef typename M::ConstColIterator coliterator;
502 typedef typename Y::block_type bblock;
503 bblock rhs;
504
505 X v(x); // allocate with same size
506
507 rowiterator endi=A.end();
508 for (rowiterator i=A.begin(); i!=endi; ++i)
509 {
510 rhs = b[i.index()];
511 coliterator endj=(*i).end();
512 coliterator j=(*i).begin();
513 if constexpr (IsNumber<typename M::block_type>())
514 {
515 for (; j.index()<i.index(); ++j)
516 rhs -= (*j) * x[j.index()];
517 coliterator diag=j;
518 for (; j!=endj; ++j)
519 rhs -= (*j) * x[j.index()];
520 v[i.index()] = rhs / (*diag);
521 }
522 else
523 {
524 for (; j.index()<i.index(); ++j)
525 j->mmv(x[j.index()],rhs);
526 coliterator diag=j;
527 for (; j!=endj; ++j)
528 j->mmv(x[j.index()],rhs);
530 }
531 }
532 x.axpy(w,v);
533 }
534 };
535 // end of recursion
536 template<typename M>
537 struct algmeta_itsteps<0,M> {
538 template<class X, class Y, class K>
539 static void dbgs (const M& A, X& x, const Y& b, const K& /*w*/)
540 {
541 A.solve(x,b);
542 }
543 template<class X, class Y, class K>
544 static void bsorf (const M& A, X& x, const Y& b, const K& /*w*/)
545 {
546 A.solve(x,b);
547 }
548 template<class X, class Y, class K>
549 static void bsorb (const M& A, X& x, const Y& b, const K& /*w*/)
550 {
551 A.solve(x,b);
552 }
553 template<class X, class Y, class K>
554 static void dbjac (const M& A, X& x, const Y& b, const K& /*w*/)
555 {
556 A.solve(x,b);
557 }
558 };
559
560 template<int I, typename T1, typename... MultiTypeMatrixArgs>
561 struct algmeta_itsteps<I,MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>> {
562 template<
563 typename... MultiTypeVectorArgs,
564 class K>
565 static void dbgs (const MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>& A,
566 MultiTypeBlockVector<MultiTypeVectorArgs...>& x,
567 const MultiTypeBlockVector<MultiTypeVectorArgs...>& b,
568 const K& w)
569 {
572 }
573
574 template<
575 typename... MultiTypeVectorArgs,
576 class K>
577 static void bsorf (const MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>& A,
578 MultiTypeBlockVector<MultiTypeVectorArgs...>& x,
579 const MultiTypeBlockVector<MultiTypeVectorArgs...>& b,
580 const K& w)
581 {
584 }
585
586 template<
587 typename... MultiTypeVectorArgs,
588 class K>
589 static void bsorb (const MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>& A,
590 MultiTypeBlockVector<MultiTypeVectorArgs...>& x,
591 const MultiTypeBlockVector<MultiTypeVectorArgs...>& b,
592 const K& w)
593 {
596 }
597
598 template<
599 typename... MultiTypeVectorArgs,
600 class K
601 >
602 static void dbjac (const MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>& A,
603 MultiTypeBlockVector<MultiTypeVectorArgs...>& x,
604 const MultiTypeBlockVector<MultiTypeVectorArgs...>& b,
605 const K& w)
606 {
609 }
610 };
611
612 // user calls
613
615 template<class M, class X, class Y, class K>
616 void dbgs (const M& A, X& x, const Y& b, const K& w)
617 {
619 }
621 template<class M, class X, class Y, class K, int l>
622 void dbgs (const M& A, X& x, const Y& b, const K& w, BL<l> /*bl*/)
623 {
625 }
627 template<class M, class X, class Y, class K>
628 void bsorf (const M& A, X& x, const Y& b, const K& w)
629 {
631 }
633 template<class M, class X, class Y, class K, int l>
634 void bsorf (const M& A, X& x, const Y& b, const K& w, BL<l> /*bl*/)
635 {
637 }
639 template<class M, class X, class Y, class K>
640 void bsorb (const M& A, X& x, const Y& b, const K& w)
641 {
643 }
645 template<class M, class X, class Y, class K, int l>
646 void bsorb (const M& A, X& x, const Y& b, const K& w, BL<l> /*bl*/)
647 {
648 algmeta_itsteps<l,typename std::remove_cv<M>::type>::bsorb(A,x,b,w);
649 }
651 template<class M, class X, class Y, class K>
652 void dbjac (const M& A, X& x, const Y& b, const K& w)
653 {
655 }
657 template<class M, class X, class Y, class K, int l>
658 void dbjac (const M& A, X& x, const Y& b, const K& w, BL<l> /*bl*/)
659 {
661 }
662
663
666} // end namespace
667
668#endif
static void dbjac(const TMatrix &A, TVector &x, const TVector &b, const K &w)
Definition: multitypeblockmatrix.hh:584
static void dbgs(const TMatrix &A, TVector &x, const TVector &b, const K &w)
Definition: multitypeblockmatrix.hh:500
static void bsorb(const TMatrix &A, TVector &x, const TVector &b, const K &w)
Definition: multitypeblockmatrix.hh:556
static constexpr size_type N()
Return the number of matrix rows.
Definition: multitypeblockmatrix.hh:84
static void bsorf(const TMatrix &A, TVector &x, const TVector &b, const K &w)
Definition: multitypeblockmatrix.hh:529
void butsolve(const M &A, X &v, const Y &d, const K &w, BL< l >)
relaxed block upper triangular solve
Definition: gsetc.hh:265
void bltsolve(const M &A, X &v, const Y &d)
block lower triangular solve
Definition: gsetc.hh:175
void bsorb(const M &A, X &x, const Y &b, const K &w)
SSOR step.
Definition: gsetc.hh:640
void ubltsolve(const M &A, X &v, const Y &d)
unit block lower triangular solve
Definition: gsetc.hh:188
void dbjac(const M &A, X &x, const Y &b, const K &w)
Jacobi step.
Definition: gsetc.hh:652
void dbgs(const M &A, X &x, const Y &b, const K &w)
GS step.
Definition: gsetc.hh:616
void bsorf(const M &A, X &x, const Y &b, const K &w, BL< l >)
SOR step.
Definition: gsetc.hh:634
void dbjac(const M &A, X &x, const Y &b, const K &w, BL< l >)
Jacobi step.
Definition: gsetc.hh:658
void dbgs(const M &A, X &x, const Y &b, const K &w, BL< l >)
GS step.
Definition: gsetc.hh:622
void bltsolve(const M &A, X &v, const Y &d, const K &w, BL< l >)
relaxed block lower triangular solve
Definition: gsetc.hh:238
void bdsolve(const M &A, X &v, const Y &d)
block diagonal solve, no relaxation
Definition: gsetc.hh:337
void bsorb(const M &A, X &x, const Y &b, const K &w, BL< l >)
Backward SOR step.
Definition: gsetc.hh:646
void bdsolve(const M &A, X &v, const Y &d, const K &w, BL< l >)
block diagonal solve, with relaxation
Definition: gsetc.hh:360
void butsolve(const M &A, X &v, const Y &d)
block upper triangular solve
Definition: gsetc.hh:202
void bsorf(const M &A, X &x, const Y &b, const K &w)
SOR step.
Definition: gsetc.hh:628
void ubutsolve(const M &A, X &v, const Y &d)
unit block upper triangular solve
Definition: gsetc.hh:215
Dune namespace
Definition: alignedallocator.hh:13
compile-time parameter for block recursion depth
Definition: gsetc.hh:45
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (May 9, 22:32, 2026)