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 | 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 Class
25  Foam::dimensionSet
26 
27 Description
28  Dimension set for the base types.
29 
30  This type may be used to implement rigorous dimension checking
31  for algebraic manipulation.
32 
33 SourceFiles
34  dimensionSet.C
35  dimensionSetIO.C
36  dimensionSets.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef dimensionSet_H
41 #define dimensionSet_H
42 
43 #include "bool.H"
44 #include "dimensionedScalarFwd.H"
45 #include "className.H"
46 #include "scalarField.H"
47 #include "PtrList.H"
48 #include "HashTable.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward declaration of friend functions and operators
56 
57 class dimensionSet;
58 class dimensionSets;
59 
60 // Friend functions
61 
62 dimensionSet max(const dimensionSet&, const dimensionSet&);
63 dimensionSet min(const dimensionSet&, const dimensionSet&);
64 dimensionSet cmptMultiply(const dimensionSet&, const dimensionSet&);
65 dimensionSet cmptDivide(const dimensionSet&, const dimensionSet&);
66 
67 dimensionSet pow(const dimensionSet&, const scalar);
68 dimensionSet pow(const dimensionSet&, const dimensionedScalar&);
69 dimensionSet pow(const dimensionedScalar&, const dimensionSet&);
70 
71 dimensionSet sqr(const dimensionSet&);
72 dimensionSet pow3(const dimensionSet&);
73 dimensionSet pow4(const dimensionSet&);
74 dimensionSet pow5(const dimensionSet&);
75 dimensionSet pow6(const dimensionSet&);
76 dimensionSet pow025(const dimensionSet&);
77 
78 dimensionSet sqrt(const dimensionSet&);
79 dimensionSet cbrt(const dimensionSet&);
80 dimensionSet magSqr(const dimensionSet&);
81 dimensionSet mag(const dimensionSet&);
82 dimensionSet sign(const dimensionSet&);
83 dimensionSet pos(const dimensionSet&);
84 dimensionSet neg(const dimensionSet&);
85 dimensionSet posPart(const dimensionSet&);
86 dimensionSet negPart(const dimensionSet&);
87 dimensionSet inv(const dimensionSet&);
88 
89 // Function to check the argument is dimensionless
90 // for transcendental functions
91 dimensionSet trans(const dimensionSet&);
92 
93 // Return the argument; transformations do not change the dimensions
94 dimensionSet transform(const dimensionSet&);
95 
96 // Friend operators
97 
98 dimensionSet operator-(const dimensionSet&);
99 dimensionSet operator+(const dimensionSet&, const dimensionSet&);
100 dimensionSet operator-(const dimensionSet&, const dimensionSet&);
101 dimensionSet operator*(const dimensionSet&, const dimensionSet&);
102 dimensionSet operator/(const dimensionSet&, const dimensionSet&);
103 dimensionSet operator&(const dimensionSet&, const dimensionSet&);
104 dimensionSet operator^(const dimensionSet&, const dimensionSet&);
105 dimensionSet operator&&(const dimensionSet&, const dimensionSet&);
106 
107 // IOstream operators
108 
109 Istream& operator>>(Istream&, dimensionSet&);
110 Ostream& operator<<(Ostream&, const dimensionSet&);
111 
112 
113 /*---------------------------------------------------------------------------*\
114  Class dimensionSet Declaration
115 \*---------------------------------------------------------------------------*/
117 class dimensionSet
118 {
119 
120 public:
121 
122  // Member constants
123 
124  enum
125  {
126  nDimensions = 7 // Number of dimensions in SI is 7
127  };
128 
129  //- Define an enumeration for the names of the dimension exponents
130  enum dimensionType
131  {
132  MASS, // kilogram kg
133  LENGTH, // metre m
134  TIME, // second s
135  TEMPERATURE, // Kelvin K
136  MOLES, // mole mol
137  CURRENT, // Ampere A
138  LUMINOUS_INTENSITY // Candela Cd
139  };
140 
141 
142  // Static data members
144  static const scalar smallExponent;
145 
146 
147 private:
148 
149  // Private classes
150 
151  class tokeniser
152  {
153  // Private data
154 
155  Istream& is_;
156 
157  List<token> tokens_;
158 
159  label start_;
160 
161  label size_;
162 
163 
164  // Private Member Functions
165 
166  void push(const token&);
167 
168  token pop();
169 
170  void unpop(const token&);
171 
172  public:
173 
174  // Constructors
175 
176  tokeniser(Istream&);
177 
178 
179  // Member Functions
180 
181  Istream& stream()
182  {
183  return is_;
184  }
185 
186  bool hasToken() const;
187 
188  token nextToken();
189 
190  void putBack(const token&);
191 
192  void splitWord(const word&);
193 
194  static bool valid(char c);
195 
196  static label priority(const token& t);
197  };
198 
199 
200  //- Reset exponents to nearest integer if close to it. Used to
201  // handle reading with insufficient precision.
202  void round(const scalar tol);
203 
204  dimensionedScalar parse
205  (
206  const label lastPrior,
207  tokeniser& tis,
209  ) const;
210 
211 
212  // private data
213 
214  // dimensionSet stored as an array of dimension exponents
215  scalar exponents_[nDimensions];
216 
217 
218 public:
219 
220  // Declare name of the class and its debug switch
221  ClassName("dimensionSet");
222 
223 
224  // Constructors
225 
226  //- Construct given individual dimension exponents for all
227  // seven dimensions
229  (
230  const scalar mass,
231  const scalar length,
232  const scalar time,
233  const scalar temperature,
234  const scalar moles,
235  const scalar current,
236  const scalar luminousIntensity
237  );
238 
239  //- Construct given individual dimension exponents for first
240  // five dimensions
242  (
243  const scalar mass,
244  const scalar length,
245  const scalar time,
246  const scalar temperature,
247  const scalar moles
248  );
249 
250  //- Copy constructor
251  dimensionSet(const dimensionSet& ds);
252 
253  //- Construct and return a clone
255  {
256  return autoPtr<dimensionSet>(new dimensionSet(*this));
257  }
258 
259  //- Construct from Istream
261 
262 
263  // Member functions
264 
265  //- Return true if it is dimensionless
266  bool dimensionless() const;
267 
268  void reset(const dimensionSet&);
269 
270 
271  // I/O
272 
273  //- Read using provided units. Used only in initial parsing
274  Istream& read
275  (
276  Istream& is,
277  scalar& multiplier,
278  const dictionary&
279  );
280 
281  //- Read using provided units
282  Istream& read
283  (
284  Istream& is,
285  scalar& multiplier,
287  );
288 
289  //- Read using system units
290  Istream& read
291  (
292  Istream& is,
293  scalar& multiplier
294  );
295 
296  //- Write using provided units
297  Ostream& write
298  (
299  Ostream& os,
300  scalar& multiplier,
301  const dimensionSets&
302  ) const;
303 
304  //- Write using system units
305  Ostream& write
306  (
307  Ostream& os,
308  scalar& multiplier
309  ) const;
310 
311 
312  // Member operators
313 
314  scalar operator[](const dimensionType) const;
315  scalar& operator[](const dimensionType);
316 
317  scalar operator[](const label) const;
318  scalar& operator[](const label);
319 
320  bool operator==(const dimensionSet&) const;
321  bool operator!=(const dimensionSet&) const;
322 
323  bool operator=(const dimensionSet&) const;
324 
325  bool operator+=(const dimensionSet&) const;
326  bool operator-=(const dimensionSet&) const;
327  bool operator*=(const dimensionSet&);
328  bool operator/=(const dimensionSet&);
329 
330 
331  // Friend functions
332 
333  friend dimensionSet max(const dimensionSet&, const dimensionSet&);
334  friend dimensionSet min(const dimensionSet&, const dimensionSet&);
335  friend dimensionSet cmptMultiply
336  (
337  const dimensionSet&,
338  const dimensionSet&
339  );
340  friend dimensionSet cmptDivide
341  (
342  const dimensionSet&,
343  const dimensionSet&
344  );
345 
346  friend dimensionSet pow(const dimensionSet&, const scalar);
347  friend dimensionSet pow(const dimensionSet&, const dimensionedScalar&);
348  friend dimensionSet pow(const dimensionedScalar&, const dimensionSet&);
349 
350  friend dimensionSet sqr(const dimensionSet&);
351  friend dimensionSet pow3(const dimensionSet&);
352  friend dimensionSet pow4(const dimensionSet&);
353  friend dimensionSet pow5(const dimensionSet&);
354  friend dimensionSet pow6(const dimensionSet&);
355  friend dimensionSet pow025(const dimensionSet&);
356 
357  friend dimensionSet sqrt(const dimensionSet&);
358  friend dimensionSet magSqr(const dimensionSet&);
359  friend dimensionSet mag(const dimensionSet&);
360  friend dimensionSet sign(const dimensionSet&);
361  friend dimensionSet pos(const dimensionSet&);
362  friend dimensionSet neg(const dimensionSet&);
363  friend dimensionSet inv(const dimensionSet&);
364 
365  //- Function to check the argument is dimensionless
366  // for transcendental functions
367  friend dimensionSet trans(const dimensionSet&);
368 
369  friend dimensionSet atan2(const dimensionSet&, const dimensionSet&);
370 
371  //- Return the argument; transformations do not change the dimensions
372  friend dimensionSet transform(const dimensionSet&);
373 
374 
375  // Friend operators
376 
377  friend dimensionSet operator-(const dimensionSet&);
378 
379  friend dimensionSet operator+
380  (
381  const dimensionSet&,
382  const dimensionSet&
383  );
384 
385  friend dimensionSet operator-
386  (
387  const dimensionSet&,
388  const dimensionSet&
389  );
390 
391  friend dimensionSet operator*
392  (
393  const dimensionSet&,
394  const dimensionSet&
395  );
396 
397  friend dimensionSet operator/
398  (
399  const dimensionSet&,
400  const dimensionSet&
401  );
402 
403  friend dimensionSet operator&
404  (
405  const dimensionSet&,
406  const dimensionSet&
407  );
408 
409  friend dimensionSet operator^
410  (
411  const dimensionSet&,
412  const dimensionSet&
413  );
414 
415  friend dimensionSet operator&&
416  (
417  const dimensionSet&,
418  const dimensionSet&
419  );
420 
421 
422  // IOstream operators
423 
424  friend Istream& operator>>(Istream&, dimensionSet&);
425  friend Ostream& operator<<(Ostream&, const dimensionSet&);
426 };
427 
428 
429 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
430 
431 } // End namespace Foam
432 
433 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
434 
435 #include "dimensionSets.H"
436 
437 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
438 
439 #endif
440 
441 // ************************************************************************* //
dimensionedScalar sqrt(const dimensionedScalar &ds)
tmp< fvMatrix< Type > > operator*(const DimensionedField< scalar, volMesh > &, const fvMatrix< Type > &)
dimensionedScalar pow3(const dimensionedScalar &ds)
bool operator-=(const dimensionSet &) const
Definition: dimensionSet.C:195
Ostream & write(Ostream &os, scalar &multiplier, const dimensionSets &) const
Write using provided units.
bool dimensionless() const
Return true if it is dimensionless.
Definition: dimensionSet.C:89
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
dimensionedScalar neg(const dimensionedScalar &ds)
tmp< GeometricField< Type, fvPatchField, volMesh > > operator&(const fvMatrix< Type > &, const DimensionedField< Type, volMesh > &)
dimensioned< scalar > mag(const dimensioned< Type > &)
friend dimensionSet min(const dimensionSet &, const dimensionSet &)
bool operator+=(const dimensionSet &) const
Definition: dimensionSet.C:181
friend dimensionSet pow025(const dimensionSet &)
dimensioned< scalar > magSqr(const dimensioned< Type > &)
tmp< fvMatrix< Type > > operator+(const fvMatrix< Type > &, const fvMatrix< Type > &)
An STL-conforming hash table.
Definition: HashTable.H:61
ClassName("dimensionSet")
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
dimensionedScalar posPart(const dimensionedScalar &ds)
friend dimensionSet pow(const dimensionSet &, const scalar)
friend dimensionSet pow3(const dimensionSet &)
A class for handling words, derived from string.
Definition: word.H:59
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
dimensionedScalar cbrt(const dimensionedScalar &ds)
dimensionedScalar sign(const dimensionedScalar &ds)
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:133
Useful dimension sets.
Namespace for OpenFOAM.
friend dimensionSet neg(const dimensionSet &)
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)
dimensionedScalar pow6(const dimensionedScalar &ds)
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
HashSet< Key, Hash > operator^(const HashSet< Key, Hash > &hash1, const HashSet< Key, Hash > &hash2)
Create a HashSet that only contains unique entries (xor)
Macro definitions for declaring ClassName(), NamespaceName(), etc.
friend dimensionSet atan2(const dimensionSet &, const dimensionSet &)
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
friend dimensionSet transform(const dimensionSet &)
Return the argument; transformations do not change the dimensions.
friend dimensionSet cmptDivide(const dimensionSet &, const dimensionSet &)
dimensionSet trans(const dimensionSet &)
Definition: dimensionSet.C:438
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
friend dimensionSet pow4(const dimensionSet &)
friend dimensionSet mag(const dimensionSet &)
dimensionedScalar negPart(const dimensionedScalar &ds)
dimensionSet transform(const dimensionSet &)
Definition: dimensionSet.C:465
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Dimension set for the base types.
Definition: dimensionSet.H:116
friend dimensionSet sqr(const dimensionSet &)
static const scalar smallExponent
Definition: dimensionSet.H:143
dimensionedScalar pow025(const dimensionedScalar &ds)
Istream & read(Istream &is, scalar &multiplier, const dictionary &)
Read using provided units. Used only in initial parsing.
bool operator==(const dimensionSet &) const
Definition: dimensionSet.C:143
A token holds items read from Istream.
Definition: token.H:67
friend dimensionSet max(const dimensionSet &, const dimensionSet &)
friend dimensionSet trans(const dimensionSet &)
Function to check the argument is dimensionless.
friend dimensionSet inv(const dimensionSet &)
friend dimensionSet cmptMultiply(const dimensionSet &, const dimensionSet &)
friend Istream & operator>>(Istream &, dimensionSet &)
friend dimensionSet pos(const dimensionSet &)
System bool.
friend dimensionSet magSqr(const dimensionSet &)
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
bool operator=(const dimensionSet &) const
Definition: dimensionSet.C:167
dimensionedScalar pow4(const dimensionedScalar &ds)
autoPtr< dimensionSet > clone() const
Construct and return a clone.
Definition: dimensionSet.H:253
dimensionedScalar pow5(const dimensionedScalar &ds)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
const dimensionedScalar c
Speed of light in a vacuum.
dimensioned< typename scalarProduct< Type1, Type2 >::type > operator&&(const dimensioned< Type1 > &, const dimensioned< Type2 > &)
bool operator*=(const dimensionSet &)
Definition: dimensionSet.C:209
bool operator/=(const dimensionSet &)
Definition: dimensionSet.C:217
dimensionedScalar pos(const dimensionedScalar &ds)
friend Ostream & operator<<(Ostream &, const dimensionSet &)
void reset(const dimensionSet &)
Definition: dimensionSet.C:108
dimensionedSymmTensor sqr(const dimensionedVector &dv)
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
friend dimensionSet operator-(const dimensionSet &)
scalar operator[](const dimensionType) const
Definition: dimensionSet.C:119
dimensionType
Define an enumeration for the names of the dimension exponents.
Definition: dimensionSet.H:129
friend dimensionSet sqrt(const dimensionSet &)
friend dimensionSet sign(const dimensionSet &)
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
dimensionSet(const scalar mass, const scalar length, const scalar time, const scalar temperature, const scalar moles, const scalar current, const scalar luminousIntensity)
Construct given individual dimension exponents for all.
Definition: dimensionSet.C:42
friend dimensionSet pow6(const dimensionSet &)
bool operator!=(const dimensionSet &) const
Definition: dimensionSet.C:161
friend dimensionSet pow5(const dimensionSet &)