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-2019 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 
74 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
75 
77 {
78  // Only a few operations require the cell volume
79  switch (operation_)
80  {
81  case opVolAverage:
82  case opVolIntegrate:
85  case opCoV:
86  return true;
87 
88  default:
89  return false;
90  }
91 }
92 
93 
95 {
96  // Operation specifically tagged to require a weight field
97  return (operation_ & typeWeighted);
98 }
99 
100 
102 (
103  const scalarField& weightField
104 ) const
105 {
106  return
107  (
108  usesWeight()
109  && returnReduce(!weightField.empty(), orOp<bool>()) // On some processor
110  );
111 }
112 
113 
115 (
116  const dictionary& dict
117 )
118 {
119  weightFieldName_ = "none";
120  if (usesWeight())
121  {
122  if (dict.readIfPresent("weightField", weightFieldName_))
123  {
124  Info<< " weight field = " << weightFieldName_;
125  }
126  else
127  {
128  // Suggest possible alternative unweighted operation?
130  << "The '" << operationTypeNames_[operation_]
131  << "' operation is missing a weightField." << nl
132  << "Either provide the weightField, "
133  << "use weightField 'none' to suppress weighting," << nl
134  << "or use a different operation."
135  << exit(FatalIOError);
136  }
137  }
138 
139  Info<< nl << endl;
140 }
141 
142 
144 (
145  Ostream& os
146 ) const
147 {
148  volRegion::writeFileHeader(*this, os);
149  if (weightFieldName_ != "none")
150  {
151  writeHeaderValue(os, "Weight field", weightFieldName_);
152  }
153 
154  writeCommented(os, "Time");
155 
156  for (const word& fieldName : fields_)
157  {
158  os << tab << operationTypeNames_[operation_]
159  << "(" << fieldName << ")";
160  }
161 
162  os << endl;
163 }
164 
165 
167 (
168  const scalarField& V,
169  const scalarField& weightField
170 )
171 {
172  label nProcessed = 0;
173 
174  for (const word& fieldName : fields_)
175  {
176  if
177  (
178  writeValues<scalar>(fieldName, V, weightField)
179  || writeValues<vector>(fieldName, V, weightField)
180  || writeValues<sphericalTensor>(fieldName, V, weightField)
181  || writeValues<symmTensor>(fieldName, V, weightField)
182  || writeValues<tensor>(fieldName, V, weightField)
183  )
184  {
185  ++nProcessed;
186  }
187  else
188  {
190  << "Requested field " << fieldName
191  << " not found in database and not processed"
192  << endl;
193  }
194  }
195 
196  return nProcessed;
197 }
198 
199 
200 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
201 
203 (
204  const word& name,
205  const Time& runTime,
206  const dictionary& dict
207 )
208 :
209  fieldValue(name, runTime, dict, typeName),
211  operation_(operationTypeNames_.get("operation", dict)),
212  weightFieldName_("none")
213 {
214  read(dict);
215  writeFileHeader(file());
216 }
217 
218 
220 (
221  const word& name,
222  const objectRegistry& obr,
223  const dictionary& dict
224 )
225 :
226  fieldValue(name, obr, dict, typeName),
228  operation_(operationTypeNames_.get("operation", dict)),
229  weightFieldName_("none")
230 {
231  read(dict);
232 }
233 
234 
235 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
236 
238 (
239  const dictionary& dict
240 )
241 {
243  initialise(dict);
244 
245  return true;
246 }
247 
248 
250 {
251  volRegion::update(); // Ensure cached values are valid
252 
254 
255  if (Pstream::master())
256  {
257  writeCurrentTime(file());
258  }
259 
260  // Only some operations need the cell volume
261  scalarField V;
262  if (usesVol())
263  {
264  V = filterField(fieldValue::mesh_.V());
265  }
266 
267  // Weight field - zero-size means weight = 1
268  scalarField weightField;
269  if (weightFieldName_ != "none")
270  {
271  weightField = getFieldValues<scalar>(weightFieldName_, true);
272  }
273 
274  writeAll(V, weightField);
275 
276  if (Pstream::master())
277  {
278  file()<< endl;
279  }
280 
281  Log << endl;
282 
283  return true;
284 }
285 
286 
287 // ************************************************************************* //
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:51
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:91
Foam::functionObjects::fieldValues::volFieldValue::usesWeight
bool usesWeight() const
True if the operation variant uses a weight-field.
Definition: volFieldValue.C:94
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::functionObjects::fieldValues::volFieldValue::volFieldValue
volFieldValue(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
Definition: volFieldValue.C:203
volFieldValue.H
Foam::functionObjects::fieldValues::volFieldValue::opWeightedVolIntegrate
Weighted volume integral.
Definition: volFieldValue.H:278
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::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::functionObjects::fieldValues::volFieldValue::read
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: volFieldValue.C:238
Foam::functionObjects::fieldValues::volFieldValue::opCoV
Coefficient of variation.
Definition: volFieldValue.H:264
Foam::functionObjects::fieldValues::volFieldValue::operation_
operationType operation_
Operation to apply to values.
Definition: volFieldValue.H:290
Foam::functionObjects::fieldValues::volFieldValue::write
virtual bool write()
Calculate and write.
Definition: volFieldValue.C:249
Foam::functionObjects::fieldValues::volFieldValue::operationType
operationType
Operation type enumeration.
Definition: volFieldValue.H:252
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::label
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:62
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:144
Foam::functionObjects::fieldValues::volFieldValue::operationTypeNames_
static const Enum< operationType > operationTypeNames_
Operation type names.
Definition: volFieldValue.H:282
Foam::functionObjects::fieldValues::volFieldValue::initialise
void initialise(const dictionary &dict)
Initialise, e.g. cell addressing.
Definition: volFieldValue.C:115
dict
dictionary dict
Definition: searchingEngine.H:14
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::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::tab
constexpr char tab
Definition: Ostream.H:371
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::functionObjects::fieldValue
Base class for field value-based function objects.
Definition: fieldValue.H:65
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:275
Foam::functionObjects::fieldValues::volFieldValue::opVolAverage
Volume average.
Definition: volFieldValue.H:262
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
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:115
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:102
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::functionObjects::fieldValues::volFieldValue::writeAll
label writeAll(const scalarField &V, const scalarField &weightField)
Helper function to output field values.
Definition: volFieldValue.C:167
Foam::functionObjects::fieldValues::volFieldValue::opVolIntegrate
Volume integral.
Definition: volFieldValue.H:263
Log
#define Log
Report write to Foam::Info if the local log switch is true.
Definition: messageStream.H:332
Foam::functionObjects::fieldValues::volFieldValue::usesVol
bool usesVol() const
True if the operation needs the cell-volume.
Definition: volFieldValue.C:76