/usr/include/dune/pdelab/backend/novlpistlsolverbackend.hh is in libdune-pdelab-dev 2.0.0-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 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 | // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=8 sw=2 sts=2:
#ifndef DUNE_NOVLPISTLSOLVERBACKEND_HH
#define DUNE_NOVLPISTLSOLVERBACKEND_HH
#include <cstddef>
#include <dune/common/deprecated.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/grid/common/gridenums.hh>
#include <dune/istl/io.hh>
#include <dune/istl/operators.hh>
#include <dune/istl/owneroverlapcopy.hh>
#include <dune/istl/paamg/amg.hh>
#include <dune/istl/paamg/pinfo.hh>
#include <dune/istl/preconditioners.hh>
#include <dune/istl/scalarproducts.hh>
#include <dune/istl/solvercategory.hh>
#include <dune/istl/solvers.hh>
#include <dune/istl/superlu.hh>
#include <dune/pdelab/constraints/common/constraints.hh>
#include <dune/pdelab/gridfunctionspace/genericdatahandle.hh>
#include <dune/pdelab/newton/newton.hh>
#include <dune/pdelab/backend/istlvectorbackend.hh>
#include <dune/pdelab/backend/istlmatrixbackend.hh>
#include <dune/pdelab/backend/istl/blockmatrixdiagonal.hh>
#include <dune/pdelab/backend/istl/parallelhelper.hh>
#include <dune/pdelab/backend/seqistlsolverbackend.hh>
namespace Dune {
namespace PDELab {
//! \addtogroup Backend
//! \ingroup PDELab
//! \{
//========================================================
// Generic support for nonoverlapping grids
//========================================================
//! Operator for the non-overlapping parallel case
/**
* Calculate \f$y:=Ax\f$.
*
* \tparam GFS The GridFunctionSpace the vectors apply to.
* \tparam M Type of the matrix. Should be one of the ISTL matrix types.
* \tparam X Type of the vectors the matrix is applied to.
* \tparam Y Type of the result vectors.
*/
template<typename GFS, typename M, typename X, typename Y>
class NonoverlappingOperator
: public Dune::AssembledLinearOperator<M,X,Y>
{
public:
//! export type of matrix
typedef typename M::BaseT matrix_type;
//! export type of vectors the matrix is applied to
typedef typename X::BaseT domain_type;
//! export type of result vectors
typedef typename Y::BaseT range_type;
//! export type of the entries for x
typedef typename X::field_type field_type;
//redefine the category, that is the only difference
enum {category=Dune::SolverCategory::nonoverlapping};
//! Construct a non-overlapping operator
/**
* \param gfs_ GridFunctionsSpace for the vectors.
* \param A Matrix for this operator. This should be the locally
* assembled matrix.
*
* \note The constructed object stores references to all the objects
* given as parameters here. They should be valid for as long as
* the constructed object is used. They are not needed to
* destruct the constructed object.
*/
NonoverlappingOperator (const GFS& gfs_, const M& A)
: gfs(gfs_), _A_(A)
{ }
//! apply operator
/**
* Compute \f$y:=A(x)\f$ on this process, then make y consistent (sum up
* corresponding entries of y on the different processes and store the
* result back in y on each process).
*/
virtual void apply (const X& x, Y& y) const
{
// apply local operator; now we have sum y_p = sequential y
istl::raw(_A_).mv(istl::raw(x),istl::raw(y));
// accumulate y on border
Dune::PDELab::AddDataHandle<GFS,Y> adddh(gfs,y);
if (gfs.gridView().comm().size()>1)
gfs.gridView().communicate(adddh,Dune::InteriorBorder_InteriorBorder_Interface,Dune::ForwardCommunication);
}
//! apply operator to x, scale and add: \f$ y = y + \alpha A(x) \f$
/**
* Compute \f$y:=\alpha A(x)\f$ on this process, then make y consistent
* (sum up corresponding entries of y on the different processes and
* store the result back in y on each process).
*/
virtual void applyscaleadd (field_type alpha, const X& x, Y& y) const
{
// apply local operator; now we have sum y_p = sequential y
istl::raw(_A_).usmv(alpha,istl::raw(x),istl::raw(y));
// accumulate y on border
Dune::PDELab::AddDataHandle<GFS,Y> adddh(gfs,y);
if (gfs.gridView().comm().size()>1)
gfs.gridView().communicate(adddh,Dune::InteriorBorder_InteriorBorder_Interface,Dune::ForwardCommunication);
}
//! extract the matrix
virtual const M& getmat () const
{
return _A_;
}
private:
const GFS& gfs;
const M& _A_;
};
// parallel scalar product assuming no overlap
template<class GFS, class X>
class NonoverlappingScalarProduct : public Dune::ScalarProduct<X>
{
public:
//! export types
typedef X domain_type;
typedef typename X::ElementType field_type;
//! define the category
enum {category=Dune::SolverCategory::nonoverlapping};
/*! \brief Constructor needs to know the grid function space
*/
NonoverlappingScalarProduct (const GFS& gfs_, const istl::ParallelHelper<GFS>& helper_)
: gfs(gfs_), helper(helper_)
{}
/*! \brief Dot product of two vectors.
It is assumed that the vectors are consistent on the interior+border
partition.
*/
virtual field_type dot (const X& x, const X& y)
{
// do local scalar product on unique partition
field_type sum = helper.disjointDot(x,y);
// do global communication
return gfs.gridView().comm().sum(sum);
}
/*! \brief Norm of a right-hand side vector.
The vector must be consistent on the interior+border partition
*/
virtual double norm (const X& x)
{
return sqrt(static_cast<double>(this->dot(x,x)));
}
/*! \brief make additive vector consistent
*/
void make_consistent (X& x) const
{
Dune::PDELab::AddDataHandle<GFS,X> adddh(gfs,x);
if (gfs.gridView().comm().size()>1)
gfs.gridView().communicate(adddh,Dune::InteriorBorder_InteriorBorder_Interface,Dune::ForwardCommunication);
}
private:
const GFS& gfs;
const istl::ParallelHelper<GFS>& helper;
};
// parallel Richardson preconditioner
template<class GFS, class X, class Y>
class NonoverlappingRichardson : public Dune::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::ElementType field_type;
// define the category
enum {
//! \brief The category the preconditioner is part of.
category=Dune::SolverCategory::nonoverlapping
};
//! \brief Constructor.
NonoverlappingRichardson (const GFS& gfs_, const istl::ParallelHelper<GFS>& helper_)
: gfs(gfs_), helper(helper_)
{
}
/*!
\brief Prepare the preconditioner.
*/
virtual void pre (X& x, Y& b) {}
/*!
\brief Apply the precondioner.
*/
virtual void apply (X& v, const Y& d)
{
v = d;
}
/*!
\brief Clean up.
*/
virtual void post (X& x) {}
private:
const GFS& gfs;
const istl::ParallelHelper<GFS>& helper;
};
//! parallel non-overlapping Jacobi preconditioner
/**
* \tparam Diagonal Vector type used to store the diagonal of the matrix
* \tparam X Vector type used to store the result of applying the
* preconditioner.
* \tparam Y Vector type used to store the defect.
*
* The Jacobi preconditioner approximates the inverse of a matrix M by
* taking the diagonal diag(M) and inverting that. In the parallel case
* the matrix M is assumed to be inconsistent, so diagonal entries for
* dofs on the border are summed up over all relevant processes by this
* precoditioner before the inverse is computed.
*/
template<typename A, typename X, typename Y>
class NonoverlappingJacobi
: public Dune::Preconditioner<X,Y>
{
typedef typename istl::BlockMatrixDiagonal<A>::MatrixElementVector Diagonal;
Diagonal _inverse_diagonal;
public:
//! The domain type of the operator.
/**
* The preconditioner is an inverse operator, so this is the output type
* of the preconditioner.
*/
typedef X domain_type;
//! \brief The range type of the operator.
/**
* The preconditioner is an inverse operator, so this is the input type
* of the preconditioner.
*/
typedef Y range_type;
//! \brief The field type of the preconditioner.
typedef typename X::ElementType field_type;
enum {
//! \brief The category the preconditioner is part of.
category=Dune::SolverCategory::nonoverlapping
};
//! \brief Constructor.
/**
* \param gfs The GridFunctionSpace the matrix and the vectors live on.
* \param m The matrix whose inverse the preconditioner should
* estimate. m is assumed to be inconsistent (i.e. rows for
* dofs on the border only contain the contribution of the
* local process).
*
* The preconditioner does not store any reference to the gfs or the
* matrix m. The diagonal of m is copied, since it has to be made
* consistent.
*/
template<typename GFS>
NonoverlappingJacobi(const GFS& gfs, const A &m)
: _inverse_diagonal(m)
{
// make the diagonal consistent...
typename istl::BlockMatrixDiagonal<A>::template AddMatrixElementVectorDataHandle<GFS> addDH(gfs, _inverse_diagonal);
gfs.gridView().communicate(addDH,
InteriorBorder_InteriorBorder_Interface,
ForwardCommunication);
// ... and then invert it
_inverse_diagonal.invert();
}
//! Prepare the preconditioner.
virtual void pre (X& x, Y& b) {}
//! Apply the precondioner.
/*
* For this preconditioner, this method works with both consistent and
* inconsistent vectors: if d is consistent, v will be consistent, if d
* is inconsistent, v will be inconsistent.
*/
virtual void apply (X& v, const Y& d)
{
_inverse_diagonal.mv(d,v);
}
//! Clean up.
virtual void post (X& x) {}
};
//! \addtogroup PDELab_novlpsolvers Nonoverlapping Solvers
//! \{
//! \brief Nonoverlapping parallel CG solver without preconditioner
template<class GFS>
class ISTLBackend_NOVLP_CG_NOPREC
{
typedef istl::ParallelHelper<GFS> PHELPER;
public:
/*! \brief make a linear solver object
\param[in] gfs_ a grid function space
\param[in] maxiter_ maximum number of iterations to do
\param[in] verbose_ print messages if true
*/
explicit ISTLBackend_NOVLP_CG_NOPREC (const GFS& gfs_,
unsigned maxiter_=5000,
int verbose_=1)
: gfs(gfs_), phelper(gfs,verbose_), maxiter(maxiter_), verbose(verbose_)
{}
/*! \brief compute global norm of a vector
\param[in] v the given vector
*/
template<class V>
typename V::ElementType norm (const V& v) const
{
V x(v); // make a copy because it has to be made consistent
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
psp.make_consistent(x);
return psp.norm(x);
}
/*! \brief solve the given linear system
\param[in] A the given matrix
\param[out] z the solution vector to be computed
\param[in] r right hand side
\param[in] reduction to be achieved
*/
template<class M, class V, class W>
void apply(M& A, V& z, W& r, typename V::ElementType reduction)
{
typedef Dune::PDELab::NonoverlappingOperator<GFS,M,V,W> POP;
POP pop(gfs,A);
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
typedef Dune::PDELab::NonoverlappingRichardson<GFS,V,W> PRICH;
PRICH prich(gfs,phelper);
int verb=0;
if (gfs.gridView().comm().rank()==0) verb=verbose;
Dune::CGSolver<V> solver(pop,psp,prich,reduction,maxiter,verb);
Dune::InverseOperatorResult stat;
solver.apply(z,r,stat);
res.converged = stat.converged;
res.iterations = stat.iterations;
res.elapsed = stat.elapsed;
res.reduction = stat.reduction;
res.conv_rate = stat.conv_rate;
}
/*! \brief Return access to result data */
const Dune::PDELab::LinearSolverResult<double>& result() const
{
return res;
}
private:
const GFS& gfs;
PHELPER phelper;
Dune::PDELab::LinearSolverResult<double> res;
unsigned maxiter;
int verbose;
};
//! \brief Nonoverlapping parallel CG solver with Jacobi preconditioner
template<class GFS>
class ISTLBackend_NOVLP_CG_Jacobi
{
typedef istl::ParallelHelper<GFS> PHELPER;
const GFS& gfs;
PHELPER phelper;
LinearSolverResult<double> res;
unsigned maxiter;
int verbose;
public:
//! make a linear solver object
/**
* \param gfs_ A grid function space
* \param maxiter_ Maximum number of iterations to do.
* \param verbose_ Verbosity level, directly handed to the CGSolver.
*/
explicit ISTLBackend_NOVLP_CG_Jacobi(const GFS& gfs_,
unsigned maxiter_ = 5000,
int verbose_ = 1) :
gfs(gfs_), phelper(gfs,verbose_), maxiter(maxiter_), verbose(verbose_)
{}
//! compute global norm of a vector
/**
* \param v The vector to compute the norm of. Should be an
* inconsistent vector (i.e. the entries corresponding a DoF on
* the border should only contain the summand of this process).
*/
template<class V>
typename V::ElementType norm (const V& v) const
{
V x(v); // make a copy because it has to be made consistent
typedef NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
psp.make_consistent(x);
return psp.norm(x);
}
//! solve the given linear system
/**
* \param A The matrix to solve. Should be a matrix from one of
* PDELabs ISTL backends (only ISTLBCRSMatrixBackend at
* the moment).
* \param z The solution vector to be computed
* \param r Right hand side
* \param reduction to be achieved
*
* Solve the linear system A*z=r such that
* norm(A*z0-r)/norm(A*z-r) < reduction where z0 is the initial value of
* z.
*/
template<class M, class V, class W>
void apply(M& A, V& z, W& r, typename V::ElementType reduction)
{
typedef NonoverlappingOperator<GFS,M,V,W> POP;
POP pop(gfs,A);
typedef NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
typedef NonoverlappingJacobi<M,V,W> PPre;
PPre ppre(gfs,istl::raw(A));
int verb=0;
if (gfs.gridView().comm().rank()==0) verb=verbose;
CGSolver<V> solver(pop,psp,ppre,reduction,maxiter,verb);
InverseOperatorResult stat;
solver.apply(z,r,stat);
res.converged = stat.converged;
res.iterations = stat.iterations;
res.elapsed = stat.elapsed;
res.reduction = stat.reduction;
res.conv_rate = stat.conv_rate;
}
//! Return access to result data
const LinearSolverResult<double>& result() const
{ return res; }
};
//! \brief Nonoverlapping parallel BiCGStab solver without preconditioner
template<class GFS>
class ISTLBackend_NOVLP_BCGS_NOPREC
{
typedef istl::ParallelHelper<GFS> PHELPER;
public:
/*! \brief make a linear solver object
\param[in] gfs_ a grid function space
\param[in] maxiter_ maximum number of iterations to do
\param[in] verbose_ print messages if true
*/
explicit ISTLBackend_NOVLP_BCGS_NOPREC (const GFS& gfs_, unsigned maxiter_=5000, int verbose_=1)
: gfs(gfs_), phelper(gfs,verbose_), maxiter(maxiter_), verbose(verbose_)
{}
/*! \brief compute global norm of a vector
\param[in] v the given vector
*/
template<class V>
typename V::ElementType norm (const V& v) const
{
V x(v); // make a copy because it has to be made consistent
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
psp.make_consistent(x);
return psp.norm(x);
}
/*! \brief solve the given linear system
\param[in] A the given matrix
\param[out] z the solution vector to be computed
\param[in] r right hand side
\param[in] reduction to be achieved
*/
template<class M, class V, class W>
void apply(M& A, V& z, W& r, typename V::ElementType reduction)
{
typedef Dune::PDELab::NonoverlappingOperator<GFS,M,V,W> POP;
POP pop(gfs,A);
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
typedef Dune::PDELab::NonoverlappingRichardson<GFS,V,W> PRICH;
PRICH prich(gfs,phelper);
int verb=0;
if (gfs.gridView().comm().rank()==0) verb=verbose;
Dune::BiCGSTABSolver<V> solver(pop,psp,prich,reduction,maxiter,verb);
Dune::InverseOperatorResult stat;
solver.apply(z,r,stat);
res.converged = stat.converged;
res.iterations = stat.iterations;
res.elapsed = stat.elapsed;
res.reduction = stat.reduction;
res.conv_rate = stat.conv_rate;
}
/*! \brief Return access to result data */
const Dune::PDELab::LinearSolverResult<double>& result() const
{
return res;
}
private:
const GFS& gfs;
PHELPER phelper;
Dune::PDELab::LinearSolverResult<double> res;
unsigned maxiter;
int verbose;
};
//! \brief Nonoverlapping parallel BiCGStab solver with Jacobi preconditioner
template<class GFS>
class ISTLBackend_NOVLP_BCGS_Jacobi
{
typedef istl::ParallelHelper<GFS> PHELPER;
public:
/*! \brief make a linear solver object
\param[in] gfs_ a grid function space
\param[in] maxiter_ maximum number of iterations to do
\param[in] verbose_ print messages if true
*/
explicit ISTLBackend_NOVLP_BCGS_Jacobi (const GFS& gfs_, unsigned maxiter_=5000, int verbose_=1)
: gfs(gfs_), phelper(gfs,verbose_), maxiter(maxiter_), verbose(verbose_)
{}
/*! \brief compute global norm of a vector
\param[in] v the given vector
*/
template<class V>
typename V::ElementType norm (const V& v) const
{
V x(v); // make a copy because it has to be made consistent
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
psp.make_consistent(x);
return psp.norm(x);
}
/*! \brief solve the given linear system
\param[in] A the given matrix
\param[out] z the solution vector to be computed
\param[in] r right hand side
\param[in] reduction to be achieved
*/
template<class M, class V, class W>
void apply(M& A, V& z, W& r, typename V::ElementType reduction)
{
typedef Dune::PDELab::NonoverlappingOperator<GFS,M,V,W> POP;
POP pop(gfs,A);
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
typedef NonoverlappingJacobi<M,V,W> PPre;
PPre ppre(gfs,A);
int verb=0;
if (gfs.gridView().comm().rank()==0) verb=verbose;
Dune::BiCGSTABSolver<V> solver(pop,psp,ppre,reduction,maxiter,verb);
Dune::InverseOperatorResult stat;
solver.apply(z,r,stat);
res.converged = stat.converged;
res.iterations = stat.iterations;
res.elapsed = stat.elapsed;
res.reduction = stat.reduction;
res.conv_rate = stat.conv_rate;
}
/*! \brief Return access to result data */
const Dune::PDELab::LinearSolverResult<double>& result() const
{
return res;
}
private:
const GFS& gfs;
PHELPER phelper;
Dune::PDELab::LinearSolverResult<double> res;
unsigned maxiter;
int verbose;
};
//! Solver to be used for explicit time-steppers with (block-)diagonal mass matrix
template<typename GFS>
class ISTLBackend_NOVLP_ExplicitDiagonal
{
typedef istl::ParallelHelper<GFS> PHELPER;
const GFS& gfs;
PHELPER phelper;
Dune::PDELab::LinearSolverResult<double> res;
public:
/*! \brief make a linear solver object
\param[in] gfs_ GridFunctionSpace, used to identify DoFs for parallel
communication
*/
explicit ISTLBackend_NOVLP_ExplicitDiagonal(const GFS& gfs_)
: gfs(gfs_), phelper(gfs)
{}
/*! \brief compute global norm of a vector
\param[in] v the given vector
*/
template<class V>
typename V::ElementType norm (const V& v) const
{
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,V> PSP;
V x(v); // make a copy because it has to be made consistent
PSP psp(gfs,phelper);
psp.make_consistent(x);
return psp.norm(x);
}
/*! \brief solve the given linear system
\param[in] A the given matrix
\param[out] z the solution vector to be computed
\param[in] r right hand side
\param[in] reduction to be achieved
*/
template<class M, class V, class W>
void apply(M& A, V& z, W& r, typename W::ElementType reduction)
{
Dune::SeqJac<M,V,W> jac(A,1,1.0);
jac.pre(z,r);
jac.apply(z,r);
jac.post(z);
if (gfs.gridView().comm().size()>1)
{
Dune::PDELab::AddDataHandle<GFS,V> adddh(gfs,z);
gfs.gridView().communicate(adddh,Dune::InteriorBorder_InteriorBorder_Interface,Dune::ForwardCommunication);
}
res.converged = true;
res.iterations = 1;
res.elapsed = 0.0;
res.reduction = reduction;
res.conv_rate = reduction; // pow(reduction,1.0/1)
}
/*! \brief Return access to result data */
const Dune::PDELab::LinearSolverResult<double>& result() const
{
return res;
}
};
//! \} Nonoverlapping Solvers
template<class GO,
template<class,class,class,int> class Preconditioner,
template<class> class Solver>
class ISTLBackend_NOVLP_BASE_PREC
{
typedef typename GO::Traits::TrialGridFunctionSpace GFS;
typedef istl::ParallelHelper<GFS> PHELPER;
public:
/*! \brief Constructor.
\param[in] gfs_ a grid function space
\param[in] maxiter_ maximum number of iterations to do
\param[in] steps_ number of preconditioner steps to apply as inner iteration
\param[in] verbose_ print messages if true
*/
explicit ISTLBackend_NOVLP_BASE_PREC (const GO& grid_operator, unsigned maxiter_ = 5000, unsigned steps_ = 5, int verbose_ = 1)
: _grid_operator(grid_operator)
, gfs(grid_operator.trialGridFunctionSpace())
, phelper(gfs,verbose_)
, maxiter(maxiter_)
, steps(steps_)
, verbose(verbose_)
{}
/*! \brief Compute global norm of a vector.
\param[in] v the given vector
*/
template<class Vector>
typename Vector::ElementType norm (const Vector& v) const
{
Vector x(v); // make a copy because it has to be made consistent
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,Vector> PSP;
PSP psp(gfs,phelper);
psp.make_consistent(x);
return psp.norm(x);
}
/*! \brief Solve the given linear system.
\param[in] A the given matrix
\param[out] z the solution vector to be computed
\param[in] r right hand side
\param[in] reduction to be achieved
*/
template<class M, class V, class W>
void apply(M& A, V& z, W& r, typename V::ElementType reduction)
{
typedef typename M::BaseT MatrixType;
MatrixType& mat=istl::raw(A);
typedef typename V::BaseT VectorType;
#if HAVE_MPI
typedef typename istl::CommSelector<96,Dune::MPIHelper::isFake>::type Comm;
_grid_operator.make_consistent(A);
Comm oocc(gfs.gridView().comm(),Dune::SolverCategory::nonoverlapping);
phelper.createIndexSetAndProjectForAMG(mat, oocc);
typedef Preconditioner<MatrixType,VectorType,VectorType,1> Smoother;
Smoother smoother(mat, steps, 1.0);
typedef Dune::NonoverlappingSchwarzScalarProduct<VectorType,Comm> PSP;
PSP psp(oocc);
typedef Dune::NonoverlappingSchwarzOperator<MatrixType,VectorType,VectorType,Comm> Operator;
Operator oop(mat,oocc);
typedef Dune::NonoverlappingBlockPreconditioner<Comm, Smoother> ParSmoother;
ParSmoother parsmoother(smoother, oocc);
#else
typedef Preconditioner<MatrixType,VectorType,VectorType,1> ParSmoother;
ParSmoother parsmoother(mat, steps, 1.0);
typedef Dune::SeqScalarProduct<VectorType> PSP;
PSP psp;
typedef Dune::MatrixAdapter<MatrixType,VectorType,VectorType> Operator;
Operator oop(mat);
#endif
int verb=0;
if (gfs.gridView().comm().rank()==0) verb=verbose;
Solver<VectorType> solver(oop,psp,parsmoother,reduction,maxiter,verb);
Dune::InverseOperatorResult stat;
//make r consistent
if (gfs.gridView().comm().size()>1){
Dune::PDELab::AddDataHandle<GFS,V> adddh(gfs,r);
gfs.gridView().communicate(adddh,
Dune::InteriorBorder_InteriorBorder_Interface,
Dune::ForwardCommunication);
}
solver.apply(z,r,stat);
res.converged = stat.converged;
res.iterations = stat.iterations;
res.elapsed = stat.elapsed;
res.reduction = stat.reduction;
res.conv_rate = stat.conv_rate;
}
/*! \brief Return access to result data. */
const Dune::PDELab::LinearSolverResult<double>& result() const
{
return res;
}
private:
const GO& _grid_operator;
const GFS& gfs;
PHELPER phelper;
Dune::PDELab::LinearSolverResult<double> res;
unsigned maxiter;
unsigned steps;
int verbose;
};
//! \addtogroup PDELab_novlpsolvers Nonoverlapping Solvers
//! \{
/**
* @brief Nonoverlapping parallel BiCGSTAB solver preconditioned by block SSOR.
* @tparam GO The type of the grid operator
* (or the fakeGOTraits class for the old grid operator space).
*
* The solver uses a NonoverlappingBlockPreconditioner with underlying
* sequential SSOR preconditioner. The crucial step is to add up the matrix entries
* corresponding to the border vertices on each process. This is achieved by
* performing a VertexExchanger::sumEntries(Matrix&) before constructing the
* sequential SSOR.
*/
template<class GO>
class ISTLBackend_NOVLP_BCGS_SSORk
: public ISTLBackend_NOVLP_BASE_PREC<GO,Dune::SeqSSOR, Dune::BiCGSTABSolver>
{
public:
/*! \brief make a linear solver object
\param[in] gfs_ a grid function space
\param[in] maxiter_ maximum number of iterations to do
\param[in] steps_ number of SSOR steps to apply as inner iteration
\param[in] verbose_ print messages if true
*/
explicit ISTLBackend_NOVLP_BCGS_SSORk (const GO& grid_operator, unsigned maxiter_=5000,
int steps_=5, int verbose_=1)
: ISTLBackend_NOVLP_BASE_PREC<GO,Dune::SeqSSOR, Dune::BiCGSTABSolver>(grid_operator, maxiter_, steps_, verbose_)
{}
};
/**
* @brief Nonoverlapping parallel CG solver preconditioned by block SSOR.
*/
template<class GO>
class ISTLBackend_NOVLP_CG_SSORk
: public ISTLBackend_NOVLP_BASE_PREC<GO,Dune::SeqSSOR, Dune::CGSolver>
{
public:
/*! \brief make a linear solver object
\param[in] gfs_ a grid function space
\param[in] maxiter_ maximum number of iterations to do
\param[in] steps_ number of SSOR steps to apply as inner iteration
\param[in] verbose_ print messages if true
*/
explicit ISTLBackend_NOVLP_CG_SSORk (const GO& grid_operator, unsigned maxiter_=5000,
int steps_=5, int verbose_=1)
: ISTLBackend_NOVLP_BASE_PREC<GO,Dune::SeqSSOR, Dune::CGSolver>(grid_operator, maxiter_, steps_, verbose_)
{}
};
//! \} Nonoverlapping Solvers
//! \} group Backend
template<class GO,int s, template<class,class,class,int> class Preconditioner,
template<class> class Solver>
class ISTLBackend_AMG_NOVLP : public LinearResultStorage
{
typedef typename GO::Traits::TrialGridFunctionSpace GFS;
typedef typename istl::ParallelHelper<GFS> PHELPER;
typedef typename GO::Traits::Jacobian M;
typedef typename M::BaseT MatrixType;
typedef typename GO::Traits::Domain V;
typedef typename V::BaseT VectorType;
typedef typename istl::CommSelector<s,Dune::MPIHelper::isFake>::type Comm;
#if HAVE_MPI
typedef Preconditioner<MatrixType,VectorType,VectorType,1> Smoother;
typedef Dune::NonoverlappingBlockPreconditioner<Comm,Smoother> ParSmoother;
typedef Dune::NonoverlappingSchwarzOperator<MatrixType,VectorType,VectorType,Comm> Operator;
#else
typedef Preconditioner<MatrixType,VectorType,VectorType,1> ParSmoother;
typedef Dune::MatrixAdapter<MatrixType,VectorType,VectorType> Operator;
#endif
typedef typename Dune::Amg::SmootherTraits<ParSmoother>::Arguments SmootherArgs;
typedef Dune::Amg::AMG<Operator,VectorType,ParSmoother,Comm> AMG;
typedef Dune::Amg::Parameters Parameters;
public:
ISTLBackend_AMG_NOVLP(const GO& grid_operator, unsigned maxiter_=5000,
int verbose_=1, bool reuse_=false,
bool usesuperlu_=true)
: _grid_operator(grid_operator)
, gfs(grid_operator.trialGridFunctionSpace())
, phelper(gfs,verbose_)
, maxiter(maxiter_)
, params(15,2000,1.2,1.6,Dune::Amg::atOnceAccu)
, verbose(verbose_)
, reuse(reuse_)
, firstapply(true)
, usesuperlu(usesuperlu_)
{
params.setDefaultValuesIsotropic(GFS::Traits::GridViewType::Traits::Grid::dimension);
params.setDebugLevel(verbose_);
#if !HAVE_SUPERLU
if (phelper.rank() == 0 && usesuperlu == true)
{
std::cout << "WARNING: You are using AMG without SuperLU!"
<< " Please consider installing SuperLU,"
<< " or set the usesuperlu flag to false"
<< " to suppress this warning." << std::endl;
}
#endif
}
/*! \brief set AMG parameters
\param[in] params_ a parameter object of Type Dune::Amg::Parameters
*/
void setParameters(const Parameters& params_)
{
params = params_;
}
void setparams(Parameters params_) DUNE_DEPRECATED_MSG("setparams() is deprecated, use setParameters() instead")
{
params = params_;
}
/**
* @brief Get the parameters describing the behaviuour of AMG.
*
* The returned object can be adjusted to ones needs and then can be
* reset using setParameters.
* @return The object holding the parameters of AMG.
*/
const Parameters& parameters() const
{
return params;
}
/*! \brief compute global norm of a vector
\param[in] v the given vector
*/
typename V::ElementType norm (const V& v) const
{
V x(v); // make a copy because it has to be made consistent
typedef Dune::PDELab::NonoverlappingScalarProduct<GFS,V> PSP;
PSP psp(gfs,phelper);
psp.make_consistent(x);
return psp.norm(x);
}
void apply(M& A, V& z, V& r, typename V::ElementType reduction)
{
Timer watch;
MatrixType& mat=istl::raw(A);
typedef Dune::Amg::CoarsenCriterion<Dune::Amg::SymmetricCriterion<MatrixType,
Dune::Amg::FirstDiagonal> > Criterion;
#if HAVE_MPI
Comm oocc(gfs.gridView().comm(),Dune::SolverCategory::nonoverlapping);
_grid_operator.make_consistent(A);
phelper.createIndexSetAndProjectForAMG(A, oocc);
Dune::NonoverlappingSchwarzScalarProduct<VectorType,Comm> sp(oocc);
Operator oop(mat, oocc);
#else
Comm oocc(gfs.gridView().comm());
Operator oop(mat);
Dune::SeqScalarProduct<VectorType> sp;
#endif
SmootherArgs smootherArgs;
smootherArgs.iterations = 1;
smootherArgs.relaxationFactor = 1;
//use noAccu or atOnceAccu
Criterion criterion(params);
stats.tprepare=watch.elapsed();
watch.reset();
int verb=0;
if (gfs.gridView().comm().rank()==0) verb=verbose;
//only construct a new AMG if the matrix changes
if (reuse==false || firstapply==true){
amg.reset(new AMG(oop, criterion, smootherArgs, oocc));
firstapply = false;
stats.tsetup = watch.elapsed();
stats.levels = amg->maxlevels();
stats.directCoarseLevelSolver=amg->usesDirectCoarseLevelSolver();
}
Dune::InverseOperatorResult stat;
// make r consistent
if (gfs.gridView().comm().size()>1) {
Dune::PDELab::AddDataHandle<GFS,V> adddh(gfs,r);
gfs.gridView().communicate(adddh,
Dune::InteriorBorder_InteriorBorder_Interface,
Dune::ForwardCommunication);
}
watch.reset();
Solver<VectorType> solver(oop,sp,*amg,reduction,maxiter,verb);
solver.apply(istl::raw(z),istl::raw(r),stat);
stats.tsolve= watch.elapsed();
res.converged = stat.converged;
res.iterations = stat.iterations;
res.elapsed = stat.elapsed;
res.reduction = stat.reduction;
res.conv_rate = stat.conv_rate;
}
/**
* @brief Get statistics of the AMG solver (no of levels, timings).
* @return statistis of the AMG solver.
*/
const ISTLAMGStatistics& statistics() const
{
return stats;
}
private:
const GO& _grid_operator;
const GFS& gfs;
PHELPER phelper;
unsigned maxiter;
Parameters params;
int verbose;
bool reuse;
bool firstapply;
bool usesuperlu;
Dune::shared_ptr<AMG> amg;
ISTLAMGStatistics stats;
};
//! \addtogroup PDELab_novlpsolvers Nonoverlapping Solvers
//! \{
/**
* @brief Nonoverlapping parallel CG solver preconditioned with AMG smoothed by SSOR.
* @tparam GO The type of the grid operator
* (or the fakeGOTraits class for the old grid operator space).
* @tparam s The bits to use for the global index.
*
* The solver uses AMG with underlying
* sequential SSOR preconditioner. The crucial step is to add up the matrix entries
* corresponding to the border vertices on each process. This is achieved by
* performing a VertexExchanger::sumEntries(Matrix&).
*/
template<class GO, int s=96>
class ISTLBackend_NOVLP_CG_AMG_SSOR
: public ISTLBackend_AMG_NOVLP<GO, s, Dune::SeqSSOR, Dune::CGSolver>
{
public:
ISTLBackend_NOVLP_CG_AMG_SSOR(const GO& grid_operator, unsigned maxiter_=5000,
int verbose_=1, bool reuse_=false,
bool usesuperlu_=true)
: ISTLBackend_AMG_NOVLP<GO, s, Dune::SeqSSOR, Dune::CGSolver>(grid_operator, maxiter_,verbose_,reuse_,usesuperlu_)
{}
};
/**
* @brief Nonoverlapping parallel BiCGStab solver preconditioned with AMG smoothed by SSOR.
* @tparam GO The type of the grid operator
* (or the fakeGOTraits class for the old grid operator space).
* @tparam s The bits to use for the global index.
*
* The solver uses AMG with underlying
* sequential SSOR preconditioner. The crucial step is to add up the matrix entries
* corresponding to the border vertices on each process. This is achieved by
* performing a VertexExchanger::sumEntries(Matrix&).
*/
template<class GO, int s=96>
class ISTLBackend_NOVLP_BCGS_AMG_SSOR
: public ISTLBackend_AMG_NOVLP<GO, s, Dune::SeqSSOR, Dune::BiCGSTABSolver>
{
public:
ISTLBackend_NOVLP_BCGS_AMG_SSOR(const GO& grid_operator, unsigned maxiter_=5000,
int verbose_=1, bool reuse_=false,
bool usesuperlu_=true)
: ISTLBackend_AMG_NOVLP<GO, s, Dune::SeqSSOR, Dune::BiCGSTABSolver>(grid_operator, maxiter_,verbose_,reuse_,usesuperlu_)
{}
};
/**
* @brief Nonoverlapping parallel LoopSolver preconditioned with AMG smoothed by SSOR.
* @tparam GO The type of the grid operator
* (or the fakeGOTraits class for the old grid operator space).
* @tparam s The bits to use for the global index.
*
* The solver uses AMG with underlying
* sequential SSOR preconditioner. The crucial step is to add up the matrix entries
* corresponding to the border vertices on each process. This is achieved by
* performing a VertexExchanger::sumEntries(Matrix&).
*/
template<class GO, int s=96>
class ISTLBackend_NOVLP_LS_AMG_SSOR
: public ISTLBackend_AMG_NOVLP<GO, s, Dune::SeqSSOR, Dune::LoopSolver>
{
public:
ISTLBackend_NOVLP_LS_AMG_SSOR(const GO& grid_operator, unsigned maxiter_=5000,
int verbose_=1, bool reuse_=false,
bool usesuperlu_=true)
: ISTLBackend_AMG_NOVLP<GO, s, Dune::SeqSSOR, Dune::LoopSolver>(grid_operator, maxiter_,verbose_,reuse_,usesuperlu_)
{}
};
//! \} Nonoverlapping Solvers
//! \} group Backend
} // namespace PDELab
} // namespace Dune
#endif
|