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 -------------------------------------------------------------------------------
11 License
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 
34 template<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 
50 template<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 
62 template<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 
91 template<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 
124 template<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 
138 template<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 
155 template<class Type>
157 (
158  const SolverPerformance<Type>& sp
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 
174 template<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 
194 template<class Type>
195 Foam::Istream& Foam::operator>>
196 (
197  Istream& is,
198  typename Foam::SolverPerformance<Type>& sp
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 
215 template<class Type>
216 Foam::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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::SolverPerformance::max
SolverPerformance< typename pTraits< Type >::cmptType > max()
Return the summary maximum of SolverPerformance<Type>
Definition: SolverPerformance.C:140
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::cmptMultiply
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::SolverPerformance::nIterations
const labelType & nIterations() const noexcept
Return number of iterations.
Definition: SolverPerformance.H:196
Foam::SolverPerformance::finalResidual
const Type & finalResidual() const noexcept
Return final residual.
Definition: SolverPerformance.H:183
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:111
Foam::SolverPerformance::print
void print(Ostream &os) const
Print summary of solver performance to the given stream.
Definition: SolverPerformance.C:93
Foam::cmptMax
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:253
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::SolverPerformance::checkConvergence
bool checkConvergence(const Type &tolerance, const Type &relTolerance, const int logLevel=0)
Check, store and return convergence.
Definition: SolverPerformance.C:64
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::SolverPerformance::replace
void replace(const label cmpt, const SolverPerformance< typename pTraits< Type >::cmptType > &sp)
Replace component based on the minimal SolverPerformance.
Definition: SolverPerformance.C:126
SolverPerformance.H
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::SolverPerformance::converged
bool converged() const noexcept
Has the solver converged?
Definition: SolverPerformance.H:209
os
OBJstream os(runTime.globalPath()/outputName)
Foam::SolverPerformance::singular
bool singular() const
Is the matrix singular?
Definition: SolverPerformance.C:51
Foam::SolverPerformance::initialResidual
const Type & initialResidual() const noexcept
Return initial residual.
Definition: SolverPerformance.H:170
Foam::SolverPerformance::checkSingularity
bool checkSingularity(const Type &residual)
Singularity test.
Definition: SolverPerformance.C:36
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::SolverPerformance::solverName
const word & solverName() const noexcept
Return solver name.
Definition: SolverPerformance.H:150
Foam::SolverPerformance
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics.
Definition: SolverPerformance.H:52