twolevelmethodcpr.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ISTL_TWOLEVELMETHODCPR_HH
4#define DUNE_ISTL_TWOLEVELMETHODCPR_HH
5
6// NOTE: This file is a modified version of dune/istl/paamg/twolevelmethod.hh from
7// dune-istl release 2.6.0. Modifications have been kept as minimal as possible.
8
9#include <tuple>
10
11#include<dune/istl/operators.hh>
12//#include "amg.hh"
13//#include"galerkin.hh"
14#include<dune/istl/paamg/amg.hh>
15#include<dune/istl/paamg/galerkin.hh>
16#include<dune/istl/solver.hh>
17
18#include<dune/common/unused.hh>
19#include<dune/common/version.hh>
20
28namespace Dune
29{
30namespace Amg
31{
32
42template<class FO, class CO>
44{
45public:
50 typedef FO FineOperatorType;
54 typedef typename FineOperatorType::range_type FineRangeType;
58 typedef typename FineOperatorType::domain_type FineDomainType;
67 typedef typename CoarseOperatorType::range_type CoarseRangeType;
71 typedef typename CoarseOperatorType::domain_type CoarseDomainType;
76 std::shared_ptr<CoarseOperatorType>& getCoarseLevelOperator()
77 {
78 return operator_;
79 }
85 {
86 return rhs_;
87 }
88
94 {
95 return lhs_;
96 }
106 virtual void moveToCoarseLevel(const FineRangeType& fineRhs)=0;
116 virtual void moveToFineLevel(FineDomainType& fineLhs)=0;
124 virtual void createCoarseLevelSystem(const FineOperatorType& fineOperator)=0;
125
129 virtual void calculateCoarseEntries(const FineOperatorType& fineOperator) = 0;
130
132 virtual LevelTransferPolicyCpr* clone() const =0;
133
136
137 protected:
143 std::shared_ptr<CoarseOperatorType> operator_;
144};
145
151template<class O, class C>
153 : public LevelTransferPolicyCpr<O,O>
154{
155 typedef Dune::Amg::AggregatesMap<typename O::matrix_type::size_type> AggregatesMap;
156public:
158 typedef C Criterion;
159 typedef SequentialInformation ParallelInformation;
160
162 : criterion_(crit)
163 {}
164
165 void createCoarseLevelSystem(const O& fineOperator)
166 {
167 prolongDamp_ = criterion_.getProlongationDampingFactor();
168 GalerkinProduct<ParallelInformation> productBuilder;
169 typedef typename Dune::Amg::MatrixGraph<const typename O::matrix_type> MatrixGraph;
170 typedef typename Dune::Amg::PropertiesGraph<MatrixGraph,Dune::Amg::VertexProperties,
171 Dune::Amg::EdgeProperties,Dune::IdentityMap,Dune::IdentityMap> PropertiesGraph;
172 MatrixGraph mg(fineOperator.getmat());
173 PropertiesGraph pg(mg,Dune::IdentityMap(),Dune::IdentityMap());
174 typedef NegateSet<typename ParallelInformation::OwnerSet> OverlapFlags;
175
176 aggregatesMap_.reset(new AggregatesMap(pg.maxVertex()+1));
177
178 int noAggregates, isoAggregates, oneAggregates, skippedAggregates;
179
180 std::tie(noAggregates, isoAggregates, oneAggregates, skippedAggregates) =
181 aggregatesMap_->buildAggregates(fineOperator.getmat(), pg, criterion_, true);
182 std::cout<<"no aggregates="<<noAggregates<<" iso="<<isoAggregates<<" one="<<oneAggregates<<" skipped="<<skippedAggregates<<std::endl;
183 // misuse coarsener to renumber aggregates
184 Dune::Amg::IndicesCoarsener<Dune::Amg::SequentialInformation,int> renumberer;
185 typedef std::vector<bool>::iterator Iterator;
186 typedef Dune::IteratorPropertyMap<Iterator, Dune::IdentityMap> VisitedMap;
187 std::vector<bool> excluded(fineOperator.getmat().N(), false);
188 VisitedMap vm(excluded.begin(), Dune::IdentityMap());
190
191 std::size_t aggregates = coarsen(renumberer, pinfo, pg, vm,*aggregatesMap_, noAggregates, true);
192 std::vector<bool>& visited=excluded;
193
194 typedef std::vector<bool>::iterator Iterator;
195
196 for(Iterator iter= visited.begin(), end=visited.end();
197 iter != end; ++iter)
198 *iter=false;
199 matrix_.reset(productBuilder.build(mg, vm,
200 SequentialInformation(),
201 *aggregatesMap_,
202 aggregates,
203 OverlapFlags()));
204 productBuilder.calculate(fineOperator.getmat(), *aggregatesMap_, *matrix_, pinfo, OverlapFlags());
205 this->lhs_.resize(this->matrix_->M());
206 this->rhs_.resize(this->matrix_->N());
207 this->operator_.reset(new O(*matrix_));
208 }
209
210 void moveToCoarseLevel(const typename FatherType::FineRangeType& fineRhs)
211 {
212 Transfer<std::size_t,typename FatherType::FineRangeType,ParallelInformation>
213 ::restrictVector(*aggregatesMap_, this->rhs_, fineRhs, ParallelInformation());
214 this->lhs_=0;
215 }
216
218 {
219 Transfer<std::size_t,typename FatherType::FineRangeType,ParallelInformation>
220 ::prolongateVector(*aggregatesMap_, this->lhs_, fineLhs,
221 prolongDamp_, ParallelInformation());
222 }
223
225 {
226 return new AggregationLevelTransferPolicyCpr(*this);
227 }
228
229private:
230 // trailing return type with decltype used for detecting the signature of coarsen member function by overloading this coarsen function
231 template <typename RN, typename PI, typename PG, typename VM, typename AM>
232 auto coarsen(RN& renumberer,
233 PI& pinfo,
234 PG& pg,
235 VM& vm,
236 AM& aggregatesMap,
237 int noAggregates,
238 bool useFixedOrder) ->
239 decltype(renumberer.coarsen(pinfo, pg, vm, aggregatesMap, pinfo, noAggregates, useFixedOrder))
240 {
241 return renumberer.coarsen(pinfo, pg, vm, aggregatesMap, pinfo, noAggregates, useFixedOrder);
242 }
243
244 template <typename RN, typename PI, typename PG, typename VM, typename AM>
245 auto coarsen(RN& renumberer,
246 PI& pinfo,
247 PG& pg,
248 VM& vm,
249 AM& aggregatesMap,
250 int noAggregates, ...)
251 {
252 return renumberer.coarsen(pinfo, pg, vm, aggregatesMap, pinfo, noAggregates);
253 }
254
255 typename O::matrix_type::field_type prolongDamp_;
256 std::shared_ptr<AggregatesMap> aggregatesMap_;
257 Criterion criterion_;
258 std::shared_ptr<typename O::matrix_type> matrix_;
259};
260
267template<class O, class S, class C>
269{
270public:
272 typedef O Operator;
274 typedef typename O::range_type X;
276 typedef C Criterion;
278 typedef S Smoother;
280 typedef typename Dune::Amg::SmootherTraits<S>::Arguments SmootherArgs;
282 typedef AMG<Operator,X,Smoother> AMGType;
289 : smootherArgs_(args), criterion_(c)
290 {}
293 : coarseOperator_(other.coarseOperator_), smootherArgs_(other.smootherArgs_),
294 criterion_(other.criterion_)
295 {}
296private:
303 struct AMGInverseOperator : public InverseOperator<X,X>
304 {
305 AMGInverseOperator(const typename AMGType::Operator& op,
306 const Criterion& crit,
307 const typename AMGType::SmootherArgs& args)
308 : amg_(op, crit,args), first_(true)
309 {}
310
311 void apply(X& x, X& b, [[maybe_unused]] double reduction, [[maybe_unused]] InverseOperatorResult& res)
312 {
313 if(first_)
314 {
315 amg_.pre(x,b);
316 first_=false;
317 x_=x;
318 }
319 amg_.apply(x,b);
320 }
321
322 void apply(X& x, X& b, InverseOperatorResult& res)
323 {
324 return apply(x,b,1e-8,res);
325 }
326
327 virtual SolverCategory::Category category() const
328 {
329 return amg_.category();
330 }
331
332 ~AMGInverseOperator()
333 {
334 if(!first_)
335 amg_.post(x_);
336 }
337 AMGInverseOperator(const AMGInverseOperator& other)
338 : x_(other.x_), amg_(other.amg_), first_(other.first_)
339 {
340 }
341 private:
342 X x_;
343 AMGType amg_;
344 bool first_;
345 };
346
347public:
349 typedef AMGInverseOperator CoarseLevelSolver;
350
358 template<class P>
360 {
361 coarseOperator_=transferPolicy.getCoarseLevelOperator();
362 AMGInverseOperator* inv = new AMGInverseOperator(*coarseOperator_,
363 criterion_,
364 smootherArgs_);
365
366 return inv; //std::shared_ptr<InverseOperator<X,X> >(inv);
367
368 }
369
370private:
372 std::shared_ptr<Operator> coarseOperator_;
374 SmootherArgs smootherArgs_;
376 Criterion criterion_;
377};
378
384template<class FO, class CSP, class S>
386 public Preconditioner<typename FO::domain_type, typename FO::range_type>
387{
388public:
392 typedef typename CoarseLevelSolverPolicy::CoarseLevelSolver CoarseLevelSolver;
401 typedef typename FineOperatorType::range_type FineRangeType;
405 typedef typename FineOperatorType::domain_type FineDomainType;
410 typedef typename CSP::Operator CoarseOperatorType;
414 typedef typename CoarseOperatorType::range_type CoarseRangeType;
418 typedef typename CoarseOperatorType::domain_type CoarseDomainType;
422 typedef S SmootherType;
423
439 std::shared_ptr<SmootherType> smoother,
441 CoarseOperatorType>& policy,
442 CoarseLevelSolverPolicy& coarsePolicy,
443 std::size_t preSteps=1, std::size_t postSteps=1)
444 : operator_(&op), smoother_(smoother),
445 preSteps_(preSteps), postSteps_(postSteps)
446 {
447 policy_ = policy.clone();
448 policy_->createCoarseLevelSystem(*operator_);
449 coarseSolver_=coarsePolicy.createCoarseLevelSolver(*policy_);
450 }
451
453 : operator_(other.operator_), coarseSolver_(new CoarseLevelSolver(*other.coarseSolver_)),
454 smoother_(other.smoother_), policy_(other.policy_->clone()),
455 preSteps_(other.preSteps_), postSteps_(other.postSteps_)
456 {}
457
459 {
460 // Each instance has its own policy.
461 delete policy_;
462 delete coarseSolver_;
463 }
464
466 std::shared_ptr<SmootherType> smoother,
467 CoarseLevelSolverPolicy& coarsePolicy)
468 {
469 updatePreconditioner(smoother, coarsePolicy);
470 }
471
472 void updatePreconditioner(std::shared_ptr<SmootherType> smoother,
473 CoarseLevelSolverPolicy& coarsePolicy)
474 {
475 //assume new matrix is not reallocated the new precondition should anyway be made
476 smoother_ = smoother;
477 if (coarseSolver_) {
478 policy_->calculateCoarseEntries(*operator_);
479 coarsePolicy.setCoarseOperator(*policy_);
480 coarseSolver_->updatePreconditioner();
481 } else {
482 // we should probably not be heere
483 policy_->createCoarseLevelSystem(*operator_);
484 coarseSolver_ = coarsePolicy.createCoarseLevelSolver(*policy_);
485 }
486 }
487
489 {
490 smoother_->pre(x,b);
491 }
492
493 void post([[maybe_unused]] FineDomainType& x)
494 {
495 }
496
498 {
499 FineDomainType u(v);
500 FineRangeType rhs(d);
501 LevelContext context;
502 SequentialInformation info;
503 context.pinfo=&info;
504 context.lhs=&u;
505 context.update=&v;
506 context.smoother=smoother_;
507 context.rhs=&rhs;
508 context.matrix=operator_;
509 // Presmoothing
510 presmooth(context, preSteps_);
511 //Coarse grid correction
512 policy_->moveToCoarseLevel(*context.rhs);
514 coarseSolver_->apply(policy_->getCoarseLevelLhs(), policy_->getCoarseLevelRhs(), res);
515 *context.lhs=0;
516 policy_->moveToFineLevel(*context.lhs);
517 *context.update += *context.lhs;
518 // Postsmoothing
519 postsmooth(context, postSteps_);
520
521 }
522// //! Category of the preconditioner (see SolverCategory::Category)
523 virtual SolverCategory::Category category() const
524 {
525 return SolverCategory::sequential;
526 }
527
528private:
532 struct LevelContext
533 {
535 typedef S SmootherType;
537 std::shared_ptr<SmootherType> smoother;
539 FineDomainType* lhs;
540 /*
541 * @brief The right hand side holding the current residual.
542 *
543 * This is passed to the smoother as the right hand side.
544 */
545 FineRangeType* rhs;
551 FineDomainType* update;
553 SequentialInformation* pinfo;
559 const FineOperatorType* matrix;
560 };
561 const FineOperatorType* operator_;
563 CoarseLevelSolver* coarseSolver_;
565 std::shared_ptr<S> smoother_;
567 LevelTransferPolicyCpr<FO,typename CSP::Operator>* policy_;
569 std::size_t preSteps_;
571 std::size_t postSteps_;
572};
573}// end namespace Amg
574}// end namespace Dune
575
577#endif
A LeveTransferPolicy that used aggregation to construct the coarse level system.
Definition: twolevelmethodcpr.hh:154
SequentialInformation ParallelInformation
Definition: twolevelmethodcpr.hh:159
void moveToFineLevel(typename FatherType::FineDomainType &fineLhs)
Updates the fine level linear system after the correction of the coarse levels system.
Definition: twolevelmethodcpr.hh:217
void moveToCoarseLevel(const typename FatherType::FineRangeType &fineRhs)
Definition: twolevelmethodcpr.hh:210
AggregationLevelTransferPolicyCpr(const Criterion &crit)
Definition: twolevelmethodcpr.hh:161
LevelTransferPolicyCpr< O, O > FatherType
Definition: twolevelmethodcpr.hh:157
void createCoarseLevelSystem(const O &fineOperator)
Algebraically creates the coarse level system.
Definition: twolevelmethodcpr.hh:165
C Criterion
Definition: twolevelmethodcpr.hh:158
AggregationLevelTransferPolicyCpr * clone() const
Clone the current object.
Definition: twolevelmethodcpr.hh:224
Abstract base class for transfer between levels and creation of the coarse level system.
Definition: twolevelmethodcpr.hh:44
CoarseOperatorType::range_type CoarseRangeType
The type of the range of the coarse level operator.
Definition: twolevelmethodcpr.hh:67
CoarseDomainType & getCoarseLevelLhs()
Get the coarse level left hand side.
Definition: twolevelmethodcpr.hh:93
virtual LevelTransferPolicyCpr * clone() const =0
Clone the current object.
FO FineOperatorType
The linear operator of the finel level system. Has to be derived from AssembledLinearOperator.
Definition: twolevelmethodcpr.hh:50
virtual void createCoarseLevelSystem(const FineOperatorType &fineOperator)=0
Algebraically creates the coarse level system.
virtual void moveToCoarseLevel(const FineRangeType &fineRhs)=0
Transfers the data to the coarse level.
CoarseOperatorType::domain_type CoarseDomainType
The type of the domain of the coarse level operator.
Definition: twolevelmethodcpr.hh:71
FineOperatorType::range_type FineRangeType
The type of the range of the fine level operator.
Definition: twolevelmethodcpr.hh:54
CoarseRangeType & getCoarseLevelRhs()
Get the coarse level right hand side.
Definition: twolevelmethodcpr.hh:84
virtual ~LevelTransferPolicyCpr()
Destructor.
Definition: twolevelmethodcpr.hh:135
virtual void moveToFineLevel(FineDomainType &fineLhs)=0
Updates the fine level linear system after the correction of the coarse levels system.
virtual void calculateCoarseEntries(const FineOperatorType &fineOperator)=0
???.
CoarseDomainType lhs_
The coarse level lhs.
Definition: twolevelmethodcpr.hh:141
CoarseRangeType rhs_
The coarse level rhs.
Definition: twolevelmethodcpr.hh:139
FineOperatorType::domain_type FineDomainType
The type of the domain of the fine level operator.
Definition: twolevelmethodcpr.hh:58
CO CoarseOperatorType
The linear operator of the finel level system. Has to be derived from AssembledLinearOperator.
Definition: twolevelmethodcpr.hh:63
std::shared_ptr< CoarseOperatorType > & getCoarseLevelOperator()
Get the coarse level operator.
Definition: twolevelmethodcpr.hh:76
std::shared_ptr< CoarseOperatorType > operator_
the coarse level linear operator.
Definition: twolevelmethodcpr.hh:143
A policy class for solving the coarse level system using one step of AMG.
Definition: twolevelmethodcpr.hh:269
AMG< Operator, X, Smoother > AMGType
The type of the AMG construct on the coarse level.
Definition: twolevelmethodcpr.hh:282
OneStepAMGCoarseSolverPolicyCpr(const OneStepAMGCoarseSolverPolicyCpr &other)
Copy constructor.
Definition: twolevelmethodcpr.hh:292
AMGInverseOperator CoarseLevelSolver
The type of solver constructed for the coarse level.
Definition: twolevelmethodcpr.hh:349
S Smoother
The type of the smoother used in AMG.
Definition: twolevelmethodcpr.hh:278
C Criterion
The type of the crition used for the aggregation within AMG.
Definition: twolevelmethodcpr.hh:276
Dune::Amg::SmootherTraits< S >::Arguments SmootherArgs
The type of the arguments used for constructing the smoother.
Definition: twolevelmethodcpr.hh:280
CoarseLevelSolver * createCoarseLevelSolver(P &transferPolicy)
Constructs a coarse level solver.
Definition: twolevelmethodcpr.hh:359
O::range_type X
The type of the range and domain of the operator.
Definition: twolevelmethodcpr.hh:274
OneStepAMGCoarseSolverPolicyCpr(const SmootherArgs &args, const Criterion &c)
Constructs the coarse solver policy.
Definition: twolevelmethodcpr.hh:288
O Operator
The type of the linear operator used.
Definition: twolevelmethodcpr.hh:272
Definition: twolevelmethodcpr.hh:387
void updatePreconditioner(FineOperatorType &, std::shared_ptr< SmootherType > smoother, CoarseLevelSolverPolicy &coarsePolicy)
Definition: twolevelmethodcpr.hh:465
virtual SolverCategory::Category category() const
Definition: twolevelmethodcpr.hh:523
S SmootherType
The type of the fine level smoother.
Definition: twolevelmethodcpr.hh:422
FineOperatorType::range_type FineRangeType
The type of the range of the fine level operator.
Definition: twolevelmethodcpr.hh:401
TwoLevelMethodCpr(const FineOperatorType &op, std::shared_ptr< SmootherType > smoother, const LevelTransferPolicyCpr< FineOperatorType, CoarseOperatorType > &policy, CoarseLevelSolverPolicy &coarsePolicy, std::size_t preSteps=1, std::size_t postSteps=1)
Constructs a two level method.
Definition: twolevelmethodcpr.hh:438
FineOperatorType::domain_type FineDomainType
The type of the domain of the fine level operator.
Definition: twolevelmethodcpr.hh:405
void apply(FineDomainType &v, const FineRangeType &d)
Definition: twolevelmethodcpr.hh:497
~TwoLevelMethodCpr()
Definition: twolevelmethodcpr.hh:458
void pre(FineDomainType &x, FineRangeType &b)
Definition: twolevelmethodcpr.hh:488
CoarseOperatorType::domain_type CoarseDomainType
The type of the domain of the coarse level operator.
Definition: twolevelmethodcpr.hh:418
TwoLevelMethodCpr(const TwoLevelMethodCpr &other)
Definition: twolevelmethodcpr.hh:452
CSP::Operator CoarseOperatorType
The linear operator of the finel level system. Has to be derived from AssembledLinearOperator.
Definition: twolevelmethodcpr.hh:410
void post(FineDomainType &x)
Definition: twolevelmethodcpr.hh:493
CSP CoarseLevelSolverPolicy
The type of the policy for constructing the coarse level solver.
Definition: twolevelmethodcpr.hh:390
CoarseLevelSolverPolicy::CoarseLevelSolver CoarseLevelSolver
The type of the coarse level solver.
Definition: twolevelmethodcpr.hh:392
CoarseOperatorType::range_type CoarseRangeType
The type of the range of the coarse level operator.
Definition: twolevelmethodcpr.hh:414
FO FineOperatorType
The linear operator of the finel level system. Has to be derived from AssembledLinearOperator.
Definition: twolevelmethodcpr.hh:397
void updatePreconditioner(std::shared_ptr< SmootherType > smoother, CoarseLevelSolverPolicy &coarsePolicy)
Definition: twolevelmethodcpr.hh:472
Definition: SupportsFaceTag.hpp:27
Dune::InverseOperatorResult InverseOperatorResult
Definition: BdaBridge.hpp:32