Loading...
Searching...
No Matches
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"
32#include "polySurfaceFields.H"
33#include "OFstream.H"
34
35// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36
37template<class Type>
39(
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 (
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
99template<class Type>
101(
102 fieldAverageItem& item
103)
104{
106 typedef typename VolFieldType::Internal VolFieldInternalType;
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 );
122
123 return added;
124}
125
126
127template<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 }
176
177 return true;
178}
179
180
181template<class Type>
183(
184 const fieldAverageItem& item
185)
186{
188 typedef typename VolFieldType::Internal VolFieldInternalType;
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)
200 );
201 }
202}
203
204
205template<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;
259
260 return false;
261}
262
263
264template<class Type1, class Type2>
266(
267 fieldAverageItem& item
268)
269{
271 typedef typename VolFieldType1::Internal VolFieldInternalType1;
273 typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
274
276 typedef typename VolFieldType2::Internal VolFieldInternalType2;
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);
301
302 return added;
303}
304
305
306template<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
350template<class Type>
352{
354 typedef typename VolFieldType::Internal VolFieldInternalType;
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
374template<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
397template<class Type1, class Type2>
399{
401 typedef typename VolFieldType1::Internal VolFieldInternalType1;
403 typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
404
406 typedef typename VolFieldType2::Internal VolFieldInternalType2;
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)
417 || item.calculatePrime2MeanField
418 <VolFieldInternalType1, VolFieldInternalType2>(obr)
419 || item.calculatePrime2MeanField
420 <SurfaceFieldType1, SurfaceFieldType2>(obr)
421 || item.calculatePrime2MeanField<SurfFieldType1, SurfFieldType2>(obr)
422 );
423 }
424}
425
426
427template<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
448template<class Type1, class Type2>
450{
452 typedef typename VolFieldType1::Internal VolFieldInternalType1;
454 typedef DimensionedField<Type1, polySurfaceGeoMesh> SurfFieldType1;
455
457 typedef typename VolFieldType2::Internal VolFieldInternalType2;
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
480template<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();
493
494 return false;
495}
496
497
498template<class Type>
500{
502 typedef typename VolFieldType::Internal VolFieldInternalType;
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// ************************************************************************* //
#define Log
Definition PDRblock.C:28
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:48
Generic GeometricField class.
@ REGISTER
Request registration (bool: true).
@ NO_READ
Nothing to be read.
@ READ_IF_PRESENT
Reading is optional [identical to LAZY_READ].
@ MUST_READ
Reading required.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
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.
bool writeFieldType(const word &fieldName) const
Write fields.
Switch restartOnOutput_
Restart the averaging process on output.
bool addMeanField(fieldAverageItem &item)
Add mean average field to database.
void calculatePrime2MeanFields() const
Calculate prime-squared average fields.
List< fieldAverageItem > faItems_
List of field average items, describing what averages to be calculated and output.
bool addPrime2MeanField(fieldAverageItem &item)
Add prime-squared average field to database.
bool addPrime2MeanFieldType(fieldAverageItem &item)
Add prime-squared average field to database.
bool restoreWindowFieldsType(const fieldAverageItem &item)
bool addMeanFieldType(fieldAverageItem &item)
Add mean average field to database.
bool addMeanSqrToPrime2MeanType(const fieldAverageItem &item) const
Add mean-squared field value to prime-squared mean field.
bool storeWindowFieldType(fieldAverageItem &item)
void calculateMeanFields() const
Calculate mean average fields.
void restoreWindowFields(const fieldAverageItem &item)
void addMeanSqrToPrime2Mean() const
Add mean-squared field value to prime-squared mean field.
const ObjectType & lookupObject(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
bool foundObject(const word &fieldName) const
Find object (eg, a field) in the (sub) objectRegistry.
virtual const objectRegistry & obr() const
The region or sub-region registry being used.
const ObjectType * findObject(const word &fieldName) const
Return const pointer to the object (eg, a field) in the (sub) objectRegistry.
ObjectType & lookupObjectRef(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
const Time & time() const
Return time database.
bool store()
Register object with its registry and transfer ownership to the registry.
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
const auto & io
auto & name
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:519
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
Fields (face and point) for polySurface.
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition stdFoam.H:235
Foam::surfaceFields.