intersection with triangle plane for miss
[OpenFOAM-1.5.x.git] / src / OpenFOAM / dimensionedTypes / dimensionedType / dimensionedType.C
blob59c9dbea59c84015210eb387d67c01477fbf2a7e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
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 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
19     for more details.
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"
28 #include "pTraits.H"
29 #include "dictionary.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 template <class Type>
39 dimensioned<Type> dimensioned<Type>::lookupOrAddToDict
41     const word& name,
42     dictionary& dict,
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  * * * * * * * * * * * * * * //
54 template <class Type>
55 dimensioned<Type>::dimensioned
57     const word& name,
58     const dimensionSet& dimSet,
59     const Type t
62     name_(name),
63     dimensions_(dimSet),
64     value_(t)
68 template <class Type>
69 dimensioned<Type>::dimensioned
71     const word& name,
72     const dimensioned<Type>& dt
75     name_(name),
76     dimensions_(dt.dimensions_),
77     value_(dt.value_)
81 template <class Type>
82 dimensioned<Type>::dimensioned
84     Istream& is
87     name_(is),
88     dimensions_(is),
89     value_(pTraits<Type>(is))
93 template <class Type>
94 dimensioned<Type>::dimensioned
96     const word& name,
97     Istream& is
100     name_(name),
101     dimensions_(is),
102     value_(pTraits<Type>(is))
106 template <class Type>
107 dimensioned<Type>::dimensioned
109     const word& name,
110     const dimensionSet& dimSet,
111     Istream& is
114     name_(name),
115     dimensions_(dimSet),
116     value_(pTraits<Type>(is))
120 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
122 template <class Type>
123 const word& dimensioned<Type>::name() const
125     return name_;
128 template <class Type>
129 word& dimensioned<Type>::name()
131     return name_;
135 template <class Type>
136 const dimensionSet& dimensioned<Type>::dimensions() const
138     return dimensions_;
141 template <class Type>
142 dimensionSet& dimensioned<Type>::dimensions()
144     return dimensions_;
148 template <class Type>
149 const Type& dimensioned<Type>::value() const
151     return value_;
154 template <class Type>
155 Type& dimensioned<Type>::value()
157     return value_;
161 template <class Type>
162 dimensioned<typename dimensioned<Type>::cmptType> dimensioned<Type>::component
164     const direction d
165 ) const
167     return dimensioned<cmptType>
168     (
169         name_ + ".component(" + Foam::name(d) + ')',
170         dimensions_,
171         value_.component(d)
172     );
176 template <class Type>
177 void dimensioned<Type>::replace
179     const direction d,
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[]
200     const direction d
201 ) const
203     return component(d);
207 template <class Type>
208 void dimensioned<Type>::operator+=
210     const dimensioned<Type>& dt
213     dimensions_ += dt.dimensions_;
214     value_ += dt.value_;
218 template <class Type>
219 void dimensioned<Type>::operator-=
221     const dimensioned<Type>& dt
224     dimensions_ -= dt.dimensions_;
225     value_ -= dt.value_;
229 template <class Type>
230 void dimensioned<Type>::operator*=
232     const scalar s
235     value_ *= s;
239 template <class Type>
240 void dimensioned<Type>::operator/=
242     const scalar s
245     value_ /= s;
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>
256     (
257         "pow(" + dt.name() + ',' + name(r) + ')',
258         pow(dt.dimensions(), r),
259         pow(dt.value(), 2)
260     );
263 template<class Type>
264 dimensioned<typename outerProduct<Type, Type>::type>
265 sqr(const dimensioned<Type>& dt)
267     return dimensioned<typename outerProduct<Type, Type>::type>
268     (
269         "sqr(" + dt.name() + ')',
270         sqr(dt.dimensions()),
271         sqr(dt.value())
272     );
275 template<class Type>
276 dimensioned<scalar> magSqr(const dimensioned<Type>& dt)
278     return dimensioned<scalar>
279     (
280         "magSqr(" + dt.name() + ')',
281         magSqr(dt.dimensions()),
282         magSqr(dt.value())
283     );
286 template<class Type>
287 dimensioned<scalar> mag(const dimensioned<Type>& dt)
289     return dimensioned<scalar>
290     (
291         "mag(" + dt.name() + ')',
292         dt.dimensions(),
293         mag(dt.value())
294     );
298 template <class Type>
299 dimensioned<Type> max
301     const dimensioned<Type>& dt1,
302     const dimensioned<Type>& dt2
305     if (dt1.dimensions() != dt2.dimensions())
306     {
307         FatalErrorIn("max(const dimensioned<Type>&, const dimensioned<Type>&)")
308             << "dimensions of arguments are not equal"
309             << abort(FatalError);
310     }
312     return dimensioned<Type>
313     (
314         "max(" + dt1.name() + ',' + dt2.name() + ')',
315         dt1.dimensions(),
316         max(dt1.value(), dt2.value())
317     );
321 template <class Type>
322 dimensioned<Type> min
324     const dimensioned<Type>& dt1,
325     const dimensioned<Type>& dt2
328     if (dt1.dimensions() != dt2.dimensions())
329     {
330         FatalErrorIn("min(const dimensioned<Type>&, const dimensioned<Type>&)")
331             << "dimensions of arguments are not equal"
332             << abort(FatalError);
333     }
335     return dimensioned<Type>
336     (
337         "min(" + dt1.name() + ',' + dt2.name() + ')',
338         dt1.dimensions(),
339         min(dt1.value(), dt2.value())
340     );
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>&)");
355     return is;
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
365         << dt.value();
367     // Check state of Ostream
368     os.check("Ostream& operator<<(Ostream&, const dimensioned<Type>&)");
370     return os;
374 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
376 template <class Type>
377 bool operator>
379     const dimensioned<Type>& dt1,
380     const dimensioned<Type>& dt2
383     return dt1.value() > dt2.value();
387 template <class Type>
388 bool operator<
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>
406     (
407         '(' + dt1.name() + '+' + dt2.name() + ')',
408         dt1.dimensions() + dt2.dimensions(),
409         dt1.value() + dt2.value()
410     );
414 template <class Type>
415 dimensioned<Type> operator-(const dimensioned<Type>& dt)
417     return dimensioned<Type>
418     (
419         '-' + dt.name(),
420         dt.dimensions(),
421         -dt.value()
422     );
426 template <class Type>
427 dimensioned<Type> operator-
429     const dimensioned<Type>& dt1,
430     const dimensioned<Type>& dt2
433     return dimensioned<Type>
434     (
435         '(' + dt1.name() + '-' + dt2.name() + ')',
436         dt1.dimensions() - dt2.dimensions(),
437         dt1.value() - dt2.value()
438     );
442 template <class Type>
443 dimensioned<Type> operator*
445     const dimensioned<scalar>& ds,
446     const dimensioned<Type>& dt
449     return dimensioned<Type>
450     (
451         '(' + ds.name() + '*' + dt.name() + ')',
452         ds.dimensions() * dt.dimensions(),
453         ds.value() * dt.value()
454     );
458 template <class Type>
459 dimensioned<Type> operator/
461     const dimensioned<Type>& dt,
462     const dimensioned<scalar>& ds
465     return dimensioned<Type>
466     (
467         '(' + dt.name() + '|' + ds.name() + ')',
468         dt.dimensions()/ds.dimensions(),
469         dt.value()/ds.value()
470     );
474 // Products
475 // ~~~~~~~~
477 #define PRODUCT_OPERATOR(product, op, opFunc)                                 \
478                                                                               \
479 template<class Type1, class Type2>                                            \
480 dimensioned<typename product<Type1, Type2>::type>                             \
481 operator op(const dimensioned<Type1>& dt1, const dimensioned<Type2>& dt2)     \
482 {                                                                             \
483     return dimensioned<typename product<Type1, Type2>::type>                  \
484     (                                                                         \
485         '(' + dt1.name() + #op + dt2.name() + ')',                            \
486         dt1.dimensions() op dt2.dimensions(),                                 \
487         dt1.value() op dt2.value()                                            \
488     );                                                                        \
489 }                                                                             \
490                                                                               \
491 template<class Type, class Form, class Cmpt, int nCmpt>                       \
492 dimensioned<typename product<Type, Form>::type>                               \
493 operator op                                                                   \
494 (                                                                             \
495     const dimensioned<Type>& dt1,                                             \
496     const VectorSpace<Form,Cmpt,nCmpt>& t2                                    \
497 )                                                                             \
498 {                                                                             \
499     return dimensioned<typename product<Type, Form>::type>                    \
500     (                                                                         \
501         '(' + dt1.name() + #op + name(t2) + ')',                              \
502         dt1.dimensions(),                                                     \
503         dt1.value() op static_cast<const Form&>(t2)                           \
504     );                                                                        \
505 }                                                                             \
506                                                                               \
507 template<class Type, class Form, class Cmpt, int nCmpt>                       \
508 dimensioned<typename product<Form, Type>::type>                               \
509 operator op                                                                   \
510 (                                                                             \
511     const VectorSpace<Form,Cmpt,nCmpt>& t1,                                   \
512     const dimensioned<Type>& dt2                                              \
513 )                                                                             \
514 {                                                                             \
515     return dimensioned<typename product<Form, Type>::type>                    \
516     (                                                                         \
517         '(' + name(t1) + #op + dt2.name() + ')',                              \
518         dt2.dimensions(),                                                     \
519         static_cast<const Form&>(t1) op dt2.value()                           \
520     );                                                                        \
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 // ************************************************************************* //