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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "bufferedAccumulator.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 template<class Type>
34 const char* const
35  Foam::bufferedAccumulator<Type>::typeName("bufferedAccumulator");
36 
37 
38 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 
40 template<class Type>
42 {
43  accumulationBuffer() += (*this)[b];
44 
45  averagesTaken_++;
46 
47  (*this)[b] = Field<Type>(bufferLength(), Zero);
48 
49  bufferOffsets_[b] = 0;
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
55 template<class Type>
57 :
58  List<Field<Type>>(),
59  averagesTaken_(),
60  bufferOffsets_()
61 {}
62 
63 
64 template<class Type>
66 (
67  const label nBuffers,
68  const label bufferLength,
69  const label bufferingInterval
70 )
71 :
73  averagesTaken_(),
74  bufferOffsets_()
75 {
76  setSizes
77  (
78  nBuffers,
79  bufferLength,
80  bufferingInterval
81  );
82 }
83 
84 
85 template<class Type>
87 (
89 )
90 :
91  List<Field<Type>>(static_cast<List<Field<Type>>>(bA)),
92  averagesTaken_(bA.averagesTaken()),
93  bufferOffsets_(bA.bufferOffsets())
94 {}
95 
96 
97 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
98 
99 template<class Type>
101 {}
102 
103 
104 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105 
106 template<class Type>
108 (
109  const label nBuffers,
110  const label bufferLength,
111  const label bufferingInterval
112 )
113 {
114  (*this).setSize(nBuffers + 1);
115 
116  forAll((*this), b)
117  {
118  (*this)[b] = Field<Type>(bufferLength, Zero);
119  }
120 
121  averagesTaken_ = 0;
122 
123  bufferOffsets_.setSize(nBuffers);
124 
125  forAll(bufferOffsets_, bO)
126  {
127  bufferOffsets_[bO] = -bufferingInterval * bO - 1;
128  }
129 }
130 
131 
132 template<class Type>
134 (
135  const List<Type>& valuesToAdd
136 )
137 {
138  label bufferToRefill = -1;
139 
140  for (label b = 0; b < nBuffers(); b++)
141  {
142  Field<Type>& buf((*this)[b]);
143 
144  label& bO = bufferOffsets_[b];
145 
146  if (bO >= 0)
147  {
148  buf[bO] = valuesToAdd[b];
149  }
150 
151  bO++;
152 
153  if (bO == bufferLength())
154  {
155  accumulateAndResetBuffer(b);
156  }
157 
158  if (bO == 0)
159  {
160  if (bufferToRefill != -1)
161  {
163  << "More than one bufferedAccumulator accumulation "
164  << "buffer filled at once, this is considered an error."
165  << abort(FatalError);
166  }
167 
168  bufferToRefill = b;
169  }
170  }
171 
172  return bufferToRefill;
173 }
174 
175 
176 template<class Type>
178 {
179  if (averagesTaken_)
180  {
181  Field<Type> bA = accumulationBuffer()/averagesTaken_;
182 
183  return bA;
184  }
185  else
186  {
188  << "Averaged correlation function requested but averagesTaken = "
189  << averagesTaken_
190  << ". Returning empty field."
191  << endl;
192 
193  return Field<Type>(bufferLength(), Zero);
194  }
195 }
196 
197 
198 template<class Type>
200 {
201  accumulationBuffer() = Field<Type>(bufferLength(), Zero);
202 
203  averagesTaken_ = 0;
204 }
205 
206 
207 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
208 
209 template<class Type>
211 (
212  const bufferedAccumulator<Type>& rhs
213 )
214 {
215  if (this == &rhs)
216  {
217  return; // Self-assignment is a no-op
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 // ************************************************************************* //
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::bufferedAccumulator::averaged
Field< Type > averaged() const
Definition: bufferedAccumulator.C:177
bufferedAccumulatorIO.C
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::bufferedAccumulator::resetAveraging
void resetAveraging()
Definition: bufferedAccumulator.C:199
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
bufferedAccumulator.H
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::bufferedAccumulator
Definition: bufferedAccumulator.H:48
Foam::bufferedAccumulator::setSizes
void setSizes(const label nBuffers, const label bufferLength, const label bufferingInterval)
Definition: bufferedAccumulator.C:108
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::bufferedAccumulator::addToBuffers
label addToBuffers(const List< Type > &valuesToAdd)
Definition: bufferedAccumulator.C:134
Foam::bufferedAccumulator::~bufferedAccumulator
~bufferedAccumulator()
Destructor.
Definition: bufferedAccumulator.C:100
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::bufferedAccumulator::typeName
static const char *const typeName
Definition: bufferedAccumulator.H:90
Foam::bufferedAccumulator::bufferedAccumulator
bufferedAccumulator()
Construct null.
Definition: bufferedAccumulator.C:56
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303