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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "SolverPerformance.H"
29 #include "IOstreams.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  const Type& wApA
37 )
38 {
39  for(direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
40  {
41  singular_[cmpt] =
42  component(wApA, cmpt) < vsmall_;
43  }
44 
45  return singular();
46 }
47 
48 
49 template<class Type>
51 {
52  for(direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
53  {
54  if (!singular_[cmpt]) return false;
55  }
56 
57  return true;
58 }
59 
60 
61 template<class Type>
63 (
64  const Type& Tolerance,
65  const Type& RelTolerance
66 )
67 {
68  if (debug >= 2)
69  {
70  Info<< solverName_
71  << ": Iteration " << nIterations_
72  << " residual = " << finalResidual_
73  << endl;
74  }
75 
76  if
77  (
78  finalResidual_ < Tolerance
79  || (
80  RelTolerance
81  > small_*pTraits<Type>::one
82  && finalResidual_ < cmptMultiply(RelTolerance, initialResidual_)
83  )
84  )
85  {
86  converged_ = true;
87  }
88  else
89  {
90  converged_ = false;
91  }
92 
93  return converged_;
94 }
95 
96 
97 template<class Type>
99 (
100  Ostream& os
101 ) const
102 {
103  for(direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
104  {
106  {
107  os << solverName_ << ": Solving for " << fieldName_;
108 
109  }
110  else
111  {
112  os << solverName_ << ": Solving for "
113  << word(fieldName_ + pTraits<Type>::componentNames[cmpt]);
114  }
115 
116  if (singular_[cmpt])
117  {
118  os << ": solution singularity" << endl;
119  }
120  else
121  {
122  os << ", Initial residual = " << component(initialResidual_, cmpt)
123  << ", Final residual = " << component(finalResidual_, cmpt)
124  << ", No Iterations " << nIterations_
125  << endl;
126  }
127  }
128 }
129 
130 
131 template<class Type>
133 (
134  const Foam::label cmpt,
136 )
137 {
138  initialResidual_.replace(cmpt, sp.initialResidual());
139  finalResidual_.replace(cmpt, sp.finalResidual());
140  nIterations_.replace(cmpt, sp.nIterations());
141  singular_[cmpt] = sp.singular();
142 }
143 
144 
145 template<class Type>
148 {
150  (
151  solverName_,
152  fieldName_,
153  cmptMax(initialResidual_),
154  cmptMax(finalResidual_),
155  cmptMax(nIterations_),
156  converged_,
157  singular()
158  );
159 }
160 
161 
162 template<class Type>
164 (
165  const SolverPerformance<Type>& sp
166 ) const
167 {
168  return
169  (
170  solverName() != sp.solverName()
171  || fieldName() != sp.fieldName()
172  || initialResidual() != sp.initialResidual()
173  || finalResidual() != sp.finalResidual()
174  || nIterations() != sp.nIterations()
175  || converged() != sp.converged()
176  || singular() != sp.singular()
177  );
178 }
179 
180 
181 template<class Type>
183 (
184  const typename Foam::SolverPerformance<Type>& sp1,
185  const typename Foam::SolverPerformance<Type>& sp2
186 )
187 {
189  (
190  sp1.solverName(),
191  sp1.fieldName_,
192  max(sp1.initialResidual(), sp2.initialResidual()),
193  max(sp1.finalResidual(), sp2.finalResidual()),
194  max(sp1.nIterations(), sp2.nIterations()),
195  sp1.converged() && sp2.converged(),
196  sp1.singular() || sp2.singular()
197  );
198 }
199 
200 
201 template<class Type>
202 Foam::Istream& Foam::operator>>
203 (
204  Istream& is,
205  typename Foam::SolverPerformance<Type>& sp
206 )
207 {
208  is.readBegin("SolverPerformance");
209  is >> sp.solverName_
210  >> sp.fieldName_
211  >> sp.initialResidual_
212  >> sp.finalResidual_
213  >> sp.nIterations_
214  >> sp.converged_
215  >> sp.singular_;
216  is.readEnd("SolverPerformance");
217 
218  return is;
219 }
220 
221 
222 template<class Type>
223 Foam::Ostream& Foam::operator<<
224 (
225  Ostream& os,
226  const typename Foam::SolverPerformance<Type>& sp
227 )
228 {
229  os << token::BEGIN_LIST
230  << sp.solverName_ << token::SPACE
231  << sp.fieldName_ << token::SPACE
232  << sp.initialResidual_ << token::SPACE
233  << sp.finalResidual_ << token::SPACE
234  << sp.nIterations_ << token::SPACE
235  << sp.converged_ << token::SPACE
236  << sp.singular_ << token::SPACE
237  << token::END_LIST;
238 
239  return os;
240 }
241 
242 
243 // ************************************************************************* //
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:147
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:62
Foam::cmptMultiply
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::SolverPerformance::finalResidual
const Type & finalResidual() const
Return final residual.
Definition: SolverPerformance.H:182
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::SolverPerformance::converged
bool converged() const
Has the solver converged?
Definition: SolverPerformance.H:208
Foam::SolverPerformance::solverName
const word & solverName() const
Return solver name.
Definition: SolverPerformance.H:149
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:109
Foam::SolverPerformance::print
void print(Ostream &os) const
Print summary of solver performance to the given stream.
Definition: SolverPerformance.C:99
Foam::cmptMax
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:253
Foam::SolverPerformance::checkConvergence
bool checkConvergence(const Type &tolerance, const Type &relTolerance)
Check, store and return convergence.
Definition: SolverPerformance.C:63
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::SolverPerformance::nIterations
const labelType & nIterations() const
Return number of iterations.
Definition: SolverPerformance.H:195
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:133
SolverPerformance.H
Foam::SolverPerformance::initialResidual
const Type & initialResidual() const
Return initial residual.
Definition: SolverPerformance.H:169
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::singular
bool singular() const
Is the matrix singular?
Definition: SolverPerformance.C:50
Foam::SolverPerformance::checkSingularity
bool checkSingularity(const Type &residual)
Singularity test.
Definition: SolverPerformance.C:35
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:54
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
SolverPerformance is the class returned by the LduMatrix solver containing performance statistics.
Definition: SolverPerformance.H:51