unitSet.C
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 \*---------------------------------------------------------------------------*/
25 
26 #include "units.H"
27 #include "NamedEnum.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
34 }
35 
38  (
40  ({
41  "fraction",
42  "angle"
43  })
44  );
45 
48 
49 const Foam::scalar Foam::unitSet::smallExponent = rootSmall;
50 
51 
52 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
53 
54 bool Foam::unitSet::compare
55 (
56  const unitSet& a,
57  const unitSet& b,
58  const bool compareMultiplier
59 )
60 {
61  if (a.any()) return true;
62  if (b.any()) return true;
63  if (a.none()) return false;
64  if (b.none()) return false;
65 
66  // Check the dimensions are the same
67  if (a.dimensions_ != b.dimensions_) return false;
68 
69  // Check the dimensionless units are the same
70  for (int i = 0; i < unitSet::nDimlessUnits; ++ i)
71  {
72  if
73  (
74  mag(a.exponents_[i] - b.exponents_[i])
76  )
77  {
78  return false;
79  }
80  }
81 
82  // If specified, check the unit conversion factors are the same
83  return !compareMultiplier || a.multiplier_ == b.multiplier_;
84 }
85 
86 
87 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
88 
90 (
91  const dimensionSet& dimensions,
92  const scalar fraction,
93  const scalar angle,
94  const scalar multiplier
95 )
96 :
97  dimensions_(dimensions),
98  multiplier_(multiplier)
99 {
100  exponents_[FRACTION] = fraction;
101  exponents_[ANGLE] = angle;
102 }
103 
104 
106 :
107  dimensions_(dimensions),
108  multiplier_(1)
109 {
110  exponents_[FRACTION] = 0;
111  exponents_[ANGLE] = 0;
112 }
113 
114 
115 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116 
117 void Foam::unitSet::reset(const unitSet& units)
118 {
119  dimensions_.reset(units.dimensions_);
120  for (int i = 0; i < unitSet::nDimlessUnits; ++ i)
121  {
122  exponents_[i] = units.exponents_[i];
123  }
124  multiplier_ = units.multiplier_;
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
129 
130 Foam::unitSet Foam::pow(const unitSet& units, const scalar exp)
131 {
132  if (units.any()) return units;
133  if (units.none()) return units::none;
134 
135  return
136  unitSet
137  (
138  pow(units.dimensions_, exp),
139  units.exponents_[0]*exp,
140  units.exponents_[1]*exp,
141  pow(units.multiplier_, exp)
142  );
143 }
144 
145 
146 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
147 
148 const Foam::unitSet& Foam::operator+
149 (
150  const unitSet& a,
151  const unitSet& b
152 )
153 {
154  if (a.any()) return b;
155  if (b.any()) return a;
156  if (a.none()) return units::none;
157  if (b.none()) return units::none;
158 
159  if (!unitSet::compare(a, b, true))
160  {
162  << "Different units for +" << endl
163  << " units : " << a << " + " << b << endl
164  << abort(FatalError);
165  }
166 
167  return a;
168 }
169 
170 
171 Foam::unitSet Foam::operator*
172 (
173  const unitSet& a,
174  const unitSet& b
175 )
176 {
177  if (a.any()) return a;
178  if (b.any()) return b;
179  if (a.none()) return units::none;
180  if (b.none()) return units::none;
181 
182  return
183  unitSet
184  (
185  a.dimensions_*b.dimensions_,
186  a.exponents_[0] + b.exponents_[0],
187  a.exponents_[1] + b.exponents_[1],
188  a.multiplier_*b.multiplier_
189  );
190 }
191 
192 
193 Foam::unitSet Foam::operator/
194 (
195  const unitSet& a,
196  const unitSet& b
197 )
198 {
199  if (a.any()) return a;
200  if (b.any()) return b;
201  if (a.none()) return units::none;
202  if (b.none()) return units::none;
203 
204  return
205  unitSet
206  (
207  a.dimensions_/b.dimensions_,
208  a.exponents_[0] - b.exponents_[0],
209  a.exponents_[1] - b.exponents_[1],
210  a.multiplier_/b.multiplier_
211  );
212 }
213 
214 
215 // ************************************************************************* //
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:55
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Dimension set for the base types.
Definition: dimensionSet.H:125
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
Definition: unitSet.H:68
bool none() const
Return whether this is the "none" unit. I.e., the case where unit.
Definition: unitSetI.H:68
unitSet(const dimensionSet &, const scalar fraction, const scalar angle, const scalar multiplier)
Construct from components.
Definition: unitSet.C:90
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
void reset(const unitSet &)
Reset the unit conversion.
Definition: unitSet.C:117
bool any() const
Return whether this is the "any" unit. I.e., the case where.
Definition: unitSetI.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
volScalarField & b
Definition: createFields.H:27
const unitSet none
const unitSet fraction
Namespace for OpenFOAM.
dimensionedScalar exp(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
errorManip< error > abort(error &err)
Definition: errorManip.H:131
tmp< DimensionedField< typename powProduct< Type, r >::type, GeoMesh, Field > > pow(const DimensionedField< Type, GeoMesh, PrimitiveField > &df, typename powProduct< Type, r >::type)
tmp< DimensionedField< scalar, GeoMesh, Field > > mag(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
error FatalError
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)
const Foam::autoPtr< Foam::NamedEnum< Foam::unitSet::dimlessUnitType, 2 > > dimlessUnitTypeNamesPtr_(new Foam::NamedEnum< Foam::unitSet::dimlessUnitType, 2 >({ "fraction", "angle" }))
Useful unit conversions.