SolverPerformance.H
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) 2012-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 Class
28  Foam::SolverPerformance
29 
30 Description
31  SolverPerformance is the class returned by the LduMatrix solver
32  containing performance statistics.
33 
34 SourceFiles
35  SolverPerformance.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef SolverPerformance_H
40 #define SolverPerformance_H
41 
42 #include "word.H"
43 #include "FixedList.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward Declarations
51 
52 template<class Type>
53 class SolverPerformance;
54 
55 template<class Type>
57 (
60 );
61 
62 template<class Type>
63 Istream& operator>>
64 (
65  Istream&,
67 );
68 
69 template<class Type>
70 Ostream& operator<<
71 (
72  Ostream&,
74 );
75 
76 
77 /*---------------------------------------------------------------------------*\
78  Class SolverPerformance Declaration
79 \*---------------------------------------------------------------------------*/
80 
81 template<class Type>
83 {
84  // Label type corresponding to Type
85  typedef typename pTraits<Type>::labelType labelType;
86 
87  // Private Data
88 
89  word solverName_;
90  word fieldName_;
91  Type initialResidual_;
92  Type finalResidual_;
93  labelType nIterations_;
94  bool converged_;
96 
97 
98 public:
99 
100  // Static Data
101 
102  // Declare name of the class and its debug switch
103  ClassName("SolverPerformance");
104 
105  //- Large Type for the use in solvers
106  static const scalar great_;
107 
108  //- Small Type for the use in solvers
109  static const scalar small_;
110 
111  //- Very small Type for the use in solvers
112  static const scalar vsmall_;
113 
114 
115  // Constructors
116 
118  :
119  initialResidual_(Zero),
120  finalResidual_(Zero),
121  nIterations_(Zero),
122  converged_(false),
123  singular_(false)
124  {}
125 
126 
128  (
129  const word& solverName,
130  const word& fieldName,
131  const Type& iRes = pTraits<Type>::zero,
132  const Type& fRes = pTraits<Type>::zero,
133  const labelType& nIter = pTraits<labelType>::zero,
134  const bool converged = false,
135  const bool singular = false
136  )
137  :
138  solverName_(solverName),
139  fieldName_(fieldName),
140  initialResidual_(iRes),
141  finalResidual_(fRes),
142  nIterations_(nIter),
143  converged_(converged),
144  singular_(singular)
145  {}
146 
147 
148  // Member Functions
149 
150  //- Return solver name
151  const word& solverName() const noexcept
152  {
153  return solverName_;
154  }
155 
156  //- Return solver name
157  word& solverName() noexcept
158  {
159  return solverName_;
160  }
161 
162 
163  //- Return field name
164  const word& fieldName() const noexcept
165  {
166  return fieldName_;
167  }
168 
169 
170  //- Return initial residual
171  const Type& initialResidual() const noexcept
172  {
173  return initialResidual_;
174  }
175 
176  //- Return initial residual
177  Type& initialResidual() noexcept
178  {
179  return initialResidual_;
180  }
181 
182 
183  //- Return final residual
184  const Type& finalResidual() const noexcept
185  {
186  return finalResidual_;
187  }
188 
189  //- Return final residual
190  Type& finalResidual() noexcept
191  {
192  return finalResidual_;
193  }
194 
195 
196  //- Return number of iterations
197  const labelType& nIterations() const noexcept
198  {
199  return nIterations_;
200  }
201 
202  //- Return number of iterations
203  labelType& nIterations() noexcept
204  {
205  return nIterations_;
206  }
207 
208 
209  //- Has the solver converged?
210  bool converged() const noexcept
211  {
212  return converged_;
213  }
214 
215  //- Is the matrix singular?
216  bool singular() const;
217 
218  //- Check, store and return convergence
219  bool checkConvergence
220  (
221  const Type& tolerance,
222  const Type& relTolerance,
223  const int logLevel = 0
224  );
225 
226  //- Singularity test
227  bool checkSingularity(const Type& residual);
228 
229  //- Print summary of solver performance to the given stream
230  void print(Ostream& os) const;
231 
232  //- Replace component based on the minimal SolverPerformance
233  void replace
234  (
235  const label cmpt,
236  const SolverPerformance<typename pTraits<Type>::cmptType>& sp
237  );
238 
239  //- Return the summary maximum of SolverPerformance<Type>
240  // Effectively it will mostly return solverPerformanceScalar
242 
243 
244  // Member Operators
245 
246  bool operator!=(const SolverPerformance<Type>&) const;
247 
248 
249  // Friend functions
250 
251  //- Return the element-wise maximum of two SolverPerformance<Type>s
252  friend SolverPerformance<Type> Foam::max <Type>
253  (
256  );
257 
258 
259  // Ostream Operator
260 
261  friend Istream& operator>> <Type>
262  (
263  Istream&,
265  );
266 
267  friend Ostream& operator<< <Type>
268  (
269  Ostream&,
271  );
272 };
273 
274 
275 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
276 
277 } // End namespace Foam
278 
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280 
281 #define makeSolverPerformance(Type) \
282  \
283 typedef Foam::SolverPerformance<Type> \
284  solverPerformance##Type; \
285  \
286 defineNamedTemplateTypeNameAndDebug(solverPerformance##Type, 0); \
287  \
288 template<> \
289 const scalar solverPerformance##Type::great_(1e20); \
290  \
291 template<> \
292 const scalar solverPerformance##Type::small_(1e-20); \
293  \
294 template<> \
295 const scalar solverPerformance##Type::vsmall_(VSMALL); \
296 
297 
298 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
299 
300 #ifdef NoRepository
301  #include "SolverPerformance.C"
302 #endif
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 #endif
307 
308 // ************************************************************************* //
Foam::SolverPerformance::SolverPerformance
SolverPerformance()
Definition: SolverPerformance.H:116
Foam::SolverPerformance::max
SolverPerformance< typename pTraits< Type >::cmptType > max()
Return the summary maximum of SolverPerformance<Type>
Definition: SolverPerformance.C:140
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::SolverPerformance::nIterations
const labelType & nIterations() const noexcept
Return number of iterations.
Definition: SolverPerformance.H:196
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::SolverPerformance::great_
static const scalar great_
Large Type for the use in solvers.
Definition: SolverPerformance.H:105
Foam::SolverPerformance::finalResidual
const Type & finalResidual() const noexcept
Return final residual.
Definition: SolverPerformance.H:183
Foam::SolverPerformance::solverName
word & solverName() noexcept
Return solver name.
Definition: SolverPerformance.H:156
Foam::SolverPerformance::vsmall_
static const scalar vsmall_
Very small Type for the use in solvers.
Definition: SolverPerformance.H:111
Foam::SolverPerformance::nIterations
labelType & nIterations() noexcept
Return number of iterations.
Definition: SolverPerformance.H:202
Foam::SolverPerformance::fieldName
const word & fieldName() const noexcept
Return field name.
Definition: SolverPerformance.H:163
Foam::SolverPerformance::print
void print(Ostream &os) const
Print summary of solver performance to the given stream.
Definition: SolverPerformance.C:93
Foam::SolverPerformance::operator!=
bool operator!=(const SolverPerformance< Type > &) const
Definition: SolverPerformance.C:157
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::SolverPerformance::finalResidual
Type & finalResidual() noexcept
Return final residual.
Definition: SolverPerformance.H:189
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
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
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::SolverPerformance::small_
static const scalar small_
Small Type for the use in solvers.
Definition: SolverPerformance.H:108
Foam::SolverPerformance::initialResidual
Type & initialResidual() noexcept
Return initial residual.
Definition: SolverPerformance.H:176
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::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::SolverPerformance::ClassName
ClassName("SolverPerformance")
SolverPerformance.C
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
word.H
FixedList.H
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