Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / lagrangian / molecularDynamics / molecularMeasurements / correlationFunction / correlationFunction.C
blob1eedef0e4cb3e3951b270d2d0ce4d23def869bb3
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2008-2010 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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "correlationFunction.H"
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 template<class Type>
31 const char* const
32     Foam::correlationFunction<Type>::typeName("correlationFunction");
35 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
37 template<class Type>
38 void Foam::correlationFunction<Type>::setTimesAndSizes
40     const label tZeroBufferSize
43     sampleSteps_  = ceil(sampleInterval_/mesh_.time().deltaTValue());
45     sampleInterval_ = sampleSteps_*mesh_.time().deltaTValue();
47     label bufferLength(ceil(duration_/sampleInterval_));
49     duration_ = bufferLength*sampleInterval_;
51     label bufferingInterval(ceil(averagingInterval_/sampleInterval_));
53     averagingInterval_ = bufferingInterval*sampleInterval_;
55     label nBuffers(ceil(duration_/averagingInterval_));
57     this->setSizes
58     (
59         nBuffers,
60         bufferLength,
61         bufferingInterval
62     );
64     tZeroBuffers_ =
65         Field< Field<Type> >
66         (
67             nBuffers,
68             Field<Type>
69             (
70                 tZeroBufferSize,
71                 pTraits<Type>::zero
72             )
73         );
77 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
79 template<class Type>
80 Foam::correlationFunction<Type>::correlationFunction
82     const polyMesh& mesh,
83     const dictionary& cfDict,
84     const label tZeroBufferSize
87     bufferedAccumulator<scalar>(),
88     mesh_(mesh)
90     duration_ = readScalar(cfDict.lookup("duration"));
92     sampleInterval_ = readScalar(cfDict.lookup("sampleInterval"));
94     averagingInterval_ = readScalar(cfDict.lookup("averagingInterval"));
96     setTimesAndSizes(tZeroBufferSize);
100 template<class Type>
101 Foam::correlationFunction<Type>::correlationFunction
103     const polyMesh& mesh,
104     const label tZeroBufferSize,
105     const scalar duration,
106     const scalar sampleInterval,
107     const scalar averagingInterval
110     bufferedAccumulator<scalar>(),
111     mesh_(mesh),
112     duration_(duration),
113     sampleInterval_(sampleInterval),
114     averagingInterval_(averagingInterval)
116     setTimesAndSizes(tZeroBufferSize);
120 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
122 template<class Type>
123 Foam::correlationFunction<Type>::~correlationFunction()
127 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
129 template<class Type>
130 void Foam::correlationFunction<Type>::calculateCorrelationFunction
132     const Field<Type>& currentValues
135     if (measurandFieldSize() != currentValues.size())
136     {
137         FatalErrorIn("correlationFunction<Type>::calculateCorrelationFunction")
138             << "Trying to supply a Field of length"
139             << currentValues.size()
140             << " to calculate the correlation function. "
141             << "Expecting a Field of length "
142             << measurandFieldSize() << nl
143             << abort(FatalError);
144     }
146     List<scalar> cFSums(nBuffers(),0.0);
148     forAll(tZeroBuffers_, tZB)
149     {
150         scalar& cFSum = cFSums[tZB];
152         const Field<Type>& tZeroBuffer = tZeroBuffers_[tZB];
154         forAll(currentValues, cV)
155         {
156             const Type& tZeroBufferValue = tZeroBuffer[cV];
158             const Type& currentValue = currentValues[cV];
160             forAll(currentValue, component)
161             {
162                 cFSum +=
163                 (
164                     tZeroBufferValue[component]*currentValue[component]
165                 );
166             }
167         }
169         cFSum /= (measurandFieldSize()*currentValues[0].size());
170     }
172     label bufferToRefill = addToBuffers(cFSums);
174     if (bufferToRefill != -1)
175     {
176         tZeroBuffers_[bufferToRefill] = currentValues;
177     }
181 template<class Type>
182 void Foam::correlationFunction<Type>::calculateCorrelationFunction
184     const Type& currentValue
187     if (measurandFieldSize() != 1)
188     {
189         FatalErrorIn("correlationFunction<Type>::calculateCorrelationFunction")
190             << "Trying to supply a single value to calculate the correlation "
191             << "function.  Expecting a Field of length "
192             << measurandFieldSize()
193             << abort(FatalError);
194     }
196     calculateCorrelationFunction(Field<Type>(1, currentValue));
200 template<class Type>
201 Foam::scalar Foam::correlationFunction<Type>::integral() const
203     Field<scalar> averageCF(averaged());
205     scalar cFIntegral = 0.0;
207     for (label v = 0; v < averageCF.size() - 1; v++)
208     {
209         cFIntegral +=
210             0.5
211            *sampleInterval_
212            *(averageCF[v+1] + averageCF[v]);
213     }
215     return cFIntegral;
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 #   include "correlationFunctionIO.C"
223 // ************************************************************************* //