scalarAndError.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 Class
25  Foam::scalarAndError
26 
27 Description
28  Class to encapsulate a scalar value and an associated round-off error.
29  The error is tracked through operations performed between scalarAndError
30  variables.
31 
32 SourceFiles
33  scalarAndErrorI.H
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef scalarAndError_H
38 #define scalarAndError_H
39 
40 #include "scalar.H"
41 #include "zero.H"
42 #include "contiguous.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class scalarAndError Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 class scalarAndError
54 {
55 public:
56 
57  //- Component type
58  typedef scalarAndError cmptType;
59 
60 
61  // Member constants
62 
63  //- Dimensionality of space
64  static const direction dim = 3;
65 
66  //- Rank of scalarAndError is 0
67  static const direction rank = 0;
68 
69  //- Number of components in scalarAndError is 1
70  static const direction nComponents = 1;
71 
72 
73  // Public Data
74 
75  //- The value
76  scalar value;
77 
78  //- The error
79  scalar error;
80 
81 
82  // Constructors
83 
84  //- Default construct
85  inline scalarAndError()
86  {}
87 
88  //- Construct zero
89  inline scalarAndError(const zero&)
90  :
91  value(scalar(0)),
92  error(scalar(0))
93  {}
94 
95  //- Construct from a scalar
96  inline scalarAndError(const scalar v)
97  :
98  value(v),
99  error(small*mag(v))
100  {}
101 
102  //- Construct from a scalar and an error
103  inline scalarAndError(const scalar v, const scalar e)
104  :
105  value(v),
106  error(e)
107  {}
108 
109 
110  // Member Operators
111 
112  //- Negate
113  inline scalarAndError operator-() const
114  {
115  return scalarAndError(- value, error);
116  }
117 
118  //- Add another scalar-and-error to this one
119  inline void operator+=(const scalarAndError& sae)
120  {
121  value += sae.value;
122  error += mag(sae.error);
123  }
124 
125  //- Subtract another scalar-and-error from this one
126  inline void operator-=(const scalarAndError& sae)
127  {
128  this->operator+=(- sae);
129  }
130 
131  //- Multiply another scalar-and-error with this one
132  inline void operator*=(const scalarAndError& sae)
133  {
134  error = mag(value*error) + mag(sae.value*sae.error);
135  value *= sae.value;
136  }
137 
138  //- Divide this scalar-and-error by another
139  inline void operator/=(const scalarAndError& sae)
140  {
141  error = mag(error/sae.value) + mag(sae.error/sqr(sae.value));
142  value /= sae.value;
143  }
144 };
145 
146 
147 //- Data associated with the scalar-and-error type is contiguous
148 template<>
149 inline bool contiguous<scalarAndError>() { return true; }
150 
151 
152 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
153 
154 } // End namespace Foam
155 
156 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
157 
158 #include "scalarAndErrorI.H"
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 #endif
163 
164 // ************************************************************************* //
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:71
Class to encapsulate a scalar value and an associated round-off error. The error is tracked through o...
scalarAndError()
Default construct.
scalar error
The error.
void operator*=(const scalarAndError &sae)
Multiply another scalar-and-error with this one.
static const direction rank
Rank of scalarAndError is 0.
void operator+=(const scalarAndError &sae)
Add another scalar-and-error to this one.
static const direction nComponents
Number of components in scalarAndError is 1.
static const direction dim
Dimensionality of space.
void operator-=(const scalarAndError &sae)
Subtract another scalar-and-error from this one.
void operator/=(const scalarAndError &sae)
Divide this scalar-and-error by another.
scalarAndError cmptType
Component type.
scalar value
The value.
scalarAndError operator-() const
Negate.
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
Template function to specify if the data of a type are contiguous.
Namespace for OpenFOAM.
const doubleScalar e
Definition: doubleScalar.H:106
dimensionedSymmTensor sqr(const dimensionedVector &dv)
bool contiguous< scalarAndError >()
Data associated with the scalar-and-error type is contiguous.
dimensioned< scalar > mag(const dimensioned< Type > &)
uint8_t direction
Definition: direction.H:45