volFieldValue.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) 2017-2020 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 "volFieldValue.H"
30 #include "fvMesh.H"
31 #include "volFields.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40 namespace fieldValues
41 {
42  defineTypeNameAndDebug(volFieldValue, 0);
43  addToRunTimeSelectionTable(fieldValue, volFieldValue, dictionary);
44  addToRunTimeSelectionTable(functionObject, volFieldValue, dictionary);
45 }
46 }
47 }
48 
49 const Foam::Enum
50 <
52 >
54 ({
55  // Normal operations
56  { operationType::opNone, "none" },
57  { operationType::opMin, "min" },
58  { operationType::opMax, "max" },
59  { operationType::opSum, "sum" },
60  { operationType::opSumMag, "sumMag" },
61  { operationType::opAverage, "average" },
62  { operationType::opVolAverage, "volAverage" },
63  { operationType::opVolIntegrate, "volIntegrate" },
64  { operationType::opCoV, "CoV" },
65 
66  // Using weighting
67  { operationType::opWeightedSum, "weightedSum" },
68  { operationType::opWeightedAverage, "weightedAverage" },
69  { operationType::opWeightedVolAverage, "weightedVolAverage" },
70  { operationType::opWeightedVolIntegrate, "weightedVolIntegrate" },
71 });
72 
73 const Foam::Enum
74 <
76 >
78 ({
79  { postOperationType::postOpNone, "none" },
80  { postOperationType::postOpMag, "mag" },
81  { postOperationType::postOpSqrt, "sqrt" },
82 });
83 
84 
85 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
86 
88 {
89  // Only a few operations require the cell volume
90  switch (operation_)
91  {
92  case opVolAverage:
93  case opVolIntegrate:
96  case opCoV:
97  return true;
98 
99  default:
100  return false;
101  }
102 }
103 
104 
106 {
107  // Operation specifically tagged to use mag
108  return (operation_ & typeAbsolute);
109 }
110 
111 
113 {
114  // Operation specifically tagged to require a weight field
115  return (operation_ & typeWeighted);
116 }
117 
118 
120 (
121  const scalarField& weightField
122 ) const
123 {
124  return
125  (
126  usesWeight()
127  && returnReduce(!weightField.empty(), orOp<bool>()) // On some processor
128  );
129 }
130 
131 
133 (
134  Ostream& os
135 ) const
136 {
137  volRegion::writeFileHeader(*this, os);
138 
139  if (weightFieldNames_.size())
140  {
141  writeHeaderValue
142  (
143  os,
144  "Weight field",
145  flatOutput(weightFieldNames_, FlatOutput::BareComma{})
146  );
147  }
148 
149  writeCommented(os, "Time");
150 
151  // TBD: add in postOperation information?
152 
153  for (const word& fieldName : fields_)
154  {
155  os << tab << operationTypeNames_[operation_]
156  << "(" << fieldName << ")";
157  }
158 
159  os << endl;
160 }
161 
162 
164 (
165  const scalarField& V,
166  const scalarField& weightField
167 )
168 {
169  label nProcessed = 0;
170 
171  for (const word& fieldName : fields_)
172  {
173  if
174  (
175  writeValues<scalar>(fieldName, V, weightField)
176  || writeValues<vector>(fieldName, V, weightField)
177  || writeValues<sphericalTensor>(fieldName, V, weightField)
178  || writeValues<symmTensor>(fieldName, V, weightField)
179  || writeValues<tensor>(fieldName, V, weightField)
180  )
181  {
182  ++nProcessed;
183  }
184  else
185  {
187  << "Requested field " << fieldName
188  << " not found in database and not processed"
189  << endl;
190  }
191  }
192 
193  return nProcessed;
194 }
195 
196 
197 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
198 
200 (
201  const word& name,
202  const Time& runTime,
203  const dictionary& dict
204 )
205 :
206  fieldValue(name, runTime, dict, typeName),
208  operation_(operationTypeNames_.get("operation", dict)),
209  postOperation_
210  (
211  postOperationTypeNames_.getOrDefault
212  (
213  "postOperation",
214  dict,
215  postOperationType::postOpNone,
216  true // Failsafe behaviour
217  )
218  ),
219  weightFieldNames_()
220 {
221  read(dict);
222  writeFileHeader(file());
223 }
224 
225 
227 (
228  const word& name,
229  const objectRegistry& obr,
230  const dictionary& dict
231 )
232 :
233  fieldValue(name, obr, dict, typeName),
235  operation_(operationTypeNames_.get("operation", dict)),
236  postOperation_
237  (
238  postOperationTypeNames_.getOrDefault
239  (
240  "postOperation",
241  dict,
242  postOperationType::postOpNone,
243  true // Failsafe behaviour
244  )
245  ),
246  weightFieldNames_()
247 {
248  read(dict);
249 }
250 
251 
252 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
253 
255 (
256  const dictionary& dict
257 )
258 {
260 
261  weightFieldNames_.clear();
262 
263  if (usesWeight())
264  {
265  // Can have "weightFields" or "weightField"
266 
267  bool missing = true;
268  if (dict.readIfPresent("weightFields", weightFieldNames_))
269  {
270  missing = false;
271  }
272  else
273  {
274  weightFieldNames_.resize(1);
275 
276  if (dict.readIfPresent("weightField", weightFieldNames_.first()))
277  {
278  missing = false;
279  if ("none" == weightFieldNames_.first())
280  {
281  // "none" == no weighting
282  weightFieldNames_.clear();
283  }
284  }
285  }
286 
287  if (missing)
288  {
289  // Suggest possible alternative unweighted operation?
291  << "The '" << operationTypeNames_[operation_]
292  << "' operation is missing a weightField." << nl
293  << "Either provide the weightField, "
294  << "use weightField 'none' to suppress weighting," << nl
295  << "or use a different operation."
296  << exit(FatalIOError);
297  }
298 
299  Info<< " weight field = ";
300  if (weightFieldNames_.empty())
301  {
302  Info<< "none" << nl;
303  }
304  else
305  {
306  Info<< flatOutput(weightFieldNames_) << nl;
307  }
308  }
309 
310  Info<< nl << endl;
311 
312  return true;
313 }
314 
315 
317 {
318  volRegion::update(); // Ensure cached values are valid
319 
321 
322  if (Pstream::master())
323  {
324  writeCurrentTime(file());
325  }
326 
327  // Only some operations need the cell volume
328  scalarField V;
329  if (usesVol())
330  {
331  V = filterField(fieldValue::mesh_.V());
332  }
333 
334  // Check availability and type of weight field
335  // Only support a few weight types:
336  // scalar: 0-N fields
337 
338  // Default is a zero-size scalar weight field (ie, weight = 1)
339  scalarField scalarWeights;
340 
341  for (const word& weightName : weightFieldNames_)
342  {
343  if (validField<scalar>(weightName))
344  {
345  tmp<scalarField> tfld = getFieldValues<scalar>(weightName, true);
346 
347  if (scalarWeights.empty())
348  {
349  scalarWeights = tfld;
350  }
351  else
352  {
353  scalarWeights *= tfld;
354  }
355  }
356  else if (weightName != "none")
357  {
358  // Silently ignore "none", flag everything else as an error
359 
360  // TBD: treat missing "rho" like incompressible with rho = 1
361  // and/or provided rhoRef value
362 
364  << "weightField " << weightName
365  << " not found or an unsupported type" << nl
366  << abort(FatalError);
367  }
368  }
369 
370 
371  // Process the fields
372  writeAll(V, scalarWeights);
373 
374  if (Pstream::master())
375  {
376  file()<< endl;
377  }
378 
379  Log << endl;
380 
381  return true;
382 }
383 
384 
385 // ************************************************************************* //
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Log
#define Log
Definition: PDRblock.C:35
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::functionObjects::fieldValues::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, fieldValueDelta, dictionary)
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::functionObjects::fieldValue::read
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: fieldValue.C:89
Foam::functionObjects::fieldValues::volFieldValue::usesWeight
bool usesWeight() const
True if the operation variant uses a weight-field.
Definition: volFieldValue.C:112
Foam::functionObjects::volRegion::update
bool update()
Update the cached values as required.
Definition: volRegion.C:228
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::functionObjects::volRegion
Volume (cell) region selection class.
Definition: volRegion.H:115
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::functionObjects::fieldValues::volFieldValue::postOperationType
postOperationType
Post-operation type enumeration.
Definition: volFieldValue.H:328
Foam::functionObjects::fieldValues::volFieldValue::volFieldValue
volFieldValue(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
Definition: volFieldValue.C:200
volFieldValue.H
Foam::functionObjects::fieldValues::volFieldValue::opWeightedVolIntegrate
Weighted volume integral.
Definition: volFieldValue.H:318
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::functionObjects::fieldValues::defineTypeNameAndDebug
defineTypeNameAndDebug(fieldValueDelta, 0)
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:458
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::functionObjects::fieldValues::volFieldValue::read
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: volFieldValue.C:255
Foam::functionObjects::fieldValues::volFieldValue::opCoV
Coefficient of variation.
Definition: volFieldValue.H:304
Foam::functionObjects::fieldValues::volFieldValue::operation_
operationType operation_
Operation to apply to values.
Definition: volFieldValue.H:344
Foam::functionObjects::fieldValues::volFieldValue::write
virtual bool write()
Calculate and write.
Definition: volFieldValue.C:316
Foam::functionObjects::fieldValues::volFieldValue::operationType
operationType
Operation type enumeration.
Definition: volFieldValue.H:292
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::functionObjects::fieldValues::volFieldValue::postOperationTypeNames_
static const Enum< postOperationType > postOperationTypeNames_
Operation type names.
Definition: volFieldValue.H:336
Foam::Field< scalar >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::functionObjects::fieldValues::volFieldValue::writeFileHeader
virtual void writeFileHeader(Ostream &os) const
Output file header information.
Definition: volFieldValue.C:133
Foam::functionObjects::fieldValues::volFieldValue::operationTypeNames_
static const Enum< operationType > operationTypeNames_
Operation type names.
Definition: volFieldValue.H:324
Foam::functionObjects::fieldValues::volFieldValue::usesMag
bool usesMag() const
True if the operation variant uses mag.
Definition: volFieldValue.C:105
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:217
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::tab
constexpr char tab
Definition: Ostream.H:384
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::FlatOutput::BareComma
Surround with '\0' and '\0' separate with ','.
Definition: FlatOutput.H:84
Foam::functionObjects::fieldValue
Intermediate class for handling field value-based function objects.
Definition: fieldValue.H:119
Foam::functionObjects::fvMeshFunctionObject::mesh_
const fvMesh & mesh_
Reference to the fvMesh.
Definition: fvMeshFunctionObject.H:73
Foam::functionObjects::volRegion::writeFileHeader
void writeFileHeader(const writeFile &wf, Ostream &file) const
Output file header information.
Definition: volRegion.C:123
Foam::functionObjects::fieldValues::volFieldValue::opWeightedVolAverage
Weighted volume average.
Definition: volFieldValue.H:315
Foam::functionObjects::fieldValues::volFieldValue::opVolAverage
Volume average.
Definition: volFieldValue.H:302
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::functionObjects::fieldValue::write
virtual bool write()
Write.
Definition: fieldValue.C:113
Foam::orOp
Definition: ops.H:234
Foam::functionObjects::fieldValues::volFieldValue::canWeight
bool canWeight(const scalarField &weightField) const
True if operation variant uses a weight-field that is available.
Definition: volFieldValue.C:120
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303
Foam::functionObjects::fieldValues::volFieldValue::writeAll
label writeAll(const scalarField &V, const scalarField &weightField)
Helper function to output field values.
Definition: volFieldValue.C:164
Foam::functionObjects::fieldValues::volFieldValue::opVolIntegrate
Volume integral.
Definition: volFieldValue.H:303
Foam::functionObjects::fieldValues::volFieldValue::usesVol
bool usesVol() const
True if the operation needs the cell-volume.
Definition: volFieldValue.C:87