1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "dimensionedType.H"
29 #include "dictionary.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 dimensioned<Type> dimensioned<Type>::lookupOrAddToDict
43 const Type& defaultValue,
44 const dimensionSet& dims
47 Type value = dict.lookupOrAddDefault<Type>(name, defaultValue);
48 return dimensioned<Type>(name, dims, value);
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 dimensioned<Type>::dimensioned
58 const dimensionSet& dimSet,
69 dimensioned<Type>::dimensioned
72 const dimensioned<Type>& dt
76 dimensions_(dt.dimensions_),
82 dimensioned<Type>::dimensioned
89 value_(pTraits<Type>(is))
94 dimensioned<Type>::dimensioned
102 value_(pTraits<Type>(is))
106 template <class Type>
107 dimensioned<Type>::dimensioned
110 const dimensionSet& dimSet,
116 value_(pTraits<Type>(is))
120 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122 template <class Type>
123 const word& dimensioned<Type>::name() const
128 template <class Type>
129 word& dimensioned<Type>::name()
135 template <class Type>
136 const dimensionSet& dimensioned<Type>::dimensions() const
141 template <class Type>
142 dimensionSet& dimensioned<Type>::dimensions()
148 template <class Type>
149 const Type& dimensioned<Type>::value() const
154 template <class Type>
155 Type& dimensioned<Type>::value()
161 template <class Type>
162 dimensioned<typename dimensioned<Type>::cmptType> dimensioned<Type>::component
167 return dimensioned<cmptType>
169 name_ + ".component(" + Foam::name(d) + ')',
176 template <class Type>
177 void dimensioned<Type>::replace
180 const dimensioned<typename dimensioned<Type>::cmptType>& dc
183 dimensions_ = dc.dimensions();
184 value_.replace(d, dc.value());
188 template <class Type>
189 bool dimensioned<Type>::readIfPresent(const dictionary& dict)
191 return dict.readIfPresent(name_, value_);
195 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
197 template <class Type>
198 dimensioned<typename dimensioned<Type>::cmptType> dimensioned<Type>::operator[]
207 template <class Type>
208 void dimensioned<Type>::operator+=
210 const dimensioned<Type>& dt
213 dimensions_ += dt.dimensions_;
218 template <class Type>
219 void dimensioned<Type>::operator-=
221 const dimensioned<Type>& dt
224 dimensions_ -= dt.dimensions_;
229 template <class Type>
230 void dimensioned<Type>::operator*=
239 template <class Type>
240 void dimensioned<Type>::operator/=
249 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
251 template<class Type, int r>
252 dimensioned<typename powProduct<Type, r>::type>
253 pow(const dimensioned<Type>& dt, typename powProduct<Type, r>::type)
255 return dimensioned<typename powProduct<Type, r>::type>
257 "pow(" + dt.name() + ',' + name(r) + ')',
258 pow(dt.dimensions(), r),
264 dimensioned<typename outerProduct<Type, Type>::type>
265 sqr(const dimensioned<Type>& dt)
267 return dimensioned<typename outerProduct<Type, Type>::type>
269 "sqr(" + dt.name() + ')',
270 sqr(dt.dimensions()),
276 dimensioned<scalar> magSqr(const dimensioned<Type>& dt)
278 return dimensioned<scalar>
280 "magSqr(" + dt.name() + ')',
281 magSqr(dt.dimensions()),
287 dimensioned<scalar> mag(const dimensioned<Type>& dt)
289 return dimensioned<scalar>
291 "mag(" + dt.name() + ')',
298 template <class Type>
299 dimensioned<Type> max
301 const dimensioned<Type>& dt1,
302 const dimensioned<Type>& dt2
305 if (dt1.dimensions() != dt2.dimensions())
307 FatalErrorIn("max(const dimensioned<Type>&, const dimensioned<Type>&)")
308 << "dimensions of arguments are not equal"
309 << abort(FatalError);
312 return dimensioned<Type>
314 "max(" + dt1.name() + ',' + dt2.name() + ')',
316 max(dt1.value(), dt2.value())
321 template <class Type>
322 dimensioned<Type> min
324 const dimensioned<Type>& dt1,
325 const dimensioned<Type>& dt2
328 if (dt1.dimensions() != dt2.dimensions())
330 FatalErrorIn("min(const dimensioned<Type>&, const dimensioned<Type>&)")
331 << "dimensions of arguments are not equal"
332 << abort(FatalError);
335 return dimensioned<Type>
337 "min(" + dt1.name() + ',' + dt2.name() + ')',
339 min(dt1.value(), dt2.value())
344 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
346 template <class Type>
347 Istream& operator>>(Istream& is, dimensioned<Type>& dt)
349 // do a stream read op for a Type and a dimensions()et
350 is >> dt.name_ >> dt.dimensions_ >> dt.value_;
352 // Check state of Istream
353 is.check("Istream& operator>>(Istream&, dimensioned<Type>&)");
359 template <class Type>
360 Ostream& operator<<(Ostream& os, const dimensioned<Type>& dt)
362 // do a stream write op for a dimensions()et
363 os << dt.name() << token::SPACE
364 << dt.dimensions() << token::SPACE
367 // Check state of Ostream
368 os.check("Ostream& operator<<(Ostream&, const dimensioned<Type>&)");
374 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
376 template <class Type>
379 const dimensioned<Type>& dt1,
380 const dimensioned<Type>& dt2
383 return dt1.value() > dt2.value();
387 template <class Type>
390 const dimensioned<Type>& dt1,
391 const dimensioned<Type>& dt2
394 return dt1.value() < dt2.value();
398 template <class Type>
399 dimensioned<Type> operator+
401 const dimensioned<Type>& dt1,
402 const dimensioned<Type>& dt2
405 return dimensioned<Type>
407 '(' + dt1.name() + '+' + dt2.name() + ')',
408 dt1.dimensions() + dt2.dimensions(),
409 dt1.value() + dt2.value()
414 template <class Type>
415 dimensioned<Type> operator-(const dimensioned<Type>& dt)
417 return dimensioned<Type>
426 template <class Type>
427 dimensioned<Type> operator-
429 const dimensioned<Type>& dt1,
430 const dimensioned<Type>& dt2
433 return dimensioned<Type>
435 '(' + dt1.name() + '-' + dt2.name() + ')',
436 dt1.dimensions() - dt2.dimensions(),
437 dt1.value() - dt2.value()
442 template <class Type>
443 dimensioned<Type> operator*
445 const dimensioned<scalar>& ds,
446 const dimensioned<Type>& dt
449 return dimensioned<Type>
451 '(' + ds.name() + '*' + dt.name() + ')',
452 ds.dimensions() * dt.dimensions(),
453 ds.value() * dt.value()
458 template <class Type>
459 dimensioned<Type> operator/
461 const dimensioned<Type>& dt,
462 const dimensioned<scalar>& ds
465 return dimensioned<Type>
467 '(' + dt.name() + '|' + ds.name() + ')',
468 dt.dimensions()/ds.dimensions(),
469 dt.value()/ds.value()
477 #define PRODUCT_OPERATOR(product, op, opFunc) \
479 template<class Type1, class Type2> \
480 dimensioned<typename product<Type1, Type2>::type> \
481 operator op(const dimensioned<Type1>& dt1, const dimensioned<Type2>& dt2) \
483 return dimensioned<typename product<Type1, Type2>::type> \
485 '(' + dt1.name() + #op + dt2.name() + ')', \
486 dt1.dimensions() op dt2.dimensions(), \
487 dt1.value() op dt2.value() \
491 template<class Type, class Form, class Cmpt, int nCmpt> \
492 dimensioned<typename product<Type, Form>::type> \
495 const dimensioned<Type>& dt1, \
496 const VectorSpace<Form,Cmpt,nCmpt>& t2 \
499 return dimensioned<typename product<Type, Form>::type> \
501 '(' + dt1.name() + #op + name(t2) + ')', \
503 dt1.value() op static_cast<const Form&>(t2) \
507 template<class Type, class Form, class Cmpt, int nCmpt> \
508 dimensioned<typename product<Form, Type>::type> \
511 const VectorSpace<Form,Cmpt,nCmpt>& t1, \
512 const dimensioned<Type>& dt2 \
515 return dimensioned<typename product<Form, Type>::type> \
517 '(' + name(t1) + #op + dt2.name() + ')', \
519 static_cast<const Form&>(t1) op dt2.value() \
524 PRODUCT_OPERATOR(outerProduct, *, outer)
525 PRODUCT_OPERATOR(crossProduct, ^, cross)
526 PRODUCT_OPERATOR(innerProduct, &, dot)
527 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
529 #undef PRODUCT_OPERATOR
532 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
534 } // End namespace Foam
536 // ************************************************************************* //