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-2021 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 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
32 template<class Type>
34 {
35  token nextToken(is);
36  is.putBack(nextToken);
37 
38  // Check if the original format is used in which the name is provided
39  // and reset the name to that read
40  if (nextToken.isWord())
41  {
42  is >> name_;
43  is >> nextToken;
44  is.putBack(nextToken);
45  }
46 
47  // If the dimensions are provided compare with the argument
48  scalar multiplier = 1.0;
49 
50  if (nextToken == token::BEGIN_SQR)
51  {
52  dimensionSet dims(dimless);
53  dims.read(is, multiplier);
54 
55  if (dims != dimensions_)
56  {
58  (
59  is
60  ) << "The dimensions " << dims
61  << " provided do not match the required dimensions "
62  << dimensions_
63  << abort(FatalIOError);
64  }
65  }
66 
67  is >> value_;
68  value_ *= multiplier;
69 }
70 
71 
72 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
73 
74 template<class Type>
76 (
77  const word& name,
78  const dimensionSet& dimSet,
79  const Type& t
80 )
81 :
82  name_(name),
83  dimensions_(dimSet),
84  value_(t)
85 {}
86 
87 
88 template<class Type>
90 (
91  const dimensionSet& dimSet,
92  const Type& t
93 )
94 :
95  name_(::Foam::name(t)),
96  dimensions_(dimSet),
97  value_(t)
98 {}
99 
100 
101 template<class Type>
103 :
104  name_(::Foam::name(t)),
105  dimensions_(dimless),
106  value_(t)
107 {}
108 
109 
110 template<class Type>
112 (
113  const word& name,
114  const dimensioned<Type>& dt
115 )
116 :
117  name_(name),
118  dimensions_(dt.dimensions_),
119  value_(dt.value_)
120 {}
121 
122 
123 template<class Type>
125 (
126  Istream& is
127 )
128 :
129  dimensions_(dimless)
130 {
131  read(is);
132 }
133 
134 
135 template<class Type>
137 (
138  const word& name,
139  Istream& is
140 )
141 :
142  name_(name),
143  dimensions_(dimless)
144 {
145  scalar multiplier;
146  dimensions_.read(is, multiplier);
147  is >> value_;
148  value_ *= multiplier;
149 }
150 
151 
152 template<class Type>
154 (
155  const word& name,
156  const dimensionSet& dimSet,
157  Istream& is
158 )
159 :
160  name_(name),
161  dimensions_(dimSet),
162  value_(Zero)
163 {
164  initialise(is);
165 }
166 
167 
168 template<class Type>
170 (
171  const word& name,
172  const dimensionSet& dimSet,
173  const dictionary& dict
174 )
175 :
176  name_(name),
177  dimensions_(dimSet),
178  value_(Zero)
179 {
180  initialise(dict.lookup(name));
181 }
182 
183 
184 template<class Type>
186 ()
187 :
188  name_("undefined"),
189  dimensions_(dimless),
190  value_(Zero)
191 {}
192 
193 
194 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
195 
196 template<class Type>
198 (
199  const word& name,
200  const dictionary& dict,
201  const dimensionSet& dims,
202  const Type& defaultValue
203 )
204 {
205  if (dict.found(name))
206  {
207  return dimensioned<Type>(name, dims, dict.lookup(name));
208  }
209  else
210  {
211  return dimensioned<Type>(name, dims, defaultValue);
212  }
213 }
214 
215 
216 template<class Type>
218 (
219  const word& name,
220  const dictionary& dict,
221  const Type& defaultValue
222 )
223 {
224  return lookupOrDefault(name, dict, dimless, defaultValue);
225 }
226 
227 
228 template<class Type>
230 (
231  const word& name,
232  dictionary& dict,
233  const dimensionSet& dims,
234  const Type& defaultValue
235 )
236 {
237  Type value = dict.lookupOrAddDefault<Type>(name, defaultValue);
238  return dimensioned<Type>(name, dims, value);
239 }
240 
241 
242 template<class Type>
244 (
245  const word& name,
246  dictionary& dict,
247  const Type& defaultValue
248 )
249 {
250  return lookupOrAddToDict(name, dict, dimless, defaultValue);
251 }
252 
253 
254 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
255 
256 template<class Type>
258 {
259  return name_;
260 }
261 
262 template<class Type>
264 {
265  return name_;
266 }
267 
268 
269 template<class Type>
271 {
272  return dimensions_;
273 }
274 
275 template<class Type>
277 {
278  return dimensions_;
279 }
280 
281 
282 template<class Type>
284 {
285  return value_;
286 }
287 
288 template<class Type>
290 {
291  return value_;
292 }
293 
294 
295 template<class Type>
298 (
299  const direction d
300 ) const
301 {
302  return dimensioned<cmptType>
303  (
304  name_ + ".component(" + Foam::name(d) + ')',
305  dimensions_,
306  value_.component(d)
307  );
308 }
309 
310 
311 template<class Type>
313 (
314  const direction d,
315  const dimensioned<typename dimensioned<Type>::cmptType>& dc
316 )
317 {
318  dimensions_ = dc.dimensions();
319  value_.replace(d, dc.value());
320 }
321 
322 
323 template<class Type>
325 {
326  initialise(dict.lookup(name_));
327 }
328 
329 
330 template<class Type>
332 {
333  return dict.readIfPresent(name_, value_);
334 }
335 
336 
337 template<class Type>
340 {
341  // Read name
342  is >> name_;
343 
344  // Read dimensionSet + multiplier
345  scalar mult;
346  dimensions_.read(is, mult, readSet);
347 
348  // Read value
349  is >> value_;
350  value_ *= mult;
351 
352  // Check state of Istream
353  is.check
354  (
355  "Istream& dimensioned<Type>::read(Istream& is, const dictionary&)"
356  );
357 
358  return is;
359 }
360 
361 
362 template<class Type>
364 (
365  Istream& is,
366  const HashTable<dimensionedScalar>& readSet
367 )
368 {
369  // Read name
370  is >> name_;
371 
372  // Read dimensionSet + multiplier
373  scalar mult;
374  dimensions_.read(is, mult, readSet);
375 
376  // Read value
377  is >> value_;
378  value_ *= mult;
379 
380  // Check state of Istream
381  is.check
382  (
383  "Istream& dimensioned<Type>::read"
384  "(Istream& is, const HashTable<dimensionedScalar>&)"
385  );
386 
387  return is;
388 }
389 
390 
391 template<class Type>
393 {
394  // Read name
395  is >> name_;
396 
397  // Read dimensionSet + multiplier
398  scalar mult;
399  dimensions_.read(is, mult);
400 
401  // Read value
402  is >> value_;
403  value_ *= mult;
404 
405  // Check state of Istream
406  is.check
407  (
408  "Istream& dimensioned<Type>::read(Istream& is)"
409  );
410 
411  return is;
412 }
413 
414 
415 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
416 
417 template<class Type>
419 Foam::dimensioned<Type>::operator[]
420 (
421  const direction d
422 ) const
423 {
424  return component(d);
425 }
426 
427 
428 template<class Type>
429 void Foam::dimensioned<Type>::operator+=
430 (
431  const dimensioned<Type>& dt
432 )
433 {
434  dimensions_ += dt.dimensions_;
435  value_ += dt.value_;
436 }
437 
438 
439 template<class Type>
440 void Foam::dimensioned<Type>::operator-=
441 (
442  const dimensioned<Type>& dt
443 )
444 {
445  dimensions_ -= dt.dimensions_;
446  value_ -= dt.value_;
447 }
448 
449 
450 template<class Type>
451 void Foam::dimensioned<Type>::operator*=
452 (
453  const scalar s
454 )
455 {
456  value_ *= s;
457 }
458 
459 
460 template<class Type>
461 void Foam::dimensioned<Type>::operator/=
462 (
463  const scalar s
464 )
465 {
466  value_ /= s;
467 }
468 
469 
470 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
471 
472 template<class Type, Foam::direction r>
475 {
477  (
478  "pow(" + dt.name() + ',' + name(r) + ')',
479  pow(dt.dimensions(), r),
480  pow(dt.value(), 2)
481  );
482 }
483 
484 
485 template<class Type>
487 Foam::sqr(const dimensioned<Type>& dt)
488 {
490  (
491  "sqr(" + dt.name() + ')',
492  sqr(dt.dimensions()),
493  sqr(dt.value())
494  );
495 }
496 
497 template<class Type>
499 {
500  return dimensioned<scalar>
501  (
502  "magSqr(" + dt.name() + ')',
503  magSqr(dt.dimensions()),
504  magSqr(dt.value())
505  );
506 }
507 
508 template<class Type>
510 {
511  return dimensioned<scalar>
512  (
513  "mag(" + dt.name() + ')',
514  dt.dimensions(),
515  mag(dt.value())
516  );
517 }
518 
519 
520 template<class Type>
522 (
523  const dimensioned<Type>& dt1,
524  const dimensioned<Type>& dt2
525 )
526 {
527  return dimensioned<Type>
528  (
529  "cmptMultiply(" + dt1.name() + ',' + dt2.name() + ')',
530  cmptMultiply(dt1.dimensions(), dt2.dimensions()),
531  cmptMultiply(dt1.value(), dt2.value())
532  );
533 }
534 
535 template<class Type>
537 (
538  const dimensioned<Type>& dt1,
539  const dimensioned<Type>& dt2
540 )
541 {
542  return dimensioned<Type>
543  (
544  "cmptDivide(" + dt1.name() + ',' + dt2.name() + ')',
545  cmptDivide(dt1.dimensions(), dt2.dimensions()),
546  cmptDivide(dt1.value(), dt2.value())
547  );
548 }
549 
550 
551 template<class Type>
553 (
554  const dimensioned<Type>& dt1,
555  const dimensioned<Type>& dt2
556 )
557 {
558  if (dt1.dimensions() != dt2.dimensions())
559  {
561  << "dimensions of arguments are not equal"
562  << abort(FatalError);
563  }
564 
565  return dimensioned<Type>
566  (
567  "max(" + dt1.name() + ',' + dt2.name() + ')',
568  dt1.dimensions(),
569  max(dt1.value(), dt2.value())
570  );
571 }
572 
573 
574 template<class Type>
576 (
577  const dimensioned<Type>& dt1,
578  const dimensioned<Type>& dt2
579 )
580 {
581  if (dt1.dimensions() != dt2.dimensions())
582  {
584  << "dimensions of arguments are not equal"
585  << abort(FatalError);
586  }
587 
588  return dimensioned<Type>
589  (
590  "min(" + dt1.name() + ',' + dt2.name() + ')',
591  dt1.dimensions(),
592  min(dt1.value(), dt2.value())
593  );
594 }
595 
596 
597 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
598 
599 template<class Type>
601 {
602  os << dt;
603 }
604 
605 
606 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
607 
608 template<class Type>
610 {
611  token nextToken(is);
612  is.putBack(nextToken);
613 
614  // Check if the original format is used in which the name is provided
615  // and reset the name to that read
616  if (nextToken.isWord())
617  {
618  is >> dt.name_;
619  is >> nextToken;
620  is.putBack(nextToken);
621  }
622 
623  // If the dimensions are provided reset the dimensions to those read
624  scalar multiplier = 1.0;
625  if (nextToken == token::BEGIN_SQR)
626  {
627  dt.dimensions_.read(is, multiplier);
628  }
629 
630  // Read the value
631  is >> dt.value_;
632  dt.value_ *= multiplier;
633 
634  // Check state of Istream
635  is.check("Istream& operator>>(Istream&, dimensioned<Type>&)");
636 
637  return is;
638 }
639 
640 
641 template<class Type>
642 Foam::Ostream& Foam::operator<<(Ostream& os, const dimensioned<Type>& dt)
643 {
644  // Write the name
645  os << dt.name() << token::SPACE;
646 
647  // Write the dimensions
648  scalar mult;
649  dt.dimensions().write(os, mult);
650 
651  os << token::SPACE;
652 
653  // Write the value
654  os << dt.value()/mult;
655 
656  // Check state of Ostream
657  os.check("Ostream& operator<<(Ostream&, const dimensioned<Type>&)");
658 
659  return os;
660 }
661 
662 
663 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
664 
665 template<class Type>
666 bool Foam::operator>
667 (
668  const dimensioned<Type>& dt1,
669  const dimensioned<Type>& dt2
670 )
671 {
672  return dt1.value() > dt2.value();
673 }
674 
675 
676 template<class Type>
677 bool Foam::operator<
678 (
679  const dimensioned<Type>& dt1,
680  const dimensioned<Type>& dt2
681 )
682 {
683  return dt1.value() < dt2.value();
684 }
685 
686 
687 template<class Type>
688 Foam::dimensioned<Type> Foam::operator+
689 (
690  const dimensioned<Type>& dt1,
691  const dimensioned<Type>& dt2
692 )
693 {
694  return dimensioned<Type>
695  (
696  '(' + dt1.name() + '+' + dt2.name() + ')',
697  dt1.dimensions() + dt2.dimensions(),
698  dt1.value() + dt2.value()
699  );
700 }
701 
702 
703 template<class Type>
705 {
706  return dimensioned<Type>
707  (
708  '-' + dt.name(),
709  dt.dimensions(),
710  -dt.value()
711  );
712 }
713 
714 
715 template<class Type>
716 Foam::dimensioned<Type> Foam::operator-
717 (
718  const dimensioned<Type>& dt1,
719  const dimensioned<Type>& dt2
720 )
721 {
722  return dimensioned<Type>
723  (
724  '(' + dt1.name() + '-' + dt2.name() + ')',
725  dt1.dimensions() - dt2.dimensions(),
726  dt1.value() - dt2.value()
727  );
728 }
729 
730 
731 template<class Type>
732 Foam::dimensioned<Type> Foam::operator*
733 (
734  const dimensioned<scalar>& ds,
735  const dimensioned<Type>& dt
736 )
737 {
738  return dimensioned<Type>
739  (
740  '(' + ds.name() + '*' + dt.name() + ')',
741  ds.dimensions() * dt.dimensions(),
742  ds.value() * dt.value()
743  );
744 }
745 
746 
747 template<class Type>
748 Foam::dimensioned<Type> Foam::operator/
749 (
750  const dimensioned<Type>& dt,
751  const dimensioned<scalar>& ds
752 )
753 {
754  return dimensioned<Type>
755  (
756  '(' + dt.name() + '|' + ds.name() + ')',
757  dt.dimensions()/ds.dimensions(),
758  dt.value()/ds.value()
759  );
760 }
761 
762 
763 #define PRODUCT_OPERATOR(product, op, opFunc) \
764  \
765 template<class Type1, class Type2> \
766 Foam::dimensioned<typename Foam::product<Type1, Type2>::type> \
767 Foam::operator op \
768 ( \
769  const dimensioned<Type1>& dt1, \
770  const dimensioned<Type2>& dt2 \
771 ) \
772 { \
773  return dimensioned<typename product<Type1, Type2>::type> \
774  ( \
775  '(' + dt1.name() + #op + dt2.name() + ')', \
776  dt1.dimensions() op dt2.dimensions(), \
777  dt1.value() op dt2.value() \
778  ); \
779 } \
780  \
781 template<class Type, class Form, class Cmpt, Foam::direction nCmpt> \
782 Foam::dimensioned<typename Foam::product<Type, Form>::type> \
783 Foam::operator op \
784 ( \
785  const dimensioned<Type>& dt1, \
786  const VectorSpace<Form,Cmpt,nCmpt>& t2 \
787 ) \
788 { \
789  return dimensioned<typename product<Type, Form>::type> \
790  ( \
791  '(' + dt1.name() + #op + name(t2) + ')', \
792  dt1.dimensions(), \
793  dt1.value() op static_cast<const Form&>(t2) \
794  ); \
795 } \
796  \
797 template<class Type, class Form, class Cmpt, Foam::direction nCmpt> \
798 Foam::dimensioned<typename Foam::product<Form, Type>::type> \
799 Foam::operator op \
800 ( \
801  const VectorSpace<Form,Cmpt,nCmpt>& t1, \
802  const dimensioned<Type>& dt2 \
803 ) \
804 { \
805  return dimensioned<typename product<Form, Type>::type> \
806  ( \
807  '(' + name(t1) + #op + dt2.name() + ')', \
808  dt2.dimensions(), \
809  static_cast<const Form&>(t1) op dt2.value() \
810  ); \
811 }
812 
813 
814 PRODUCT_OPERATOR(outerProduct, *, outer)
815 PRODUCT_OPERATOR(crossProduct, ^, cross)
816 PRODUCT_OPERATOR(innerProduct, &, dot)
817 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
818 
819 #undef PRODUCT_OPERATOR
820 
821 
822 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
bool isWord() const
Definition: tokenI.H:261
static dimensioned< Type > lookupOrAddToDict(const word &, dictionary &, const dimensionSet &dims=dimless, const Type &defaultValue=pTraits< Type >::zero)
Construct from dictionary, with default value.
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
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.
T lookupOrAddDefault(const word &, const T &, bool recursive=false, bool patternMatch=true)
Find and return a T, if not found return the given.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
uint8_t direction
Definition: direction.H:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:72
void putBack(const token &)
Put back token.
Definition: Istream.C:30
bool readIfPresent(const dictionary &)
Update the value of dimensioned<Type> if found in the dictionary.
void replace(const direction, const dimensioned< cmptType > &)
Return a component with a dimensioned<cmptType>
void cross(FieldField< Field1, typename crossProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
const dimensionSet dimless
Generic dimensioned Type class.
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
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 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 > &)
Dimension set for the base types.
Definition: dimensionSet.H:121
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.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
#define PRODUCT_OPERATOR(product, op, opFunc)
virtual Istream & read(token &)=0
Return next token from stream.
pTraits< Type >::cmptType cmptType
Component type.
void read(const dictionary &)
Update the value of dimensioned<Type>
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
const Type & value() const
Return const reference to value.
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
dimensioned< cmptType > component(const direction) const
Return a component as a dimensioned<cmptType>
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)
static const zero Zero
Definition: zero.H:97
An STL-conforming hash table.
Definition: HashTable.H:61
errorManip< error > abort(error &err)
Definition: errorManip.H:131
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
dimensioned< scalar > magSqr(const dimensioned< Type > &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
dimensioned()
Null constructor.
const word & name() const
Return const reference to name.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
symmTypeOfRank< typename pTraits< arg1 >::cmptType, arg2 *direction(pTraits< arg1 >::rank) >::type type
Definition: products.H:136
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
const dimensionSet & dimensions() const
Return const reference to dimensions.
dimensioned< scalar > mag(const dimensioned< Type > &)
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Istream & read(Istream &is, scalar &multiplier, const dictionary &)
Read using provided units. Used only in initial parsing.
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864
IOerror FatalIOError