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-------------------------------------------------------------------------------
11License
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"
32#include "polySurfaceFields.H"
33#include "OFstream.H"
34
35// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36
37template<class Type>
39(
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 (
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
95template<class Type>
97(
99)
100{
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
114template<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
141 (
142 name,
143 obr().time().timeName(obr().time().startTime().value()),
144 obr(),
147 );
148
149 if (io.typeHeaderOk<Type>(true))
150 {
151 DebugInfo << "Read and store: " << name << endl;
152 obr().store(new Type(io, fieldPtr->mesh()));
153 }
154 else
155 {
157 << "Unable to read window " << Type::typeName << " " << name
158 << ". Averaging restart behaviour may be compromised"
159 << endl;
160 }
161 }
162}
163
164
165template<class Type>
167(
168 const fieldAverageItem& item
169)
170{
173 typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
174
175 if (item.window() > 0)
176 {
177 restoreWindowFieldsType<VolFieldType>(item);
178 restoreWindowFieldsType<SurfaceFieldType>(item);
179 restoreWindowFieldsType<SurfFieldType>(item);
180 }
181}
182
183
184template<class Type1, class Type2>
186(
187 fieldAverageItem& item
188)
189{
190 const word& fieldName = item.fieldName();
191
192 if (!foundObject<Type1>(fieldName))
193 {
194 return;
195 }
196
197 const word& meanFieldName = item.meanFieldName();
198 const word& prime2MeanFieldName = item.prime2MeanFieldName();
199
200 Log << " Reading/initialising field " << prime2MeanFieldName << nl;
201
202 if (foundObject<Type2>(prime2MeanFieldName))
203 {}
204 else if (obr().found(prime2MeanFieldName))
205 {
206 Log << " Cannot allocate average field " << prime2MeanFieldName
207 << " since an object with that name already exists."
208 << " Disabling averaging for field." << endl;
209
210 item.prime2Mean() = false;
211 }
212 else
213 {
214 const Type1& baseField = lookupObject<Type1>(fieldName);
215 const Type1& meanField = lookupObject<Type1>(meanFieldName);
216
217 // Store on registry
218 obr().store
219 (
220 new Type2
221 (
223 (
224 prime2MeanFieldName,
225 obr().time().timeName(obr().time().startTime().value()),
226 obr(),
227 restartOnOutput_?
231 ),
232 sqr(baseField) - sqr(meanField)
233 )
234 );
235 }
236}
237
238
239template<class Type1, class Type2>
241(
242 fieldAverageItem& item
243)
244{
247 typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
248
251 typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
252
253 if (item.prime2Mean())
254 {
255 if (!item.mean())
256 {
258 << "To calculate the prime-squared average, the "
259 << "mean average must also be selected for field "
260 << item.fieldName() << nl << exit(FatalError);
261 }
262
263 addPrime2MeanFieldType<VolFieldType1, VolFieldType2>(item);
264 addPrime2MeanFieldType<SurfaceFieldType1, SurfaceFieldType2>(item);
265 addPrime2MeanFieldType<SurfFieldType1, SurfFieldType2>(item);
266 }
267}
268
269
270template<class Type>
272(
273 fieldAverageItem& item
274)
275{
276 const word& fieldName = item.fieldName();
277 if (!foundObject<Type>(fieldName))
278 {
279 return;
280 }
281
282 const Type& baseField = lookupObject<Type>(fieldName);
283
284 const word windowFieldName = item.windowFieldName(this->name());
285
286 // Store on registry
287 obr().store
288 (
289 new Type
290 (
292 (
293 windowFieldName,
294 obr().time().timeName(obr().time().startTime().value()),
295 obr(),
296 restartOnOutput_ ?
300 ),
301 1*baseField
302 )
303 );
304
305 DebugInfo << "Create and store: " << windowFieldName << endl;
306
307 item.addToWindow(windowFieldName, obr().time().deltaTValue());
308}
309
310
311template<class Type>
313{
316 typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
317
318 for (fieldAverageItem& item : faItems_)
319 {
320 if (item.storeWindowFields())
321 {
322 storeWindowFieldType<VolFieldType>(item);
323 storeWindowFieldType<SurfaceFieldType>(item);
324 storeWindowFieldType<SurfFieldType>(item);
325 }
326 }
327}
328
329
330template<class Type>
332{
335 typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
336
337 for (const fieldAverageItem& item : faItems_)
338 {
339 item.calculateMeanField<VolFieldType>(obr());
340 item.calculateMeanField<SurfaceFieldType>(obr());
341 item.calculateMeanField<SurfFieldType>(obr());
342 }
343}
344
345
346template<class Type1, class Type2>
348{
351 typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
352
355 typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
356
357 for (const fieldAverageItem& item : faItems_)
358 {
359 item.calculatePrime2MeanField<VolFieldType1, VolFieldType2>(obr());
360 item.calculatePrime2MeanField<SurfaceFieldType1, SurfaceFieldType2>
361 (
362 obr()
363 );
364 item.calculatePrime2MeanField<SurfFieldType1, SurfFieldType2>(obr());
365 }
366}
367
368
369template<class Type1, class Type2>
371(
372 const fieldAverageItem& item
373) const
374{
375 const word& fieldName = item.fieldName();
376
377 if (!foundObject<Type1>(fieldName))
378 {
379 return;
380 }
381
382 const Type1& meanField = lookupObject<Type1>(item.meanFieldName());
383
384 Type2& prime2MeanField = lookupObjectRef<Type2>(item.prime2MeanFieldName());
385
386 prime2MeanField += sqr(meanField);
387}
388
389
390template<class Type1, class Type2>
392{
395 typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
396
399 typedef DimensionedField<Type2, polySurfaceGeoMesh> SurfFieldType2;
400
401 for (const fieldAverageItem& item : faItems_)
402 {
403 if (item.prime2Mean())
404 {
405 addMeanSqrToPrime2MeanType<VolFieldType1, VolFieldType2>(item);
406 addMeanSqrToPrime2MeanType<SurfaceFieldType1, SurfaceFieldType2>
407 (
408 item
409 );
410 addMeanSqrToPrime2MeanType<SurfFieldType1, SurfFieldType2>(item);
411 }
412 }
413}
414
415
416template<class Type>
418(
419 const word& fieldName
420) const
421{
422 if (foundObject<Type>(fieldName))
423 {
424 const Type& f = lookupObject<Type>(fieldName);
425 f.write();
426 }
427}
428
429
430template<class Type>
432{
435 typedef DimensionedField<Type, polySurfaceGeoMesh> SurfFieldType;
436
437 for (const fieldAverageItem& item : faItems_)
438 {
439 if (item.mean())
440 {
441 const word& fieldName = item.meanFieldName();
442 writeFieldType<VolFieldType>(fieldName);
443 writeFieldType<SurfaceFieldType>(fieldName);
444 writeFieldType<SurfFieldType>(fieldName);
445 }
446
447 if (item.prime2Mean())
448 {
449 const word& fieldName = item.prime2MeanFieldName();
450 writeFieldType<VolFieldType>(fieldName);
451 writeFieldType<SurfaceFieldType>(fieldName);
452 writeFieldType<SurfFieldType>(fieldName);
453 }
454
455 if (item.writeWindowFields())
456 {
457 FIFOStack<word> fieldNames = item.windowFieldNames();
458 forAllConstIters(fieldNames, fieldNameIter)
459 {
460 const word& fieldName = fieldNameIter();
461 writeFieldType<VolFieldType>(fieldName);
462 writeFieldType<SurfaceFieldType>(fieldName);
463 writeFieldType<SurfFieldType>(fieldName);
464 }
465 }
466 }
467}
468
469
470// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
bool found
Foam::label startTime
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
A FIFO stack based on a singly-linked list.
Definition: FIFOStack.H:54
Generic GeometricField class.
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
Helper class to describe what form of averaging to apply. A set will be applied to each base field in...
const FIFOStack< word > & windowFieldNames() const
Return the list of window field names (windowType = EXACT)
const word & fieldName() const
Return const access to the field name.
bool mean() const
Return const access to the mean flag.
void addToWindow(const word &fieldName, const scalar deltaT)
Add field to window.
scalar window() const
Return the window length (iterations or seconds)
word windowFieldName(const word &prefix) const
Helper function to construct a window field name.
bool prime2Mean() const
Return const access to the prime-squared mean flag.
const word & prime2MeanFieldName() const
Return const access to the prime-squared mean field name.
bool active() const
Return const access to the active flag.
const word & meanFieldName() const
Return const access to the mean field name.
Switch restartOnOutput_
Restart the averaging process on output.
Definition: fieldAverage.H:277
void addMeanSqrToPrime2MeanType(const fieldAverageItem &item) const
Add mean-squared field value to prime-squared mean field.
void addMeanFieldType(fieldAverageItem &item)
Add mean average field to database.
void addMeanField(fieldAverageItem &item)
Add mean average field to database.
void calculatePrime2MeanFields() const
Calculate prime-squared average fields.
void addPrime2MeanFieldType(fieldAverageItem &item)
Add prime-squared average field to database.
void storeWindowFieldType(fieldAverageItem &item)
void writeFieldType(const word &fieldName) const
Write fields.
void calculateMeanFields() const
Calculate mean average fields.
void restoreWindowFields(const fieldAverageItem &item)
void addPrime2MeanField(fieldAverageItem &item)
Add prime-squared average field to database.
void addMeanSqrToPrime2Mean() const
Add mean-squared field value to prime-squared mean field.
void restoreWindowFieldsType(const fieldAverageItem &item)
virtual const objectRegistry & obr() const
The region or sub-region registry being used.
const Time & time() const
Return time database.
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
word timeName
Definition: getTimeIndex.H:3
#define DebugInfo
Report an information message using Foam::Info.
#define WarningInFunction
Report a warning using Foam::Warning.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Fields (face and point) for polySurface.
labelList f(nPoints)
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278
Foam::surfaceFields.