| 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 |
|
| 22 |
/*! \file |
| 23 |
\brief Simple iterative methods like Jacobi, Gauss-Seidel, SOR, SSOR, etc. |
| 24 |
in a generic way |
| 25 |
*/ |
| 26 |
|
| 27 |
namespace Dune { |
| 28 |
|
| 29 |
/** |
| 30 |
* @defgroup ISTL_Kernel Block Recursive Iterative Kernels |
| 31 |
* @ingroup ISTL_SPMV |
| 32 |
* |
| 33 |
* Generic iterative kernels for the solvers which work on the block recursive |
| 34 |
* structure of the matrices and vectors. |
| 35 |
* @addtogroup ISTL_Kernel |
| 36 |
* @{ |
| 37 |
*/ |
| 38 |
|
| 39 |
//============================================================ |
| 40 |
// parameter types |
| 41 |
//============================================================ |
| 42 |
|
| 43 |
//! compile-time parameter for block recursion depth |
| 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 |
|
| 173 |
//! block lower triangular solve |
| 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; |
| 178 |
algmeta_btsolve<1,withdiag,norelax>::bltsolve(A,v,d,w); |
| 179 |
} |
| 180 |
//! relaxed block lower triangular solve |
| 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 |
{ |
| 184 |
algmeta_btsolve<1,withdiag,withrelax>::bltsolve(A,v,d,w); |
| 185 |
} |
| 186 |
//! unit block lower triangular solve |
| 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; |
| 191 |
algmeta_btsolve<1,nodiag,norelax>::bltsolve(A,v,d,w); |
| 192 |
} |
| 193 |
//! relaxed unit block lower triangular solve |
| 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 |
{ |
| 197 |
algmeta_btsolve<1,nodiag,withrelax>::bltsolve(A,v,d,w); |
| 198 |
} |
| 199 |
|
| 200 |
//! block upper triangular solve |
| 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; |
| 205 |
algmeta_btsolve<1,withdiag,norelax>::butsolve(A,v,d,w); |
| 206 |
} |
| 207 |
//! relaxed block upper triangular solve |
| 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 |
{ |
| 211 |
algmeta_btsolve<1,withdiag,withrelax>::butsolve(A,v,d,w); |
| 212 |
} |
| 213 |
//! unit block upper triangular solve |
| 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; |
| 218 |
algmeta_btsolve<1,nodiag,norelax>::butsolve(A,v,d,w); |
| 219 |
} |
| 220 |
//! relaxed unit block upper triangular solve |
| 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 |
{ |
| 224 |
algmeta_btsolve<1,nodiag,withrelax>::butsolve(A,v,d,w); |
| 225 |
} |
| 226 |
|
| 227 |
// general block recursion level >= 0 |
| 228 |
|
| 229 |
//! block lower triangular solve |
| 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; |
| 234 |
algmeta_btsolve<l,withdiag,norelax>::bltsolve(A,v,d,w); |
| 235 |
} |
| 236 |
//! relaxed block lower triangular solve |
| 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 |
{ |
| 240 |
algmeta_btsolve<l,withdiag,withrelax>::bltsolve(A,v,d,w); |
| 241 |
} |
| 242 |
//! unit block lower triangular solve |
| 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; |
| 247 |
algmeta_btsolve<l,nodiag,norelax>::bltsolve(A,v,d,w); |
| 248 |
} |
| 249 |
//! relaxed unit block lower triangular solve |
| 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 |
{ |
| 253 |
algmeta_btsolve<l,nodiag,withrelax>::bltsolve(A,v,d,w); |
| 254 |
} |
| 255 |
|
| 256 |
//! block upper triangular solve |
| 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; |
| 261 |
algmeta_btsolve<l,withdiag,norelax>::butsolve(A,v,d,w); |
| 262 |
} |
| 263 |
//! relaxed block upper triangular solve |
| 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 |
{ |
| 267 |
algmeta_btsolve<l,withdiag,withrelax>::butsolve(A,v,d,w); |
| 268 |
} |
| 269 |
//! unit block upper triangular solve |
| 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; |
| 274 |
algmeta_btsolve<l,nodiag,norelax>::butsolve(A,v,d,w); |
| 275 |
} |
| 276 |
//! relaxed unit block upper triangular solve |
| 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 |
{ |
| 280 |
algmeta_btsolve<l,nodiag,withrelax>::butsolve(A,v,d,w); |
| 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 |
|
| 335 |
//! block diagonal solve, no relaxation |
| 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; |
| 340 |
algmeta_bdsolve<1,norelax>::bdsolve(A,v,d,w); |
| 341 |
} |
| 342 |
//! block diagonal solve, with relaxation |
| 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 |
{ |
| 346 |
algmeta_bdsolve<1,withrelax>::bdsolve(A,v,d,w); |
| 347 |
} |
| 348 |
|
| 349 |
// general block recursion level >= 0 |
| 350 |
|
| 351 |
//! block diagonal solve, no relaxation |
| 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; |
| 356 |
algmeta_bdsolve<l,norelax>::bdsolve(A,v,d,w); |
| 357 |
} |
| 358 |
//! block diagonal solve, with relaxation |
| 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 |
{ |
| 362 |
algmeta_bdsolve<l,withrelax>::bdsolve(A,v,d,w); |
| 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 |
// Initialize nested data structure if there are entries |
| 428 |
if(A.begin()!=A.end()) |
| 429 |
v=x[0]; |
| 430 |
|
| 431 |
rowiterator endi=A.end(); |
| 432 |
for (rowiterator i=A.begin(); i!=endi; ++i) |
| 433 |
{ |
| 434 |
rhs = b[i.index()]; // rhs = b_i |
| 435 |
coliterator endj=(*i).end(); // iterate over a_ij with j < i |
| 436 |
coliterator j=(*i).begin(); |
| 437 |
if constexpr (IsNumber<typename M::block_type>()) |
| 438 |
{ |
| 439 |
for (; j.index()<i.index(); ++j) |
| 440 |
rhs -= (*j) * x[j.index()]; // rhs -= sum_{j<i} a_ij * xnew_j |
| 441 |
coliterator diag=j; // *diag = a_ii |
| 442 |
for (; j!=endj; ++j) |
| 443 |
rhs -= (*j) * x[j.index()]; // rhs -= sum_{j<i} a_ij * xnew_j |
| 444 |
v = rhs / (*diag); |
| 445 |
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) |
| 446 |
} |
| 447 |
else |
| 448 |
{ |
| 449 |
for (; j.index()<i.index(); ++j) |
| 450 |
(*j).mmv(x[j.index()],rhs); // rhs -= sum_{j<i} a_ij * xnew_j |
| 451 |
coliterator diag=j; // *diag = a_ii |
| 452 |
for (; j!=endj; ++j) |
| 453 |
(*j).mmv(x[j.index()],rhs); // rhs -= sum_{j<i} a_ij * xnew_j |
| 454 |
algmeta_itsteps<I-1,typename M::block_type>::bsorf(*diag,v,rhs,w); // if blocksize I==1: v = rhs/a_ii |
| 455 |
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) |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
template<class X, class Y, class K> |
| 461 |
static void bsorb (const M& A, X& x, const Y& b, const K& w) |
| 462 |
{ |
| 463 |
typedef typename M::ConstRowIterator rowiterator; |
| 464 |
typedef typename M::ConstColIterator coliterator; |
| 465 |
typedef typename Y::block_type bblock; |
| 466 |
typedef typename X::block_type xblock; |
| 467 |
bblock rhs; |
| 468 |
xblock v; |
| 469 |
|
| 470 |
// Initialize nested data structure if there are entries |
| 471 |
if(A.begin()!=A.end()) |
| 472 |
v=x[0]; |
| 473 |
|
| 474 |
rowiterator endi=A.beforeBegin(); |
| 475 |
for (rowiterator i=A.beforeEnd(); i!=endi; --i) |
| 476 |
{ |
| 477 |
rhs = b[i.index()]; |
| 478 |
coliterator endj=(*i).end(); |
| 479 |
coliterator j=(*i).begin(); |
| 480 |
if constexpr (IsNumber<typename M::block_type>()) |
| 481 |
{ |
| 482 |
for (; j.index()<i.index(); ++j) |
| 483 |
rhs -= (*j) * x[j.index()]; |
| 484 |
coliterator diag=j; |
| 485 |
for (; j!=endj; ++j) |
| 486 |
rhs -= (*j) * x[j.index()]; |
| 487 |
v = rhs / (*diag); |
| 488 |
x[i.index()] += w*v; |
| 489 |
} |
| 490 |
else |
| 491 |
{ |
| 492 |
for (; j.index()<i.index(); ++j) |
| 493 |
j->mmv(x[j.index()],rhs); |
| 494 |
coliterator diag=j; |
| 495 |
for (; j!=endj; ++j) |
| 496 |
j->mmv(x[j.index()],rhs); |
| 497 |
algmeta_itsteps<I-1,typename M::block_type>::bsorb(*diag,v,rhs,w); |
| 498 |
x[i.index()].axpy(w,v); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
template<class X, class Y, class K> |
| 504 |
static void dbjac (const M& A, X& x, const Y& b, const K& w) |
| 505 |
{ |
| 506 |
typedef typename M::ConstRowIterator rowiterator; |
| 507 |
typedef typename M::ConstColIterator coliterator; |
| 508 |
typedef typename Y::block_type bblock; |
| 509 |
bblock rhs; |
| 510 |
|
| 511 |
X v(x); // allocate with same size |
| 512 |
|
| 513 |
rowiterator endi=A.end(); |
| 514 |
for (rowiterator i=A.begin(); i!=endi; ++i) |
| 515 |
{ |
| 516 |
rhs = b[i.index()]; |
| 517 |
coliterator endj=(*i).end(); |
| 518 |
coliterator j=(*i).begin(); |
| 519 |
if constexpr (IsNumber<typename M::block_type>()) |
| 520 |
{ |
| 521 |
for (; j.index()<i.index(); ++j) |
| 522 |
rhs -= (*j) * x[j.index()]; |
| 523 |
coliterator diag=j; |
| 524 |
for (; j!=endj; ++j) |
| 525 |
rhs -= (*j) * x[j.index()]; |
| 526 |
v[i.index()] = rhs / (*diag); |
| 527 |
} |
| 528 |
else |
| 529 |
{ |
| 530 |
for (; j.index()<i.index(); ++j) |
| 531 |
j->mmv(x[j.index()],rhs); |
| 532 |
coliterator diag=j; |
| 533 |
for (; j!=endj; ++j) |
| 534 |
j->mmv(x[j.index()],rhs); |
| 535 |
algmeta_itsteps<I-1,typename M::block_type>::dbjac(*diag,v[i.index()],rhs,w); |
| 536 |
} |
| 537 |
} |
| 538 |
x.axpy(w,v); |
| 539 |
} |
| 540 |
}; |
| 541 |
// end of recursion |
| 542 |
template<typename M> |
| 543 |
struct algmeta_itsteps<0,M> { |
| 544 |
template<class X, class Y, class K> |
| 545 |
static void dbgs (const M& A, X& x, const Y& b, const K& /*w*/) |
| 546 |
{ |
| 547 |
A.solve(x,b); |
| 548 |
} |
| 549 |
template<class X, class Y, class K> |
| 550 |
static void bsorf (const M& A, X& x, const Y& b, const K& /*w*/) |
| 551 |
{ |
| 552 |
A.solve(x,b); |
| 553 |
} |
| 554 |
template<class X, class Y, class K> |
| 555 |
static void bsorb (const M& A, X& x, const Y& b, const K& /*w*/) |
| 556 |
{ |
| 557 |
A.solve(x,b); |
| 558 |
} |
| 559 |
template<class X, class Y, class K> |
| 560 |
static void dbjac (const M& A, X& x, const Y& b, const K& /*w*/) |
| 561 |
{ |
| 562 |
A.solve(x,b); |
| 563 |
} |
| 564 |
}; |
| 565 |
|
| 566 |
template<int I, typename T1, typename... MultiTypeMatrixArgs> |
| 567 |
struct algmeta_itsteps<I,MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>> { |
| 568 |
template< |
| 569 |
typename... MultiTypeVectorArgs, |
| 570 |
class K> |
| 571 |
static void dbgs (const MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>& A, |
| 572 |
MultiTypeBlockVector<MultiTypeVectorArgs...>& x, |
| 573 |
const MultiTypeBlockVector<MultiTypeVectorArgs...>& b, |
| 574 |
const K& w) |
| 575 |
{ |
| 576 |
static const int N = MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>::N(); |
| 577 |
Dune::MultiTypeBlockMatrix_Solver<I,0,N>::dbgs(A, x, b, w); |
| 578 |
} |
| 579 |
|
| 580 |
template< |
| 581 |
typename... MultiTypeVectorArgs, |
| 582 |
class K> |
| 583 |
static void bsorf (const MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>& A, |
| 584 |
MultiTypeBlockVector<MultiTypeVectorArgs...>& x, |
| 585 |
const MultiTypeBlockVector<MultiTypeVectorArgs...>& b, |
| 586 |
const K& w) |
| 587 |
{ |
| 588 |
static const int N = MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>::N(); |
| 589 |
Dune::MultiTypeBlockMatrix_Solver<I,0,N>::bsorf(A, x, b, w); |
| 590 |
} |
| 591 |
|
| 592 |
template< |
| 593 |
typename... MultiTypeVectorArgs, |
| 594 |
class K> |
| 595 |
static void bsorb (const MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>& A, |
| 596 |
MultiTypeBlockVector<MultiTypeVectorArgs...>& x, |
| 597 |
const MultiTypeBlockVector<MultiTypeVectorArgs...>& b, |
| 598 |
const K& w) |
| 599 |
{ |
| 600 |
static const int N = MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>::N(); |
| 601 |
Dune::MultiTypeBlockMatrix_Solver<I,N-1,N>::bsorb(A, x, b, w); |
| 602 |
} |
| 603 |
|
| 604 |
template< |
| 605 |
typename... MultiTypeVectorArgs, |
| 606 |
class K |
| 607 |
> |
| 608 |
static void dbjac (const MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>& A, |
| 609 |
MultiTypeBlockVector<MultiTypeVectorArgs...>& x, |
| 610 |
const MultiTypeBlockVector<MultiTypeVectorArgs...>& b, |
| 611 |
const K& w) |
| 612 |
{ |
| 613 |
static const int N = MultiTypeBlockMatrix<T1, MultiTypeMatrixArgs...>::N(); |
| 614 |
Dune::MultiTypeBlockMatrix_Solver<I,0,N>::dbjac(A, x, b, w); |
| 615 |
} |
| 616 |
}; |
| 617 |
|
| 618 |
// user calls |
| 619 |
|
| 620 |
//! GS step |
| 621 |
template<class M, class X, class Y, class K> |
| 622 |
void dbgs (const M& A, X& x, const Y& b, const K& w) |
| 623 |
{ |
| 624 |
algmeta_itsteps<1,M>::dbgs(A,x,b,w); |
| 625 |
} |
| 626 |
//! GS step |
| 627 |
template<class M, class X, class Y, class K, int l> |
| 628 |
void dbgs (const M& A, X& x, const Y& b, const K& w, BL<l> /*bl*/) |
| 629 |
{ |
| 630 |
algmeta_itsteps<l,M>::dbgs(A,x,b,w); |
| 631 |
} |
| 632 |
//! SOR step |
| 633 |
template<class M, class X, class Y, class K> |
| 634 |
void bsorf (const M& A, X& x, const Y& b, const K& w) |
| 635 |
{ |
| 636 |
algmeta_itsteps<1,M>::bsorf(A,x,b,w); |
| 637 |
} |
| 638 |
//! SOR step |
| 639 |
template<class M, class X, class Y, class K, int l> |
| 640 |
void bsorf (const M& A, X& x, const Y& b, const K& w, BL<l> /*bl*/) |
| 641 |
{ |
| 642 |
algmeta_itsteps<l,M>::bsorf(A,x,b,w); |
| 643 |
} |
| 644 |
//! SSOR step |
| 645 |
template<class M, class X, class Y, class K> |
| 646 |
void bsorb (const M& A, X& x, const Y& b, const K& w) |
| 647 |
{ |
| 648 |
algmeta_itsteps<1,M>::bsorb(A,x,b,w); |
| 649 |
} |
| 650 |
//! Backward SOR step |
| 651 |
template<class M, class X, class Y, class K, int l> |
| 652 |
void bsorb (const M& A, X& x, const Y& b, const K& w, BL<l> /*bl*/) |
| 653 |
{ |
| 654 |
algmeta_itsteps<l,typename std::remove_cv<M>::type>::bsorb(A,x,b,w); |
| 655 |
} |
| 656 |
//! Jacobi step |
| 657 |
template<class M, class X, class Y, class K> |
| 658 |
void dbjac (const M& A, X& x, const Y& b, const K& w) |
| 659 |
{ |
| 660 |
algmeta_itsteps<1,M>::dbjac(A,x,b,w); |
| 661 |
} |
| 662 |
//! Jacobi step |
| 663 |
template<class M, class X, class Y, class K, int l> |
| 664 |
void dbjac (const M& A, X& x, const Y& b, const K& w, BL<l> /*bl*/) |
| 665 |
{ |
| 666 |
algmeta_itsteps<l,M>::dbjac(A,x,b,w); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
/** @} end documentation */ |
| 671 |
|
| 672 |
} // end namespace |
| 673 |
|
| 674 |
#endif |