3.1-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
amgbackend.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
25#ifndef DUMUX_PARALLEL_AMGBACKEND_HH
26#define DUMUX_PARALLEL_AMGBACKEND_HH
27
28#include <memory>
29
30#include <dune/common/exceptions.hh>
31#include <dune/common/parallel/indexset.hh>
32#include <dune/common/parallel/mpicollectivecommunication.hh>
33#include <dune/grid/common/capabilities.hh>
34#include <dune/istl/paamg/amg.hh>
35#include <dune/istl/paamg/pinfo.hh>
36#include <dune/istl/solvers.hh>
37
40
41namespace Dumux {
42
51template <class Matrix, class Vector>
52void scaleLinearSystem(Matrix& matrix, Vector& rhs)
53{
54 typename Matrix::RowIterator row = matrix.begin();
55 for(; row != matrix.end(); ++row)
56 {
57 using size_type = typename Matrix::size_type;
58 size_type rowIdx = row.index();
59
60 using MatrixBlock = typename Matrix::block_type;
61 MatrixBlock diagonal = matrix[rowIdx][rowIdx];
62 diagonal.invert();
63
64 using VectorBlock = typename Vector::block_type;
65 const VectorBlock b = rhs[rowIdx];
66 diagonal.mv(b, rhs[rowIdx]);
67
68 typename Matrix::ColIterator col = row->begin();
69 for (; col != row->end(); ++col)
70 col->leftmultiply(diagonal);
71 }
72}
73
79template <class GridView, class AmgTraits>
81{
82 using Grid = typename GridView::Grid;
83 using LinearOperator = typename AmgTraits::LinearOperator;
84 using ScalarProduct = typename AmgTraits::ScalarProduct;
85 using VType = typename AmgTraits::VType;
86 using Comm = typename AmgTraits::Comm;
87 using Smoother = typename AmgTraits::Smoother;
88 using AMGType = Dune::Amg::AMG<typename AmgTraits::LinearOperator, VType, Smoother,Comm>;
89 using BCRSMat = typename AmgTraits::LinearOperator::matrix_type;
90 using DofMapper = typename AmgTraits::DofMapper;
91public:
97 ParallelAMGBackend(const std::string& paramGroup = "")
99 , firstCall_(true)
100 {
101 if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
102 DUNE_THROW(Dune::InvalidStateException, "Using sequential constructor for parallel run. Use signature with gridView and dofMapper!");
103 }
104
112 ParallelAMGBackend(const GridView& gridView,
113 const DofMapper& dofMapper,
114 const std::string& paramGroup = "")
116 , phelper_(std::make_shared<ParallelISTLHelper<GridView, AmgTraits>>(gridView, dofMapper))
117 , firstCall_(true)
118 {}
119
127 template<class Matrix, class Vector>
128 bool solve(Matrix& A, Vector& x, Vector& b)
129 {
130 int rank = 0;
131 std::shared_ptr<Comm> comm;
132 std::shared_ptr<LinearOperator> fop;
133 std::shared_ptr<ScalarProduct> sp;
134 static const bool isParallel = AmgTraits::isParallel;
135 prepareLinearAlgebra_<Matrix, Vector, isParallel>(A, b, rank, comm, fop, sp);
136
137 using SmootherArgs = typename Dune::Amg::SmootherTraits<Smoother>::Arguments;
138 using Criterion = Dune::Amg::CoarsenCriterion<Dune::Amg::SymmetricCriterion<BCRSMat, Dune::Amg::FirstDiagonal>>;
139
142 Dune::Amg::Parameters params(15,2000,1.2,1.6,Dune::Amg::atOnceAccu);
143 params.setDefaultValuesIsotropic(Grid::dimension);
144 params.setDebugLevel(this->verbosity());
145 Criterion criterion(params);
146 SmootherArgs smootherArgs;
147 smootherArgs.iterations = 1;
148 smootherArgs.relaxationFactor = 1;
149
150 AMGType amg(*fop, criterion, smootherArgs, *comm);
151 Dune::BiCGSTABSolver<VType> solver(*fop, *sp, amg, this->residReduction(), this->maxIter(),
152 rank == 0 ? this->verbosity() : 0);
153
154 solver.apply(x, b, result_);
155 firstCall_ = false;
156 return result_.converged;
157 }
158
162 std::string name() const
163 {
164 return "AMG preconditioned BiCGSTAB solver";
165 }
166
170 const Dune::InverseOperatorResult& result() const
171 {
172 return result_;
173 }
174
175private:
176
194 template<class Matrix, class Vector, bool isParallel>
195 void prepareLinearAlgebra_(Matrix& A, Vector& b, int& rank,
196 std::shared_ptr<Comm>& comm,
197 std::shared_ptr<LinearOperator>& fop,
198 std::shared_ptr<ScalarProduct>& sp)
199 {
201 ::prepareLinearAlgebra(A, b, rank, comm, fop, sp,
202 *phelper_, firstCall_);
203 }
204
205 std::shared_ptr<ParallelISTLHelper<GridView, AmgTraits>> phelper_;
206 Dune::InverseOperatorResult result_;
207 bool firstCall_;
208};
209
210} // namespace Dumux
211
214
215namespace Dumux {
216
223template<class TypeTag>
227
228} // namespace Dumux
229
230#endif // DUMUX_AMGBACKEND_HH
Provides a helper class for nonoverlapping decomposition using the ISTL AMG.
Define traits for the AMG backend.
void scaleLinearSystem(Matrix &matrix, Vector &rhs)
Scale the linear system by the inverse of its (block-)diagonal entries.
Definition: amgbackend.hh:52
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:149
A linear solver based on the ISTL AMG preconditioner and the ISTL BiCGSTAB solver.
Definition: amgbackend.hh:81
ParallelAMGBackend(const std::string &paramGroup="")
Construct the backend for the sequential case only.
Definition: amgbackend.hh:97
ParallelAMGBackend(const GridView &gridView, const DofMapper &dofMapper, const std::string &paramGroup="")
Construct the backend for parallel or sequential runs.
Definition: amgbackend.hh:112
std::string name() const
The name of the solver.
Definition: amgbackend.hh:162
bool solve(Matrix &A, Vector &x, Vector &b)
Solve a linear system.
Definition: amgbackend.hh:128
const Dune::InverseOperatorResult & result() const
The result containing the convergence history.
Definition: amgbackend.hh:170
A parallel helper class providing a nonoverlapping decomposition of all degrees of freedom.
Definition: amgparallelhelpers.hh:45
static void prepareLinearAlgebra(Matrix &A, Vector &b, int &rank, std::shared_ptr< Comm > &comm, std::shared_ptr< LinearOperator > &fop, std::shared_ptr< ScalarProduct > &sp, ParallelHelper &pHelper, const bool firstCall)
Definition: amgparallelhelpers.hh:1032
The implementation is specialized for the different discretizations.
Definition: amgtraits.hh:71
Base class for linear solvers.
Definition: dumux/linear/solver.hh:37
double residReduction() const
the linear solver residual reduction
Definition: dumux/linear/solver.hh:97
int maxIter() const
the maximum number of linear solver iterations
Definition: dumux/linear/solver.hh:89
const std::string & paramGroup() const
the parameter group for getting parameter from the parameter tree
Definition: dumux/linear/solver.hh:77
int verbosity() const
the verbosity level
Definition: dumux/linear/solver.hh:81
Declares all properties used in Dumux.
Base class for linear solvers.