dimensionSet.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "dimensionSet.H"
30 #include "dimensionedScalar.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(dimensionSet, 1);
37 }
38 
39 const Foam::scalar Foam::dimensionSet::smallExponent = SMALL;
40 
41 
42 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 static inline bool checkDims
48 (
49  const char* what,
50  const dimensionSet& a,
51  const dimensionSet& b
52 )
53 {
54  if (a != b)
55  {
57  << "Different dimensions for '" << what
58  << "'\n dimensions : " << a << " != " << b << nl
59  << abort(FatalError);
60  return false;
61  }
62 
63  return true;
64 }
65 
66 } // End namespace Foam
67 
68 
69 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
70 
72 :
73  exponents_(Zero)
74 {}
75 
76 
78 (
79  const scalar mass,
80  const scalar length,
81  const scalar time,
82  const scalar temperature,
83  const scalar moles,
84  const scalar current,
85  const scalar luminousIntensity
86 )
87 :
88  exponents_()
89 {
90  exponents_[MASS] = mass;
91  exponents_[LENGTH] = length;
92  exponents_[TIME] = time;
93  exponents_[TEMPERATURE] = temperature;
94  exponents_[MOLES] = moles;
95  exponents_[CURRENT] = current;
96  exponents_[LUMINOUS_INTENSITY] = luminousIntensity;
97 }
98 
99 
101 :
102  exponents_(dims)
103 {}
104 
105 
107 :
108  exponents_(ds.exponents_)
109 {}
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
115 {
116  for (const scalar val : exponents_)
117  {
118  // ie, mag(val) > smallExponent
119  if ((val > smallExponent) || (val < -smallExponent))
120  {
121  return false;
122  }
123  }
124 
125  return true;
126 }
127 
128 
131 {
132  return exponents_;
133 }
134 
135 
138 {
139  return exponents_;
140 }
141 
144 {
145  exponents_ = Zero;
146 }
147 
148 
150 {
151  exponents_ = ds.exponents_;
152 }
153 
154 
155 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
157 Foam::scalar Foam::dimensionSet::operator[](const dimensionType type) const
158 {
159  return exponents_[type];
160 }
161 
164 {
165  return exponents_[type];
166 }
167 
169 Foam::scalar Foam::dimensionSet::operator[](const label type) const
170 {
171  return exponents_[type];
172 }
173 
175 Foam::scalar& Foam::dimensionSet::operator[](const label type)
176 {
177  return exponents_[type];
178 }
179 
180 
181 bool Foam::dimensionSet::operator==(const dimensionSet& ds) const
182 {
183  for (int d=0; d<nDimensions; ++d)
184  {
185  if
186  (
187  mag(exponents_[d] - ds.exponents_[d])
188  > smallExponent
189  )
190  {
191  return false;
192  }
193  }
194 
195  return true;
196 }
197 
199 bool Foam::dimensionSet::operator!=(const dimensionSet& ds) const
200 {
201  return !(operator==(ds));
202 }
203 
204 
205 bool Foam::dimensionSet::operator=(const dimensionSet& ds) const
206 {
208  {
209  checkDims("(a = b)", *this, ds);
210  }
211 
212  return true;
213 }
214 
215 
216 bool Foam::dimensionSet::operator+=(const dimensionSet& ds) const
217 {
219  {
220  checkDims("(a += b)", *this, ds);
221  }
222 
223  return true;
224 }
225 
226 
227 bool Foam::dimensionSet::operator-=(const dimensionSet& ds) const
228 {
230  {
231  checkDims("(a -= b)", *this, ds);
232  }
233 
234  return true;
235 }
236 
237 
238 bool Foam::dimensionSet::operator*=(const dimensionSet& ds)
239 {
240  reset((*this)*ds);
241 
242  return true;
243 }
244 
245 
247 {
248  reset((*this)/ds);
250  return true;
251 }
252 
253 
254 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
255 
256 Foam::dimensionSet Foam::min(const dimensionSet& ds1, const dimensionSet& ds2)
257 {
259  {
260  checkDims("min(a, b)", ds1, ds2);
261  }
262 
263  return ds1;
264 }
265 
266 
267 Foam::dimensionSet Foam::max(const dimensionSet& ds1, const dimensionSet& ds2)
268 {
270  {
271  checkDims("max(a, b)", ds1, ds2);
272  }
273 
274  return ds1;
275 }
276 
277 
278 Foam::dimensionSet Foam::clip(const dimensionSet& ds1, const dimensionSet& ds2)
279 {
281  {
282  checkDims("clip(a, b)", ds1, ds2);
283  }
284 
285  return ds1;
286 }
287 
288 
290 (
291  const dimensionSet& ds1,
292  const dimensionSet& ds2
293 )
294 {
295  return ds1*ds2;
296 }
297 
298 
300 (
301  const dimensionSet& ds1,
302  const dimensionSet& ds2
303 )
304 {
305  return ds1/ds2;
306 }
307 
308 
309 Foam::dimensionSet Foam::pow(const dimensionSet& ds, const scalar p)
310 {
311  return dimensionSet
312  (
313  ds[dimensionSet::MASS]*p,
315  ds[dimensionSet::TIME]*p,
320  );
321 }
322 
323 
325 (
326  const dimensionSet& ds,
327  const dimensionedScalar& dS
328 )
329 {
330  if (dimensionSet::checking() && !dS.dimensions().dimensionless())
331  {
333  << "Exponent of pow is not dimensionless" << endl
334  << abort(FatalError);
335  }
336 
337  return pow(ds, dS.value());
338 }
339 
340 
342 (
343  const dimensionedScalar& dS,
344  const dimensionSet& ds
345 )
346 {
347  if
348  (
350  && !dS.dimensions().dimensionless()
351  && !ds.dimensionless()
352  )
353  {
355  << "Argument or exponent of pow not dimensionless" << endl
357  }
358 
359  return ds;
360 }
361 
364 {
365  return pow(ds, 2);
366 }
367 
370 {
371  return pow(ds, 2);
372 }
373 
376 {
377  return pow(ds, 3);
378 }
379 
382 {
383  return pow(ds, 4);
384 }
385 
388 {
389  return pow(ds, 5);
390 }
391 
394 {
395  return pow(ds, 6);
396 }
397 
400 {
401  return pow(ds, 0.25);
402 }
403 
406 {
407  return pow(ds, 0.5);
408 }
409 
412 {
413  return pow(ds, 1.0/3.0);
414 }
415 
418 {
419  return pow(ds, 2);
420 }
421 
424 {
425  return ds;
426 }
427 
430 {
431  return dimless;
432 }
433 
436 {
437  return dimless;
438 }
439 
442 {
443  return dimless;
444 }
445 
448 {
449  return dimless;
450 }
451 
454 {
455  return dimless;
456 }
457 
460 {
461  return ds;
462 }
463 
466 {
467  return ds;
468 }
469 
470 
471 Foam::dimensionSet Foam::inv(const dimensionSet& ds)
472 {
473  return dimensionSet
474  (
475  0.0-ds[dimensionSet::MASS],
476  0.0-ds[dimensionSet::LENGTH],
477  0.0-ds[dimensionSet::TIME],
480  0.0-ds[dimensionSet::CURRENT],
482  );
483 }
484 
485 
486 Foam::dimensionSet Foam::trans(const dimensionSet& ds)
487 {
488  if (dimensionSet::checking() && !ds.dimensionless())
489  {
491  << "Argument of trancendental function not dimensionless" << nl
493  }
494 
495  return ds;
496 }
497 
498 
499 Foam::dimensionSet Foam::atan2(const dimensionSet& ds1, const dimensionSet& ds2)
500 {
502  {
503  checkDims("atan2(a, b)", ds1, ds2);
504  }
505 
506  return dimless;
507 }
508 
509 
510 Foam::dimensionSet Foam::hypot(const dimensionSet& ds1, const dimensionSet& ds2)
511 {
513  {
514  checkDims("hypot(a, b)", ds1, ds2);
515  }
516 
517  return ds1;
518 }
519 
520 
522 (
523  const dimensionSet& ds1,
524  const dimensionSet& ds2
525 )
526 {
528  {
529  checkDims("stabilise(a, b)", ds1, ds2);
530  }
531 
532  return ds1;
533 }
534 
537 {
538  return ds;
539 }
540 
541 
542 Foam::dimensionSet Foam::invTransform(const dimensionSet& ds)
543 {
544  return ds;
545 }
546 
547 
548 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
551 {
552  return inv(ds);
553 }
554 
555 
557 {
558  return ds;
559 }
560 
561 
562 Foam::dimensionSet Foam::operator+
563 (
564  const dimensionSet& ds1,
565  const dimensionSet& ds2
566 )
567 {
569  {
570  checkDims("(a + b)", ds1, ds2);
571  }
572 
573  return ds1;
574 }
575 
576 
577 Foam::dimensionSet Foam::operator-
578 (
579  const dimensionSet& ds1,
580  const dimensionSet& ds2
581 )
582 {
584  {
585  checkDims("(a - b)", ds1, ds2);
586  }
587 
588  return ds1;
589 }
590 
591 
592 Foam::dimensionSet Foam::operator*
593 (
594  const dimensionSet& ds1,
595  const dimensionSet& ds2
596 )
597 {
598  dimensionSet result(ds1);
599 
600  auto rhs = ds2.values().begin();
601 
602  for (scalar& val : result.values())
603  {
604  val += *rhs;
605  ++rhs;
606  }
607 
608  return result;
609 }
610 
611 
612 Foam::dimensionSet Foam::operator/
613 (
614  const dimensionSet& ds1,
615  const dimensionSet& ds2
616 )
617 {
618  dimensionSet result(ds1);
619 
620  auto rhs = ds2.values().begin();
621 
622  for (scalar& val : result.values())
623  {
624  val -= *rhs;
625  ++rhs;
626  }
627 
628  return result;
629 }
630 
631 
632 Foam::dimensionSet Foam::operator&
633 (
634  const dimensionSet& ds1,
635  const dimensionSet& ds2
636 )
637 {
638  return ds1*ds2;
639 }
640 
641 
642 Foam::dimensionSet Foam::operator^
643 (
644  const dimensionSet& ds1,
645  const dimensionSet& ds2
646 )
647 {
648  return ds1*ds2;
649 }
650 
651 
652 Foam::dimensionSet Foam::operator&&
653 (
654  const dimensionSet& ds1,
655  const dimensionSet& ds2
656 )
657 {
658  return ds1*ds2;
659 }
660 
661 
662 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
bool operator/=(const dimensionSet &)
Definition: dimensionSet.C:239
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition: bitSetI.H:755
dimensionSet invTransform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:535
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
dimensionedSymmTensor sqr(const dimensionedVector &dv)
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
bool operator-=(const dimensionSet &) const
Definition: dimensionSet.C:220
dimensionedScalar sqrt(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
dimensionedScalar pow025(const dimensionedScalar &ds)
static const scalar smallExponent
Tolerance for &#39;small&#39; exponents, for near-zero rounding.
Definition: dimensionSet.H:142
bool operator=(const dimensionSet &) const
Definition: dimensionSet.C:198
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
const dimensionSet dimless
Dimensionless.
dimensionedScalar posPart(const dimensionedScalar &ds)
dimensionedScalar neg(const dimensionedScalar &ds)
dimensionedScalar pow5(const dimensionedScalar &ds)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
bool operator*=(const dimensionSet &)
Definition: dimensionSet.C:231
dimensionedScalar pos(const dimensionedScalar &ds)
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:752
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:105
const FixedList< scalar, 7 > & values() const noexcept
Const access to the exponents as a list.
Definition: dimensionSet.C:123
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
dimensionSet clip(const dimensionSet &ds1, const dimensionSet &ds2)
Definition: dimensionSet.C:271
dimensionedScalar cbrt(const dimensionedScalar &ds)
dimensionedScalar neg0(const dimensionedScalar &ds)
dimensionSet()
Default construct (dimensionless).
Definition: dimensionSet.C:64
bool operator+=(const dimensionSet &) const
Definition: dimensionSet.C:209
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
dimensionSet pow2(const dimensionSet &ds)
Definition: dimensionSet.C:362
errorManip< error > abort(error &err)
Definition: errorManip.H:139
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
bool operator!=(const dimensionSet &) const
Definition: dimensionSet.C:192
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
void reset(const dimensionSet &ds)
Copy assign the exponents from the dimensionSet.
Definition: dimensionSet.C:142
const direction noexcept
Definition: Scalar.H:258
dimensionedScalar pos0(const dimensionedScalar &ds)
dimensionedScalar atan2(const dimensionedScalar &x, const dimensionedScalar &y)
defineTypeNameAndDebug(combustionModel, 0)
dimensionSet trans(const dimensionSet &ds)
Check the argument is dimensionless (for transcendental functions)
Definition: dimensionSet.C:479
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
void clear()
Clear exponents - resets to be dimensionless.
Definition: dimensionSet.C:136
dimensionedScalar pow3(const dimensionedScalar &ds)
scalar operator[](const dimensionType) const
Definition: dimensionSet.C:150
static bool checking() noexcept
True if dimension checking is enabled (the usual default)
Definition: dimensionSet.H:241
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
dimensionedScalar pow4(const dimensionedScalar &ds)
static bool checkDims(const char *what, const dimensionSet &a, const dimensionSet &b)
Definition: dimensionSet.C:41
dimensionedScalar pow6(const dimensionedScalar &ds)
dimensionType
Enumeration for the dimension exponents.
Definition: dimensionSet.H:125
volScalarField & p
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:529
bool operator==(const dimensionSet &) const
Definition: dimensionSet.C:174
dimensionedScalar hypot(const dimensionedScalar &x, const dimensionedScalar &y)
bool dimensionless() const
Return true if it is dimensionless.
Definition: dimensionSet.C:107
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
dimensionedScalar negPart(const dimensionedScalar &ds)
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:157