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

FieldField.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 Description
00025     Generic fieldField type.
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include <OpenFOAM/FieldField.H>
00030 
00031 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035 
00036 #ifdef FULLDEBUG
00037 
00038 template<template<class> class Field, class Type1, class Type2>
00039 void checkFields
00040 (
00041     const FieldField<Field, Type1>& f1,
00042     const FieldField<Field, Type2>& f2,
00043     const char* op
00044 )
00045 {
00046     if (f1.size() != f2.size())
00047     {
00048         FatalErrorIn
00049         (
00050             "checkFields(const FieldField<Field, Type1>&, "
00051             "const FieldField<Field, Type2>&, const char* op)"
00052         )   << "    incompatible fields"
00053             << " FieldField<" << pTraits<Type1>::typeName
00054             << "> f1(" << f1.size() << ')'
00055             << " and FieldField<" << pTraits<Type2>::typeName
00056             << "> f2(" << f2.size() << ')'
00057             << endl << " for operation " << op
00058             << abort(FatalError);
00059     }
00060 }
00061 
00062 template<template<class> class Field, class Type1, class Type2, class Type3>
00063 void checkFields
00064 (
00065     const FieldField<Field, Type1>& f1,
00066     const FieldField<Field, Type2>& f2,
00067     const FieldField<Field, Type3>& f3,
00068     const char* op
00069 )
00070 {
00071     if (f1.size() != f2.size() || f1.size() != f3.size())
00072     {
00073         FatalErrorIn
00074         (
00075             "checkFields(const FieldField<Field, Type1>&, "
00076             "const FieldField<Field, Type2>&, "
00077             "const FieldField<Field, Type3>&, "
00078             "const char* op)"
00079         )   << "    incompatible fields"
00080             << " FieldField<" << pTraits<Type1>::typeName
00081             << "> f1(" << f1.size() << ')'
00082             << ", FieldField<" <<pTraits<Type2>::typeName
00083             << "> f2(" << f2.size() << ')'
00084             << " and FieldField<"<<pTraits<Type3>::typeName
00085             << "> f3("<<f3.size() << ')'
00086             << endl << "    for operation " << op
00087             << abort(FatalError);
00088     }
00089 }
00090 
00091 #else
00092 
00093 template<template<class> class Field, class Type1, class Type2>
00094 void checkFields
00095 (
00096     const FieldField<Field, Type1>&,
00097     const FieldField<Field, Type2>&,
00098     const char* op
00099 )
00100 {}
00101 
00102 template<template<class> class Field, class Type1, class Type2, class Type3>
00103 void checkFields
00104 (
00105     const FieldField<Field, Type1>&,
00106     const FieldField<Field, Type2>&,
00107     const FieldField<Field, Type3>&,
00108     const char* op
00109 )
00110 {}
00111 
00112 #endif
00113 
00114 
00115 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00116 
00117 template<template<class> class Field, class Type>
00118 FieldField<Field, Type>::FieldField()
00119 :
00120     PtrList<Field<Type> >()
00121 {}
00122 
00123 
00124 template<template<class> class Field, class Type>
00125 FieldField<Field, Type>::FieldField(const label size)
00126 :
00127     PtrList<Field<Type> >(size)
00128 {}
00129 
00130 
00131 template<template<class> class Field, class Type>
00132 FieldField<Field, Type>::FieldField
00133 (
00134     const word& type,
00135     const FieldField<Field, Type>& ff
00136 )
00137 :
00138     PtrList<Field<Type> >(ff.size())
00139 {
00140     forAll(*this, i)
00141     {
00142         set(i, Field<Type>::New(type, ff[i]));
00143     }
00144 }
00145 
00146 
00147 template<template<class> class Field, class Type>
00148 FieldField<Field, Type>::FieldField(const FieldField<Field, Type>& f)
00149 :
00150     refCount(),
00151     PtrList<Field<Type> >(f)
00152 {}
00153 
00154 
00155 template<template<class> class Field, class Type>
00156 FieldField<Field, Type>::FieldField(FieldField<Field, Type>& f, bool reUse)
00157 :
00158     refCount(),
00159     PtrList<Field<Type> >(f, reUse)
00160 {}
00161 
00162 
00163 template<template<class> class Field, class Type>
00164 FieldField<Field, Type>::FieldField(const PtrList<Field<Type> >& tl)
00165 :
00166     PtrList<Field<Type> >(tl)
00167 {}
00168 
00169 
00170 // Construct as copy of tmp<FieldField>
00171 #ifdef ConstructFromTmp
00172 template<template<class> class Field, class Type>
00173 FieldField<Field, Type>::FieldField(const tmp<FieldField<Field, Type> >& tf)
00174 :
00175     PtrList<Field<Type> >
00176     (
00177         const_cast<FieldField<Field, Type>&>(tf()),
00178         tf.isTmp()
00179     )
00180 {
00181     const_cast<FieldField<Field, Type>&>(tf()).resetRefCount();
00182 }
00183 #endif
00184 
00185 
00186 template<template<class> class Field, class Type>
00187 FieldField<Field, Type>::FieldField(Istream& is)
00188 :
00189     PtrList<Field<Type> >(is)
00190 {}
00191 
00192 
00193 template<template<class> class Field, class Type>
00194 tmp<FieldField<Field, Type> > FieldField<Field, Type>::clone() const
00195 {
00196     return tmp<FieldField<Field, Type> >(new FieldField<Field, Type>(*this));
00197 }
00198 
00199 
00200 #ifndef __INTEL_COMPILER
00201 template<template<class> class Field, class Type>
00202 template<class Type2>
00203 tmp<FieldField<Field, Type> > FieldField<Field, Type>::NewCalculatedType
00204 (
00205     const FieldField<Field, Type2>& ff
00206 )
00207 {
00208     FieldField<Field, Type>* nffPtr
00209     (
00210         new FieldField<Field, Type>(ff.size())
00211     );
00212 
00213     forAll(*nffPtr, i)
00214     { 
00215         nffPtr->set(i, Field<Type>::NewCalculatedType(ff[i]).ptr());
00216     }
00217 
00218     return tmp<FieldField<Field, Type> >(nffPtr);
00219 }
00220 #endif
00221 
00222 
00223 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00224 
00225 template<template<class> class Field, class Type>
00226 void FieldField<Field, Type>::negate()
00227 {
00228     forAll(*this, i)
00229     {
00230         this->operator[](i).negate();
00231     }
00232 }
00233 
00234 
00235 template<template<class> class Field, class Type>
00236 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> >
00237 FieldField<Field, Type>::component
00238 (
00239     const direction d
00240 ) const
00241 {
00242     tmp<FieldField<Field, cmptType> > Component
00243     (
00244         FieldField<Field, typename FieldField<Field, Type>::cmptType>::
00245             NewCalculatedType(*this)
00246     );
00247 
00248     ::Foam::component(Component(), *this, d);
00249 
00250     return Component;
00251 }
00252 
00253 
00254 template<template<class> class Field, class Type>
00255 void FieldField<Field, Type>::replace
00256 (
00257     const direction d,
00258     const FieldField<Field, cmptType>& sf
00259 )
00260 {
00261     forAll(*this, i)
00262     {
00263         this->operator[](i).replace(d, sf[i]);
00264     }
00265 }
00266 
00267 
00268 template<template<class> class Field, class Type>
00269 void FieldField<Field, Type>::replace
00270 (
00271     const direction d,
00272     const cmptType& s
00273 )
00274 {
00275     forAll(*this, i)
00276     {
00277         this->operator[](i).replace(d, s);
00278     }
00279 }
00280 
00281 
00282 template<template<class> class Field, class Type>
00283 tmp<FieldField<Field, Type> > FieldField<Field, Type>::T() const
00284 {
00285     tmp<FieldField<Field, Type> > transpose
00286     (
00287         FieldField<Field, Type>::NewCalculatedType(*this)
00288     );
00289 
00290     ::Foam::T(transpose(), *this);
00291     return transpose;
00292 }
00293 
00294 
00295 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00296 
00297 template<template<class> class Field, class Type>
00298 void FieldField<Field, Type>::operator=(const FieldField<Field, Type>& f)
00299 {
00300     if (this == &f)
00301     {
00302         FatalErrorIn
00303         (
00304             "FieldField<Field, Type>::"
00305             "operator=(const FieldField<Field, Type>&)"
00306         )   << "attempted assignment to self"
00307             << abort(FatalError);
00308     }
00309 
00310     forAll(*this, i)
00311     {
00312         this->operator[](i) = f[i];
00313     }
00314 }
00315 
00316 
00317 template<template<class> class Field, class Type>
00318 void FieldField<Field, Type>::operator=(const tmp<FieldField>& tf)
00319 {
00320     if (this == &(tf()))
00321     {
00322         FatalErrorIn
00323         (
00324             "FieldField<Field, Type>::operator=(const tmp<FieldField>&)"
00325         )   << "attempted assignment to self"
00326             << abort(FatalError);
00327     }
00328 
00329     // This is dodgy stuff, don't try this at home.
00330     FieldField* fieldPtr = tf.ptr();
00331     PtrList<Field<Type> >::transfer(*fieldPtr);
00332     delete fieldPtr;
00333 }
00334 
00335 
00336 template<template<class> class Field, class Type>
00337 void FieldField<Field, Type>::operator=(const Type& t)
00338 {
00339     forAll(*this, i)
00340     {
00341         this->operator[](i) = t;
00342     }
00343 }
00344 
00345 
00346 #define COMPUTED_ASSIGNMENT(TYPE, op)                                         \
00347                                                                               \
00348 template<template<class> class Field, class Type>                             \
00349 void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f)   \
00350 {                                                                             \
00351     forAll(*this, i)                                                          \
00352     {                                                                         \
00353         this->operator[](i) op f[i];                                          \
00354     }                                                                         \
00355 }                                                                             \
00356                                                                               \
00357 template<template<class> class Field, class Type>                             \
00358 void FieldField<Field, Type>::operator op                                     \
00359 (                                                                             \
00360     const tmp<FieldField<Field, TYPE> >& tf                                   \
00361 )                                                                             \
00362 {                                                                             \
00363     operator op(tf());                                                        \
00364     tf.clear();                                                               \
00365 }                                                                             \
00366                                                                               \
00367 template<template<class> class Field, class Type>                             \
00368 void FieldField<Field, Type>::operator op(const TYPE& t)                      \
00369 {                                                                             \
00370     forAll(*this, i)                                                          \
00371     {                                                                         \
00372         this->operator[](i) op t;                                             \
00373     }                                                                         \
00374 }
00375 
00376 COMPUTED_ASSIGNMENT(Type, +=)
00377 COMPUTED_ASSIGNMENT(Type, -=)
00378 COMPUTED_ASSIGNMENT(scalar, *=)
00379 COMPUTED_ASSIGNMENT(scalar, /=)
00380 
00381 #undef COMPUTED_ASSIGNMENT
00382 
00383 
00384 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
00385 
00386 template<template<class> class Field, class Type>
00387 Ostream& operator<<(Ostream& os, const FieldField<Field, Type>& f)
00388 {
00389     os << static_cast<const PtrList<Field<Type> >&>(f);
00390     return os;
00391 }
00392 
00393 
00394 template<template<class> class Field, class Type>
00395 Ostream& operator<<(Ostream& os, const tmp<FieldField<Field, Type> >& tf)
00396 {
00397     os << tf();
00398     tf.clear();
00399     return os;
00400 }
00401 
00402 
00403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00404 
00405 } // End namespace Foam
00406 
00407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00408 
00409 #   include <OpenFOAM/FieldFieldFunctions.C>
00410 
00411 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines