41 template<
class M,
class X,
class S,
class P,
class K,
class A>
54 template<
class M,
class X,
class S,
class PI=SequentialInformation,
55 class A=std::allocator<X> >
58 template<
class M1,
class X1,
class S1,
class P1,
class K1,
class A1>
117 std::size_t preSmoothingSteps,
118 std::size_t postSmoothingSteps,
155 std::
size_t preSmoothingSteps,
156 std::
size_t postSmoothingSteps,
199 std::
size_t levels();
201 std::
size_t maxlevels();
230 void createHierarchies(C& criterion,
Operator& matrix,
256 typename OperatorHierarchy::RedistributeInfoList::const_iterator
redist;
260 typename OperatorHierarchy::AggregatesMapList::const_iterator
aggregates;
284 void mgc(LevelContext& levelContext);
294 void moveToFineLevel(LevelContext& levelContext,
bool processedFineLevel);
300 bool moveToCoarseLevel(LevelContext& levelContext);
306 void initIteratorsWithFineLevel(LevelContext& levelContext);
325 typedef typename ScalarProductChooser::ScalarProduct ScalarProduct;
332 std::size_t preSteps_;
334 std::size_t postSteps_;
335 bool buildHierarchy_;
337 bool coarsesolverconverged;
340 std::size_t verbosity_;
343 template<
class M,
class X,
class S,
class PI,
class A>
345 : matrices_(amg.matrices_), smootherArgs_(amg.smootherArgs_),
346 smoothers_(amg.smoothers_), solver_(amg.solver_),
347 rhs_(), lhs_(), update_(),
348 scalarProduct_(amg.scalarProduct_), gamma_(amg.gamma_),
349 preSteps_(amg.preSteps_), postSteps_(amg.postSteps_),
350 buildHierarchy_(amg.buildHierarchy_),
351 additive(amg.additive), coarsesolverconverged(amg.coarsesolverconverged),
352 coarseSmoother_(amg.coarseSmoother_), verbosity_(amg.verbosity_)
362 template<
class M,
class X,
class S,
class PI,
class A>
365 std::size_t gamma, std::size_t preSmoothingSteps,
366 std::size_t postSmoothingSteps,
bool additive_)
367 : matrices_(&matrices), smootherArgs_(smootherArgs),
369 rhs_(), lhs_(), update_(), scalarProduct_(0),
370 gamma_(gamma), preSteps_(preSmoothingSteps), postSteps_(postSmoothingSteps), buildHierarchy_(false),
371 additive(additive_), coarsesolverconverged(true),
372 coarseSmoother_(), verbosity_(2)
374 assert(matrices_->isBuilt());
377 matrices_->coarsenSmoother(*smoothers_, smootherArgs_);
380 template<
class M,
class X,
class S,
class PI,
class A>
384 : matrices_(&matrices), smootherArgs_(smootherArgs),
386 rhs_(), lhs_(), update_(), scalarProduct_(0),
387 gamma_(parms.getGamma()), preSteps_(parms.getNoPreSmoothSteps()),
388 postSteps_(parms.getNoPostSmoothSteps()), buildHierarchy_(false),
389 additive(parms.getAdditive()), coarsesolverconverged(true),
390 coarseSmoother_(), verbosity_(parms.debugLevel())
392 assert(matrices_->isBuilt());
395 matrices_->coarsenSmoother(*smoothers_, smootherArgs_);
398 template<
class M,
class X,
class S,
class PI,
class A>
403 std::size_t gamma, std::size_t preSmoothingSteps,
404 std::size_t postSmoothingSteps,
407 : smootherArgs_(smootherArgs),
409 rhs_(), lhs_(), update_(), scalarProduct_(), gamma_(gamma),
410 preSteps_(preSmoothingSteps), postSteps_(postSmoothingSteps), buildHierarchy_(true),
411 additive(additive_), coarsesolverconverged(true),
412 coarseSmoother_(), verbosity_(criterion.debugLevel())
415 "Matrix and Solver must match in terms of category!");
419 createHierarchies(criterion,
const_cast<Operator&
>(matrix), pinfo);
422 template<
class M,
class X,
class S,
class PI,
class A>
428 : smootherArgs_(smootherArgs),
430 rhs_(), lhs_(), update_(), scalarProduct_(),
431 gamma_(criterion.getGamma()), preSteps_(criterion.getNoPreSmoothSteps()),
432 postSteps_(criterion.getNoPostSmoothSteps()), buildHierarchy_(true),
433 additive(criterion.getAdditive()), coarsesolverconverged(true),
434 coarseSmoother_(), verbosity_(criterion.debugLevel())
437 "Matrix and Solver must match in terms of category!");
441 createHierarchies(criterion,
const_cast<Operator&
>(matrix), pinfo);
445 template<
class M,
class X,
class S,
class PI,
class A>
448 if(buildHierarchy_) {
452 coarseSmoother_.reset();
465 template<
class M,
class X,
class S,
class PI,
class A>
467 void AMG<M,X,S,PI,A>::createHierarchies(C& criterion, Operator& matrix,
471 matrices_.reset(
new OperatorHierarchy(matrix, pinfo));
473 matrices_->template build<NegateSet<typename PI::OwnerSet> >(criterion);
476 matrices_->coarsenSmoother(*smoothers_, smootherArgs_);
478 if(buildHierarchy_ && matrices_->levels()==matrices_->maxlevels()) {
480 SmootherArgs sargs(smootherArgs_);
481 sargs.iterations = 1;
484 cargs.setArgs(sargs);
485 if(matrices_->redistributeInformation().back().isSetup()) {
487 cargs.setMatrix(matrices_->matrices().coarsest().getRedistributed().getmat());
488 cargs.setComm(matrices_->parallelInformation().coarsest().getRedistributed());
490 cargs.setMatrix(matrices_->matrices().coarsest()->getmat());
491 cargs.setComm(*matrices_->parallelInformation().coarsest());
495 scalarProduct_.reset(ScalarProductChooser::construct(cargs.getComm()));
497#if HAVE_SUPERLU || HAVE_UMFPACK
499#define DIRECTSOLVER UMFPack
501#define DIRECTSOLVER SuperLU
504 if(is_same<ParallelInformation,SequentialInformation>::value
505 || matrices_->parallelInformation().coarsest()->communicator().size()==1
506 || (matrices_->parallelInformation().coarsest().isRedistributed()
507 && matrices_->parallelInformation().coarsest().getRedistributed().communicator().size()==1
508 && matrices_->parallelInformation().coarsest().getRedistributed().communicator().size()>0)) {
509 if(matrices_->parallelInformation().coarsest().isRedistributed())
511 if(matrices_->matrices().coarsest().getRedistributed().getmat().N()>0)
513 solver_.reset(
new DIRECTSOLVER<typename M::matrix_type>(matrices_->matrices().coarsest().getRedistributed().getmat(),
false,
false));
517 solver_.reset(
new DIRECTSOLVER<typename M::matrix_type>(matrices_->matrices().coarsest()->getmat(),
false,
false));
518 if(verbosity_>0 && matrices_->parallelInformation().coarsest()->communicator().rank()==0)
519 std::cout<<
"Using a direct coarse solver (" <<
static_cast< DIRECTSOLVER<typename M::matrix_type>*
>(solver_.get())->name() <<
")" << std::endl;
524 if(matrices_->parallelInformation().coarsest().isRedistributed())
526 if(matrices_->matrices().coarsest().getRedistributed().getmat().N()>0)
528 solver_.reset(
new BiCGSTABSolver<X>(
const_cast<M&
>(matrices_->matrices().coarsest().getRedistributed()),
530 *coarseSmoother_, 1E-2, 1000, 0));
534 solver_.reset(
new BiCGSTABSolver<X>(
const_cast<M&
>(*matrices_->matrices().coarsest()),
536 *coarseSmoother_, 1E-2, 1000, 0));
540 if(verbosity_>0 && matrices_->parallelInformation().finest()->communicator().rank()==0)
541 std::cout<<
"Building hierarchy of "<<matrices_->maxlevels()<<
" levels "
542 <<
"(inclusive coarse solver) took "<<watch.elapsed()<<
" seconds."<<std::endl;
546 template<
class M,
class X,
class S,
class PI,
class A>
553 typedef typename M::matrix_type
Matrix;
560 const Matrix& mat=matrices_->matrices().finest()->getmat();
561 for(RowIter row=mat.begin(); row!=mat.end(); ++row) {
562 bool isDirichlet =
true;
563 bool hasDiagonal =
false;
565 for(ColIter col=row->begin(); col!=row->end(); ++col) {
566 if(row.index()==col.index()) {
574 if(isDirichlet && hasDiagonal)
575 diagonal.solve(x[row.index()], b[row.index()]);
578 if(smoothers_->levels()>0)
579 smoothers_->finest()->pre(x,b);
582 matrices_->parallelInformation().coarsest()->copyOwnerToAll(x,x);
595 matrices_->coarsenVector(*rhs_);
596 matrices_->coarsenVector(*lhs_);
597 matrices_->coarsenVector(*update_);
603 Iterator coarsest = smoothers_->
coarsest();
604 Iterator smoother = smoothers_->finest();
605 RIterator rhs = rhs_->finest();
606 DIterator lhs = lhs_->finest();
607 if(smoothers_->levels()>0) {
609 assert(lhs_->levels()==rhs_->levels());
610 assert(smoothers_->levels()==lhs_->levels() || matrices_->levels()==matrices_->maxlevels());
611 assert(smoothers_->levels()+1==lhs_->levels() || matrices_->levels()<matrices_->maxlevels());
613 if(smoother!=coarsest)
614 for(++smoother, ++lhs, ++rhs; smoother != coarsest; ++smoother, ++lhs, ++rhs)
615 smoother->pre(*lhs,*rhs);
616 smoother->pre(*lhs,*rhs);
626 template<
class M,
class X,
class S,
class PI,
class A>
629 return matrices_->levels();
631 template<
class M,
class X,
class S,
class PI,
class A>
632 std::size_t AMG<M,X,S,PI,A>::maxlevels()
634 return matrices_->maxlevels();
638 template<
class M,
class X,
class S,
class PI,
class A>
641 LevelContext levelContext;
649 initIteratorsWithFineLevel(levelContext);
652 *levelContext.lhs = v;
653 *levelContext.rhs = d;
654 *levelContext.update=0;
655 levelContext.level=0;
659 if(postSteps_==0||matrices_->maxlevels()==1)
660 levelContext.pinfo->copyOwnerToAll(*levelContext.update, *levelContext.update);
662 v=*levelContext.update;
667 template<
class M,
class X,
class S,
class PI,
class A>
670 levelContext.smoother = smoothers_->finest();
671 levelContext.matrix = matrices_->matrices().finest();
672 levelContext.pinfo = matrices_->parallelInformation().finest();
673 levelContext.redist =
674 matrices_->redistributeInformation().begin();
675 levelContext.aggregates = matrices_->aggregatesMaps().begin();
676 levelContext.lhs = lhs_->finest();
677 levelContext.update = update_->finest();
678 levelContext.rhs = rhs_->finest();
681 template<
class M,
class X,
class S,
class PI,
class A>
683 ::moveToCoarseLevel(LevelContext& levelContext)
686 bool processNextLevel=
true;
688 if(levelContext.redist->isSetup()) {
689 levelContext.redist->redistribute(
static_cast<const Range&
>(*levelContext.rhs),
690 levelContext.rhs.getRedistributed());
691 processNextLevel = levelContext.rhs.getRedistributed().size()>0;
692 if(processNextLevel) {
695 ++levelContext.pinfo;
696 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
697 ::restrictVector(*(*levelContext.aggregates), *levelContext.rhs,
698 static_cast<const Range&
>(fineRhs.getRedistributed()),
699 *levelContext.pinfo);
704 ++levelContext.pinfo;
705 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
706 ::restrictVector(*(*levelContext.aggregates),
707 *levelContext.rhs,
static_cast<const Range&
>(*fineRhs),
708 *levelContext.pinfo);
711 if(processNextLevel) {
714 ++levelContext.update;
715 ++levelContext.matrix;
716 ++levelContext.level;
717 ++levelContext.redist;
719 if(levelContext.matrix != matrices_->matrices().coarsest() || matrices_->levels()<matrices_->maxlevels()) {
721 ++levelContext.smoother;
722 ++levelContext.aggregates;
725 *levelContext.update=0;
727 return processNextLevel;
730 template<
class M,
class X,
class S,
class PI,
class A>
732 ::moveToFineLevel(LevelContext& levelContext,
bool processNextLevel)
734 if(processNextLevel) {
735 if(levelContext.matrix != matrices_->matrices().coarsest() || matrices_->levels()<matrices_->maxlevels()) {
737 --levelContext.smoother;
738 --levelContext.aggregates;
740 --levelContext.redist;
741 --levelContext.level;
743 --levelContext.matrix;
747 --levelContext.pinfo;
749 if(levelContext.redist->isSetup()) {
751 levelContext.lhs.getRedistributed()=0;
752 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
753 ::prolongateVector(*(*levelContext.aggregates), *levelContext.update, *levelContext.lhs,
754 levelContext.lhs.getRedistributed(),
755 matrices_->getProlongationDampingFactor(),
756 *levelContext.pinfo, *levelContext.redist);
759 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
760 ::prolongateVector(*(*levelContext.aggregates), *levelContext.update, *levelContext.lhs,
761 matrices_->getProlongationDampingFactor(),
762 *levelContext.pinfo);
766 if(processNextLevel) {
767 --levelContext.update;
771 *levelContext.update += *levelContext.lhs;
774 template<
class M,
class X,
class S,
class PI,
class A>
777 return IsDirectSolver< CoarseSolver>::value;
780 template<
class M,
class X,
class S,
class PI,
class A>
782 if(levelContext.matrix == matrices_->matrices().coarsest() && levels()==maxlevels()) {
786 if(levelContext.redist->isSetup()) {
787 levelContext.redist->redistribute(*levelContext.rhs, levelContext.rhs.getRedistributed());
788 if(levelContext.rhs.getRedistributed().size()>0) {
790 levelContext.pinfo.getRedistributed().copyOwnerToAll(levelContext.rhs.getRedistributed(),
791 levelContext.rhs.getRedistributed());
792 solver_->apply(levelContext.update.getRedistributed(),
793 levelContext.rhs.getRedistributed(), res);
795 levelContext.redist->redistributeBackward(*levelContext.update, levelContext.update.getRedistributed());
796 levelContext.pinfo->copyOwnerToAll(*levelContext.update, *levelContext.update);
798 levelContext.pinfo->copyOwnerToAll(*levelContext.rhs, *levelContext.rhs);
799 solver_->apply(*levelContext.update, *levelContext.rhs, res);
803 coarsesolverconverged =
false;
808#ifndef DUNE_AMG_NO_COARSEGRIDCORRECTION
809 bool processNextLevel = moveToCoarseLevel(levelContext);
811 if(processNextLevel) {
813 for(std::size_t i=0; i<gamma_; i++)
817 moveToFineLevel(levelContext, processNextLevel);
822 if(levelContext.matrix == matrices_->matrices().finest()) {
823 coarsesolverconverged = matrices_->parallelInformation().finest()->communicator().prod(coarsesolverconverged);
824 if(!coarsesolverconverged)
825 DUNE_THROW(MathError,
"Coarse solver did not converge");
833 template<
class M,
class X,
class S,
class PI,
class A>
834 void AMG<M,X,S,PI,A>::additiveMgc(){
837 typename ParallelInformationHierarchy::Iterator pinfo=matrices_->parallelInformation().finest();
840 typename OperatorHierarchy::AggregatesMapList::const_iterator aggregates=matrices_->aggregatesMaps().begin();
844 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
845 ::restrictVector(*(*aggregates), *rhs,
static_cast<const Range&
>(*fineRhs), *pinfo);
851 lhs = lhs_->finest();
854 for(rhs=rhs_->finest(); rhs != rhs_->coarsest(); ++lhs, ++rhs, ++smoother) {
857 smoother->apply(*lhs, *rhs);
861#ifndef DUNE_AMG_NO_COARSEGRIDCORRECTION
862 InverseOperatorResult res;
863 pinfo->copyOwnerToAll(*rhs, *rhs);
864 solver_->apply(*lhs, *rhs, res);
867 DUNE_THROW(MathError,
"Coarse solver did not converge");
876 Transfer<typename OperatorHierarchy::AggregatesMap::AggregateDescriptor,Range,ParallelInformation>
877 ::prolongateVector(*(*aggregates), *coarseLhs, *lhs, 1.0, *pinfo);
883 template<
class M,
class X,
class S,
class PI,
class A>
890 Iterator coarsest = smoothers_->
coarsest();
891 Iterator smoother = smoothers_->finest();
892 DIterator lhs = lhs_->finest();
893 if(smoothers_->levels()>0) {
894 if(smoother != coarsest || matrices_->levels()<matrices_->maxlevels())
895 smoother->post(*lhs);
896 if(smoother!=coarsest)
897 for(++smoother, ++lhs; smoother != coarsest; ++smoother, ++lhs)
898 smoother->post(*lhs);
899 smoother->post(*lhs);
912 template<
class M,
class X,
class S,
class PI,
class A>
916 matrices_->getCoarsestAggregatesOnFinest(cont);
Parallel algebraic multigrid based on agglomeration.
Definition: amg.hh:57
LevelIterator< Hierarchy< Range, A >, Range > Iterator
Type of the mutable iterator.
Definition: hierarchy.hh:258
LevelIterator< const Hierarchy< MatrixOperator, Allocator >, const MatrixOperator > ConstIterator
Type of the const iterator.
Definition: hierarchy.hh:261
an algebraic multigrid method using a Krylov-cycle.
Definition: kamg.hh:134
Two grid operator for AMG with Krylov cycle.
Definition: kamg.hh:31
The hierarchies build by the coarsening process.
Definition: hierarchy.hh:318
All parameters for AMG.
Definition: parameters.hh:391
Enables iteration over all entities of a given codimension and level of a grid. See also the document...
Definition: leveliterator.hh:31
A generic dynamic dense matrix.
Definition: matrix.hh:25
T::field_type field_type
Export the type representing the underlying field.
Definition: matrix.hh:29
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:53
T block_type
Export the type representing the components.
Definition: matrix.hh:32
The negation of a set. An item is contained in the set if and only if it is not contained in the nega...
Definition: enumset.hh:95
Base class for matrix free definition of preconditioners.
Definition: preconditioner.hh:26
ConstIterator class for sequential access.
Definition: vbvector.hh:647
A reference counting smart pointer.
Definition: shared_ptr.hh:64
A few common exception classes.
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
#define DUNE_DEPRECATED
Mark some entity as deprecated.
Definition: deprecated.hh:84
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
void pre(Domain &x, Range &b)
Prepare the preconditioner.
Definition: amg.hh:547
Hierarchy< Domain, A >::Iterator update
The iterator over the updates.
Definition: amg.hh:268
Hierarchy< Range, A >::Iterator rhs
The iterator over the right hand sided.
Definition: amg.hh:272
bool usesDirectCoarseLevelSolver() const
Check whether the coarse solver used is a direct solver.
Definition: amg.hh:775
AMG(const OperatorHierarchy &matrices, CoarseSolver &coarseSolver, const SmootherArgs &smootherArgs, std::size_t gamma, std::size_t preSmoothingSteps, std::size_t postSmoothingSteps, bool additive=false) DUNE_DEPRECATED
Construct a new amg with a specific coarse solver.
Definition: amg.hh:363
X Domain
The domain type.
Definition: amg.hh:79
ParallelInformationHierarchy::Iterator pinfo
The iterator over the parallel information.
Definition: amg.hh:252
OperatorHierarchy::AggregatesMapList::const_iterator aggregates
The iterator over the aggregates maps.
Definition: amg.hh:260
SmootherTraits< Smoother >::Arguments SmootherArgs
The argument type for the construction of the smoother.
Definition: amg.hh:92
S Smoother
The type of the smoother.
Definition: amg.hh:89
Hierarchy< Smoother, A >::Iterator smoother
The iterator over the smoothers.
Definition: amg.hh:244
M Operator
The matrix operator type.
Definition: amg.hh:65
OperatorHierarchy::ParallelMatrixHierarchy::ConstIterator matrix
The iterator over the matrices.
Definition: amg.hh:248
OperatorHierarchy::RedistributeInfoList::const_iterator redist
The iterator over the redistribution information.
Definition: amg.hh:256
X Range
The range type.
Definition: amg.hh:81
void presmooth(LevelContext &levelContext, size_t steps)
Apply pre smoothing on the current level.
Definition: smoother.hh:408
void getCoarsestAggregateNumbers(std::vector< std::size_t, A1 > &cont)
Get the aggregate number of each unknown on the coarsest level.
Definition: amg.hh:914
Hierarchy< Domain, A >::Iterator lhs
The iterator over the left hand side.
Definition: amg.hh:264
const void * Arguments
A type holding all the arguments needed to call the constructor.
Definition: construction.hh:45
void recalculateHierarchy()
Recalculate the matrix hierarchy.
Definition: amg.hh:211
Iterator coarsest()
Get an iterator positioned at the coarsest level.
Definition: hierarchy.hh:1386
OperatorHierarchy::ParallelInformationHierarchy ParallelInformationHierarchy
The parallal data distribution hierarchy type.
Definition: amg.hh:76
InverseOperator< X, X > CoarseSolver
the type of the coarse solver.
Definition: amg.hh:83
void post(Domain &x)
Clean up.
Definition: amg.hh:884
void postsmooth(LevelContext &levelContext, size_t steps)
Apply post smoothing on the current level.
Definition: smoother.hh:430
static T * construct(Arguments &args)
Construct an object with the specified arguments.
Definition: construction.hh:53
std::size_t level
The level index.
Definition: amg.hh:276
void apply(Domain &v, const Range &d)
Apply one step of the preconditioner to the system A(v)=d.
Definition: amg.hh:639
MatrixHierarchy< M, ParallelInformation, A > OperatorHierarchy
The operator hierarchy type.
Definition: amg.hh:74
PI ParallelInformation
The type of the parallel information. Either OwnerOverlapCommunication or another type describing the...
Definition: amg.hh:72
@ category
The solver category.
Definition: amg.hh:96
Provides a classes representing the hierarchies in AMG.
Dune namespace.
Definition: alignment.hh:14
Define base class for scalar product and norm.
Classes for the generic construction and application of the smoothers.
Implementations of the inverse operator interface.
Templates characterizing the type of a solver.
Traits class for getting the attribute class of a smoother.
Definition: smoother.hh:64
Statistics about the application of an inverse operator.
Definition: solver.hh:32
bool converged
True if convergence criterion has been met.
Definition: solver.hh:56
Choose the approriate scalar product for a solver category.
Definition: scalarproducts.hh:76
Classes for using SuperLU with ISTL matrices.
Prolongation and restriction for amg.
Traits for type conversions and type information.
Classes for using UMFPack with ISTL matrices.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentional unused function parameters with.
Definition: unused.hh:18