dimensionedType.H
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-2016 OpenFOAM Foundation
9  Copyright (C) 2016-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 Class
28  Foam::dimensioned
29 
30 Description
31  Generic dimensioned Type class
32 
33 SourceFiles
34  dimensionedType.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_dimensionedType_H
39 #define Foam_dimensionedType_H
40 
41 #include "word.H"
42 #include "direction.H"
43 #include "dimensionSet.H"
44 #include "VectorSpace.H"
45 #include "IOobjectOption.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 class one;
54 class zero;
55 class dictionary;
56 class primitiveEntry;
57 
58 template<class Type> class dimensioned;
59 
60 template<class Type>
61 Istream& operator>>(Istream& is, dimensioned<Type>& dt);
62 
63 /*---------------------------------------------------------------------------*\
64  Class dimensioned Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 template<class Type>
68 class dimensioned
69 {
70  // Private Data
71 
72  //- The variable name
73  word name_;
74 
75  //- The dimension set
76  dimensionSet dimensions_;
77 
78  //- The data value
79  Type value_;
80 
81 
82  // Private Member Functions
83 
84  //- Read helper.
85  // Requires a value, optional preceded with name and/or dimensions.
86  // \verbatim
87  // [name] [dims] value
88  // \endverbatim
89  // If the name is present, it is used to rename.
90  // If dimensions are present, they are read.
91  // With checkDims = true, the dimensions read are verified
92  // against the current (expected) dimensions.
93  void initialize(Istream& is, const bool checkDims);
94 
95  //- Find entry and assign to dimensioned Type
96  //- FatalIOError if it is found and the number of tokens is incorrect,
97  //- or it is mandatory and not found.
98  //
99  // Requires a value, optional preceded with name and/or dimensions.
100  // \verbatim
101  // [name] [dims] value
102  // \endverbatim
103  // If the name is present, it is used to rename.
104  // If dimensions are present, they are read.
105  // With checkDims = true, the dimensions read are verified
106  // against the current (expected) dimensions.
107  bool readEntry
108  (
109  const word& key,
110  const dictionary& dict,
112  const bool checkDims = true,
113  enum keyType::option matchOpt = keyType::REGEX
114  );
115 
116 public:
117 
118  //- The underlying data type
119  typedef Type value_type;
120 
121  //- Component type
122  typedef typename pTraits<Type>::cmptType cmptType;
123 
124 
125  // Constructors
126 
127  //- A dimensionless Zero, named "0"
129 
130  //- A dimensioned Zero, named "0"
131  explicit dimensioned(const dimensionSet& dims);
132 
133  //- A dimensioned Zero, named "0"
134  explicit dimensioned(const dimensionSet& dims, const Foam::zero);
135 
136  //- A dimensioned pTraits::one, named "1"
137  explicit dimensioned(const dimensionSet& dims, const Foam::one);
138 
139  //- Implicit construct dimensionless from given value.
140  dimensioned(const Type& val)
141  :
142  name_(::Foam::name(val)),
143  dimensions_(dimless),
144  value_(val)
145  {}
146 
147  //- Construct dimensioned from given value
148  dimensioned(const dimensionSet& dims, const Type& val);
149 
150  //- Construct from components (name, dimensions, value).
152  (
153  const word& name,
154  const dimensionSet& dims,
155  const Type& val
156  );
157 
158  //- Copy construct dimensioned Type with a new name
159  dimensioned(const word& name, const dimensioned<Type>& dt);
160 
161  //- Construct from primitive entry with given name.
162  // The entry may contain optional name and dimensions.
163  // \verbatim
164  // [name] [dims] value
165  // \endverbatim
166  // If the optional name is found, it is used for renaming.
167  // If the optional dimensions are present, they are read and
168  // used without further verification.
169  // If no dimensions are found, the quantity is dimensionless.
170  // Fatal if not primitiveEntry or if number of tokens is incorrect.
171  explicit dimensioned(const primitiveEntry& e);
172 
173  //- Construct from primitive entry with given name and dimensions.
174  // The entry may contain optional name and dimensions.
175  // \verbatim
176  // [name] [dims] value
177  // \endverbatim
178  // If the optional name is found, it is used for renaming.
179  // If the optional dimensions are present, they are read and
180  // verified against the expected dimensions.
181  // Fatal if not primitiveEntry or if number of tokens is incorrect.
182  explicit dimensioned(const primitiveEntry& e, const dimensionSet& dims);
183 
184  //- Construct from dictionary lookup with a given name.
185  // The entry may contain optional name and dimensions.
186  // \verbatim
187  // [name] [dims] value
188  // \endverbatim
189  // If the optional name is found, it is used for renaming.
190  // If the optional dimensions are present, they are read and
191  // used without further verification.
192  // If no dimensions are found, the quantity is dimensionless.
193  dimensioned(const word& name, const dictionary& dict);
194 
195  //- Construct from dictionary lookup with a given name and dimensions.
196  // The entry may contain optional name and dimensions.
197  // \verbatim
198  // [name] [dims] value
199  // \endverbatim
200  // If the optional name is found, it is used for renaming.
201  // If the optional dimensions are present, they are read and
202  // verified against the expected dimensions.
204  (
205  const word& name,
206  const dimensionSet& dims,
207  const dictionary& dict
208  );
209 
210  //- Construct from dictionary lookup with a given name and dimensions.
211  // The entry may contain optional name and dimensions.
212  // \verbatim
213  // [name] [dims] value
214  // \endverbatim
215  // If the optional name is found, it is used for renaming.
216  // If the optional dimensions are present, they are read and
217  // verified against the expected dimensions.
219  (
220  const word& name,
221  const dimensionSet& dims,
222  const dictionary& dict,
223  const word& entryName
224  );
225 
226  //- Construct from components (name, dimensions, value) with
227  //- optional dictionary override.
228  // The entry may contain optional name and dimensions.
229  // \verbatim
230  // [name] [dims] value
231  // \endverbatim
233  (
234  const word& name,
235  const dimensionSet& dims,
236  const Type& val,
237  const dictionary& dict
238  );
239 
240 
241  // Static Member Functions
242 
243  //- Construct dimensioned from dictionary, with default value.
244  // FatalIOError if there are excess tokens.
246  (
247  const word& name,
248  const dictionary& dict,
249  const dimensionSet& dims = dimless,
250  const Type& deflt = Type(Zero)
251  );
252 
253  //- Construct dimensionless from dictionary, with default value.
254  // FatalIOError if it is found and there are excess tokens.
256  (
257  const word& name,
258  const dictionary& dict,
259  const Type& deflt = Type(Zero)
260  );
261 
262  //- Construct dimensioned from dictionary, with default value.
263  // If the value is not found, it is added into the dictionary.
264  // FatalIOError if it is found and there are excess tokens.
266  (
267  const word& name,
268  dictionary& dict,
269  const dimensionSet& dims = dimless,
270  const Type& deflt = Type(Zero)
271  );
272 
273  //- Construct dimensionless from dictionary, with default value.
274  // If the value is not found, it is added into the dictionary.
275  // FatalIOError if it is found and there are excess tokens.
277  (
278  const word& name,
279  dictionary& dict,
280  const Type& deflt = Type(Zero)
281  );
282 
283 
284  // Member Functions
285 
286  //- Return const reference to name.
287  const word& name() const noexcept { return name_; }
288 
289  //- Return non-const reference to name.
290  word& name() noexcept { return name_; }
291 
292  //- Return const reference to dimensions.
293  const dimensionSet& dimensions() const noexcept { return dimensions_; }
294 
295  //- Return non-const reference to dimensions.
296  dimensionSet& dimensions() noexcept { return dimensions_; }
297 
298  //- Return const reference to value.
299  const Type& value() const noexcept { return value_; }
300 
301  //- Return non-const reference to value.
302  Type& value() noexcept { return value_; }
303 
304  //- Return a component as a dimensioned<cmptType>
305  dimensioned<cmptType> component(const direction d) const;
306 
307  //- Return a component with a dimensioned<cmptType>
308  void replace(const direction d, const dimensioned<cmptType>& dc);
309 
310  //- Return transpose.
311  dimensioned<Type> T() const;
312 
313  //- Update the value of dimensioned<Type>,
314  //- lookup in dictionary with the name().
315  bool read(const dictionary& dict);
316 
317  //- Update the value of dimensioned<Type> if found in the dictionary,
318  //- lookup in dictionary with the name().
319  bool readIfPresent(const dictionary& dict);
320 
321  //- Update the value of dimensioned<Type>,
322  //- using an alternative entry name
323  bool read(const word& entryName, const dictionary& dict);
324 
325  //- Update the value of dimensioned<Type> if found in the dictionary,
326  //- using an alternative entry name
327  bool readIfPresent(const word& entryName, const dictionary& dict);
328 
329 
330  // IO
331 
332  //- Read (name, dimensions, value) from stream,
333  //- using units from system table.
334  // Optionally skip reading the name
335  Istream& read(Istream& is, const bool readName = true);
336 
337  //- Read (name, dimensions, value) from stream,
338  //- using units from dictionary
339  Istream& read(Istream& is, const dictionary& readSet);
340 
341  //- Read (name, dimensions, value) from stream,
342  //- using units from table
343  Istream& read(Istream& is, const HashTable<dimensionedScalar>& readSet);
344 
345  //- Write as a dictionary entry with keyword.
346  // The name is not written when it is identical to keyword.
347  // The dimensions are always written.
348  void writeEntry(const word& keyword, Ostream& os) const;
349 
350 
351  // Member Operators
352 
353  //- Return a component as a dimensioned<cmptType>
355 
356  void operator+=(const dimensioned<Type>& dt);
357  void operator-=(const dimensioned<Type>& dt);
358  void operator*=(const scalar s);
359  void operator/=(const scalar s);
360 
362  // IOstream Operators
363 
364  //- Read from stream. The name and dimensions are optional.
365  // If the optional dimensions are present,
366  // they are used without further verification.
367  friend Istream& operator>> <Type>
368  (
369  Istream& is,
371  );
372 
373 
374  // Housekeeping
375 
376  //- Deprecated(2018-11) Construct from Istream
377  //- (expects name, dimensions, value)
378  // \deprecated(2018-11) - should generally use construct from
379  // dictionary or primitiveEntry instead
380  // (additional checks on the input stream).
381  FOAM_DEPRECATED_FOR(2018-11, "construct from dictionary or entry")
382  explicit dimensioned(Istream& is);
383 
384  //- Deprecated(2018-11) Construct from Istream with given name
385  //- (expects dimensions, value)
386  // \deprecated(2018-11) - should generally use construct from
387  // dictionary or primitiveEntry instead
388  // (additional checks on the input stream).
389  FOAM_DEPRECATED_FOR(2018-11, "construct from dictionary or entry")
390  dimensioned(const word& name, Istream& is);
391 
392  //- Deprecated(2018-11) Construct from Istream with given name
393  //- and expected dimensions.
394  // Expects value, but supports optional name and dimensions.
395  // If the optional dimensions are present, they are read and
396  // verified against the expected dimensions.
397  // \deprecated(2018-11) - should generally use construct from
398  // dictionary or primitiveEntry instead
399  // (additional checks on the input stream).
400  FOAM_DEPRECATED_FOR(2018-11, "construct from dictionary or entry")
401  dimensioned(const word& name, const dimensionSet& dims, Istream& is);
402 
403  //- Same as getOrDefault()
405  static dimensioned<Type> lookupOrDefault
406  (
407  const word& name,
408  const dictionary& dict,
409  const dimensionSet& dims = dimless,
410  const Type& deflt = Type(Zero)
411  )
412  {
413  return getOrDefault(name, dict, dims, deflt);
414  }
415 
416  //- Same as getOrDefault()
417  FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault()")
418  static dimensioned<Type> lookupOrDefault
419  (
420  const word& name,
421  const dictionary& dict,
422  const Type& deflt = Type(Zero)
423  )
424  {
425  return getOrDefault(name, dict, deflt);
426  }
427 
428  //- Same as getOrAddToDict()
429  FOAM_DEPRECATED_STRICT(2019-06, "getOrAddToDict()")
430  static dimensioned<Type> lookupOrAddToDict
431  (
432  const word& name,
433  dictionary& dict,
434  const dimensionSet& dims = dimless,
435  const Type& deflt = Type(Zero)
436  )
437  {
438  return getOrAddToDict(name, dict, dims, deflt);
439  }
440 
441  //- Same as getOrAddToDict()
442  FOAM_DEPRECATED_STRICT(2019-06, "getOrAddToDict()")
443  static dimensioned<Type> lookupOrAddToDict
444  (
445  const word& name,
446  dictionary& dict,
447  const Type& deflt = Type(Zero)
448  )
449  {
450  return getOrAddToDict(name, dict, deflt);
451  }
452 };
453 
454 
455 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
456 
457 //- Output operator
458 template<class Type>
459 Ostream& operator<<(Ostream& os, const dimensioned<Type>& dt);
460 
461 template<class Type, direction r>
463 pow
464 (
465  const dimensioned<Type>&,
467  = pTraits<typename powProduct<Type, r>::type>::zero
468 );
469 
470 template<class Type>
472 sqr(const dimensioned<Type>&);
473 
474 template<class Type>
476 magSqr(const dimensioned<Type>& dt);
477 
478 template<class Type>
480 mag(const dimensioned<Type>& dt);
481 
482 template<class Type>
483 dimensioned<Type> cmptMultiply
484 (
485  const dimensioned<Type>&,
486  const dimensioned<Type>&
487 );
488 
489 template<class Type>
490 dimensioned<Type> cmptDivide
491 (
492  const dimensioned<Type>&,
493  const dimensioned<Type>&
494 );
495 
496 template<class Type>
497 dimensioned<Type> max(const dimensioned<Type>&, const dimensioned<Type>&);
498 
499 template<class Type>
500 dimensioned<Type> min(const dimensioned<Type>&, const dimensioned<Type>&);
501 
502 template<class Type>
503 dimensioned<Type> lerp
504 (
505  const dimensioned<Type>& a,
506  const dimensioned<Type>& b,
507  const scalar t
508 );
509 
510 template<class Type>
511 bool operator<(const dimensioned<Type>&, const dimensioned<Type>&);
512 
513 template<class Type>
514 bool operator>(const dimensioned<Type>&, const dimensioned<Type>&);
516 template<class Type>
518 
519 template<class Type>
521 
522 template<class Type>
524 
525 template<class Type>
526 dimensioned<Type> operator*
527 (
528  const dimensioned<scalar>&,
529  const dimensioned<Type>&
530 );
531 
532 template<class Type>
533 dimensioned<Type> operator/
534 (
535  const dimensioned<Type>&,
536  const dimensioned<scalar>&
537 );
538 
539 
540 #define PRODUCT_OPERATOR(product, op, opFunc) \
541  \
542 template<class Type1, class Type2> \
543 dimensioned<typename product<Type1, Type2>::type> \
544 operator op(const dimensioned<Type1>&, const dimensioned<Type2>&); \
545  \
546 template<class Type, class Form, class Cmpt, direction nCmpt> \
547 dimensioned<typename product<Type, Form>::type> \
548 operator op \
549 ( \
550  const dimensioned<Type>&, \
551  const VectorSpace<Form,Cmpt,nCmpt>& \
552 ); \
553  \
554 template<class Type, class Form, class Cmpt, direction nCmpt> \
555 dimensioned<typename product<Form, Type>::type> \
556 operator op \
557 ( \
558  const VectorSpace<Form,Cmpt,nCmpt>&, \
559  const dimensioned<Type>& \
560 );
561 
566 
567 #undef PRODUCT_OPERATOR
568 
569 
570 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
571 
572 } // End namespace Foam
573 
574 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
575 
576 #ifdef NoRepository
577  #include "dimensionedType.C"
578 #endif
579 
580 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
581 
582 #endif
583 
584 // ************************************************************************* //
dimensioned< cmptType > component(const direction d) const
Return a component as a dimensioned<cmptType>
const Type & value() const noexcept
Return const reference to value.
dictionary dict
type
Types of root.
Definition: Roots.H:52
uint8_t direction
Definition: direction.H:46
static dimensioned< Type > getOrDefault(const word &name, const dictionary &dict, const dimensionSet &dims=dimless, const Type &deflt=Type(Zero))
Construct dimensioned from dictionary, with default value.
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
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)
bool readIfPresent(const dictionary &dict)
Update the value of dimensioned<Type> if found in the dictionary, lookup in dictionary with the name(...
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
void cross(FieldField< Field1, typename crossProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Generic dimensioned Type class.
const dimensionSet dimless
Dimensionless.
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
void dotdot(FieldField< Field1, typename scalarProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
void outer(FieldField< Field1, typename outerProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
void operator-=(const dimensioned< Type > &dt)
void dot(FieldField< Field1, typename innerProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
#define PRODUCT_OPERATOR(product, op, opFunc)
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read...
static dimensioned< Type > lookupOrDefault(const word &name, const dictionary &dict, const dimensionSet &dims=dimless, const Type &deflt=Type(Zero))
Same as getOrDefault()
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:105
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
pTraits< Type >::cmptType cmptType
Component type.
void writeEntry(const word &keyword, Ostream &os) const
Write as a dictionary entry with keyword.
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
Type value_type
The underlying data type.
const dimensionSet & dimensions() const noexcept
Return const reference to dimensions.
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:55
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
bool read(const dictionary &dict)
Update the value of dimensioned<Type>, lookup in dictionary with the name().
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
dimensioned()
A dimensionless Zero, named "0".
OBJstream os(runTime.globalPath()/outputName)
dimensioned< Type > T() const
Return transpose.
void operator*=(const scalar s)
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
static dimensioned< Type > getOrAddToDict(const word &name, dictionary &dict, const dimensionSet &dims=dimless, const Type &deflt=Type(Zero))
Construct dimensioned from dictionary, with default value.
const word & name() const noexcept
Return const reference to name.
void operator+=(const dimensioned< Type > &dt)
symmTypeOfRank< typename pTraits< arg1 >::cmptType, arg2 *direction(pTraits< arg1 >::rank) >::type type
Definition: products.H:176
dimensioned< cmptType > operator[](const direction d) const
Return a component as a dimensioned<cmptType>
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Direction is an 8-bit unsigned integer type used to represent Cartesian directions, components etc.
void operator/=(const scalar s)
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
static bool checkDims(const char *what, const dimensionSet &a, const dimensionSet &b)
Definition: dimensionSet.C:41
option
Enumeration for the data type and search/match modes (bitmask)
Definition: keyType.H:79
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
void replace(const direction d, const dimensioned< cmptType > &dc)
Return a component with a dimensioned<cmptType>
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Regular expression.
Definition: keyType.H:83
static dimensioned< Type > lookupOrAddToDict(const word &name, dictionary &dict, const dimensionSet &dims=dimless, const Type &deflt=Type(Zero))
Same as getOrAddToDict()
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:56
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127
readOption
Enumeration defining read preferences.