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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::dimensionSet
29
30Description
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
47SourceFiles
48 dimensionSet.C
49 dimensionSetIO.C
50
51\*---------------------------------------------------------------------------*/
52
53#ifndef dimensionSet_H
54#define dimensionSet_H
55
56#include "bool.H"
58#include "className.H"
59#include "scalarField.H"
60#include "PtrList.H"
61#include "HashTable.H"
62
63// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64
65namespace Foam
66{
67
68// Forward Declarations
69class dictionary;
70class dimensionSet;
71class dimensionSets;
72
73/*---------------------------------------------------------------------------*\
74 Class dimensionSet Declaration
75\*---------------------------------------------------------------------------*/
76
77class dimensionSet
78{
79public:
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,
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
109private:
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
123 Istream& is_;
125 List<token> tokens_;
127 label start_;
129 label size_;
130
132 // Private Member Functions
133
134 void push(const token& t);
135
136 token pop();
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
173 (
174 const label lastPrior,
175 tokeniser& tis,
176 const HashTable<dimensionedScalar>&
177 ) const;
178
179
180public:
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
288 (
289 Istream& is,
290 scalar& multiplier,
291 const dictionary&
292 );
293
294 //- Read using provided units
296 (
297 Istream& is,
298 scalar& multiplier,
300 );
301
302 //- Read using system units
304 (
305 Istream& is,
306 scalar& multiplier
307 );
308
309 //- Write using provided units
311 (
312 Ostream& os,
313 scalar& multiplier,
314 const dimensionSets&
315 ) const;
316
317 //- Write using system units
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
349Istream& operator>>(Istream& is, dimensionSet& ds);
350Ostream& operator<<(Ostream& os, const dimensionSet& ds);
351
352
353// Global Functions
354
355dimensionSet min(const dimensionSet& ds1, const dimensionSet& ds2);
356dimensionSet max(const dimensionSet& ds1, const dimensionSet& ds2);
357dimensionSet clip(const dimensionSet& ds1, const dimensionSet& ds2);
358
359dimensionSet cmptMultiply(const dimensionSet& ds1, const dimensionSet& ds2);
360dimensionSet cmptDivide(const dimensionSet& ds1, const dimensionSet& ds2);
361
362
363dimensionSet pow(const dimensionSet& ds, const scalar p);
364dimensionSet pow(const dimensionSet& ds, const dimensionedScalar& dS);
365dimensionSet pow(const dimensionedScalar& dS, const dimensionSet& ds);
366
367dimensionSet sqr(const dimensionSet& ds);
374
377
379dimensionSet mag(const dimensionSet& ds);
387
388//- The dimensionSet inverted
389dimensionSet inv(const dimensionSet& ds);
390
391//- Check the argument is dimensionless (for transcendental functions)
393
394//- Arguments need the same dimensions. Return dimensionless.
395dimensionSet atan2(const dimensionSet& ds1, const dimensionSet& ds2);
396
397//- Arguments need the same dimensions. Does not change the dimension.
398dimensionSet hypot(const dimensionSet& ds1, const dimensionSet& ds2);
399
400//- Arguments need the same dimensions. Does not change the dimension.
401dimensionSet stabilise(const dimensionSet& ds1, const dimensionSet& ds2);
402
403//- Return the argument; transformations do not change the dimensions
405
406//- Return the argument; transformations do not change the dimensions
408
409
410// Global Operators
411
412//- The dimensionSet inverted
413dimensionSet operator~(const dimensionSet& ds);
414
415//- Unary negation.
416dimensionSet operator-(const dimensionSet& ds);
417
418dimensionSet operator+(const dimensionSet& ds1, const dimensionSet& ds2);
419dimensionSet operator-(const dimensionSet& ds1, const dimensionSet& ds2);
420dimensionSet operator*(const dimensionSet& ds1, const dimensionSet& ds2);
421dimensionSet operator/(const dimensionSet& ds1, const dimensionSet& ds2);
422dimensionSet operator&(const dimensionSet& ds1, const dimensionSet& ds2);
423dimensionSet operator^(const dimensionSet& ds1, const dimensionSet& ds2);
424dimensionSet operator&&(const dimensionSet& ds1, const dimensionSet& ds2);
425
426
427// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
428
429} // End namespace Foam
430
431// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
432
433#include "dimensionSets.H"
434
435// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
436
437#endif
438
439// ************************************************************************* //
System bool.
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:109
static constexpr int nDimensions
There are 7 base dimensions.
Definition: dimensionSet.H:119
Istream & read(Istream &is, scalar &multiplier, const dictionary &)
Read using provided units. Used only in initial parsing.
dimensionSet()
Default construct (dimensionless).
Definition: dimensionSet.C:71
static bool checking() noexcept
True if dimension checking is enabled (the usual default)
Definition: dimensionSet.H:229
FixedList< scalar, 7 > list_type
The array of dimension exponents.
Definition: dimensionSet.H:113
ClassName("dimensionSet")
const FixedList< scalar, 7 > & values() const noexcept
Const access to the exponents as a list.
Definition: dimensionSet.C:130
static const scalar smallExponent
Tolerance for 'small' exponents, for near-zero rounding.
Definition: dimensionSet.H:137
bool dimensionless() const
Return true if it is dimensionless.
Definition: dimensionSet.C:114
void clear()
Clear exponents - resets to be dimensionless.
Definition: dimensionSet.C:143
void reset(const dimensionSet &ds)
Copy assign the exponents from the dimensionSet.
Definition: dimensionSet.C:149
dimensionType
Enumeration for the dimension exponents.
Definition: dimensionSet.H:123
@ LUMINOUS_INTENSITY
Candela cd.
Definition: dimensionSet.H:130
@ MASS
kilogram kg
Definition: dimensionSet.H:124
@ CURRENT
Ampere A.
Definition: dimensionSet.H:129
@ TEMPERATURE
Kelvin K.
Definition: dimensionSet.H:127
autoPtr< dimensionSet > clone() const
Construct and return a clone.
Definition: dimensionSet.H:276
bool readEntry(const word &entryName, const dictionary &dict, const bool mandatory=true)
Construction of unit sets.
Definition: dimensionSets.H:83
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
Macro definitions for declaring ClassName(), NamespaceName(), etc.
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:67
volScalarField & p
OBJstream os(runTime.globalPath()/outputName)
int debug
Static debugging option.
Namespace for OpenFOAM.
dimensionedScalar pow6(const dimensionedScalar &ds)
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
dimensionSet invTransform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:542
dimensionedScalar pos(const dimensionedScalar &ds)
dimensionedScalar pow5(const dimensionedScalar &ds)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dimensionSet clip(const dimensionSet &ds1, const dimensionSet &ds2)
Definition: dimensionSet.C:278
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:536
dimensionedScalar pos0(const dimensionedScalar &ds)
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedSymmTensor sqr(const dimensionedVector &dv)
dimensionedScalar pow3(const dimensionedScalar &ds)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
dimensionedScalar atan2(const dimensionedScalar &x, const dimensionedScalar &y)
dimensionedScalar negPart(const dimensionedScalar &ds)
dimensionedScalar sqrt(const dimensionedScalar &ds)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
dimensionedScalar hypot(const dimensionedScalar &x, const dimensionedScalar &y)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
dimensionedScalar pow4(const dimensionedScalar &ds)
dimensionSet trans(const dimensionSet &ds)
Check the argument is dimensionless (for transcendental functions)
Definition: dimensionSet.C:486
dimensionedScalar neg(const dimensionedScalar &ds)
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
const direction noexcept
Definition: Scalar.H:223
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
dimensionedScalar neg0(const dimensionedScalar &ds)
dimensionedScalar cbrt(const dimensionedScalar &ds)
dimensionSet pow2(const dimensionSet &ds)
Definition: dimensionSet.C:369
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
dimensionedScalar posPart(const dimensionedScalar &ds)
dimensionedScalar pow025(const dimensionedScalar &ds)
runTime write()
dictionary dict