/usr/include/dune/istl/preconditioners.hh is in libdune-istl-dev 2.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_ISTL_PRECONDITIONERS_HH
#define DUNE_ISTL_PRECONDITIONERS_HH
#include <cmath>
#include <complex>
#include <iostream>
#include <iomanip>
#include <string>
#include <dune/common/unused.hh>
#include "preconditioner.hh"
#include "solver.hh"
#include "solvercategory.hh"
#include "istlexception.hh"
#include "matrixutils.hh"
#include "gsetc.hh"
#include "ilu.hh"
namespace Dune {
/** @defgroup ISTL_Prec Preconditioners
* @ingroup ISTL_Solvers
*
* All of our \ref ISTL_Solvers "Krylow solvers" are preconditioned versions.
* There are sequential preconditioners (e,g. SeqJacobi, SeqSOR, SeqSSOR) as well as parallel preconditioners
* (e.g. AMG, BlockPreconditioner) available for plugging them into the solvers
* together with matching ScalarProducts.
*
* Some of the available perconditioners (e.g. SeqJacobi, SeqSOR, SeqSSOR))
* may be given an aditional int as a template parameter, the block recursion level.
* These preconditioners
* can be used on blockrecursive matrices with an arbitrary hierarchy depths
* (eg. BCRSMatrix<BCRSMatrix<FieldMatrix,n,m> > >. Given a block recursion level
* \f$k\f$ those preconditioners work as
* normal on the offdiagonal blocks, treating them as traditional matrix
* entries. For the diagonal values a special procedure applies: If
* \f$k>1\f$ the diagonal is treated as a matrix itself and the preconditioner
* is applied recursively on the matrix representing the diagonal value
* \f$D=A_{ii}\f$ with block level \f$k-1\f$. For the case that \f$k=1\f$ the diagonal
* is treated as a
* matrix entry resulting in a linear solve or an identity operation
* depending on the algorithm.
*/
/** @addtogroup ISTL_Prec
@{
*/
/** \file
\brief Define general preconditioner interface
Wrap the methods implemented by ISTL in this interface.
However, the interface is extensible such that new preconditioners
can be implemented and used with the solvers.
*/
/**
* @brief Turns an InverseOperator into a Preconditioner.
* @tparam O The type of the inverse operator to wrap.
*/
template<class O, int c>
class InverseOperator2Preconditioner :
public Preconditioner<typename O::domain_type, typename O::range_type>
{
public:
//! \brief The domain type of the preconditioner.
typedef typename O::domain_type domain_type;
//! \brief The range type of the preconditioner.
typedef typename O::range_type range_type;
//! \brief The field type of the preconditioner.
typedef typename range_type::field_type field_type;
typedef O InverseOperator;
// define the category
enum {
//! \brief The category the preconditioner is part of.
category=c
};
/**
* @brief Construct the preconditioner from the solver
* @param inverse_operator The inverse operator to wrap.
*/
InverseOperator2Preconditioner(InverseOperator& inverse_operator)
: inverse_operator_(inverse_operator)
{}
void pre(domain_type&,range_type&)
{}
void apply(domain_type& v, const range_type& d)
{
InverseOperatorResult res;
range_type copy(d);
inverse_operator_.apply(v, copy, res);
}
void post(domain_type&)
{}
private:
InverseOperator& inverse_operator_;
};
//=====================================================================
// Implementation of this interface for sequential ISTL-preconditioners
//=====================================================================
/*!
\brief Sequential SSOR preconditioner.
Wraps the naked ISTL generic SSOR preconditioner into the
solver framework.
\tparam M The matrix type to operate on
\tparam X Type of the update
\tparam Y Type of the defect
\tparam l The block level to invert. Default is 1
*/
template<class M, class X, class Y, int l=1>
class SeqSSOR : public Preconditioner<X,Y> {
public:
//! \brief The matrix type the preconditioner is for.
typedef M matrix_type;
//! \brief The domain type of the preconditioner.
typedef X domain_type;
//! \brief The range type of the preconditioner.
typedef Y range_type;
//! \brief The field type of the preconditioner.
typedef typename X::field_type field_type;
// define the category
enum {
//! \brief The category the preconditioner is part of.
category=SolverCategory::sequential
};
/*! \brief Constructor.
constructor gets all parameters to operate the prec.
\param A The matrix to operate on.
\param n The number of iterations to perform.
\param w The relaxation factor.
*/
SeqSSOR (const M& A, int n, field_type w)
: _A_(A), _n(n), _w(w)
{
CheckIfDiagonalPresent<M,l>::check(_A_);
}
/*!
\brief Prepare the preconditioner.
\copydoc Preconditioner::pre(X&,Y&)
*/
virtual void pre (X& x, Y& b)
{
DUNE_UNUSED_PARAMETER(x);
DUNE_UNUSED_PARAMETER(b);
}
/*!
\brief Apply the preconditioner
\copydoc Preconditioner::apply(X&,const Y&)
*/
virtual void apply (X& v, const Y& d)
{
for (int i=0; i<_n; i++) {
bsorf(_A_,v,d,_w,BL<l>());
bsorb(_A_,v,d,_w,BL<l>());
}
}
/*!
\brief Clean up.
\copydoc Preconditioner::post(X&)
*/
virtual void post (X& x)
{
DUNE_UNUSED_PARAMETER(x);
}
private:
//! \brief The matrix we operate on.
const M& _A_;
//! \brief The number of steps to do in apply
int _n;
//! \brief The relaxation factor to use
field_type _w;
};
/*!
\brief Sequential SOR preconditioner.
Wraps the naked ISTL generic SOR preconditioner into the
solver framework.
\tparam M The matrix type to operate on
\tparam X Type of the update
\tparam Y Type of the defect
\tparam l The block level to invert. Default is 1
*/
template<class M, class X, class Y, int l=1>
class SeqSOR : public Preconditioner<X,Y> {
public:
//! \brief The matrix type the preconditioner is for.
typedef M matrix_type;
//! \brief The domain type of the preconditioner.
typedef X domain_type;
//! \brief The range type of the preconditioner.
typedef Y range_type;
//! \brief The field type of the preconditioner.
typedef typename X::field_type field_type;
// define the category
enum {
//! \brief The category the preconditioner is part of.
category=SolverCategory::sequential
};
/*! \brief Constructor.
constructor gets all parameters to operate the prec.
\param A The matrix to operate on.
\param n The number of iterations to perform.
\param w The relaxation factor.
*/
SeqSOR (const M& A, int n, field_type w)
: _A_(A), _n(n), _w(w)
{
CheckIfDiagonalPresent<M,l>::check(_A_);
}
/*!
\brief Prepare the preconditioner.
\copydoc Preconditioner::pre(X&,Y&)
*/
virtual void pre (X& x, Y& b)
{
DUNE_UNUSED_PARAMETER(x);
DUNE_UNUSED_PARAMETER(b);
}
/*!
\brief Apply the preconditioner.
\copydoc Preconditioner::apply(X&,const Y&)
*/
virtual void apply (X& v, const Y& d)
{
this->template apply<true>(v,d);
}
/*!
\brief Apply the preconditioner in a special direction.
The template parameter forward indications the direction
the smoother is applied. If true The application is
started at the lowest index in the vector v, if false at
the highest index of vector v.
*/
template<bool forward>
void apply(X& v, const Y& d)
{
if(forward)
for (int i=0; i<_n; i++) {
bsorf(_A_,v,d,_w,BL<l>());
}
else
for (int i=0; i<_n; i++) {
bsorb(_A_,v,d,_w,BL<l>());
}
}
/*!
\brief Clean up.
\copydoc Preconditioner::post(X&)
*/
virtual void post (X& x)
{
DUNE_UNUSED_PARAMETER(x);
}
private:
//! \brief the matrix we operate on.
const M& _A_;
//! \brief The number of steps to perform in apply.
int _n;
//! \brief The relaxation factor to use.
field_type _w;
};
/*! \brief Sequential Gauss Seidel preconditioner
Wraps the naked ISTL generic block Gauss-Seidel preconditioner into the
solver framework.
\tparam M The matrix type to operate on
\tparam X Type of the update
\tparam Y Type of the defect
\tparam l The block level to invert. Default is 1
*/
template<class M, class X, class Y, int l=1>
class SeqGS : public Preconditioner<X,Y> {
public:
//! \brief The matrix type the preconditioner is for.
typedef M matrix_type;
//! \brief The domain type of the preconditioner.
typedef X domain_type;
//! \brief The range type of the preconditioner.
typedef Y range_type;
//! \brief The field type of the preconditioner.
typedef typename X::field_type field_type;
// define the category
enum {
//! \brief The category the preconditioner is part of.
category=SolverCategory::sequential
};
/*! \brief Constructor.
Constructor gets all parameters to operate the prec.
\param A The matrix to operate on.
\param n The number of iterations to perform.
\param w The relaxation factor.
*/
SeqGS (const M& A, int n, field_type w)
: _A_(A), _n(n), _w(w)
{
CheckIfDiagonalPresent<M,l>::check(_A_);
}
/*!
\brief Prepare the preconditioner.
\copydoc Preconditioner::pre(X&,Y&)
*/
virtual void pre (X& x, Y& b)
{
DUNE_UNUSED_PARAMETER(x);
DUNE_UNUSED_PARAMETER(b);
}
/*!
\brief Apply the preconditioner.
\copydoc Preconditioner::apply(X&,const Y&)
*/
virtual void apply (X& v, const Y& d)
{
for (int i=0; i<_n; i++) {
dbgs(_A_,v,d,_w,BL<l>());
}
}
/*!
\brief Clean up.
\copydoc Preconditioner::post(X&)
*/
virtual void post (X& x)
{
DUNE_UNUSED_PARAMETER(x);
}
private:
//! \brief The matrix we operate on.
const M& _A_;
//! \brief The number of iterations to perform in apply.
int _n;
//! \brief The relaxation factor to use.
field_type _w;
};
/*! \brief The sequential jacobian preconditioner.
Wraps the naked ISTL generic block Jacobi preconditioner into the
solver framework.
\tparam M The matrix type to operate on
\tparam X Type of the update
\tparam Y Type of the defect
\tparam l The block level to invert. Default is 1
*/
template<class M, class X, class Y, int l=1>
class SeqJac : public Preconditioner<X,Y> {
public:
//! \brief The matrix type the preconditioner is for.
typedef M matrix_type;
//! \brief The domain type of the preconditioner.
typedef X domain_type;
//! \brief The range type of the preconditioner.
typedef Y range_type;
//! \brief The field type of the preconditioner.
typedef typename X::field_type field_type;
// define the category
enum {
//! \brief The category the preconditioner is part of
category=SolverCategory::sequential
};
/*! \brief Constructor.
Constructor gets all parameters to operate the prec.
\param A The matrix to operate on.
\param n The number of iterations to perform.
\param w The relaxation factor.
*/
SeqJac (const M& A, int n, field_type w)
: _A_(A), _n(n), _w(w)
{
CheckIfDiagonalPresent<M,l>::check(_A_);
}
/*!
\brief Prepare the preconditioner.
\copydoc Preconditioner::pre(X&,Y&)
*/
virtual void pre (X& x, Y& b)
{
DUNE_UNUSED_PARAMETER(x);
DUNE_UNUSED_PARAMETER(b);
}
/*!
\brief Apply the preconditioner.
\copydoc Preconditioner::apply(X&,const Y&)
*/
virtual void apply (X& v, const Y& d)
{
for (int i=0; i<_n; i++) {
dbjac(_A_,v,d,_w,BL<l>());
}
}
/*!
\brief Clean up.
\copydoc Preconditioner::post(X&)
*/
virtual void post (X& x)
{
DUNE_UNUSED_PARAMETER(x);
}
private:
//! \brief The matrix we operate on.
const M& _A_;
//! \brief The number of steps to perform during apply.
int _n;
//! \brief The relaxation parameter to use.
field_type _w;
};
/*!
\brief Sequential ILU0 preconditioner.
Wraps the naked ISTL generic ILU0 preconditioner into the solver framework.
\tparam M The matrix type to operate on
\tparam X Type of the update
\tparam Y Type of the defect
\tparam l Ignored. Just there to have the same number of template arguments
as other preconditioners.
*/
template<class M, class X, class Y, int l=1>
class SeqILU0 : public Preconditioner<X,Y> {
public:
//! \brief The matrix type the preconditioner is for.
typedef typename Dune::remove_const<M>::type matrix_type;
//! \brief The domain type of the preconditioner.
typedef X domain_type;
//! \brief The range type of the preconditioner.
typedef Y range_type;
//! \brief The field type of the preconditioner.
typedef typename X::field_type field_type;
// define the category
enum {
//! \brief The category the preconditioner is part of.
category=SolverCategory::sequential
};
/*! \brief Constructor.
Constructor gets all parameters to operate the prec.
\param A The matrix to operate on.
\param w The relaxation factor.
*/
SeqILU0 (const M& A, field_type w)
: ILU(A) // copy A
{
_w =w;
bilu0_decomposition(ILU);
}
/*!
\brief Prepare the preconditioner.
\copydoc Preconditioner::pre(X&,Y&)
*/
virtual void pre (X& x, Y& b)
{
DUNE_UNUSED_PARAMETER(x);
DUNE_UNUSED_PARAMETER(b);
}
/*!
\brief Apply the preconditoner.
\copydoc Preconditioner::apply(X&,const Y&)
*/
virtual void apply (X& v, const Y& d)
{
bilu_backsolve(ILU,v,d);
v *= _w;
}
/*!
\brief Clean up.
\copydoc Preconditioner::post(X&)
*/
virtual void post (X& x)
{
DUNE_UNUSED_PARAMETER(x);
}
private:
//! \brief The relaxation factor to use.
field_type _w;
//! \brief The ILU0 decomposition of the matrix.
matrix_type ILU;
};
/*!
\brief Sequential ILU(n) preconditioner.
Wraps the naked ISTL generic ILU(n) preconditioner into the
solver framework.
\tparam M The matrix type to operate on
\tparam X Type of the update
\tparam Y Type of the defect
\tparam l Ignored. Just there to have the same number of template arguments
as other preconditioners.
*/
template<class M, class X, class Y, int l=1>
class SeqILUn : public Preconditioner<X,Y> {
public:
//! \brief The matrix type the preconditioner is for.
typedef typename Dune::remove_const<M>::type matrix_type;
//! \brief The domain type of the preconditioner.
typedef X domain_type;
//! \brief The range type of the preconditioner.
typedef Y range_type;
//! \brief The field type of the preconditioner.
typedef typename X::field_type field_type;
// define the category
enum {
//! \brief The category the preconditioner is part of.
category=SolverCategory::sequential
};
/*! \brief Constructor.
Constructor gets all parameters to operate the prec.
\param A The matrix to operate on.
\param n The number of iterations to perform.
\param w The relaxation factor.
*/
SeqILUn (const M& A, int n, field_type w)
: ILU(A.N(),A.M(),M::row_wise)
{
_n = n;
_w = w;
bilu_decomposition(A,n,ILU);
}
/*!
\brief Prepare the preconditioner.
\copydoc Preconditioner::pre(X&,Y&)
*/
virtual void pre (X& x, Y& b)
{
DUNE_UNUSED_PARAMETER(x);
DUNE_UNUSED_PARAMETER(b);
}
/*!
\brief Apply the precondioner.
\copydoc Preconditioner::apply(X&,const Y&)
*/
virtual void apply (X& v, const Y& d)
{
bilu_backsolve(ILU,v,d);
v *= _w;
}
/*!
\brief Clean up.
\copydoc Preconditioner::post(X&)
*/
virtual void post (X& x)
{
DUNE_UNUSED_PARAMETER(x);
}
private:
//! \brief ILU(n) decomposition of the matrix we operate on.
matrix_type ILU;
//! \brief The number of steps to perform in apply.
int _n;
//! \brief The relaxation factor to use.
field_type _w;
};
/*!
\brief Richardson preconditioner.
Multiply simply by a constant.
\tparam X Type of the update
\tparam Y Type of the defect
*/
template<class X, class Y>
class Richardson : public Preconditioner<X,Y> {
public:
//! \brief The domain type of the preconditioner.
typedef X domain_type;
//! \brief The range type of the preconditioner.
typedef Y range_type;
//! \brief The field type of the preconditioner.
typedef typename X::field_type field_type;
// define the category
enum {
//! \brief The category the preconditioner is part of.
category=SolverCategory::sequential
};
/*! \brief Constructor.
Constructor gets all parameters to operate the prec.
\param w The relaxation factor.
*/
Richardson (field_type w=1.0)
{
_w = w;
}
/*!
\brief Prepare the preconditioner.
\copydoc Preconditioner::pre(X&,Y&)
*/
virtual void pre (X& x, Y& b)
{
DUNE_UNUSED_PARAMETER(x);
DUNE_UNUSED_PARAMETER(b);
}
/*!
\brief Apply the precondioner.
\copydoc Preconditioner::apply(X&,const Y&)
*/
virtual void apply (X& v, const Y& d)
{
v = d;
v *= _w;
}
/*!
\brief Clean up.
\copydoc Preconditioner::post(X&)
*/
virtual void post (X& x)
{
DUNE_UNUSED_PARAMETER(x);
}
private:
//! \brief The relaxation factor to use.
field_type _w;
};
/** @} end documentation */
} // end namespace
#endif
|