Loading...
Searching...
No Matches
twolevelmethodcpr.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_ISTL_TWOLEVELMETHODCPR_HH
#define DUNE_ISTL_TWOLEVELMETHODCPR_HH
// NOTE: This file is a modified version of dune/istl/paamg/twolevelmethod.hh from
// dune-istl release 2.6.0. Modifications have been kept as minimal as possible.
#include <dune/istl/operators.hh>
//#include "amg.hh"
//#include"galerkin.hh"
#include <dune/istl/paamg/amg.hh>
#include <dune/istl/paamg/galerkin.hh>
#include <dune/istl/solver.hh>
#include <dune/common/unused.hh>
#include <dune/common/version.hh>
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <memory>
#include <tuple>
#include <vector>
namespace Dune
{
namespace Amg
{
template<class FO, class CO>
class LevelTransferPolicyCpr
{
public:
typedef FO FineOperatorType;
typedef typename FineOperatorType::range_type FineRangeType;
typedef typename FineOperatorType::domain_type FineDomainType;
typedef CO CoarseOperatorType;
typedef typename CoarseOperatorType::range_type CoarseRangeType;
typedef typename CoarseOperatorType::domain_type CoarseDomainType;
std::shared_ptr<CoarseOperatorType>& getCoarseLevelOperator()
{
return operator_;
}
CoarseRangeType& getCoarseLevelRhs()
{
return rhs_;
}
CoarseDomainType& getCoarseLevelLhs()
{
return lhs_;
}
virtual void moveToCoarseLevel(const FineRangeType& fineRhs)=0;
virtual void moveToFineLevel(FineDomainType& fineLhs)=0;
virtual void createCoarseLevelSystem(const FineOperatorType& fineOperator)=0;
virtual void calculateCoarseEntries(const FineOperatorType& fineOperator) = 0;
virtual LevelTransferPolicyCpr* clone() const =0;
virtual ~LevelTransferPolicyCpr(){}
protected:
CoarseRangeType rhs_;
CoarseDomainType lhs_;
std::shared_ptr<CoarseOperatorType> operator_;
};
template<class O, class C>
class AggregationLevelTransferPolicyCpr
: public LevelTransferPolicyCpr<O,O>
{
typedef Dune::Amg::AggregatesMap<typename O::matrix_type::size_type> AggregatesMap;
public:
typedef LevelTransferPolicyCpr<O,O> FatherType;
typedef C Criterion;
typedef SequentialInformation ParallelInformation;
explicit AggregationLevelTransferPolicyCpr(const Criterion& crit)
: criterion_(crit)
{}
void createCoarseLevelSystem(const O& fineOperator)
{
prolongDamp_ = criterion_.getProlongationDampingFactor();
GalerkinProduct<ParallelInformation> productBuilder;
typedef typename Dune::Amg::MatrixGraph<const typename O::matrix_type> MatrixGraph;
typedef typename Dune::Amg::PropertiesGraph<MatrixGraph,Dune::Amg::VertexProperties,
Dune::Amg::EdgeProperties,Dune::IdentityMap,Dune::IdentityMap> PropertiesGraph;
MatrixGraph mg(fineOperator.getmat());
PropertiesGraph pg(mg,Dune::IdentityMap(),Dune::IdentityMap());
typedef NegateSet<typename ParallelInformation::OwnerSet> OverlapFlags;
aggregatesMap_.reset(new AggregatesMap(pg.maxVertex()+1));
int noAggregates, isoAggregates, oneAggregates, skippedAggregates;
std::tie(noAggregates, isoAggregates, oneAggregates, skippedAggregates) =
aggregatesMap_->buildAggregates(fineOperator.getmat(), pg, criterion_, true);
std::cout<<"no aggregates="<<noAggregates<<" iso="<<isoAggregates<<" one="<<oneAggregates<<" skipped="<<skippedAggregates<<std::endl;
// misuse coarsener to renumber aggregates
Dune::Amg::IndicesCoarsener<Dune::Amg::SequentialInformation,int> renumberer;
typedef std::vector<bool>::iterator Iterator;
typedef Dune::IteratorPropertyMap<Iterator, Dune::IdentityMap> VisitedMap;
std::vector<bool> excluded(fineOperator.getmat().N(), false);
VisitedMap vm(excluded.begin(), Dune::IdentityMap());
ParallelInformation pinfo;
std::size_t aggregates = coarsen(renumberer, pinfo, pg, vm,*aggregatesMap_, noAggregates, true);
std::fill(excluded.begin(), excluded.end(), false);
matrix_.reset(productBuilder.build(mg, vm,
SequentialInformation(),
*aggregatesMap_,
aggregates,
OverlapFlags()));
productBuilder.calculate(fineOperator.getmat(), *aggregatesMap_, *matrix_, pinfo, OverlapFlags());
this->lhs_.resize(this->matrix_->M());
this->rhs_.resize(this->matrix_->N());
this->operator_.reset(new O(*matrix_));
}
void moveToCoarseLevel(const typename FatherType::FineRangeType& fineRhs)
{
Transfer<std::size_t,typename FatherType::FineRangeType,ParallelInformation>
::restrictVector(*aggregatesMap_, this->rhs_, fineRhs, ParallelInformation());
this->lhs_=0;
}
void moveToFineLevel(typename FatherType::FineDomainType& fineLhs)
{
Transfer<std::size_t,typename FatherType::FineRangeType,ParallelInformation>
::prolongateVector(*aggregatesMap_, this->lhs_, fineLhs,
prolongDamp_, ParallelInformation());
}
AggregationLevelTransferPolicyCpr* clone() const
{
return new AggregationLevelTransferPolicyCpr(*this);
}
private:
// trailing return type with decltype used for detecting the signature of coarsen member function by overloading this coarsen function
template <typename RN, typename PI, typename PG, typename VM, typename AM>
auto coarsen(RN& renumberer,
PI& pinfo,
PG& pg,
VM& vm,
AM& aggregatesMap,
int noAggregates,
bool useFixedOrder) ->
decltype(renumberer.coarsen(pinfo, pg, vm, aggregatesMap, pinfo, noAggregates, useFixedOrder))
{
return renumberer.coarsen(pinfo, pg, vm, aggregatesMap, pinfo, noAggregates, useFixedOrder);
}
template <typename RN, typename PI, typename PG, typename VM, typename AM>
auto coarsen(RN& renumberer,
PI& pinfo,
PG& pg,
VM& vm,
AM& aggregatesMap,
int noAggregates, ...)
{
return renumberer.coarsen(pinfo, pg, vm, aggregatesMap, pinfo, noAggregates);
}
typename O::matrix_type::field_type prolongDamp_;
std::shared_ptr<AggregatesMap> aggregatesMap_;
Criterion criterion_;
std::shared_ptr<typename O::matrix_type> matrix_;
};
template<class O, class S, class C>
class OneStepAMGCoarseSolverPolicyCpr
{
public:
typedef O Operator;
typedef typename O::range_type X;
typedef C Criterion;
typedef S Smoother;
typedef typename Dune::Amg::SmootherTraits<S>::Arguments SmootherArgs;
typedef AMG<Operator,X,Smoother> AMGType;
OneStepAMGCoarseSolverPolicyCpr(const SmootherArgs& args, const Criterion& c)
: smootherArgs_(args), criterion_(c)
{}
OneStepAMGCoarseSolverPolicyCpr(const OneStepAMGCoarseSolverPolicyCpr& other)
: coarseOperator_(other.coarseOperator_), smootherArgs_(other.smootherArgs_),
criterion_(other.criterion_)
{}
private:
struct AMGInverseOperator : public InverseOperator<X,X>
{
AMGInverseOperator(const typename AMGType::Operator& op,
const Criterion& crit,
const typename AMGType::SmootherArgs& args)
: amg_(op, crit,args), first_(true)
{}
void apply(X& x, X& b, [[maybe_unused]] double reduction, [[maybe_unused]] InverseOperatorResult& res)
{
if(first_)
{
amg_.pre(x,b);
first_=false;
x_=x;
}
amg_.apply(x,b);
}
void apply(X& x, X& b, InverseOperatorResult& res)
{
return apply(x,b,1e-8,res);
}
virtual SolverCategory::Category category() const
{
return amg_.category();
}
~AMGInverseOperator()
{
if(!first_)
amg_.post(x_);
}
AMGInverseOperator(const AMGInverseOperator& other)
: x_(other.x_), amg_(other.amg_), first_(other.first_)
{
}
private:
X x_;
AMGType amg_;
bool first_;
};
public:
typedef AMGInverseOperator CoarseLevelSolver;
template<class P>
CoarseLevelSolver* createCoarseLevelSolver(P& transferPolicy)
{
coarseOperator_=transferPolicy.getCoarseLevelOperator();
AMGInverseOperator* inv = new AMGInverseOperator(*coarseOperator_,
criterion_,
smootherArgs_);
return inv; //std::shared_ptr<InverseOperator<X,X> >(inv);
}
private:
std::shared_ptr<Operator> coarseOperator_;
SmootherArgs smootherArgs_;
Criterion criterion_;
};
template<class FO, class CSP, class S>
class TwoLevelMethodCpr :
public Preconditioner<typename FO::domain_type, typename FO::range_type>
{
public:
typedef CSP CoarseLevelSolverPolicy;
typedef typename CoarseLevelSolverPolicy::CoarseLevelSolver CoarseLevelSolver;
typedef FO FineOperatorType;
typedef typename FineOperatorType::range_type FineRangeType;
typedef typename FineOperatorType::domain_type FineDomainType;
typedef typename CSP::Operator CoarseOperatorType;
typedef typename CoarseOperatorType::range_type CoarseRangeType;
typedef typename CoarseOperatorType::domain_type CoarseDomainType;
typedef S SmootherType;
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)
: operator_(&op), smoother_(smoother),
preSteps_(preSteps), postSteps_(postSteps)
{
policy_ = policy.clone();
policy_->createCoarseLevelSystem(*operator_);
coarseSolver_=coarsePolicy.createCoarseLevelSolver(*policy_);
}
TwoLevelMethodCpr(const TwoLevelMethodCpr& other)
: operator_(other.operator_), coarseSolver_(new CoarseLevelSolver(*other.coarseSolver_)),
smoother_(other.smoother_), policy_(other.policy_->clone()),
preSteps_(other.preSteps_), postSteps_(other.postSteps_)
{}
~TwoLevelMethodCpr()
{
// Each instance has its own policy.
delete policy_;
delete coarseSolver_;
}
void updatePreconditioner(FineOperatorType& /* op */,
std::shared_ptr<SmootherType> smoother,
CoarseLevelSolverPolicy& coarsePolicy)
{
updatePreconditioner(smoother, coarsePolicy);
}
void updatePreconditioner(std::shared_ptr<SmootherType> smoother,
CoarseLevelSolverPolicy& coarsePolicy)
{
smoother_ = smoother;
// We assume the matrix has the same sparsity structure and memory locations throughout, only the values are changed.
smoother_->update();
if (coarseSolver_) {
policy_->calculateCoarseEntries(*operator_);
coarsePolicy.setCoarseOperator(*policy_);
coarseSolver_->updatePreconditioner();
} else {
// we should probably not be heere
policy_->createCoarseLevelSystem(*operator_);
coarseSolver_ = coarsePolicy.createCoarseLevelSolver(*policy_);
}
}
void pre(FineDomainType& x, FineRangeType& b)
{
if (x.dim() != u_.dim()) {
u_.resize(x.dim());
}
if (b.dim() != rhs_.dim()) {
rhs_.resize(b.dim());
}
smoother_->pre(x,b);
}
void post([[maybe_unused]] FineDomainType& x)
{
}
bool hasPerfectUpdate() const
{
// The two-level method has perfect update if both the finesmoother and coarse solver do.
return smoother_->hasPerfectUpdate() && coarseSolver_->hasPerfectUpdate();
}
void apply(FineDomainType& v, const FineRangeType& d)
{
u_ = v;
rhs_ = d;
LevelContext context;
SequentialInformation info;
context.pinfo=&info;
context.lhs=&u_;
context.update=&v;
context.smoother=smoother_;
context.rhs=&rhs_;
context.matrix=operator_;
// Presmoothing
presmooth(context, preSteps_);
//Coarse grid correction
policy_->moveToCoarseLevel(*context.rhs);
InverseOperatorResult res;
coarseSolver_->apply(policy_->getCoarseLevelLhs(), policy_->getCoarseLevelRhs(), res);
*context.lhs=0;
policy_->moveToFineLevel(*context.lhs);
*context.update += *context.lhs;
// Postsmoothing
postsmooth(context, postSteps_);
}
// //! Category of the preconditioner (see SolverCategory::Category)
virtual SolverCategory::Category category() const
{
return SolverCategory::sequential;
}
private:
struct LevelContext
{
typedef S SmootherType;
std::shared_ptr<SmootherType> smoother;
FineDomainType* lhs;
/*
* @brief The right hand side holding the current residual.
*
* This is passed to the smoother as the right hand side.
*/
FineRangeType* rhs;
FineDomainType* update;
SequentialInformation* pinfo;
const FineOperatorType* matrix;
};
const FineOperatorType* operator_;
CoarseLevelSolver* coarseSolver_;
std::shared_ptr<S> smoother_;
LevelTransferPolicyCpr<FO,typename CSP::Operator>* policy_;
std::size_t preSteps_;
std::size_t postSteps_;
mutable FineDomainType u_;
mutable FineRangeType rhs_;
};
}// end namespace Amg
}// end namespace Dune
#endif
Dune::Amg::AggregationLevelTransferPolicyCpr::moveToFineLevel
void moveToFineLevel(typename FatherType::FineDomainType &fineLhs)
Updates the fine level linear system after the correction of the coarse levels system.
Definition twolevelmethodcpr.hh:217
Dune::Amg::AggregationLevelTransferPolicyCpr::createCoarseLevelSystem
void createCoarseLevelSystem(const O &fineOperator)
Algebraically creates the coarse level system.
Definition twolevelmethodcpr.hh:170
Dune::Amg::AggregationLevelTransferPolicyCpr::clone
AggregationLevelTransferPolicyCpr * clone() const
Clone the current object.
Definition twolevelmethodcpr.hh:224
Dune::Amg::LevelTransferPolicyCpr
Abstract base class for transfer between levels and creation of the coarse level system.
Definition twolevelmethodcpr.hh:49
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::CoarseRangeType
CoarseOperatorType::range_type CoarseRangeType
Definition twolevelmethodcpr.hh:72
Dune::Amg::LevelTransferPolicyCpr::getCoarseLevelLhs
CoarseDomainType & getCoarseLevelLhs()
Get the coarse level left hand side.
Definition twolevelmethodcpr.hh:98
Dune::Amg::LevelTransferPolicyCpr::clone
virtual LevelTransferPolicyCpr * clone() const =0
Clone the current object.
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::FineOperatorType
FineOperator FineOperatorType
Definition twolevelmethodcpr.hh:55
Dune::Amg::LevelTransferPolicyCpr::createCoarseLevelSystem
virtual void createCoarseLevelSystem(const FineOperatorType &fineOperator)=0
Algebraically creates the coarse level system.
Dune::Amg::LevelTransferPolicyCpr::moveToCoarseLevel
virtual void moveToCoarseLevel(const FineRangeType &fineRhs)=0
Transfers the data to the coarse level.
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::CoarseDomainType
CoarseOperatorType::domain_type CoarseDomainType
Definition twolevelmethodcpr.hh:76
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::FineRangeType
FineOperatorType::range_type FineRangeType
Definition twolevelmethodcpr.hh:59
Dune::Amg::LevelTransferPolicyCpr::getCoarseLevelRhs
CoarseRangeType & getCoarseLevelRhs()
Get the coarse level right hand side.
Definition twolevelmethodcpr.hh:89
Dune::Amg::LevelTransferPolicyCpr::~LevelTransferPolicyCpr
virtual ~LevelTransferPolicyCpr()
Destructor.
Definition twolevelmethodcpr.hh:140
Dune::Amg::LevelTransferPolicyCpr::moveToFineLevel
virtual void moveToFineLevel(FineDomainType &fineLhs)=0
Updates the fine level linear system after the correction of the coarse levels system.
Dune::Amg::LevelTransferPolicyCpr::calculateCoarseEntries
virtual void calculateCoarseEntries(const FineOperatorType &fineOperator)=0
?
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::lhs_
CoarseDomainType lhs_
Definition twolevelmethodcpr.hh:146
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::rhs_
CoarseRangeType rhs_
Definition twolevelmethodcpr.hh:144
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::FineDomainType
FineOperatorType::domain_type FineDomainType
Definition twolevelmethodcpr.hh:63
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::CoarseOperatorType
CoarseOperator CoarseOperatorType
Definition twolevelmethodcpr.hh:68
Dune::Amg::LevelTransferPolicyCpr::getCoarseLevelOperator
std::shared_ptr< CoarseOperatorType > & getCoarseLevelOperator()
Get the coarse level operator.
Definition twolevelmethodcpr.hh:81
Dune::Amg::LevelTransferPolicyCpr< FineOperator, CoarseOperator >::operator_
std::shared_ptr< CoarseOperatorType > operator_
Definition twolevelmethodcpr.hh:148
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::AMGType
AMG< Operator, X, Smoother > AMGType
The type of the AMG construct on the coarse level.
Definition twolevelmethodcpr.hh:282
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::OneStepAMGCoarseSolverPolicyCpr
OneStepAMGCoarseSolverPolicyCpr(const OneStepAMGCoarseSolverPolicyCpr &other)
Copy constructor.
Definition twolevelmethodcpr.hh:292
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::CoarseLevelSolver
AMGInverseOperator CoarseLevelSolver
The type of solver constructed for the coarse level.
Definition twolevelmethodcpr.hh:349
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::Smoother
S Smoother
The type of the smoother used in AMG.
Definition twolevelmethodcpr.hh:278
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::Criterion
C Criterion
The type of the crition used for the aggregation within AMG.
Definition twolevelmethodcpr.hh:276
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::SmootherArgs
Dune::Amg::SmootherTraits< S >::Arguments SmootherArgs
The type of the arguments used for constructing the smoother.
Definition twolevelmethodcpr.hh:280
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::createCoarseLevelSolver
CoarseLevelSolver * createCoarseLevelSolver(P &transferPolicy)
Constructs a coarse level solver.
Definition twolevelmethodcpr.hh:359
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::X
O::range_type X
The type of the range and domain of the operator.
Definition twolevelmethodcpr.hh:274
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::OneStepAMGCoarseSolverPolicyCpr
OneStepAMGCoarseSolverPolicyCpr(const SmootherArgs &args, const Criterion &c)
Constructs the coarse solver policy.
Definition twolevelmethodcpr.hh:288
Dune::Amg::OneStepAMGCoarseSolverPolicyCpr::Operator
O Operator
The type of the linear operator used.
Definition twolevelmethodcpr.hh:272
Dune::Amg::TwoLevelMethodCpr
Definition twolevelmethodcpr.hh:387
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::SmootherType
Dune::PreconditionerWithUpdate< VectorType, VectorType > SmootherType
Definition twolevelmethodcpr.hh:422
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::FineRangeType
FineOperatorType::range_type FineRangeType
Definition twolevelmethodcpr.hh:401
Dune::Amg::TwoLevelMethodCpr::TwoLevelMethodCpr
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:436
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::FineDomainType
FineOperatorType::domain_type FineDomainType
Definition twolevelmethodcpr.hh:405
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::CoarseDomainType
CoarseOperatorType::domain_type CoarseDomainType
Definition twolevelmethodcpr.hh:418
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::CoarseOperatorType
CoarseSolverPolicy::Operator CoarseOperatorType
Definition twolevelmethodcpr.hh:410
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::CoarseLevelSolverPolicy
CoarseSolverPolicy CoarseLevelSolverPolicy
Definition twolevelmethodcpr.hh:390
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::CoarseLevelSolver
CoarseLevelSolverPolicy::CoarseLevelSolver CoarseLevelSolver
Definition twolevelmethodcpr.hh:392
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::CoarseRangeType
CoarseOperatorType::range_type CoarseRangeType
Definition twolevelmethodcpr.hh:414
Dune::Amg::TwoLevelMethodCpr< OperatorType, CoarseSolverPolicy, Dune::PreconditionerWithUpdate< VectorType, VectorType > >::FineOperatorType
OperatorType FineOperatorType
Definition twolevelmethodcpr.hh:397