OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

FieldM.H

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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     High performance macro functions for Field<Type> algebra.  These expand
00027     using either array element access (for vector machines) or pointer
00028     dereferencing for scalar machines as appropriate.
00029 
00030 \*---------------------------------------------------------------------------*/
00031 
00032 #ifndef FieldM_H
00033 #define FieldM_H
00034 
00035 #include "error.H"
00036 #include "ListLoopM.H"
00037 
00038 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00039 
00040 namespace Foam
00041 {
00042 
00043 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00044 
00045 #ifdef FULLDEBUG
00046 
00047 template<class Type1, class Type2>
00048 void checkFields
00049 (
00050     const UList<Type1>& f1,
00051     const UList<Type2>& f2,
00052     const char* op
00053 )
00054 {
00055     if (f1.size() != f2.size())
00056     {
00057         FatalErrorIn
00058         (
00059             "checkFields(const UList<Type1>&, "
00060             "const UList<Type2>&, const char*)"
00061         )   << "    incompatible fields"
00062             << " Field<"<<pTraits<Type1>::typeName<<"> f1("<<f1.size()<<')'
00063             << " and Field<"<<pTraits<Type2>::typeName<<"> f2("<<f2.size()<<')'
00064             << endl << " for operation " << op
00065             << abort(FatalError);
00066     }
00067 }
00068 
00069 template<class Type1, class Type2, class Type3>
00070 void checkFields
00071 (
00072     const UList<Type1>& f1,
00073     const UList<Type2>& f2,
00074     const UList<Type3>& f3,
00075     const char* op
00076 )
00077 {
00078     if (f1.size() != f2.size() || f1.size() != f3.size())
00079     {
00080         FatalErrorIn
00081         (
00082             "checkFields(const UList<Type1>&, "
00083             "const UList<Type2>&, const UList<Type3>&, "
00084             "const char*)"
00085         )   << "    incompatible fields"
00086             << " Field<"<<pTraits<Type1>::typeName<<"> f1("<<f1.size()<<')'
00087             << ", Field<"<<pTraits<Type2>::typeName<<"> f2("<<f2.size()<<')'
00088             << " and Field<"<<pTraits<Type3>::typeName<<"> f3("<<f3.size()<<')'
00089             << endl << "    for operation " << op
00090             << abort(FatalError);
00091     }
00092 }
00093 
00094 #else
00095 
00096 template<class Type1, class Type2>
00097 void checkFields
00098 (
00099     const UList<Type1>&,
00100     const UList<Type2>&,
00101     const char*
00102 )
00103 {}
00104 
00105 template<class Type1, class Type2, class Type3>
00106 void checkFields
00107 (
00108     const UList<Type1>&,
00109     const UList<Type2>&,
00110     const UList<Type3>&,
00111     const char*
00112 )
00113 {}
00114 
00115 #endif
00116 
00117 
00118 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00119 
00120 // member function : this f1 OP fUNC f2
00121 
00122 #define TFOR_ALL_F_OP_FUNC_F(typeF1, f1, OP, FUNC, typeF2, f2)              \
00123                                                                             \
00124     /* check the two fields have same Field<Type> mesh */                   \
00125     checkFields(f1, f2, "f1 " #OP " " #FUNC "(f2)");                        \
00126                                                                             \
00127     /* set access to f1, f2 and f3 at end of each field */                  \
00128     List_ACCESS(typeF1, f1, f1P);                                           \
00129     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00130                                                                             \
00131     /* loop through fields performing f1 OP1 f2 OP2 f3 */                   \
00132     List_FOR_ALL(f1, i)                                                     \
00133         List_ELEM(f1, f1P, i) OP FUNC(List_ELEM(f2, f2P, i));               \
00134     List_END_FOR_ALL                                                        \
00135 
00136 
00137 #define TFOR_ALL_F_OP_F_FUNC(typeF1, f1, OP, typeF2, f2, FUNC)              \
00138                                                                             \
00139     /* check the two fields have same Field<Type> mesh */                   \
00140     checkFields(f1, f2, "f1 " #OP " f2" #FUNC);                             \
00141                                                                             \
00142     /* set access to f1, f2 and f3 at end of each field */                  \
00143     List_ACCESS(typeF1, f1, f1P);                                           \
00144     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00145                                                                             \
00146     /* loop through fields performing f1 OP1 f2 OP2 f3 */                   \
00147     List_FOR_ALL(f1, i)                                                     \
00148         List_ELEM(f1, f1P, i) OP List_ELEM(f2, f2P, i).FUNC();              \
00149     List_END_FOR_ALL                                                        \
00150 
00151 
00152 // member function : this field f1 OP fUNC f2, f3
00153 
00154 #define TFOR_ALL_F_OP_FUNC_F_F(typeF1, f1, OP, FUNC, typeF2, f2, typeF3, f3)\
00155                                                                             \
00156     /* check the three fields have same Field<Type> mesh */                 \
00157     checkFields(f1, f2, f3, "f1 " #OP " " #FUNC "(f2, f3)");                \
00158                                                                             \
00159     /* set access to f1, f2 and f3 at end of each field */                  \
00160     List_ACCESS(typeF1, f1, f1P);                                           \
00161     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00162     List_CONST_ACCESS(typeF3, f3, f3P);                                     \
00163                                                                             \
00164     /* loop through fields performing f1 OP1 f2 OP2 f3 */                   \
00165     List_FOR_ALL(f1, i)                                                     \
00166         List_ELEM(f1, f1P, i)                                               \
00167         OP FUNC(List_ELEM(f2, f2P, i), List_ELEM(f3, f3P, i));              \
00168     List_END_FOR_ALL                                                        \
00169 
00170 
00171 // member function : this field f1 OP fUNC f2, f3
00172 
00173 #define TFOR_ALL_S_OP_FUNC_F_F(typeS, s, OP, FUNC, typeF1, f1, typeF2, f2)  \
00174                                                                             \
00175     /* check the two fields have same Field<Type> mesh */                   \
00176     checkFields(f1, f2, "s " #OP " " #FUNC "(f1, f2)");                     \
00177                                                                             \
00178     /* set access to f1 and f2 at end of each field */                      \
00179     List_CONST_ACCESS(typeF1, f1, f1P);                                     \
00180     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00181                                                                             \
00182     /* loop through fields performing s OP FUNC(f1, f2) */                  \
00183     List_FOR_ALL(f1, i)                                                     \
00184         (s) OP FUNC(List_ELEM(f1, f1P, i), List_ELEM(f2, f2P, i));          \
00185     List_END_FOR_ALL                                                        \
00186 
00187 
00188 // member function : this f1 OP fUNC f2, s
00189 
00190 #define TFOR_ALL_F_OP_FUNC_F_S(typeF1, f1, OP, FUNC, typeF2, f2, typeS, s)  \
00191                                                                             \
00192     /* check the two fields have same Field<Type> mesh */                   \
00193     checkFields(f1, f2, "f1 " #OP " " #FUNC "(f2, s)");                     \
00194                                                                             \
00195     /* set access to f1, f2 and f3 at end of each field */                  \
00196     List_ACCESS(typeF1, f1, f1P);                                           \
00197     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00198                                                                             \
00199     /* loop through fields performing f1 OP1 f2 OP2 f3 */                   \
00200     List_FOR_ALL(f1, i)                                                     \
00201         List_ELEM(f1, f1P, i) OP FUNC(List_ELEM(f2, f2P, i), (s));          \
00202     List_END_FOR_ALL
00203 
00204 
00205 // member function : s1 OP fUNC f, s2
00206 
00207 #define TFOR_ALL_S_OP_FUNC_F_S(typeS1, s1, OP, FUNC, typeF, f, typeS2, s2)  \
00208                                                                             \
00209     /* set access to f at end of field */                                   \
00210     List_CONST_ACCESS(typeF, f, fP);                                        \
00211                                                                             \
00212     /* loop through fields performing f1 OP1 f2 OP2 f3 */                   \
00213     List_FOR_ALL(f, i)                                                      \
00214         (s1) OP FUNC(List_ELEM(f, fP, i), (s2));                            \
00215     List_END_FOR_ALL                                                        \
00216 
00217 
00218 // member function : this f1 OP fUNC s, f2
00219 
00220 #define TFOR_ALL_F_OP_FUNC_S_F(typeF1, f1, OP, FUNC, typeS, s, typeF2, f2)  \
00221                                                                             \
00222     /* check the two fields have same Field<Type> mesh */                   \
00223     checkFields(f1, f2, "f1 " #OP " " #FUNC "(s, f2)");                     \
00224                                                                             \
00225     /* set access to f1, f2 and f3 at end of each field */                  \
00226     List_ACCESS(typeF1, f1, f1P);                                           \
00227     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00228                                                                             \
00229     /* loop through fields performing f1 OP1 f2 OP2 f3 */                   \
00230     List_FOR_ALL(f1, i)                                                     \
00231         List_ELEM(f1, f1P, i) OP FUNC((s), List_ELEM(f2, f2P, i));          \
00232     List_END_FOR_ALL                                                        \
00233 
00234 
00235 // member function : this f1 OP fUNC s, f2
00236 
00237 #define TFOR_ALL_F_OP_FUNC_S_S(typeF1, f1, OP, FUNC, typeS1, s1, typeS2, s2)\
00238                                                                             \
00239     /* set access to f1 at end of field */                                  \
00240     List_ACCESS(typeF1, f1, f1P);                                           \
00241                                                                             \
00242     /* loop through fields performing f1 OP1 FUNC(s1, s2) */                \
00243     List_FOR_ALL(f1, i)                                                     \
00244         List_ELEM(f1, f1P, i) OP FUNC((s1), (s2));                          \
00245     List_END_FOR_ALL                                                        \
00246 
00247 
00248 // member function : this f1 OP1 f2 OP2 FUNC s
00249 
00250 #define TFOR_ALL_F_OP_F_FUNC_S(typeF1, f1, OP, typeF2, f2, FUNC, typeS, s)  \
00251                                                                             \
00252     /* check the two fields have same Field<Type> mesh */                   \
00253     checkFields(f1, f2, "f1 " #OP " f2 " #FUNC "(s)");                      \
00254                                                                             \
00255     /* set access to f1, f2 and f3 at end of each field */                  \
00256     List_ACCESS(typeF1, f1, f1P);                                           \
00257     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00258                                                                             \
00259     /* loop through fields performing f1 OP1 f2 OP2 f3 */                   \
00260     List_FOR_ALL(f1, i)                                                     \
00261         List_ELEM(f1, f1P, i) OP List_ELEM(f2, f2P, i) FUNC((s));           \
00262     List_END_FOR_ALL                                                        \
00263 
00264 
00265 // define high performance macro functions for Field<Type> operations
00266 
00267 // member operator : this field f1 OP1 f2 OP2 f3
00268 
00269 #define TFOR_ALL_F_OP_F_OP_F(typeF1, f1, OP1, typeF2, f2, OP2, typeF3, f3)  \
00270                                                                             \
00271     /* check the three fields have same Field<Type> mesh */                 \
00272     checkFields(f1, f2, f3, "f1 " #OP1 " f2 " #OP2 " f3");                  \
00273                                                                             \
00274     /* set access to f1, f2 and f3 at end of each field */                  \
00275     List_ACCESS(typeF1, f1, f1P);                                           \
00276     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00277     List_CONST_ACCESS(typeF3, f3, f3P);                                     \
00278                                                                             \
00279     /* loop through fields performing f1 OP1 f2 OP2 f3 */                   \
00280     List_FOR_ALL(f1, i)                                                     \
00281         List_ELEM(f1, f1P, i) OP1 List_ELEM(f2, f2P, i)                     \
00282                               OP2 List_ELEM(f3, f3P, i);                    \
00283     List_END_FOR_ALL                                                        \
00284 
00285 
00286 // member operator : this field f1 OP1 s OP2 f2
00287 
00288 #define TFOR_ALL_F_OP_S_OP_F(typeF1, f1, OP1, typeS, s, OP2, typeF2, f2)    \
00289                                                                             \
00290     /* check the two fields have same Field<Type> mesh */                   \
00291     checkFields(f1, f2, "f1 " #OP1 " s " #OP2 " f2");                       \
00292                                                                             \
00293     /* set access to f1 and f2 at end of each field */                      \
00294     List_ACCESS(typeF1, f1, f1P);                                           \
00295     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00296                                                                             \
00297     /* loop through fields performing f1 OP1 s OP2 f2 */                    \
00298     List_FOR_ALL(f1, i)                                                     \
00299         List_ELEM(f1, f1P, i) OP1 (s) OP2 List_ELEM(f2, f2P, i);            \
00300     List_END_FOR_ALL                                                        \
00301 
00302 
00303 // member operator : this field f1 OP1 f2 OP2 s
00304 
00305 #define TFOR_ALL_F_OP_F_OP_S(typeF1, f1, OP1, typeF2, f2, OP2, typeS, s)    \
00306                                                                             \
00307     /* check the two fields have same Field<Type> mesh */                   \
00308     checkFields(f1, f2, "f1 " #OP1 " f2 " #OP2 " s");                       \
00309                                                                             \
00310     /* set access to f1 and f2 at end of each field */                      \
00311     List_ACCESS(typeF1, f1, f1P);                                           \
00312     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00313                                                                             \
00314     /* loop through fields performing f1 OP1 s OP2 f2 */                    \
00315     List_FOR_ALL(f1, i)                                                     \
00316         List_ELEM(f1, f1P, i) OP1 List_ELEM(f2, f2P, i) OP2 (s);            \
00317     List_END_FOR_ALL                                                        \
00318 
00319 
00320 // member operator : this field f1 OP f2
00321 
00322 #define TFOR_ALL_F_OP_F(typeF1, f1, OP, typeF2, f2)                         \
00323                                                                             \
00324     /* check the two fields have same Field<Type> mesh */                   \
00325     checkFields(f1, f2, "f1 " #OP " f2");                                   \
00326                                                                             \
00327     /* set pointer to f1P at end of f1 and */                               \
00328     /* f2.p at end of f2 */                                                 \
00329     List_ACCESS(typeF1, f1, f1P);                                           \
00330     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00331                                                                             \
00332     /* loop through fields performing f1 OP f2 */                           \
00333     List_FOR_ALL(f1, i)                                                     \
00334         List_ELEM(f1, f1P, i) OP List_ELEM(f2, f2P, i);                     \
00335     List_END_FOR_ALL                                                        \
00336 
00337 // member operator : this field f1 OP1 OP2 f2
00338 
00339 #define TFOR_ALL_F_OP_OP_F(typeF1, f1, OP1, OP2, typeF2, f2)                \
00340                                                                             \
00341     /* check the two fields have same Field<Type> mesh */                   \
00342     checkFields(f1, f2, #OP1 " " #OP2 " f2");                               \
00343                                                                             \
00344     /* set pointer to f1P at end of f1 and */                               \
00345     /* f2.p at end of f2 */                                                 \
00346     List_ACCESS(typeF1, f1, f1P);                                           \
00347     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00348                                                                             \
00349     /* loop through fields performing f1 OP1 OP2 f2 */                      \
00350     List_FOR_ALL(f1, i)                                                     \
00351         List_ELEM(f1, f1P, i) OP1 OP2 List_ELEM(f2, f2P, i);                \
00352     List_END_FOR_ALL                                                        \
00353 
00354 
00355 // member operator : this field f OP s
00356 
00357 #define TFOR_ALL_F_OP_S(typeF, f, OP, typeS, s)                             \
00358                                                                             \
00359     /* set access to f at end of field */                                   \
00360     List_ACCESS(typeF, f, fP);                                              \
00361                                                                             \
00362     /* loop through field performing f OP s */                              \
00363     List_FOR_ALL(f, i)                                                      \
00364         List_ELEM(f, fP, i) OP (s);                                         \
00365     List_END_FOR_ALL                                                        \
00366 
00367 
00368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00369 // define high performance macro functions for Field<Type> friend functions
00370 
00371 // friend operator function : s OP f, allocates storage for s
00372 
00373 #define TFOR_ALL_S_OP_F(typeS, s, OP, typeF, f)                             \
00374                                                                             \
00375     /* set access to f at end of field */                                   \
00376     List_CONST_ACCESS(typeF, f, fP);                                        \
00377                                                                             \
00378     /* loop through field performing s OP f */                              \
00379     List_FOR_ALL(f, i)                                                      \
00380         (s) OP List_ELEM(f, fP, i);                                         \
00381     List_END_FOR_ALL
00382 
00383 
00384 // friend operator function : s OP1 f1 OP2 f2, allocates storage for s
00385 
00386 #define TFOR_ALL_S_OP_F_OP_F(typeS, s, OP1, typeF1, f1, OP2, typeF2, f2)    \
00387                                                                             \
00388     /* set access to f1 and f2 at end of each field */                      \
00389     List_CONST_ACCESS(typeF1, f1, f1P);                                     \
00390     List_CONST_ACCESS(typeF2, f2, f2P);                                     \
00391                                                                             \
00392     /* loop through field performing s OP f */                              \
00393     List_FOR_ALL(f1, i)                                                     \
00394         (s) OP1 List_ELEM(f1, f1P, i) OP2 List_ELEM(f2, f2P, i);            \
00395     List_END_FOR_ALL
00396 
00397 
00398 // friend operator function : s OP FUNC(f), allocates storage for s
00399 
00400 #define TFOR_ALL_S_OP_FUNC_F(typeS, s, OP, FUNC, typeF, f)                  \
00401                                                                             \
00402     /* set access to f at end of field */                                   \
00403     List_CONST_ACCESS(typeF, f, fP);                                        \
00404                                                                             \
00405     /* loop through field performing s OP f */                              \
00406     List_FOR_ALL(f, i)                                                      \
00407         (s) OP FUNC(List_ELEM(f, fP, i));                                   \
00408     List_END_FOR_ALL
00409 
00410 
00411 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00412 
00413 } // End namespace Foam
00414 
00415 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00416 
00417 #endif
00418 
00419 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd