FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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