SolverPerformance.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 "SolverPerformance.H"
30#include "IOstreams.H"
31
32// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33
34template<class Type>
36(
37 const Type& wApA
38)
39{
40 for(direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
41 {
42 singular_[cmpt] =
43 component(wApA, cmpt) < vsmall_;
44 }
45
46 return singular();
47}
48
49
50template<class Type>
52{
53 for(direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
54 {
55 if (!singular_[cmpt]) return false;
56 }
57
58 return true;
59}
60
61
62template<class Type>
64(
65 const Type& Tolerance,
66 const Type& RelTolerance,
67 const int logLevel
68)
69{
70 if ((logLevel >= 2) || (debug >= 2))
71 {
72 Info<< solverName_
73 << ": Iteration " << nIterations_
74 << " residual = " << finalResidual_
75 << endl;
76 }
77
78 converged_ =
79 (
80 finalResidual_ < Tolerance
81 || (
82 RelTolerance > small_*pTraits<Type>::one
83 && finalResidual_ < cmptMultiply(RelTolerance, initialResidual_)
84 )
85 );
86
87 return converged_;
88}
89
90
91template<class Type>
93(
94 Ostream& os
95) const
96{
97 for(direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
98 {
99 os << solverName_ << ": Solving for ";
101 {
102 os << fieldName_;
103 }
104 else
105 {
106 os << word(fieldName_ + pTraits<Type>::componentNames[cmpt]);
107 }
108
109 if (singular_[cmpt])
110 {
111 os << ": solution singularity" << endl;
112 }
113 else
114 {
115 os << ", Initial residual = " << component(initialResidual_, cmpt)
116 << ", Final residual = " << component(finalResidual_, cmpt)
117 << ", No Iterations " << nIterations_
118 << endl;
119 }
120 }
121}
122
123
124template<class Type>
126(
127 const Foam::label cmpt,
129)
130{
131 initialResidual_.replace(cmpt, sp.initialResidual());
132 finalResidual_.replace(cmpt, sp.finalResidual());
133 nIterations_.replace(cmpt, sp.nIterations());
134 singular_[cmpt] = sp.singular();
135}
136
137
138template<class Type>
141{
143 (
144 solverName_,
145 fieldName_,
146 cmptMax(initialResidual_),
147 cmptMax(finalResidual_),
148 cmptMax(nIterations_),
149 converged_,
150 singular()
151 );
152}
153
154
155template<class Type>
157(
159) const
160{
161 return
162 (
163 solverName() != sp.solverName()
164 || fieldName() != sp.fieldName()
165 || initialResidual() != sp.initialResidual()
166 || finalResidual() != sp.finalResidual()
167 || nIterations() != sp.nIterations()
168 || converged() != sp.converged()
169 || singular() != sp.singular()
170 );
171}
172
173
174template<class Type>
176(
177 const typename Foam::SolverPerformance<Type>& sp1,
178 const typename Foam::SolverPerformance<Type>& sp2
179)
180{
182 (
183 sp1.solverName(),
184 sp1.fieldName_,
185 max(sp1.initialResidual(), sp2.initialResidual()),
186 max(sp1.finalResidual(), sp2.finalResidual()),
187 max(sp1.nIterations(), sp2.nIterations()),
188 sp1.converged() && sp2.converged(),
189 sp1.singular() || sp2.singular()
190 );
191}
192
193
194template<class Type>
195Foam::Istream& Foam::operator>>
196(
197 Istream& is,
199)
200{
201 is.readBegin("SolverPerformance");
202 is >> sp.solverName_
203 >> sp.fieldName_
204 >> sp.initialResidual_
205 >> sp.finalResidual_
206 >> sp.nIterations_
207 >> sp.converged_
208 >> sp.singular_;
209 is.readEnd("SolverPerformance");
210
211 return is;
212}
213
214
215template<class Type>
216Foam::Ostream& Foam::operator<<
217(
218 Ostream& os,
219 const typename Foam::SolverPerformance<Type>& sp
220)
221{
222 os << token::BEGIN_LIST
223 << sp.solverName_ << token::SPACE
224 << sp.fieldName_ << token::SPACE
225 << sp.initialResidual_ << token::SPACE
226 << sp.finalResidual_ << token::SPACE
227 << sp.nIterations_ << token::SPACE
228 << sp.converged_ << token::SPACE
229 << sp.singular_ << token::SPACE
230 << token::END_LIST;
231
232 return os;
233}
234
235
236// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Y[inertIndex] max(0.0)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:111
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
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.
void replace(const label cmpt, const SolverPerformance< typename pTraits< Type >::cmptType > &sp)
Replace component based on the minimal SolverPerformance.
bool singular() const
Is the matrix singular?
bool converged() const noexcept
Has the solver converged?
bool checkSingularity(const Type &residual)
Singularity test.
const word & solverName() const noexcept
Return solver name.
const Type & initialResidual() const noexcept
Return initial residual.
SolverPerformance< typename pTraits< Type >::cmptType > max()
Return the summary maximum of SolverPerformance<Type>
bool checkConvergence(const Type &tolerance, const Type &relTolerance, const int logLevel=0)
Check, store and return convergence.
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:66
scalar print()
Print to screen.
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
uint8_t direction
Definition: direction.H:56
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)