File size: 10,624 Bytes
5a600cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | /*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "units.H"
#include "demandDrivenData.H"
#include "dictionary.H"
#include "symbols.H"
#include "mathematicalConstants.H"
using namespace Foam::constant::mathematical;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
dictionary* unitsDictPtr_(nullptr);
const dictionary& unitsDict()
{
if (!unitsDictPtr_)
{
dictionary* cachedPtr = nullptr;
unitsDictPtr_ = new dictionary
(
debug::switchSet
(
debug::configDict().found("units")
? "units"
: debug::configDict().found("UnitSets")
? "UnitSets"
: debug::configDict().found("DimensionSets")
? "DimensionSets"
: "units",
cachedPtr
)
);
}
return *unitsDictPtr_;
}
HashTable<unitSet>* addedUnitsPtr_(nullptr);
HashTable<unitSet>* unitsPtr_(nullptr);
// Delete the above data at the end of the run
struct deleteUnitsPtr
{
~deleteUnitsPtr()
{
deleteDemandDrivenData(unitsDictPtr_);
deleteDemandDrivenData(addedUnitsPtr_);
deleteDemandDrivenData(unitsPtr_);
}
};
deleteUnitsPtr deleteUnitsPtr_;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace units
{
unitSet newFraction()
{
return unitSet(dimensionSet(0, 0, 0, 0, 0), 1, 0, 1);
}
unitSet newPercent()
{
return unitSet(dimensionSet(0, 0, 0, 0, 0), 1, 0, 0.01);
}
unitSet newRadians()
{
return unitSet(dimensionSet(0, 0, 0, 0, 0), 0, 1, 1);
}
unitSet newRotations()
{
return unitSet(dimensionSet(0, 0, 0, 0, 0), 0, 1, 2*pi);
}
unitSet newDegrees()
{
return unitSet(dimensionSet(0, 0, 0, 0, 0), 0, 1, pi/180);
}
unitSet length_(dimensionSet(0, 1, 0, 0, 0), 0, 0, 1);
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
const Foam::unitSet Foam::units::unitless(unitSet::newUnitless());
const Foam::unitSet Foam::units::any(unitSet::newAny());
const Foam::unitSet Foam::units::none(unitSet::newNone());
const Foam::unitSet Foam::units::fraction(newFraction());
const Foam::unitSet Foam::units::percent(newPercent());
const Foam::unitSet Foam::units::radians(newRadians());
const Foam::unitSet Foam::units::rotations(newRotations());
const Foam::unitSet Foam::units::degrees(newDegrees());
const Foam::unitSet& Foam::units::length = Foam::units::length_;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
const Foam::HashTable<Foam::unitSet>& Foam::units::table()
{
if (!unitsPtr_)
{
unitsPtr_ = new HashTable<unitSet>();
unitsPtr_->insert("%", newPercent());
unitsPtr_->insert("rad", newRadians());
unitsPtr_->insert("rot", newRotations());
unitsPtr_->insert("deg", newDegrees());
// Get the relevant part of the control dictionary
const dictionary& unitSetDict =
unitsDict().subDict(unitsDict().lookup<word>("set"));
// Read fundamental units
HashTable<label> fundamentalUnits;
forAllConstIter(dictionary, unitSetDict, iter)
{
ITstream& is = iter().stream();
const dimensionSet units(is);
// Check that this is unique
if (fundamentalUnits.found(iter().keyword()))
{
FatalIOErrorInFunction(unitsDict())
<< "Duplicate-fundamental unit specified"
<< exit(FatalIOError);
}
// Get the index of the dimension of this fundamental unit
label i = -1;
for (label j = 0; j < dimensionSet::nDimensions; ++ j)
{
if (units[j] == 0);
else if (units[j] == 1 && i == -1) i = j;
else i = -2;
}
if (i < 0)
{
FatalIOErrorInFunction(unitsDict())
<< "Non-fundamental unit specified" << units
<< exit(FatalIOError);
}
// Store the name
fundamentalUnits.insert(iter().keyword(), i);
// Add to the table
unitsPtr_->insert
(
iter().keyword(),
units*unitSet(dimless, 0, 0, scalar(1))
);
}
// Local function to add a unit to the table with checking
auto addUnit = []
(
const word& name,
const unitSet& units,
const scalar multiplier
)
{
const bool ok =
unitsPtr_->insert
(
name,
units*unitSet(dimless, 0, 0, multiplier)
);
if (!ok)
{
FatalIOErrorInFunction(unitsDict())
<< "Duplicate unit " << name
<< " read from dictionary"
<< exit(FatalIOError);
}
};
// Read conversions to fundamental units. These are defined the wrong
// way around relative to the chosen unit set, so we have to parse them
// and then add the reverse conversion to the table.
forAllConstIter(HashTable<label>, fundamentalUnits, iter)
{
if (!unitsDict().found(iter.key())) continue;
auto readPunctuation = [](symbols::tokeniser& tis, const char p)
{
token t(tis.nextToken());
if (!t.isPunctuation() || t.pToken() != p)
{
FatalIOErrorInFunction(tis.stream())
<< "Illegal token " << t
<< ". Expected character '" << p << "'."
<< exit(FatalIOError);
}
};
ITstream& is = unitsDict().lookup(iter.key());
symbols::tokeniser tis(is);
// Read the name and the multiplier, in whatever order
token t(tis.nextToken());
tis.putBack(t);
word derivedUnitName;
scalar multiplier;
if (!t.isNumber())
{
readPunctuation(tis, token::BEGIN_SQR);
derivedUnitName = tis.nextToken().wordToken();
readPunctuation(tis, token::END_SQR);
multiplier = tis.nextToken().number();
}
else
{
multiplier = tis.nextToken().number();
readPunctuation(tis, token::BEGIN_SQR);
derivedUnitName = tis.nextToken().wordToken();
readPunctuation(tis, token::END_SQR);
}
// Now we have the name and the multiplier, we can add the
// inverse conversion to the table; i.e., flip the names, and
// use the reciprocal of the multiplier
addUnit
(
derivedUnitName,
unitsPtr_->operator[](iter.key()),
1/multiplier
);
}
// Read other units
forAllConstIter(dictionary, unitsDict(), iter)
{
if
(
iter().isDict()
|| iter().keyword() == "set"
|| iter().keyword() == "unitSet"
|| fundamentalUnits.found(iter().keyword())
) continue;
// Read the units and the multiplier, in whatever order
ITstream& is = iter().stream();
token t(is);
is.putBack(t);
unitSet units(units::any);
scalar multiplier;
if (!t.isNumber())
{
is >> units;
multiplier = pTraits<scalar>(is);
}
else
{
multiplier = pTraits<scalar>(is);
is >> units;
}
// Add the conversion to the table
addUnit(iter().keyword(), units, multiplier);
}
// Add programmatically defined units
if (addedUnitsPtr_)
{
forAllConstIter(HashTable<unitSet>, *addedUnitsPtr_, iter)
{
const bool ok = unitsPtr_->insert(iter.key(), iter());
if (!ok)
{
FatalIOErrorInFunction(unitsDict())
<< "Duplicate unit " << iter.key()
<< " added to dictionary"
<< exit(FatalIOError);
}
}
}
}
return *unitsPtr_;
}
void Foam::units::add(const word& name, const unitSet& units)
{
deleteDemandDrivenData(unitsDictPtr_);
if (!addedUnitsPtr_)
{
addedUnitsPtr_ = new HashTable<unitSet>();
}
addedUnitsPtr_->insert(name, units);
deleteDemandDrivenData(unitsPtr_);
}
const Foam::unitSet& Foam::units::lookup(const word& unitName)
{
return table()[unitName];
}
void Foam::units::setLength(const unitSet& length)
{
length_.reset(length);
}
void Foam::units::setLength(const scalar length)
{
length_.reset(unitSet(dimensions::length, 0, 0, length));
}
Foam::scalar Foam::degToRad(const scalar deg)
{
return units::degrees.toStandard(deg);
}
Foam::scalar Foam::radToDeg(const scalar rad)
{
return units::degrees.toUser(rad);
}
// ************************************************************************* //
|