fieldAverageTemplates.C
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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2015-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "fieldAverageItem.H"
30 #include "volFields.H"
31 #include "surfaceFields.H"
33 #include "OFstream.H"
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 template<class Type>
39 (
40  fieldAverageItem& item
41 )
42 {
43  const Type* fieldPtr = findObject<Type>(item.fieldName());
44 
45  if (!fieldPtr)
46  {
47  return false;
48  }
49 
50  // Field has been found, so set active flag to true
51  item.active() = true;
52 
53  const word& meanFieldName = item.meanFieldName();
54 
55  Log << " Reading/initialising field " << meanFieldName << endl;
56 
57  if (foundObject<Type>(meanFieldName))
58  {}
59  else if (obr().found(meanFieldName))
60  {
61  Log << " Cannot allocate average field " << meanFieldName
62  << " since an object with that name already exists."
63  << " Disabling averaging for field." << endl;
64 
65  item.mean() = false;
66  }
67  else
68  {
69  const Type& baseField = *fieldPtr;
70 
71  // Store on registry
72  obr().store
73  (
74  new Type
75  (
76  IOobject
77  (
78  meanFieldName,
79  obr().time().timeName(obr().time().startTime().value()),
80  obr(),
81  (
85  ),
87  ),
88  1*baseField
89  )
90  );
91 
92  return true;
93  }
94 
95  return false;
96 }
97 
98 
99 template<class Type>
101 (
102  fieldAverageItem& item
103 )
104 {
105  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
106  typedef typename VolFieldType::Internal VolFieldInternalType;
107  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
108  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
109 
110  bool added = false;
111 
112  if (item.mean())
113  {
114  added =
115  (
116  addMeanFieldType<VolFieldType>(item)
117  || addMeanFieldType<VolFieldInternalType>(item)
118  || addMeanFieldType<SurfaceFieldType>(item)
119  || addMeanFieldType<SurfFieldType>(item)
120  );
121  }
123  return added;
124 }
125 
126 
127 template<class Type>
129 (
130  const fieldAverageItem& item
131 )
132 {
133  if (restartOnOutput_)
134  {
135  return false;
136  }
137 
138  const word& fieldName = item.fieldName();
139 
140  const Type* fieldPtr = findObject<Type>(fieldName);
141 
142  if (!fieldPtr)
143  {
144  return false;
145  }
146 
147  const FIFOStack<word>& fieldNames = item.windowFieldNames();
148 
149  forAllConstIters(fieldNames, fieldIter)
150  {
151  const word& name = fieldIter();
152 
153  IOobject io
154  (
155  name,
156  obr().time().timeName(obr().time().startTime().value()),
157  obr(),
161  );
162 
163  if (io.typeHeaderOk<Type>(true))
164  {
165  DebugInfo << "Read and store: " << name << endl;
166  obr().store(new Type(io, fieldPtr->mesh()));
167  }
168  else
169  {
171  << "Unable to read window " << Type::typeName << " " << name
172  << ". Averaging restart behaviour may be compromised"
173  << endl;
174  }
175  }
177  return true;
178 }
179 
180 
181 template<class Type>
183 (
184  const fieldAverageItem& item
185 )
186 {
187  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
188  typedef typename VolFieldType::Internal VolFieldInternalType;
189  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
190  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
191 
192  if (item.window() > 0)
193  {
194  (void)
195  (
196  restoreWindowFieldsType<VolFieldType>(item)
197  || restoreWindowFieldsType<VolFieldInternalType>(item)
198  || restoreWindowFieldsType<SurfaceFieldType>(item)
199  || restoreWindowFieldsType<SurfFieldType>(item)
200  );
201  }
202 }
203 
204 
205 template<class Type1, class Type2>
207 (
208  fieldAverageItem& item
209 )
210 {
211  const auto* baseFieldPtr = findObject<Type1>(item.fieldName());
212 
213  if (!baseFieldPtr)
214  {
215  return false;
216  }
217 
218  const word& meanFieldName = item.meanFieldName();
219  const word& prime2MeanFieldName = item.prime2MeanFieldName();
220 
221  Log << " Reading/initialising field " << prime2MeanFieldName << nl;
222 
223  if (foundObject<Type2>(prime2MeanFieldName))
224  {}
225  else if (obr().found(prime2MeanFieldName))
226  {
227  Log << " Cannot allocate average field " << prime2MeanFieldName
228  << " since an object with that name already exists."
229  << " Disabling averaging for field." << endl;
230 
231  item.prime2Mean() = false;
232  }
233  else
234  {
235  const auto& baseField = *baseFieldPtr;
236  const Type1& meanField = lookupObject<Type1>(meanFieldName);
237 
238  // Store on registry
239  obr().store
240  (
241  new Type2
242  (
243  IOobject
244  (
245  prime2MeanFieldName,
246  obr().time().timeName(obr().time().startTime().value()),
247  obr(),
248  restartOnOutput_?
252  ),
253  sqr(baseField) - sqr(meanField)
254  )
255  );
256 
257  return true;
258  }
260  return false;
261 }
262 
263 
264 template<class Type1, class Type2>
266 (
267  fieldAverageItem& item
268 )
269 {
270  typedef GeometricField<Type1, fvPatchField, volMesh> VolFieldType1;
271  typedef typename VolFieldType1::Internal VolFieldInternalType1;
272  typedef GeometricField<Type1, fvsPatchField, surfaceMesh> SurfaceFieldType1;
273  typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
274 
275  typedef GeometricField<Type2, fvPatchField, volMesh> VolFieldType2;
276  typedef typename VolFieldType2::Internal VolFieldInternalType2;
277  typedef GeometricField<Type2, fvsPatchField, surfaceMesh> SurfaceFieldType2;
278  typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
279 
280  bool added = false;
281 
282  if (item.prime2Mean())
283  {
284  if (!item.mean())
285  {
287  << "To calculate the prime-squared average, the "
288  << "mean average must also be selected for field "
289  << item.fieldName() << nl << exit(FatalError);
290  }
291 
292  added =
293  addPrime2MeanFieldType<VolFieldType1, VolFieldType2>(item)
294  || addPrime2MeanFieldType<VolFieldInternalType1, VolFieldInternalType2>
295  (
296  item
297  )
298  || addPrime2MeanFieldType<SurfaceFieldType1, SurfaceFieldType2>(item)
299  || addPrime2MeanFieldType<SurfFieldType1, SurfFieldType2>(item);
300  }
302  return added;
303 }
304 
305 
306 template<class Type>
308 (
309  fieldAverageItem& item
310 )
311 {
312  const auto* fPtr = findObject<Type>(item.fieldName());
313 
314  if (!fPtr)
315  {
316  return false;
317  }
318 
319  const Type& baseField = *fPtr;
320 
321  const word windowFieldName = item.windowFieldName(this->name());
322 
323  // Store on registry
324  obr().store
325  (
326  new Type
327  (
328  IOobject
329  (
330  windowFieldName,
331  obr().time().timeName(obr().time().startTime().value()),
332  obr(),
333  restartOnOutput_ ?
337  ),
338  1*baseField
339  )
340  );
341 
342  DebugInfo << "Create and store: " << windowFieldName << endl;
343 
344  item.addToWindow(windowFieldName, obr().time().deltaTValue());
345 
346  return true;
347 }
348 
349 
350 template<class Type>
352 {
353  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
354  typedef typename VolFieldType::Internal VolFieldInternalType;
355  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
356  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
357 
358  for (fieldAverageItem& item : faItems_)
359  {
360  if (item.storeWindowFields())
361  {
362  (void)
363  (
364  storeWindowFieldType<VolFieldType>(item)
365  || storeWindowFieldType<VolFieldInternalType>(item)
366  || storeWindowFieldType<SurfaceFieldType>(item)
367  || storeWindowFieldType<SurfFieldType>(item)
368  );
369  }
370  }
371 }
372 
373 
374 template<class Type>
376 {
377  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
378  typedef typename VolFieldType::Internal VolFieldInternalType;
379  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
380  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
381 
382  const auto& obr = this->obr();
383 
384  for (const fieldAverageItem& item : faItems_)
385  {
386  (void)
387  (
388  item.calculateMeanField<VolFieldType>(obr)
389  || item.calculateMeanField<VolFieldInternalType>(obr)
390  || item.calculateMeanField<SurfaceFieldType>(obr)
391  || item.calculateMeanField<SurfFieldType>(obr)
392  );
393  }
394 }
395 
396 
397 template<class Type1, class Type2>
399 {
400  typedef GeometricField<Type1, fvPatchField, volMesh> VolFieldType1;
401  typedef typename VolFieldType1::Internal VolFieldInternalType1;
402  typedef GeometricField<Type1, fvsPatchField, surfaceMesh> SurfaceFieldType1;
403  typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
404 
405  typedef GeometricField<Type2, fvPatchField, volMesh> VolFieldType2;
406  typedef typename VolFieldType2::Internal VolFieldInternalType2;
407  typedef GeometricField<Type2, fvsPatchField, surfaceMesh> SurfaceFieldType2;
408  typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
409 
410  const auto& obr = this->obr();
411 
412  for (const fieldAverageItem& item : faItems_)
413  {
414  (void)
415  (
416  item.calculatePrime2MeanField<VolFieldType1, VolFieldType2>(obr)
418  <VolFieldInternalType1, VolFieldInternalType2>(obr)
420  <SurfaceFieldType1, SurfaceFieldType2>(obr)
421  || item.calculatePrime2MeanField<SurfFieldType1, SurfFieldType2>(obr)
422  );
423  }
424 }
425 
426 
427 template<class Type1, class Type2>
429 (
430  const fieldAverageItem& item
431 ) const
432 {
433  if (!foundObject<Type1>(item.fieldName()))
434  {
435  return false;
436  }
437 
438  const Type1& meanField = lookupObject<Type1>(item.meanFieldName());
439 
440  Type2& prime2MeanField = lookupObjectRef<Type2>(item.prime2MeanFieldName());
441 
442  prime2MeanField += sqr(meanField);
443 
444  return true;
445 }
446 
447 
448 template<class Type1, class Type2>
450 {
451  typedef GeometricField<Type1, fvPatchField, volMesh> VolFieldType1;
452  typedef typename VolFieldType1::Internal VolFieldInternalType1;
453  typedef GeometricField<Type1, fvsPatchField, surfaceMesh> SurfaceFieldType1;
454  typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
455 
456  typedef GeometricField<Type2, fvPatchField, volMesh> VolFieldType2;
457  typedef typename VolFieldType2::Internal VolFieldInternalType2;
458  typedef GeometricField<Type2, fvsPatchField, surfaceMesh> SurfaceFieldType2;
459  typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
460 
461  for (const fieldAverageItem& item : faItems_)
462  {
463  if (item.prime2Mean())
464  {
465  (void)
466  (
467  addMeanSqrToPrime2MeanType<VolFieldType1, VolFieldType2>(item)
468  || addMeanSqrToPrime2MeanType
469  <VolFieldInternalType1, VolFieldInternalType2>(item)
470  || addMeanSqrToPrime2MeanType
471  <SurfaceFieldType1, SurfaceFieldType2>(item)
472  || addMeanSqrToPrime2MeanType
473  <SurfFieldType1, SurfFieldType2>(item)
474  );
475  }
476  }
477 }
478 
479 
480 template<class Type>
482 (
483  const word& fieldName
484 ) const
485 {
486  const auto* fPtr = findObject<Type>(fieldName);
487 
488  if (fPtr)
489  {
490  DebugInfo<< "writing " << Type::typeName << ": " << fieldName << endl;
491  return fPtr->write();
492  }
493 
494  return false;
495 }
496 
497 
498 template<class Type>
500 {
501  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
502  typedef typename VolFieldType::Internal VolFieldInternalType;
503  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
504  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
505 
506  for (const fieldAverageItem& item : faItems_)
507  {
508  if (item.mean())
509  {
510  const word& fieldName = item.meanFieldName();
511 
512  (void)
513  (
514  writeFieldType<VolFieldType>(fieldName)
515  || writeFieldType<VolFieldInternalType>(fieldName)
516  || writeFieldType<SurfaceFieldType>(fieldName)
517  || writeFieldType<SurfFieldType>(fieldName)
518  );
519  }
520 
521  if (item.prime2Mean())
522  {
523  const word& fieldName = item.prime2MeanFieldName();
524 
525  (void)
526  (
527  writeFieldType<VolFieldType>(fieldName)
528  || writeFieldType<VolFieldInternalType>(fieldName)
529  || writeFieldType<SurfaceFieldType>(fieldName)
530  || writeFieldType<SurfFieldType>(fieldName)
531  );
532  }
533 
534  if (item.writeWindowFields())
535  {
536  FIFOStack<word> fieldNames = item.windowFieldNames();
537  forAllConstIters(fieldNames, fieldNameIter)
538  {
539  const word& fieldName = fieldNameIter();
540 
541  (void)
542  (
543  writeFieldType<VolFieldType>(fieldName)
544  || writeFieldType<VolFieldInternalType>(fieldName)
545  || writeFieldType<SurfaceFieldType>(fieldName)
546  || writeFieldType<SurfFieldType>(fieldName)
547  );
548  }
549  }
550  }
551 }
552 
553 
554 // ************************************************************************* //
Foam::surfaceFields.
const word & meanFieldName() const
Return const access to the mean field name.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
bool addPrime2MeanFieldType(fieldAverageItem &item)
Add prime-squared average field to database.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:600
dimensionedSymmTensor sqr(const dimensionedVector &dv)
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Fields (face and point) for polySurface.
bool mean() const
Return const access to the mean flag.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:529
word windowFieldName(const word &prefix) const
Helper function to construct a window field name.
bool store()
Register object with its registry and transfer ownership to the registry.
Definition: regIOobjectI.H:36
scalar window() const
Return the window length (iterations or seconds)
bool addMeanFieldType(fieldAverageItem &item)
Add mean average field to database.
Generic GeometricField class.
Ignore writing from objectRegistry::writeObject()
void calculatePrime2MeanFields() const
Calculate prime-squared average fields.
bool active() const
Return const access to the active flag.
void writeFields() const
Write fields.
word timeName
Definition: getTimeIndex.H:3
const word & fieldName() const
Return const access to the field name.
bool addMeanSqrToPrime2MeanType(const fieldAverageItem &item) const
Add mean-squared field value to prime-squared mean field.
Switch restartOnOutput_
Restart the averaging process on output.
Definition: fieldAverage.H:281
bool restoreWindowFieldsType(const fieldAverageItem &item)
void addToWindow(const word &fieldName, const scalar deltaT)
Add field to window.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Reading is optional [identical to LAZY_READ].
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
void calculateMeanFields() const
Calculate mean average fields.
#define DebugInfo
Report an information message using Foam::Info.
const FIFOStack< word > & windowFieldNames() const
Return the list of window field names (windowType = EXACT)
bool addPrime2MeanField(fieldAverageItem &item)
Add prime-squared average field to database.
bool addMeanField(fieldAverageItem &item)
Add mean average field to database.
bool storeWindowFieldType(fieldAverageItem &item)
#define WarningInFunction
Report a warning using Foam::Warning.
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Nothing to be read.
#define Log
Definition: PDRblock.C:28
bool storeWindowFields() const
Return true if we wish to store window fields.
Helper class to describe what form of averaging to apply. A set will be applied to each base field in...
bool writeFieldType(const word &fieldName) const
Write fields.
const Time & time() const
Return time database.
void addMeanSqrToPrime2Mean() const
Add mean-squared field value to prime-squared mean field.
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
bool prime2Mean() const
Return const access to the prime-squared mean flag.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
Foam::label startTime
Request registration (bool: true)
void restoreWindowFields(const fieldAverageItem &item)
bool found
virtual const objectRegistry & obr() const
The region or sub-region registry being used.
bool calculatePrime2MeanField(const objectRegistry &obr) const
Calculate prime-squared average fields.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
bool calculateMeanField(const objectRegistry &obr) const
Calculate the mean field value.