version 3.7
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// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13#ifndef DUMUX_PARALLEL_AMGBACKEND_HH
14#define DUMUX_PARALLEL_AMGBACKEND_HH
15
16#warning "This header is deprecated and will be removed after release 3.7. Use the AMG solver from dumux/linear/istlsolvers.hh"
17
18#include <memory>
19
20#include <dune/common/exceptions.hh>
21#include <dune/common/parallel/indexset.hh>
22#include <dune/common/parallel/mpicommunication.hh>
23#include <dune/common/parallel/mpihelper.hh>
24#include <dune/grid/common/capabilities.hh>
25#include <dune/istl/paamg/amg.hh>
26#include <dune/istl/paamg/pinfo.hh>
27#include <dune/istl/solvers.hh>
28
32
33namespace Dumux {
34
35// OLD AMG Backend. Use the new one from dumux/linear/istlsolvers.hh
36template <class LinearSolverTraits>
38{
39public:
45 [[deprecated("Use new AMGBiCGSTABIstlSolver<LinearSolverTraits, LinearAlgebraTraits> with 2nd template parameter from dumux/linear/istlsolvers.hh.")]]
46 AMGBiCGSTABBackend(const std::string& paramGroup = "")
48 , isParallel_(Dune::MPIHelper::getCommunication().size() > 1)
49 {
50 if (isParallel_)
51 DUNE_THROW(Dune::InvalidStateException, "Using sequential constructor for parallel run. Use signature with gridView and dofMapper!");
52
53 checkAvailabilityOfDirectSolver_();
54 }
55
63 [[deprecated("Use new AMGBiCGSTABIstlSolver<LinearSolverTraits, LinearAlgebraTraits> with 2nd template parameter from dumux/linear/istlsolvers.hh.")]]
64 AMGBiCGSTABBackend(const typename LinearSolverTraits::GridView& gridView,
65 const typename LinearSolverTraits::DofMapper& dofMapper,
66 const std::string& paramGroup = "")
68#if HAVE_MPI
69 , isParallel_(Dune::MPIHelper::getCommunication().size() > 1)
70#endif
71 {
72#if HAVE_MPI
74 {
75 if (isParallel_)
76 phelper_ = std::make_unique<ParallelISTLHelper<LinearSolverTraits>>(gridView, dofMapper);
77 }
78#endif
79 checkAvailabilityOfDirectSolver_();
80 }
81
88 void updateAfterGridAdaption(const typename LinearSolverTraits::GridView& gridView,
89 const typename LinearSolverTraits::DofMapper& dofMapper)
90 {
91#if HAVE_MPI
92 if (isParallel_)
93 phelper_ = std::make_unique<ParallelISTLHelper<LinearSolverTraits>>(gridView, dofMapper);
94#endif
95 }
96
104 template<class Matrix, class Vector>
105 bool solve(Matrix& A, Vector& x, Vector& b)
106 {
107#if HAVE_MPI
108 solveSequentialOrParallel_(A, x, b);
109#else
110 solveSequential_(A, x, b);
111#endif
112 return result_.converged;
113 }
114
118 std::string name() const
119 {
120 return "AMG-preconditioned BiCGSTAB solver";
121 }
122
126 const Dune::InverseOperatorResult& result() const
127 {
128 return result_;
129 }
130
131 template<class Vector>
132 Scalar norm(const Vector& x) const = delete;
133
134private:
136 void checkAvailabilityOfDirectSolver_()
137 {
138#if !HAVE_SUPERLU && !HAVE_UMFPACK
139 std::cout << "\nAMGBiCGSTABBackend: No direct solver backend found. Using iterative solver as coarse grid solver.\n"
140 << "Note that dune-istl currently hard-codes a tolerance of 1e-2 for the iterative coarse grid solver.\n"
141 << "This may result in reduced accuracy or performance depending on your setup.\nConsider installing "
142 << "UMFPack (SuiteSparse) or SuperLU or apply the istl patch, see dumux/patches/README.md." << std::endl;
143#endif
144 }
145
146#if HAVE_MPI
147 template<class Matrix, class Vector>
148 void solveSequentialOrParallel_(Matrix& A, Vector& x, Vector& b)
149 {
151 {
152 if (isParallel_)
153 {
154 if (LinearSolverTraits::isNonOverlapping(phelper_->gridView()))
155 {
156 using PTraits = typename LinearSolverTraits::template ParallelNonoverlapping<Matrix, Vector>;
157 solveParallel_<PTraits>(A, x, b);
158 }
159 else
160 {
161 using PTraits = typename LinearSolverTraits::template ParallelOverlapping<Matrix, Vector>;
162 solveParallel_<PTraits>(A, x, b);
163 }
164 }
165 else
166 solveSequential_(A, x, b);
167 }
168 else
169 {
170 solveSequential_(A, x, b);
171 }
172 }
173
174 template<class ParallelTraits, class Matrix, class Vector>
175 void solveParallel_(Matrix& A, Vector& x, Vector& b)
176 {
177 using Comm = typename ParallelTraits::Comm;
178 using LinearOperator = typename ParallelTraits::LinearOperator;
179 using ScalarProduct = typename ParallelTraits::ScalarProduct;
180
181 std::shared_ptr<Comm> comm;
182 std::shared_ptr<LinearOperator> linearOperator;
183 std::shared_ptr<ScalarProduct> scalarProduct;
184 prepareLinearAlgebraParallel<LinearSolverTraits, ParallelTraits>(A, b, comm, linearOperator, scalarProduct, *phelper_);
185
186 using SeqSmoother = Dune::SeqSSOR<Matrix, Vector, Vector>;
187 using Smoother = typename ParallelTraits::template Preconditioner<SeqSmoother>;
188 solveWithAmg_<Smoother>(A, x, b, linearOperator, comm, scalarProduct);
189 }
190#endif // HAVE_MPI
191
192 template<class Matrix, class Vector>
193 void solveSequential_(Matrix& A, Vector& x, Vector& b)
194 {
195 using Comm = Dune::Amg::SequentialInformation;
196 using Traits = typename LinearSolverTraits::template Sequential<Matrix, Vector>;
197 using LinearOperator = typename Traits::LinearOperator;
198 using ScalarProduct = typename Traits::ScalarProduct;
199
200 auto comm = std::make_shared<Comm>();
201 auto linearOperator = std::make_shared<LinearOperator>(A);
202 auto scalarProduct = std::make_shared<ScalarProduct>();
203
204 using Smoother = Dune::SeqSSOR<Matrix, Vector, Vector>;
205 solveWithAmg_<Smoother>(A, x, b, linearOperator, comm, scalarProduct);
206 }
207
208 template<class Smoother, class Matrix, class Vector, class LinearOperator, class Comm, class ScalarProduct>
209 void solveWithAmg_(Matrix& A, Vector& x, Vector& b,
210 std::shared_ptr<LinearOperator>& linearOperator,
211 std::shared_ptr<Comm>& comm,
212 std::shared_ptr<ScalarProduct>& scalarProduct)
213 {
214 using SmootherArgs = typename Dune::Amg::SmootherTraits<Smoother>::Arguments;
215 using Criterion = Dune::Amg::CoarsenCriterion<Dune::Amg::SymmetricCriterion<Matrix, Dune::Amg::FirstDiagonal>>;
216
219 Dune::Amg::Parameters params(15, 2000, 1.2, 1.6, Dune::Amg::atOnceAccu);
220 params.setDefaultValuesIsotropic(LinearSolverTraits::GridView::dimension);
221 params.setDebugLevel(this->verbosity());
222 Criterion criterion(params);
223 SmootherArgs smootherArgs;
224 smootherArgs.iterations = 1;
225 smootherArgs.relaxationFactor = 1;
226
227 using Amg = Dune::Amg::AMG<LinearOperator, Vector, Smoother, Comm>;
228 auto amg = std::make_shared<Amg>(*linearOperator, criterion, smootherArgs, *comm);
229
230 Dune::BiCGSTABSolver<Vector> solver(*linearOperator, *scalarProduct, *amg, this->residReduction(), this->maxIter(),
231 comm->communicator().rank() == 0 ? this->verbosity() : 0);
232
233 solver.apply(x, b, result_);
234 }
235
236#if HAVE_MPI
237 std::unique_ptr<ParallelISTLHelper<LinearSolverTraits>> phelper_;
238#endif
239 Dune::InverseOperatorResult result_;
240 bool isParallel_ = false;
241};
242
243} // end namespace Dumux
244
245#endif
Definition: amgbackend.hh:38
const Dune::InverseOperatorResult & result() const
The result containing the convergence history.
Definition: amgbackend.hh:126
std::string name() const
The name of the solver.
Definition: amgbackend.hh:118
AMGBiCGSTABBackend(const std::string &paramGroup="")
Construct the backend for the sequential case only.
Definition: amgbackend.hh:46
Scalar norm(const Vector &x) const =delete
AMGBiCGSTABBackend(const typename LinearSolverTraits::GridView &gridView, const typename LinearSolverTraits::DofMapper &dofMapper, const std::string &paramGroup="")
Construct the backend for parallel or sequential runs.
Definition: amgbackend.hh:64
bool solve(Matrix &A, Vector &x, Vector &b)
Solve a linear system.
Definition: amgbackend.hh:105
void updateAfterGridAdaption(const typename LinearSolverTraits::GridView &gridView, const typename LinearSolverTraits::DofMapper &dofMapper)
Update the solver after grid adaption.
Definition: amgbackend.hh:88
Base class for linear solvers.
Definition: solver.hh:27
Scalar residReduction() const
the linear solver residual reduction
Definition: solver.hh:98
int maxIter() const
the maximum number of linear solver iterations
Definition: solver.hh:90
const std::string & paramGroup() const
the parameter group for getting parameter from the parameter tree
Definition: solver.hh:78
int verbosity() const
the verbosity level
Definition: solver.hh:82
double Scalar
Definition: solver.hh:31
static constexpr bool canCommunicate
Definition: gridcapabilities.hh:51
Definition: adapt.hh:17
Definition: common/pdesolver.hh:24
Provides a helper class for nonoverlapping decomposition.
Base class for linear solvers.