fieldStatistics.H
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) 2025 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::functionObjects::fieldStatistics
28 
29 Group
30  grpFieldFunctionObjects
31 
32 Description
33  Calculates various statistics of the specified fields.
34 
35  Operands:
36  \table
37  Operand | Type | Location
38  input | vol<Type>Field(s) | Registry
39  output file | dat | <case>/postProcessing/<FO>/<time>/<file>
40  output field | - | -
41  \endtable
42 
43  where \c <Type>=Scalar/Vector/SphericalTensor/SymmTensor/Tensor.
44 
45 Usage
46  Minimal example by using \c system/controlDict.functions:
47  \verbatim
48  fieldStatistics1
49  {
50  // Mandatory entries
51  type fieldStatistics;
52  libs (fieldFunctionObjects);
53  fields (<wordList>);
54  statistics (<wordList>);
55 
56  // Optional entries
57  mode <word>;
58  mean <word>;
59  extrema <bool>;
60  internal <bool>;
61 
62  // Inherited entries
63  ...
64  }
65  \endverbatim
66 
67  where the entries mean:
68  \table
69  Property | Description | Type | Reqd | Deflt
70  type | Type name: fieldStatistics | word | yes | -
71  libs | Library name: fieldFunctionObjects | word | yes | -
72  fields | List of operand fields | wordList | yes | -
73  statistics | List of operand statistics | wordList | yes | -
74  mode | Output format of the statistical results | word | no | magnitude
75  mean | Type of the mean operation | word | no | arithmetic
76  internal | Flag to use internal fields only in computing statistics <!--
77  --> | bool | no | false
78  extrema | Flag to enable extrema data calculations | bool | no | false
79  \endtable
80 
81  Available statistics of the operand field through the \c statistics entry:
82  \verbatim
83  min | Minimum value
84  max | Maximum value
85  mean | Arithmetic mean value (optionally volume-weighted)
86  variance | Sample variance value (unbiased)
87  \endverbatim
88 
89  Options for the \c mode entry:
90  \verbatim
91  magnitude | Output statistics magnitude-wise
92  component | Output statistics separately for each component
93  \endverbatim
94 
95  Options for the \c mean entry:
96  \verbatim
97  arithmetic | Arithmetic mean (average)
98  volumetric | Volume-weighted arithmetic mean
99  \endverbatim
100 
101  The inherited entries are elaborated in:
102  - \link functionObject.H \endlink
103  - \link writeFile.H \endlink
104 
105 SourceFiles
106  fieldStatistics.cxx
107  fieldStatisticsImpl.cxx
108 
109 \*---------------------------------------------------------------------------*/
110 
111 #ifndef functionObjects_fieldStatistics_H
112 #define functionObjects_fieldStatistics_H
113 
114 #include "fvMeshFunctionObject.H"
115 #include "writeFile.H"
116 #include "volFieldSelection.H"
117 #include <functional>
118 #include <variant>
119 
120 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
121 
122 namespace Foam
123 {
124 namespace functionObjects
125 {
126 
127 /*---------------------------------------------------------------------------*\
128  Class fieldStatistics Declaration
129 \*---------------------------------------------------------------------------*/
130 
131 class fieldStatistics
132 :
133  public fvMeshFunctionObject,
134  public writeFile
135 {
136  // Private Enumerations
137 
138  //- Options for the output format of the statistical results
139  enum class modeType : char
140  {
141  MAG = 0,
142  CMPT
143  };
144 
145  //- Names for modeType
146  static const Enum<modeType> modeTypeNames_;
147 
148  //- Options for the mean type of the specified fields
149  enum class meanType : char
150  {
151  ARITHMETIC = 0,
152  VOLUMETRIC
153  };
154 
155  //- Names for meanType
156  static const Enum<meanType> meanTypeNames_;
157 
158  //- Options for the type of statistics calculation
159  enum class calcType : char
160  {
161  UNKNOWN = 0,
162  MIN,
163  MAX,
164  MEAN,
165  VARIANCE
166  };
167 
168  //- Names for calcType
169  static const Enum<calcType> calcTypeNames_;
170 
171 
172  // Private Classes
173 
174  //- Type-safe union for input field types
175  using variantInput = std::variant
176  <
177  scalarField,
178  vectorField,
182  >;
183 
184  //- Type-safe union for output data types
185  using variantOutput = std::variant
186  <
187  scalar,
188  vector,
190  symmTensor,
191  tensor
192  >;
193 
194  //- Class to encapsulate information about specified statistic
195  struct statistic
196  {
197  //- Name of the statistic
198  word name_;
199 
200  //- Returns the value of the specified statistic
201  std::function<variantOutput(variantInput)> calc;
202  };
203 
204  //- Class to encapsulate the data about minima and maxima
205  struct extremaData
206  {
207  //- Value of the extremum
208  variantOutput value_;
209 
210  //- Processor index of the extremum
211  label procID_;
212 
213  //- Cell index of the extremum
214  label cellID_;
215 
216  //- Position (cell or face centre) of the extremum
217  point position_;
218  };
219 
220 
221  // Private Data
222 
223  //- Flag to use internal fields only in computing statistics
224  bool internal_;
225 
226  //- Flag to enable extrema data calculations
227  bool extrema_;
228 
229  //- Output-format mode - only applicable for tensor ranks > 0
230  modeType mode_;
231 
232  //- Type of the mean of the specified fields
233  meanType mean_;
234 
235  //- Operand fields on which statistics are computed
236  volFieldSelection fieldSet_;
237 
238  //- List of file pointers; one file per field
239  HashPtrTable<OFstream> filePtrs_;
240 
241  //- List of file pointers for extrema data; one file per field
242  HashPtrTable<OFstream> extremaFilePtrs_;
243 
244  //- Hash table containing all specified statistics
245  HashTable<statistic> statistics_;
246 
247  //- Hash table containing all statistical results per field
249 
250  //- Hash table containing the results of the extrema per field
251  HashTable<Pair<extremaData>> extremaResults_;
252 
253 
254  // Private Member Functions
255 
256  //- Return the statistic container
257  statistic createStatistic(const word& statName, const modeType mode);
258 
259  //- Compute the specified statistics of a given field
260  template<class T>
261  bool calcStat(const word& fieldName);
262 
263 
264  // Central tendency statistics
265 
266  //- Return the arithmetic mean of the given input field
267  template<class T>
268  T calcMean(const Field<T>& field) const;
269 
270 
271  // Dispersion statistics
272 
273  //- Return the minimum value of the given input field
274  // Store the processor index, cell index and location of the minimum
275  template<class T>
276  T calcMin(const Field<T>& field) const;
277 
278  //- Return the maximum value of the given input field
279  // Store the processor index, cell index and location of the maximum
280  template<class T>
281  T calcMax(const Field<T>& field) const;
282 
283  //- Return the sample variance of the given input field
284  template<class T>
285  T calcVariance(const Field<T>& field) const;
286 
287  //- Return a combined field: internal + flattened boundary
288  template<class GeoField>
290  flatten(const GeoField& field) const;
291 
292  //- Return the extrema data of the specified field
293  template<class GeoField>
294  Pair<extremaData> calcExtremaData(const GeoField& field) const;
295 
296  //- Output the file header information
297  void writeFileHeader(Ostream& os, const word& fieldName);
298 
299  //- Write the statistical data to the specified file
300  void writeStatData();
301 
302  //- Write the statistical data to the standard stream
303  void logStatData();
304 
305  //- Output the extrema-data file header information
306  void writeExtremaFileHeader(Ostream& os, const word& fieldName);
307 
308  //- Write extrema data to the specified file
309  void writeExtremaData();
310 
311  //- Write extrema data to the standard stream
312  void logExtremaData();
313 
314 
315 public:
316 
317  //- Runtime type information
318  TypeName("fieldStatistics");
319 
320 
321  // Generated Methods
322 
323  //- No copy construct
324  fieldStatistics(const fieldStatistics&) = delete;
325 
326  //- No copy assignment
327  void operator=(const fieldStatistics&) = delete;
328 
329 
330  // Constructors
331 
332  //- Construct from name, Time and dictionary
334  (
335  const word& name,
336  const Time& runTime,
337  const dictionary& dict
338  );
339 
340 
341  //- Destructor
342  virtual ~fieldStatistics() = default;
343 
344 
345  // Member Functions
346 
347  //- Read function object settings
348  virtual bool read(const dictionary&);
349 
350  //- Execute function object
351  virtual bool execute();
352 
353  //- Write function object results
354  virtual bool write();
355 };
356 
357 
358 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
359 
360 } // End namespace functionObjects
361 } // End namespace Foam
362 
363 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
364 
365 #endif
366 
367 // ************************************************************************* //
dictionary dict
rDeltaTY field()
Field< sphericalTensor > sphericalTensorField
Specialisation of Field<T> for sphericalTensor.
Field< symmTensor > symmTensorField
Specialisation of Field<T> for symmTensor.
virtual ~fieldStatistics()=default
Destructor.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
engineTime & runTime
Tensor< scalar > tensor
Definition: symmTensor.H:57
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers...
Definition: HashPtrTable.H:51
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
const word & name() const noexcept
Return the name of this functionObject.
void operator=(const fieldStatistics &)=delete
No copy assignment.
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition: symmTensor.H:55
A class for handling words, derived from Foam::string.
Definition: word.H:63
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
virtual bool write()
Write function object results.
Vector< scalar > vector
Definition: vector.H:57
virtual bool execute()
Execute function object.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
OBJstream os(runTime.globalPath()/outputName)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Field< tensor > tensorField
Specialisation of Field<T> for tensor.
TypeName("fieldStatistics")
Runtime type information.
virtual bool read(const dictionary &)
Read function object settings.
Field< vector > vectorField
Specialisation of Field<T> for vector.
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
A class for managing temporary objects.
Definition: HashPtrTable.H:50
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: POSIX.C:775
Namespace for OpenFOAM.
fieldStatistics(const fieldStatistics &)=delete
No copy construct.