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