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 "scalable.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  //- Return true if it is unit-less
180  inline bool unitless() const;
181 
182  //- Convert a value to standard units
183  template<class T>
184  T toStandard(const T&) const;
185 
186  //- Convert a value to standard units
187  template<class T>
188  void makeStandard(T&) const;
189 
190  //- Convert a value to user units
191  template<class T>
192  T toUser(const T&) const;
193 
194  //- Convert a value to user units
195  template<class T>
196  void makeUser(T&) const;
197 
198  //- Return whether this is the "any" unit. I.e., the case where
199  // dimensions and dimensionless units are not checked, and any
200  // conversion is permitted.
201  inline bool any() const;
202 
203  //- Return whether this is the "none" unit. I.e., the case where unit
204  // conversions are prohibited.
205  inline bool none() const;
206 
207  //- Return whether this unit is standard. I.e., is its multiplier one?
208  inline bool standard() const;
209 
210  //- Reset the unit conversion
211  void reset(const unitSet&);
212 
213  //- Update
214  void read(const word& keyword, const dictionary&);
215 
216  //- Update
217  void read(Istream& is);
218 
219  //- Update
220  void read(const word& keyword, const dictionary&, Istream& is);
221 
222  //- Update if found in the dictionary
223  bool readIfPresent(const word& keyword, const dictionary&);
224 
225  //- Update if found on the stream
226  bool readIfPresent(Istream& is);
227 
228  //- Update if found on the dictionary stream
229  bool readIfPresent(const word& keyword, const dictionary&, Istream& is);
230 
231  //- Return info proxy
232  inline InfoProxy<unitSet> info() const
233  {
234  return *this;
235  }
236 
237 
238  // Member Operators
239 
240  //- Access the dimension exponents
241  inline scalar operator[](const dimensionSet::dimensionType type) const;
242 
243  //- Access the dimensionless unit exponents
244  inline scalar operator[](const dimlessUnitType type) const;
245 
246  //- Disallow default bitwise assignment
247  void operator=(const unitSet&) = delete;
248 
249  //- Disallow default bitwise move assignment
250  void operator=(const unitSet&&) = delete;
251 
252 
253  // Friend Functions
254 
255  //- Raise to a power
256  friend unitSet pow(const unitSet&, const scalar);
257 
258 
259  // Friend Operators
260 
261  //- Combine
262  friend const unitSet& operator+
263  (
264  const unitSet&,
265  const unitSet&
266  );
267 
268  //- Multiply
269  friend unitSet operator*
270  (
271  const unitSet&,
272  const unitSet&
273  );
274 
275  //- Divide
276  friend unitSet operator/
277  (
278  const unitSet&,
279  const unitSet&
280  );
281 
282 
283  // IOstream Operators
284 
285  //- Read from stream
286  friend Istream& operator>>(Istream&, unitSet&);
287 
288  //- Write to stream
289  friend Ostream& operator<<(Ostream&, const unitSet&);
290 
291  //- Write info to stream
292  friend Ostream& operator<<(Ostream&, const InfoProxy<unitSet>&);
293 
294 
295  // Public Types
296 
297  //- Functor to convert to standard
298  struct makeStandardOp
299  {
300  template<class Type>
301  inline void operator()(Type& t, const unitSet& units) const
302  {
303  units.makeStandard(t);
304  }
305  };
306 
307  //- Functor to convert to user
308  struct makeUserOp
309  {
310  template<class Type>
311  inline void operator()(Type& t, const unitSet& units) const
312  {
313  units.makeUser(t);
314  }
315  };
316 };
317 
318 
319 // Global Functions
320 
321 //- Convert a primitive type
322 template<class Type, class Convert>
324 (
325  Type& t,
326  const unitSet& units,
327  const Convert&
328 );
329 
330 //- Get the units type for a given type. This returns the unit set for
331 // everything except label, for which it returns nil. This can be used to
332 // modify the type of the units passed to dictionary, such that a single
333 // template instantiation can be used for label as well as the field types.
334 // The label specialisation checks the unit set provided to make sure it is
335 // both dimensionless and standard.
336 template<class Type>
337 struct typeUnitsType { typedef unitSet type; };
338 
339 template<>
340 struct typeUnitsType<label> { typedef nil type; };
341 
342 template<class Type>
343 const typename typeUnitsType<Type>::type& typeUnits
344 (
345  const unitSet&
346 );
347 
348 template<class Type>
350 (
351  const dimensionSet&
352 );
353 
354 template<>
355 inline const typename typeUnitsType<label>::type& typeUnits<label>
356 (
357  const unitSet&
358 );
359 
360 template<>
362 (
363  const dimensionSet&
364 );
365 
366 //- Read a type which supports unit conversion
367 template<class Type>
368 Type readAndConvert(Istream&, const unitSet&);
369 
370 //- Read a type which supports unit conversion
371 template<class Type>
372 Type readAndConvert(Istream&, const dimensionSet&);
373 
374 //- Read a type which supports unit conversion
375 template<class Type, class Units>
376 Type readAndConvert(Istream&, const Units&);
377 
378 //- Read a type which does not support unit conversion
379 template<class Type>
380 Type readAndConvert(Istream&, const nil&);
381 
382 //- Read a type which may or may not support unit conversion
383 template<class Type>
385 
386 //- Read a type which may or may not support unit conversion
387 template<class Type>
389 
390 //- Read a unit set
391 template<>
393 
394 //- Read a dimension set
395 template<>
397 
398 //- Write a type with a given unit conversion
399 template<class Type>
400 void writeEntry(Ostream&, const unitSet&, const Type&);
401 
402 //- Write a type without unit conversion
403 template<class Type>
404 void writeEntry(Ostream&, const nil&, const Type&);
405 
406 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407 
408 } // End namespace Foam
409 
410 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
411 
412 #include "unitSetI.H"
413 
414 #ifdef NoRepository
415  #include "unitSetTemplates.C"
416 #endif
417 
418 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
419 
420 #endif
421 
422 // ************************************************************************* //
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.
bool unitless() const
Return true if it is unit-less.
Definition: unitSetI.H:62
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:74
InfoProxy< unitSet > info() const
Return info proxy.
Definition: unitSet.H:231
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:80
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:89
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:68
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
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)
std::enable_if_t< scalable< Type >::value, Void > enableIfScalable
Definition: scalable.H:53
std::enable_if_t<!scalable< Type >::value, Void > enableIfNotScalable
Definition: scalable.H:56
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.
enableIfScalable< Type, Type > readAndMaybeConvert(Istream &)
Read a type which may or may not support unit conversion.
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
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 "scalable"; 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:336
Functor to convert to standard.
Definition: unitSet.H:298
void operator()(Type &t, const unitSet &units) const
Definition: unitSet.H:300
Functor to convert to user.
Definition: unitSet.H:308
void operator()(Type &t, const unitSet &units) const
Definition: unitSet.H:310