BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / dimensionedTypes / dimensionedType / dimensionedType.C
blob1d65b22fd6680251f87acb27ae70ddedc9658fb8
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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"
27 #include "pTraits.H"
28 #include "dictionary.H"
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 namespace Foam
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 template <class Type>
38 dimensioned<Type> dimensioned<Type>::lookupOrDefault
40     const word& name,
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);
51 template <class Type>
52 dimensioned<Type> dimensioned<Type>::lookupOrAddToDict
54     const word& name,
55     dictionary& dict,
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  * * * * * * * * * * * * * * //
67 template <class Type>
68 dimensioned<Type>::dimensioned
70     const word& name,
71     const dimensionSet& dimSet,
72     const Type t
75     name_(name),
76     dimensions_(dimSet),
77     value_(t)
81 template <class Type>
82 dimensioned<Type>::dimensioned
84     const word& name,
85     const dimensioned<Type>& dt
88     name_(name),
89     dimensions_(dt.dimensions_),
90     value_(dt.value_)
94 template <class Type>
95 dimensioned<Type>::dimensioned
97     Istream& is
100     name_(is),
101     dimensions_(is),
102     value_(pTraits<Type>(is))
106 template <class Type>
107 dimensioned<Type>::dimensioned
109     const word& name,
110     Istream& is
113     name_(name),
114     dimensions_(is),
115     value_(pTraits<Type>(is))
119 template <class Type>
120 dimensioned<Type>::dimensioned
122     const word& name,
123     const dimensionSet& dimSet,
124     Istream& is
127     name_(name),
128     dimensions_(dimSet),
129     value_(pTraits<Type>(is))
133 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
135 template <class Type>
136 const word& dimensioned<Type>::name() const
138     return name_;
141 template <class Type>
142 word& dimensioned<Type>::name()
144     return name_;
148 template <class Type>
149 const dimensionSet& dimensioned<Type>::dimensions() const
151     return dimensions_;
154 template <class Type>
155 dimensionSet& dimensioned<Type>::dimensions()
157     return dimensions_;
161 template <class Type>
162 const Type& dimensioned<Type>::value() const
164     return value_;
167 template <class Type>
168 Type& dimensioned<Type>::value()
170     return value_;
174 template <class Type>
175 dimensioned<typename dimensioned<Type>::cmptType> dimensioned<Type>::component
177     const direction d
178 ) const
180     return dimensioned<cmptType>
181     (
182         name_ + ".component(" + Foam::name(d) + ')',
183         dimensions_,
184         value_.component(d)
185     );
189 template <class Type>
190 void dimensioned<Type>::replace
192     const direction d,
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[]
213     const direction d
214 ) const
216     return component(d);
220 template <class Type>
221 void dimensioned<Type>::operator+=
223     const dimensioned<Type>& dt
226     dimensions_ += dt.dimensions_;
227     value_ += dt.value_;
231 template <class Type>
232 void dimensioned<Type>::operator-=
234     const dimensioned<Type>& dt
237     dimensions_ -= dt.dimensions_;
238     value_ -= dt.value_;
242 template <class Type>
243 void dimensioned<Type>::operator*=
245     const scalar s
248     value_ *= s;
252 template <class Type>
253 void dimensioned<Type>::operator/=
255     const scalar s
258     value_ /= s;
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>
269     (
270         "pow(" + dt.name() + ',' + name(r) + ')',
271         pow(dt.dimensions(), r),
272         pow(dt.value(), 2)
273     );
276 template<class Type>
277 dimensioned<typename outerProduct<Type, Type>::type>
278 sqr(const dimensioned<Type>& dt)
280     return dimensioned<typename outerProduct<Type, Type>::type>
281     (
282         "sqr(" + dt.name() + ')',
283         sqr(dt.dimensions()),
284         sqr(dt.value())
285     );
288 template<class Type>
289 dimensioned<scalar> magSqr(const dimensioned<Type>& dt)
291     return dimensioned<scalar>
292     (
293         "magSqr(" + dt.name() + ')',
294         magSqr(dt.dimensions()),
295         magSqr(dt.value())
296     );
299 template<class Type>
300 dimensioned<scalar> mag(const dimensioned<Type>& dt)
302     return dimensioned<scalar>
303     (
304         "mag(" + dt.name() + ')',
305         dt.dimensions(),
306         mag(dt.value())
307     );
311 template <class Type>
312 dimensioned<Type> max
314     const dimensioned<Type>& dt1,
315     const dimensioned<Type>& dt2
318     if (dt1.dimensions() != dt2.dimensions())
319     {
320         FatalErrorIn("max(const dimensioned<Type>&, const dimensioned<Type>&)")
321             << "dimensions of arguments are not equal"
322             << abort(FatalError);
323     }
325     return dimensioned<Type>
326     (
327         "max(" + dt1.name() + ',' + dt2.name() + ')',
328         dt1.dimensions(),
329         max(dt1.value(), dt2.value())
330     );
334 template <class Type>
335 dimensioned<Type> min
337     const dimensioned<Type>& dt1,
338     const dimensioned<Type>& dt2
341     if (dt1.dimensions() != dt2.dimensions())
342     {
343         FatalErrorIn("min(const dimensioned<Type>&, const dimensioned<Type>&)")
344             << "dimensions of arguments are not equal"
345             << abort(FatalError);
346     }
348     return dimensioned<Type>
349     (
350         "min(" + dt1.name() + ',' + dt2.name() + ')',
351         dt1.dimensions(),
352         min(dt1.value(), dt2.value())
353     );
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>&)");
368     return is;
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
378         << dt.value();
380     // Check state of Ostream
381     os.check("Ostream& operator<<(Ostream&, const dimensioned<Type>&)");
383     return os;
387 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
389 template <class Type>
390 bool operator>
392     const dimensioned<Type>& dt1,
393     const dimensioned<Type>& dt2
396     return dt1.value() > dt2.value();
400 template <class Type>
401 bool operator<
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>
419     (
420         '(' + dt1.name() + '+' + dt2.name() + ')',
421         dt1.dimensions() + dt2.dimensions(),
422         dt1.value() + dt2.value()
423     );
427 template <class Type>
428 dimensioned<Type> operator-(const dimensioned<Type>& dt)
430     return dimensioned<Type>
431     (
432         '-' + dt.name(),
433         dt.dimensions(),
434         -dt.value()
435     );
439 template <class Type>
440 dimensioned<Type> operator-
442     const dimensioned<Type>& dt1,
443     const dimensioned<Type>& dt2
446     return dimensioned<Type>
447     (
448         '(' + dt1.name() + '-' + dt2.name() + ')',
449         dt1.dimensions() - dt2.dimensions(),
450         dt1.value() - dt2.value()
451     );
455 template <class Type>
456 dimensioned<Type> operator*
458     const dimensioned<scalar>& ds,
459     const dimensioned<Type>& dt
462     return dimensioned<Type>
463     (
464         '(' + ds.name() + '*' + dt.name() + ')',
465         ds.dimensions() * dt.dimensions(),
466         ds.value() * dt.value()
467     );
471 template <class Type>
472 dimensioned<Type> operator/
474     const dimensioned<Type>& dt,
475     const dimensioned<scalar>& ds
478     return dimensioned<Type>
479     (
480         '(' + dt.name() + '|' + ds.name() + ')',
481         dt.dimensions()/ds.dimensions(),
482         dt.value()/ds.value()
483     );
487 // Products
488 // ~~~~~~~~
490 #define PRODUCT_OPERATOR(product, op, opFunc)                                 \
491                                                                               \
492 template<class Type1, class Type2>                                            \
493 dimensioned<typename product<Type1, Type2>::type>                             \
494 operator op(const dimensioned<Type1>& dt1, const dimensioned<Type2>& dt2)     \
495 {                                                                             \
496     return dimensioned<typename product<Type1, Type2>::type>                  \
497     (                                                                         \
498         '(' + dt1.name() + #op + dt2.name() + ')',                            \
499         dt1.dimensions() op dt2.dimensions(),                                 \
500         dt1.value() op dt2.value()                                            \
501     );                                                                        \
502 }                                                                             \
503                                                                               \
504 template<class Type, class Form, class Cmpt, int nCmpt>                       \
505 dimensioned<typename product<Type, Form>::type>                               \
506 operator op                                                                   \
507 (                                                                             \
508     const dimensioned<Type>& dt1,                                             \
509     const VectorSpace<Form,Cmpt,nCmpt>& t2                                    \
510 )                                                                             \
511 {                                                                             \
512     return dimensioned<typename product<Type, Form>::type>                    \
513     (                                                                         \
514         '(' + dt1.name() + #op + name(t2) + ')',                              \
515         dt1.dimensions(),                                                     \
516         dt1.value() op static_cast<const Form&>(t2)                           \
517     );                                                                        \
518 }                                                                             \
519                                                                               \
520 template<class Type, class Form, class Cmpt, int nCmpt>                       \
521 dimensioned<typename product<Form, Type>::type>                               \
522 operator op                                                                   \
523 (                                                                             \
524     const VectorSpace<Form,Cmpt,nCmpt>& t1,                                   \
525     const dimensioned<Type>& dt2                                              \
526 )                                                                             \
527 {                                                                             \
528     return dimensioned<typename product<Form, Type>::type>                    \
529     (                                                                         \
530         '(' + name(t1) + #op + dt2.name() + ')',                              \
531         dt2.dimensions(),                                                     \
532         static_cast<const Form&>(t1) op dt2.value()                           \
533     );                                                                        \
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 // ************************************************************************* //