1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "dimensionSet.H"
27 #include "dimensionedScalar.H"
28 #include "OStringStream.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::dimensionSet, 1);
33 const Foam::scalar Foam::dimensionSet::smallExponent = SMALL;
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 Foam::dimensionSet::dimensionSet
43 const scalar temperature,
46 const scalar luminousIntensity
49 exponents_[MASS] = mass;
50 exponents_[LENGTH] = length;
51 exponents_[TIME] = time;
52 exponents_[TEMPERATURE] = temperature;
53 exponents_[MOLES] = moles;
54 exponents_[CURRENT] = current;
55 exponents_[LUMINOUS_INTENSITY] = luminousIntensity;
59 Foam::dimensionSet::dimensionSet
64 const scalar temperature,
68 exponents_[MASS] = mass;
69 exponents_[LENGTH] = length;
70 exponents_[TIME] = time;
71 exponents_[TEMPERATURE] = temperature;
72 exponents_[MOLES] = moles;
73 exponents_[CURRENT] = 0;
74 exponents_[LUMINOUS_INTENSITY] = 0;
78 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 bool Foam::dimensionSet::dimensionless() const
82 for (int Dimension=0; Dimension<nDimensions; ++Dimension)
84 // ie, mag(exponents_[Dimension]) > smallExponent
87 exponents_[Dimension] > smallExponent
88 || exponents_[Dimension] < -smallExponent
99 void Foam::dimensionSet::reset(const dimensionSet& ds)
101 for (int Dimension=0; Dimension<nDimensions; ++Dimension)
103 exponents_[Dimension] = ds.exponents_[Dimension];
108 Foam::string Foam::dimensionSet::asText() const
112 bool Dimensionless = true;
114 for (int Dimension=0; Dimension < dimensionSet::nDimensions-1; ++Dimension)
116 const scalar& expt = exponents_[Dimension];
118 if (expt < smallExponent && expt > -smallExponent)
125 Dimensionless = false;
132 // note: currently only handle SI
159 case LUMINOUS_INTENSITY:
164 buf << "??"; // this shouldn't be - flag as being weird
185 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
187 Foam::scalar Foam::dimensionSet::operator[](const dimensionType type) const
189 return exponents_[type];
193 Foam::scalar& Foam::dimensionSet::operator[](const dimensionType type)
195 return exponents_[type];
199 bool Foam::dimensionSet::operator==(const dimensionSet& ds) const
201 for (int Dimension=0; Dimension < nDimensions; ++Dimension)
205 mag(exponents_[Dimension] - ds.exponents_[Dimension])
217 bool Foam::dimensionSet::operator!=(const dimensionSet& ds) const
219 return !(operator==(ds));
223 bool Foam::dimensionSet::operator=(const dimensionSet& ds) const
225 if (dimensionSet::debug && *this != ds)
227 FatalErrorIn("dimensionSet::operator=(const dimensionSet&) const")
228 << "Different dimensions for =" << endl
229 << " dimensions : " << *this << " = " << ds << endl
230 << abort(FatalError);
237 bool Foam::dimensionSet::operator+=(const dimensionSet& ds) const
239 if (dimensionSet::debug && *this != ds)
241 FatalErrorIn("dimensionSet::operator+=(const dimensionSet&) const")
242 << "Different dimensions for +=" << endl
243 << " dimensions : " << *this << " = " << ds << endl
244 << abort(FatalError);
251 bool Foam::dimensionSet::operator-=(const dimensionSet& ds) const
253 if (dimensionSet::debug && *this != ds)
255 FatalErrorIn("dimensionSet::operator-=(const dimensionSet&) const")
256 << "Different dimensions for -=" << endl
257 << " dimensions : " << *this << " = " << ds << endl
258 << abort(FatalError);
265 bool Foam::dimensionSet::operator*=(const dimensionSet& ds)
273 bool Foam::dimensionSet::operator/=(const dimensionSet& ds)
281 // * * * * * * * * * * * * * * * Friend functions * * * * * * * * * * * * * * //
283 Foam::dimensionSet Foam::max(const dimensionSet& ds1, const dimensionSet& ds2)
285 if (dimensionSet::debug && ds1 != ds2)
287 FatalErrorIn("max(const dimensionSet&, const dimensionSet&)")
288 << "Arguments of max have different dimensions" << endl
289 << " dimensions : " << ds1 << " and " << ds2 << endl
290 << abort(FatalError);
297 Foam::dimensionSet Foam::min(const dimensionSet& ds1, const dimensionSet& ds2)
299 if (dimensionSet::debug && ds1 != ds2)
301 FatalErrorIn("min(const dimensionSet&, const dimensionSet&)")
302 << "Arguments of min have different dimensions" << endl
303 << " dimensions : " << ds1 << " and " << ds2 << endl
304 << abort(FatalError);
311 Foam::dimensionSet Foam::cmptMultiply
313 const dimensionSet& ds1,
314 const dimensionSet& ds2
321 Foam::dimensionSet Foam::cmptDivide
323 const dimensionSet& ds1,
324 const dimensionSet& ds2
331 Foam::dimensionSet Foam::pow(const dimensionSet& ds, const scalar p)
335 ds[dimensionSet::MASS]*p,
336 ds[dimensionSet::LENGTH]*p,
337 ds[dimensionSet::TIME]*p,
338 ds[dimensionSet::TEMPERATURE]*p,
339 ds[dimensionSet::MOLES]*p,
340 ds[dimensionSet::CURRENT]*p,
341 ds[dimensionSet::LUMINOUS_INTENSITY]*p
348 Foam::dimensionSet Foam::pow
350 const dimensionSet& ds,
351 const dimensionedScalar& dS
354 if (dimensionSet::debug && !dS.dimensions().dimensionless())
356 FatalErrorIn("pow(const dimensionSet&, const dimensionedScalar&)")
357 << "Exponent of pow is not dimensionless"
358 << abort(FatalError);
363 ds[dimensionSet::MASS]*dS.value(),
364 ds[dimensionSet::LENGTH]*dS.value(),
365 ds[dimensionSet::TIME]*dS.value(),
366 ds[dimensionSet::TEMPERATURE]*dS.value(),
367 ds[dimensionSet::MOLES]*dS.value(),
368 ds[dimensionSet::CURRENT]*dS.value(),
369 ds[dimensionSet::LUMINOUS_INTENSITY]*dS.value()
376 Foam::dimensionSet Foam::pow
378 const dimensionedScalar& dS,
379 const dimensionSet& ds
385 && !dS.dimensions().dimensionless()
386 && !ds.dimensionless()
389 FatalErrorIn("pow(const dimensionedScalar&, const dimensionSet&)")
390 << "Argument or exponent of pow not dimensionless" << endl
391 << abort(FatalError);
398 Foam::dimensionSet Foam::sqr(const dimensionSet& ds)
404 Foam::dimensionSet Foam::pow3(const dimensionSet& ds)
410 Foam::dimensionSet Foam::pow4(const dimensionSet& ds)
416 Foam::dimensionSet Foam::pow5(const dimensionSet& ds)
422 Foam::dimensionSet Foam::pow6(const dimensionSet& ds)
428 Foam::dimensionSet Foam::pow025(const dimensionSet& ds)
430 return sqrt(sqrt(ds));
434 Foam::dimensionSet Foam::sqrt(const dimensionSet& ds)
440 Foam::dimensionSet Foam::magSqr(const dimensionSet& ds)
446 Foam::dimensionSet Foam::mag(const dimensionSet& ds)
452 Foam::dimensionSet Foam::sign(const dimensionSet&)
458 Foam::dimensionSet Foam::pos(const dimensionSet&)
464 Foam::dimensionSet Foam::neg(const dimensionSet&)
470 Foam::dimensionSet Foam::inv(const dimensionSet& ds)
476 Foam::dimensionSet Foam::trans(const dimensionSet& ds)
478 if (dimensionSet::debug && !ds.dimensionless())
480 FatalErrorIn("trans(const dimensionSet&)")
481 << "Argument of trancendental function not dimensionless"
482 << abort(FatalError);
489 Foam::dimensionSet Foam::transform(const dimensionSet& ds)
495 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
497 Foam::dimensionSet Foam::operator-(const dimensionSet& ds)
503 Foam::dimensionSet Foam::operator+
505 const dimensionSet& ds1,
506 const dimensionSet& ds2
509 dimensionSet dimSum(ds1);
511 if (dimensionSet::debug && ds1 != ds2)
514 ("operator+(const dimensionSet&, const dimensionSet&)")
515 << "LHS and RHS of + have different dimensions" << endl
516 << " dimensions : " << ds1 << " + " << ds2 << endl
517 << abort(FatalError);
524 Foam::dimensionSet Foam::operator-
526 const dimensionSet& ds1,
527 const dimensionSet& ds2
530 dimensionSet dimDifference(ds1);
532 if (dimensionSet::debug && ds1 != ds2)
535 ("operator-(const dimensionSet&, const dimensionSet&)")
536 << "LHS and RHS of - have different dimensions" << endl
537 << " dimensions : " << ds1 << " - " << ds2 << endl
538 << abort(FatalError);
541 return dimDifference;
545 Foam::dimensionSet Foam::operator*
547 const dimensionSet& ds1,
548 const dimensionSet& ds2
551 dimensionSet dimProduct(ds1);
553 for (int Dimension=0; Dimension<dimensionSet::nDimensions; Dimension++)
555 dimProduct.exponents_[Dimension] += ds2.exponents_[Dimension];
562 Foam::dimensionSet Foam::operator/
564 const dimensionSet& ds1,
565 const dimensionSet& ds2
568 dimensionSet dimQuotient(ds1);
570 for (int Dimension=0; Dimension<dimensionSet::nDimensions; Dimension++)
572 dimQuotient.exponents_[Dimension] -= ds2.exponents_[Dimension];
579 Foam::dimensionSet Foam::operator&
581 const dimensionSet& ds1,
582 const dimensionSet& ds2
589 Foam::dimensionSet Foam::operator^
591 const dimensionSet& ds1,
592 const dimensionSet& ds2
599 Foam::dimensionSet Foam::operator&&
601 const dimensionSet& ds1,
602 const dimensionSet& ds2
609 // ************************************************************************* //