fieldAverageTemplates.C
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-2023 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 \*---------------------------------------------------------------------------*/
25 
26 #include "fieldAverage.H"
27 #include "fieldAverageItem.H"
28 #include "volFields.H"
29 #include "surfaceFields.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class Type>
35 {
36  const word& meanFieldName = faItems_[fieldi].meanFieldName();
37 
38  IOobject meanFieldIo
39  (
40  meanFieldName,
41  time_.name(),
42  obr_,
45  );
46 
47  if
48  (
49  meanFieldIo.headerOk()
50  && meanFieldIo.headerClassName() == Type::typeName
51  )
52  {
53  if (obr_.found(meanFieldName))
54  {
55  Log << " Cannot read average field " << meanFieldName
56  << " since an object with that name already exists."
57  << " Disabling averaging for field." << endl;
58 
59  faItems_[fieldi].mean() = false;
60  }
61  else
62  {
63  Log << " Reading field " << meanFieldName << endl;
64 
65  obr_.store(new Type(meanFieldIo, mesh_));
66  }
67  }
68 }
69 
70 
71 template<class Type>
73 {
74  if (faItems_[fieldi].mean())
75  {
76  readMeanFieldType<VolField<Type>>(fieldi);
77  readMeanFieldType<VolInternalField<Type>>(fieldi);
78  readMeanFieldType<SurfaceField<Type>>(fieldi);
79  }
80 }
81 
82 
83 template<class Type>
85 (
86  const label fieldi
87 )
88 {
89  const word& fieldName = faItems_[fieldi].fieldName();
90  const word& meanFieldName = faItems_[fieldi].meanFieldName();
91 
92  if (obr_.foundObject<Type>(meanFieldName))
93  {
94  // Do nothing ...
95  }
96  else if (obr_.found(meanFieldName))
97  {
98  Log << " Cannot initialise average field " << meanFieldName
99  << " since an object with that name already exists."
100  << " Disabling averaging for field." << endl;
101 
102  faItems_[fieldi].mean() = false;
103  }
104  else
105  {
106  Log << " Initialising field " << meanFieldName << endl;
107 
108  const Type& baseField = obr_.lookupObject<Type>(fieldName);
109 
110  obr_.store
111  (
112  new Type
113  (
114  IOobject
115  (
116  meanFieldName,
117  time_.name(),
118  obr_
119  ),
120  1*baseField
121  )
122  );
123  }
124 }
125 
126 
127 template<class Type>
129 (
130  const label fieldi
131 )
132 {
133  if (faItems_[fieldi].mean())
134  {
135  const word& fieldName = faItems_[fieldi].fieldName();
136 
137  if (obr_.foundObject<VolField<Type>>(fieldName))
138  {
139  initialiseMeanFieldType<VolField<Type>>(fieldi);
140  }
141  else if (obr_.foundObject<VolInternalField<Type>>(fieldName))
142  {
143  initialiseMeanFieldType<VolInternalField<Type>>(fieldi);
144  }
145  else if (obr_.foundObject<SurfaceField<Type>>(fieldName))
146  {
147  initialiseMeanFieldType<SurfaceField<Type>>(fieldi);
148  }
149  }
150 }
151 
152 
153 template<class Type1, class Type2>
155 (
156  const label fieldi
157 )
158 {
159  const word& prime2MeanFieldName = faItems_[fieldi].prime2MeanFieldName();
160 
161  IOobject prime2MeanFieldIo
162  (
163  prime2MeanFieldName,
164  time_.name(),
165  obr_,
168  );
169 
170  if
171  (
172  prime2MeanFieldIo.headerOk()
173  && prime2MeanFieldIo.headerClassName() == Type2::typeName
174  )
175  {
176  if (obr_.found(prime2MeanFieldName))
177  {
178  Log << " Cannot read average field " << prime2MeanFieldName
179  << " since an object with that name already exists."
180  << " Disabling averaging for field." << endl;
181 
182  faItems_[fieldi].mean() = false;
183  }
184  else
185  {
186  Log << " Reading field " << prime2MeanFieldName << endl;
187 
188  obr_.store(new Type2(prime2MeanFieldIo, mesh_));
189  }
190  }
191 }
192 
193 
194 template<class Type1, class Type2>
196 (
197  const label fieldi
198 )
199 {
200  if (faItems_[fieldi].prime2Mean())
201  {
202  if (!faItems_[fieldi].mean())
203  {
205  << "To calculate the prime-squared average, the "
206  << "mean average must also be selected for field "
207  << faItems_[fieldi].fieldName() << nl << exit(FatalError);
208  }
209 
210  readPrime2MeanFieldType
211  <VolField<Type1>, VolField<Type2>>(fieldi);
212  readPrime2MeanFieldType
214  readPrime2MeanFieldType
216  }
217 }
218 
219 
220 template<class Type1, class Type2>
222 (
223  const label fieldi
224 )
225 {
226  const word& fieldName = faItems_[fieldi].fieldName();
227  const word& meanFieldName = faItems_[fieldi].meanFieldName();
228  const word& prime2MeanFieldName = faItems_[fieldi].prime2MeanFieldName();
229 
230  if (obr_.foundObject<Type2>(prime2MeanFieldName))
231  {
232  // Do nothing ...
233  }
234  else if (obr_.found(prime2MeanFieldName))
235  {
236  Log << " Cannot initialise average field " << prime2MeanFieldName
237  << " since an object with that name already exists."
238  << " Disabling averaging for field." << nl;
239 
240  faItems_[fieldi].prime2Mean() = false;
241  }
242  else
243  {
244  Log << " Initialising field " << prime2MeanFieldName << nl;
245 
246  const Type1& baseField = obr_.lookupObject<Type1>(fieldName);
247  const Type1& meanField = obr_.lookupObject<Type1>(meanFieldName);
248 
249  obr_.store
250  (
251  new Type2
252  (
253  IOobject
254  (
255  prime2MeanFieldName,
256  time_.name(),
257  obr_
258  ),
259  sqr(baseField) - sqr(meanField)
260  )
261  );
262  }
263 }
264 
265 
266 template<class Type1, class Type2>
268 (
269  const label fieldi
270 )
271 {
272  if (faItems_[fieldi].prime2Mean())
273  {
274  const word& fieldName = faItems_[fieldi].fieldName();
275 
276  if (!faItems_[fieldi].mean())
277  {
279  << "To calculate the prime-squared average, the "
280  << "mean average must also be selected for field "
281  << fieldName << nl << exit(FatalError);
282  }
283 
284  if (obr_.foundObject<VolField<Type1>>(fieldName))
285  {
286  initialisePrime2MeanFieldType
287  <VolField<Type1>, VolField<Type2>>(fieldi);
288  }
289  else if (obr_.foundObject<VolInternalField<Type1>>(fieldName))
290  {
291  initialisePrime2MeanFieldType
293  }
294  else if (obr_.foundObject<SurfaceField<Type1>>(fieldName))
295  {
296  initialisePrime2MeanFieldType
298  }
299  }
300 }
301 
302 
303 template<class Type>
305 (
306  const label fieldi
307 ) const
308 {
309  const word& fieldName = faItems_[fieldi].fieldName();
310 
311  const Type& baseField = obr_.lookupObject<Type>(fieldName);
312 
313  Type& meanField =
314  obr_.lookupObjectRef<Type>(faItems_[fieldi].meanFieldName());
315 
316  scalar dt = time_.deltaTValue();
317  scalar Dt = totalTime_[fieldi];
318 
319  if (iterBase())
320  {
321  dt = 1;
322  Dt = scalar(totalIter_[fieldi]);
323  }
324 
325  scalar beta = dt/Dt;
326 
327  if (window() > 0)
328  {
329  const scalar w = window();
330 
331  if (Dt - dt >= w)
332  {
333  beta = dt/w;
334  }
335  }
336 
337  meanField = (1 - beta)*meanField + beta*baseField;
338 }
339 
340 
341 template<class Type>
343 {
344  forAll(faItems_, fieldi)
345  {
346  if (faItems_[fieldi].mean())
347  {
348  const word& fieldName = faItems_[fieldi].fieldName();
349 
350  if (obr_.foundObject<VolField<Type>>(fieldName))
351  {
352  calculateMeanFieldType<VolField<Type>>(fieldi);
353  }
354  else if (obr_.foundObject<VolInternalField<Type>>(fieldName))
355  {
356  calculateMeanFieldType<VolInternalField<Type>>(fieldi);
357  }
358  else if (obr_.foundObject<SurfaceField<Type>>(fieldName))
359  {
360  calculateMeanFieldType<SurfaceField<Type>>(fieldi);
361  }
362  }
363  }
364 }
365 
366 
367 template<class Type1, class Type2>
369 (
370  const label fieldi
371 ) const
372 {
373  const word& fieldName = faItems_[fieldi].fieldName();
374 
375  const Type1& baseField = obr_.lookupObject<Type1>(fieldName);
376  const Type1& meanField =
377  obr_.lookupObject<Type1>(faItems_[fieldi].meanFieldName());
378 
379  Type2& prime2MeanField =
380  obr_.lookupObjectRef<Type2>(faItems_[fieldi].prime2MeanFieldName());
381 
382  scalar dt = time_.deltaTValue();
383  scalar Dt = totalTime_[fieldi];
384 
385  if (iterBase())
386  {
387  dt = 1;
388  Dt = scalar(totalIter_[fieldi]);
389  }
390 
391  scalar beta = dt/Dt;
392 
393  if (window() > 0)
394  {
395  const scalar w = window();
396 
397  if (Dt - dt >= w)
398  {
399  beta = dt/w;
400  }
401  }
402 
403  prime2MeanField =
404  (1 - beta)*prime2MeanField
405  + beta*sqr(baseField)
406  - sqr(meanField);
407 }
408 
409 
410 template<class Type1, class Type2>
412 {
413  forAll(faItems_, fieldi)
414  {
415  if (faItems_[fieldi].prime2Mean())
416  {
417  const word& fieldName = faItems_[fieldi].fieldName();
418 
419  if (obr_.foundObject<VolField<Type1>>(fieldName))
420  {
421  calculatePrime2MeanFieldType
422  <VolField<Type1>, VolField<Type2>>(fieldi);
423  }
424  else if (obr_.foundObject<VolInternalField<Type1>>(fieldName))
425  {
426  calculatePrime2MeanFieldType
428  }
429  else if (obr_.foundObject<SurfaceField<Type1>>(fieldName))
430  {
431  calculatePrime2MeanFieldType
433  }
434  }
435  }
436 }
437 
438 
439 template<class Type1, class Type2>
441 (
442  const label fieldi
443 ) const
444 {
445  const Type1& meanField =
446  obr_.lookupObject<Type1>(faItems_[fieldi].meanFieldName());
447 
448  Type2& prime2MeanField =
449  obr_.lookupObjectRef<Type2>(faItems_[fieldi].prime2MeanFieldName());
450 
451  prime2MeanField += sqr(meanField);
452 }
453 
454 
455 template<class Type1, class Type2>
457 {
458  forAll(faItems_, fieldi)
459  {
460  if (faItems_[fieldi].prime2Mean())
461  {
462  const word& fieldName = faItems_[fieldi].fieldName();
463 
464  if (obr_.foundObject<VolField<Type1>>(fieldName))
465  {
466  addMeanSqrToPrime2MeanType
467  <VolField<Type1>, VolField<Type2>>(fieldi);
468  }
469  else if (obr_.foundObject<VolInternalField<Type1>>(fieldName))
470  {
471  addMeanSqrToPrime2MeanType
473  }
474  else if (obr_.foundObject<SurfaceField<Type1>>(fieldName))
475  {
476  addMeanSqrToPrime2MeanType
478  }
479  }
480  }
481 }
482 
483 
484 template<class Type>
486 (
487  const word& fieldName
488 ) const
489 {
490  if (obr_.foundObject<Type>(fieldName))
491  {
492  const Type& f = obr_.lookupObject<Type>(fieldName);
493  f.write();
494  }
495 }
496 
497 
498 template<class Type>
500 {
501  forAll(faItems_, fieldi)
502  {
503  if (faItems_[fieldi].mean())
504  {
505  const word& fieldName = faItems_[fieldi].meanFieldName();
506  writeFieldType<VolField<Type>>(fieldName);
507  writeFieldType<VolInternalField<Type>>(fieldName);
508  writeFieldType<SurfaceField<Type>>(fieldName);
509  }
510  if (faItems_[fieldi].prime2Mean())
511  {
512  const word& fieldName = faItems_[fieldi].prime2MeanFieldName();
513  writeFieldType<VolField<Type>>(fieldName);
514  writeFieldType<VolInternalField<Type>>(fieldName);
515  writeFieldType<SurfaceField<Type>>(fieldName);
516  }
517  }
518 }
519 
520 
521 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Generic GeometricField class.
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:138
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
bool headerOk()
Read header of local object without type-checking.
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:316
const word & name() const
Return const reference to name.
const Time & time_
Reference to time.
void readPrime2MeanField(const label fieldi)
Read the prime-squared average field.
void initialiseMeanField(const label fieldi)
Initialise the mean average field.
void readMeanFieldType(const label fieldi)
Read the mean average field.
void readPrime2MeanFieldType(const label fieldi)
Read the prime-squared average field.
void calculatePrime2MeanFields() const
Calculate prime-squared average fields.
void initialisePrime2MeanFieldType(const label fieldi)
Initialise the prime-squared average field.
PtrList< fieldAverageItem > faItems_
List of field average items, describing what averages to be.
Definition: fieldAverage.H:213
void addMeanSqrToPrime2MeanType(const label fieldi) const
Add mean-squared field value to prime-squared mean field.
void readMeanField(const label fieldi)
Read the mean average field.
void calculatePrime2MeanFieldType(const label fieldi) const
Calculate prime-squared average fields.
void writeFieldType(const word &fieldName) const
Write fields.
void calculateMeanFieldType(const label fieldi) const
Calculate mean average fields.
void initialiseMeanFieldType(const label fieldi)
Initialise the mean average field.
void initialisePrime2MeanField(const label fieldi)
Initialise the prime-squared average field.
void calculateMeanFields() const
Calculate mean average fields.
void addMeanSqrToPrime2Mean() const
Add mean-squared field value to prime-squared mean field.
const fvMesh & mesh_
Reference to the fvMesh.
const objectRegistry & obr_
Reference to the region objectRegistry.
void store()
Transfer ownership of this object to its registry.
Definition: regIOobjectI.H:40
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
#define Log
Report write to Foam::Info if the local log switch is true.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
typename VolField< Type >::Internal VolInternalField
Definition: volFieldsFwd.H:61
error FatalError
static const char nl
Definition: Ostream.H:266
labelList f(nPoints)
Foam::surfaceFields.