volFieldValue.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-2022 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 "volFieldValue.H"
27 #include "fvMesh.H"
28 #include "volFields.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace functionObjects
36 {
37 namespace fieldValues
38 {
39  defineTypeNameAndDebug(volFieldValue, 0);
40  addToRunTimeSelectionTable(fieldValue, volFieldValue, dictionary);
41  addToRunTimeSelectionTable(functionObject, volFieldValue, dictionary);
42 }
43 }
44 }
45 
46 template<>
47 const char*
49 <
51  11
52 >::names[] =
53 {
54  "none",
55  "sum",
56  "sumMag",
57  "average",
58  "volAverage",
59  "volIntegrate",
60  "min",
61  "max",
62  "minMag",
63  "maxMag",
64  "CoV"
65 };
66 
67 const Foam::NamedEnum
68 <
70  11
72 
73 
74 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
75 
77 (
78  const dictionary& dict
79 )
80 {
81  dict.readIfPresent<Switch>("writeLocation", writeLocation_);
82 
83  if (dict.readIfPresent("weightFields", weightFieldNames_))
84  {
85  Info<< name() << " " << operationTypeNames_[operation_]
86  << " weight fields " << weightFieldNames_;
87  }
88  else if (dict.found("weightField"))
89  {
90  weightFieldNames_.setSize(1);
91  dict.lookup("weightField") >> weightFieldNames_[0];
92 
93  Info<< name() << " " << operationTypeNames_[operation_]
94  << " weight field " << weightFieldNames_[0];
95  }
96 
97  if (dict.readIfPresent("scaleFactor", scaleFactor_))
98  {
99  Info<< " scale factor = " << scaleFactor_ << nl;
100  }
101 
102  Info<< nl << endl;
103 }
104 
105 
106 template<class Type>
109 {
110  switch (operation_)
111  {
112  case operationType::minMag:
113  case operationType::maxMag:
114  file() << tab << "location" << tab << "cell";
115  if (Pstream::parRun())
116  {
117  file() << tab << "processor";
118  }
119  break;
120  default:
121  break;
122  }
123 }
124 
125 
126 template<>
127 void Foam::functionObjects::fieldValues::volFieldValue::
128 writeFileHeaderLocation<Foam::scalar>()
129 {
130  switch (operation_)
131  {
132  case operationType::min:
133  case operationType::max:
134  case operationType::minMag:
135  case operationType::maxMag:
136  file() << tab << "location" << tab << "cell";
137  if (Pstream::parRun())
138  {
139  file() << tab << "processor";
140  }
141  break;
142  default:
143  break;
144  }
145 }
146 
147 
149 (
150  const label i
151 )
152 {
154 
155  writeCommented(file(), "Time");
156 
157  forAll(fields_, fieldi)
158  {
159  file() << tab << operationTypeNames_[operation_] << "(";
160 
161  forAll(weightFieldNames_, i)
162  {
163  file() << weightFieldNames_[i] << ',';
164  }
165 
166  file() << fields_[fieldi] << ")";
167 
168  if (writeLocation_)
169  {
170  #define writeFileHeaderLocationFieldType(fieldType, none) \
171  if (validField<fieldType>(fields_[fieldi])) \
172  { \
173  writeFileHeaderLocation<fieldType>(); \
174  }
176  #undef writeHeaderLocationFieldType
177  }
178  }
179 
180  file() << endl;
181 }
182 
183 
185 (
186  const Field<scalar>& values,
187  const scalarField& weights,
188  const scalarField& V,
189  Result<scalar>& result
190 ) const
191 {
192  switch (operation_)
193  {
194  case operationType::min:
195  {
196  compareScalars(values, result, lessOp<scalar>());
197  return true;
198  }
199  case operationType::minMag:
200  {
201  compareScalars(mag(values), result, lessOp<scalar>());
202  return true;
203  }
204  case operationType::max:
205  {
206  compareScalars(values, result, greaterOp<scalar>());
207  return true;
208  }
209  case operationType::maxMag:
210  {
211  compareScalars(mag(values), result, greaterOp<scalar>());
212  return true;
213  }
214  default:
215  {
216  // Fall through to same-type operations
217  return processValuesTypeType(values, weights, V, result);
218  }
219  }
220 }
221 
222 
223 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
224 
226 (
227  const word& name,
228  const Time& runTime,
229  const dictionary& dict
230 )
231 :
232  fieldValue(name, runTime, dict, typeName),
234  writeLocation_(false),
235  operation_(operationTypeNames_.read(dict.lookup("operation"))),
236  scaleFactor_(1)
237 {
238  read(dict);
239 }
240 
241 
243 (
244  const word& name,
245  const objectRegistry& obr,
246  const dictionary& dict
247 )
248 :
249  fieldValue(name, obr, dict, typeName),
251  writeLocation_(false),
252  operation_(operationTypeNames_.read(dict.lookup("operation"))),
253  scaleFactor_(1)
254 {
255  read(dict);
256 }
257 
258 
259 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
260 
262 {}
263 
264 
265 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
266 
268 (
269  const dictionary& dict
270 )
271 {
272  fieldValue::read(dict);
273 
274  // No additional info to read
275  initialise(dict);
276 
277  return true;
278 }
279 
280 
282 {
284 
285  if (Pstream::master())
286  {
287  writeTime(file());
288  }
289 
290  // Construct the weight field and the volumes
291  scalarField weights
292  (
293  isNull(cellIDs()) ? fieldValue::mesh_.nCells() : cellIDs().size(),
294  1
295  );
296  forAll(weightFieldNames_, i)
297  {
298  weights *= getFieldValues<scalar>(weightFieldNames_[i]);
299  }
300  const scalarField V(filterField(fieldValue::mesh_.V()));
301 
302  forAll(fields_, i)
303  {
304  const word& fieldName = fields_[i];
305  bool ok = false;
306 
307  #define writeValuesFieldType(fieldType, none) \
308  ok = ok || writeValues<fieldType>(fieldName, weights, V);
310  #undef writeValuesFieldType
311 
312  if (!ok)
313  {
314  cannotFindObject(fieldName);
315  }
316  }
317 
318  if (operation_ != operationType::none && Pstream::master())
319  {
320  file() << endl;
321  }
322 
323  Log << endl;
324 
325  return true;
326 }
327 
328 
329 // ************************************************************************* //
#define writeValuesFieldType(fieldType, none)
bool processValues(const Field< Type > &values, const scalarField &weights, const scalarField &V, Result< ResultType > &result) const
Apply the operation to the values, and return true if successful.
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
static const char tab
Definition: Ostream.H:259
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
addToRunTimeSelectionTable(functionObject, fieldValueDelta, dictionary)
void initialise(const dictionary &dict)
Initialise, e.g. cell addressing.
Definition: volFieldValue.C:77
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: fieldValue.C:90
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none/any.
Definition: Switch.H:60
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
volFieldValue(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:51
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
bool isNull(const T &t)
Return true if t is a reference to the nullObject of type T.
Definition: nullObjectI.H:46
A class for handling words, derived from string.
Definition: word.H:59
defineTypeNameAndDebug(fieldValueDelta, 0)
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
static const char nl
Definition: Ostream.H:260
#define writeFileHeaderLocationFieldType(fieldType, none)
Volume (cell) region selection class.
Definition: volRegion.H:102
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void writeFileHeader(const writeFile &wf, Ostream &file)
Output file header information.
Definition: volRegion.C:58
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
virtual void writeFileHeader(const label i)
Output file header information.
#define Log
Report write to Foam::Info if the local log switch is true.
messageStream Info
FOR_ALL_FIELD_TYPES(DefineFvWallInfoType)
dimensioned< scalar > mag(const dimensioned< Type > &)
Base class for field value -based function objects.
Definition: fieldValue.H:62
Registry of regIOobjects.
virtual bool write()
Calculate and write.
virtual bool read(const dictionary &)
Read from dictionary.
const fvMesh & mesh_
Reference to the fvMesh.
Namespace for OpenFOAM.
static const NamedEnum< operationType, 11 > operationTypeNames_
Operation type names.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864
virtual bool write()
Write.
Definition: fieldValue.C:114
void writeFileHeaderLocation()
Output file header location information for a given type.