14#ifndef MG_MULTIPLICATIVEMULTIGRID_HH
15#define MG_MULTIPLICATIVEMULTIGRID_HH
19#include <boost/timer/timer.hpp>
21#include <dune/istl/preconditioner.hh>
75 template <
class Entry,
class Index,
class Smoother,
class Pro
longation>
99 template <
class MakeSmoother>
101 std::unique_ptr<CoarsePreconditioner>&& coarsePreconditioner_,
int nPre_=3,
int nPost_=3)
102 : mgStack(std::move(mgStack_)), nPre(nPre_), nPost(nPost_), coarsePreconditioner(std::move(coarsePreconditioner_)),
103 linesearch(false), smootherStepSize(1.0)
107 timer.
start(
"smoother construction");
108 for (
int l=1; l<mgStack.levels(); ++l)
109 smoothers.push_back(makeSmoother(mgStack.a(l)));
110 timer.
stop(
"smoother construction");
125 assert(x.two_norm()==0);
127 runMG(x,b,mgStack.levels()-1);
153 assert(nPre>=0 && nPost>=0 && nPre+nPost>0);
164 smootherStepSize = w;
191 return *coarsePreconditioner;
199 return std::make_pair(nPre,nPost);
210 timer.
start(
"coarse grid solver");
211 coarsePreconditioner->pre(x,r);
212 coarsePreconditioner->apply(x,r);
213 coarsePreconditioner->post(x);
214 timer.
stop(
"coarse grid solver");
218 auto& s = smoothers[level-1];
219 auto const& a = mgStack.a(level);
220 double w = smootherStepSize;
225 bool const sRequiresZeroInput = s.requiresInitializedInput();
227 for (
int i=0; i<nPre; ++i)
229 if (sRequiresZeroInput) dx = 0;
230 timer.
start(
"smoother application");
232 timer.
stop(
"smoother application");
234 timer.
start(
"matrix-vector");
237 double dxadx = a.mv(dx,adx);
243 timer.
stop(
"matrix-vector");
250 auto const& p = mgStack.p(level-1);
251 timer.
start(
"restriction");
254 timer.
stop(
"restriction");
256 runMG(cx,cr,level-1);
257 timer.
start(
"prolongation");
259 timer.
stop(
"prolongation");
266 for (
int i=0; i<nPost; ++i)
268 if (sRequiresZeroInput) dx = 0;
269 timer.
start(
"smoother application");
271 timer.
stop(
"smoother application");
273 if (i+1<nPost || linesearch)
275 timer.
start(
"matrix-vector");
278 double dxadx = a.mv(dx,adx);
284 timer.
stop(
"matrix-vector");
294 double omega = (x*r) / (dx*x);
300 MultiGridStack<Prolongation,Entry,Index> mgStack;
301 std::vector<Smoother> smoothers;
303 std::unique_ptr<CoarsePreconditioner> coarsePreconditioner;
305 double smootherStepSize;
323 template <
class Entry,
class Index,
class Pro
longation,
class MakeSmoother,
class CoarsePreconditioner>
325 MakeSmoother
const& makeSmoother,
326 std::unique_ptr<CoarsePreconditioner>&& coarsePreconditioner,
327 int nPre=3,
int nPost=3)
331 std::move(mgStack),makeSmoother,std::move(coarsePreconditioner),nPre,nPost);
344 template <
typename Matrix>
356 template <
typename Space,
typename Scalar=
float>
367 : space(space_), targetCondition(targetCondition_)
370 template <
typename Entry,
typename Index>
374 Index>(space,a,targetCondition);
379 double targetCondition;
386 template <
typename Matrix>
395 template <
class Domain,
class Range,
class SparseIndex>
417 if(endBd != &(bd[0][0]) + bd.dim()) {
418 throw DirectSolverException(
"Copying float to double block-vector failed. End Iterators differ.",__FILE__,__LINE__);
420 directSolver.apply(xd, bd);
422 if(endX != &(x[0][0]) + x.dim()) {
423 throw DirectSolverException(
"Copying float to double block-vector failed. End Iterators differ.",__FILE__,__LINE__);
439 template <
int domainBlockSize,
int rangeBlockSize,
class Index>
463 template <
class Entry,
class Index,
class Gr
idMan>
465 int nPre=3,
int nPost=3,
bool onlyLowerTriangle=
false,
470 timer.
start(
"MG stack creation");
472 timer.
stop(
"MG stack creation");
474 timer.
start(
"direct solver creation");
476 timer.
stop(
"direct solver creation");
478 timer.
start(
"MG creation");
480 moveUnique(std::move(coarsePreconditioner)),nPre,nPost);
481 timer.
stop(
"MG creation");
482 mg.setSmootherStepSize(0.5);
501 template <
class Entry,
class Index,
class Space,
class MakeSmoother>
506 timer.
start(
"create P multigrid stack");
508 timer.
stop(
"create P multigrid stack");
510 timer.
start(
"create MG as coarse solver");
511 auto coarsePreconditioner =
makeJacobiMultiGrid(std::move(mgStack.coarseGridMatrix()),space.gridManager(),nPre,nPost,onlyLowerTriangle,directType);
512 coarsePreconditioner.setSmootherStepSize(0.6);
513 timer.
stop(
"create MG as coarse solver");
515 timer.
start(
"create multigrid");
517 timer.
stop(
"create multigrid");
542 template <
class Entry,
class Index,
class Space,
class CoarseSpace>
548 timer.
start(
"create P multigrid stack");
550 timer.
stop(
"create P multigrid stack");
552 timer.
start(
"create MG as coarse solver");
553 auto coarsePreconditioner =
makeJacobiMultiGrid(std::move(mgStack.coarseGridMatrix()),space.gridManager(),nPre,nPost,
false,directType);
554 coarsePreconditioner.setSmootherStepSize(0.6);
555 timer.
stop(
"create MG as coarse solver");
557 timer.
start(
"create multigrid");
559 moveUnique(std::move(coarsePreconditioner)),nPre,nPost);
560 mg.setSmootherStepSize(Space::Grid::dimension==3 ? 0.4 : 0.5);
561 timer.
stop(
"create multigrid");
586 template <
class Entry,
class Index,
class Space>
591 nPre,nPost,onlyLowerTriangle,directType);
592 mg.setSmootherStepSize(Space::Grid::dimension==3 ? 0.4 : 0.5);
608 template <
class Entry,
class Index,
class FineSpace>
613 mg.setSmootherStepSize(0.5);
virtual void pre(FloatDomain &, FloatRange &)
NumaBCRSMatrix< Dune::FieldMatrix< float, rangeBlockSize, domainBlockSize >, SparseIndex > FloatMatrix
static constexpr int rangeBlockSize
DirectPreconditionerFloatWrapper(FloatMatrix const &matA, DirectType directType)
virtual void apply(FloatDomain &x, FloatRange const &b)
static constexpr int domainBlockSize
virtual void post(FloatDomain &)
NumaBCRSMatrix< Dune::FieldMatrix< double, rangeBlockSize, domainBlockSize >, SparseIndex > DoubleMatrix
typename MatrixTraits< DoubleMatrix >::NaturalRange DoubleRange
typename MatrixTraits< DoubleMatrix >::NaturalDomain DoubleDomain
To be raised if a direct solver fails.
Functor for creating overlapping Schwarz smoothers.
MakeAdditiveSchwarzSmoother(Space const &space_, double targetCondition_=-1.0)
Constructor.
auto operator()(NumaBCRSMatrix< Entry, Index > const &a) const
Functor for creating Jacobi smoothers.
auto operator()(Matrix const &a) const
Class for multigrid stacks.
CoarsePreconditioner & getCoarsePreconditioner()
Provides access to the coarse grid preconditioner.
void setLinesearch(bool ls)
Enable or disable the line search option.
typename Smoother::domain_type domain_type
typename Smoother::field_type field_type
MultiplicativeMultiGrid(MultiplicativeMultiGrid &&other)=default
MultiplicativeMultiGrid & operator=(MultiplicativeMultiGrid &&other)=default
void setSmoothings(int nPre_, int nPost_)
Sets the number of pre- and post-smoothing iterations to perform.
double getSmootherStepSize() const
virtual field_type applyDp(domain_type &x, range_type const &r)
Computes and returns .
Dune::Preconditioner< domain_type, range_type > CoarsePreconditioner
virtual void apply(domain_type &x, range_type const &r)
Application of preconditioner.
typename Smoother::range_type range_type
MultiplicativeMultiGrid()=default
Default constructor.
void setSmootherStepSize(double w)
Define the smoother step size.
virtual bool requiresInitializedInput() const
Returns true if the target vector x has to be initialized to zero before calling apply or applyDp.
std::pair< int, int > getSmoothings()
Returns a pair of number of pre- and post-smoothing iterations to perform.
MultiplicativeMultiGrid(MultiGridStack< Prolongation, Entry, Index > &&mgStack_, MakeSmoother const &makeSmoother, std::unique_ptr< CoarsePreconditioner > &&coarsePreconditioner_, int nPre_=3, int nPost_=3)
Constructor.
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
An additive overlapping domain decomposition type preconditioner for higher order finite elements app...
Interface for symmetric preconditioners.
Supports gathering and reporting execution times information for nested program parts.
static Timings & instance()
Returns a reference to a single default instance.
void stop(std::string const &name)
Stops the timing of given section.
struct Times const * start(std::string const &name)
Starts or continues the timing of given section.
DirectType
Available direct solvers for linear equation systems.
@ MUMPS
MUMPS sparse solver.
auto makeBlockJacobiPMultiGrid(NumaBCRSMatrix< Entry, Index > A, Space const &space, NumaBCRSMatrix< Entry, Index > coarseA, CoarseSpace const &coarseSpace, int nPre=3, int nPost=3, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type for higher orde...
auto makeDirectPreconditioner(Matrix &&A, DirectType directType=DirectType::MUMPS)
Creates a direct solver for the given matrix.
auto makeMultiplicativeMultiGrid(MultiGridStack< Prolongation, Entry, Index > &&mgStack, MakeSmoother const &makeSmoother, std::unique_ptr< CoarsePreconditioner > &&coarsePreconditioner, int nPre=3, int nPost=3)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type for P1 elements...
auto makeJacobiMultiGrid(NumaBCRSMatrix< Entry, Index > const &A, GridMan const &gridman, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative Jacobi multigrid for P1 elements.
auto makeMultiplicativePMultiGrid(NumaBCRSMatrix< Entry, Index > &&A, Space const &space, MakeSmoother const &makeSmoother, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type for higher orde...
auto makePMultiGridStack(FineSpace const &space, Matrix &&A, bool onlyLowerTriangle)
convenience routine for creating multigrid stacks based on coarsening by reducing the ansatz order fr...
auto makeBlockJacobiPMultiGrid(NumaBCRSMatrix< Entry, Index > A, Space const &space, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type for higher orde...
MultiGridStack< MGProlongation, Entry, Index > makeGeometricMultiGridStack(GridMan const &gridManager, NumaBCRSMatrix< Entry, Index > &&A, size_t minNodes=10000, bool onlyLowerTriangle=false)
convenience routine for creating multigrid stacks based on geometric coarsening for P1 elements
auto makeJacobiPMultiGrid(NumaBCRSMatrix< Entry, Index > A, FineSpace const &space, int nPre=3, int nPost=3, bool onlyLowerTriangle=false, DirectType directType=DirectType::MUMPS)
Convenience function creating multiplicative multigrid preconditioner of V-cycle type with Jacobi smo...
auto moveUnique(A &&a)
Creates a dynamically allocated object from an r-value reference.
A duplicate(A const &a)
Creates a copy of a given object.
OutIter vectorToSequence(double x, OutIter iter)
Convenience typedefs and creation functions for common function spaces.
void NaturalDomain
The natural domain type.
void NaturalRange
The natural range type.