smoothSolver.C
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-2021 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 \*---------------------------------------------------------------------------*/
25 
26 #include "smoothSolver.H"
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32  defineTypeNameAndDebug(smoothSolver, 0);
33 
34  lduMatrix::solver::addsymMatrixConstructorToTable<smoothSolver>
36 
37  lduMatrix::solver::addasymMatrixConstructorToTable<smoothSolver>
39 }
40 
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
45 (
46  const word& fieldName,
47  const lduMatrix& matrix,
48  const FieldField<Field, scalar>& interfaceBouCoeffs,
49  const FieldField<Field, scalar>& interfaceIntCoeffs,
50  const lduInterfaceFieldPtrsList& interfaces,
51  const dictionary& solverControls
52 )
53 :
55  (
56  fieldName,
57  matrix,
58  interfaceBouCoeffs,
59  interfaceIntCoeffs,
60  interfaces,
61  solverControls
62  )
63 {
64  readControls();
65 }
66 
67 
68 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69 
71 {
73  nSweeps_ = controlDict_.lookupOrDefault<label>("nSweeps", 1);
74 }
75 
76 
78 (
79  scalarField& psi,
80  const scalarField& source,
81  const direction cmpt
82 ) const
83 {
84  // Setup class containing solver performance data
85  solverPerformance solverPerf(typeName, fieldName_);
86 
87  // If the nSweeps_ is negative do a fixed number of sweeps
88  if (nSweeps_ < 0)
89  {
91  (
92  fieldName_,
93  matrix_,
94  interfaceBouCoeffs_,
95  interfaceIntCoeffs_,
96  interfaces_,
97  controlDict_
98  );
99 
100  smootherPtr->smooth
101  (
102  psi,
103  source,
104  cmpt,
105  -nSweeps_
106  );
107 
108  solverPerf.nIterations() -= nSweeps_;
109  }
110  else
111  {
112  scalar normFactor = 0;
113 
114  {
115  scalarField Apsi(psi.size());
116  scalarField temp(psi.size());
117 
118  // Calculate A.psi
119  matrix_.Amul(Apsi, psi, interfaceBouCoeffs_, interfaces_, cmpt);
120 
121  // Calculate normalisation factor
122  normFactor = this->normFactor(psi, source, Apsi, temp);
123 
124  // Calculate residual magnitude
125  solverPerf.initialResidual() = gSumMag
126  (
127  (source - Apsi)(),
128  matrix().mesh().comm()
129  )/normFactor;
130  solverPerf.finalResidual() = solverPerf.initialResidual();
131  }
132 
133  if (lduMatrix::debug >= 2)
134  {
135  Info(matrix().mesh().comm())
136  << " Normalisation factor = " << normFactor << endl;
137  }
138 
139 
140  // Check convergence, solve if not converged
141  if
142  (
143  minIter_ > 0
144  || !solverPerf.checkConvergence(tolerance_, relTol_)
145  )
146  {
148  (
149  fieldName_,
150  matrix_,
151  interfaceBouCoeffs_,
152  interfaceIntCoeffs_,
153  interfaces_,
154  controlDict_
155  );
156 
157  // Smoothing loop
158  do
159  {
160  smootherPtr->smooth
161  (
162  psi,
163  source,
164  cmpt,
165  nSweeps_
166  );
167 
168  // Calculate the residual to check convergence
169  solverPerf.finalResidual() = gSumMag
170  (
171  matrix_.residual
172  (
173  psi,
174  source,
175  interfaceBouCoeffs_,
176  interfaces_,
177  cmpt
178  )(),
179  matrix().mesh().comm()
180  )/normFactor;
181  } while
182  (
183  (
184  (solverPerf.nIterations() += nSweeps_) < maxIter_
185  && !solverPerf.checkConvergence(tolerance_, relTol_)
186  )
187  || solverPerf.nIterations() < minIter_
188  );
189  }
190  }
191 
192  return solverPerf;
193 }
194 
195 
196 // ************************************************************************* //
virtual solverPerformance solve(scalarField &psi, const scalarField &source, const direction cmpt=0) const
Solve the matrix with this solver.
Definition: smoothSolver.C:78
scalar gSumMag(const FieldField< Field, Type > &f)
const labelType & nIterations() const
Return number of iterations.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
uint8_t direction
Definition: direction.H:45
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
virtual label comm() const =0
Return communicator used for parallel communication.
const Type & finalResidual() const
Return final residual.
Generic field type.
Definition: FieldField.H:51
bool checkConvergence(const Type &tolerance, const Type &relTolerance)
Check, store and return convergence.
const Type & initialResidual() const
Return initial residual.
A class for handling words, derived from string.
Definition: word.H:59
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics...
Abstract base-class for lduMatrix solvers.
Definition: lduMatrix.H:93
lduMatrix::solver::addsymMatrixConstructorToTable< smoothSolver > addsmoothSolverSymMatrixConstructorToTable_
Definition: smoothSolver.C:35
lduMatrix::solver::addasymMatrixConstructorToTable< smoothSolver > addsmoothSolverAsymMatrixConstructorToTable_
Definition: smoothSolver.C:38
defineTypeNameAndDebug(combustionModel, 0)
smoothSolver(const word &fieldName, const lduMatrix &matrix, const FieldField< Field, scalar > &interfaceBouCoeffs, const FieldField< Field, scalar > &interfaceIntCoeffs, const lduInterfaceFieldPtrsList &interfaces, const dictionary &solverControls)
Construct from matrix components and solver controls.
Definition: smoothSolver.C:45
virtual void readControls()
Read the control parameters from the controlDict_.
Definition: smoothSolver.C:70
lduMatrix is a general matrix class in which the coefficients are stored as three arrays...
Definition: lduMatrix.H:79
static autoPtr< smoother > New(const word &fieldName, const lduMatrix &matrix, const FieldField< Field, scalar > &interfaceBouCoeffs, const FieldField< Field, scalar > &interfaceIntCoeffs, const lduInterfaceFieldPtrsList &interfaces, const dictionary &solverControls)
Return a new smoother.
messageStream Info
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
const lduMesh & mesh() const
Return the LDU mesh from which the addressing is obtained.
Definition: lduMatrix.H:544
Namespace for OpenFOAM.
virtual void readControls()
Read the control parameters from the controlDict_.