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(2018-11)
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(2018-11)
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(2018-11)
401  dimensioned(const word& name, const dimensionSet& dims, Istream& is);
402 
403 
404  //- Construct dimensioned from dictionary, with default value.
405  //- FatalIOError if there are excess tokens.
406  static dimensioned<Type> lookupOrDefault
407  (
408  const word& name,
409  const dictionary& dict,
410  const dimensionSet& dims = dimless,
411  const Type& deflt = Type(Zero)
412  )
413  {
414  return getOrDefault(name, dict, dims, deflt);
415  }
416 
417  //- Construct dimensionless from dictionary, with default value.
418  // FatalIOError if it is found and there are excess tokens.
420  (
421  const word& name,
422  const dictionary& dict,
423  const Type& deflt = Type(Zero)
424  )
425  {
426  return getOrDefault(name, dict, deflt);
427  }
428 
429  //- Construct dimensioned from dictionary, with default value.
430  // If the value is not found, it is added into the dictionary.
431  // FatalIOError if it is found and there are excess tokens.
432  static dimensioned<Type> lookupOrAddToDict
433  (
434  const word& name,
435  dictionary& dict,
436  const dimensionSet& dims = dimless,
437  const Type& deflt = Type(Zero)
438  )
439  {
440  return getOrAddToDict(name, dict, dims, deflt);
441  }
442 
443  //- Construct dimensionless from dictionary, with default value.
444  // If the value is not found, it is added into the dictionary.
445  // FatalIOError if it is found and there are excess tokens.
446  static dimensioned<Type> lookupOrAddToDict
447  (
448  const word& name,
449  dictionary& dict,
450  const Type& deflt = Type(Zero)
451  )
452  {
453  return getOrAddToDict(name, dict, deflt);
454  }
455 };
456 
457 
458 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
459 
460 //- Output operator
461 template<class Type>
462 Ostream& operator<<(Ostream& os, const dimensioned<Type>& dt);
463 
464 template<class Type, direction r>
466 pow
467 (
468  const dimensioned<Type>&,
470  = pTraits<typename powProduct<Type, r>::type>::zero
471 );
472 
473 template<class Type>
475 sqr(const dimensioned<Type>&);
476 
477 template<class Type>
479 magSqr(const dimensioned<Type>& dt);
480 
481 template<class Type>
483 mag(const dimensioned<Type>& dt);
484 
485 template<class Type>
486 dimensioned<Type> cmptMultiply
487 (
488  const dimensioned<Type>&,
489  const dimensioned<Type>&
490 );
491 
492 template<class Type>
493 dimensioned<Type> cmptDivide
494 (
495  const dimensioned<Type>&,
496  const dimensioned<Type>&
497 );
498 
499 template<class Type>
500 dimensioned<Type> max(const dimensioned<Type>&, const dimensioned<Type>&);
501 
502 template<class Type>
503 dimensioned<Type> min(const dimensioned<Type>&, const dimensioned<Type>&);
504 
505 template<class Type>
506 dimensioned<Type> lerp
507 (
508  const dimensioned<Type>& a,
509  const dimensioned<Type>& b,
510  const scalar t
511 );
512 
513 template<class Type>
514 bool operator<(const dimensioned<Type>&, const dimensioned<Type>&);
515 
516 template<class Type>
517 bool operator>(const dimensioned<Type>&, const dimensioned<Type>&);
518 
519 template<class Type>
521 
522 template<class Type>
524 
525 template<class Type>
527 
528 template<class Type>
529 dimensioned<Type> operator*
530 (
531  const dimensioned<scalar>&,
533 );
534 
535 template<class Type>
536 dimensioned<Type> operator/
537 (
538  const dimensioned<Type>&,
539  const dimensioned<scalar>&
540 );
541 
542 
543 #define PRODUCT_OPERATOR(product, op, opFunc) \
544  \
545 template<class Type1, class Type2> \
546 dimensioned<typename product<Type1, Type2>::type> \
547 operator op(const dimensioned<Type1>&, const dimensioned<Type2>&); \
548  \
549 template<class Type, class Form, class Cmpt, direction nCmpt> \
550 dimensioned<typename product<Type, Form>::type> \
551 operator op \
552 ( \
553  const dimensioned<Type>&, \
554  const VectorSpace<Form,Cmpt,nCmpt>& \
555 ); \
556  \
557 template<class Type, class Form, class Cmpt, direction nCmpt> \
558 dimensioned<typename product<Form, Type>::type> \
559 operator op \
560 ( \
561  const VectorSpace<Form,Cmpt,nCmpt>&, \
562  const dimensioned<Type>& \
563 );
564 
569 
570 #undef PRODUCT_OPERATOR
571 
572 
573 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
574 
575 } // End namespace Foam
576 
577 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
578 
579 #ifdef NoRepository
580  #include "dimensionedType.C"
581 #endif
582 
583 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
584 
585 #endif
586 
587 // ************************************************************************* //
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:48
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:120
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 > &)
#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))
Construct dimensioned from dictionary, with default value. FatalIOError if there are excess tokens...
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
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:55
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)
#define FOAM_DEPRECATED(since)
Definition: stdFoam.H:47
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))
Construct dimensioned from dictionary, with default value.
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
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:133
readOption
Enumeration defining read preferences.