| 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_AMG_KAMG_HH |
| 6 |
#define DUNE_AMG_KAMG_HH |
| 7 |
|
| 8 |
#include <dune/istl/preconditioners.hh> |
| 9 |
#include "amg.hh" |
| 10 |
|
| 11 |
namespace Dune |
| 12 |
{ |
| 13 |
namespace Amg |
| 14 |
{ |
| 15 |
|
| 16 |
/** |
| 17 |
* @addtogroup ISTL_PAAMG |
| 18 |
* @{ |
| 19 |
*/ |
| 20 |
/** @file |
| 21 |
* @author Markus Blatt |
| 22 |
* @brief Provides an algebraic multigrid using a Krylov cycle. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
/** |
| 27 |
* @brief Two grid operator for AMG with Krylov cycle. |
| 28 |
* @tparam AMG The type of the underlying agglomeration AMG. |
| 29 |
*/ |
| 30 |
template<class AMG> |
| 31 |
class KAmgTwoGrid |
| 32 |
: public Preconditioner<typename AMG::Domain,typename AMG::Range> |
| 33 |
{ |
| 34 |
/** @brief The type of the domain. */ |
| 35 |
typedef typename AMG::Domain Domain; |
| 36 |
/** @brief the type of the range. */ |
| 37 |
typedef typename AMG::Range Range; |
| 38 |
public: |
| 39 |
|
| 40 |
//! Category of the preconditioner (see SolverCategory::Category) |
| 41 |
virtual SolverCategory::Category category() const |
| 42 |
{ |
| 43 |
return amg_.category(); |
| 44 |
}; |
| 45 |
|
| 46 |
/** |
| 47 |
* @brief Constructor. |
| 48 |
* @param amg The underlying amg. It is used as the storage for the hierarchic |
| 49 |
* data structures. |
| 50 |
* @param coarseSolver The solver used for the coarse grid correction. |
| 51 |
*/ |
| 52 |
|
| 53 |
KAmgTwoGrid(AMG& amg, std::shared_ptr<InverseOperator<Domain,Range> > coarseSolver) |
| 54 |
: amg_(amg), coarseSolver_(coarseSolver) |
| 55 |
{} |
| 56 |
|
| 57 |
/** \copydoc Preconditioner::pre(X&,Y&) */ |
| 58 |
void pre([[maybe_unused]] typename AMG::Domain& x, [[maybe_unused]] typename AMG::Range& b) |
| 59 |
{} |
| 60 |
|
| 61 |
/** \copydoc Preconditioner::post(X&) */ |
| 62 |
void post([[maybe_unused]] typename AMG::Domain& x) |
| 63 |
{} |
| 64 |
|
| 65 |
/** \copydoc Preconditioner::apply(X&,const Y&) */ |
| 66 |
void apply(typename AMG::Domain& v, const typename AMG::Range& d) |
| 67 |
{ |
| 68 |
// Copy data |
| 69 |
*levelContext_->update=0; |
| 70 |
*levelContext_->rhs = d; |
| 71 |
*levelContext_->lhs = v; |
| 72 |
|
| 73 |
presmooth(*levelContext_, amg_.preSteps_); |
| 74 |
bool processFineLevel = |
| 75 |
amg_.moveToCoarseLevel(*levelContext_); |
| 76 |
|
| 77 |
if(processFineLevel) { |
| 78 |
typename AMG::Range b=*levelContext_->rhs; |
| 79 |
typename AMG::Domain x=*levelContext_->update; |
| 80 |
InverseOperatorResult res; |
| 81 |
coarseSolver_->apply(x, b, res); |
| 82 |
*levelContext_->update=x; |
| 83 |
} |
| 84 |
|
| 85 |
amg_.moveToFineLevel(*levelContext_, processFineLevel); |
| 86 |
|
| 87 |
postsmooth(*levelContext_, amg_.postSteps_); |
| 88 |
v=*levelContext_->update; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @brief Get a pointer to the coarse grid solver. |
| 93 |
* @return The coarse grid solver. |
| 94 |
*/ |
| 95 |
InverseOperator<Domain,Range>* coarseSolver() |
| 96 |
{ |
| 97 |
return coarseSolver_; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @brief Set the level context pointer. |
| 102 |
* @param p The pointer to set it to. |
| 103 |
*/ |
| 104 |
void setLevelContext(std::shared_ptr<typename AMG::LevelContext> p) |
| 105 |
{ |
| 106 |
levelContext_=p; |
| 107 |
} |
| 108 |
|
| 109 |
/** @brief Destructor. */ |
| 110 |
~KAmgTwoGrid() |
| 111 |
{} |
| 112 |
|
| 113 |
private: |
| 114 |
/** @brief Underlying AMG used as storage and engine. */ |
| 115 |
AMG& amg_; |
| 116 |
/** @brief The coarse grid solver.*/ |
| 117 |
std::shared_ptr<InverseOperator<Domain,Range> > coarseSolver_; |
| 118 |
/** @brief A shared pointer to the level context of AMG. */ |
| 119 |
std::shared_ptr<typename AMG::LevelContext> levelContext_; |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* @brief an algebraic multigrid method using a Krylov-cycle. |
| 126 |
* |
| 127 |
* The implementation is based on the paper |
| 128 |
* [[Notay and Vassilevski, 2007]](http://onlinelibrary.wiley.com/doi/10.1002/nla.542/abstract) |
| 129 |
* |
| 130 |
* @tparam M The type of the linear operator. |
| 131 |
* @tparam X The type of the range and domain. |
| 132 |
* @tparam PI The parallel information object. Use SequentialInformation (default) |
| 133 |
* for a sequential AMG, OwnerOverlapCopyCommunication for the parallel case. |
| 134 |
* @tparam K The type of the Krylov method to use for the cycle. |
| 135 |
* @tparam A The type of the allocator to use. |
| 136 |
*/ |
| 137 |
template<class M, class X, class S, class PI=SequentialInformation, |
| 138 |
class K=GeneralizedPCGSolver<X>, class A=std::allocator<X> > |
| 139 |
class KAMG : public Preconditioner<X,X> |
| 140 |
{ |
| 141 |
public: |
| 142 |
/** @brief The type of the underlying AMG. */ |
| 143 |
typedef AMG<M,X,S,PI,A> Amg; |
| 144 |
/** @brief The type of the Krylov solver for the cycle. */ |
| 145 |
typedef K KrylovSolver; |
| 146 |
/** @brief The type of the hierarchy of operators. */ |
| 147 |
typedef typename Amg::OperatorHierarchy OperatorHierarchy; |
| 148 |
/** @brief The type of the coarse solver. */ |
| 149 |
typedef typename Amg::CoarseSolver CoarseSolver; |
| 150 |
/** @brief the type of the parallelinformation to use.*/ |
| 151 |
typedef typename Amg::ParallelInformation ParallelInformation; |
| 152 |
/** @brief The type of the arguments for construction of the smoothers. */ |
| 153 |
typedef typename Amg::SmootherArgs SmootherArgs; |
| 154 |
/** @brief the type of the lineatr operator. */ |
| 155 |
typedef typename Amg::Operator Operator; |
| 156 |
/** @brief the type of the domain. */ |
| 157 |
typedef typename Amg::Domain Domain; |
| 158 |
/** @brief The type of the range. */ |
| 159 |
typedef typename Amg::Range Range; |
| 160 |
/** @brief The type of the hierarchy of parallel information. */ |
| 161 |
typedef typename Amg::ParallelInformationHierarchy ParallelInformationHierarchy; |
| 162 |
/** @brief The type of the scalar product. */ |
| 163 |
typedef typename Amg::ScalarProduct ScalarProduct; |
| 164 |
|
| 165 |
//! Category of the preconditioner (see SolverCategory::Category) |
| 166 |
virtual SolverCategory::Category category() const |
| 167 |
{ |
| 168 |
return amg.category(); |
| 169 |
}; |
| 170 |
|
| 171 |
/** |
| 172 |
* @brief Construct a new amg with a specific coarse solver. |
| 173 |
* @param matrices The already set-up matrix hierarchy. |
| 174 |
* @param coarseSolver The set up solver to use on the coarse |
| 175 |
* grid, must match the coarse matrix in the matrix hierarchy. |
| 176 |
* @param smootherArgs The arguments needed for thesmoother to use |
| 177 |
* for pre and post smoothing. |
| 178 |
* @param parms The parameters for the AMG. |
| 179 |
* @param maxLevelKrylovSteps maximum of krylov iterations on a particular level (default=3) |
| 180 |
* @param minDefectReduction minimal defect reduction during the krylov iterations on a particular level (default=1e-1) |
| 181 |
*/ |
| 182 |
KAMG(OperatorHierarchy& matrices, CoarseSolver& coarseSolver, |
| 183 |
const SmootherArgs& smootherArgs, const Parameters& parms, |
| 184 |
std::size_t maxLevelKrylovSteps=3, double minDefectReduction=1e-1); |
| 185 |
|
| 186 |
/** |
| 187 |
* @brief Construct an AMG with an inexact coarse solver based on the smoother. |
| 188 |
* |
| 189 |
* As coarse solver a preconditioned CG method with the smoother as preconditioner |
| 190 |
* will be used. The matrix hierarchy is built automatically. |
| 191 |
* @param fineOperator The operator on the fine level. |
| 192 |
* @param criterion The criterion describing the coarsening strategy. E. g. SymmetricCriterion |
| 193 |
* or UnsymmetricCriterion, and providing the parameters. |
| 194 |
* @param smootherArgs The arguments for constructing the smoothers. |
| 195 |
* @param maxLevelKrylovSteps maximum of krylov iterations on a particular level (default=3) |
| 196 |
* @param minDefectReduction minimal defect reduction during the krylov iterations on a particular level (default=1e-1) |
| 197 |
* @param pinfo The information about the parallel distribution of the data. |
| 198 |
*/ |
| 199 |
template<class C> |
| 200 |
KAMG(const Operator& fineOperator, const C& criterion, |
| 201 |
const SmootherArgs& smootherArgs=SmootherArgs(), |
| 202 |
std::size_t maxLevelKrylovSteps=3, double minDefectReduction=1e-1, |
| 203 |
const ParallelInformation& pinfo=ParallelInformation()); |
| 204 |
|
| 205 |
/** \copydoc Preconditioner::pre(X&,Y&) */ |
| 206 |
void pre(Domain& x, Range& b); |
| 207 |
/** \copydoc Preconditioner::post(X&) */ |
| 208 |
void post(Domain& x); |
| 209 |
/** \copydoc Preconditioner::apply(X&,const Y&) */ |
| 210 |
void apply(Domain& v, const Range& d); |
| 211 |
|
| 212 |
std::size_t maxlevels(); |
| 213 |
|
| 214 |
private: |
| 215 |
/** @brief The underlying amg. */ |
| 216 |
Amg amg; |
| 217 |
|
| 218 |
/** \brief The maximum number of Krylov steps allowed at each level. */ |
| 219 |
std::size_t maxLevelKrylovSteps; |
| 220 |
|
| 221 |
/** \brief The defect reduction to achieve on each krylov level. */ |
| 222 |
double levelDefectReduction; |
| 223 |
|
| 224 |
/** @brief pointers to the allocated scalar products. */ |
| 225 |
std::vector<std::shared_ptr<typename Amg::ScalarProduct> > scalarproducts; |
| 226 |
|
| 227 |
/** @brief pointers to the allocated krylov solvers. */ |
| 228 |
std::vector<std::shared_ptr<KAmgTwoGrid<Amg> > > ksolvers; |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
template<class M, class X, class S, class P, class K, class A> |
| 233 |
KAMG<M,X,S,P,K,A>::KAMG(OperatorHierarchy& matrices, CoarseSolver& coarseSolver, |
| 234 |
const SmootherArgs& smootherArgs, const Parameters& params, |
| 235 |
std::size_t ksteps, double reduction) |
| 236 |
: amg(matrices, coarseSolver, smootherArgs, params), |
| 237 |
maxLevelKrylovSteps(ksteps), levelDefectReduction(reduction) |
| 238 |
{} |
| 239 |
|
| 240 |
|
| 241 |
template<class M, class X, class S, class P, class K, class A> |
| 242 |
template<class C> |
| 243 |
KAMG<M,X,S,P,K,A>::KAMG(const Operator& fineOperator, const C& criterion, |
| 244 |
const SmootherArgs& smootherArgs, |
| 245 |
std::size_t ksteps, double reduction, |
| 246 |
const ParallelInformation& pinfo) |
| 247 |
: amg(fineOperator, criterion, smootherArgs, pinfo), |
| 248 |
maxLevelKrylovSteps(ksteps), levelDefectReduction(reduction) |
| 249 |
{} |
| 250 |
|
| 251 |
|
| 252 |
template<class M, class X, class S, class P, class K, class A> |
| 253 |
void KAMG<M,X,S,P,K,A>::pre(Domain& x, Range& b) |
| 254 |
{ |
| 255 |
amg.pre(x,b); |
| 256 |
scalarproducts.reserve(amg.levels()); |
| 257 |
ksolvers.reserve(amg.levels()); |
| 258 |
|
| 259 |
typename OperatorHierarchy::ParallelMatrixHierarchy::Iterator |
| 260 |
matrix = amg.matrices_->matrices().coarsest(); |
| 261 |
typename ParallelInformationHierarchy::Iterator |
| 262 |
pinfo = amg.matrices_->parallelInformation().coarsest(); |
| 263 |
bool hasCoarsest=(amg.levels()==amg.maxlevels()); |
| 264 |
|
| 265 |
if(hasCoarsest) { |
| 266 |
if(matrix==amg.matrices_->matrices().finest()) |
| 267 |
return; |
| 268 |
--matrix; |
| 269 |
--pinfo; |
| 270 |
ksolvers.push_back(std::shared_ptr<KAmgTwoGrid<Amg> >(new KAmgTwoGrid<Amg>(amg, amg.solver_))); |
| 271 |
}else |
| 272 |
ksolvers.push_back(std::shared_ptr<KAmgTwoGrid<Amg> >(new KAmgTwoGrid<Amg>(amg, std::shared_ptr<InverseOperator<Domain,Range> >()))); |
| 273 |
|
| 274 |
std::ostringstream s; |
| 275 |
|
| 276 |
if(matrix!=amg.matrices_->matrices().finest()) |
| 277 |
while(true) { |
| 278 |
scalarproducts.push_back(createScalarProduct<X>(*pinfo,category())); |
| 279 |
std::shared_ptr<InverseOperator<Domain,Range> > ks = |
| 280 |
std::shared_ptr<InverseOperator<Domain,Range> >(new KrylovSolver(*matrix, *(scalarproducts.back()), |
| 281 |
*(ksolvers.back()), levelDefectReduction, |
| 282 |
maxLevelKrylovSteps, 0)); |
| 283 |
ksolvers.push_back(std::shared_ptr<KAmgTwoGrid<Amg> >(new KAmgTwoGrid<Amg>(amg, ks))); |
| 284 |
--matrix; |
| 285 |
--pinfo; |
| 286 |
if(matrix==amg.matrices_->matrices().finest()) |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
template<class M, class X, class S, class P, class K, class A> |
| 293 |
void KAMG<M,X,S,P,K,A>::post(Domain& x) |
| 294 |
{ |
| 295 |
amg.post(x); |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
template<class M, class X, class S, class P, class K, class A> |
| 300 |
void KAMG<M,X,S,P,K,A>::apply(Domain& v, const Range& d) |
| 301 |
{ |
| 302 |
if(ksolvers.size()==0) |
| 303 |
{ |
| 304 |
Range td=d; |
| 305 |
InverseOperatorResult res; |
| 306 |
amg.solver_->apply(v,td,res); |
| 307 |
}else |
| 308 |
{ |
| 309 |
typedef typename Amg::LevelContext LevelContext; |
| 310 |
std::shared_ptr<LevelContext> levelContext(new LevelContext); |
| 311 |
amg.initIteratorsWithFineLevel(*levelContext); |
| 312 |
typedef typename std::vector<std::shared_ptr<KAmgTwoGrid<Amg> > >::iterator Iter; |
| 313 |
for(Iter solver=ksolvers.begin(); solver!=ksolvers.end(); ++solver) |
| 314 |
(*solver)->setLevelContext(levelContext); |
| 315 |
ksolvers.back()->apply(v,d); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
template<class M, class X, class S, class P, class K, class A> |
| 320 |
std::size_t KAMG<M,X,S,P,K,A>::maxlevels() |
| 321 |
{ |
| 322 |
return amg.maxlevels(); |
| 323 |
} |
| 324 |
|
| 325 |
/** @}*/ |
| 326 |
} // Amg |
| 327 |
} // Dune |
| 328 |
|
| 329 |
#endif |