dimensionedType.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 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 word& name,
92  const dimensioned<Type>& dt
93 )
94 :
95  name_(name),
96  dimensions_(dt.dimensions_),
97  value_(dt.value_)
98 {}
99 
100 
101 template<class Type>
103 (
104  Istream& is
105 )
106 :
107  dimensions_(dimless)
108 {
109  read(is);
110 }
111 
112 
113 template<class Type>
115 (
116  const word& name,
117  Istream& is
118 )
119 :
120  name_(name),
121  dimensions_(dimless)
122 {
123  scalar multiplier;
124  dimensions_.read(is, multiplier);
125  is >> value_;
126  value_ *= multiplier;
127 }
128 
129 
130 template<class Type>
132 (
133  const word& name,
134  const dimensionSet& dimSet,
135  Istream& is
136 )
137 :
138  name_(name),
139  dimensions_(dimSet),
140  value_(Zero)
141 {
142  initialize(is);
143 }
144 
145 
146 template<class Type>
148 (
149  const word& name,
150  const dimensionSet& dimSet,
151  const dictionary& dict
152 )
153 :
154  name_(name),
155  dimensions_(dimSet),
156  value_(Zero)
157 {
158  initialize(dict.lookup(name));
159 }
160 
161 
162 template<class Type>
164 ()
165 :
166  name_("undefined"),
167  dimensions_(dimless),
168  value_(Zero)
169 {}
170 
171 
172 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
173 
174 template<class Type>
176 (
177  const word& name,
178  const dictionary& dict,
179  const dimensionSet& dims,
180  const Type& defaultValue
181 )
182 {
183  if (dict.found(name))
184  {
185  return dimensioned<Type>(name, dims, dict.lookup(name));
186  }
187  else
188  {
189  return dimensioned<Type>(name, dims, defaultValue);
190  }
191 }
192 
193 
194 template<class Type>
196 (
197  const word& name,
198  const dictionary& dict,
199  const Type& defaultValue
200 )
201 {
202  return lookupOrDefault(name, dict, dimless, defaultValue);
203 }
204 
205 
206 template<class Type>
208 (
209  const word& name,
210  dictionary& dict,
211  const dimensionSet& dims,
212  const Type& defaultValue
213 )
214 {
215  Type value = dict.lookupOrAddDefault<Type>(name, defaultValue);
216  return dimensioned<Type>(name, dims, value);
217 }
218 
219 
220 template<class Type>
222 (
223  const word& name,
224  dictionary& dict,
225  const Type& defaultValue
226 )
227 {
228  return lookupOrAddToDict(name, dict, dimless, defaultValue);
229 }
230 
231 
232 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
233 
234 template<class Type>
236 {
237  return name_;
238 }
239 
240 template<class Type>
242 {
243  return name_;
244 }
245 
246 
247 template<class Type>
249 {
250  return dimensions_;
251 }
252 
253 template<class Type>
255 {
256  return dimensions_;
257 }
258 
259 
260 template<class Type>
262 {
263  return value_;
264 }
265 
266 template<class Type>
268 {
269  return value_;
270 }
271 
272 
273 template<class Type>
276 (
277  const direction d
278 ) const
279 {
280  return dimensioned<cmptType>
281  (
282  name_ + ".component(" + Foam::name(d) + ')',
283  dimensions_,
284  value_.component(d)
285  );
286 }
287 
288 
289 template<class Type>
291 (
292  const direction d,
293  const dimensioned<typename dimensioned<Type>::cmptType>& dc
294 )
295 {
296  dimensions_ = dc.dimensions();
297  value_.replace(d, dc.value());
298 }
299 
300 
301 template<class Type>
303 {
304  dict.lookup(name_) >> value_;
305 }
306 
307 
308 template<class Type>
310 {
311  return dict.readIfPresent(name_, value_);
312 }
313 
314 
315 template<class Type>
318 {
319  // Read name
320  is >> name_;
321 
322  // Read dimensionSet + multiplier
323  scalar mult;
324  dimensions_.read(is, mult, readSet);
325 
326  // Read value
327  is >> value_;
328  value_ *= mult;
329 
330  // Check state of Istream
331  is.check
332  (
333  "Istream& dimensioned<Type>::read(Istream& is, const dictionary&)"
334  );
335 
336  return is;
337 }
338 
339 
340 template<class Type>
342 (
343  Istream& is,
344  const HashTable<dimensionedScalar>& readSet
345 )
346 {
347  // Read name
348  is >> name_;
349 
350  // Read dimensionSet + multiplier
351  scalar mult;
352  dimensions_.read(is, mult, readSet);
353 
354  // Read value
355  is >> value_;
356  value_ *= mult;
357 
358  // Check state of Istream
359  is.check
360  (
361  "Istream& dimensioned<Type>::read"
362  "(Istream& is, const HashTable<dimensionedScalar>&)"
363  );
364 
365  return is;
366 }
367 
368 
369 template<class Type>
371 {
372  // Read name
373  is >> name_;
374 
375  // Read dimensionSet + multiplier
376  scalar mult;
377  dimensions_.read(is, mult);
378 
379  // Read value
380  is >> value_;
381  value_ *= mult;
382 
383  // Check state of Istream
384  is.check
385  (
386  "Istream& dimensioned<Type>::read(Istream& is)"
387  );
388 
389  return is;
390 }
391 
392 
393 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
394 
395 template<class Type>
397 Foam::dimensioned<Type>::operator[]
398 (
399  const direction d
400 ) const
401 {
402  return component(d);
403 }
404 
405 
406 template<class Type>
407 void Foam::dimensioned<Type>::operator+=
408 (
409  const dimensioned<Type>& dt
410 )
411 {
412  dimensions_ += dt.dimensions_;
413  value_ += dt.value_;
414 }
415 
416 
417 template<class Type>
418 void Foam::dimensioned<Type>::operator-=
419 (
420  const dimensioned<Type>& dt
421 )
422 {
423  dimensions_ -= dt.dimensions_;
424  value_ -= dt.value_;
425 }
426 
427 
428 template<class Type>
429 void Foam::dimensioned<Type>::operator*=
430 (
431  const scalar s
432 )
433 {
434  value_ *= s;
435 }
436 
437 
438 template<class Type>
439 void Foam::dimensioned<Type>::operator/=
440 (
441  const scalar s
442 )
443 {
444  value_ /= s;
445 }
446 
447 
448 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
449 
450 template<class Type, Foam::direction r>
453 {
455  (
456  "pow(" + dt.name() + ',' + name(r) + ')',
457  pow(dt.dimensions(), r),
458  pow(dt.value(), 2)
459  );
460 }
461 
462 
463 template<class Type>
465 Foam::sqr(const dimensioned<Type>& dt)
466 {
468  (
469  "sqr(" + dt.name() + ')',
470  sqr(dt.dimensions()),
471  sqr(dt.value())
472  );
473 }
474 
475 template<class Type>
477 {
478  return dimensioned<scalar>
479  (
480  "magSqr(" + dt.name() + ')',
481  magSqr(dt.dimensions()),
482  magSqr(dt.value())
483  );
484 }
485 
486 template<class Type>
488 {
489  return dimensioned<scalar>
490  (
491  "mag(" + dt.name() + ')',
492  dt.dimensions(),
493  mag(dt.value())
494  );
495 }
496 
497 
498 template<class Type>
500 (
501  const dimensioned<Type>& dt1,
502  const dimensioned<Type>& dt2
503 )
504 {
505  return dimensioned<Type>
506  (
507  "cmptMultiply(" + dt1.name() + ',' + dt2.name() + ')',
508  cmptMultiply(dt1.dimensions(), dt2.dimensions()),
509  cmptMultiply(dt1.value(), dt2.value())
510  );
511 }
512 
513 template<class Type>
515 (
516  const dimensioned<Type>& dt1,
517  const dimensioned<Type>& dt2
518 )
519 {
520  return dimensioned<Type>
521  (
522  "cmptDivide(" + dt1.name() + ',' + dt2.name() + ')',
523  cmptDivide(dt1.dimensions(), dt2.dimensions()),
524  cmptDivide(dt1.value(), dt2.value())
525  );
526 }
527 
528 
529 template<class Type>
531 (
532  const dimensioned<Type>& dt1,
533  const dimensioned<Type>& dt2
534 )
535 {
536  if (dt1.dimensions() != dt2.dimensions())
537  {
539  << "dimensions of arguments are not equal"
540  << abort(FatalError);
541  }
542 
543  return dimensioned<Type>
544  (
545  "max(" + dt1.name() + ',' + dt2.name() + ')',
546  dt1.dimensions(),
547  max(dt1.value(), dt2.value())
548  );
549 }
550 
551 
552 template<class Type>
554 (
555  const dimensioned<Type>& dt1,
556  const dimensioned<Type>& dt2
557 )
558 {
559  if (dt1.dimensions() != dt2.dimensions())
560  {
562  << "dimensions of arguments are not equal"
563  << abort(FatalError);
564  }
565 
566  return dimensioned<Type>
567  (
568  "min(" + dt1.name() + ',' + dt2.name() + ')',
569  dt1.dimensions(),
570  min(dt1.value(), dt2.value())
571  );
572 }
573 
574 
575 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
576 
577 template<class Type>
579 {
580  token nextToken(is);
581  is.putBack(nextToken);
582 
583  // Check if the original format is used in which the name is provided
584  // and reset the name to that read
585  if (nextToken.isWord())
586  {
587  is >> dt.name_;
588  is >> nextToken;
589  is.putBack(nextToken);
590  }
591 
592  // If the dimensions are provided reset the dimensions to those read
593  scalar multiplier = 1.0;
594  if (nextToken == token::BEGIN_SQR)
595  {
596  dt.dimensions_.read(is, multiplier);
597  }
598 
599  // Read the value
600  is >> dt.value_;
601  dt.value_ *= multiplier;
602 
603  // Check state of Istream
604  is.check("Istream& operator>>(Istream&, dimensioned<Type>&)");
605 
606  return is;
607 }
608 
609 
610 template<class Type>
611 Foam::Ostream& Foam::operator<<(Ostream& os, const dimensioned<Type>& dt)
612 {
613  // Write the name
614  os << dt.name() << token::SPACE;
615 
616  // Write the dimensions
617  scalar mult;
618  dt.dimensions().write(os, mult);
619 
620  os << token::SPACE;
621 
622  // Write the value
623  os << dt.value()/mult;
624 
625  // Check state of Ostream
626  os.check("Ostream& operator<<(Ostream&, const dimensioned<Type>&)");
627 
628  return os;
629 }
630 
631 
632 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
633 
634 template<class Type>
635 bool Foam::operator>
636 (
637  const dimensioned<Type>& dt1,
638  const dimensioned<Type>& dt2
639 )
640 {
641  return dt1.value() > dt2.value();
642 }
643 
644 
645 template<class Type>
646 bool Foam::operator<
647 (
648  const dimensioned<Type>& dt1,
649  const dimensioned<Type>& dt2
650 )
651 {
652  return dt1.value() < dt2.value();
653 }
654 
655 
656 template<class Type>
657 Foam::dimensioned<Type> Foam::operator+
658 (
659  const dimensioned<Type>& dt1,
660  const dimensioned<Type>& dt2
661 )
662 {
663  return dimensioned<Type>
664  (
665  '(' + dt1.name() + '+' + dt2.name() + ')',
666  dt1.dimensions() + dt2.dimensions(),
667  dt1.value() + dt2.value()
668  );
669 }
670 
671 
672 template<class Type>
674 {
675  return dimensioned<Type>
676  (
677  '-' + dt.name(),
678  dt.dimensions(),
679  -dt.value()
680  );
681 }
682 
683 
684 template<class Type>
685 Foam::dimensioned<Type> Foam::operator-
686 (
687  const dimensioned<Type>& dt1,
688  const dimensioned<Type>& dt2
689 )
690 {
691  return dimensioned<Type>
692  (
693  '(' + dt1.name() + '-' + dt2.name() + ')',
694  dt1.dimensions() - dt2.dimensions(),
695  dt1.value() - dt2.value()
696  );
697 }
698 
699 
700 template<class Type>
701 Foam::dimensioned<Type> Foam::operator*
702 (
703  const dimensioned<scalar>& ds,
704  const dimensioned<Type>& dt
705 )
706 {
707  return dimensioned<Type>
708  (
709  '(' + ds.name() + '*' + dt.name() + ')',
710  ds.dimensions() * dt.dimensions(),
711  ds.value() * dt.value()
712  );
713 }
714 
715 
716 template<class Type>
717 Foam::dimensioned<Type> Foam::operator/
718 (
719  const dimensioned<Type>& dt,
720  const dimensioned<scalar>& ds
721 )
722 {
723  return dimensioned<Type>
724  (
725  '(' + dt.name() + '|' + ds.name() + ')',
726  dt.dimensions()/ds.dimensions(),
727  dt.value()/ds.value()
728  );
729 }
730 
731 
732 #define PRODUCT_OPERATOR(product, op, opFunc) \
733  \
734 template<class Type1, class Type2> \
735 Foam::dimensioned<typename Foam::product<Type1, Type2>::type> \
736 Foam::operator op \
737 ( \
738  const dimensioned<Type1>& dt1, \
739  const dimensioned<Type2>& dt2 \
740 ) \
741 { \
742  return dimensioned<typename product<Type1, Type2>::type> \
743  ( \
744  '(' + dt1.name() + #op + dt2.name() + ')', \
745  dt1.dimensions() op dt2.dimensions(), \
746  dt1.value() op dt2.value() \
747  ); \
748 } \
749  \
750 template<class Type, class Form, class Cmpt, Foam::direction nCmpt> \
751 Foam::dimensioned<typename Foam::product<Type, Form>::type> \
752 Foam::operator op \
753 ( \
754  const dimensioned<Type>& dt1, \
755  const VectorSpace<Form,Cmpt,nCmpt>& t2 \
756 ) \
757 { \
758  return dimensioned<typename product<Type, Form>::type> \
759  ( \
760  '(' + dt1.name() + #op + name(t2) + ')', \
761  dt1.dimensions(), \
762  dt1.value() op static_cast<const Form&>(t2) \
763  ); \
764 } \
765  \
766 template<class Type, class Form, class Cmpt, Foam::direction nCmpt> \
767 Foam::dimensioned<typename Foam::product<Form, Type>::type> \
768 Foam::operator op \
769 ( \
770  const VectorSpace<Form,Cmpt,nCmpt>& t1, \
771  const dimensioned<Type>& dt2 \
772 ) \
773 { \
774  return dimensioned<typename product<Form, Type>::type> \
775  ( \
776  '(' + name(t1) + #op + dt2.name() + ')', \
777  dt2.dimensions(), \
778  static_cast<const Form&>(t1) op dt2.value() \
779  ); \
780 }
781 
782 
783 PRODUCT_OPERATOR(outerProduct, *, outer)
784 PRODUCT_OPERATOR(crossProduct, ^, cross)
785 PRODUCT_OPERATOR(innerProduct, &, dot)
786 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
787 
788 #undef PRODUCT_OPERATOR
789 
790 
791 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:431
bool isWord() const
Definition: tokenI.H:213
static dimensioned< Type > lookupOrAddToDict(const word &, dictionary &, const dimensionSet &dims=dimless, const Type &defaultValue=pTraits< Type >::zero)
Construct from dictionary, with default value.
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
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:69
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)
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:120
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>
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)
static const zero Zero
Definition: zero.H:91
An STL-conforming hash table.
Definition: HashTable.H:62
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:53
dimensioned()
Null constructor.
const word & name() const
Return const reference to name.
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
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)
const dimensionSet dimless(0, 0, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:47
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
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.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576
IOerror FatalIOError