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 
131 public:
132 
133  //- Enumeration defining the averaging base type
134  enum class baseType
135  {
136  iter,
137  time
138  };
139 
140 
141 protected:
142 
143  // Protected data
144 
145  //- Time at last call, prevents repeated averaging
147 
148  //- Restart the averaging process on restart
149  Switch restartOnRestart_;
150 
151  //- Restart the averaging process on output
152  Switch restartOnOutput_;
153 
154  //- Periodically restart the averaging process
155  Switch periodicRestart_;
156 
157  //- Restart period
158  scalar restartPeriod_;
159 
160  //- Initialised flag
161  bool initialised_;
162 
163  //- Averaging base type names
165 
166  //- Averaging base type
167  baseType base_;
169  //- Averaging window - defaults to -1 for 'all iters/time'
170  scalar window_;
171 
172  //- Averaging window name - defaults to 'window'
174 
175  //- Compute mean flag
176  Switch mean_;
177 
178  //- Compute prime-squared mean flag
181  //- List of field average items, describing what averages to be
182  // calculated and output
184 
185  // Counters
187  //- Iteration steps counter
190  //- Total time counter
193  //- Index for periodic restart
196 
197  // Protected Member Functions
199  // Initialisation routines
200 
201  //- Checkout fields (causes deletion) from the database
202  // and reset lists
203  void resetFields();
205  //- Reset lists (clear existing values) and initialise averaging.
206  // Check requested field averages are valid, populate field lists
207  void initialise();
208 
209  //- Restart averaging for restartOnOutput
210  void restart();
211 
212  //- Add mean average field to database
213  template<class Type>
214  void addMeanFieldType(const label fieldi);
215 
216  //- Add mean average field to database
217  template<class Type>
218  void addMeanField(const label fieldi);
219 
220  //- Add prime-squared average field to database
221  template<class Type1, class Type2>
222  void addPrime2MeanFieldType(const label fieldi);
223 
224  //- Add prime-squared average field to database
225  template<class Type1, class Type2>
226  void addPrime2MeanField(const label fieldi);
227 
229  // Calculation functions
230 
231  //- Main calculation routine
232  virtual void calcAverages();
233 
234  //- Calculate mean average fields
235  template<class Type>
236  void calculateMeanFieldType(const label fieldi) const;
237 
238  //- Calculate mean average fields
239  template<class Type>
240  void calculateMeanFields() const;
241 
242  //- Calculate prime-squared average fields
243  template<class Type1, class Type2>
244  void calculatePrime2MeanFieldType(const label fieldi) const;
245 
246  //- Calculate prime-squared average fields
247  template<class Type1, class Type2>
248  void calculatePrime2MeanFields() const;
249 
250  //- Add mean-squared field value to prime-squared mean field
251  template<class Type1, class Type2>
252  void addMeanSqrToPrime2MeanType(const label fieldi) const;
253 
254  //- Add mean-squared field value to prime-squared mean field
255  template<class Type1, class Type2>
256  void addMeanSqrToPrime2Mean() const;
257 
258 
259  // I-O
260 
261  //- Write averages
262  virtual void writeAverages() const;
263 
264  //- Write fields
265  template<class Type>
266  void writeFieldType(const word& fieldName) const;
267 
268  //- Write fields
269  template<class Type>
270  void writeFields() const;
271 
272  //- Write averaging properties - steps and time
273  void writeAveragingProperties() const;
274 
275  //- Read averaging properties - steps and time
277 
278 
279 public:
280 
281  friend class fieldAverageItem;
282 
283 
284  //- Runtime type information
285  TypeName("fieldAverage");
286 
287 
288  // Constructors
289 
290  //- Construct from Time and dictionary
292  (
293  const word& name,
294  const Time& runTime,
295  const dictionary&
296  );
297 
298  //- Disallow default bitwise copy construction
299  fieldAverage(const fieldAverage&) = delete;
300 
301 
302  //- Destructor
303  virtual ~fieldAverage();
304 
305 
306  // Member Functions
307 
308  // Access
309 
310  //- Return averaging base type name
311  const word base() const
312  {
313  return baseTypeNames_[base_];
314  }
316  //- Return true if base is iter
317  Switch iterBase() const
318  {
319  return base_ == baseType::iter;
320  }
321 
322  //- Return true if base is time
323  Switch timeBase() const
324  {
325  return base_ == baseType::time;
326  }
327 
328  scalar window() const
329  {
330  return window_;
331  }
332 
333  const word& windowName() const
334  {
335  return windowName_;
336  }
337 
338 
339  //- Read the field average data
340  virtual bool read(const dictionary&);
341 
342  //- Return the list of fields required
343  virtual wordList fields() const;
344 
345  //- Do not average at the start of the run
346  virtual bool executeAtStart() const
347  {
348  return false;
349  }
350 
351  //- Calculate the field averages
352  virtual bool execute();
353 
354  //- Write the field averages
355  virtual bool write();
356 
358  // Member Operators
359 
360  //- Disallow default bitwise assignment
361  void operator=(const fieldAverage&) = delete;
362 };
363 
364 
365 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
366 
367 } // End namespace functionObjects
368 } // End namespace Foam
369 
370 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
371 
372 #ifdef NoRepository
373  #include "fieldAverageTemplates.C"
374 #endif
375 
376 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
377 
378 #endif
379 
380 // ************************************************************************* //
void writeAveragingProperties() const
Write averaging properties - steps and time.
Definition: fieldAverage.C:207
void addPrime2MeanFieldType(const label fieldi)
Add prime-squared average field to database.
void addMeanField(const label fieldi)
Add mean average field to database.
void initialise()
Reset lists (clear existing values) and initialise averaging.
Definition: fieldAverage.C:82
void addMeanSqrToPrime2MeanType(const label fieldi) const
Add mean-squared field value to prime-squared mean field.
Switch timeBase() const
Return true if base is time.
Definition: fieldAverage.H:357
virtual bool read(const dictionary &)
Read the field average data.
Definition: fieldAverage.C:330
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
const word & name() const
Return the name of this functionObject.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
Switch mean_
Compute mean flag.
Definition: fieldAverage.H:210
virtual bool write()
Write the field averages.
Definition: fieldAverage.C:391
PtrList< fieldAverageItem > faItems_
List of field average items, describing what averages to be.
Definition: fieldAverage.H:217
Switch prime2Mean_
Compute prime-squared mean flag.
Definition: fieldAverage.H:213
List< scalar > totalTime_
Total time counter.
Definition: fieldAverage.H:225
static const NamedEnum< baseType, 2 > baseTypeNames_
Averaging base type names.
Definition: fieldAverage.H:198
Switch periodicRestart_
Periodically restart the averaging process.
Definition: fieldAverage.H:189
scalar restartPeriod_
Restart period.
Definition: fieldAverage.H:192
virtual bool executeAtStart() const
Do not average at the start of the run.
Definition: fieldAverage.H:380
baseType base_
Averaging base type.
Definition: fieldAverage.H:201
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none/any.
Definition: Switch.H:60
void calculatePrime2MeanFieldType(const label fieldi) const
Calculate prime-squared average fields.
void addMeanFieldType(const label fieldi)
Add mean average field to database.
void operator=(const fieldAverage &)=delete
Disallow default bitwise assignment.
virtual bool execute()
Calculate the field averages.
Definition: fieldAverage.C:383
void calculateMeanFieldType(const label fieldi) const
Calculate mean average fields.
void calculatePrime2MeanFields() const
Calculate prime-squared average fields.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
scalar window_
Averaging window - defaults to -1 for &#39;all iters/time&#39;.
Definition: fieldAverage.H:204
label prevTimeIndex_
Time at last call, prevents repeated averaging.
Definition: fieldAverage.H:180
void writeFields() const
Write fields.
Switch restartOnOutput_
Restart the averaging process on output.
Definition: fieldAverage.H:186
List< label > totalIter_
Iteration steps counter.
Definition: fieldAverage.H:222
fieldAverage(const word &name, const Time &runTime, const dictionary &)
Construct from Time and dictionary.
Definition: fieldAverage.C:297
const word base() const
Return averaging base type name.
Definition: fieldAverage.H:345
void writeFieldType(const word &fieldName) const
Write fields.
void readAveragingProperties()
Read averaging properties - steps and time.
Definition: fieldAverage.C:237
A class for handling words, derived from string.
Definition: word.H:59
Calculates average quantities for a user-specified selection of volumetric and surface fields...
Definition: fieldAverage.H:160
virtual void calcAverages()
Main calculation routine.
Definition: fieldAverage.C:143
word windowName_
Averaging window name - defaults to &#39;window&#39;.
Definition: fieldAverage.H:207
void calculateMeanFields() const
Calculate mean average fields.
virtual wordList fields() const
Return the list of fields required.
Definition: fieldAverage.C:370
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
Switch restartOnRestart_
Restart the averaging process on restart.
Definition: fieldAverage.H:183
void addPrime2MeanField(const label fieldi)
Add prime-squared average field to database.
void resetFields()
Checkout fields (causes deletion) from the database.
Definition: fieldAverage.C:59
label periodIndex_
Index for periodic restart.
Definition: fieldAverage.H:228
Helper class to describe what form of averaging to apply. A set will be applied to each base field in...
void restart()
Restart averaging for restartOnOutput.
Definition: fieldAverage.C:131
Switch iterBase() const
Return true if base is iter.
Definition: fieldAverage.H:351
TypeName("fieldAverage")
Runtime type information.
void addMeanSqrToPrime2Mean() const
Add mean-squared field value to prime-squared mean field.
virtual ~fieldAverage()
Destructor.
Definition: fieldAverage.C:324
bool initialised_
Initialised flag.
Definition: fieldAverage.H:195
virtual void writeAverages() const
Write averages.
Definition: fieldAverage.C:193
baseType
Enumeration defining the averaging base type.
Definition: fieldAverage.H:168
Namespace for OpenFOAM.