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-2021 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 dimensionSet_H
54 #define 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 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 namespace Foam
66 {
67 
68 // Forward Declarations
69 class dictionary;
70 class dimensionSet;
71 class dimensionSets;
72 
73 /*---------------------------------------------------------------------------*\
74  Class dimensionSet Declaration
75 \*---------------------------------------------------------------------------*/
76 
77 class dimensionSet
78 {
79 public:
80 
81  //- The array of dimension exponents
82  typedef FixedList<scalar,7> list_type;
83 
84 
85  // Member Constants
86 
87  //- There are 7 base dimensions
88  static constexpr int nDimensions = 7;
89 
90  //- Enumeration for the dimension exponents
91  enum dimensionType
92  {
93  MASS,
94  LENGTH,
95  TIME,
96  TEMPERATURE,
97  MOLES,
98  CURRENT,
100  };
101 
102 
103  // Static Data Members
104 
105  //- Tolerance for 'small' exponents, for near-zero rounding
106  static const scalar smallExponent;
107 
108 
109 private:
110 
111  // Private Data
112 
113  //- The array of dimension exponents
114  list_type exponents_;
115 
116 
117  // Private Classes
118 
119  class tokeniser
120  {
121  // Private Data
122 
123  Istream& is_;
124 
125  List<token> tokens_;
126 
127  label start_;
128 
129  label size_;
130 
131 
132  // Private Member Functions
133 
134  void push(const token& t);
135 
136  token pop();
137 
138  void unpop(const token& t);
139 
140  public:
141 
142  // Constructors
143 
144  explicit tokeniser(Istream& is);
145 
146 
147  // Member Functions
148 
149  Istream& stream()
150  {
151  return is_;
152  }
153 
154  bool hasToken() const;
155 
156  token nextToken();
157 
158  void putBack(const token&);
159 
160  void splitWord(const word&);
161 
162  static bool valid(char c);
163 
164  static label priority(const token& t);
165  };
166 
167 
168  //- Reset exponents to nearest integer if close to it.
169  // Handles reading with insufficient precision.
170  void round(const scalar tol);
171 
172  dimensionedScalar parse
173  (
174  const label lastPrior,
175  tokeniser& tis,
176  const HashTable<dimensionedScalar>&
177  ) const;
178 
179 
180 public:
181 
182  // Declare name of the class and its debug switch
183  ClassName("dimensionSet");
184 
185 
186  // Static Functions
187 
188  //- Turn dimension checking on/off.
189  // \return the previous value
190  static bool checking(const bool on) noexcept
191  {
192  bool old(debug);
193  debug = static_cast<int>(on);
194  return old;
195  }
196 
197  //- True if dimension checking is enabled (the usual default)
198  static bool checking() noexcept
199  {
200  return static_cast<bool>(debug);
201  }
202 
203 
204  // Constructors
205 
206  //- Default construct (dimensionless).
207  dimensionSet();
208 
209  //- Construct from exponents for the first five or all seven dimensions
211  (
212  const scalar mass,
213  const scalar length,
214  const scalar time,
215  const scalar temperature,
216  const scalar moles,
217  const scalar current = 0,
218  const scalar luminousIntensity = 0
219  );
220 
221  //- Construct from exponents for all seven dimensions
222  dimensionSet(const FixedList<scalar,7>& dimensions);
223 
224  //- Copy construct
225  dimensionSet(const dimensionSet& ds);
226 
227  //- Construct from dictionary entry (usually "dimensions")
228  // Dimensionless if non-mandatory and not found.
230  (
231  const word& entryName,
232  const dictionary& dict,
233  const bool mandatory = true
234  );
235 
236  //- Construct from dictionary entry (usually "dimensions")
238  (
239  const dictionary& dict,
240  const word& entryName,
241  const bool mandatory = true
242  );
243 
244  //- Construct and return a clone
246  {
247  return autoPtr<dimensionSet>::New(*this);
248  }
249 
250  //- Construct from Istream
251  explicit dimensionSet(Istream& is);
252 
253 
254  // Member Functions
255 
256  //- Return true if it is dimensionless
257  bool dimensionless() const;
258 
259  //- Const access to the exponents as a list
260  const FixedList<scalar,7>& values() const noexcept;
261 
262  //- Non-const access to the exponents as a list
263  FixedList<scalar,7>& values() noexcept;
264 
265  //- Clear exponents - resets to be dimensionless
266  void clear();
267 
268  //- Copy assign the exponents from the dimensionSet
269  void reset(const dimensionSet& ds);
270 
271 
272  // IO
273 
274  //- Update the dimensions from dictionary entry.
275  //- FatalIOError if it is found and the number of tokens is incorrect,
276  //- or it is mandatory and not found.
277  //
278  // \return true if the entry was found.
279  bool readEntry
280  (
281  const word& entryName,
282  const dictionary& dict,
283  const bool mandatory = true
284  );
285 
286  //- Read using provided units. Used only in initial parsing
287  Istream& read
288  (
289  Istream& is,
290  scalar& multiplier,
291  const dictionary&
292  );
293 
294  //- Read using provided units
295  Istream& read
296  (
297  Istream& is,
298  scalar& multiplier,
300  );
301 
302  //- Read using system units
303  Istream& read
304  (
305  Istream& is,
306  scalar& multiplier
307  );
308 
309  //- Write using provided units
310  Ostream& write
311  (
312  Ostream& os,
313  scalar& multiplier,
314  const dimensionSets&
315  ) const;
316 
317  //- Write using system units
318  Ostream& write
319  (
320  Ostream& os,
321  scalar& multiplier
322  ) const;
323 
324 
325  // Member Operators
326 
327  scalar operator[](const dimensionType) const;
328  scalar& operator[](const dimensionType);
329 
330  scalar operator[](const label) const;
331  scalar& operator[](const label);
332 
333  bool operator==(const dimensionSet&) const;
334  bool operator!=(const dimensionSet&) const;
335 
336  bool operator=(const dimensionSet&) const;
337 
338  bool operator+=(const dimensionSet&) const;
339  bool operator-=(const dimensionSet&) const;
340  bool operator*=(const dimensionSet&);
341  bool operator/=(const dimensionSet&);
342 };
343 
344 
345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 
347 // IOstream Operators
348 
349 Istream& operator>>(Istream& is, dimensionSet& ds);
350 Ostream& operator<<(Ostream& os, const dimensionSet& ds);
351 
352 
353 // Global Functions
354 
355 dimensionSet min(const dimensionSet& ds1, const dimensionSet& ds2);
356 dimensionSet max(const dimensionSet& ds1, const dimensionSet& ds2);
357 dimensionSet clip(const dimensionSet& ds1, const dimensionSet& ds2);
358 
359 dimensionSet cmptMultiply(const dimensionSet& ds1, const dimensionSet& ds2);
360 dimensionSet cmptDivide(const dimensionSet& ds1, const dimensionSet& ds2);
361 
362 
363 dimensionSet pow(const dimensionSet& ds, const scalar p);
364 dimensionSet pow(const dimensionSet& ds, const dimensionedScalar& dS);
365 dimensionSet pow(const dimensionedScalar& dS, const dimensionSet& ds);
366 
367 dimensionSet sqr(const dimensionSet& ds);
368 dimensionSet pow2(const dimensionSet& ds);
369 dimensionSet pow3(const dimensionSet& ds);
370 dimensionSet pow4(const dimensionSet& ds);
371 dimensionSet pow5(const dimensionSet& ds);
372 dimensionSet pow6(const dimensionSet& ds);
373 dimensionSet pow025(const dimensionSet& ds);
374 
375 dimensionSet sqrt(const dimensionSet& ds);
376 dimensionSet cbrt(const dimensionSet& ds);
377 
378 dimensionSet magSqr(const dimensionSet& ds);
379 dimensionSet mag(const dimensionSet& ds);
387 
388 //- The dimensionSet inverted
389 dimensionSet inv(const dimensionSet& ds);
390 
391 //- Check the argument is dimensionless (for transcendental functions)
392 dimensionSet trans(const dimensionSet& ds);
393 
394 //- Arguments need the same dimensions. Return dimensionless.
395 dimensionSet atan2(const dimensionSet& ds1, const dimensionSet& ds2);
396 
397 //- Arguments need the same dimensions. Does not change the dimension.
398 dimensionSet hypot(const dimensionSet& ds1, const dimensionSet& ds2);
399 
400 //- Return the argument; transformations do not change the dimensions
402 
403 //- Return the argument; transformations do not change the dimensions
405 
406 
407 // Global Operators
408 
409 //- The dimensionSet inverted
410 dimensionSet operator~(const dimensionSet& ds);
411 
412 //- Unary negation.
413 dimensionSet operator-(const dimensionSet& ds);
414 
415 dimensionSet operator+(const dimensionSet& ds1, const dimensionSet& ds2);
416 dimensionSet operator-(const dimensionSet& ds1, const dimensionSet& ds2);
417 dimensionSet operator*(const dimensionSet& ds1, const dimensionSet& ds2);
418 dimensionSet operator/(const dimensionSet& ds1, const dimensionSet& ds2);
419 dimensionSet operator&(const dimensionSet& ds1, const dimensionSet& ds2);
420 dimensionSet operator^(const dimensionSet& ds1, const dimensionSet& ds2);
421 dimensionSet operator&&(const dimensionSet& ds1, const dimensionSet& ds2);
422 
423 
424 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
425 
426 } // End namespace Foam
427 
428 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
429 
430 #include "dimensionSets.H"
431 
432 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
433 
434 #endif
435 
436 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::dimensionSet::nDimensions
static constexpr int nDimensions
There are 7 base dimensions.
Definition: dimensionSet.H:119
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:486
Foam::dimensionSet::ClassName
ClassName("dimensionSet")
Foam::dimensionSet::clone
autoPtr< dimensionSet > clone() const
Construct and return a clone.
Definition: dimensionSet.H:276
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::dimensionSet::readEntry
bool readEntry(const word &entryName, const dictionary &dict, const bool mandatory=true)
Definition: dimensionSetIO.C:429
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
HashTable.H
Foam::cmptMultiply
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::pow2
dimensionSet pow2(const dimensionSet &ds)
Definition: dimensionSet.C:369
Foam::dimensionSet::LUMINOUS_INTENSITY
Candela cd.
Definition: dimensionSet.H:130
scalarField.H
Foam::dimensionSet::smallExponent
static const scalar smallExponent
Tolerance for 'small' exponents, for near-zero rounding.
Definition: dimensionSet.H:137
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::neg0
dimensionedScalar neg0(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:210
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, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:108
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:521
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
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:122
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:548
Foam::dimensionSet::TEMPERATURE
Kelvin K.
Definition: dimensionSet.H:127
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:42
bool.H
System bool.
Foam::dimensionSet::list_type
FixedList< scalar, 7 > list_type
The array of dimension exponents.
Definition: dimensionSet.H:113
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:75
Foam::dimensionSet::clear
void clear()
Clear exponents - resets to be dimensionless.
Definition: dimensionSet.C:143
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
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::pow5
dimensionedScalar pow5(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:111
os
OBJstream os(runTime.globalPath()/outputName)
Foam::dimensioned
Generic dimensioned Type class.
Definition: dimensionedScalarFwd.H:42
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::dimensionSet::MASS
kilogram kg
Definition: dimensionSet.H:124
Foam::dimensionSet::values
const FixedList< scalar, 7 > & values() const noexcept
Const access to the exponents as a list.
Definition: dimensionSet.C:130
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:672
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:278
Foam::dimensionSet::CURRENT
Ampere A.
Definition: dimensionSet.H:129
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::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::FixedList< scalar, 7 >
Foam::invTransform
dimensionSet invTransform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:527
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
PtrList.H
Foam::dimensionSet::TIME
second s
Definition: dimensionSet.H:126
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::dimensionSet::LENGTH
metre m
Definition: dimensionSet.H:125
Foam::cbrt
dimensionedScalar cbrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:155
Foam::dimensionSet::MOLES
mole mol
Definition: dimensionSet.H:128
Foam::neg
dimensionedScalar neg(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:199
Foam::dimensionSet::checking
static bool checking() noexcept
True if dimension checking is enabled (the usual default)
Definition: dimensionSet.H:229
Foam::dimensionSets
Construction of unit sets.
Definition: dimensionSets.H:82
Foam::dimensionSet::reset
void reset(const dimensionSet &ds)
Copy assign the exponents from the dimensionSet.
Definition: dimensionSet.C:149
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177