Loading...
Searching...
No Matches
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 Copyright (C) 2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
31// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32
33template<class Type, class DType, class LUType>
35(
36 const word& fieldName,
38 const dictionary& solverDict
39)
40:
41 LduMatrix<Type, DType, LUType>::solver
42 (
44 matrix,
45 solverDict
46 ),
47 nSweeps_(1)
48{
50}
51
52
53// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
54
55template<class Type, class DType, class LUType>
57{
59 this->controlDict_.readIfPresent("nSweeps", nSweeps_);
60}
61
62
63template<class Type, class DType, class LUType>
66{
67 // --- Setup class containing solver performance data
69 (
71 this->fieldName_
72 );
73
74 label nIter = 0;
75
76 // If the nSweeps_ is negative do a fixed number of sweeps
77 if (nSweeps_ < 0)
78 {
81 (
82 this->fieldName_,
83 this->matrix_,
84 this->controlDict_
85 );
86
87 smootherPtr->smooth(psi, -nSweeps_);
88
89 nIter -= nSweeps_;
90 }
91 else
92 {
93 Type normFactor = Zero;
94
95 {
96 Field<Type> Apsi(psi.size());
97 Field<Type> temp(psi.size());
98
99 // Calculate A.psi
100 this->matrix_.Amul(Apsi, psi);
101
102 // Calculate normalisation factor
103 normFactor = this->normFactor(psi, Apsi, temp);
104
105 // Calculate residual magnitude
106 solverPerf.initialResidual() = cmptDivide
107 (
108 gSumCmptMag(this->matrix_.source() - Apsi),
109 normFactor
110 );
111 solverPerf.finalResidual() = solverPerf.initialResidual();
112 }
113
114 if ((this->log_ >= 2) || (LduMatrix<Type, DType, LUType>::debug >= 2))
115 {
116 Info<< " Normalisation factor = " << normFactor << endl;
117 }
118
119
120 // Check convergence, solve if not converged
121 if
122 (
123 this->minIter_ > 0
124 || !solverPerf.checkConvergence
125 (
126 this->tolerance_,
127 this->relTol_,
128 this->log_
129 )
130 )
131 {
134 (
135 this->fieldName_,
136 this->matrix_,
137 this->controlDict_
138 );
139
140 // Smoothing loop
141 do
142 {
143 smootherPtr->smooth
144 (
145 psi,
146 nSweeps_
147 );
148
149 // Calculate the residual to check convergence
150 solverPerf.finalResidual() = cmptDivide
151 (
152 gSumCmptMag(this->matrix_.residual(psi)),
153 normFactor
154 );
155 } while
156 (
157 (
158 (nIter += nSweeps_) < this->maxIter_
159 && !solverPerf.checkConvergence
160 (
161 this->tolerance_,
162 this->relTol_,
163 this->log_
164 )
165 )
166 || nIter < this->minIter_
167 );
168 }
169 }
170
171 solverPerf.nIterations() =
173
174 return solverPerf;
175}
176
177
178// ************************************************************************* //
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
static autoPtr< smoother > New(const word &fieldName, const LduMatrix< Type, DType, LUType > &matrix, const dictionary &smootherDict)
Return a new smoother.
label maxIter_
Maximum number of iterations in the solver.
Definition LduMatrix.H:163
solver(const word &fieldName, const LduMatrix< Type, DType, LUType > &matrix, const dictionary &solverDict)
Construct for given field name, matrix and controls.
const LduMatrix< Type, DType, LUType > & matrix() const noexcept
Definition LduMatrix.H:287
Type normFactor(const Field< Type > &psi, const Field< Type > &Apsi, Field< Type > &tmpField, const lduMatrix::normTypes normType) const
Return the matrix norm using the specified norm method.
label minIter_
Minimum number of iterations in the solver.
Definition LduMatrix.H:158
int log_
Verbosity level for solver output statements.
Definition LduMatrix.H:153
virtual void readControls()
Read the control parameters from controlDict_.
const LduMatrix< Type, DType, LUType > & matrix_
Definition LduMatrix.H:143
dictionary controlDict_
Dictionary of solution controls.
Definition LduMatrix.H:148
const word & fieldName() const noexcept
Definition LduMatrix.H:282
LduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition LduMatrix.H:84
SmoothSolver(const word &fieldName, const LduMatrix< Type, DType, LUType > &matrix, const dictionary &solverDict)
Construct from matrix components and solver data dictionary.
label nSweeps_
Number of sweeps before the evaluation of residual.
virtual void readControls()
Read the control parameters from the controlDict_.
virtual SolverPerformance< Type > solve(Field< Type > &psi) const
Solve the matrix with this solver.
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics.
const Type & finalResidual() const noexcept
Return final residual.
const labelType & nIterations() const noexcept
Return number of iterations.
const Type & initialResidual() const noexcept
Return initial residual.
bool checkConvergence(const Type &tolerance, const Type &relTolerance, const int logLevel=0)
Check, store and return convergence.
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition one.H:57
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
A class for handling words, derived from Foam::string.
Definition word.H:66
const volScalarField & psi
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
messageStream Info
Information stream (stdout output on master, null elsewhere).
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
Type gSumCmptMag(const UList< Type > &f, const label comm)