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-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
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 )
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
125 ConstPrecisionAdaptor<solveScalar, scalar> tsource(source);
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 (
141 ConstPrecisionAdaptor<scalar, solveScalar>(residual)(),
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:77
A non-const Field/List wrapper with possible data conversion.
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
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.
const FieldField< Field, scalar > & interfaceIntCoeffs() const noexcept
Definition lduMatrix.H:338
const lduMatrix & matrix_
Definition lduMatrix.H:158
label maxIter_
Maximum number of iterations in the solver.
Definition lduMatrix.H:181
solver(const word &fieldName, const lduMatrix &matrix, const FieldField< Field, scalar > &interfaceBouCoeffs, const FieldField< Field, scalar > &interfaceIntCoeffs, const lduInterfaceFieldPtrsList &interfaces, const dictionary &solverControls)
Construct solver for given field name, matrix etc.
scalar tolerance_
Final convergence tolerance.
Definition lduMatrix.H:191
const lduInterfaceFieldPtrsList & interfaces() const noexcept
Definition lduMatrix.H:343
lduInterfaceFieldPtrsList interfaces_
Definition lduMatrix.H:161
const FieldField< Field, scalar > & interfaceBouCoeffs_
Definition lduMatrix.H:159
label minIter_
Minimum number of iterations in the solver.
Definition lduMatrix.H:176
const lduMatrix & matrix() const noexcept
Definition lduMatrix.H:328
solveScalarField::cmptType normFactor(const solveScalarField &psi, const solveScalarField &source, const solveScalarField &Apsi, solveScalarField &tmpField, const lduMatrix::normTypes normType) const
Return the matrix norm using the specified norm method.
int log_
Verbosity level for solver output statements.
Definition lduMatrix.H:171
scalar relTol_
Convergence tolerance relative to the initial.
Definition lduMatrix.H:196
const FieldField< Field, scalar > & interfaceIntCoeffs_
Definition lduMatrix.H:160
virtual void readControls()
Read the control parameters from controlDict_.
const FieldField< Field, scalar > & interfaceBouCoeffs() const noexcept
Definition lduMatrix.H:333
dictionary controlDict_
Dictionary of solution controls.
Definition lduMatrix.H:166
const word & fieldName() const noexcept
Definition lduMatrix.H:323
lduMatrix is a general matrix class in which the coefficients are stored as three arrays,...
Definition lduMatrix.H:81
T & ref() const
Return non-const reference to the contents of a non-null managed pointer.
Definition refPtrI.H:230
Iterative solver for symmetric and asymmetric matrices which uses a run-time selected smoother e....
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.
label nSweeps_
Number of sweeps before the evaluation of residual.
virtual void readControls()
Read the control parameters from the controlDict_.
virtual solverPerformance solve(scalarField &psi, const scalarField &source, const direction cmpt=0) const
Solve the matrix with this solver.
Base solver class.
Definition solver.H:48
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
const volScalarField & psi
dynamicFvMesh & mesh
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere).
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
lduMatrix::solver::addsymMatrixConstructorToTable< smoothSolver > addsmoothSolverSymMatrixConstructorToTable_
Field< solveScalar > solveScalarField
SolverPerformance< Type > solve(faMatrix< Type > &, const dictionary &solverControls)
Solve returning the solution statistics given convergence tolerance.
uint8_t direction
Definition direction.H:49
typeOfMag< Type >::type gSumMag(const FieldField< Field, Type > &f)
lduMatrix::solver::addasymMatrixConstructorToTable< smoothSolver > addsmoothSolverAsymMatrixConstructorToTable_
UPtrList< const lduInterfaceField > lduInterfaceFieldPtrsList
List of coupled interface fields to be used in coupling.
SolverPerformance< scalar > solverPerformance
SolverPerformance instantiated for a scalar.
#define addProfiling(Name,...)
Define profiling trigger with specified name and description string. The description is generated by ...