fieldAverage.H
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-2022 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 Class
25  Foam::functionObjects::fieldAverage
26 
27 Description
28  Calculates average quantities for a user-specified selection of volumetric
29  and surface fields.
30 
31  Fields are entered as a list of sub-dictionaries, which indicate the type of
32  averages to perform, and can be updated during the calculation. The current
33  options include:
34  - \c mean: arithmetic mean
35  - \c prime2Mean: prime-squared mean
36  - \c base: average over 'time', or 'iteration'
37  - \c window: optional averaging window, specified in 'base' units
38 
39  Average field names are constructed by concatenating the base field with
40  the averaging type, e.g. when averaging field 'U', the resultant fields
41  are:
42  - arithmetic mean field, \c UMean
43  - prime-squared field, \c UPrime2Mean
44 
45  Information regarding the number of averaging steps, and total averaging
46  time are written on a per-field basis to the \c "<functionObject
47  name>Properties" dictionary, located in <time>/uniform
48 
49  When restarting form a previous calculation, the averaging is continuous or
50  may be restarted using the \c restartOnRestart option.
51 
52  The averaging process may be restarted after each calculation output time
53  using the \c restartOnOutput option or restarted periodically using the \c
54  periodicRestart option and setting \c restartPeriod to the required
55  averaging period.
56 
57  Example of function object specification:
58  \verbatim
59  fieldAverage1
60  {
61  type fieldAverage;
62  libs ("libfieldFunctionObjects.so");
63 
64  writeControl writeTime;
65 
66  restartOnRestart false;
67  restartOnOutput false;
68  periodicRestart false;
69  restartPeriod 0.002;
70 
71  base time;
72  window 10.0;
73  windowName w1;
74 
75  mean yes;
76  prime2Mean yes;
77 
78  fields (U p);
79  }
80  \endverbatim
81 
82 Usage
83  \table
84  Property | Description | Required | Default
85  type | type name: fieldAverage | yes |
86  restartOnRestart | Restart the averaging on restart | no | no
87  restartOnOutput | Restart the averaging on output | no | no
88  periodicRestart | Periodically restart the averaging | no | no
89  restartPeriod | Periodic restart period | conditional |
90  fields | list of fields and averaging options | yes |
91  \endtable
92 
93  Note:
94  To employ the \c prime2Mean option, the \c mean option must be selected.
95 
96 See also
97  Foam::functionObjects::fvMeshFunctionObject
98  Foam::functionObject
99 
100 SourceFiles
101  fieldAverage.C
102  fieldAverageTemplates.C
103  fieldAverageItem.C
104 
105 \*---------------------------------------------------------------------------*/
106 
107 #ifndef functionObjects_fieldAverage_H
108 #define functionObjects_fieldAverage_H
109 
110 #include "fvMeshFunctionObject.H"
111 
112 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
113 
114 namespace Foam
115 {
116 namespace functionObjects
117 {
118 
119 // Forward declaration of classes
120 class fieldAverageItem;
121 
122 /*---------------------------------------------------------------------------*\
123  Class fieldAverage Declaration
124 \*---------------------------------------------------------------------------*/
125 
126 class fieldAverage
127 :
128  public fvMeshFunctionObject
129 {
130 public:
131 
132  //- Enumeration defining the averaging base type
133  enum class baseType
134  {
135  iter,
136  time
137  };
138 
139 
140 protected:
141 
142  // Protected data
143 
144  //- Time at last call, prevents repeated averaging
146 
147  //- Restart the averaging process on restart
148  Switch restartOnRestart_;
149 
150  //- Restart the averaging process on output
151  Switch restartOnOutput_;
152 
153  //- Periodically restart the averaging process
154  Switch periodicRestart_;
155 
156  //- Restart period
157  scalar restartPeriod_;
158 
159  //- Averaging base type names
161 
162  //- Averaging base type
163  baseType base_;
164 
165  //- Averaging window - defaults to -1 for 'all iters/time'
166  scalar window_;
167 
168  //- Averaging window name - defaults to 'window'
170 
171  //- Compute mean flag
172  Switch mean_;
173 
174  //- Compute prime-squared mean flag
176 
177  //- List of field average items, describing what averages to be
178  // calculated and output
180 
181  //- Iteration steps counter
183 
184  //- Total time counter
186 
187  //- Index for periodic restart
189 
190 
191  // Protected Member Functions
192 
193  // Initialisation routines
194 
195  //- Reset lists (clear existing values) and initialise averaging.
196  // Check requested field averages are valid, populate field lists
197  void initialise();
198 
199  //- Restart averaging for restartOnOutput
200  void restart();
201 
202  //- Read the mean average field
203  template<class Type>
204  void readMeanFieldType(const label fieldi);
205 
206  //- Read the mean average field
207  template<class Type>
208  void readMeanField(const label fieldi);
209 
210  //- Initialise the mean average field
211  template<class Type>
212  void initialiseMeanFieldType(const label fieldi);
213 
214  //- Initialise the mean average field
215  template<class Type>
216  void initialiseMeanField(const label fieldi);
217 
218  //- Read the prime-squared average field
219  template<class Type1, class Type2>
220  void readPrime2MeanFieldType(const label fieldi);
221 
222  //- Read the prime-squared average field
223  template<class Type1, class Type2>
224  void readPrime2MeanField(const label fieldi);
225 
226  //- Initialise the prime-squared average field
227  template<class Type1, class Type2>
228  void initialisePrime2MeanFieldType(const label fieldi);
229 
230  //- Initialise the prime-squared average field
231  template<class Type1, class Type2>
232  void initialisePrime2MeanField(const label fieldi);
233 
234 
235  // Calculation functions
236 
237  //- Main calculation routine
238  virtual void calcAverages();
239 
240  //- Calculate mean average fields
241  template<class Type>
242  void calculateMeanFieldType(const label fieldi) const;
243 
244  //- Calculate mean average fields
245  template<class Type>
246  void calculateMeanFields() const;
247 
248  //- Calculate prime-squared average fields
249  template<class Type1, class Type2>
250  void calculatePrime2MeanFieldType(const label fieldi) const;
251 
252  //- Calculate prime-squared average fields
253  template<class Type1, class Type2>
254  void calculatePrime2MeanFields() const;
255 
256  //- Add mean-squared field value to prime-squared mean field
257  template<class Type1, class Type2>
258  void addMeanSqrToPrime2MeanType(const label fieldi) const;
259 
260  //- Add mean-squared field value to prime-squared mean field
261  template<class Type1, class Type2>
262  void addMeanSqrToPrime2Mean() const;
263 
264 
265  // I-O
266 
267  //- Write averages
268  virtual void writeAverages() const;
269 
270  //- Write fields
271  template<class Type>
272  void writeFieldType(const word& fieldName) const;
273 
274  //- Write fields
275  template<class Type>
276  void writeFields() const;
277 
278  //- Read
279  void read(const dictionary& dict, const bool construct);
280 
281 
282 public:
283 
284  friend class fieldAverageItem;
285 
286 
287  //- Runtime type information
288  TypeName("fieldAverage");
289 
290 
291  // Constructors
292 
293  //- Construct from Time and dictionary
295  (
296  const word& name,
297  const Time& runTime,
298  const dictionary&
299  );
300 
301  //- Disallow default bitwise copy construction
302  fieldAverage(const fieldAverage&) = delete;
303 
304 
305  //- Destructor
306  virtual ~fieldAverage();
307 
308 
309  // Member Functions
310 
311  // Access
312 
313  //- Return averaging base type name
314  const word base() const
315  {
316  return baseTypeNames_[base_];
317  }
318 
319  //- Return true if base is iter
320  Switch iterBase() const
321  {
323  }
324 
325  //- Return true if base is time
326  Switch timeBase() const
327  {
328  return base_ == baseType::time;
329  }
330 
331  scalar window() const
332  {
333  return window_;
334  }
335 
336  const word& windowName() const
337  {
338  return windowName_;
339  }
340 
341 
342  //- Read the field average data
343  virtual bool read(const dictionary&);
344 
345  //- Return the list of fields required
346  virtual wordList fields() const;
347 
348  //- Do not average at the start of the run
349  virtual bool executeAtStart() const
350  {
351  return false;
352  }
353 
354  //- Calculate the field averages
355  virtual bool execute();
356 
357  //- Write the field averages
358  virtual bool write();
359 
360 
361  // Member Operators
362 
363  //- Disallow default bitwise assignment
364  void operator=(const fieldAverage&) = delete;
365 };
366 
367 
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
369 
370 } // End namespace functionObjects
371 } // End namespace Foam
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
375 #ifdef NoRepository
376  #include "fieldAverageTemplates.C"
377 #endif
378 
379 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
380 
381 #endif
382 
383 // ************************************************************************* //
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
const word & name() const
Return the name of this functionObject.
Helper class to describe what form of averaging to apply. A set will be applied to each base field in...
Calculates average quantities for a user-specified selection of volumetric and surface fields.
Definition: fieldAverage.H:163
Switch periodicRestart_
Periodically restart the averaging process.
Definition: fieldAverage.H:188
baseType
Enumeration defining the averaging base type.
Definition: fieldAverage.H:168
void readPrime2MeanField(const label fieldi)
Read the prime-squared average field.
void initialiseMeanField(const label fieldi)
Initialise the mean average field.
Switch prime2Mean_
Compute prime-squared mean flag.
Definition: fieldAverage.H:209
TypeName("fieldAverage")
Runtime type information.
label prevTimeIndex_
Time at last call, prevents repeated averaging.
Definition: fieldAverage.H:179
void initialise()
Reset lists (clear existing values) and initialise averaging.
Definition: fieldAverage.C:59
void readMeanFieldType(const label fieldi)
Read the mean average field.
void restart()
Restart averaging for restartOnOutput.
Definition: fieldAverage.C:87
const word base() const
Return averaging base type name.
Definition: fieldAverage.H:348
Switch restartOnRestart_
Restart the averaging process on restart.
Definition: fieldAverage.H:182
Switch restartOnOutput_
Restart the averaging process on output.
Definition: fieldAverage.H:185
label periodIndex_
Index for periodic restart.
Definition: fieldAverage.H:222
static const NamedEnum< baseType, 2 > baseTypeNames_
Averaging base type names.
Definition: fieldAverage.H:194
virtual void calcAverages()
Main calculation routine.
Definition: fieldAverage.C:123
scalar restartPeriod_
Restart period.
Definition: fieldAverage.H:191
virtual wordList fields() const
Return the list of fields required.
Definition: fieldAverage.C:448
virtual ~fieldAverage()
Destructor.
Definition: fieldAverage.C:428
void readPrime2MeanFieldType(const label fieldi)
Read the prime-squared average field.
Switch iterBase() const
Return true if base is iter.
Definition: fieldAverage.H:354
void read(const dictionary &dict, const bool construct)
Read.
Definition: fieldAverage.C:212
void calculatePrime2MeanFields() const
Calculate prime-squared average fields.
List< scalar > totalTime_
Total time counter.
Definition: fieldAverage.H:219
List< label > totalIter_
Iteration steps counter.
Definition: fieldAverage.H:216
void initialisePrime2MeanFieldType(const label fieldi)
Initialise the prime-squared average field.
void operator=(const fieldAverage &)=delete
Disallow default bitwise assignment.
Switch timeBase() const
Return true if base is time.
Definition: fieldAverage.H:360
PtrList< fieldAverageItem > faItems_
List of field average items, describing what averages to be.
Definition: fieldAverage.H:213
baseType base_
Averaging base type.
Definition: fieldAverage.H:197
void addMeanSqrToPrime2MeanType(const label fieldi) const
Add mean-squared field value to prime-squared mean field.
scalar window_
Averaging window - defaults to -1 for 'all iters/time'.
Definition: fieldAverage.H:200
void readMeanField(const label fieldi)
Read the mean average field.
Switch mean_
Compute mean flag.
Definition: fieldAverage.H:206
void calculatePrime2MeanFieldType(const label fieldi) const
Calculate prime-squared average fields.
virtual void writeAverages() const
Write averages.
Definition: fieldAverage.C:172
void writeFieldType(const word &fieldName) const
Write fields.
virtual bool executeAtStart() const
Do not average at the start of the run.
Definition: fieldAverage.H:383
void calculateMeanFieldType(const label fieldi) const
Calculate mean average fields.
void initialiseMeanFieldType(const label fieldi)
Initialise the mean average field.
virtual bool execute()
Calculate the field averages.
Definition: fieldAverage.C:461
virtual bool write()
Write the field averages.
Definition: fieldAverage.C:478
void initialisePrime2MeanField(const label fieldi)
Initialise the prime-squared average field.
fieldAverage(const word &name, const Time &runTime, const dictionary &)
Construct from Time and dictionary.
Definition: fieldAverage.C:381
word windowName_
Averaging window name - defaults to 'window'.
Definition: fieldAverage.H:203
void calculateMeanFields() const
Calculate mean average fields.
void addMeanSqrToPrime2Mean() const
Add mean-squared field value to prime-squared mean field.
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
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
dictionary dict