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