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-2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "SmoothSolver.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Type, class DType, class LUType>
34 (
35  const word& fieldName,
36  const LduMatrix<Type, DType, LUType>& matrix,
37  const dictionary& solverDict
38 )
39 :
41  (
42  fieldName,
43  matrix,
44  solverDict
45  ),
46  nSweeps_(1)
47 {
48  readControls();
49 }
50 
51 
52 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
53 
54 template<class Type, class DType, class LUType>
56 {
58  this->readControl(this->controlDict_, nSweeps_, "nSweeps");
59 }
60 
61 
62 template<class Type, class DType, class LUType>
65 {
66  // --- Setup class containing solver performance data
67  SolverPerformance<Type> solverPerf
68  (
69  typeName,
70  this->fieldName_
71  );
72 
73  label nIter = 0;
74 
75  // If the nSweeps_ is negative do a fixed number of sweeps
76  if (nSweeps_ < 0)
77  {
80  (
81  this->fieldName_,
82  this->matrix_,
83  this->controlDict_
84  );
85 
86  smootherPtr->smooth(psi, -nSweeps_);
87 
88  nIter -= nSweeps_;
89  }
90  else
91  {
92  Type normFactor = Zero;
93 
94  {
95  Field<Type> Apsi(psi.size());
96  Field<Type> temp(psi.size());
97 
98  // Calculate A.psi
99  this->matrix_.Amul(Apsi, psi);
100 
101  // Calculate normalisation factor
102  normFactor = this->normFactor(psi, Apsi, temp);
103 
104  // Calculate residual magnitude
105  solverPerf.initialResidual() = cmptDivide
106  (
107  gSumCmptMag(this->matrix_.source() - Apsi),
108  normFactor
109  );
110  solverPerf.finalResidual() = solverPerf.initialResidual();
111  }
112 
114  {
115  Info<< " Normalisation factor = " << normFactor << endl;
116  }
117 
118 
119  // Check convergence, solve if not converged
120  if
121  (
122  this->minIter_ > 0
123  || !solverPerf.checkConvergence(this->tolerance_, this->relTol_)
124  )
125  {
128  (
129  this->fieldName_,
130  this->matrix_,
131  this->controlDict_
132  );
133 
134  // Smoothing loop
135  do
136  {
137  smootherPtr->smooth
138  (
139  psi,
140  nSweeps_
141  );
142 
143  // Calculate the residual to check convergence
144  solverPerf.finalResidual() = cmptDivide
145  (
146  gSumCmptMag(this->matrix_.residual(psi)),
147  normFactor
148  );
149  } while
150  (
151  (
152  (nIter += nSweeps_) < this->maxIter_
153  && !solverPerf.checkConvergence(this->tolerance_, this->relTol_)
154  )
155  || nIter < this->minIter_
156  );
157  }
158  }
159 
160  solverPerf.nIterations() =
162 
163  return solverPerf;
164 }
165 
166 
167 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::SmoothSolver::SmoothSolver
SmoothSolver(const word &fieldName, const LduMatrix< Type, DType, LUType > &matrix, const dictionary &solverDict)
Construct from matrix components and solver data dictionary.
Definition: SmoothSolver.C:34
Foam::SmoothSolver::readControls
virtual void readControls()
Read the control parameters from the controlDict_.
Definition: SmoothSolver.C:55
Foam::SolverPerformance::finalResidual
const Type & finalResidual() const
Return final residual.
Definition: SolverPerformance.H:182
Foam::LduMatrix
LduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition: LduMatrix.H:72
Foam::one
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:61
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
SmoothSolver.H
Foam::LduMatrix::solver
Abstract base-class for LduMatrix solvers.
Definition: LduMatrix.H:115
Foam::gSumCmptMag
Type gSumCmptMag(const UList< Type > &f, const label comm)
Definition: FieldFunctions.C:592
Foam::SolverPerformance::checkConvergence
bool checkConvergence(const Type &tolerance, const Type &relTolerance)
Check, store and return convergence.
Definition: SolverPerformance.C:63
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::SolverPerformance::nIterations
const labelType & nIterations() const
Return number of iterations.
Definition: SolverPerformance.H:195
Foam::SolverPerformance::initialResidual
const Type & initialResidual() const
Return initial residual.
Definition: SolverPerformance.H:169
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::cmptDivide
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
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:51
Foam::SmoothSolver::solve
virtual SolverPerformance< Type > solve(Field< Type > &psi) const
Solve the matrix with this solver.
Definition: SmoothSolver.C:64