bufferedAccumulator.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "bufferedAccumulator.H"
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 template<class Type>
31 const char* const
32  Foam::bufferedAccumulator<Type>::typeName("bufferedAccumulator");
33 
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 template<class Type>
39 {
40  accumulationBuffer() += (*this)[b];
41 
42  averagesTaken_++;
43 
44  (*this)[b] = Field<Type>(bufferLength(), pTraits<Type>::zero);
45 
46  bufferOffsets_[b] = 0;
47 }
48 
49 
50 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
51 
52 template<class Type>
54 :
55  List< Field<Type> >(),
56  averagesTaken_(),
57  bufferOffsets_()
58 {}
59 
60 
61 template<class Type>
63 (
64  const label nBuffers,
65  const label bufferLength,
66  const label bufferingInterval
67 )
68 :
70  averagesTaken_(),
71  bufferOffsets_()
72 {
73  setSizes
74  (
75  nBuffers,
76  bufferLength,
77  bufferingInterval
78  );
79 }
80 
81 
82 template<class Type>
84 (
86 )
87 :
88  List< Field<Type> >(static_cast< List< Field<Type> > >(bA)),
89  averagesTaken_(bA.averagesTaken()),
90  bufferOffsets_(bA.bufferOffsets())
91 {}
92 
93 
94 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
95 
96 template<class Type>
98 {}
99 
100 
101 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
102 
103 template<class Type>
105 (
106  const label nBuffers,
107  const label bufferLength,
108  const label bufferingInterval
109 )
110 {
111  (*this).setSize(nBuffers + 1);
112 
113  forAll((*this), b)
114  {
116  }
117 
118  averagesTaken_ = 0;
119 
120  bufferOffsets_.setSize(nBuffers);
121 
122  forAll(bufferOffsets_, bO)
123  {
124  bufferOffsets_[bO] = -bufferingInterval * bO - 1;
125  }
126 }
127 
128 
129 template<class Type>
131 (
132  const List<Type>& valuesToAdd
133 )
134 {
135  label bufferToRefill = -1;
136 
137  for (label b = 0; b < nBuffers(); b++)
138  {
139  Field<Type>& buf((*this)[b]);
140 
141  label& bO = bufferOffsets_[b];
142 
143  if (bO >= 0)
144  {
145  buf[bO] = valuesToAdd[b];
146  }
147 
148  bO++;
149 
150  if (bO == bufferLength())
151  {
152  accumulateAndResetBuffer(b);
153  }
154 
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  }
164 
165  bufferToRefill = b;
166  }
167  }
168 
169  return bufferToRefill;
170 }
171 
172 
173 template<class Type>
175 {
176  if (averagesTaken_)
177  {
178  Field<Type> bA = accumulationBuffer()/averagesTaken_;
179 
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;
191 
193  }
194 }
195 
196 
197 template<class Type>
199 {
200  accumulationBuffer() = Field<Type>(bufferLength(), pTraits<Type>::zero);
201 
202  averagesTaken_ = 0;
203 }
204 
205 
206 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
207 
208 template<class Type>
209 void Foam::bufferedAccumulator<Type>::operator=
210 (
211  const bufferedAccumulator<Type>& rhs
212 )
213 {
214  // Check for assignment to self
215  if (this == &rhs)
216  {
218  (
219  "bufferedAccumulator<Type>::operator=(const bufferedAccumulator&)"
220  ) << "Attempted assignment to self"
221  << abort(FatalError);
222  }
223 
225 
226  averagesTaken_ = rhs.averagesTaken();
227 
228  bufferOffsets_ = rhs.bufferOffsets();
229 }
230 
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 # include "bufferedAccumulatorIO.C"
235 
236 // ************************************************************************* //
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:28
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
static const char *const typeName
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
void setSize(const label)
Reset size of List.
Definition: List.C:318
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define WarningIn(functionName)
Report a warning using Foam::Warning.
label addToBuffers(const List< Type > &valuesToAdd)
#define forAll(list, i)
Definition: UList.H:421
Pre-declare SubField and related Field type.
Definition: Field.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bufferedAccumulator()
Construct null.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
Traits class for primitives.
Definition: pTraits.H:50
error FatalError
Field< Type > averaged() const
friend Ostream & operator(Ostream &, const bufferedAccumulator< Type > &)
void setSizes(const label nBuffers, const label bufferLength, const label bufferingInterval)