smoothSolver.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2016-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "smoothSolver.H"
30 #include "profiling.H"
31 #include "PrecisionAdaptor.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(smoothSolver, 0);
38 
39  lduMatrix::solver::addsymMatrixConstructorToTable<smoothSolver>
41 
42  lduMatrix::solver::addasymMatrixConstructorToTable<smoothSolver>
44 }
45 
46 
47 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
48 
50 (
51  const word& fieldName,
52  const lduMatrix& matrix,
53  const FieldField<Field, scalar>& interfaceBouCoeffs,
54  const FieldField<Field, scalar>& interfaceIntCoeffs,
55  const lduInterfaceFieldPtrsList& interfaces,
56  const dictionary& solverControls
57 )
58 :
60  (
61  fieldName,
62  matrix,
63  interfaceBouCoeffs,
64  interfaceIntCoeffs,
65  interfaces,
66  solverControls
67  )
68 {
69  readControls();
70 }
71 
72 
73 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
74 
76 {
78  nSweeps_ = controlDict_.getOrDefault<label>("nSweeps", 1);
79 }
80 
81 
83 (
84  scalarField& psi_s,
85  const scalarField& source,
86  const direction cmpt
87 ) const
88 {
90  solveScalarField& psi = tpsi.ref();
91 
92  // Setup class containing solver performance data
93  solverPerformance solverPerf(typeName, fieldName_);
94 
95  // If the nSweeps_ is negative do a fixed number of sweeps
96  if (nSweeps_ < 0)
97  {
98  addProfiling(solve, "lduMatrix::smoother." + fieldName_);
99 
101  (
102  fieldName_,
103  matrix_,
104  interfaceBouCoeffs_,
105  interfaceIntCoeffs_,
106  interfaces_,
107  controlDict_
108  );
109 
110  smootherPtr->smooth
111  (
112  psi,
113  source,
114  cmpt,
115  -nSweeps_
116  );
117 
118  solverPerf.nIterations() -= nSweeps_;
119  }
120  else
121  {
122  solveScalar normFactor = 0;
123  solveScalarField residual;
124 
126 
127  {
128  solveScalarField Apsi(psi.size());
129  solveScalarField temp(psi.size());
130 
131  // Calculate A.psi
132  matrix_.Amul(Apsi, psi, interfaceBouCoeffs_, interfaces_, cmpt);
133 
134  // Calculate normalisation factor
135  normFactor = this->normFactor(psi, tsource(), Apsi, temp);
136 
137  residual = tsource() - Apsi;
138 
139  matrix().setResidualField
140  (
142  fieldName_,
143  true
144  );
145 
146  // Calculate residual magnitude
147  solverPerf.initialResidual() =
148  gSumMag(residual, matrix().mesh().comm())/normFactor;
149  solverPerf.finalResidual() = solverPerf.initialResidual();
150  }
151 
152  if ((log_ >= 2) || (lduMatrix::debug >= 2))
153  {
154  Info.masterStream(matrix().mesh().comm())
155  << " Normalisation factor = " << normFactor << endl;
156  }
157 
158 
159  // Check convergence, solve if not converged
160  if
161  (
162  minIter_ > 0
163  || !solverPerf.checkConvergence(tolerance_, relTol_, log_)
164  )
165  {
166  addProfiling(solve, "lduMatrix::smoother." + fieldName_);
167 
169  (
170  fieldName_,
171  matrix_,
172  interfaceBouCoeffs_,
173  interfaceIntCoeffs_,
174  interfaces_,
175  controlDict_
176  );
177 
178  // Smoothing loop
179  do
180  {
181  smootherPtr->smooth
182  (
183  psi,
184  source,
185  cmpt,
186  nSweeps_
187  );
188 
189  residual =
190  matrix_.residual
191  (
192  psi,
193  source,
194  interfaceBouCoeffs_,
195  interfaces_,
196  cmpt
197  );
198 
199  // Calculate the residual to check convergence
200  solverPerf.finalResidual() =
201  gSumMag(residual, matrix().mesh().comm())/normFactor;
202  } while
203  (
204  (
205  (solverPerf.nIterations() += nSweeps_) < maxIter_
206  && !solverPerf.checkConvergence(tolerance_, relTol_, log_)
207  )
208  || solverPerf.nIterations() < minIter_
209  );
210  }
211 
212  matrix().setResidualField
213  (
215  fieldName_,
216  false
217  );
218  }
219 
220  return solverPerf;
221 }
222 
223 
224 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
profiling.H
Foam::gSumMag
typeOfMag< Type >::type gSumMag(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:598
Foam::addsmoothSolverAsymMatrixConstructorToTable_
lduMatrix::solver::addasymMatrixConstructorToTable< smoothSolver > addsmoothSolverAsymMatrixConstructorToTable_
Definition: smoothSolver.C:43
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::FieldField
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:53
Foam::SolverPerformance::nIterations
const labelType & nIterations() const noexcept
Return number of iterations.
Definition: SolverPerformance.H:196
Foam::messageStream::masterStream
OSstream & masterStream(const label communicator)
Definition: messageStream.C:140
Foam::lduMatrix
lduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition: lduMatrix.H:83
PrecisionAdaptor.H
Foam::SolverPerformance::finalResidual
const Type & finalResidual() const noexcept
Return final residual.
Definition: SolverPerformance.H:183
Foam::lduMatrix::solver
Abstract base-class for lduMatrix solvers.
Definition: lduMatrix.H:98
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::smoothSolver::readControls
virtual void readControls()
Read the control parameters from the controlDict_.
Definition: smoothSolver.C:75
Foam::smoothSolver::solve
virtual solverPerformance solve(scalarField &psi, const scalarField &source, const direction cmpt=0) const
Solve the matrix with this solver.
Definition: smoothSolver.C:83
smoothSolver.H
Foam::lduMatrix::setResidualField
void setResidualField(const scalarField &residual, const word &fieldName, const bool initial) const
Definition: lduMatrix.C:322
Foam::Field< scalar >
Foam::SolverPerformance::checkConvergence
bool checkConvergence(const Type &tolerance, const Type &relTolerance, const int logLevel=0)
Check, store and return convergence.
Definition: SolverPerformance.C:64
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::smoothSolver::smoothSolver
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:50
Foam::UPtrList< const lduInterfaceField >
Foam::solve
SolverPerformance< Type > solve(faMatrix< Type > &, Istream &)
Solve returning the solution statistics given convergence tolerance.
addProfiling
#define addProfiling(name, descr)
Define profiling trigger with specified name and description string.
Definition: profilingTrigger.H:113
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::lduMatrix::solver::readControls
virtual void readControls()
Read the control parameters from the controlDict_.
Definition: lduMatrixSolver.C:163
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::smoothSolver::nSweeps_
label nSweeps_
Number of sweeps before the evaluation of residual.
Definition: smoothSolver.H:68
Foam::lduMatrix::smoother::New
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.
Definition: lduMatrixSmoother.C:67
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::PrecisionAdaptor
A non-const Field/List wrapper with possible data conversion.
Definition: PrecisionAdaptor.H:186
Foam::lduMatrix::solver::controlDict_
dictionary controlDict_
Dictionary of controls.
Definition: lduMatrix.H:114
Foam::SolverPerformance::initialResidual
const Type & initialResidual() const noexcept
Return initial residual.
Definition: SolverPerformance.H:170
Foam::ConstPrecisionAdaptor
A const Field/List wrapper with possible data conversion.
Definition: PrecisionAdaptor.H:57
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::lduMatrix::smoother::smooth
virtual void smooth(solveScalarField &psi, const scalarField &source, const direction cmpt, const label nSweeps) const =0
Smooth the solution for a given number of sweeps.
Foam::addsmoothSolverSymMatrixConstructorToTable_
lduMatrix::solver::addsymMatrixConstructorToTable< smoothSolver > addsmoothSolverSymMatrixConstructorToTable_
Definition: smoothSolver.C:40
Foam::refPtr< Container< Type > >::ref
Container< Type > & ref() const
Definition: refPtrI.H:203
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
psi
const volScalarField & psi
Definition: createFieldRefs.H:1
Foam::SolverPerformance
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics.
Definition: SolverPerformance.H:52
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)