initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / lagrangian / molecularDynamics / molecularMeasurements / bufferedAccumulator / bufferedAccumulator.C
blobde7d31fcb5291c99895422b2b75fe76be6439db7
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 "bufferedAccumulator.H"
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 template<class Type>
32 const char* const
33     Foam::bufferedAccumulator<Type>::typeName("bufferedAccumulator");
36 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
38 template<class Type>
39 void Foam::bufferedAccumulator<Type>::accumulateAndResetBuffer(const label b)
41     accumulationBuffer() += (*this)[b];
43     averagesTaken_++;
45     (*this)[b] = Field<Type>(bufferLength(), pTraits<Type>::zero);
47     bufferOffsets_[b] = 0;
51 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
53 template<class Type>
54 Foam::bufferedAccumulator<Type>::bufferedAccumulator()
56     List< Field<Type> >(),
57     averagesTaken_(),
58     bufferOffsets_()
62 template<class Type>
63 Foam::bufferedAccumulator<Type>::bufferedAccumulator
65     const label nBuffers,
66     const label bufferLength,
67     const label bufferingInterval
70     List< Field<Type> >(),
71     averagesTaken_(),
72     bufferOffsets_()
74     setSizes
75     (
76         nBuffers,
77         bufferLength,
78         bufferingInterval
79     );
83 template<class Type>
84 Foam::bufferedAccumulator<Type>::bufferedAccumulator
86     const bufferedAccumulator<Type>& bA
89     List< Field<Type> >(static_cast< List< Field<Type> > >(bA)),
90     averagesTaken_(bA.averagesTaken()),
91     bufferOffsets_(bA.bufferOffsets())
95 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
97 template<class Type>
98 Foam::bufferedAccumulator<Type>::~bufferedAccumulator()
102 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
104 template<class Type>
105 void Foam::bufferedAccumulator<Type>::setSizes
107     const label nBuffers,
108     const label bufferLength,
109     const label bufferingInterval
112     (*this).setSize(nBuffers + 1);
114     forAll((*this), b)
115     {
116         (*this)[b] = Field<Type>(bufferLength, pTraits<Type>::zero);
117     }
119     averagesTaken_ = 0;
121     bufferOffsets_.setSize(nBuffers);
123     forAll(bufferOffsets_, bO)
124     {
125         bufferOffsets_[bO] = -bufferingInterval * bO - 1;
126     }
130 template<class Type>
131 Foam::label Foam::bufferedAccumulator<Type>::addToBuffers
133     const List<Type>& valuesToAdd
136     label bufferToRefill = -1;
138     for (label b = 0; b < nBuffers(); b++)
139     {
140         Field<Type>& buf((*this)[b]);
142         label& bO = bufferOffsets_[b];
144         if (bO >= 0)
145         {
146             buf[bO] = valuesToAdd[b];
147         }
149         bO++;
151         if (bO == bufferLength())
152         {
153             accumulateAndResetBuffer(b);
154         }
156         if (bO == 0)
157         {
158             if (bufferToRefill != -1)
159             {
160                 FatalErrorIn("bufferedAccumulator<Type>::addToBuffers ")
161                     << "More than one bufferedAccumulator accumulation "
162                     << "buffer filled at once, this is considered an error."
163                     << abort(FatalError);
164             }
166             bufferToRefill = b;
167         }
168     }
170     return bufferToRefill;
174 template<class Type>
175 Foam::Field<Type> Foam::bufferedAccumulator<Type>::averaged() const
177     if (averagesTaken_)
178     {
179         Field<Type> bA = accumulationBuffer()/averagesTaken_;
181         return bA;
182     }
183     else
184     {
185         WarningIn
186         (
187             "bufferedAccumulator<Type>::averagedbufferedAccumulator() const"
188         )   << "Averaged correlation function requested but averagesTaken = "
189             << averagesTaken_
190             << ". Returning empty field."
191             << endl;
193         return Field<Type>(bufferLength(), pTraits<Type>::zero);
194     }
198 template<class Type>
199 void Foam::bufferedAccumulator<Type>::resetAveraging()
201     accumulationBuffer() = Field<Type>(bufferLength(), pTraits<Type>::zero);
203     averagesTaken_ = 0;
207 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
209 template<class Type>
210 void Foam::bufferedAccumulator<Type>::operator=
212     const bufferedAccumulator<Type>& rhs
215     // Check for assignment to self
216     if (this == &rhs)
217     {
218         FatalErrorIn
219         (
220             "bufferedAccumulator<Type>::operator=(const bufferedAccumulator&)"
221         )   << "Attempted assignment to self"
222             << abort(FatalError);
223     }
225     List< Field<Type> >::operator=(rhs);
227     averagesTaken_ = rhs.averagesTaken();
229     bufferOffsets_ = rhs.bufferOffsets();
233 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
235 #   include "bufferedAccumulatorIO.C"
237 // ************************************************************************* //