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