// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_AMGSMOOTHER_HH #define DUNE_AMGSMOOTHER_HH #include #include #include #include #include #include #include namespace Dune { namespace Amg { /** * @addtogroup ISTL_PAAMG * * @{ */ /** @file * @author Markus Blatt * @brief Classes for the generic construction and application * of the smoothers. */ /** * @brief The default class for the smoother arguments. */ template struct DefaultSmootherArgs { /** * @brief The type of the relaxation factor. */ typedef typename FieldTraits::real_type RelaxationFactor; /** * @brief The number of iterations to perform. */ int iterations; /** * @brief The relaxation factor to use. */ RelaxationFactor relaxationFactor; /** * @brief Default constructor. */ DefaultSmootherArgs() : iterations(1), relaxationFactor(1.0) {} }; /** * @brief Traits class for getting the attribute class of a smoother. */ template struct SmootherTraits { typedef DefaultSmootherArgs Arguments; }; template struct SmootherTraits> { typedef DefaultSmootherArgs Arguments; }; template struct SmootherTraits > : public SmootherTraits {}; template struct SmootherTraits > : public SmootherTraits {}; /** * @brief Construction Arguments for the default smoothers */ template class DefaultConstructionArgs { typedef typename T::matrix_type Matrix; typedef typename SmootherTraits::Arguments SmootherArgs; typedef Dune::Amg::AggregatesMap::VertexDescriptor> AggregatesMap; public: virtual ~DefaultConstructionArgs() {} void setMatrix(const Matrix& matrix) { matrix_=&matrix; } virtual void setMatrix(const Matrix& matrix, [[maybe_unused]] const AggregatesMap& amap) { setMatrix(matrix); } const Matrix& getMatrix() const { return *matrix_; } void setArgs(const SmootherArgs& args) { args_=&args; } template void setComm([[maybe_unused]] T1& comm) {} const SequentialInformation& getComm() { return comm_; } const SmootherArgs getArgs() const { return *args_; } protected: const Matrix* matrix_; private: const SmootherArgs* args_; SequentialInformation comm_; }; template struct ConstructionArgs : public DefaultConstructionArgs {}; template class DefaultParallelConstructionArgs : public ConstructionArgs { public: virtual ~DefaultParallelConstructionArgs() {} void setComm(const C& comm) { comm_ = &comm; } const C& getComm() const { return *comm_; } private: const C* comm_; }; template class DefaultConstructionArgs> { typedef Richardson T; typedef typename SmootherTraits::Arguments SmootherArgs; public: virtual ~DefaultConstructionArgs() {} template void setMatrix(const Args&...) {} void setArgs(const SmootherArgs& args) { args_=&args; } template void setComm([[maybe_unused]] T1& comm) {} const SequentialInformation& getComm() { return comm_; } const SmootherArgs getArgs() const { return *args_; } private: const SmootherArgs* args_; SequentialInformation comm_; }; template struct ConstructionTraits; /** * @brief Policy for the construction of the SeqSSOR smoother */ template struct ConstructionTraits > { typedef DefaultConstructionArgs > Arguments; static inline std::shared_ptr> construct(Arguments& args) { return std::make_shared> (args.getMatrix(), args.getArgs().iterations, args.getArgs().relaxationFactor); } }; /** * @brief Policy for the construction of the SeqSOR smoother */ template struct ConstructionTraits > { typedef DefaultConstructionArgs > Arguments; static inline std::shared_ptr> construct(Arguments& args) { return std::make_shared> (args.getMatrix(), args.getArgs().iterations, args.getArgs().relaxationFactor); } }; /** * @brief Policy for the construction of the SeqJac smoother */ template struct ConstructionTraits > { typedef DefaultConstructionArgs > Arguments; static inline std::shared_ptr> construct(Arguments& args) { return std::make_shared> (args.getMatrix(), args.getArgs().iterations, args.getArgs().relaxationFactor); } }; /** * @brief Policy for the construction of the Richardson smoother */ template struct ConstructionTraits > { typedef DefaultConstructionArgs > Arguments; static inline std::shared_ptr> construct(Arguments& args) { return std::make_shared> (args.getArgs().relaxationFactor); } }; template class ConstructionArgs > : public DefaultConstructionArgs > { public: ConstructionArgs(int n=0) : n_(n) {} void setN(int n) { n_ = n; } int getN() { return n_; } private: int n_; }; /** * @brief Policy for the construction of the SeqILU smoother */ template struct ConstructionTraits > { typedef ConstructionArgs > Arguments; static inline std::shared_ptr> construct(Arguments& args) { return std::make_shared> (args.getMatrix(), args.getN(), args.getArgs().relaxationFactor); } }; /** * @brief Policy for the construction of the ParSSOR smoother */ template struct ConstructionTraits > { typedef DefaultParallelConstructionArgs Arguments; static inline std::shared_ptr> construct(Arguments& args) { return std::make_shared> (args.getMatrix(), args.getArgs().iterations, args.getArgs().relaxationFactor, args.getComm()); } }; template struct ConstructionTraits > { typedef DefaultParallelConstructionArgs Arguments; typedef ConstructionTraits SeqConstructionTraits; static inline std::shared_ptr> construct(Arguments& args) { auto seqPrec = SeqConstructionTraits::construct(args); return std::make_shared> (seqPrec, args.getComm()); } }; template struct ConstructionTraits > { typedef DefaultParallelConstructionArgs Arguments; typedef ConstructionTraits SeqConstructionTraits; static inline std::shared_ptr> construct(Arguments& args) { auto seqPrec = SeqConstructionTraits::construct(args); return std::make_shared> (seqPrec, args.getComm()); } }; /** * @brief Helper class for applying the smoothers. * * The goal of this class is to get a symmetric AMG method * whenever possible. * * The specializations for SOR and SeqOverlappingSchwarz in * MultiplicativeSchwarzMode will apply * the smoother forward when pre and backward when post smoothing. */ template struct SmootherApplier { typedef T Smoother; typedef typename Smoother::range_type Range; typedef typename Smoother::domain_type Domain; /** * @brief apply pre smoothing in forward direction * * @param smoother The smoother to use. * @param d The current defect. * @param v handle to store the update in. */ static void preSmooth(Smoother& smoother, Domain& v, const Range& d) { smoother.apply(v,d); } /** * @brief apply post smoothing in forward direction * * @param smoother The smoother to use. * @param d The current defect. * @param v handle to store the update in. */ static void postSmooth(Smoother& smoother, Domain& v, const Range& d) { smoother.apply(v,d); } }; /** * @brief Apply pre smoothing on the current level. * @param levelContext the iterators of the current level. * @param steps The number of smoothing steps to apply. */ template void presmooth(LevelContext& levelContext, size_t steps) { for(std::size_t i=0; i < steps; ++i) { *levelContext.lhs=0; SmootherApplier ::preSmooth(*levelContext.smoother, *levelContext.lhs, *levelContext.rhs); // Accumulate update *levelContext.update += *levelContext.lhs; // update defect levelContext.matrix->applyscaleadd(-1, *levelContext.lhs, *levelContext.rhs); levelContext.pinfo->project(*levelContext.rhs); } } /** * @brief Apply post smoothing on the current level. * @param levelContext the iterators of the current level. * @param steps The number of smoothing steps to apply. */ template void postsmooth(LevelContext& levelContext, size_t steps) { for(std::size_t i=0; i < steps; ++i) { // update defect levelContext.matrix->applyscaleadd(-1, *levelContext.lhs, *levelContext.rhs); *levelContext.lhs=0; levelContext.pinfo->project(*levelContext.rhs); SmootherApplier ::postSmooth(*levelContext.smoother, *levelContext.lhs, *levelContext.rhs); // Accumulate update *levelContext.update += *levelContext.lhs; } } template struct SmootherApplier > { typedef SeqSOR Smoother; typedef typename Smoother::range_type Range; typedef typename Smoother::domain_type Domain; static void preSmooth(Smoother& smoother, Domain& v, Range& d) { smoother.template apply(v,d); } static void postSmooth(Smoother& smoother, Domain& v, Range& d) { smoother.template apply(v,d); } }; template struct SmootherApplier > > { typedef BlockPreconditioner > Smoother; typedef typename Smoother::range_type Range; typedef typename Smoother::domain_type Domain; static void preSmooth(Smoother& smoother, Domain& v, Range& d) { smoother.template apply(v,d); } static void postSmooth(Smoother& smoother, Domain& v, Range& d) { smoother.template apply(v,d); } }; template struct SmootherApplier > > { typedef NonoverlappingBlockPreconditioner > Smoother; typedef typename Smoother::range_type Range; typedef typename Smoother::domain_type Domain; static void preSmooth(Smoother& smoother, Domain& v, Range& d) { smoother.template apply(v,d); } static void postSmooth(Smoother& smoother, Domain& v, Range& d) { smoother.template apply(v,d); } }; } // end namespace Amg // forward declarations template class SeqOverlappingSchwarz; struct MultiplicativeSchwarzMode; namespace Amg { template struct SmootherApplier > { typedef SeqOverlappingSchwarz Smoother; typedef typename Smoother::range_type Range; typedef typename Smoother::domain_type Domain; static void preSmooth(Smoother& smoother, Domain& v, const Range& d) { smoother.template apply(v,d); } static void postSmooth(Smoother& smoother, Domain& v, const Range& d) { smoother.template apply(v,d); } }; // template // class SeqOverlappingSchwarz; template struct SeqOverlappingSchwarzSmootherArgs : public DefaultSmootherArgs { enum Overlap {vertex, aggregate, pairwise, none}; Overlap overlap; bool onthefly; SeqOverlappingSchwarzSmootherArgs(Overlap overlap_=vertex, bool onthefly_=false) : overlap(overlap_), onthefly(onthefly_) {} }; template struct SmootherTraits > { typedef SeqOverlappingSchwarzSmootherArgs Arguments; }; template class ConstructionArgs > : public DefaultConstructionArgs > { typedef DefaultConstructionArgs > Father; public: typedef typename MatrixGraph::VertexDescriptor VertexDescriptor; typedef Dune::Amg::AggregatesMap AggregatesMap; typedef typename AggregatesMap::AggregateDescriptor AggregateDescriptor; typedef typename SeqOverlappingSchwarz::subdomain_vector Vector; typedef typename Vector::value_type Subdomain; virtual void setMatrix(const M& matrix, const AggregatesMap& amap) { Father::setMatrix(matrix); std::vector visited(amap.noVertices(), false); typedef IteratorPropertyMap::iterator,IdentityMap> VisitedMapType; VisitedMapType visitedMap(visited.begin()); MatrixGraph graph(matrix); typedef SeqOverlappingSchwarzSmootherArgs SmootherArgs; switch(Father::getArgs().overlap) { case SmootherArgs::vertex : { VertexAdder visitor(subdomains, amap); createSubdomains(matrix, graph, amap, visitor, visitedMap); } break; case SmootherArgs::pairwise : { createPairDomains(graph); } break; case SmootherArgs::aggregate : { AggregateAdder visitor(subdomains, amap, graph, visitedMap); createSubdomains(matrix, graph, amap, visitor, visitedMap); } break; case SmootherArgs::none : NoneAdder visitor; createSubdomains(matrix, graph, amap, visitor, visitedMap); break; default : DUNE_THROW(NotImplemented, "This overlapping scheme is not supported!"); } } void setMatrix(const M& matrix) { Father::setMatrix(matrix); /* Create aggregates map where each aggregate is just one vertex. */ AggregatesMap amap(matrix.N()); VertexDescriptor v=0; for(typename AggregatesMap::iterator iter=amap.begin(); iter!=amap.end(); ++iter) *iter=v++; std::vector visited(amap.noVertices(), false); typedef IteratorPropertyMap::iterator,IdentityMap> VisitedMapType; VisitedMapType visitedMap(visited.begin()); MatrixGraph graph(matrix); typedef SeqOverlappingSchwarzSmootherArgs SmootherArgs; switch(Father::getArgs().overlap) { case SmootherArgs::vertex : { VertexAdder visitor(subdomains, amap); createSubdomains(matrix, graph, amap, visitor, visitedMap); } break; case SmootherArgs::aggregate : { DUNE_THROW(NotImplemented, "Aggregate overlap is not supported yet"); /* AggregateAdder visitor(subdomains, amap, graph, visitedMap); createSubdomains(matrix, graph, amap, visitor, visitedMap); */ } break; case SmootherArgs::pairwise : { createPairDomains(graph); } break; case SmootherArgs::none : NoneAdder visitor; createSubdomains(matrix, graph, amap, visitor, visitedMap); } } const Vector& getSubDomains() { return subdomains; } private: struct VertexAdder { VertexAdder(Vector& subdomains_, const AggregatesMap& aggregates_) : subdomains(subdomains_), max(-1), subdomain(-1), aggregates(aggregates_) {} template void operator()(const T& edge) { if(aggregates[edge.target()]!=AggregatesMap::ISOLATED) subdomains[subdomain].insert(edge.target()); } int setAggregate(const AggregateDescriptor& aggregate_) { subdomain=aggregate_; max = std::max(subdomain, aggregate_); return subdomain; } int noSubdomains() const { return max+1; } private: Vector& subdomains; AggregateDescriptor max; AggregateDescriptor subdomain; const AggregatesMap& aggregates; }; struct NoneAdder { template void operator()(const T& /*edge*/) {} int setAggregate(const AggregateDescriptor& /*aggregate_*/) { return -1; } int noSubdomains() const { return -1; } }; template struct AggregateAdder { AggregateAdder(Vector& subdomains_, const AggregatesMap& aggregates_, const MatrixGraph& graph_, VM& visitedMap_) : subdomains(subdomains_), subdomain(-1), aggregates(aggregates_), adder(subdomains_, aggregates_), graph(graph_), visitedMap(visitedMap_) {} template void operator()(const T& edge) { subdomains[subdomain].insert(edge.target()); // If we (the neighbouring vertex of the aggregate) // are not isolated, add the aggregate we belong to // to the same subdomain using the OneOverlapAdder if(aggregates[edge.target()]!=AggregatesMap::ISOLATED) { assert(aggregates[edge.target()]!=aggregate); typename AggregatesMap::VertexList vlist; aggregates.template breadthFirstSearch(edge.target(), aggregate, graph, vlist, adder, adder, visitedMap); } } int setAggregate(const AggregateDescriptor& aggregate_) { adder.setAggregate(aggregate_); aggregate=aggregate_; return ++subdomain; } int noSubdomains() const { return subdomain+1; } private: AggregateDescriptor aggregate; Vector& subdomains; int subdomain; const AggregatesMap& aggregates; VertexAdder adder; const MatrixGraph& graph; VM& visitedMap; }; void createPairDomains(const MatrixGraph& graph) { typedef typename MatrixGraph::ConstVertexIterator VIter; typedef typename MatrixGraph::ConstEdgeIterator EIter; typedef typename M::size_type size_type; std::set > pairs; int total=0; for(VIter v=graph.begin(), ve=graph.end(); ve != v; ++v) for(EIter e = v.begin(), ee=v.end(); ee!=e; ++e) { ++total; if(e.source() >::const_iterator SIter; typename Vector::iterator subdomain=subdomains.begin(); for(SIter s=pairs.begin(), se =pairs.end(); se!=s; ++s) { subdomain->insert(s->first); subdomain->insert(s->second); ++subdomain; } std::size_t minsize=10000; std::size_t maxsize=0; int sum=0; for(typename Vector::size_type i=0; i < subdomains.size(); ++i) { sum+=subdomains[i].size(); minsize=std::min(minsize, subdomains[i].size()); maxsize=std::max(maxsize, subdomains[i].size()); } Dune::dinfo<<"Subdomain size: min="<