dimensionedType.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "dimensionedType.H"
27 #include "pTraits.H"
28 #include "dictionary.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  const word& name,
36  const unitConversion& defaultUnits,
37  Istream& is
38 )
39 {
40  token nextToken(is);
41 
42  // Set the name using the argument, if specified, or read it from the
43  // stream if it is present. If neither are, then it will be set to a word
44  // representation of the value lower down.
45  if (!name.empty())
46  {
47  name_ = name;
48  }
49  else if (nextToken.isWord())
50  {
51  name_ = nextToken.wordToken();
52  }
53  else
54  {
55  name_ = word::null;
56  }
57 
58  // Put the token back if it wasn't the name
59  if (!nextToken.isWord())
60  {
61  is.putBack(nextToken);
62  }
63 
64  // Read the units if they are before the value
65  unitConversion units(defaultUnits);
66  const bool haveUnits = units.readIfPresent(is);
67 
68  // Read the value
69  value_ = pTraits<Type>(is);
70 
71  // Read the units if they are after the value
72  if (!haveUnits && !is.eof())
73  {
74  units.readIfPresent(is);
75  }
76 
77  // Set the name
78  if (name_.empty())
79  {
80  name_ = Foam::name(value_);
81  }
82 
83  // Set the dimensions
84  dimensions_.reset(units.dimensions());
85 
86  // Modify the value by the unit conversion
87  units.makeStandard(value_);
88 }
89 
90 
91 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
92 
93 template<class Type>
95 :
96  name_("NaN"),
97  dimensions_(dimless),
98  value_(pTraits<Type>::nan)
99 {}
100 
101 
102 template<class Type>
104 (
105  const word& name,
106  const dimensionSet& dims,
107  const Type& t
108 )
109 :
110  name_(name),
111  dimensions_(dims),
112  value_(t)
113 {}
114 
115 
116 template<class Type>
118 :
119  name_(::Foam::name(t)),
120  dimensions_(dims),
121  value_(t)
122 {}
123 
124 
125 template<class Type>
127 :
128  name_(::Foam::name(t)),
129  dimensions_(dimless),
130  value_(t)
131 {}
132 
133 
134 template<class Type>
136 (
137  const word& name,
139 )
140 :
141  name_(name),
142  dimensions_(dt.dimensions_),
143  value_(dt.value_)
144 {}
145 
146 
147 template<class Type>
149 :
150  dimensions_(dimless)
151 {
152  initialise(word::null, unitAny, is);
153 }
154 
155 
156 template<class Type>
158 :
159  name_(name),
160  dimensions_(dimless)
161 {
162  initialise(name, unitAny, is);
163 }
164 
165 
166 template<class Type>
168 (
169  const word& name,
170  const dimensionSet& dims,
171  Istream& is
172 )
173 :
174  name_(name),
175  dimensions_(dims),
176  value_(Zero)
177 {
178  initialise(name, dims, is);
179 }
180 
181 
182 template<class Type>
184 (
185  const word& name,
186  const dimensionSet& dims,
187  const dictionary& dict
188 )
189 :
190  name_(name),
191  dimensions_(dims),
192  value_(Zero)
193 {
194  initialise(name, dims, dict.lookup(name));
195 }
196 
197 
198 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
199 
200 template<class Type>
202 (
203  const word& name,
204  const dictionary& dict,
205  const dimensionSet& dims,
206  const Type& defaultValue
207 )
208 {
209  if (dict.found(name))
210  {
211  return dimensioned<Type>(name, dims, dict.lookup(name));
212  }
213  else
214  {
215  return dimensioned<Type>(name, dims, defaultValue);
216  }
217 }
218 
219 
220 template<class Type>
222 (
223  const word& name,
224  const dictionary& dict,
225  const Type& defaultValue
226 )
227 {
228  return lookupOrDefault(name, dict, dimless, defaultValue);
229 }
230 
231 
232 template<class Type>
234 (
235  const word& name,
236  dictionary& dict,
237  const dimensionSet& dims,
238  const Type& defaultValue
239 )
240 {
241  Type value = dict.lookupOrAddDefault<Type>(name, defaultValue);
242  return dimensioned<Type>(name, dims, value);
243 }
244 
245 
246 template<class Type>
248 (
249  const word& name,
250  dictionary& dict,
251  const Type& defaultValue
252 )
253 {
254  return lookupOrAddToDict(name, dict, dimless, defaultValue);
255 }
256 
257 
258 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
259 
260 template<class Type>
262 {
263  return name_;
264 }
265 
266 template<class Type>
268 {
269  return name_;
270 }
271 
272 
273 template<class Type>
275 {
276  return dimensions_;
277 }
278 
279 template<class Type>
281 {
282  return dimensions_;
283 }
284 
285 
286 template<class Type>
288 {
289  return value_;
290 }
291 
292 template<class Type>
294 {
295  return value_;
296 }
297 
298 
299 template<class Type>
302 (
303  const direction d
304 ) const
305 {
306  return dimensioned<cmptType>
307  (
308  name_ + ".component(" + Foam::name(d) + ')',
309  dimensions_,
310  value_.component(d)
311  );
312 }
313 
314 
315 template<class Type>
317 (
318  const direction d,
319  const dimensioned<typename dimensioned<Type>::cmptType>& dc
320 )
321 {
322  dimensions_ = dc.dimensions();
323  value_.replace(d, dc.value());
324 }
325 
326 
327 template<class Type>
329 {
330  initialise(name_, dimensions_, dict.lookup(name_));
331 }
332 
333 
334 template<class Type>
336 {
337  const entry* entryPtr = dict.lookupEntryPtr(name_, false, true);
338 
339  if (entryPtr)
340  {
341  initialise(name_, dimensions_, entryPtr->stream());
342  return true;
343  }
344  else
345  {
347  {
349  << "Optional entry '" << name_ << "' is not present,"
350  << " the default value '" << *this << "' will be used."
351  << endl;
352  }
353 
354  return false;
355  }
356 }
357 
358 
359 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
360 
361 template<class Type>
364 {
365  return component(d);
366 }
367 
368 
369 template<class Type>
371 {
372  dimensions_ += dt.dimensions_;
373  value_ += dt.value_;
374 }
375 
376 
377 template<class Type>
379 {
380  dimensions_ -= dt.dimensions_;
381  value_ -= dt.value_;
382 }
383 
384 
385 template<class Type>
387 {
388  value_ *= s;
389 }
390 
391 
392 template<class Type>
394 {
395  value_ /= s;
396 }
397 
398 
399 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
400 
401 template<class Type, Foam::direction r>
404 {
406  (
407  "pow(" + dt.name() + ',' + name(r) + ')',
408  pow(dt.dimensions(), r),
409  pow(dt.value(), 2)
410  );
411 }
412 
413 
414 template<class Type>
416 Foam::sqr(const dimensioned<Type>& dt)
417 {
419  (
420  "sqr(" + dt.name() + ')',
421  sqr(dt.dimensions()),
422  sqr(dt.value())
423  );
424 }
425 
426 
427 template<class Type>
428 Foam::dimensioned<Foam::scalar> Foam::magSqr(const dimensioned<Type>& dt)
429 {
430  return dimensioned<scalar>
431  (
432  "magSqr(" + dt.name() + ')',
433  magSqr(dt.dimensions()),
434  magSqr(dt.value())
435  );
436 }
437 
438 
439 template<class Type>
440 Foam::dimensioned<Foam::scalar> Foam::mag(const dimensioned<Type>& dt)
441 {
442  return dimensioned<scalar>
443  (
444  "mag(" + dt.name() + ')',
445  dt.dimensions(),
446  mag(dt.value())
447  );
448 }
449 
450 
451 template<class Type>
453 (
454  const dimensioned<Type>& dt1,
455  const dimensioned<Type>& dt2
456 )
457 {
458  return dimensioned<Type>
459  (
460  "cmptMultiply(" + dt1.name() + ',' + dt2.name() + ')',
461  cmptMultiply(dt1.dimensions(), dt2.dimensions()),
462  cmptMultiply(dt1.value(), dt2.value())
463  );
464 }
465 
466 
467 template<class Type>
469 (
470  const dimensioned<Type>& dt1,
471  const dimensioned<Type>& dt2
472 )
473 {
474  return dimensioned<Type>
475  (
476  "cmptDivide(" + dt1.name() + ',' + dt2.name() + ')',
477  cmptDivide(dt1.dimensions(), dt2.dimensions()),
478  cmptDivide(dt1.value(), dt2.value())
479  );
480 }
481 
482 
483 template<class Type>
485 (
486  const dimensioned<Type>& dt1,
487  const dimensioned<Type>& dt2
488 )
489 {
490  if (dt1.dimensions() != dt2.dimensions())
491  {
493  << "dimensions of arguments are not equal"
494  << abort(FatalError);
495  }
496 
497  return dimensioned<Type>
498  (
499  "max(" + dt1.name() + ',' + dt2.name() + ')',
500  dt1.dimensions(),
501  max(dt1.value(), dt2.value())
502  );
503 }
504 
505 
506 template<class Type>
508 (
509  const dimensioned<Type>& dt1,
510  const dimensioned<Type>& dt2
511 )
512 {
513  if (dt1.dimensions() != dt2.dimensions())
514  {
516  << "dimensions of arguments are not equal"
517  << abort(FatalError);
518  }
519 
520  return dimensioned<Type>
521  (
522  "min(" + dt1.name() + ',' + dt2.name() + ')',
523  dt1.dimensions(),
524  min(dt1.value(), dt2.value())
525  );
526 }
527 
528 
529 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
530 
531 template<class Type>
533 {
534  os << dt;
535 }
536 
537 
538 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
539 
540 template<class Type>
541 Foam::Istream& Foam::operator>>(Istream& is, dimensioned<Type>& dt)
542 {
543  dt.initialise(word::null, unitAny, is);
544 
545  // Check state of Istream
546  is.check("Istream& operator>>(Istream&, dimensioned<Type>&)");
547 
548  return is;
549 }
550 
551 
552 template<class Type>
553 Foam::Ostream& Foam::operator<<(Ostream& os, const dimensioned<Type>& dt)
554 {
555  // Write the name
556  os << dt.name() << token::SPACE;
557 
558  // Write the dimensions
559  dt.dimensions().write(os);
560 
561  os << token::SPACE;
562 
563  // Write the value
564  os << dt.value();
565 
566  // Check state of Ostream
567  os.check("Ostream& operator<<(Ostream&, const dimensioned<Type>&)");
568 
569  return os;
570 }
571 
572 
573 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
574 
575 template<class Type>
576 bool Foam::operator>
577 (
578  const dimensioned<Type>& dt1,
579  const dimensioned<Type>& dt2
580 )
581 {
582  return dt1.value() > dt2.value();
583 }
584 
585 
586 template<class Type>
587 bool Foam::operator<
588 (
589  const dimensioned<Type>& dt1,
590  const dimensioned<Type>& dt2
591 )
592 {
593  return dt1.value() < dt2.value();
594 }
595 
596 
597 template<class Type>
598 Foam::dimensioned<Type> Foam::operator+
599 (
600  const dimensioned<Type>& dt1,
601  const dimensioned<Type>& dt2
602 )
603 {
604  return dimensioned<Type>
605  (
606  '(' + dt1.name() + '+' + dt2.name() + ')',
607  dt1.dimensions() + dt2.dimensions(),
608  dt1.value() + dt2.value()
609  );
610 }
611 
612 
613 template<class Type>
614 Foam::dimensioned<Type> Foam::operator-(const dimensioned<Type>& dt)
615 {
616  return dimensioned<Type>
617  (
618  '-' + dt.name(),
619  dt.dimensions(),
620  -dt.value()
621  );
622 }
623 
624 
625 template<class Type>
626 Foam::dimensioned<Type> Foam::operator-
627 (
628  const dimensioned<Type>& dt1,
629  const dimensioned<Type>& dt2
630 )
631 {
632  return dimensioned<Type>
633  (
634  '(' + dt1.name() + '-' + dt2.name() + ')',
635  dt1.dimensions() - dt2.dimensions(),
636  dt1.value() - dt2.value()
637  );
638 }
639 
640 
641 template<class Type>
642 Foam::dimensioned<Type> Foam::operator*
643 (
644  const dimensioned<scalar>& ds,
645  const dimensioned<Type>& dt
646 )
647 {
648  return dimensioned<Type>
649  (
650  '(' + ds.name() + '*' + dt.name() + ')',
651  ds.dimensions() * dt.dimensions(),
652  ds.value() * dt.value()
653  );
654 }
655 
656 
657 template<class Type>
658 Foam::dimensioned<Type> Foam::operator/
659 (
660  const dimensioned<Type>& dt,
661  const dimensioned<scalar>& ds
662 )
663 {
664  return dimensioned<Type>
665  (
666  '(' + dt.name() + '|' + ds.name() + ')',
667  dt.dimensions()/ds.dimensions(),
668  dt.value()/ds.value()
669  );
670 }
671 
672 
673 #define PRODUCT_OPERATOR(product, op, opFunc) \
674  \
675 template<class Type1, class Type2> \
676 Foam::dimensioned<typename Foam::product<Type1, Type2>::type> \
677 Foam::operator op \
678 ( \
679  const dimensioned<Type1>& dt1, \
680  const dimensioned<Type2>& dt2 \
681 ) \
682 { \
683  return dimensioned<typename product<Type1, Type2>::type> \
684  ( \
685  '(' + dt1.name() + #op + dt2.name() + ')', \
686  dt1.dimensions() op dt2.dimensions(), \
687  dt1.value() op dt2.value() \
688  ); \
689 } \
690  \
691 template<class Type, class Form, class Cmpt, Foam::direction nCmpt> \
692 Foam::dimensioned<typename Foam::product<Type, Form>::type> \
693 Foam::operator op \
694 ( \
695  const dimensioned<Type>& dt1, \
696  const VectorSpace<Form,Cmpt,nCmpt>& t2 \
697 ) \
698 { \
699  return dimensioned<typename product<Type, Form>::type> \
700  ( \
701  '(' + dt1.name() + #op + name(t2) + ')', \
702  dt1.dimensions(), \
703  dt1.value() op static_cast<const Form&>(t2) \
704  ); \
705 } \
706  \
707 template<class Type, class Form, class Cmpt, Foam::direction nCmpt> \
708 Foam::dimensioned<typename Foam::product<Form, Type>::type> \
709 Foam::operator op \
710 ( \
711  const VectorSpace<Form,Cmpt,nCmpt>& t1, \
712  const dimensioned<Type>& dt2 \
713 ) \
714 { \
715  return dimensioned<typename product<Form, Type>::type> \
716  ( \
717  '(' + name(t1) + #op + dt2.name() + ')', \
718  dt2.dimensions(), \
719  static_cast<const Form&>(t1) op dt2.value() \
720  ); \
721 }
722 
723 PRODUCT_OPERATOR(outerProduct, *, outer)
724 PRODUCT_OPERATOR(crossProduct, ^, cross)
725 PRODUCT_OPERATOR(innerProduct, &, dot)
726 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
727 
728 #undef PRODUCT_OPERATOR
729 
730 
731 // ************************************************************************* //
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
static bool writeOptionalEntries
If true write optional keywords and values.
Definition: dictionary.H:262
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:548
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:710
T lookupOrAddDefault(const word &, const T &, bool recursive=false, bool patternMatch=true)
Find and return a T, if not found return the given.
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:509
Dimension set for the base types.
Definition: dimensionSet.H:125
Generic dimensioned Type class.
void operator+=(const dimensioned< Type > &)
static dimensioned< Type > lookupOrAddToDict(const word &, dictionary &, const dimensionSet &dims=dimless, const Type &defaultValue=pTraits< Type >::zero)
Construct from dictionary, with default value.
dimensioned()
Null constructor.
void replace(const direction, const dimensioned< cmptType > &)
Return a component with a dimensioned<cmptType>
const dimensionSet & dimensions() const
Return const reference to dimensions.
pTraits< Type >::cmptType cmptType
Component type.
const Type & value() const
Return const reference to value.
dimensioned< cmptType > component(const direction) const
Return a component as a dimensioned<cmptType>
void operator/=(const scalar)
void operator-=(const dimensioned< Type > &)
void read(const dictionary &)
Update the value of dimensioned<Type>
dimensioned< cmptType > operator[](const direction) const
Return a component as a dimensioned<cmptType>
static dimensioned< Type > lookupOrDefault(const word &, const dictionary &, const dimensionSet &dims=dimless, const Type &defaultValue=pTraits< Type >::zero)
Construct from dictionary, with default dimensions and value.
const word & name() const
Return const reference to name.
bool readIfPresent(const dictionary &)
Update the value of dimensioned<Type> if found in the dictionary.
void operator*=(const scalar)
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
Traits class for primitives.
Definition: pTraits.H:53
symmTypeOfRank< typename pTraits< arg1 >::cmptType, arg2 *direction(pTraits< arg1 >::rank) >::type type
Definition: products.H:136
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
#define PRODUCT_OPERATOR(product, op, opFunc)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
#define IOInfoInFunction(ios)
Report an IO information message using Foam::Info.
Namespace for OpenFOAM.
static const zero Zero
Definition: zero.H:97
const unitConversion unitAny
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const dimensionSet dimless
void dot(FieldField< Field1, typename innerProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
const HashTable< unitConversion > & units()
Get the table of unit conversions.
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
dimensioned< scalar > mag(const dimensioned< Type > &)
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
void dotdot(FieldField< Field1, typename scalarProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
error FatalError
void cross(FieldField< Field1, typename crossProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)
dimensioned< scalar > magSqr(const dimensioned< Type > &)
void outer(FieldField< Field1, typename outerProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
uint8_t direction
Definition: direction.H:45
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict