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