bufferedAccumulator.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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(), 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  {
115  (*this)[b] = Field<Type>(bufferLength, Zero);
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  {
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  {
185  << "Averaged correlation function requested but averagesTaken = "
186  << averagesTaken_
187  << ". Returning empty field."
188  << endl;
189 
190  return Field<Type>(bufferLength(), Zero);
191  }
192 }
193 
194 
195 template<class Type>
197 {
198  accumulationBuffer() = Field<Type>(bufferLength(), Zero);
199 
200  averagesTaken_ = 0;
201 }
202 
203 
204 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
205 
206 template<class Type>
207 void Foam::bufferedAccumulator<Type>::operator=
208 (
209  const bufferedAccumulator<Type>& rhs
210 )
211 {
212  // Check for assignment to self
213  if (this == &rhs)
214  {
216  << "Attempted assignment to self"
217  << abort(FatalError);
218  }
219 
220  List<Field<Type>>::operator=(rhs);
221 
222  averagesTaken_ = rhs.averagesTaken();
223 
224  bufferOffsets_ = rhs.bufferOffsets();
225 }
226 
227 
228 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 
230  #include "bufferedAccumulatorIO.C"
231 
232 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
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
const dimensionedScalar b
Wien displacement law constant: default SI units: [m K].
Definition: createFields.H:27
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void setSizes(const label nBuffers, const label bufferLength, const label bufferingInterval)
Field< Type > averaged() const
Pre-declare SubField and related Field type.
Definition: Field.H:56
static const char *const typeName
static const zero Zero
Definition: zero.H:97
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bufferedAccumulator()
Construct null.
void setSize(const label)
Reset size of List.
Definition: List.C:281
#define WarningInFunction
Report a warning using Foam::Warning.
label addToBuffers(const List< Type > &valuesToAdd)