dimensionSet.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2019 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::dimensionSet
29 
30 Description
31  Dimension set for the base types.
32 
33  This type may be used to implement rigorous dimension checking
34  for algebraic manipulation.
35 
36 SourceFiles
37  dimensionSet.C
38  dimensionSetIO.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef dimensionSet_H
43 #define dimensionSet_H
44 
45 #include "bool.H"
46 #include "dimensionedScalarFwd.H"
47 #include "className.H"
48 #include "scalarField.H"
49 #include "PtrList.H"
50 #include "HashTable.H"
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 
57 // Forward declarations
58 
59 class dimensionSet;
60 class dimensionSets;
61 
62 /*---------------------------------------------------------------------------*\
63  Class dimensionSet Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 class dimensionSet
67 {
68 public:
69 
70  //- The array of dimension exponents
72 
73 
74  // Member Constants
75 
76  //- There are 7 base dimensions
77  static constexpr int nDimensions = 7;
78 
79  //- Enumeration for the dimension exponents
80  enum dimensionType
81  {
82  MASS,
84  TIME,
89  };
90 
91 
92  // Static Data Members
93 
94  //- Tolerance for 'small' exponents, for near-zero rounding
95  static const scalar smallExponent;
96 
97 
98 private:
99 
100  // Private data
101 
102  //- The array of dimension exponents
103  list_type exponents_;
104 
105 
106  // Private classes
107 
108  class tokeniser
109  {
110  // Private data
111 
112  Istream& is_;
113 
114  List<token> tokens_;
115 
116  label start_;
117 
118  label size_;
119 
120 
121  // Private Member Functions
122 
123  void push(const token& t);
124 
125  token pop();
126 
127  void unpop(const token& t);
128 
129  public:
130 
131  // Constructors
132 
133  tokeniser(Istream& is);
134 
135 
136  // Member Functions
137 
138  Istream& stream()
139  {
140  return is_;
141  }
142 
143  bool hasToken() const;
144 
145  token nextToken();
146 
147  void putBack(const token&);
148 
149  void splitWord(const word&);
150 
151  static bool valid(char c);
152 
153  static label priority(const token& t);
154  };
155 
156 
157  //- Reset exponents to nearest integer if close to it. Used to
158  // handle reading with insufficient precision.
159  void round(const scalar tol);
160 
161  dimensionedScalar parse
162  (
163  const label lastPrior,
164  tokeniser& tis,
166  ) const;
167 
168 public:
169 
170  // Declare name of the class and its debug switch
171  ClassName("dimensionSet");
172 
173 
174  // Constructors
175 
176  //- Default construct (dimensionless).
177  dimensionSet();
178 
179  //- Construct from exponents for the first five or all seven dimensions
181  (
182  const scalar mass,
183  const scalar length,
184  const scalar time,
185  const scalar temperature,
186  const scalar moles,
187  const scalar current = 0,
188  const scalar luminousIntensity = 0
189  );
190 
191  //- Construct from exponents for all seven dimensions
192  dimensionSet(const FixedList<scalar,7>& dimensions);
193 
194  //- Copy construct
195  dimensionSet(const dimensionSet& ds);
196 
197  //- Construct from dictionary entry - usually "dimensions".
198  dimensionSet(const dictionary& dict, const word& entryName);
199 
200  //- Construct and return a clone
202  {
203  return autoPtr<dimensionSet>::New(*this);
204  }
205 
206  //- Construct from Istream
207  explicit dimensionSet(Istream& is);
208 
209 
210  // Member Functions
211 
212  //- Return true if it is dimensionless
213  bool dimensionless() const;
214 
215  //- Return const access to the exponents as a list
216  const FixedList<scalar,7>& values() const;
217 
218  //- Return non-const access to the exponents as a list
220 
221  //- Reset exponents to be dimensionless
222  void clear();
223 
224  //- Copy assign the exponents from the dimensionSet
225  void reset(const dimensionSet& ds);
226 
227 
228  // IO
229 
230  //- Read using provided units. Used only in initial parsing
231  Istream& read
232  (
233  Istream& is,
234  scalar& multiplier,
235  const dictionary&
236  );
237 
238  //- Read using provided units
239  Istream& read
240  (
241  Istream& is,
242  scalar& multiplier,
244  );
245 
246  //- Read using system units
247  Istream& read
248  (
249  Istream& is,
250  scalar& multiplier
251  );
252 
253  //- Write using provided units
254  Ostream& write
255  (
256  Ostream& os,
257  scalar& multiplier,
258  const dimensionSets&
259  ) const;
260 
261  //- Write using system units
262  Ostream& write
263  (
264  Ostream& os,
265  scalar& multiplier
266  ) const;
267 
268 
269  // Member Operators
270 
271  scalar operator[](const dimensionType) const;
272  scalar& operator[](const dimensionType);
273 
274  scalar operator[](const label) const;
275  scalar& operator[](const label);
276 
277  bool operator==(const dimensionSet&) const;
278  bool operator!=(const dimensionSet&) const;
279 
280  bool operator=(const dimensionSet&) const;
281 
282  bool operator+=(const dimensionSet&) const;
283  bool operator-=(const dimensionSet&) const;
284  bool operator*=(const dimensionSet&);
285  bool operator/=(const dimensionSet&);
286 };
287 
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 
290 // IOstream Operators
291 
292 Istream& operator>>(Istream& is, dimensionSet& ds);
293 Ostream& operator<<(Ostream& os, const dimensionSet& ds);
294 
295 
296 // Global Functions
297 
298 dimensionSet min(const dimensionSet& ds1, const dimensionSet& ds2);
299 dimensionSet max(const dimensionSet& ds1, const dimensionSet& ds2);
300 dimensionSet clip(const dimensionSet& ds1, const dimensionSet& ds2);
301 
302 dimensionSet cmptMultiply(const dimensionSet& ds1, const dimensionSet& ds2);
303 dimensionSet cmptDivide(const dimensionSet& ds1, const dimensionSet& ds2);
304 
305 
306 dimensionSet pow(const dimensionSet& ds, const scalar p);
307 dimensionSet pow(const dimensionSet& ds, const dimensionedScalar& dS);
308 dimensionSet pow(const dimensionedScalar& dS, const dimensionSet& ds);
309 
310 dimensionSet sqr(const dimensionSet& ds);
311 dimensionSet pow2(const dimensionSet& ds);
312 dimensionSet pow3(const dimensionSet& ds);
313 dimensionSet pow4(const dimensionSet& ds);
314 dimensionSet pow5(const dimensionSet& ds);
315 dimensionSet pow6(const dimensionSet& ds);
316 dimensionSet pow025(const dimensionSet& ds);
317 
318 dimensionSet sqrt(const dimensionSet& ds);
319 dimensionSet cbrt(const dimensionSet& ds);
320 
321 dimensionSet magSqr(const dimensionSet& ds);
322 dimensionSet mag(const dimensionSet& ds);
323 dimensionSet sign(const dimensionSet&);
324 dimensionSet pos(const dimensionSet&);
325 dimensionSet pos0(const dimensionSet&);
326 dimensionSet neg(const dimensionSet&);
327 dimensionSet neg0(const dimensionSet&);
328 dimensionSet posPart(const dimensionSet&);
329 dimensionSet negPart(const dimensionSet&);
330 
331 //- The dimensionSet inverted
332 dimensionSet inv(const dimensionSet& ds);
333 
334 //- Check the argument is dimensionless (for transcendental functions)
335 dimensionSet trans(const dimensionSet& ds);
336 
337 //- Arguments need the same dimensions. Return dimensionless.
338 dimensionSet atan2(const dimensionSet& ds1, const dimensionSet& ds2);
339 
340 //- Arguments need the same dimensions. Does not change the dimension.
341 dimensionSet hypot(const dimensionSet& ds1, const dimensionSet& ds2);
342 
343 //- Return the argument; transformations do not change the dimensions
344 dimensionSet transform(const dimensionSet& ds);
345 
346 //- Return the argument; transformations do not change the dimensions
347 dimensionSet invTransform(const dimensionSet& ds);
348 
349 
350 // Global Operators
351 
352 //- The dimensionSet inverted
353 dimensionSet operator~(const dimensionSet& ds);
354 
355 //- Unary negation.
356 dimensionSet operator-(const dimensionSet& ds);
357 
358 dimensionSet operator+(const dimensionSet& ds1, const dimensionSet& ds2);
359 dimensionSet operator-(const dimensionSet& ds1, const dimensionSet& ds2);
360 dimensionSet operator*(const dimensionSet& ds1, const dimensionSet& ds2);
361 dimensionSet operator/(const dimensionSet& ds1, const dimensionSet& ds2);
362 dimensionSet operator&(const dimensionSet& ds1, const dimensionSet& ds2);
363 dimensionSet operator^(const dimensionSet& ds1, const dimensionSet& ds2);
364 dimensionSet operator&&(const dimensionSet& ds1, const dimensionSet& ds2);
365 
366 
367 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
368 
369 } // End namespace Foam
370 
371 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
372 
373 #include "dimensionSets.H"
374 
375 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
376 
377 #endif
378 
379 // ************************************************************************* //
Foam::dimensionSet::nDimensions
static constexpr int nDimensions
There are 7 base dimensions.
Definition: dimensionSet.H:76
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
Foam::trans
dimensionSet trans(const dimensionSet &ds)
Check the argument is dimensionless (for transcendental functions)
Definition: dimensionSet.C:484
Foam::dimensionSet::ClassName
ClassName("dimensionSet")
Foam::dimensionSet::clone
autoPtr< dimensionSet > clone() const
Construct and return a clone.
Definition: dimensionSet.H:200
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::dimensionSet::operator+=
bool operator+=(const dimensionSet &) const
Definition: dimensionSet.C:214
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
HashTable.H
Foam::cmptMultiply
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::dimensionSet::operator/=
bool operator/=(const dimensionSet &)
Definition: dimensionSet.C:244
Foam::operator&
tmp< GeometricField< Type, fvPatchField, volMesh > > operator&(const fvMatrix< Type > &, const DimensionedField< Type, volMesh > &)
Foam::pow2
dimensionSet pow2(const dimensionSet &ds)
Definition: dimensionSet.C:367
Foam::dimensionSet::LUMINOUS_INTENSITY
Candela Cd.
Definition: dimensionSet.H:87
scalarField.H
Foam::dimensionSet::smallExponent
static const scalar smallExponent
Tolerance for 'small' exponents, for near-zero rounding.
Definition: dimensionSet.H:94
Foam::posPart
dimensionedScalar posPart(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:221
Foam::atan2
dimensionedScalar atan2(const dimensionedScalar &x, const dimensionedScalar &y)
Definition: dimensionedScalar.C:312
Foam::operator-
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Foam::neg0
dimensionedScalar neg0(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:210
Foam::dimensionSet::operator*=
bool operator*=(const dimensionSet &)
Definition: dimensionSet.C:236
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::pos0
dimensionedScalar pos0(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:188
Foam::dimensionSet
Dimension set for the base types.
Definition: dimensionSet.H:65
Foam::sign
dimensionedScalar sign(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:166
Foam::transform
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:519
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::dimensionSet::dimensionSet
dimensionSet()
Default construct (dimensionless).
Definition: dimensionSet.C:71
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Foam::pow025
dimensionedScalar pow025(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:133
dimensionSets.H
Useful dimension sets.
Foam::hypot
dimensionedScalar hypot(const dimensionedScalar &x, const dimensionedScalar &y)
Definition: dimensionedScalar.C:327
dimensionedScalarFwd.H
Foam::dimensionSet::dimensionType
dimensionType
Enumeration for the dimension exponents.
Definition: dimensionSet.H:79
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::pow4
dimensionedScalar pow4(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:100
Foam::dimensionSet::dimensionless
bool dimensionless() const
Return true if it is dimensionless.
Definition: dimensionSet.C:114
Foam::pow6
dimensionedScalar pow6(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:122
Foam::dimensionSet::read
Istream & read(Istream &is, scalar &multiplier, const dictionary &)
Read using provided units. Used only in initial parsing.
Definition: dimensionSetIO.C:500
Foam::dimensionSet::TEMPERATURE
Kelvin K.
Definition: dimensionSet.H:84
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::pow3
dimensionedScalar pow3(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:89
Foam::inv
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
Definition: dimensionedSphericalTensor.C:73
className.H
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Foam::dimensionedScalar
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
Definition: dimensionedScalarFwd.H:43
bool.H
System bool.
Foam::dimensionSet::operator==
bool operator==(const dimensionSet &) const
Definition: dimensionSet.C:179
Foam::dimensionSet::list_type
FixedList< scalar, 7 > list_type
The array of dimension exponents.
Definition: dimensionSet.H:70
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:75
Foam::dimensionSet::clear
void clear()
Reset exponents to be dimensionless.
Definition: dimensionSet.C:141
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::dimensionSet::operator!=
bool operator!=(const dimensionSet &) const
Definition: dimensionSet.C:197
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dimensionSet::operator-=
bool operator-=(const dimensionSet &) const
Definition: dimensionSet.C:225
Foam::operator&&
dimensioned< typename scalarProduct< Type1, Type2 >::type > operator&&(const dimensioned< Type1 > &, const dimensioned< Type2 > &)
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::pow5
dimensionedScalar pow5(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:111
Foam::dimensioned
Generic dimensioned Type class.
Definition: dimensionedScalarFwd.H:43
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::dimensionSet::MASS
kilogram kg
Definition: dimensionSet.H:81
Foam::operator/
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
Definition: dimensionedScalar.C:68
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::cmptDivide
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::dimensionSet::write
Ostream & write(Ostream &os, scalar &multiplier, const dimensionSets &) const
Write using provided units.
Definition: dimensionSetIO.C:624
Foam::operator~
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition: bitSetI.H:746
Foam::dimensionSet::values
const FixedList< scalar, 7 > & values() const
Return const access to the exponents as a list.
Definition: dimensionSet.C:129
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::negPart
dimensionedScalar negPart(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:232
Foam::clip
dimensionSet clip(const dimensionSet &ds1, const dimensionSet &ds2)
Definition: dimensionSet.C:276
Foam::dimensionSet::CURRENT
Ampere A.
Definition: dimensionSet.H:86
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
Foam::operator+
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::FixedList< scalar, 7 >
Foam::dimensionSet::operator[]
scalar operator[](const dimensionType) const
Definition: dimensionSet.C:155
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
Foam::invTransform
dimensionSet invTransform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:525
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
PtrList.H
Foam::dimensionSet::TIME
second s
Definition: dimensionSet.H:83
Foam::dimensionSet::operator=
bool operator=(const dimensionSet &) const
Definition: dimensionSet.C:203
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::operator^
bitSet operator^(const bitSet &a, const bitSet &b)
Bitwise-XOR of two bitsets to form a unique bit-set.
Definition: bitSetI.H:768
Foam::dimensionSet::LENGTH
metre m
Definition: dimensionSet.H:82
Foam::cbrt
dimensionedScalar cbrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:155
Foam::dimensionSet::MOLES
mole mol
Definition: dimensionSet.H:85
Foam::neg
dimensionedScalar neg(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:199
Foam::dimensionSets
Construction of unit sets.
Definition: dimensionSets.H:83
Foam::dimensionSet::reset
void reset(const dimensionSet &ds)
Copy assign the exponents from the dimensionSet.
Definition: dimensionSet.C:147
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177