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 word& fieldName = item.fieldName();
44 
45  if (!foundObject<Type>(fieldName))
46  {
47  return;
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 = lookupObject<Type>(fieldName);
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 }
93 
94 
95 template<class Type>
97 (
98  fieldAverageItem& item
99 )
100 {
101  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
102  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
103  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
104 
105  if (item.mean())
106  {
107  addMeanFieldType<VolFieldType>(item);
108  addMeanFieldType<SurfaceFieldType>(item);
109  addMeanFieldType<SurfFieldType>(item);
110  }
111 }
112 
113 
114 template<class Type>
116 (
117  const fieldAverageItem& item
118 )
119 {
120  if (restartOnOutput_)
121  {
122  return;
123  }
124 
125  const word& fieldName = item.fieldName();
126 
127  const Type* fieldPtr = findObject<Type>(fieldName);
128 
129  if (!fieldPtr)
130  {
131  return;
132  }
133 
134  const FIFOStack<word>& fieldNames = item.windowFieldNames();
135 
136  forAllConstIters(fieldNames, fieldIter)
137  {
138  const word& name = fieldIter();
139 
140  IOobject io
141  (
142  name,
143  obr().time().timeName(obr().time().startTime().value()),
144  obr(),
148  );
149 
150  if (io.typeHeaderOk<Type>(true))
151  {
152  DebugInfo << "Read and store: " << name << endl;
153  obr().store(new Type(io, fieldPtr->mesh()));
154  }
155  else
156  {
158  << "Unable to read window " << Type::typeName << " " << name
159  << ". Averaging restart behaviour may be compromised"
160  << endl;
161  }
162  }
163 }
164 
165 
166 template<class Type>
168 (
169  const fieldAverageItem& item
170 )
171 {
172  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
173  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
174  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
175 
176  if (item.window() > 0)
177  {
178  restoreWindowFieldsType<VolFieldType>(item);
179  restoreWindowFieldsType<SurfaceFieldType>(item);
180  restoreWindowFieldsType<SurfFieldType>(item);
181  }
182 }
183 
184 
185 template<class Type1, class Type2>
187 (
188  fieldAverageItem& item
189 )
190 {
191  const word& fieldName = item.fieldName();
192 
193  if (!foundObject<Type1>(fieldName))
194  {
195  return;
196  }
197 
198  const word& meanFieldName = item.meanFieldName();
199  const word& prime2MeanFieldName = item.prime2MeanFieldName();
200 
201  Log << " Reading/initialising field " << prime2MeanFieldName << nl;
202 
203  if (foundObject<Type2>(prime2MeanFieldName))
204  {}
205  else if (obr().found(prime2MeanFieldName))
206  {
207  Log << " Cannot allocate average field " << prime2MeanFieldName
208  << " since an object with that name already exists."
209  << " Disabling averaging for field." << endl;
210 
211  item.prime2Mean() = false;
212  }
213  else
214  {
215  const Type1& baseField = lookupObject<Type1>(fieldName);
216  const Type1& meanField = lookupObject<Type1>(meanFieldName);
217 
218  // Store on registry
219  obr().store
220  (
221  new Type2
222  (
223  IOobject
224  (
225  prime2MeanFieldName,
226  obr().time().timeName(obr().time().startTime().value()),
227  obr(),
228  restartOnOutput_?
232  ),
233  sqr(baseField) - sqr(meanField)
234  )
235  );
236  }
237 }
238 
239 
240 template<class Type1, class Type2>
242 (
243  fieldAverageItem& item
244 )
245 {
246  typedef GeometricField<Type1, fvPatchField, volMesh> VolFieldType1;
247  typedef GeometricField<Type1, fvsPatchField, surfaceMesh> SurfaceFieldType1;
248  typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
249 
250  typedef GeometricField<Type2, fvPatchField, volMesh> VolFieldType2;
251  typedef GeometricField<Type2, fvsPatchField, surfaceMesh> SurfaceFieldType2;
252  typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
253 
254  if (item.prime2Mean())
255  {
256  if (!item.mean())
257  {
259  << "To calculate the prime-squared average, the "
260  << "mean average must also be selected for field "
261  << item.fieldName() << nl << exit(FatalError);
262  }
263 
264  addPrime2MeanFieldType<VolFieldType1, VolFieldType2>(item);
265  addPrime2MeanFieldType<SurfaceFieldType1, SurfaceFieldType2>(item);
266  addPrime2MeanFieldType<SurfFieldType1, SurfFieldType2>(item);
267  }
268 }
269 
270 
271 template<class Type>
273 (
274  fieldAverageItem& item
275 )
276 {
277  const word& fieldName = item.fieldName();
278  if (!foundObject<Type>(fieldName))
279  {
280  return;
281  }
282 
283  const Type& baseField = lookupObject<Type>(fieldName);
284 
285  const word windowFieldName = item.windowFieldName(this->name());
286 
287  // Store on registry
288  obr().store
289  (
290  new Type
291  (
292  IOobject
293  (
294  windowFieldName,
295  obr().time().timeName(obr().time().startTime().value()),
296  obr(),
297  restartOnOutput_ ?
301  ),
302  1*baseField
303  )
304  );
305 
306  DebugInfo << "Create and store: " << windowFieldName << endl;
307 
308  item.addToWindow(windowFieldName, obr().time().deltaTValue());
309 }
310 
311 
312 template<class Type>
314 {
315  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
316  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
317  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
318 
319  for (fieldAverageItem& item : faItems_)
320  {
321  if (item.storeWindowFields())
322  {
323  storeWindowFieldType<VolFieldType>(item);
324  storeWindowFieldType<SurfaceFieldType>(item);
325  storeWindowFieldType<SurfFieldType>(item);
326  }
327  }
328 }
329 
330 
331 template<class Type>
333 {
334  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
335  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
336  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
337 
338  for (const fieldAverageItem& item : faItems_)
339  {
340  item.calculateMeanField<VolFieldType>(obr());
341  item.calculateMeanField<SurfaceFieldType>(obr());
342  item.calculateMeanField<SurfFieldType>(obr());
343  }
344 }
345 
346 
347 template<class Type1, class Type2>
349 {
350  typedef GeometricField<Type1, fvPatchField, volMesh> VolFieldType1;
351  typedef GeometricField<Type1, fvsPatchField, surfaceMesh> SurfaceFieldType1;
352  typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
353 
354  typedef GeometricField<Type2, fvPatchField, volMesh> VolFieldType2;
355  typedef GeometricField<Type2, fvsPatchField, surfaceMesh> SurfaceFieldType2;
356  typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
357 
358  for (const fieldAverageItem& item : faItems_)
359  {
360  item.calculatePrime2MeanField<VolFieldType1, VolFieldType2>(obr());
361  item.calculatePrime2MeanField<SurfaceFieldType1, SurfaceFieldType2>
362  (
363  obr()
364  );
365  item.calculatePrime2MeanField<SurfFieldType1, SurfFieldType2>(obr());
366  }
367 }
368 
369 
370 template<class Type1, class Type2>
372 (
373  const fieldAverageItem& item
374 ) const
375 {
376  const word& fieldName = item.fieldName();
377 
378  if (!foundObject<Type1>(fieldName))
379  {
380  return;
381  }
382 
383  const Type1& meanField = lookupObject<Type1>(item.meanFieldName());
384 
385  Type2& prime2MeanField = lookupObjectRef<Type2>(item.prime2MeanFieldName());
386 
387  prime2MeanField += sqr(meanField);
388 }
389 
390 
391 template<class Type1, class Type2>
393 {
394  typedef GeometricField<Type1, fvPatchField, volMesh> VolFieldType1;
395  typedef GeometricField<Type1, fvsPatchField, surfaceMesh> SurfaceFieldType1;
396  typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
397 
398  typedef GeometricField<Type2, fvPatchField, volMesh> VolFieldType2;
399  typedef GeometricField<Type2, fvsPatchField, surfaceMesh> SurfaceFieldType2;
400  typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
401 
402  for (const fieldAverageItem& item : faItems_)
403  {
404  if (item.prime2Mean())
405  {
406  addMeanSqrToPrime2MeanType<VolFieldType1, VolFieldType2>(item);
407  addMeanSqrToPrime2MeanType<SurfaceFieldType1, SurfaceFieldType2>
408  (
409  item
410  );
411  addMeanSqrToPrime2MeanType<SurfFieldType1, SurfFieldType2>(item);
412  }
413  }
414 }
415 
416 
417 template<class Type>
419 (
420  const word& fieldName
421 ) const
422 {
423  if (foundObject<Type>(fieldName))
424  {
425  const Type& f = lookupObject<Type>(fieldName);
426  f.write();
427  }
428 }
429 
430 
431 template<class Type>
433 {
434  typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
435  typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
436  typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
437 
438  for (const fieldAverageItem& item : faItems_)
439  {
440  if (item.mean())
441  {
442  const word& fieldName = item.meanFieldName();
443  writeFieldType<VolFieldType>(fieldName);
444  writeFieldType<SurfaceFieldType>(fieldName);
445  writeFieldType<SurfFieldType>(fieldName);
446  }
447 
448  if (item.prime2Mean())
449  {
450  const word& fieldName = item.prime2MeanFieldName();
451  writeFieldType<VolFieldType>(fieldName);
452  writeFieldType<SurfaceFieldType>(fieldName);
453  writeFieldType<SurfFieldType>(fieldName);
454  }
455 
456  if (item.writeWindowFields())
457  {
458  FIFOStack<word> fieldNames = item.windowFieldNames();
459  forAllConstIters(fieldNames, fieldNameIter)
460  {
461  const word& fieldName = fieldNameIter();
462  writeFieldType<VolFieldType>(fieldName);
463  writeFieldType<SurfaceFieldType>(fieldName);
464  writeFieldType<SurfFieldType>(fieldName);
465  }
466  }
467  }
468 }
469 
470 
471 // ************************************************************************* //
Foam::surfaceFields.
const word & meanFieldName() const
Return const access to the mean field name.
void storeWindowFieldType(fieldAverageItem &item)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void restoreWindowFieldsType(const fieldAverageItem &item)
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:598
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:531
bool store()
Register object with its registry and transfer ownership to the registry.
Definition: regIOobjectI.H:36
Generic GeometricField class.
Definition: areaFieldsFwd.H:50
void addMeanField(fieldAverageItem &item)
Add mean average field to database.
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.
Switch restartOnOutput_
Restart the averaging process on output.
Definition: fieldAverage.H:281
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
void writeFieldType(const word &fieldName) const
Write fields.
void addMeanSqrToPrime2MeanType(const fieldAverageItem &item) const
Add mean-squared field value to prime-squared mean field.
A class for handling words, derived from Foam::string.
Definition: word.H:63
Reading is optional [identical to LAZY_READ].
void calculateMeanFields() const
Calculate mean average fields.
#define DebugInfo
Report an information message using Foam::Info.
labelList f(nPoints)
void addPrime2MeanField(fieldAverageItem &item)
Add prime-squared average field to database.
#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...
Definition: areaFieldsFwd.H:42
Nothing to be read.
#define Log
Definition: PDRblock.C:28
Helper class to describe what form of averaging to apply. A set will be applied to each base field in...
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)
void addPrime2MeanFieldType(fieldAverageItem &item)
Add prime-squared average field to database.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
Foam::label startTime
Request registration (bool: true)
void restoreWindowFields(const fieldAverageItem &item)
bool found
void addMeanFieldType(fieldAverageItem &item)
Add mean average field to database.
virtual const objectRegistry & obr() const
The region or sub-region registry being used.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28