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 | Copyright (C) 2011-2016 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class Type, class DType, class LUType>
32 (
33  const word& fieldName,
34  const LduMatrix<Type, DType, LUType>& matrix,
35  const dictionary& solverDict
36 )
37 :
39  (
40  fieldName,
41  matrix,
42  solverDict
43  ),
44  nSweeps_(1)
45 {
46  readControls();
47 }
48 
49 
50 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
51 
52 template<class Type, class DType, class LUType>
54 {
56  this->readControl(this->controlDict_, nSweeps_, "nSweeps");
57 }
58 
59 
60 template<class Type, class DType, class LUType>
63 {
64  // --- Setup class containing solver performance data
65  SolverPerformance<Type> solverPerf
66  (
67  typeName,
68  this->fieldName_
69  );
70 
71  // If the nSweeps_ is negative do a fixed number of sweeps
72  if (nSweeps_ < 0)
73  {
76  (
77  this->fieldName_,
78  this->matrix_,
79  this->controlDict_
80  );
81 
82  smootherPtr->smooth(psi, -nSweeps_);
83 
84  solverPerf.nIterations() -= nSweeps_;
85  }
86  else
87  {
88  Type normFactor = Zero;
89 
90  {
91  Field<Type> Apsi(psi.size());
92  Field<Type> temp(psi.size());
93 
94  // Calculate A.psi
95  this->matrix_.Amul(Apsi, psi);
96 
97  // Calculate normalisation factor
98  normFactor = this->normFactor(psi, Apsi, temp);
99 
100  // Calculate residual magnitude
101  solverPerf.initialResidual() = cmptDivide
102  (
103  gSumCmptMag(this->matrix_.source() - Apsi),
104  normFactor
105  );
106  solverPerf.finalResidual() = solverPerf.initialResidual();
107  }
108 
110  {
111  Info<< " Normalisation factor = " << normFactor << endl;
112  }
113 
114 
115  // Check convergence, solve if not converged
116  if
117  (
118  this->minIter_ > 0
119  || !solverPerf.checkConvergence(this->tolerance_, this->relTol_)
120  )
121  {
124  (
125  this->fieldName_,
126  this->matrix_,
127  this->controlDict_
128  );
129 
130  // Smoothing loop
131  do
132  {
133  smootherPtr->smooth
134  (
135  psi,
136  nSweeps_
137  );
138 
139  // Calculate the residual to check convergence
140  solverPerf.finalResidual() = cmptDivide
141  (
142  gSumCmptMag(this->matrix_.residual(psi)),
143  normFactor
144  );
145  } while
146  (
147  (
148  (solverPerf.nIterations() += nSweeps_) < this->maxIter_
149  && !solverPerf.checkConvergence(this->tolerance_, this->relTol_)
150  )
151  || solverPerf.nIterations() < this->minIter_
152  );
153  }
154  }
155 
156  return solverPerf;
157 }
158 
159 
160 // ************************************************************************* //
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
virtual SolverPerformance< Type > solve(Field< Type > &psi) const
Solve the matrix with this solver.
Definition: SmoothSolver.C:62
const Type & finalResidual() const
Return final residual.
void size(const label)
Override size to be inconsistent with allocated storage.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Type gSumCmptMag(const UList< Type > &f, const label comm)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
bool checkConvergence(const Type &tolerance, const Type &relTolerance)
Check, store and return convergence.
Pre-declare SubField and related Field type.
Definition: Field.H:57
A class for handling words, derived from string.
Definition: word.H:59
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics...
label nIterations() const
Return number of iterations.
virtual void readControls()
Read the control parameters from the controlDict_.
Definition: SmoothSolver.C:53
static const zero Zero
Definition: zero.H:91
SmoothSolver(const word &fieldName, const LduMatrix< Type, DType, LUType > &matrix, const dictionary &solverDict)
Construct from matrix components and solver data dictionary.
Definition: SmoothSolver.C:32
Abstract base-class for LduMatrix solvers.
Definition: LduMatrix.H:112
LduMatrix is a general matrix class in which the coefficients are stored as three arrays...
Definition: LduMatrix.H:69
const Type & initialResidual() const
Return initial residual.
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:53
const volScalarField & psi