initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / lagrangian / molecularDynamics / molecularMeasurements / correlationFunction / correlationFunction.C
blobcce0d86a22c0d6f9000d2f3b6f2d9a35e48b10bc
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2008-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "correlationFunction.H"
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 template<class Type>
32 const char* const
33     Foam::correlationFunction<Type>::typeName("correlationFunction");
36 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
38 template<class Type>
39 void Foam::correlationFunction<Type>::setTimesAndSizes
41     const label tZeroBufferSize
44     sampleSteps_  = ceil(sampleInterval_/mesh_.time().deltaT().value());
46     sampleInterval_ = sampleSteps_*mesh_.time().deltaT().value();
48     label bufferLength(ceil(duration_/sampleInterval_));
50     duration_ = bufferLength*sampleInterval_;
52     label bufferingInterval(ceil(averagingInterval_/sampleInterval_));
54     averagingInterval_ = bufferingInterval*sampleInterval_;
56     label nBuffers(ceil(duration_/averagingInterval_));
58     this->setSizes
59     (
60         nBuffers,
61         bufferLength,
62         bufferingInterval
63     );
65     tZeroBuffers_ =
66         Field< Field<Type> >
67         (
68             nBuffers,
69             Field<Type>
70             (
71                 tZeroBufferSize,
72                 pTraits<Type>::zero
73             )
74         );
78 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
80 template<class Type>
81 Foam::correlationFunction<Type>::correlationFunction
83     const polyMesh& mesh,
84     const dictionary& cfDict,
85     const label tZeroBufferSize
88     bufferedAccumulator<scalar>(),
89     mesh_(mesh)
91     duration_ = readScalar(cfDict.lookup("duration"));
93     sampleInterval_ = readScalar(cfDict.lookup("sampleInterval"));
95     averagingInterval_ = readScalar(cfDict.lookup("averagingInterval"));
97     setTimesAndSizes(tZeroBufferSize);
101 template<class Type>
102 Foam::correlationFunction<Type>::correlationFunction
104     const polyMesh& mesh,
105     const label tZeroBufferSize,
106     const scalar duration,
107     const scalar sampleInterval,
108     const scalar averagingInterval
111     bufferedAccumulator<scalar>(),
112     mesh_(mesh),
113     duration_(duration),
114     sampleInterval_(sampleInterval),
115     averagingInterval_(averagingInterval)
117     setTimesAndSizes(tZeroBufferSize);
121 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
123 template<class Type>
124 Foam::correlationFunction<Type>::~correlationFunction()
128 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
130 template<class Type>
131 void Foam::correlationFunction<Type>::calculateCorrelationFunction
133     const Field<Type>& currentValues
136     if (measurandFieldSize() != currentValues.size())
137     {
138         FatalErrorIn("correlationFunction<Type>::calculateCorrelationFunction")
139             << "Trying to supply a Field of length"
140             << currentValues.size()
141             << " to calculate the correlation function. "
142             << "Expecting a Field of length "
143             << measurandFieldSize() << nl
144             << abort(FatalError);
145     }
147     List<scalar> cFSums(nBuffers(),0.0);
149     forAll(tZeroBuffers_, tZB)
150     {
151         scalar& cFSum = cFSums[tZB];
153         const Field<Type>& tZeroBuffer = tZeroBuffers_[tZB];
155         forAll(currentValues, cV)
156         {
157             const Type& tZeroBufferValue = tZeroBuffer[cV];
159             const Type& currentValue = currentValues[cV];
161             forAll(currentValue, component)
162             {
163                 cFSum +=
164                 (
165                     tZeroBufferValue[component]*currentValue[component]
166                 );
167             }
168         }
170         cFSum /= (measurandFieldSize()*currentValues[0].size());
171     }
173     label bufferToRefill = addToBuffers(cFSums);
175     if (bufferToRefill != -1)
176     {
177         tZeroBuffers_[bufferToRefill] = currentValues;
178     }
182 template<class Type>
183 void Foam::correlationFunction<Type>::calculateCorrelationFunction
185     const Type& currentValue
188     if( measurandFieldSize() != 1)
189     {
190         FatalErrorIn("correlationFunction<Type>::calculateCorrelationFunction")
191             << "Trying to supply a single value to calculate the correlation "
192             << "function.  Expecting a Field of length "
193             << measurandFieldSize()
194             << abort(FatalError);
195     }
197     calculateCorrelationFunction(Field<Type>(1, currentValue));
201 template<class Type>
202 Foam::scalar Foam::correlationFunction<Type>::integral() const
204     Field<scalar> averageCF(averaged());
206     scalar cFIntegral = 0.0;
208     for (label v = 0; v < averageCF.size() - 1; v++)
209     {
210         cFIntegral +=
211             0.5
212            *sampleInterval_
213            *(averageCF[v+1] + averageCF[v]);
214     }
216     return cFIntegral;
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 #   include "correlationFunctionIO.C"
224 // ************************************************************************* //