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-------------------------------------------------------------------------------
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#include "profiling.H"
31#include "PrecisionAdaptor.H"
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35namespace Foam
36{
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{
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// ************************************************************************* //
A const Field/List wrapper with possible data conversion.
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:80
A non-const Field/List wrapper with possible data conversion.
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.
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
virtual void readControls()
Read the control parameters from the controlDict_.
lduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition: lduMatrix.H:84
OSstream & masterStream(const label communicator)
T & ref() const
Definition: refPtrI.H:203
Iterative solver for symmetric and asymmetric matrices which uses a run-time selected smoother e....
Definition: smoothSolver.H:62
virtual void readControls()
Read the control parameters from the controlDict_.
Definition: smoothSolver.C:75
Base class for solution control classes.
Definition: solver.H:54
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
const volScalarField & psi
dynamicFvMesh & mesh
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
lduMatrix::solver::addsymMatrixConstructorToTable< smoothSolver > addsmoothSolverSymMatrixConstructorToTable_
Definition: smoothSolver.C:40
uint8_t direction
Definition: direction.H:56
typeOfMag< Type >::type gSumMag(const FieldField< Field, Type > &f)
lduMatrix::solver::addasymMatrixConstructorToTable< smoothSolver > addsmoothSolverAsymMatrixConstructorToTable_
Definition: smoothSolver.C:43
#define addProfiling(name, descr)
Define profiling trigger with specified name and description string.
CEqn solve()