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

fieldAverageTemplates.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 <fieldFunctionObjects/fieldAverageItem.H>
00027 #include <finiteVolume/volFields.H>
00028 #include <OpenFOAM/OFstream.H>
00029 
00030 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00031 
00032 template<class Type>
00033 void Foam::fieldAverage::addMeanField
00034 (
00035     const label fieldI,
00036     wordList& meanFieldList
00037 ) const
00038 {
00039     if (faItems_[fieldI].mean())
00040     {
00041         typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
00042 
00043         const word& fieldName = faItems_[fieldI].fieldName();
00044 
00045         const word meanFieldName = fieldName + EXT_MEAN;
00046 
00047         Info<< "Reading/calculating field " << meanFieldName << nl << endl;
00048 
00049         if (obr_.foundObject<fieldType>(meanFieldName))
00050         {
00051             meanFieldList[fieldI] = meanFieldName;
00052         }
00053         else if (obr_.found(meanFieldName))
00054         {
00055             Info<< "Cannot allocate average field " << meanFieldName
00056                 << " since an object with that name already exists."
00057                 << " Disabling averaging." << nl << endl;
00058             meanFieldList[fieldI] = word::null;
00059         }
00060         else
00061         {
00062             const fieldType& baseField =
00063                 obr_.lookupObject<fieldType>(fieldName);
00064 
00065             // Store on registry
00066             obr_.store
00067             (
00068                 new fieldType
00069                 (
00070                     IOobject
00071                     (
00072                         meanFieldName,
00073                         obr_.time().timeName(),
00074                         obr_,
00075                         IOobject::READ_IF_PRESENT,
00076                         IOobject::NO_WRITE
00077                     ),
00078                     1*baseField
00079                 )
00080             );
00081 
00082             meanFieldList[fieldI] = meanFieldName;
00083         }
00084     }
00085 }
00086 
00087 
00088 template<class Type1, class Type2>
00089 void Foam::fieldAverage::addPrime2MeanField
00090 (
00091     const label fieldI,
00092     const wordList& meanFieldList,
00093     wordList& prime2MeanFieldList
00094 ) const
00095 {
00096     if (faItems_[fieldI].mean() && meanFieldList[fieldI].size())
00097     {
00098         typedef GeometricField<Type1, fvPatchField, volMesh> fieldType1;
00099         typedef GeometricField<Type2, fvPatchField, volMesh> fieldType2;
00100 
00101         const word& fieldName = faItems_[fieldI].fieldName();
00102 
00103         const word meanFieldName = fieldName + EXT_PRIME2MEAN;
00104         Info<< "Reading/calculating field " << meanFieldName << nl << endl;
00105 
00106         if (obr_.foundObject<fieldType2>(meanFieldName))
00107         {
00108             prime2MeanFieldList[fieldI] = meanFieldName;
00109         }
00110         else if (obr_.found(meanFieldName))
00111         {
00112             Info<< "Cannot allocate average field " << meanFieldName
00113                 << " since an object with that name already exists."
00114                 << " Disabling averaging." << nl << endl;
00115             prime2MeanFieldList[fieldI] = word::null;
00116         }
00117         else
00118         {
00119             const fieldType1& baseField =
00120                 obr_.lookupObject<fieldType1>(fieldName);
00121             const fieldType1& meanField =
00122                 obr_.lookupObject<fieldType1>(meanFieldList[fieldI]);
00123 
00124             obr_.store
00125             (
00126                 new fieldType2
00127                 (
00128                     IOobject
00129                     (
00130                         meanFieldName,
00131                         obr_.time().timeName(),
00132                         obr_,
00133                         IOobject::READ_IF_PRESENT,
00134                         IOobject::NO_WRITE
00135                     ),
00136                     sqr(baseField) - sqr(meanField)
00137                 )
00138             );
00139 
00140             prime2MeanFieldList[fieldI] = meanFieldName;
00141         }
00142     }
00143 }
00144 
00145 
00146 template<class Type>
00147 void Foam::fieldAverage::calculateMeanFields(const wordList& meanFieldList)
00148 const
00149 {
00150     typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
00151 
00152     const scalar dt = obr_.time().deltaT().value();
00153 
00154     forAll(faItems_, i)
00155     {
00156         if (faItems_[i].mean() && meanFieldList[i].size())
00157         {
00158             const word& fieldName = faItems_[i].fieldName();
00159             const fieldType& baseField =
00160                 obr_.lookupObject<fieldType>(fieldName);
00161             fieldType& meanField = const_cast<fieldType&>
00162             (
00163                 obr_.lookupObject<fieldType>(meanFieldList[i])
00164             );
00165 
00166             scalar alpha = 0.0;
00167             scalar beta = 0.0;
00168             if (faItems_[i].timeBase())
00169             {
00170                  alpha = (totalTime_[i] - dt)/totalTime_[i];
00171                  beta = dt/totalTime_[i];
00172             }
00173             else
00174             {
00175                 alpha = scalar(totalIter_[i] - 1)/scalar(totalIter_[i]);
00176                 beta = 1.0/scalar(totalIter_[i]);
00177             }
00178 
00179             meanField = alpha*meanField + beta*baseField;
00180         }
00181     }
00182 }
00183 
00184 
00185 template<class Type1, class Type2>
00186 void Foam::fieldAverage::calculatePrime2MeanFields
00187 (
00188     const wordList& meanFieldList,
00189     const wordList& prime2MeanFieldList
00190 ) const
00191 {
00192     typedef GeometricField<Type1, fvPatchField, volMesh> fieldType1;
00193     typedef GeometricField<Type2, fvPatchField, volMesh> fieldType2;
00194 
00195     const scalar dt = obr_.time().deltaT().value();
00196 
00197     forAll(faItems_, i)
00198     {
00199         if
00200         (
00201             faItems_[i].prime2Mean()
00202          && meanFieldList[i].size()
00203          && prime2MeanFieldList[i].size()
00204         )
00205         {
00206             const word& fieldName = faItems_[i].fieldName();
00207             const fieldType1& baseField =
00208                 obr_.lookupObject<fieldType1>(fieldName);
00209             const fieldType1& meanField =
00210                 obr_.lookupObject<fieldType1>(meanFieldList[i]);
00211             fieldType2& prime2MeanField = const_cast<fieldType2&>
00212             (
00213                 obr_.lookupObject<fieldType2>(prime2MeanFieldList[i])
00214             );
00215 
00216             scalar alpha = 0.0;
00217             scalar beta = 0.0;
00218             if (faItems_[i].timeBase())
00219             {
00220                 alpha = (totalTime_[i] - dt)/totalTime_[i];
00221                 beta = dt/totalTime_[i];
00222             }
00223             else
00224             {
00225                 alpha = scalar(totalIter_[i] - 1)/scalar(totalIter_[i]);
00226                 beta = 1.0/scalar(totalIter_[i]);
00227             }
00228 
00229             prime2MeanField =
00230                 alpha*prime2MeanField
00231               + beta*sqr(baseField)
00232               - sqr(meanField);
00233         }
00234     }
00235 }
00236 
00237 
00238 template<class Type1, class Type2>
00239 void Foam::fieldAverage::addMeanSqrToPrime2Mean
00240 (
00241     const wordList& meanFieldList,
00242     const wordList& prime2MeanFieldList
00243 ) const
00244 {
00245     typedef GeometricField<Type1, fvPatchField, volMesh> fieldType1;
00246     typedef GeometricField<Type2, fvPatchField, volMesh> fieldType2;
00247 
00248     forAll(faItems_, i)
00249     {
00250         if
00251         (
00252             faItems_[i].prime2Mean()
00253          && meanFieldList[i].size()
00254          && prime2MeanFieldList[i].size()
00255         )
00256         {
00257             const fieldType1& meanField =
00258                 obr_.lookupObject<fieldType1>(meanFieldList[i]);
00259             fieldType2& prime2MeanField = const_cast<fieldType2&>
00260             (
00261                 obr_.lookupObject<fieldType2>(prime2MeanFieldList[i])
00262             );
00263 
00264             prime2MeanField += sqr(meanField);
00265         }
00266     }
00267 }
00268 
00269 
00270 template<class Type>
00271 void Foam::fieldAverage::writeFieldList(const wordList& fieldList) const
00272 {
00273     typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
00274 
00275     forAll(fieldList, i)
00276     {
00277         if (fieldList[i].size())
00278         {
00279             const fieldType& f = obr_.lookupObject<fieldType>(fieldList[i]);
00280             f.write();
00281         }
00282     }
00283 }
00284 
00285 
00286 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines