G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsMultiGrid.h
Go to the documentation of this file.
1
14#pragma once
15
18#include <gsIO/gsOptionList.h>
19
20namespace gismo
21{
22
56template<class T>
58{
59
60public:
61
63 typedef memory::shared_ptr<gsMultiGridOp> Ptr;
64
66 typedef memory::unique_ptr<gsMultiGridOp> uPtr;
67
70
73
76
79
82
85
87 typedef memory::shared_ptr<SpMatrix> SpMatrixPtr;
88
90 typedef memory::shared_ptr<SpMatrixRowMajor> SpMatrixRowMajorPtr;
91
99 SpMatrix fineMatrix,
100 std::vector<SpMatrixRowMajor> transferMatrices,
102 );
103
111 SpMatrixPtr fineMatrix,
112 std::vector<SpMatrixRowMajorPtr> transferMatrices,
114 );
115
124 const std::vector<OpPtr>& ops,
125 const std::vector<OpPtr>& prolongation,
126 const std::vector<OpPtr>& restriction,
128 );
129
136 static uPtr make(
137 SpMatrix fineMatrix,
138 std::vector<SpMatrixRowMajor> transferMatrices,
140 )
141 { return uPtr( new gsMultiGridOp( give(fineMatrix), give(transferMatrices), give(coarseSolver) ) ); }
142
149 static uPtr make(
150 SpMatrixPtr fineMatrix,
151 std::vector<SpMatrixRowMajorPtr> transferMatrices,
153 )
154 { return uPtr( new gsMultiGridOp( give(fineMatrix), give(transferMatrices), give(coarseSolver) ) ); }
155
163 static uPtr make(
164 const std::vector<OpPtr>& ops,
165 const std::vector<OpPtr>& prolongation,
166 const std::vector<OpPtr>& restriction,
168 )
169 { return uPtr( new gsMultiGridOp( ops, prolongation, restriction, give(coarseSolver) ) ); }
170
171private:
173 void init(SpMatrixPtr fineMatrix, std::vector<SpMatrixRowMajorPtr> transferMatrices);
175 void initCoarseSolver() const;
176public:
177
183 void smoothingStep(index_t level, const Matrix& rhs, Matrix& x) const;
184
189 void smoothingStep(const Matrix& rhs, Matrix& x) const
190 { smoothingStep(finestLevel(), rhs, x); }
191
197 void multiGridStep(index_t level, const Matrix& rhs, Matrix& x) const;
198
203 void step(const Matrix& rhs, Matrix& x) const override
204 { multiGridStep(finestLevel(), rhs, x); }
205
213 void stepT(const Matrix& rhs, Matrix& x) const override
214 {
216 step(rhs,x);
218 }
219
225 void fullMultiGrid(
226 const std::vector<Matrix>& rhs,
227 const std::vector<Matrix>& dirichletIntp,
228 Matrix& result
229 ) const;
230
237 const std::vector<Matrix>& rhs,
238 const std::vector<Matrix>& fixedValues,
239 Matrix& result
240 ) const;
241
243 void restrictVector(index_t lf, const Matrix& fine, Matrix& coarse) const;
244
246 void prolongVector(index_t lc, const Matrix& coarse, Matrix& fine) const;
247
249 void solveCoarse(const Matrix& rhs, Matrix& result) const
250 {
252 m_coarseSolver->apply(rhs, result);
253 }
254
256 index_t numLevels() const { return m_nLevels; }
257
259 index_t finestLevel() const { return m_nLevels - 1; }
260
263 {
264 GISMO_ASSERT ( lvl >= 0 && lvl < m_nLevels, "gsMultiGridOp: The given level "<<lvl<<" is not feasible." );
265 m_ops[lvl] = op;
266 }
267
269 OpPtr underlyingOp(index_t lvl) const { return m_ops[lvl]; }
270
272 OpPtr underlyingOp() const override { return m_ops[finestLevel()]; }
273
275 const SpMatrix& matrix(index_t lvl) const;
276
278 const SpMatrix& matrix() const { return matrix(finestLevel()); }
279
281 index_t nDofs(index_t lvl) const { return underlyingOp(lvl)->cols(); }
282
284 index_t nDofs() const { return nDofs( finestLevel() ); }
285
286 // For docs, see base class
287 index_t rows() const override { return underlyingOp()->rows(); }
288
289 // For docs, see base class
290 index_t cols() const override { return underlyingOp()->cols(); }
291
295 void setSmoother(index_t lvl, const PrecondPtr& sm);
296
299 PrecondPtr smoother(index_t lvl) const { return m_smoother[lvl]; }
300
302 void setCoarseSolver(const OpPtr& sol) { m_coarseSolver = sol; }
303
306
309
312
315
318
321 void setSymmSmooth(bool s) { m_symmSmooth = s; }
322
324 bool symmSmooth() const { return m_symmSmooth; }
325
329 {
330 m_numCycles.setConstant(m_nLevels-1,n);
331 // The direct solver on coarsest level is only invoked once
332 m_numCycles[0] = 1;
333 }
334
340 {
341 GISMO_ASSERT(lvl>=0&&lvl<m_nLevels-1, "gsMultiGrid::setNumCycles: Givel level out of bounds.");
342 m_numCycles[lvl] = n;
343 }
344
346 index_t numCycles(index_t lvl) const { return m_numCycles[lvl]; }
347
349 void setCoarseGridCorrectionDamping(T damping) { m_damping = damping; }
350
353
356
358 void setOptions(const gsOptionList& opt) override;
359
360private:
361
364
366 std::vector<OpPtr> m_ops;
367
369 std::vector<PrecondPtr> m_smoother;
370
372 std::vector<OpPtr> m_prolong;
373
375 std::vector<OpPtr> m_restrict;
376
379
382
385
388
391
394
395}; // class gsMultiGridOp
396
397} // namespace gismo
398
399#ifndef GISMO_BUILD_LIB
400#include GISMO_HPP_HEADER(gsMultiGrid.hpp)
401#endif
memory::shared_ptr< gsLinearOperator > Ptr
Shared pointer for gsLinearOperator.
Definition gsLinearOperator.h:33
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Multigrid preconditioner.
Definition gsMultiGrid.h:58
index_t setNumPostSmooth() const
Get number of post-smoothing steps to perform.
Definition gsMultiGrid.h:317
index_t m_nLevels
Number of levels.
Definition gsMultiGrid.h:363
memory::shared_ptr< gsMultiGridOp > Ptr
Shared pointer for gsMultiGridOp.
Definition gsMultiGrid.h:63
gsMatrix< T > Matrix
Matrix type.
Definition gsMultiGrid.h:78
index_t numLevels() const
Number of levels in the multigrid construction.
Definition gsMultiGrid.h:256
static uPtr make(const std::vector< OpPtr > &ops, const std::vector< OpPtr > &prolongation, const std::vector< OpPtr > &restriction, OpPtr coarseSolver=OpPtr())
Make function returning a shared pointer for a matix-free variant.
Definition gsMultiGrid.h:163
index_t m_numPreSmooth
Number of pre-smoothing steps.
Definition gsMultiGrid.h:381
void restrictVector(index_t lf, const Matrix &fine, Matrix &coarse) const
Restrict a vector of coefficients from the level lf, given by fine, to the next coarser level.
Definition gsMultiGrid.hpp:266
std::vector< PrecondPtr > m_smoother
Smoothers on each level.
Definition gsMultiGrid.h:369
void setNumPostSmooth(index_t n)
Set number of post-smoothing steps to perform.
Definition gsMultiGrid.h:314
void stepT(const Matrix &rhs, Matrix &x) const override
Perform one transposed multigrid cycle.
Definition gsMultiGrid.h:213
gsLinearOperator< T >::Ptr OpPtr
Shared pointer to gsLinearOperator.
Definition gsMultiGrid.h:69
void setOptions(const gsOptionList &opt) override
Set the options based on a gsOptionList.
Definition gsMultiGrid.hpp:313
index_t setNumPreSmooth() const
Get number of pre-smoothing steps to perform.
Definition gsMultiGrid.h:311
index_t rows() const override
Returns the number of rows of the operator.
Definition gsMultiGrid.h:287
OpPtr coarseSolver() const
Get the solver for the coarsest problem (level 0)
Definition gsMultiGrid.h:305
void init(SpMatrixPtr fineMatrix, std::vector< SpMatrixRowMajorPtr > transferMatrices)
Init function that is used by matrix based constructors.
Definition gsMultiGrid.hpp:70
void setNumCycles(index_t n)
Definition gsMultiGrid.h:328
T coarseGridCorrectionDamping() const
Get the damping of for the coarse-grid correction.
Definition gsMultiGrid.h:352
void prolongVector(index_t lc, const Matrix &coarse, Matrix &fine) const
Prolong a vector of coefficients from the level lc, given by coarse, to the next finer level.
Definition gsMultiGrid.hpp:275
void setSymmSmooth(bool s)
Definition gsMultiGrid.h:321
bool m_symmSmooth
Symmmetric smoothing.
Definition gsMultiGrid.h:387
void step(const Matrix &rhs, Matrix &x) const override
Perform one multigrid cycle.
Definition gsMultiGrid.h:203
T m_damping
Damping for the coarse-grid correction.
Definition gsMultiGrid.h:393
std::vector< OpPtr > m_restrict
Restriction matrix for each grid transition.
Definition gsMultiGrid.h:375
void setSmoother(index_t lvl, const PrecondPtr &sm)
Definition gsMultiGrid.hpp:284
index_t nDofs() const
Number of dofs for the finest level.
Definition gsMultiGrid.h:284
void smoothingStep(const Matrix &rhs, Matrix &x) const
Apply smoothing steps on finest grid.
Definition gsMultiGrid.h:189
void setCoarseSolver(const OpPtr &sol)
Set the solver for the coarsest problem (level 0)
Definition gsMultiGrid.h:302
void smoothingStep(index_t level, const Matrix &rhs, Matrix &x) const
Apply smoothing steps on the corresponding level.
Definition gsMultiGrid.hpp:125
std::vector< OpPtr > m_ops
Underlying operators (=stiffness matrix) on each level.
Definition gsMultiGrid.h:366
gsSparseMatrix< T, RowMajor > SpMatrixRowMajor
Matrix type for transfers.
Definition gsMultiGrid.h:84
index_t finestLevel() const
The index of the finest level (0-based)
Definition gsMultiGrid.h:259
gsPreconditionerOp< T >::Ptr PrecondPtr
Shared pointer to gsPreconditionerOp.
Definition gsMultiGrid.h:72
memory::shared_ptr< SpMatrix > SpMatrixPtr
Smart pointer to sparse matrix type.
Definition gsMultiGrid.h:87
void solveCoarse(const Matrix &rhs, Matrix &result) const
Solve the problem with direct solver on coarsest level.
Definition gsMultiGrid.h:249
static uPtr make(SpMatrixPtr fineMatrix, std::vector< SpMatrixRowMajorPtr > transferMatrices, OpPtr coarseSolver=OpPtr())
Make function returning smart pointer.
Definition gsMultiGrid.h:149
void cascadicMultiGrid(const std::vector< Matrix > &rhs, const std::vector< Matrix > &fixedValues, Matrix &result) const
Perform one cascadic multigrid cycle and store the resulting solution vector in result.
Definition gsMultiGrid.hpp:235
bool symmSmooth() const
Get symmetric post-smoothing option.
Definition gsMultiGrid.h:324
PrecondPtr smoother(index_t lvl) const
Definition gsMultiGrid.h:299
OpPtr m_coarseSolver
Solver for the coarsest-grid problem.
Definition gsMultiGrid.h:378
static uPtr make(SpMatrix fineMatrix, std::vector< SpMatrixRowMajor > transferMatrices, OpPtr coarseSolver=OpPtr())
Make function returning smart pointer.
Definition gsMultiGrid.h:136
const SpMatrix & matrix() const
Stiffness matrix for finest level.
Definition gsMultiGrid.h:278
void initCoarseSolver() const
Init solver on coarsest grid level.
Definition gsMultiGrid.hpp:102
OpPtr underlyingOp() const override
Underlying operator (=stiffness matrix) for finest level.
Definition gsMultiGrid.h:272
index_t cols() const override
Returns the number of columns of the operator.
Definition gsMultiGrid.h:290
void fullMultiGrid(const std::vector< Matrix > &rhs, const std::vector< Matrix > &dirichletIntp, Matrix &result) const
Perform one full multigrid cycle and store the resulting solution vector in result.
Definition gsMultiGrid.hpp:204
gsVector< index_t > m_numCycles
Number of coarse-grid steps (1=V-cycle, 2=W-cycle)
Definition gsMultiGrid.h:390
void multiGridStep(index_t level, const Matrix &rhs, Matrix &x) const
Perform one multigrid cycle, starting from the given level.
Definition gsMultiGrid.hpp:148
index_t nDofs(index_t lvl) const
Number of dofs for the given level.
Definition gsMultiGrid.h:281
void setNumCycles(index_t lvl, index_t n)
Definition gsMultiGrid.h:339
gsSparseMatrix< T > SpMatrix
Sparse matrix type.
Definition gsMultiGrid.h:81
static gsOptionList defaultOptions()
Returns a list of default options.
Definition gsMultiGrid.hpp:300
memory::shared_ptr< SpMatrixRowMajor > SpMatrixRowMajorPtr
Smart pointer to matrix type for transfers.
Definition gsMultiGrid.h:90
index_t numCycles(index_t lvl) const
Get number of coarse grid steps to be applied.
Definition gsMultiGrid.h:346
index_t m_numPostSmooth
Number of post-smoothing steps.
Definition gsMultiGrid.h:384
OpPtr underlyingOp(index_t lvl) const
Underlying operator (=stiffness matrix) for given level.
Definition gsMultiGrid.h:269
void setUnderlyingOp(index_t lvl, OpPtr op)
Set underlying operator (=stiffness matrix) for certain level.
Definition gsMultiGrid.h:262
std::vector< OpPtr > m_prolong
Prolongation matrix for each grid transition.
Definition gsMultiGrid.h:372
void setCoarseGridCorrectionDamping(T damping)
Set the damping of for the coarse-grid correction.
Definition gsMultiGrid.h:349
gsPreconditionerOp< T > Base
Direct base class.
Definition gsMultiGrid.h:75
void setNumPreSmooth(index_t n)
Set number of pre-smoothing steps to perform.
Definition gsMultiGrid.h:308
memory::unique_ptr< gsMultiGridOp > uPtr
Unique pointer for gsMultiGridOp.
Definition gsMultiGrid.h:66
Class which holds a list of parameters/options, and provides easy access to them.
Definition gsOptionList.h:33
Simple abstract class for perconditioners.
Definition gsPreconditioner.h:43
memory::shared_ptr< gsPreconditionerOp > Ptr
Shared pointer for gsLinearOperator.
Definition gsPreconditioner.h:47
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition gsSparseMatrix.h:139
A vector with arbitrary coefficient type and fixed or dynamic size.
Definition gsVector.h:37
#define index_t
Definition gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
Provides forward declarations of types and structs.
Provides a list of labeled parameters/options that can be set and accessed easily.
Simple abstract class for (discrete) linear preconditioners.
The G+Smo namespace, containing all definitions for the library.
S give(S &x)
Definition gsMemory.h:266