scalarAndErrorI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2023 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 "scalarAndError.H"
27 #include "Istream.H"
28 #include "Ostream.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
35 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
36 
37 //- Arithmetic operations between two scalar-and-errors
38 #define SCALAR_AND_ERROR_BINARY_OPERATION(Op) \
39  \
40  inline scalarAndError operator Op \
41  ( \
42  const scalarAndError& a, \
43  const scalarAndError& b \
44  ) \
45  { \
46  scalarAndError c(a); \
47  c Op##= b; \
48  return c; \
49  }
54 
55 
56 //- Equality compare two scalar-and-error-s
57 inline bool operator==(const scalarAndError& a, const scalarAndError& b)
58 {
59  return a.value == b.value && a.error == b.error;
60 }
61 
62 
63 //- Inequality compare two scalar-and-error-s
64 inline bool operator!=(const scalarAndError& a, const scalarAndError& b)
65 {
66  return !(a == b);
67 }
68 
69 
70 //- Comparisons between two scalar-and-errors
71 #define SCALAR_AND_ERROR_BINARY_COMPARE(Op) \
72  \
73  inline bool operator Op \
74  ( \
75  const scalarAndError& a, \
76  const scalarAndError& b \
77  ) \
78  { \
79  return a.value Op b.value; \
80  }
85 
86 
87 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
88 
89 //- Return the magnitude square of a scalar-and-error
91 {
92  return sae*sae;
93 }
94 
95 
96 //- Return the square root of a scalar-and-error
98 {
99  const scalar sqrtV = ::sqrt(sae.value);
100  return scalarAndError(sqrtV, 0.5*sqrtV*sae.error);
101 }
102 
103 
104 // * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * * //
105 
106 //- Write a scalar-and-error
107 inline Ostream& operator<<(Ostream& os, const scalarAndError& sae)
108 {
109  return os << sae.value << "(+-" << sae.error << ')';
110 }
111 
112 
113 //- Read a scalar-and-error
115 {
116  auto readPunctuationToken = [&](const token::punctuationToken p)
117  {
118  token t(is);
119 
120  if (t != p)
121  {
122  is.setBad();
123 
125  << "Expected a '" << p
126  << "' while reading scalarAndError"
127  << ", found " << t.info()
128  << exit(FatalIOError);
129  }
130  };
131 
132  is >> sae.value;
133  readPunctuationToken(token::BEGIN_LIST);
134  readPunctuationToken(token::ADD);
135  readPunctuationToken(token::SUBTRACT);
136  is >> sae.error;
137  readPunctuationToken(token::END_LIST);
138 
139  return is;
140 }
141 
142 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
143 
144 } // End namespace Foam
145 
146 // ************************************************************************* //
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Class to encapsulate a scalar value and an associated round-off error. The error is tracked through o...
scalar error
The error.
scalar value
The value.
A token holds items read from Istream.
Definition: token.H:73
punctuationToken
Standard punctuation tokens.
Definition: token.H:102
@ BEGIN_LIST
Definition: token.H:109
@ SUBTRACT
Definition: token.H:124
@ END_LIST
Definition: token.H:110
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:422
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
volScalarField & b
Definition: createFields.H:25
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool operator!=(const particle &, const particle &)
Definition: particle.C:1210
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
dimensionedScalar sqrt(const dimensionedScalar &ds)
Istream & operator>>(Istream &, pistonPointEdgeData &)
IOerror FatalIOError
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
dimensioned< scalar > magSqr(const dimensioned< Type > &)
#define SCALAR_AND_ERROR_BINARY_OPERATION(Op)
Arithmetic operations between two scalar-and-errors.
#define SCALAR_AND_ERROR_BINARY_COMPARE(Op)
Comparisons between two scalar-and-errors.
volScalarField & p