KASKADE 7 development version
qpmg.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2017-2020 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef QPMG_HH
14#define QPMG_HH
15
16#include <memory>
17#include <tuple>
18
19#include "dune/istl/bvector.hh"
20
23#include "mg/prolongation.hh"
24#include "utilities/enums.hh"
25
26namespace Kaskade
27{
28
36 template <int d, class Real=double>
38 {
39 public:
42
44
45 virtual void enterPreSmoothingCorrection(int level, VectorX const& dx) {}
46 virtual void enterCoarseGridCorrection(int level, VectorX const& dx) {}
47 virtual void enterPostSmoothingCorrection(int level, VectorX const& dx) {}
48
55 virtual void enterVCycleCorrection(int iter, VectorX const& dx, Real energy,
56 VectorX const& grad, VectorX const& grad0, VectorX const& grad1,
57 VectorB const& cons, int activeSetChanges,
58 std::vector<VectorX> &correctionStack,
59 std::vector<std::tuple<double,double,double>> &coarseRes)
60 {}
61 };
62
63 // ----------------------------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------------------------
65
66
67
91 template <int d, class Prolongation, class Smoother, class CoarseSmoother=Smoother, class Real=double>
93 {
95 public:
101
102 // check if coarse smoother is of equal type as rest of smoothers
103 // if so, SmootherType = Smoother, else use std::variant<Smoother,CoarseSmoother>
104 static constexpr bool smoothersEqual = std::is_same<Smoother,CoarseSmoother>::value;
105 using SmootherType = std::conditional_t<smoothersEqual,
106 Smoother,
107 std::variant<Smoother,CoarseSmoother>>;
108
124 QPMultiGridBase(MatrixA A, MatrixB const& B, std::vector<Prolongation>&& prolongations,
125 Real smootherRegularization=0, bool blocks=true, bool directOnCoarse=true);
126
131
140 VectorX step(VectorX const& x, VectorX c, VectorB b) const;
141
151 std::tuple<VectorX,int> solve(VectorX x, VectorX const& c, VectorB const& b, double tol, int vcycles) const;
152
153
161 Self& setCoarseLevel(int coarselevel);
162
163 Self& setSmoothings(int pre, int post);
164
175
185
196
197 protected:
201 virtual double computeEnergy(MatrixA const& A, MatrixB const& B,
202 VectorX const& c, VectorB const& b, VectorX const& x) const = 0;
207 virtual std::tuple<VectorX,VectorB,int> gradient(MatrixA const& A, MatrixB const& B,
208 VectorX const& c, VectorB const& b, VectorX const& x) const = 0;
209
215 virtual std::tuple<VectorX,VectorX,VectorX,VectorB,int> gradient_extended(MatrixA const& A, MatrixB const& B,
216 VectorX const& c, VectorB const& b, VectorX const& x) const = 0;
217
221 virtual double qpLinesearch(MatrixA const& A, MatrixB const& B, VectorX const& c,
222 VectorB const& b, VectorX const& dx) const = 0;
223
224
225 // vector of smoothers, one for each level
226 // note: SmootherType might be either of Type
227 // Smoother or std::variant<Smoother,CoarseSmoother>
228 std::vector<SmootherType> smoothers;
229
233 MatrixB const& B() const;
234
235 private:
243 VectorX mgRecursion(int level, int lpre, int lpost, VectorX c, VectorB b) const;
244
246 std::vector<MatrixB> mgStackB;
247 mutable std::vector<VectorX> correctionStack; // vector with coarse grid corrections on each level (interpolated to fine grid)
248 mutable std::vector<std::tuple<double,double,double>> coarseResidual; // vector with norm of residual on each level (after computing correction)
249
250 mutable Real gamma;
251 int coarseLevel = 0;
252 int pre = 3;
253 int post = 3;
254 int mid = 1;
255
256 std::unique_ptr<MGSolverStatistics<d,Real>> dummyLogger; // this dummy will be used if no other logger is given
258 };
259
260 // ----------------------------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------------------------
262
263 // forward declaration
264 template <int d, class Real, QPStructure sparsity=QPStructure::DENSE>
266
267
268
310 template <int d, class Prolongation, class Real=double, class Smoother=QPPenaltySmoother<d,Real>, class CoarseSmoother=QPPenaltySmoother<d,Real>>
312 : public QPMultiGridBase<d,Prolongation,Smoother,CoarseSmoother,Real>
313 {
316
317 public:
323
339 QPMultiGrid(MatrixA const& A, MatrixB const& B, std::vector<Prolongation>&& prolongations,
340 Real smootherRegularization=0, bool blocks=true, bool directOnCoarse=true);
341
346
357 std::tuple<VectorX,VectorB,int> solve(VectorX x, VectorX const& c, VectorB const& b, VectorB const& lambda, double tol, int vcycles) const;
358
359// /**
360// * \brief A least-squares multiplier update.
361// *
362// * This can be used if a good initial guess for \f$ x \f$ is available, but not for \f$ \lambda \f$.
363// * A common situation where this is typically the case is finite strain contact mechanics, where the
364// * constraints and hence the multiplier changes with deformation, after having solved a QP linearization.
365// */
366// void leastSquaresMultiplierUpdate(VectorX const& c, VectorB const& b, VectorX const& x, VectorB& lambda) const;
367//
368
383 VectorB firstOrderUpdate(VectorX const& x, VectorB const& b, VectorB const& lambda) const;
384
385
402 Self& setPenalty(Real gamma, bool levelDependent=false);
403
406
411 double getPenalty();
412
413 protected:
419 virtual double computeEnergy(MatrixA const& A, MatrixB const& B,
420 VectorX const& c, VectorB const& b, VectorX const& x) const;
430 virtual std::tuple<VectorX,VectorB,int> gradient(MatrixA const& A, MatrixB const& B,
431 VectorX const& c, VectorB const& b, VectorX const& x) const;
443 virtual std::tuple<VectorX,VectorX,VectorX,VectorB,int> gradient_extended(MatrixA const& A, MatrixB const& B,
444 VectorX const& c, VectorB const& b, VectorX const& x) const;
445
449 virtual double qpLinesearch(MatrixA const& A, MatrixB const& B, VectorX const& c,
450 VectorB const& b, VectorX const& dx) const;
451
452
453 private:
454 mutable Real gamma;
455 };
456
457 // ----------------------------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------------------------
459
460 // forward declaration
461 template <int d, class Real, QPStructure sparsity=QPStructure::DENSE>
463
464 template <int d, class Prolongation, class Real=double, class Smoother=QPSmoother<d,Real>, class CoarseSmoother=QPSmoother<d,Real>>
466 : public QPMultiGridBase<d,Prolongation,Smoother,CoarseSmoother,Real>
467 {
469
470 public:
472 using typename Base::MatrixA;
473 using typename Base::MatrixB;
474 using typename Base::VectorX;
475 using typename Base::VectorB;
476
492 QPMultiGridStrict(MatrixA const& A, MatrixB const& B, std::vector<Prolongation>&& prolongations,
493 Real smootherRegularization=0, bool blocks=true, bool directOnCoarse=true);
494
499
500
511 std::tuple<VectorX,int> solve(VectorX x, VectorX const& c, VectorB const& b, double tol, int vcycles) const;
512
513
514 protected:
515 virtual double computeEnergy(MatrixA const& A, MatrixB const& B,
516 VectorX const& c, VectorB const& b, VectorX const& x) const;
517
525 virtual std::tuple<VectorX,VectorB,int> gradient(MatrixA const& A, MatrixB const& B,
526 VectorX const& c, VectorB const& b, VectorX const& x) const;
527
528
540 virtual std::tuple<VectorX,VectorX,VectorX,VectorB,int> gradient_extended(MatrixA const& A, MatrixB const& B,
541 VectorX const& c, VectorB const& b, VectorX const& x) const;
542
543 virtual double qpLinesearch(MatrixA const& A, MatrixB const& B, VectorX const& c,
544 VectorB const& b, VectorX const& dx) const;
545 };
546}
547
548#endif
An interface for gathering multigrid solver statistics.
Definition: qpmg.hh:38
virtual void enterPostSmoothingCorrection(int level, VectorX const &dx)
Definition: qpmg.hh:47
virtual void enterVCycleCorrection(int iter, VectorX const &dx, Real energy, VectorX const &grad, VectorX const &grad0, VectorX const &grad1, VectorB const &cons, int activeSetChanges, std::vector< VectorX > &correctionStack, std::vector< std::tuple< double, double, double > > &coarseRes)
At the end of a V-cycle, specify the iteration number, the correction, and the resulting energy at th...
Definition: qpmg.hh:55
virtual void enterPreSmoothingCorrection(int level, VectorX const &dx)
Definition: qpmg.hh:45
virtual void enterCoarseGridCorrection(int level, VectorX const &dx)
Definition: qpmg.hh:46
Class for multigrid stacks.
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
A base class for multigrid solver for QPs.
Definition: qpmg.hh:93
QPMultiGridBase(MatrixA A, MatrixB const &B, std::vector< Prolongation > &&prolongations, Real smootherRegularization=0, bool blocks=true, bool directOnCoarse=true)
Constructs the solver, providing the matrices and .
virtual double qpLinesearch(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &dx) const =0
Compute the optimal step size in direction of dx.
Dune::BlockVector< Dune::FieldVector< Real, d > > VectorX
Definition: qpmg.hh:96
NumaBCRSMatrix< EntryA > MatrixA
Definition: qpmg.hh:98
MatrixB const & B() const
Provides a reference to the fine grid constraints.
virtual std::tuple< VectorX, VectorB, int > gradient(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const =0
Compute the problem gradient of current iterate.
virtual double computeEnergy(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const =0
Compute the problem energy of current iterate for logging purposes.
virtual std::tuple< VectorX, VectorX, VectorX, VectorB, int > gradient_extended(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const =0
Computes the gradient of current iterate, giving more information on the components of the gradient....
VectorX step(VectorX const &x, VectorX c, VectorB b) const
Computes a single V-cycle correction for approximately solving the QP for the given right hand side v...
std::vector< SmootherType > smoothers
Definition: qpmg.hh:228
NumaBCRSMatrix< Dune::FieldMatrix< Real, 1, d > > MatrixB
Definition: qpmg.hh:99
static constexpr bool smoothersEqual
Definition: qpmg.hh:104
std::conditional_t< smoothersEqual, Smoother, std::variant< Smoother, CoarseSmoother > > SmootherType
Definition: qpmg.hh:107
virtual ~QPMultiGridBase()
Destructor.
Self & setBulkMode(ParallelMode m)
Defines the smoothing strategy to use for unconstrained degrees of freedom.
Dune::BlockVector< Dune::FieldVector< Real, 1 > > VectorB
Definition: qpmg.hh:97
std::tuple< VectorX, int > solve(VectorX x, VectorX const &c, VectorB const &b, double tol, int vcycles) const
Approximately solves the QP for the given right hand side vectors up to a given tolerance.
Self & setLogger(MGSolverStatistics< d, Real > *logger)
Sets the logging facility for reporting solver statistics.
Self & setSmoothings(int pre, int post)
Self & setCoarseCorrections(int n)
Sets the number of coarse corrections to compute and apply.
Self & setCoarseLevel(int coarselevel)
Defines the grid level to be used as coarse grid.
A multigrid solver for convex QPs.
Definition: qpmg.hh:313
virtual double qpLinesearch(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &dx) const
QPMultiGrid(MatrixA const &A, MatrixB const &B, std::vector< Prolongation > &&prolongations, Real smootherRegularization=0, bool blocks=true, bool directOnCoarse=true)
Constructs the solver, providing the matrices and .
double getPenalty()
Returns the current penalty factor .
Self & setConstraintsMode(ParallelMode m)
std::tuple< VectorX, VectorB, int > solve(VectorX x, VectorX const &c, VectorB const &b, VectorB const &lambda, double tol, int vcycles) const
Approximately solves the QP for the given right hand side vectors up to a given tolerance.
virtual std::tuple< VectorX, VectorX, VectorX, VectorB, int > gradient_extended(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const
Computes the gradient of the penalized energy function.
~QPMultiGrid()
Destructor.
Self & setPenalty(Real gamma, bool levelDependent=false)
Sets the penalty factor .
virtual double computeEnergy(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const
Computes the energy, given by the penalized energy function.
Self & setGlobalMode(ParallelMode m)
virtual std::tuple< VectorX, VectorB, int > gradient(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const
Computes the gradient of the penalized energy function.
VectorB firstOrderUpdate(VectorX const &x, VectorB const &b, VectorB const &lambda) const
A first order update for the Lagrange multiplier.
QPMultiGridStrict(MatrixA const &A, MatrixB const &B, std::vector< Prolongation > &&prolongations, Real smootherRegularization=0, bool blocks=true, bool directOnCoarse=true)
Constructs the solver, providing the matrices and .
std::tuple< VectorX, int > solve(VectorX x, VectorX const &c, VectorB const &b, double tol, int vcycles) const
Approximately solves the QP for the given right hand side vectors up to a given tolerance.
virtual std::tuple< VectorX, VectorB, int > gradient(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const
Computes the gradient of the (non-augmented) Lagrangian.
virtual double qpLinesearch(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &dx) const
~QPMultiGridStrict()
Destructor.
virtual std::tuple< VectorX, VectorX, VectorX, VectorB, int > gradient_extended(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const
Computes the gradient of the (non-augmented) Lagrangian.
virtual double computeEnergy(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x) const
ParallelMode
Whether to execute some algorithm in sequential or parallel manner.
Definition: enums.hh:84