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-------------------------------------------------------------------------------
11License
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
34namespace Foam
35{
37}
38
39const Foam::scalar Foam::dimensionSet::smallExponent = SMALL;
40
41
42// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
43
44namespace Foam
45{
46
47static 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
142
144{
145 exponents_ = Zero;
146}
147
148
150{
151 exponents_ = ds.exponents_;
152}
153
154
155// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
156
158{
159 return exponents_[type];
160}
161
162
164{
165 return exponents_[type];
166}
167
168
169Foam::scalar Foam::dimensionSet::operator[](const label type) const
170{
171 return exponents_[type];
172}
173
174
175Foam::scalar& Foam::dimensionSet::operator[](const label type)
176{
177 return exponents_[type];
178}
179
180
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
198
200{
201 return !(operator==(ds));
202}
203
204
206{
208 {
209 checkDims("(a = b)", *this, ds);
210 }
211
212 return true;
213}
214
215
217{
219 {
220 checkDims("(a += b)", *this, ds);
221 }
222
223 return true;
224}
225
226
228{
230 {
231 checkDims("(a -= b)", *this, ds);
232 }
233
234 return true;
235}
236
237
239{
240 reset((*this)*ds);
241
242 return true;
243}
244
245
247{
248 reset((*this)/ds);
249
250 return true;
251}
252
253
254// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
255
257{
259 {
260 checkDims("min(a, b)", ds1, ds2);
261 }
262
263 return ds1;
264}
265
266
268{
270 {
271 checkDims("max(a, b)", ds1, ds2);
272 }
273
274 return ds1;
275}
276
277
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
310{
311 return dimensionSet
312 (
320 );
321}
322
323
325(
326 const dimensionSet& ds,
327 const dimensionedScalar& dS
328)
329{
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
356 << abort(FatalError);
357 }
358
359 return ds;
360}
361
362
364{
365 return pow(ds, 2);
366}
367
368
370{
371 return pow(ds, 2);
372}
373
374
376{
377 return pow(ds, 3);
378}
379
380
382{
383 return pow(ds, 4);
384}
385
386
388{
389 return pow(ds, 5);
390}
391
392
394{
395 return pow(ds, 6);
396}
397
398
400{
401 return pow(ds, 0.25);
402}
403
404
406{
407 return pow(ds, 0.5);
408}
409
410
412{
413 return pow(ds, 1.0/3.0);
414}
415
416
418{
419 return pow(ds, 2);
420}
421
422
424{
425 return ds;
426}
427
428
430{
431 return dimless;
432}
433
434
436{
437 return dimless;
438}
439
440
442{
443 return dimless;
444}
445
446
448{
449 return dimless;
450}
451
452
454{
455 return dimless;
456}
457
458
460{
461 return ds;
462}
463
464
466{
467 return ds;
468}
469
470
472{
473 return dimensionSet
474 (
475 0.0-ds[dimensionSet::MASS],
476 0.0-ds[dimensionSet::LENGTH],
477 0.0-ds[dimensionSet::TIME],
479 0.0-ds[dimensionSet::MOLES],
480 0.0-ds[dimensionSet::CURRENT],
482 );
483}
484
485
487{
489 {
491 << "Argument of trancendental function not dimensionless" << nl
492 << abort(FatalError);
493 }
494
495 return ds;
496}
497
498
500{
502 {
503 checkDims("atan2(a, b)", ds1, ds2);
504 }
505
506 return dimless;
507}
508
509
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
535
537{
538 return ds;
539}
540
541
543{
544 return ds;
545}
546
547
548// * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
549
551{
552 return inv(ds);
553}
554
555
557{
558 return ds;
559}
560
561
562Foam::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
577Foam::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
592Foam::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
612Foam::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
632Foam::dimensionSet Foam::operator&
633(
634 const dimensionSet& ds1,
635 const dimensionSet& ds2
636)
637{
638 return ds1*ds2;
639}
640
641
642Foam::dimensionSet Foam::operator^
643(
644 const dimensionSet& ds1,
645 const dimensionSet& ds2
646)
647{
648 return ds1*ds2;
649}
650
651
652Foam::dimensionSet Foam::operator&&
653(
654 const dimensionSet& ds1,
655 const dimensionSet& ds2
656)
657{
658 return ds1*ds2;
659}
660
661
662// ************************************************************************* //
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:109
bool operator+=(const dimensionSet &) const
Definition: dimensionSet.C:216
dimensionSet()
Default construct (dimensionless).
Definition: dimensionSet.C:71
static bool checking() noexcept
True if dimension checking is enabled (the usual default)
Definition: dimensionSet.H:229
bool operator/=(const dimensionSet &)
Definition: dimensionSet.C:246
bool operator-=(const dimensionSet &) const
Definition: dimensionSet.C:227
scalar operator[](const dimensionType) const
Definition: dimensionSet.C:157
const FixedList< scalar, 7 > & values() const noexcept
Const access to the exponents as a list.
Definition: dimensionSet.C:130
static const scalar smallExponent
Tolerance for 'small' exponents, for near-zero rounding.
Definition: dimensionSet.H:137
bool dimensionless() const
Return true if it is dimensionless.
Definition: dimensionSet.C:114
void clear()
Clear exponents - resets to be dimensionless.
Definition: dimensionSet.C:143
dimensionType
Enumeration for the dimension exponents.
Definition: dimensionSet.H:123
@ LUMINOUS_INTENSITY
Candela cd.
Definition: dimensionSet.H:130
@ MASS
kilogram kg
Definition: dimensionSet.H:124
@ CURRENT
Ampere A.
Definition: dimensionSet.H:129
@ TEMPERATURE
Kelvin K.
Definition: dimensionSet.H:127
bool operator*=(const dimensionSet &)
Definition: dimensionSet.C:238
bool operator=(const dimensionSet &) const
Definition: dimensionSet.C:205
const dimensionSet & dimensions() const
Return const reference to dimensions.
const Type & value() const
Return const reference to value.
void reset()
Reset to defaults.
friend bool operator!=(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:106
friend bool operator==(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:97
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
volScalarField & p
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Namespace for OpenFOAM.
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition: bitSetI.H:762
dimensionedScalar pow6(const dimensionedScalar &ds)
dimensionSet invTransform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:542
dimensionedScalar pos(const dimensionedScalar &ds)
dimensionedScalar pow5(const dimensionedScalar &ds)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dimensionSet clip(const dimensionSet &ds1, const dimensionSet &ds2)
Definition: dimensionSet.C:278
const dimensionSet dimless
Dimensionless.
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:536
dimensionedScalar pos0(const dimensionedScalar &ds)
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedSymmTensor sqr(const dimensionedVector &dv)
dimensionedScalar pow3(const dimensionedScalar &ds)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
dimensionedScalar atan2(const dimensionedScalar &x, const dimensionedScalar &y)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
dimensionedScalar negPart(const dimensionedScalar &ds)
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
dimensionedScalar sqrt(const dimensionedScalar &ds)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
static bool checkDims(const char *what, const dimensionSet &a, const dimensionSet &b)
Definition: dimensionSet.C:48
dimensionedScalar hypot(const dimensionedScalar &x, const dimensionedScalar &y)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
dimensionedScalar pow4(const dimensionedScalar &ds)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
dimensionSet trans(const dimensionSet &ds)
Check the argument is dimensionless (for transcendental functions)
Definition: dimensionSet.C:486
dimensionedScalar neg(const dimensionedScalar &ds)
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
const direction noexcept
Definition: Scalar.H:223
error FatalError
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
dimensionedScalar neg0(const dimensionedScalar &ds)
dimensionedScalar cbrt(const dimensionedScalar &ds)
dimensionSet pow2(const dimensionSet &ds)
Definition: dimensionSet.C:369
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
dimensionedScalar posPart(const dimensionedScalar &ds)
dimensionedScalar pow025(const dimensionedScalar &ds)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
volScalarField & b
Definition: createFields.H:27