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-2021 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  case operationType::minMag:
196  {
197  opMag(values, result, lessOp<scalar>());
198  return true;
199  }
200  case operationType::max:
201  case operationType::maxMag:
202  {
203  opMag(values, result, greaterOp<scalar>());
204  return true;
205  }
206  default:
207  {
208  // Fall through to same-type operations
209  return processValuesTypeType(values, weights, V, result);
210  }
211  }
212 }
213 
214 
215 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
216 
218 (
219  const word& name,
220  const Time& runTime,
221  const dictionary& dict
222 )
223 :
224  fieldValue(name, runTime, dict, typeName),
226  writeLocation_(false),
227  operation_(operationTypeNames_.read(dict.lookup("operation"))),
228  scaleFactor_(1)
229 {
230  read(dict);
231 }
232 
233 
235 (
236  const word& name,
237  const objectRegistry& obr,
238  const dictionary& dict
239 )
240 :
241  fieldValue(name, obr, dict, typeName),
243  writeLocation_(false),
244  operation_(operationTypeNames_.read(dict.lookup("operation"))),
245  scaleFactor_(1)
246 {
247  read(dict);
248 }
249 
250 
251 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
252 
254 {}
255 
256 
257 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
258 
260 (
261  const dictionary& dict
262 )
263 {
264  fieldValue::read(dict);
265 
266  // No additional info to read
267  initialise(dict);
268 
269  return true;
270 }
271 
272 
274 {
276 
277  if (Pstream::master())
278  {
279  writeTime(file());
280  }
281 
282  // Construct the weight field and the volumes
283  scalarField weights
284  (
285  isNull(cellIDs()) ? fieldValue::mesh_.nCells() : cellIDs().size(),
286  1
287  );
288  forAll(weightFieldNames_, i)
289  {
290  weights *= getFieldValues<scalar>(weightFieldNames_[i]);
291  }
292  const scalarField V(filterField(fieldValue::mesh_.V()));
293 
294  forAll(fields_, i)
295  {
296  const word& fieldName = fields_[i];
297  bool ok = false;
298 
299  #define writeValuesFieldType(fieldType, none) \
300  ok = ok || writeValues<fieldType>(fieldName, weights, V);
302  #undef writeValuesFieldType
303 
304  if (!ok)
305  {
306  cannotFindObject(fieldName);
307  }
308  }
309 
310  if (operation_ != operationType::none && Pstream::master())
311  {
312  file() << endl;
313  }
314 
315  Log << endl;
316 
317  return true;
318 }
319 
320 
321 // ************************************************************************* //
#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:643
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
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
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
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.
#define FOR_ALL_FIELD_TYPES(Macro,...)
Definition: fieldTypes.H:50
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:68
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.
static const char nl
Definition: Ostream.H:260
#define writeFileHeaderLocationFieldType(fieldType, none)
Volume (cell) region selection class.
Definition: volRegion.H:102
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
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
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:844
virtual bool write()
Write.
Definition: fieldValue.C:114
void writeFileHeaderLocation()
Output file header location information for a given type.