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-------------------------------------------------------------------------------
11License
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
33template<class Type>
34const char* const
35 Foam::bufferedAccumulator<Type>::typeName("bufferedAccumulator");
36
37
38// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39
40template<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
55template<class Type>
57:
58 List<Field<Type>>(),
59 averagesTaken_(),
60 bufferOffsets_()
61{}
62
63
64template<class Type>
66(
67 const label nBuffers,
68 const label bufferLength,
69 const label bufferingInterval
70)
71:
72 List<Field<Type>>(),
73 averagesTaken_(),
74 bufferOffsets_()
75{
77 (
80 bufferingInterval
81 );
82}
83
84
85template<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
97// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
98
99template<class Type>
101{}
102
103
104// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105
106template<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 }
121 averagesTaken_ = 0;
122
123 bufferOffsets_.setSize(nBuffers);
124
125 forAll(bufferOffsets_, bO)
126 {
127 bufferOffsets_[bO] = -bufferingInterval * bO - 1;
128 }
129}
130
131
132template<class Type>
134(
135 const List<Type>& valuesToAdd
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
176template<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
198template<class Type>
200{
201 accumulationBuffer() = Field<Type>(bufferLength(), Zero);
202
203 averagesTaken_ = 0;
204}
205
206
207// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
208
209template<class Type>
211(
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
231
232// ************************************************************************* //
Generic templated field type.
Definition: Field.H:82
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
Field< Type > averaged() const
bufferedAccumulator()
Construct null.
label addToBuffers(const List< Type > &valuesToAdd)
void setSizes(const label nBuffers, const label bufferLength, const label bufferingInterval)
static const char *const typeName
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define WarningInFunction
Report a warning using Foam::Warning.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
volScalarField & b
Definition: createFields.H:27
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333