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

correlationFunction.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) 2008-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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "correlationFunction.H"
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 template<class Type>
00032 const char* const
00033     Foam::correlationFunction<Type>::typeName("correlationFunction");
00034 
00035 
00036 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00037 
00038 template<class Type>
00039 void Foam::correlationFunction<Type>::setTimesAndSizes
00040 (
00041     const label tZeroBufferSize
00042 )
00043 {
00044     sampleSteps_  = ceil(sampleInterval_/mesh_.time().deltaT().value());
00045 
00046     sampleInterval_ = sampleSteps_*mesh_.time().deltaT().value();
00047 
00048     label bufferLength(ceil(duration_/sampleInterval_));
00049 
00050     duration_ = bufferLength*sampleInterval_;
00051 
00052     label bufferingInterval(ceil(averagingInterval_/sampleInterval_));
00053 
00054     averagingInterval_ = bufferingInterval*sampleInterval_;
00055 
00056     label nBuffers(ceil(duration_/averagingInterval_));
00057 
00058     this->setSizes
00059     (
00060         nBuffers,
00061         bufferLength,
00062         bufferingInterval
00063     );
00064 
00065     tZeroBuffers_ =
00066         Field< Field<Type> >
00067         (
00068             nBuffers,
00069             Field<Type>
00070             (
00071                 tZeroBufferSize,
00072                 pTraits<Type>::zero
00073             )
00074         );
00075 }
00076 
00077 
00078 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00079 
00080 template<class Type>
00081 Foam::correlationFunction<Type>::correlationFunction
00082 (
00083     const polyMesh& mesh,
00084     const dictionary& cfDict,
00085     const label tZeroBufferSize
00086 )
00087 :
00088     bufferedAccumulator<scalar>(),
00089     mesh_(mesh)
00090 {
00091     duration_ = readScalar(cfDict.lookup("duration"));
00092 
00093     sampleInterval_ = readScalar(cfDict.lookup("sampleInterval"));
00094 
00095     averagingInterval_ = readScalar(cfDict.lookup("averagingInterval"));
00096 
00097     setTimesAndSizes(tZeroBufferSize);
00098 }
00099 
00100 
00101 template<class Type>
00102 Foam::correlationFunction<Type>::correlationFunction
00103 (
00104     const polyMesh& mesh,
00105     const label tZeroBufferSize,
00106     const scalar duration,
00107     const scalar sampleInterval,
00108     const scalar averagingInterval
00109 )
00110 :
00111     bufferedAccumulator<scalar>(),
00112     mesh_(mesh),
00113     duration_(duration),
00114     sampleInterval_(sampleInterval),
00115     averagingInterval_(averagingInterval)
00116 {
00117     setTimesAndSizes(tZeroBufferSize);
00118 }
00119 
00120 
00121 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00122 
00123 template<class Type>
00124 Foam::correlationFunction<Type>::~correlationFunction()
00125 {}
00126 
00127 
00128 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00129 
00130 template<class Type>
00131 void Foam::correlationFunction<Type>::calculateCorrelationFunction
00132 (
00133     const Field<Type>& currentValues
00134 )
00135 {
00136     if (measurandFieldSize() != currentValues.size())
00137     {
00138         FatalErrorIn("correlationFunction<Type>::calculateCorrelationFunction")
00139             << "Trying to supply a Field of length"
00140             << currentValues.size()
00141             << " to calculate the correlation function. "
00142             << "Expecting a Field of length "
00143             << measurandFieldSize() << nl
00144             << abort(FatalError);
00145     }
00146 
00147     List<scalar> cFSums(nBuffers(),0.0);
00148 
00149     forAll(tZeroBuffers_, tZB)
00150     {
00151         scalar& cFSum = cFSums[tZB];
00152 
00153         const Field<Type>& tZeroBuffer = tZeroBuffers_[tZB];
00154 
00155         forAll(currentValues, cV)
00156         {
00157             const Type& tZeroBufferValue = tZeroBuffer[cV];
00158 
00159             const Type& currentValue = currentValues[cV];
00160 
00161             forAll(currentValue, component)
00162             {
00163                 cFSum +=
00164                 (
00165                     tZeroBufferValue[component]*currentValue[component]
00166                 );
00167             }
00168         }
00169 
00170         cFSum /= (measurandFieldSize()*currentValues[0].size());
00171     }
00172 
00173     label bufferToRefill = addToBuffers(cFSums);
00174 
00175     if (bufferToRefill != -1)
00176     {
00177         tZeroBuffers_[bufferToRefill] = currentValues;
00178     }
00179 }
00180 
00181 
00182 template<class Type>
00183 void Foam::correlationFunction<Type>::calculateCorrelationFunction
00184 (
00185     const Type& currentValue
00186 )
00187 {
00188     if( measurandFieldSize() != 1)
00189     {
00190         FatalErrorIn("correlationFunction<Type>::calculateCorrelationFunction")
00191             << "Trying to supply a single value to calculate the correlation "
00192             << "function.  Expecting a Field of length "
00193             << measurandFieldSize()
00194             << abort(FatalError);
00195     }
00196 
00197     calculateCorrelationFunction(Field<Type>(1, currentValue));
00198 }
00199 
00200 
00201 template<class Type>
00202 Foam::scalar Foam::correlationFunction<Type>::integral() const
00203 {
00204     Field<scalar> averageCF(averaged());
00205 
00206     scalar cFIntegral = 0.0;
00207 
00208     for (label v = 0; v < averageCF.size() - 1; v++)
00209     {
00210         cFIntegral +=
00211             0.5
00212            *sampleInterval_
00213            *(averageCF[v+1] + averageCF[v]);
00214     }
00215 
00216     return cFIntegral;
00217 }
00218 
00219 
00220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00221 
00222 #   include "correlationFunctionIO.C"
00223 
00224 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd