Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / lagrangian / molecularDynamics / molecularMeasurements / bufferedAccumulator / bufferedAccumulator.C
bloba547261c96ff5fb19d4ff9e900f95d4fd3ee3af6
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "bufferedAccumulator.H"
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 template<class Type>
31 const char* const
32     Foam::bufferedAccumulator<Type>::typeName("bufferedAccumulator");
35 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
37 template<class Type>
38 void Foam::bufferedAccumulator<Type>::accumulateAndResetBuffer(const label b)
40     accumulationBuffer() += (*this)[b];
42     averagesTaken_++;
44     (*this)[b] = Field<Type>(bufferLength(), pTraits<Type>::zero);
46     bufferOffsets_[b] = 0;
50 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
52 template<class Type>
53 Foam::bufferedAccumulator<Type>::bufferedAccumulator()
55     List< Field<Type> >(),
56     averagesTaken_(),
57     bufferOffsets_()
61 template<class Type>
62 Foam::bufferedAccumulator<Type>::bufferedAccumulator
64     const label nBuffers,
65     const label bufferLength,
66     const label bufferingInterval
69     List< Field<Type> >(),
70     averagesTaken_(),
71     bufferOffsets_()
73     setSizes
74     (
75         nBuffers,
76         bufferLength,
77         bufferingInterval
78     );
82 template<class Type>
83 Foam::bufferedAccumulator<Type>::bufferedAccumulator
85     const bufferedAccumulator<Type>& bA
88     List< Field<Type> >(static_cast< List< Field<Type> > >(bA)),
89     averagesTaken_(bA.averagesTaken()),
90     bufferOffsets_(bA.bufferOffsets())
94 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
96 template<class Type>
97 Foam::bufferedAccumulator<Type>::~bufferedAccumulator()
101 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
103 template<class Type>
104 void Foam::bufferedAccumulator<Type>::setSizes
106     const label nBuffers,
107     const label bufferLength,
108     const label bufferingInterval
111     (*this).setSize(nBuffers + 1);
113     forAll((*this), b)
114     {
115         (*this)[b] = Field<Type>(bufferLength, pTraits<Type>::zero);
116     }
118     averagesTaken_ = 0;
120     bufferOffsets_.setSize(nBuffers);
122     forAll(bufferOffsets_, bO)
123     {
124         bufferOffsets_[bO] = -bufferingInterval * bO - 1;
125     }
129 template<class Type>
130 Foam::label Foam::bufferedAccumulator<Type>::addToBuffers
132     const List<Type>& valuesToAdd
135     label bufferToRefill = -1;
137     for (label b = 0; b < nBuffers(); b++)
138     {
139         Field<Type>& buf((*this)[b]);
141         label& bO = bufferOffsets_[b];
143         if (bO >= 0)
144         {
145             buf[bO] = valuesToAdd[b];
146         }
148         bO++;
150         if (bO == bufferLength())
151         {
152             accumulateAndResetBuffer(b);
153         }
155         if (bO == 0)
156         {
157             if (bufferToRefill != -1)
158             {
159                 FatalErrorIn("bufferedAccumulator<Type>::addToBuffers ")
160                     << "More than one bufferedAccumulator accumulation "
161                     << "buffer filled at once, this is considered an error."
162                     << abort(FatalError);
163             }
165             bufferToRefill = b;
166         }
167     }
169     return bufferToRefill;
173 template<class Type>
174 Foam::Field<Type> Foam::bufferedAccumulator<Type>::averaged() const
176     if (averagesTaken_)
177     {
178         Field<Type> bA = accumulationBuffer()/averagesTaken_;
180         return bA;
181     }
182     else
183     {
184         WarningIn
185         (
186             "bufferedAccumulator<Type>::averagedbufferedAccumulator() const"
187         )   << "Averaged correlation function requested but averagesTaken = "
188             << averagesTaken_
189             << ". Returning empty field."
190             << endl;
192         return Field<Type>(bufferLength(), pTraits<Type>::zero);
193     }
197 template<class Type>
198 void Foam::bufferedAccumulator<Type>::resetAveraging()
200     accumulationBuffer() = Field<Type>(bufferLength(), pTraits<Type>::zero);
202     averagesTaken_ = 0;
206 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
208 template<class Type>
209 void Foam::bufferedAccumulator<Type>::operator=
211     const bufferedAccumulator<Type>& rhs
214     // Check for assignment to self
215     if (this == &rhs)
216     {
217         FatalErrorIn
218         (
219             "bufferedAccumulator<Type>::operator=(const bufferedAccumulator&)"
220         )   << "Attempted assignment to self"
221             << abort(FatalError);
222     }
224     List< Field<Type> >::operator=(rhs);
226     averagesTaken_ = rhs.averagesTaken();
228     bufferOffsets_ = rhs.bufferOffsets();
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
234 #   include "bufferedAccumulatorIO.C"
236 // ************************************************************************* //