1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "dimensionedType.H"
28 #include "dictionary.H"
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 dimensioned<Type> dimensioned<Type>::lookupOrDefault
41 const dictionary& dict,
42 const Type& defaultValue,
43 const dimensionSet& dims
46 Type value = dict.lookupOrDefault<Type>(name, defaultValue);
47 return dimensioned<Type>(name, dims, value);
52 dimensioned<Type> dimensioned<Type>::lookupOrAddToDict
56 const Type& defaultValue,
57 const dimensionSet& dims
60 Type value = dict.lookupOrAddDefault<Type>(name, defaultValue);
61 return dimensioned<Type>(name, dims, value);
65 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
68 dimensioned<Type>::dimensioned
71 const dimensionSet& dimSet,
82 dimensioned<Type>::dimensioned
85 const dimensioned<Type>& dt
89 dimensions_(dt.dimensions_),
95 dimensioned<Type>::dimensioned
102 value_(pTraits<Type>(is))
106 template <class Type>
107 dimensioned<Type>::dimensioned
115 value_(pTraits<Type>(is))
119 template <class Type>
120 dimensioned<Type>::dimensioned
123 const dimensionSet& dimSet,
129 value_(pTraits<Type>(is))
133 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135 template <class Type>
136 const word& dimensioned<Type>::name() const
141 template <class Type>
142 word& dimensioned<Type>::name()
148 template <class Type>
149 const dimensionSet& dimensioned<Type>::dimensions() const
154 template <class Type>
155 dimensionSet& dimensioned<Type>::dimensions()
161 template <class Type>
162 const Type& dimensioned<Type>::value() const
167 template <class Type>
168 Type& dimensioned<Type>::value()
174 template <class Type>
175 dimensioned<typename dimensioned<Type>::cmptType> dimensioned<Type>::component
180 return dimensioned<cmptType>
182 name_ + ".component(" + Foam::name(d) + ')',
189 template <class Type>
190 void dimensioned<Type>::replace
193 const dimensioned<typename dimensioned<Type>::cmptType>& dc
196 dimensions_ = dc.dimensions();
197 value_.replace(d, dc.value());
201 template <class Type>
202 bool dimensioned<Type>::readIfPresent(const dictionary& dict)
204 return dict.readIfPresent(name_, value_);
208 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
210 template <class Type>
211 dimensioned<typename dimensioned<Type>::cmptType> dimensioned<Type>::operator[]
220 template <class Type>
221 void dimensioned<Type>::operator+=
223 const dimensioned<Type>& dt
226 dimensions_ += dt.dimensions_;
231 template <class Type>
232 void dimensioned<Type>::operator-=
234 const dimensioned<Type>& dt
237 dimensions_ -= dt.dimensions_;
242 template <class Type>
243 void dimensioned<Type>::operator*=
252 template <class Type>
253 void dimensioned<Type>::operator/=
262 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
264 template<class Type, int r>
265 dimensioned<typename powProduct<Type, r>::type>
266 pow(const dimensioned<Type>& dt, typename powProduct<Type, r>::type)
268 return dimensioned<typename powProduct<Type, r>::type>
270 "pow(" + dt.name() + ',' + name(r) + ')',
271 pow(dt.dimensions(), r),
277 dimensioned<typename outerProduct<Type, Type>::type>
278 sqr(const dimensioned<Type>& dt)
280 return dimensioned<typename outerProduct<Type, Type>::type>
282 "sqr(" + dt.name() + ')',
283 sqr(dt.dimensions()),
289 dimensioned<scalar> magSqr(const dimensioned<Type>& dt)
291 return dimensioned<scalar>
293 "magSqr(" + dt.name() + ')',
294 magSqr(dt.dimensions()),
300 dimensioned<scalar> mag(const dimensioned<Type>& dt)
302 return dimensioned<scalar>
304 "mag(" + dt.name() + ')',
311 template <class Type>
312 dimensioned<Type> max
314 const dimensioned<Type>& dt1,
315 const dimensioned<Type>& dt2
318 if (dt1.dimensions() != dt2.dimensions())
320 FatalErrorIn("max(const dimensioned<Type>&, const dimensioned<Type>&)")
321 << "dimensions of arguments are not equal"
322 << abort(FatalError);
325 return dimensioned<Type>
327 "max(" + dt1.name() + ',' + dt2.name() + ')',
329 max(dt1.value(), dt2.value())
334 template <class Type>
335 dimensioned<Type> min
337 const dimensioned<Type>& dt1,
338 const dimensioned<Type>& dt2
341 if (dt1.dimensions() != dt2.dimensions())
343 FatalErrorIn("min(const dimensioned<Type>&, const dimensioned<Type>&)")
344 << "dimensions of arguments are not equal"
345 << abort(FatalError);
348 return dimensioned<Type>
350 "min(" + dt1.name() + ',' + dt2.name() + ')',
352 min(dt1.value(), dt2.value())
357 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
359 template <class Type>
360 Istream& operator>>(Istream& is, dimensioned<Type>& dt)
362 // do a stream read op for a Type and a dimensions()et
363 is >> dt.name_ >> dt.dimensions_ >> dt.value_;
365 // Check state of Istream
366 is.check("Istream& operator>>(Istream&, dimensioned<Type>&)");
372 template <class Type>
373 Ostream& operator<<(Ostream& os, const dimensioned<Type>& dt)
375 // do a stream write op for a dimensions()et
376 os << dt.name() << token::SPACE
377 << dt.dimensions() << token::SPACE
380 // Check state of Ostream
381 os.check("Ostream& operator<<(Ostream&, const dimensioned<Type>&)");
387 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
389 template <class Type>
392 const dimensioned<Type>& dt1,
393 const dimensioned<Type>& dt2
396 return dt1.value() > dt2.value();
400 template <class Type>
403 const dimensioned<Type>& dt1,
404 const dimensioned<Type>& dt2
407 return dt1.value() < dt2.value();
411 template <class Type>
412 dimensioned<Type> operator+
414 const dimensioned<Type>& dt1,
415 const dimensioned<Type>& dt2
418 return dimensioned<Type>
420 '(' + dt1.name() + '+' + dt2.name() + ')',
421 dt1.dimensions() + dt2.dimensions(),
422 dt1.value() + dt2.value()
427 template <class Type>
428 dimensioned<Type> operator-(const dimensioned<Type>& dt)
430 return dimensioned<Type>
439 template <class Type>
440 dimensioned<Type> operator-
442 const dimensioned<Type>& dt1,
443 const dimensioned<Type>& dt2
446 return dimensioned<Type>
448 '(' + dt1.name() + '-' + dt2.name() + ')',
449 dt1.dimensions() - dt2.dimensions(),
450 dt1.value() - dt2.value()
455 template <class Type>
456 dimensioned<Type> operator*
458 const dimensioned<scalar>& ds,
459 const dimensioned<Type>& dt
462 return dimensioned<Type>
464 '(' + ds.name() + '*' + dt.name() + ')',
465 ds.dimensions() * dt.dimensions(),
466 ds.value() * dt.value()
471 template <class Type>
472 dimensioned<Type> operator/
474 const dimensioned<Type>& dt,
475 const dimensioned<scalar>& ds
478 return dimensioned<Type>
480 '(' + dt.name() + '|' + ds.name() + ')',
481 dt.dimensions()/ds.dimensions(),
482 dt.value()/ds.value()
490 #define PRODUCT_OPERATOR(product, op, opFunc) \
492 template<class Type1, class Type2> \
493 dimensioned<typename product<Type1, Type2>::type> \
494 operator op(const dimensioned<Type1>& dt1, const dimensioned<Type2>& dt2) \
496 return dimensioned<typename product<Type1, Type2>::type> \
498 '(' + dt1.name() + #op + dt2.name() + ')', \
499 dt1.dimensions() op dt2.dimensions(), \
500 dt1.value() op dt2.value() \
504 template<class Type, class Form, class Cmpt, int nCmpt> \
505 dimensioned<typename product<Type, Form>::type> \
508 const dimensioned<Type>& dt1, \
509 const VectorSpace<Form,Cmpt,nCmpt>& t2 \
512 return dimensioned<typename product<Type, Form>::type> \
514 '(' + dt1.name() + #op + name(t2) + ')', \
516 dt1.value() op static_cast<const Form&>(t2) \
520 template<class Type, class Form, class Cmpt, int nCmpt> \
521 dimensioned<typename product<Form, Type>::type> \
524 const VectorSpace<Form,Cmpt,nCmpt>& t1, \
525 const dimensioned<Type>& dt2 \
528 return dimensioned<typename product<Form, Type>::type> \
530 '(' + name(t1) + #op + dt2.name() + ')', \
532 static_cast<const Form&>(t1) op dt2.value() \
537 PRODUCT_OPERATOR(outerProduct, *, outer)
538 PRODUCT_OPERATOR(crossProduct, ^, cross)
539 PRODUCT_OPERATOR(innerProduct, &, dot)
540 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
542 #undef PRODUCT_OPERATOR
545 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
547 } // End namespace Foam
549 // ************************************************************************* //