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-2023 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, which can be used to implement
32  rigorous dimension checking for algebraic manipulation.
33 
34  The dimensions are specified in the following order
35  (SI units for reference only):
36  \table
37  Property | SI Description | SI unit
38  MASS | kilogram | \c kg
39  LENGTH | metre | \c m
40  TIME | second | \c s
41  TEMPERATURE | Kelvin | \c K
42  MOLES | mole | \c mol
43  CURRENT | Ampere | \c A
44  LUMINOUS_INTENSITY | Candela | \c cd
45  \endtable
46 
47 SourceFiles
48  dimensionSet.C
49  dimensionSetIO.C
50 
51 \*---------------------------------------------------------------------------*/
52 
53 #ifndef Foam_dimensionSet_H
54 #define Foam_dimensionSet_H
55 
56 #include "bool.H"
57 #include "dimensionedScalarFwd.H"
58 #include "className.H"
59 #include "scalarField.H"
60 #include "PtrList.H"
61 #include "HashTable.H"
62 #include "IOobjectOption.H"
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 
69 // Forward Declarations
70 class dictionary;
71 class dimensionSet;
72 class dimensionSets;
73 
74 /*---------------------------------------------------------------------------*\
75  Class dimensionSet Declaration
76 \*---------------------------------------------------------------------------*/
77 
78 class dimensionSet
79 {
80 public:
81 
82  //- The array of dimension exponents
83  typedef FixedList<scalar,7> list_type;
84 
85 
86  // Member Constants
87 
88  //- There are 7 base dimensions
89  static constexpr int nDimensions = 7;
90 
91  //- Enumeration for the dimension exponents
92  enum dimensionType
93  {
94  MASS,
95  LENGTH,
96  TIME,
97  TEMPERATURE,
98  MOLES,
99  CURRENT,
101  };
102 
103 
104  // Static Data Members
106  //- Tolerance for 'small' exponents, for near-zero rounding
107  static const scalar smallExponent;
108 
109 
110 private:
111 
112  // Private Data
113 
114  //- The array of dimension exponents
115  list_type exponents_;
116 
117 
118  // Private Classes
119 
120  class tokeniser
121  {
122  // Private Data
123 
124  Istream& is_;
126  List<token> tokens_;
128  label start_;
130  label size_;
133  // Private Member Functions
134 
135  void push(const token& t);
136 
137  token pop();
138 
139  void unpop(const token& t);
140 
141  public:
143  // Constructors
144 
145  explicit tokeniser(Istream& is);
146 
147 
148  // Member Functions
149 
150  Istream& stream() noexcept { return is_; }
151 
152  bool hasToken() const;
153 
154  token nextToken();
155 
156  void putBack(const token&);
157 
158  void splitWord(const word&);
159 
160  static bool valid(char c);
161 
162  static label priority(const token& t);
163  };
164 
165 
166  //- Reset exponents to nearest integer if close to it.
167  // Handles reading with insufficient precision.
168  void round(const scalar tol);
169 
170  dimensionedScalar parse
171  (
172  const label lastPrior,
173  tokeniser& tis,
174  const HashTable<dimensionedScalar>&
175  ) const;
176 
177 
178 public:
179 
180  // Declare name of the class and its debug switch
181  ClassName("dimensionSet");
182 
183 
184  // Static Functions
185 
186  //- Turn dimension checking on/off.
187  // \return the previous value
188  static bool checking(bool on) noexcept
189  {
190  bool old(debug);
191  debug = static_cast<int>(on);
192  return old;
193  }
194 
195  //- True if dimension checking is enabled (the usual default)
196  static bool checking() noexcept
197  {
198  return static_cast<bool>(debug);
199  }
200 
201 
202  // Constructors
203 
204  //- Default construct (dimensionless).
205  dimensionSet();
206 
207  //- Construct from exponents for the first five or all seven dimensions
209  (
210  const scalar mass,
211  const scalar length,
212  const scalar time,
213  const scalar temperature,
214  const scalar moles,
215  const scalar current = 0,
216  const scalar luminousIntensity = 0
217  );
218 
219  //- Construct from exponents for all seven dimensions
220  dimensionSet(const FixedList<scalar,7>& dimensions);
221 
222  //- Copy construct
223  dimensionSet(const dimensionSet& ds);
224 
225  //- Construct from dictionary entry (usually "dimensions")
226  // Dimensionless if non-mandatory and not found.
228  (
229  const word& entryName,
230  const dictionary& dict,
232  );
233 
234  //- Construct and return a clone
236  {
237  return autoPtr<dimensionSet>::New(*this);
238  }
239 
240  //- Construct from Istream
241  explicit dimensionSet(Istream& is);
242 
243 
244  // Member Functions
245 
246  //- Return true if it is dimensionless
247  bool dimensionless() const;
248 
249  //- Const access to the exponents as a list
250  const FixedList<scalar,7>& values() const noexcept;
251 
252  //- Non-const access to the exponents as a list
253  FixedList<scalar,7>& values() noexcept;
254 
255  //- Clear exponents - resets to be dimensionless
256  void clear();
257 
258  //- Copy assign the exponents from the dimensionSet
259  void reset(const dimensionSet& ds);
260 
261 
262  // IO
263 
264  //- Update the dimensions from dictionary entry.
265  //- FatalIOError if it is found and the number of tokens is incorrect,
266  //- or it is mandatory and not found.
267  //
268  // \return true if the entry was found.
269  bool readEntry
270  (
271  const word& entryName,
272  const dictionary& dict,
273  IOobjectOption::readOption readOpt = IOobjectOption::MUST_READ
275  );
276 
277  //- Update the dimensions from dictionary entry.
278  //- FatalIOError if it is found and the number of tokens is incorrect,
279  //- or it is mandatory and not found.
280  //
281  // \return true if the entry was found.
282  bool readIfPresent
283  (
284  const word& entryName,
285  const dictionary& dict
286  )
287  {
288  return readEntry(entryName, dict, IOobjectOption::READ_IF_PRESENT);
289  }
290 
291  //- Read using provided units, return scaling in multiplier.
292  //- Used only in initial parsing
294  (
295  Istream& is,
296  scalar& multiplier,
297  const dictionary&
298  );
299 
300  //- Read using provided units, return scaling in multiplier
301  Istream& read
302  (
303  Istream& is,
304  scalar& multiplier,
306  );
307 
308  //- Read using system units, return scaling in multiplier
309  Istream& read
310  (
311  Istream& is,
312  scalar& multiplier
313  );
314 
315  //- Write using provided write units, return scaling in multiplier
316  Ostream& write
317  (
318  Ostream& os,
319  scalar& multiplier,
320  const dimensionSets& writeUnits
321  ) const;
322 
323  //- Write using system units, return scaling in multiplier
324  Ostream& write(Ostream& os, scalar& multiplier) const;
325 
326 
327  // Member Operators
328 
329  scalar operator[](const dimensionType) const;
330  scalar& operator[](const dimensionType);
331 
332  scalar operator[](const label) const;
333  scalar& operator[](const label);
334 
335  bool operator==(const dimensionSet&) const;
336  bool operator!=(const dimensionSet&) const;
337 
338  bool operator=(const dimensionSet&) const;
339 
340  bool operator+=(const dimensionSet&) const;
341  bool operator-=(const dimensionSet&) const;
342  bool operator*=(const dimensionSet&);
343  bool operator/=(const dimensionSet&);
344 };
345 
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 // IOstream Operators
350 
353 
354 
355 // Global Functions
356 
357 dimensionSet min(const dimensionSet& a, const dimensionSet& b);
358 dimensionSet max(const dimensionSet& a, const dimensionSet& b);
360 dimensionSet lerp(const dimensionSet& a, const dimensionSet& b);
361 
362 dimensionSet cmptMultiply(const dimensionSet& ds1, const dimensionSet& ds2);
363 dimensionSet cmptDivide(const dimensionSet& ds1, const dimensionSet& ds2);
364 
365 
366 dimensionSet pow(const dimensionSet& ds, const scalar p);
367 dimensionSet pow(const dimensionSet& ds, const dimensionedScalar& p);
368 
369 dimensionSet sqr(const dimensionSet& ds);
370 dimensionSet pow2(const dimensionSet& ds);
371 dimensionSet pow3(const dimensionSet& ds);
372 dimensionSet pow4(const dimensionSet& ds);
373 dimensionSet pow5(const dimensionSet& ds);
374 dimensionSet pow6(const dimensionSet& ds);
375 dimensionSet pow025(const dimensionSet& ds);
376 
377 dimensionSet sqrt(const dimensionSet& ds);
378 dimensionSet cbrt(const dimensionSet& ds);
379 
380 dimensionSet magSqr(const dimensionSet& ds);
381 dimensionSet mag(const dimensionSet& ds);
389 
390 //- The dimensionSet inverted
391 dimensionSet inv(const dimensionSet& ds);
392 
393 //- Check the argument is dimensionless (for transcendental functions)
394 dimensionSet trans(const dimensionSet& ds);
395 
396 //- Arguments need the same dimensions. Return dimensionless.
397 dimensionSet atan2(const dimensionSet& ds1, const dimensionSet& ds2);
398 
399 //- Arguments need the same dimensions. Does not change the dimension.
400 dimensionSet hypot(const dimensionSet& ds1, const dimensionSet& ds2);
401 
402 //- Arguments need the same dimensions. Does not change the dimension.
403 dimensionSet stabilise(const dimensionSet& ds1, const dimensionSet& ds2);
404 
405 //- Return the argument; transformations do not change the dimensions
407 
408 //- Return the argument; transformations do not change the dimensions
410 
411 
412 // Global Operators
413 
414 //- The dimensionSet inverted
416 
417 //- Unary negation.
419 
420 dimensionSet operator+(const dimensionSet& ds1, const dimensionSet& ds2);
421 dimensionSet operator-(const dimensionSet& ds1, const dimensionSet& ds2);
422 dimensionSet operator*(const dimensionSet& ds1, const dimensionSet& ds2);
423 dimensionSet operator/(const dimensionSet& ds1, const dimensionSet& ds2);
424 dimensionSet operator&(const dimensionSet& ds1, const dimensionSet& ds2);
425 dimensionSet operator^(const dimensionSet& ds1, const dimensionSet& ds2);
426 dimensionSet operator&&(const dimensionSet& ds1, const dimensionSet& ds2);
427 
428 
429 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
430 
431 } // End namespace Foam
432 
433 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
434 
435 // Predefined dimensions (eg, dimless, dimMass, dimLength, dimTime, ...)
436 // and unit systems
437 #include "dimensionSets.H"
438 
439 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
440 
441 #endif
442 
443 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
dictionary dict
bool operator/=(const dimensionSet &)
Definition: dimensionSet.C:239
autoPtr< dimensionSet > clone() const
Construct and return a clone.
Definition: dimensionSet.H:293
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition: bitSetI.H:676
dimensionSet invTransform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:527
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
dimensionedSymmTensor sqr(const dimensionedVector &dv)
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds an item read from Istream.
Definition: token.H:65
bool operator-=(const dimensionSet &) const
Definition: dimensionSet.C:220
dimensionedScalar sqrt(const dimensionedScalar &ds)
bool readEntry(const word &entryName, const dictionary &dict, IOobjectOption::readOption readOpt=IOobjectOption::MUST_READ)
Update the dimensions from dictionary entry. FatalIOError if it is found and the number of tokens is ...
dimensionedScalar pow025(const dimensionedScalar &ds)
static const scalar smallExponent
Tolerance for &#39;small&#39; exponents, for near-zero rounding.
Definition: dimensionSet.H:142
bool operator=(const dimensionSet &) const
Definition: dimensionSet.C:198
dimensionSet operator &&(const dimensionSet &ds1, const dimensionSet &ds2)
ClassName("dimensionSet")
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
Ostream & write(Ostream &os, scalar &multiplier, const dimensionSets &writeUnits) const
Write using provided write units, return scaling in multiplier.
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
dimensionedScalar posPart(const dimensionedScalar &ds)
dimensionedScalar neg(const dimensionedScalar &ds)
dimensionedScalar pow5(const dimensionedScalar &ds)
scalar range
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
bool operator*=(const dimensionSet &)
Definition: dimensionSet.C:231
dimensionedScalar pos(const dimensionedScalar &ds)
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:105
const FixedList< scalar, 7 > & values() const noexcept
Const access to the exponents as a list.
Definition: dimensionSet.C:123
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
FixedList< scalar, 7 > list_type
The array of dimension exponents.
Definition: dimensionSet.H:112
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
dimensionedScalar cbrt(const dimensionedScalar &ds)
dimensionedScalar neg0(const dimensionedScalar &ds)
dimensionSet()
Default construct (dimensionless).
Definition: dimensionSet.C:64
Reading is optional [identical to LAZY_READ].
bool operator+=(const dimensionSet &) const
Definition: dimensionSet.C:209
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
dimensionSet pow2(const dimensionSet &ds)
Definition: dimensionSet.C:352
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
bool operator!=(const dimensionSet &) const
Definition: dimensionSet.C:192
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
void reset(const dimensionSet &ds)
Copy assign the exponents from the dimensionSet.
Definition: dimensionSet.C:142
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
dimensionedScalar pos0(const dimensionedScalar &ds)
dimensionedScalar atan2(const dimensionedScalar &x, const dimensionedScalar &y)
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
dimensionSet trans(const dimensionSet &ds)
Check the argument is dimensionless (for transcendental functions)
Definition: dimensionSet.C:471
bool readIfPresent(const word &entryName, const dictionary &dict)
Update the dimensions from dictionary entry. FatalIOError if it is found and the number of tokens is ...
Definition: dimensionSet.H:359
bitSet operator^(const bitSet &a, const bitSet &b)
Bitwise-XOR of two bitsets to form a unique bit-set.
Definition: bitSetI.H:704
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
void clear()
Clear exponents - resets to be dimensionless.
Definition: dimensionSet.C:136
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
dimensionedScalar pow3(const dimensionedScalar &ds)
scalar operator[](const dimensionType) const
Definition: dimensionSet.C:150
static bool checking() noexcept
True if dimension checking is enabled (the usual default)
Definition: dimensionSet.H:241
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
tmp< GeometricField< Type, faPatchField, areaMesh > > operator &(const faMatrix< Type > &, const DimensionedField< Type, areaMesh > &)
Macro definitions for declaring ClassName(), NamespaceName(), etc.
const dimensionedScalar c
Speed of light in a vacuum.
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
dimensionedScalar pow4(const dimensionedScalar &ds)
static constexpr int nDimensions
There are 7 base dimensions.
Definition: dimensionSet.H:120
dimensionedScalar pow6(const dimensionedScalar &ds)
dimensionType
Enumeration for the dimension exponents.
Definition: dimensionSet.H:125
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Useful dimension sets.
volScalarField & p
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:521
bool operator==(const dimensionSet &) const
Definition: dimensionSet.C:174
dimensionedScalar hypot(const dimensionedScalar &x, const dimensionedScalar &y)
bool dimensionless() const
Return true if it is dimensionless.
Definition: dimensionSet.C:107
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Istream & read(Istream &is, scalar &multiplier, const dictionary &)
Read using provided units, return scaling in multiplier. Used only in initial parsing.
System bool.
Namespace for OpenFOAM.
dimensionedScalar negPart(const dimensionedScalar &ds)
dimensionSet clamp(const dimensionSet &a, const dimensionSet &range)
Definition: dimensionSet.C:271
Construction of unit sets.
Definition: dimensionSets.H:89
readOption
Enumeration defining read preferences.