GAMGSolver.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::GAMGSolver
26 
27 Description
28  Geometric agglomerated algebraic multigrid solver.
29 
30  Characteristics:
31  - Requires positive definite, diagonally dominant matrix.
32  - Agglomeration algorithm: selectable and optionally cached.
33  - Restriction operator: summation.
34  - Prolongation operator: injection.
35  - Smoother: Gauss-Seidel.
36  - Coarse matrix creation: central coefficient: summation of fine grid
37  central coefficients with the removal of intra-cluster face;
38  off-diagonal coefficient: summation of off-diagonal faces.
39  - Coarse matrix scaling: performed by correction scaling, using steepest
40  descent optimisation.
41  - Type of cycle: V-cycle with optional pre-smoothing.
42  - Coarsest-level matrix solved using PCG or PBiCGStab.
43 
44 SourceFiles
45  GAMGSolver.C
46  GAMGSolverAgglomerateMatrix.C
47  GAMGSolverInterpolate.C
48  GAMGSolverScale.C
49  GAMGSolverSolve.C
50 
51 \*---------------------------------------------------------------------------*/
52 
53 #ifndef GAMGSolver_H
54 #define GAMGSolver_H
55 
56 #include "GAMGAgglomeration.H"
57 #include "lduMatrix.H"
58 #include "labelField.H"
59 #include "primitiveFields.H"
60 #include "LUscalarMatrix.H"
61 
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 
64 namespace Foam
65 {
66 
67 /*---------------------------------------------------------------------------*\
68  Class GAMGSolver Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 class GAMGSolver
72 :
73  public lduMatrix::solver
74 {
75  // Private Data
76 
77  bool cacheAgglomeration_;
78 
79  //- Number of pre-smoothing sweeps
80  label nPreSweeps_;
81 
82  //- Lever multiplier for the number of pre-smoothing sweeps
83  label preSweepsLevelMultiplier_;
84 
85  //- Maximum number of pre-smoothing sweeps
86  label maxPreSweeps_;
87 
88  //- Number of post-smoothing sweeps
89  label nPostSweeps_;
90 
91  //- Lever multiplier for the number of post-smoothing sweeps
92  label postSweepsLevelMultiplier_;
93 
94  //- Maximum number of post-smoothing sweeps
95  label maxPostSweeps_;
96 
97  //- Number of smoothing sweeps on finest mesh
98  label nFinestSweeps_;
99 
100  //- Choose if the corrections should be interpolated after injection.
101  // By default corrections are not interpolated.
102  bool interpolateCorrection_;
103 
104  //- Choose if the corrections should be scaled.
105  // By default corrections for symmetric matrices are scaled
106  // but not for asymmetric matrices.
107  bool scaleCorrection_;
108 
109  //- Direct or iteratively solve the coarsest level
110  bool directSolveCoarsest_;
111 
112  //- The agglomeration
113  const GAMGAgglomeration& agglomeration_;
114 
115  //- Hierarchy of matrix levels
116  PtrList<lduMatrix> matrixLevels_;
117 
118  //- Hierarchy of interfaces.
119  PtrList<PtrList<lduInterfaceField>> primitiveInterfaceLevels_;
120 
121  //- Hierarchy of interfaces in lduInterfaceFieldPtrs form
122  PtrList<lduInterfaceFieldPtrsList> interfaceLevels_;
123 
124  //- Hierarchy of interface boundary coefficients
125  PtrList<FieldField<Field, scalar>> interfaceLevelsBouCoeffs_;
126 
127  //- Hierarchy of interface internal coefficients
128  PtrList<FieldField<Field, scalar>> interfaceLevelsIntCoeffs_;
129 
130  //- LU decomposed coarsest matrix for direct solution on the master
131  autoPtr<LUscalarMatrix> coarsestLUMatrixPtr_;
132 
133  //- Coarsest matrix solver for iterative solution
134  autoPtr<lduMatrix::solver> coarsestSolverPtr_;
135 
136 
137  // Private Member Functions
138 
139  //- Read control parameters from the control dictionary
140  virtual void readControls();
141 
142  //- Simplified access to interface level
143  const lduInterfaceFieldPtrsList& interfaceLevel
144  (
145  const label i
146  ) const;
147 
148  //- Simplified access to matrix level
149  const lduMatrix& matrixLevel(const label i) const;
150 
151  //- Simplified access to interface boundary coeffs level
152  const FieldField<Field, scalar>& interfaceBouCoeffsLevel
153  (
154  const label i
155  ) const;
156 
157  //- Simplified access to interface internal coeffs level
158  const FieldField<Field, scalar>& interfaceIntCoeffsLevel
159  (
160  const label i
161  ) const;
162 
163  //- Agglomerate coarse matrix. Supply mesh to use - so we can
164  // construct temporary matrix on the fine mesh (instead of the coarse
165  // mesh)
166  void agglomerateMatrix
167  (
168  const label fineLevelIndex,
169  const lduMesh& coarseMesh,
170  const lduInterfacePtrsList& coarseMeshInterfaces
171  );
172 
173  //- Agglomerate coarse interface coefficients
174  void agglomerateInterfaceCoefficients
175  (
176  const label fineLevelIndex,
177  const lduInterfacePtrsList& coarseMeshInterfaces,
178  PtrList<lduInterfaceField>& coarsePrimInterfaces,
179  lduInterfaceFieldPtrsList& coarseInterfaces,
180  FieldField<Field, scalar>& coarseInterfaceBouCoeffs,
181  FieldField<Field, scalar>& coarseInterfaceIntCoeffs
182  ) const;
183 
184  //- Collect matrices from other processors
185  void gatherMatrices
186  (
187  const labelList& procIDs,
188  const lduMesh& dummyMesh,
189  const label meshComm,
190 
191  const lduMatrix& mat,
195 
196  PtrList<lduMatrix>& otherMats,
197  PtrList<FieldField<Field, scalar>>& otherBouCoeffs,
198  PtrList<FieldField<Field, scalar>>& otherIntCoeffs,
199  List<boolList>& otherTransforms,
200  List<List<label>>& otherRanks
201  ) const;
202 
203  //- Agglomerate processor matrices
204  void procAgglomerateMatrix
205  (
206  // Agglomeration information
207  const labelList& procAgglomMap,
208  const List<label>& agglomProcIDs,
209 
210  const label levelI,
211 
212  // Resulting matrix
213  autoPtr<lduMatrix>& allMatrixPtr,
214  FieldField<Field, scalar>& allInterfaceBouCoeffs,
215  FieldField<Field, scalar>& allInterfaceIntCoeffs,
216  PtrList<lduInterfaceField>& allPrimitiveInterfaces,
217  lduInterfaceFieldPtrsList& allInterfaces
218  ) const;
219 
220  //- Agglomerate processor matrices
221  void procAgglomerateMatrix
222  (
223  const labelList& procAgglomMap,
224  const List<label>& agglomProcIDs,
225  const label levelI
226  );
227 
228  //- Interpolate the correction after injected prolongation
229  void interpolate
230  (
231  scalarField& psi,
232  scalarField& Apsi,
233  const lduMatrix& m,
236  const direction cmpt
237  ) const;
238 
239  //- Interpolate the correction after injected prolongation and
240  // re-normalise
241  void interpolate
242  (
243  scalarField& psi,
244  scalarField& Apsi,
245  const lduMatrix& m,
248  const labelList& restrictAddressing,
249  const scalarField& psiC,
250  const direction cmpt
251  ) const;
252 
253  //- Calculate and apply the scaling factor from Acf, coarseSource
254  // and coarseField.
255  // At the same time do a Jacobi iteration on the coarseField using
256  // the Acf provided after the coarseField values are used for the
257  // scaling factor.
258  void scale
259  (
260  scalarField& field,
261  scalarField& Acf,
262  const lduMatrix& A,
263  const FieldField<Field, scalar>& interfaceLevelBouCoeffs,
264  const lduInterfaceFieldPtrsList& interfaceLevel,
265  const scalarField& source,
266  const direction cmpt
267  ) const;
268 
269  //- Initialise the data structures for the V-cycle
270  void initVcycle
271  (
272  PtrList<scalarField>& coarseCorrFields,
273  PtrList<scalarField>& coarseSources,
274  PtrList<lduMatrix::smoother>& smoothers,
275  scalarField& scratch1,
276  scalarField& scratch2
277  ) const;
278 
279 
280  //- Perform a single GAMG V-cycle with pre, post and finest smoothing.
281  void Vcycle
282  (
283  const PtrList<lduMatrix::smoother>& smoothers,
284  scalarField& psi,
285  const scalarField& source,
286  scalarField& Apsi,
287  scalarField& finestCorrection,
288  scalarField& finestResidual,
289 
290  scalarField& scratch1,
291  scalarField& scratch2,
292 
293  PtrList<scalarField>& coarseCorrFields,
294  PtrList<scalarField>& coarseSources,
295  const direction cmpt=0
296  ) const;
297 
298  //- Solve the coarsest level with either an iterative or direct solver
299  void solveCoarsestLevel
300  (
301  scalarField& coarsestCorrField,
302  const scalarField& coarsestSource
303  ) const;
304 
305 
306 public:
307 
308  friend class GAMGPreconditioner;
309 
310  //- Runtime type information
311  TypeName("GAMG");
312 
313 
314  // Constructors
315 
316  //- Construct from lduMatrix and solver controls
317  GAMGSolver
318  (
319  const word& fieldName,
320  const lduMatrix& matrix,
324  const dictionary& solverControls
325  );
326 
327 
328  //- Destructor
329  virtual ~GAMGSolver();
330 
331 
332  // Member Functions
333 
334  //- Solve
335  virtual solverPerformance solve
336  (
337  scalarField& psi,
338  const scalarField& source,
339  const direction cmpt=0
340  ) const;
341 };
342 
343 
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
345 
346 } // End namespace Foam
347 
348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
349 
350 #endif
351 
352 // ************************************************************************* //
Generic field type.
Definition: FieldField.H:77
Geometric agglomerated algebraic multigrid agglomeration class.
Geometric agglomerated algebraic multigrid preconditioner.
Geometric agglomerated algebraic multigrid solver.
Definition: GAMGSolver.H:73
GAMGSolver(const word &fieldName, const lduMatrix &matrix, const FieldField< Field, scalar > &interfaceBouCoeffs, const FieldField< Field, scalar > &interfaceIntCoeffs, const lduInterfaceFieldPtrsList &interfaces, const dictionary &solverControls)
Construct from lduMatrix and solver controls.
Definition: GAMGSolver.C:49
TypeName("GAMG")
Runtime type information.
virtual ~GAMGSolver()
Destructor.
Definition: GAMGSolver.C:337
virtual solverPerformance solve(scalarField &psi, const scalarField &source, const direction cmpt=0) const
Solve.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Abstract base-class for lduMatrix solvers.
Definition: lduMatrix.H:94
const lduInterfaceFieldPtrsList & interfaces() const
Definition: lduMatrix.H:241
const FieldField< Field, scalar > & interfaceIntCoeffs() const
Definition: lduMatrix.H:236
const word & fieldName() const
Definition: lduMatrix.H:221
const lduMatrix & matrix() const
Definition: lduMatrix.H:226
const FieldField< Field, scalar > & interfaceBouCoeffs() const
Definition: lduMatrix.H:231
lduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition: lduMatrix.H:80
Abstract base class for meshes which provide LDU addressing for the construction of lduMatrix and LDU...
Definition: lduMesh.H:60
A class for handling words, derived from string.
Definition: word.H:63
const volScalarField & psi
static const coefficient A("A", dimPressure, 611.21)
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
uint8_t direction
Definition: direction.H:45
Specialisations of Field<T> for scalar, vector and tensor.