unitSet.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2024-2026 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::unitSet
26 
27 Description
28  Unit conversion structure. Contains the associated dimensions and the
29  multiplier with which to convert values.
30 
31 SourceFiles
32  unitSet.C
33  unitSetIO.C
34  unitSetI.H
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef unitSet_H
39 #define unitSet_H
40 
41 #include "dimensionSet.H"
42 #include "nil.H"
43 #include "scaleable.H"
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward declaration of classes
51 class Istream;
52 class Ostream;
53 
54 // Forward declaration of friend functions and operators
55 class unitSet;
56 unitSet pow(const unitSet&, const scalar);
57 const unitSet& operator+(const unitSet&, const unitSet&);
58 unitSet operator*(const unitSet&, const unitSet&);
59 unitSet operator/(const unitSet&, const unitSet&);
60 Istream& operator>>(Istream&, unitSet&);
61 Ostream& operator<<(Ostream&, const unitSet&);
62 Ostream& operator<<(Ostream&, const InfoProxy<unitSet>&);
63 
64 /*---------------------------------------------------------------------------*\
65  Class unitSet Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 class unitSet
69 {
70 public:
71 
72  // Member constants
73 
74  //- Define an enumeration for the number of dimensionless units
75  enum
76  {
78  };
79 
80  //- Define an enumeration for the names of the dimensionless unit
81  // exponents
82  enum dimlessUnitType
83  {
84  FRACTION, // fraction %
85  ANGLE // angle rad, rot, deg
86  };
87 
88  //- Names of the dimensionless units
90 
91 
92  // Static Data Members
93 
94  //- A small exponent with which to perform inexact comparisons
95  static const scalar smallExponent;
96 
97 
98 private:
99 
100  // Private Data
101 
102  //- The dimensions
103  dimensionSet dimensions_;
104 
105  //- Array of dimensionless unit exponents
106  scalar exponents_[nDimlessUnits];
107 
108  //- The conversion multiplier with which to multiply quantities
109  // in this unit in order to recover the quantity in standard units
110  scalar multiplier_;
111 
112 
113  // Private Member Functions
114 
115  //- Compare two unit conversions and return if they are the same.
116  // An additional control determines whether the multiplier is compared
117  // as well as the dimensions and dimensionless units.
118  static bool compare
119  (
120  const unitSet&,
121  const unitSet&,
122  const bool compareMultiplier
123  );
124 
125 
126 public:
127 
128  // Static Data Members
129 
130  //- Run time type information
131  ClassName("unitSet");
132 
133 
134  // Static Member Functions
135 
136  //- Return a new "unitless" unit set
137  inline static unitSet newUnitless();
138 
139  //- Return a new "any" unit set
140  inline static unitSet newAny();
141 
142  //- Return a new "none" unit set
143  inline static unitSet newNone();
144 
145 
146  // Constructors
147 
148  //- Construct from components
149  unitSet
150  (
151  const dimensionSet&,
152  const scalar fraction,
153  const scalar angle,
154  const scalar multiplier
155  );
156 
157  //- Construct from a dimension set. No dimensionless units. Unity
158  // multiplier.
159  unitSet(const dimensionSet&);
160 
161  //- Copy constructor
162  unitSet(const unitSet&) = default;
163 
164  //- Move constructor
165  unitSet(unitSet&&) = default;
166 
167  //- Construct from stream
168  unitSet(Istream& is);
169 
170 
171  // Member Functions
172 
173  //- Access the dimensions
174  inline const dimensionSet& dimensions() const;
175 
176  //- Return true if it is dimensionless
177  inline bool dimensionless() const;
178 
179  //- Convert a value to standard units
180  template<class T>
181  T toStandard(const T&) const;
182 
183  //- Convert a value to standard units
184  template<class T>
185  void makeStandard(T&) const;
186 
187  //- Convert a value to user units
188  template<class T>
189  T toUser(const T&) const;
190 
191  //- Convert a value to user units
192  template<class T>
193  void makeUser(T&) const;
194 
195  //- Return whether this is the "any" unit. I.e., the case where
196  // dimensions and dimensionless units are not checked, and any
197  // conversion is permitted.
198  inline bool any() const;
199 
200  //- Return whether this is the "none" unit. I.e., the case where unit
201  // conversions are prohibited.
202  inline bool none() const;
203 
204  //- Return whether this unit is standard. I.e., is its multiplier one?
205  inline bool standard() const;
206 
207  //- Reset the unit conversion
208  void reset(const unitSet&);
209 
210  //- Update
211  void read(const word& keyword, const dictionary&);
212 
213  //- Update
214  void read(Istream& is);
215 
216  //- Update
217  void read(const word& keyword, const dictionary&, Istream& is);
218 
219  //- Update if found in the dictionary
220  bool readIfPresent(const word& keyword, const dictionary&);
221 
222  //- Update if found on the stream
223  bool readIfPresent(Istream& is);
224 
225  //- Update if found on the dictionary stream
226  bool readIfPresent(const word& keyword, const dictionary&, Istream& is);
227 
228  //- Return info proxy
229  inline InfoProxy<unitSet> info() const
230  {
231  return *this;
232  }
233 
234 
235  // Member Operators
236 
237  //- Access the dimension exponents
238  inline scalar operator[](const dimensionSet::dimensionType type) const;
239 
240  //- Access the dimensionless unit exponents
241  inline scalar operator[](const dimlessUnitType type) const;
242 
243  //- Disallow default bitwise assignment
244  void operator=(const unitSet&) = delete;
245 
246  //- Disallow default bitwise move assignment
247  void operator=(const unitSet&&) = delete;
248 
249 
250  // Friend Functions
251 
252  //- Raise to a power
253  friend unitSet pow(const unitSet&, const scalar);
254 
255 
256  // Friend Operators
257 
258  //- Combine
259  friend const unitSet& operator+
260  (
261  const unitSet&,
262  const unitSet&
263  );
264 
265  //- Multiply
266  friend unitSet operator*
267  (
268  const unitSet&,
269  const unitSet&
270  );
271 
272  //- Divide
273  friend unitSet operator/
274  (
275  const unitSet&,
276  const unitSet&
277  );
278 
279 
280  // IOstream Operators
281 
282  //- Read from stream
283  friend Istream& operator>>(Istream&, unitSet&);
284 
285  //- Write to stream
286  friend Ostream& operator<<(Ostream&, const unitSet&);
287 
288  //- Write info to stream
289  friend Ostream& operator<<(Ostream&, const InfoProxy<unitSet>&);
290 
291 
292  // Public Types
293 
294  //- Functor to convert to standard
295  struct makeStandardOp
296  {
297  template<class Type>
298  inline void operator()(Type& t, const unitSet& units) const
299  {
300  units.makeStandard(t);
301  }
302  };
303 
304  //- Functor to convert to user
305  struct makeUserOp
306  {
307  template<class Type>
308  inline void operator()(Type& t, const unitSet& units) const
309  {
310  units.makeUser(t);
311  }
312  };
313 };
314 
315 
316 // Global Functions
317 
318 //- Convert a primitive type
319 template<class Type, class Convert>
321 (
322  Type& t,
323  const unitSet& units,
324  const Convert&
325 );
326 
327 //- Get the units type for a given type. This returns the unit set for
328 // everything except label, for which it returns nil. This can be used to
329 // modify the type of the units passed to dictionary, such that a single
330 // template instantiation can be used for label as well as the field types.
331 // The label specialisation checks the unit set provided to make sure it is
332 // both dimensionless and standard.
333 template<class Type>
334 struct typeUnitsType { typedef unitSet type; };
335 
336 template<>
337 struct typeUnitsType<label> { typedef nil type; };
338 
339 template<class Type>
340 const typename typeUnitsType<Type>::type& typeUnits
341 (
342  const unitSet&
343 );
344 
345 template<class Type>
347 (
348  const dimensionSet&
349 );
350 
351 template<>
352 inline const typename typeUnitsType<label>::type& typeUnits<label>
353 (
354  const unitSet&
355 );
356 
357 template<>
359 (
360  const dimensionSet&
361 );
362 
363 //- Read a type which supports unit conversion
364 template<class Type>
365 Type readAndConvert(Istream&, const unitSet&);
366 
367 //- Read a type which supports unit conversion
368 template<class Type>
369 Type readAndConvert(Istream&, const dimensionSet&);
370 
371 //- Read a type which supports unit conversion
372 template<class Type, class Units>
373 Type readAndConvert(Istream&, const Units&);
374 
375 //- Read a type which does not support unit conversion
376 template<class Type>
377 Type readAndConvert(Istream&, const nil&);
378 
379 //- Read a type which may or may not support unit conversion
380 template<class Type>
382 
383 //- Read a type which may or may not support unit conversion
384 template<class Type>
386 
387 //- Read a unit set
388 template<>
390 
391 //- Read a dimension set
392 template<>
394 
395 //- Write a type with a given unit conversion
396 template<class Type>
397 void writeEntry(Ostream&, const unitSet&, const Type&);
398 
399 //- Write a type without unit conversion
400 template<class Type>
401 void writeEntry(Ostream&, const nil&, const Type&);
402 
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404 
405 } // End namespace Foam
406 
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408 
409 #include "unitSetI.H"
410 
411 #ifdef NoRepository
412  #include "unitSetTemplates.C"
413 #endif
414 
415 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
416 
417 #endif
418 
419 // ************************************************************************* //
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Dimension set for the base types.
Definition: dimensionSet.H:125
dimensionType
Define an enumeration for the names of the dimension exponents.
Definition: dimensionSet.H:139
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:59
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
Definition: unitSet.H:68
void operator=(const unitSet &)=delete
Disallow default bitwise assignment.
static unitSet newAny()
Return a new "any" unit set.
Definition: unitSetI.H:36
bool readIfPresent(const word &keyword, const dictionary &)
Update if found in the dictionary.
Definition: unitSetIO.C:106
T toUser(const T &) const
Convert a value to user units.
bool none() const
Return whether this is the "none" unit. I.e., the case where unit.
Definition: unitSetI.H:68
InfoProxy< unitSet > info() const
Return info proxy.
Definition: unitSet.H:228
const dimensionSet & dimensions() const
Access the dimensions.
Definition: unitSetI.H:50
void makeStandard(T &) const
Convert a value to standard units.
static unitSet newNone()
Return a new "none" unit set.
Definition: unitSetI.H:42
bool standard() const
Return whether this unit is standard. I.e., is its multiplier one?
Definition: unitSetI.H:74
static unitSet newUnitless()
Return a new "unitless" unit set.
Definition: unitSetI.H:30
void read(const word &keyword, const dictionary &)
Update.
Definition: unitSetIO.C:44
unitSet(const dimensionSet &, const scalar fraction, const scalar angle, const scalar multiplier)
Construct from components.
Definition: unitSet.C:90
scalar operator[](const dimensionSet::dimensionType type) const
Access the dimension exponents.
Definition: unitSetI.H:83
T toStandard(const T &) const
Convert a value to standard units.
static const scalar smallExponent
A small exponent with which to perform inexact comparisons.
Definition: unitSet.H:94
static const NamedEnum< dimlessUnitType, 2 > & dimlessUnitTypeNames_
Names of the dimensionless units.
Definition: unitSet.H:88
@ nDimlessUnits
Definition: unitSet.H:76
bool dimensionless() const
Return true if it is dimensionless.
Definition: unitSetI.H:56
ClassName("unitSet")
Run time type information.
dimlessUnitType
Define an enumeration for the names of the dimensionless unit.
Definition: unitSet.H:82
friend unitSet pow(const unitSet &, const scalar)
Raise to a power.
void reset(const unitSet &)
Reset the unit conversion.
Definition: unitSet.C:117
friend Ostream & operator<<(Ostream &, const unitSet &)
Write to stream.
bool any() const
Return whether this is the "any" unit. I.e., the case where.
Definition: unitSetI.H:62
friend Istream & operator>>(Istream &, unitSet &)
Read from stream.
void makeUser(T &) const
Convert a value to user units.
A class for handling words, derived from string.
Definition: word.H:63
const unitSet fraction
Namespace for OpenFOAM.
Istream & operator>>(Istream &, pointEdgeDist &)
Definition: pointEdgeDist.C:41
std::enable_if_t<!scaleable< Type >::value, Void > enableIfNotScaleable
Definition: scaleable.H:56
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
std::enable_if_t< std::is_same< scalar, typename pTraits< Type >::cmptType >::value, Void > enableIfScalarCmptType
Definition: scalar.H:196
const typeUnitsType< label >::type & typeUnits< label >(const unitSet &)
Type readAndConvert(Istream &, const unitSet &)
Read a type which supports unit conversion.
tmp< DimensionedField< Type, GeoMesh, Field > > operator/(const DimensionedField< Type, GeoMesh, PrimitiveField1 > &df1, const DimensionedField< scalar, GeoMesh, PrimitiveField2 > &df2)
tmp< DimensionedField< typename typeOfSum< Type1, Type2 >::type, GeoMesh, Field > > operator+(const DimensionedField< Type1, GeoMesh, PrimitiveField1 > &df1, const DimensionedField< Type2, GeoMesh, PrimitiveField2 > &df2)
tmp< DimensionedField< Type, GeoMesh, Field > > operator*(const DimensionedField< Type, GeoMesh, PrimitiveField1 > &df1, const DimensionedField< scalar, GeoMesh, PrimitiveField2 > &df2)
enableIfScaleable< Type, Type > readAndMaybeConvert(Istream &)
Read a type which may or may not support unit conversion.
tmp< DimensionedField< typename powProduct< Type, r >::type, GeoMesh, Field > > pow(const DimensionedField< Type, GeoMesh, PrimitiveField > &df, typename powProduct< Type, r >::type)
dimensionSet readAndMaybeConvert< dimensionSet >(Istream &)
Read a dimension set.
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
std::enable_if_t< scaleable< Type >::value, Void > enableIfScaleable
Definition: scaleable.H:53
unitSet readAndMaybeConvert< unitSet >(Istream &)
Read a unit set.
void T(GeometricField< Type, GeoMesh, PrimitiveField1 > &gf, const GeometricField< Type, GeoMesh, PrimitiveField2 > &gf1)
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)
const typeUnitsType< Type >::type & typeUnits(const unitSet &)
void convert(UList< Type > &l, const Args &... args)
Apply a conversion to a UList by applying to each element individually.
Definition: UList.H:421
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
Trait to identify types which are "scaleable"; i.e., that can be multiply-equals-d with a scalar....
Get the units type for a given type. This returns the unit set for.
Definition: unitSet.H:333
Functor to convert to standard.
Definition: unitSet.H:295
void operator()(Type &t, const unitSet &units) const
Definition: unitSet.H:297
Functor to convert to user.
Definition: unitSet.H:305
void operator()(Type &t, const unitSet &units) const
Definition: unitSet.H:307