FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

dimensionedType.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "dimensionedType.H"
00027 #include <OpenFOAM/pTraits.H>
00028 #include <OpenFOAM/dictionary.H>
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 template <class Type>
00038 dimensioned<Type> dimensioned<Type>::lookupOrDefault
00039 (
00040     const word& name,
00041     const dictionary& dict,
00042     const Type& defaultValue,
00043     const dimensionSet& dims
00044 )
00045 {
00046     Type value = dict.lookupOrDefault<Type>(name, defaultValue);
00047     return dimensioned<Type>(name, dims, value);
00048 }
00049 
00050 
00051 template <class Type>
00052 dimensioned<Type> dimensioned<Type>::lookupOrAddToDict
00053 (
00054     const word& name,
00055     dictionary& dict,
00056     const Type& defaultValue,
00057     const dimensionSet& dims
00058 )
00059 {
00060     Type value = dict.lookupOrAddDefault<Type>(name, defaultValue);
00061     return dimensioned<Type>(name, dims, value);
00062 }
00063 
00064 
00065 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00066 
00067 template <class Type>
00068 dimensioned<Type>::dimensioned
00069 (
00070     const word& name,
00071     const dimensionSet& dimSet,
00072     const Type t
00073 )
00074 :
00075     name_(name),
00076     dimensions_(dimSet),
00077     value_(t)
00078 {}
00079 
00080 
00081 template <class Type>
00082 dimensioned<Type>::dimensioned
00083 (
00084     const word& name,
00085     const dimensioned<Type>& dt
00086 )
00087 :
00088     name_(name),
00089     dimensions_(dt.dimensions_),
00090     value_(dt.value_)
00091 {}
00092 
00093 
00094 template <class Type>
00095 dimensioned<Type>::dimensioned
00096 (
00097     Istream& is
00098 )
00099 :
00100     name_(is),
00101     dimensions_(is),
00102     value_(pTraits<Type>(is))
00103 {}
00104 
00105 
00106 template <class Type>
00107 dimensioned<Type>::dimensioned
00108 (
00109     const word& name,
00110     Istream& is
00111 )
00112 :
00113     name_(name),
00114     dimensions_(is),
00115     value_(pTraits<Type>(is))
00116 {}
00117 
00118 
00119 template <class Type>
00120 dimensioned<Type>::dimensioned
00121 (
00122     const word& name,
00123     const dimensionSet& dimSet,
00124     Istream& is
00125 )
00126 :
00127     name_(name),
00128     dimensions_(dimSet),
00129     value_(pTraits<Type>(is))
00130 {}
00131 
00132 
00133 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00134 
00135 template <class Type>
00136 const word& dimensioned<Type>::name() const
00137 {
00138     return name_;
00139 }
00140 
00141 template <class Type>
00142 word& dimensioned<Type>::name()
00143 {
00144     return name_;
00145 }
00146 
00147 
00148 template <class Type>
00149 const dimensionSet& dimensioned<Type>::dimensions() const
00150 {
00151     return dimensions_;
00152 }
00153 
00154 template <class Type>
00155 dimensionSet& dimensioned<Type>::dimensions()
00156 {
00157     return dimensions_;
00158 }
00159 
00160 
00161 template <class Type>
00162 const Type& dimensioned<Type>::value() const
00163 {
00164     return value_;
00165 }
00166 
00167 template <class Type>
00168 Type& dimensioned<Type>::value()
00169 {
00170     return value_;
00171 }
00172 
00173 
00174 template <class Type>
00175 dimensioned<typename dimensioned<Type>::cmptType> dimensioned<Type>::component
00176 (
00177     const direction d
00178 ) const
00179 {
00180     return dimensioned<cmptType>
00181     (
00182         name_ + ".component(" + Foam::name(d) + ')',
00183         dimensions_,
00184         value_.component(d)
00185     );
00186 }
00187 
00188 
00189 template <class Type>
00190 void dimensioned<Type>::replace
00191 (
00192     const direction d,
00193     const dimensioned<typename dimensioned<Type>::cmptType>& dc
00194 )
00195 {
00196     dimensions_ = dc.dimensions();
00197     value_.replace(d, dc.value());
00198 }
00199 
00200 
00201 template <class Type>
00202 bool dimensioned<Type>::readIfPresent(const dictionary& dict)
00203 {
00204     return dict.readIfPresent(name_, value_);
00205 }
00206 
00207 
00208 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00209 
00210 template <class Type>
00211 dimensioned<typename dimensioned<Type>::cmptType> dimensioned<Type>::operator[]
00212 (
00213     const direction d
00214 ) const
00215 {
00216     return component(d);
00217 }
00218 
00219 
00220 template <class Type>
00221 void dimensioned<Type>::operator+=
00222 (
00223     const dimensioned<Type>& dt
00224 )
00225 {
00226     dimensions_ += dt.dimensions_;
00227     value_ += dt.value_;
00228 }
00229 
00230 
00231 template <class Type>
00232 void dimensioned<Type>::operator-=
00233 (
00234     const dimensioned<Type>& dt
00235 )
00236 {
00237     dimensions_ -= dt.dimensions_;
00238     value_ -= dt.value_;
00239 }
00240 
00241 
00242 template <class Type>
00243 void dimensioned<Type>::operator*=
00244 (
00245     const scalar s
00246 )
00247 {
00248     value_ *= s;
00249 }
00250 
00251 
00252 template <class Type>
00253 void dimensioned<Type>::operator/=
00254 (
00255     const scalar s
00256 )
00257 {
00258     value_ /= s;
00259 }
00260 
00261 
00262 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00263 
00264 template<class Type, int r>
00265 dimensioned<typename powProduct<Type, r>::type>
00266 pow(const dimensioned<Type>& dt, typename powProduct<Type, r>::type)
00267 {
00268     return dimensioned<typename powProduct<Type, r>::type>
00269     (
00270         "pow(" + dt.name() + ',' + name(r) + ')',
00271         pow(dt.dimensions(), r),
00272         pow(dt.value(), 2)
00273     );
00274 }
00275 
00276 template<class Type>
00277 dimensioned<typename outerProduct<Type, Type>::type>
00278 sqr(const dimensioned<Type>& dt)
00279 {
00280     return dimensioned<typename outerProduct<Type, Type>::type>
00281     (
00282         "sqr(" + dt.name() + ')',
00283         sqr(dt.dimensions()),
00284         sqr(dt.value())
00285     );
00286 }
00287 
00288 template<class Type>
00289 dimensioned<scalar> magSqr(const dimensioned<Type>& dt)
00290 {
00291     return dimensioned<scalar>
00292     (
00293         "magSqr(" + dt.name() + ')',
00294         magSqr(dt.dimensions()),
00295         magSqr(dt.value())
00296     );
00297 }
00298 
00299 template<class Type>
00300 dimensioned<scalar> mag(const dimensioned<Type>& dt)
00301 {
00302     return dimensioned<scalar>
00303     (
00304         "mag(" + dt.name() + ')',
00305         dt.dimensions(),
00306         mag(dt.value())
00307     );
00308 }
00309 
00310 
00311 template <class Type>
00312 dimensioned<Type> max
00313 (
00314     const dimensioned<Type>& dt1,
00315     const dimensioned<Type>& dt2
00316 )
00317 {
00318     if (dt1.dimensions() != dt2.dimensions())
00319     {
00320         FatalErrorIn("max(const dimensioned<Type>&, const dimensioned<Type>&)")
00321             << "dimensions of arguments are not equal"
00322             << abort(FatalError);
00323     }
00324 
00325     return dimensioned<Type>
00326     (
00327         "max(" + dt1.name() + ',' + dt2.name() + ')',
00328         dt1.dimensions(),
00329         max(dt1.value(), dt2.value())
00330     );
00331 }
00332 
00333 
00334 template <class Type>
00335 dimensioned<Type> min
00336 (
00337     const dimensioned<Type>& dt1,
00338     const dimensioned<Type>& dt2
00339 )
00340 {
00341     if (dt1.dimensions() != dt2.dimensions())
00342     {
00343         FatalErrorIn("min(const dimensioned<Type>&, const dimensioned<Type>&)")
00344             << "dimensions of arguments are not equal"
00345             << abort(FatalError);
00346     }
00347 
00348     return dimensioned<Type>
00349     (
00350         "min(" + dt1.name() + ',' + dt2.name() + ')',
00351         dt1.dimensions(),
00352         min(dt1.value(), dt2.value())
00353     );
00354 }
00355 
00356 
00357 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00358 
00359 template <class Type>
00360 Istream& operator>>(Istream& is, dimensioned<Type>& dt)
00361 {
00362     // do a stream read op for a Type and a dimensions()et
00363     is >> dt.name_ >> dt.dimensions_ >> dt.value_;
00364 
00365     // Check state of Istream
00366     is.check("Istream& operator>>(Istream&, dimensioned<Type>&)");
00367 
00368     return is;
00369 }
00370 
00371 
00372 template <class Type>
00373 Ostream& operator<<(Ostream& os, const dimensioned<Type>& dt)
00374 {
00375     // do a stream write op for a dimensions()et
00376     os  << dt.name() << token::SPACE
00377         << dt.dimensions() << token::SPACE
00378         << dt.value();
00379 
00380     // Check state of Ostream
00381     os.check("Ostream& operator<<(Ostream&, const dimensioned<Type>&)");
00382 
00383     return os;
00384 }
00385 
00386 
00387 // * * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * //
00388 
00389 template <class Type>
00390 bool operator>
00391 (
00392     const dimensioned<Type>& dt1,
00393     const dimensioned<Type>& dt2
00394 )
00395 {
00396     return dt1.value() > dt2.value();
00397 }
00398 
00399 
00400 template <class Type>
00401 bool operator<
00402 (
00403     const dimensioned<Type>& dt1,
00404     const dimensioned<Type>& dt2
00405 )
00406 {
00407     return dt1.value() < dt2.value();
00408 }
00409 
00410 
00411 template <class Type>
00412 dimensioned<Type> operator+
00413 (
00414     const dimensioned<Type>& dt1,
00415     const dimensioned<Type>& dt2
00416 )
00417 {
00418     return dimensioned<Type>
00419     (
00420         '(' + dt1.name() + '+' + dt2.name() + ')',
00421         dt1.dimensions() + dt2.dimensions(),
00422         dt1.value() + dt2.value()
00423     );
00424 }
00425 
00426 
00427 template <class Type>
00428 dimensioned<Type> operator-(const dimensioned<Type>& dt)
00429 {
00430     return dimensioned<Type>
00431     (
00432         '-' + dt.name(),
00433         dt.dimensions(),
00434         -dt.value()
00435     );
00436 }
00437 
00438 
00439 template <class Type>
00440 dimensioned<Type> operator-
00441 (
00442     const dimensioned<Type>& dt1,
00443     const dimensioned<Type>& dt2
00444 )
00445 {
00446     return dimensioned<Type>
00447     (
00448         '(' + dt1.name() + '-' + dt2.name() + ')',
00449         dt1.dimensions() - dt2.dimensions(),
00450         dt1.value() - dt2.value()
00451     );
00452 }
00453 
00454 
00455 template <class Type>
00456 dimensioned<Type> operator*
00457 (
00458     const dimensioned<scalar>& ds,
00459     const dimensioned<Type>& dt
00460 )
00461 {
00462     return dimensioned<Type>
00463     (
00464         '(' + ds.name() + '*' + dt.name() + ')',
00465         ds.dimensions() * dt.dimensions(),
00466         ds.value() * dt.value()
00467     );
00468 }
00469 
00470 
00471 template <class Type>
00472 dimensioned<Type> operator/
00473 (
00474     const dimensioned<Type>& dt,
00475     const dimensioned<scalar>& ds
00476 )
00477 {
00478     return dimensioned<Type>
00479     (
00480         '(' + dt.name() + '|' + ds.name() + ')',
00481         dt.dimensions()/ds.dimensions(),
00482         dt.value()/ds.value()
00483     );
00484 }
00485 
00486 
00487 // Products
00488 // ~~~~~~~~
00489 
00490 #define PRODUCT_OPERATOR(product, op, opFunc)                                 \
00491                                                                               \
00492 template<class Type1, class Type2>                                            \
00493 dimensioned<typename product<Type1, Type2>::type>                             \
00494 operator op(const dimensioned<Type1>& dt1, const dimensioned<Type2>& dt2)     \
00495 {                                                                             \
00496     return dimensioned<typename product<Type1, Type2>::type>                  \
00497     (                                                                         \
00498         '(' + dt1.name() + #op + dt2.name() + ')',                            \
00499         dt1.dimensions() op dt2.dimensions(),                                 \
00500         dt1.value() op dt2.value()                                            \
00501     );                                                                        \
00502 }                                                                             \
00503                                                                               \
00504 template<class Type, class Form, class Cmpt, int nCmpt>                       \
00505 dimensioned<typename product<Type, Form>::type>                               \
00506 operator op                                                                   \
00507 (                                                                             \
00508     const dimensioned<Type>& dt1,                                             \
00509     const VectorSpace<Form,Cmpt,nCmpt>& t2                                    \
00510 )                                                                             \
00511 {                                                                             \
00512     return dimensioned<typename product<Type, Form>::type>                    \
00513     (                                                                         \
00514         '(' + dt1.name() + #op + name(t2) + ')',                              \
00515         dt1.dimensions(),                                                     \
00516         dt1.value() op static_cast<const Form&>(t2)                           \
00517     );                                                                        \
00518 }                                                                             \
00519                                                                               \
00520 template<class Type, class Form, class Cmpt, int nCmpt>                       \
00521 dimensioned<typename product<Form, Type>::type>                               \
00522 operator op                                                                   \
00523 (                                                                             \
00524     const VectorSpace<Form,Cmpt,nCmpt>& t1,                                   \
00525     const dimensioned<Type>& dt2                                              \
00526 )                                                                             \
00527 {                                                                             \
00528     return dimensioned<typename product<Form, Type>::type>                    \
00529     (                                                                         \
00530         '(' + name(t1) + #op + dt2.name() + ')',                              \
00531         dt2.dimensions(),                                                     \
00532         static_cast<const Form&>(t1) op dt2.value()                           \
00533     );                                                                        \
00534 }
00535 
00536 
00537 PRODUCT_OPERATOR(outerProduct, *, outer)
00538 PRODUCT_OPERATOR(crossProduct, ^, cross)
00539 PRODUCT_OPERATOR(innerProduct, &, dot)
00540 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
00541 
00542 #undef PRODUCT_OPERATOR
00543 
00544 
00545 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00546 
00547 } // End namespace Foam
00548 
00549 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines