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

bufferedAccumulator.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 "bufferedAccumulator.H"
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 template<class Type>
00032 const char* const
00033     Foam::bufferedAccumulator<Type>::typeName("bufferedAccumulator");
00034 
00035 
00036 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00037 
00038 template<class Type>
00039 void Foam::bufferedAccumulator<Type>::accumulateAndResetBuffer(const label b)
00040 {
00041     accumulationBuffer() += (*this)[b];
00042 
00043     averagesTaken_++;
00044 
00045     (*this)[b] = Field<Type>(bufferLength(), pTraits<Type>::zero);
00046 
00047     bufferOffsets_[b] = 0;
00048 }
00049 
00050 
00051 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00052 
00053 template<class Type>
00054 Foam::bufferedAccumulator<Type>::bufferedAccumulator()
00055 :
00056     List< Field<Type> >(),
00057     averagesTaken_(),
00058     bufferOffsets_()
00059 {}
00060 
00061 
00062 template<class Type>
00063 Foam::bufferedAccumulator<Type>::bufferedAccumulator
00064 (
00065     const label nBuffers,
00066     const label bufferLength,
00067     const label bufferingInterval
00068 )
00069 :
00070     List< Field<Type> >(),
00071     averagesTaken_(),
00072     bufferOffsets_()
00073 {
00074     setSizes
00075     (
00076         nBuffers,
00077         bufferLength,
00078         bufferingInterval
00079     );
00080 }
00081 
00082 
00083 template<class Type>
00084 Foam::bufferedAccumulator<Type>::bufferedAccumulator
00085 (
00086     const bufferedAccumulator<Type>& bA
00087 )
00088 :
00089     List< Field<Type> >(static_cast< List< Field<Type> > >(bA)),
00090     averagesTaken_(bA.averagesTaken()),
00091     bufferOffsets_(bA.bufferOffsets())
00092 {}
00093 
00094 
00095 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00096 
00097 template<class Type>
00098 Foam::bufferedAccumulator<Type>::~bufferedAccumulator()
00099 {}
00100 
00101 
00102 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00103 
00104 template<class Type>
00105 void Foam::bufferedAccumulator<Type>::setSizes
00106 (
00107     const label nBuffers,
00108     const label bufferLength,
00109     const label bufferingInterval
00110 )
00111 {
00112     (*this).setSize(nBuffers + 1);
00113 
00114     forAll((*this), b)
00115     {
00116         (*this)[b] = Field<Type>(bufferLength, pTraits<Type>::zero);
00117     }
00118 
00119     averagesTaken_ = 0;
00120 
00121     bufferOffsets_.setSize(nBuffers);
00122 
00123     forAll(bufferOffsets_, bO)
00124     {
00125         bufferOffsets_[bO] = -bufferingInterval * bO - 1;
00126     }
00127 }
00128 
00129 
00130 template<class Type>
00131 Foam::label Foam::bufferedAccumulator<Type>::addToBuffers
00132 (
00133     const List<Type>& valuesToAdd
00134 )
00135 {
00136     label bufferToRefill = -1;
00137 
00138     for (label b = 0; b < nBuffers(); b++)
00139     {
00140         Field<Type>& buf((*this)[b]);
00141 
00142         label& bO = bufferOffsets_[b];
00143 
00144         if (bO >= 0)
00145         {
00146             buf[bO] = valuesToAdd[b];
00147         }
00148 
00149         bO++;
00150 
00151         if (bO == bufferLength())
00152         {
00153             accumulateAndResetBuffer(b);
00154         }
00155 
00156         if (bO == 0)
00157         {
00158             if (bufferToRefill != -1)
00159             {
00160                 FatalErrorIn("bufferedAccumulator<Type>::addToBuffers ")
00161                     << "More than one bufferedAccumulator accumulation "
00162                     << "buffer filled at once, this is considered an error."
00163                     << abort(FatalError);
00164             }
00165 
00166             bufferToRefill = b;
00167         }
00168     }
00169 
00170     return bufferToRefill;
00171 }
00172 
00173 
00174 template<class Type>
00175 Foam::Field<Type> Foam::bufferedAccumulator<Type>::averaged() const
00176 {
00177     if (averagesTaken_)
00178     {
00179         Field<Type> bA = accumulationBuffer()/averagesTaken_;
00180 
00181         return bA;
00182     }
00183     else
00184     {
00185         WarningIn
00186         (
00187             "bufferedAccumulator<Type>::averagedbufferedAccumulator() const"
00188         )   << "Averaged correlation function requested but averagesTaken = "
00189             << averagesTaken_
00190             << ". Returning empty field."
00191             << endl;
00192 
00193         return Field<Type>(bufferLength(), pTraits<Type>::zero);
00194     }
00195 }
00196 
00197 
00198 template<class Type>
00199 void Foam::bufferedAccumulator<Type>::resetAveraging()
00200 {
00201     accumulationBuffer() = Field<Type>(bufferLength(), pTraits<Type>::zero);
00202 
00203     averagesTaken_ = 0;
00204 }
00205 
00206 
00207 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00208 
00209 template<class Type>
00210 void Foam::bufferedAccumulator<Type>::operator=
00211 (
00212     const bufferedAccumulator<Type>& rhs
00213 )
00214 {
00215     // Check for assignment to self
00216     if (this == &rhs)
00217     {
00218         FatalErrorIn
00219         (
00220             "bufferedAccumulator<Type>::operator=(const bufferedAccumulator&)"
00221         )   << "Attempted assignment to self"
00222             << abort(FatalError);
00223     }
00224 
00225     List< Field<Type> >::operator=(rhs);
00226 
00227     averagesTaken_ = rhs.averagesTaken();
00228 
00229     bufferOffsets_ = rhs.bufferOffsets();
00230 }
00231 
00232 
00233 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00234 
00235 #   include "bufferedAccumulatorIO.C"
00236 
00237 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd