volRegion.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 | Copyright (C) 2011-2016 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 "volRegion.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(volRegion, 0);
40  addToRunTimeSelectionTable(fieldValue, volRegion, dictionary);
41  addToRunTimeSelectionTable(functionObject, volRegion, dictionary);
42 }
43 }
44 }
45 
46 template<>
47 const char*
49 <
51  2
52 >::names[] = {"cellZone", "all"};
53 
54 template<>
55 const char*
57 <
59  11
60 >::names[] =
61 {
62  "none",
63  "sum",
64  "sumMag",
65  "average",
66  "weightedAverage",
67  "volAverage",
68  "weightedVolAverage",
69  "volIntegrate",
70  "min",
71  "max",
72  "CoV"
73 };
74 
75 const Foam::NamedEnum
76 <
78  2
80 
81 const Foam::NamedEnum
82 <
84  11
86 
87 
88 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
89 
90 void Foam::functionObjects::fieldValues::volRegion::setCellZoneCells()
91 {
92  switch (regionType_)
93  {
94  case stCellZone:
95  {
96  dict().lookup("name") >> regionName_;
97 
98  label zoneId = mesh().cellZones().findZoneID(regionName_);
99 
100  if (zoneId < 0)
101  {
103  << "Unknown cell zone name: " << regionName_
104  << ". Valid cell zones are: " << mesh().cellZones().names()
105  << nl << exit(FatalError);
106  }
107 
108  cellId_ = mesh().cellZones()[zoneId];
109  nCells_ = returnReduce(cellId_.size(), sumOp<label>());
110  break;
111  }
112 
113  case stAll:
114  {
115  cellId_ = identity(mesh().nCells());
116  nCells_ = returnReduce(cellId_.size(), sumOp<label>());
117  break;
118  }
119 
120  default:
121  {
123  << "Unknown region type. Valid region types are:"
124  << regionTypeNames_ << nl << exit(FatalError);
125  }
126  }
127 
128  if (debug)
129  {
130  Pout<< "Selected region size = " << cellId_.size() << endl;
131  }
132 }
133 
134 
135 Foam::scalar Foam::functionObjects::fieldValues::volRegion::volume() const
136 {
137  return gSum(filterField(mesh().V()));
138 }
139 
140 
141 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
142 
144 (
145  const dictionary& dict
146 )
147 {
148  setCellZoneCells();
149 
150  if (nCells_ == 0)
151  {
153  << type() << " " << name() << ": "
154  << regionTypeNames_[regionType_] << "(" << regionName_ << "):" << nl
155  << " Region has no cells" << exit(FatalError);
156  }
157 
158  volume_ = volume();
159 
160  Info<< type() << " " << name() << ":"
161  << regionTypeNames_[regionType_] << "(" << regionName_ << "):" << nl
162  << " total cells = " << nCells_ << nl
163  << " total volume = " << volume_
164  << nl << endl;
165 
166  if (dict.readIfPresent("weightField", weightFieldName_))
167  {
168  Info<< " weight field = " << weightFieldName_;
169  }
170 
171  Info<< nl << endl;
172 }
173 
174 
176 (
177  const label i
178 )
179 {
180  writeCommented(file(), "Region type : ");
181  file() << regionTypeNames_[regionType_] << " " << regionName_ << endl;
182  writeCommented(file(), "Cells : ");
183  file() << nCells_ << endl;
184  writeCommented(file(), "Volume : ");
185  file() << volume_ << endl;
186 
187  writeCommented(file(), "Time");
188  if (writeVolume_)
189  {
190  file() << tab << "Volume";
191  }
192 
193  forAll(fields_, i)
194  {
195  file()
196  << tab << operationTypeNames_[operation_]
197  << "(" << fields_[i] << ")";
198  }
199 
200  file() << endl;
201 }
202 
203 
204 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
205 
207 (
208  const word& name,
209  const Time& runTime,
210  const dictionary& dict
211 )
212 :
213  fieldValue(name, runTime, dict, typeName),
214  regionType_(regionTypeNames_.read(dict.lookup("regionType"))),
215  operation_(operationTypeNames_.read(dict.lookup("operation"))),
216  nCells_(0),
217  cellId_(),
218  weightFieldName_("none"),
219  writeVolume_(dict.lookupOrDefault("writeVolume", false))
220 {
221  if (!isA<fvMesh>(obr_))
222  {
224  << "objectRegistry is not an fvMesh" << exit(FatalError);
225  }
226 
227  read(dict);
228 }
229 
230 
232 (
233  const word& name,
234  const objectRegistry& obr,
235  const dictionary& dict
236 )
237 :
238  fieldValue(name, obr, dict, typeName),
239  regionType_(regionTypeNames_.read(dict.lookup("regionType"))),
240  operation_(operationTypeNames_.read(dict.lookup("operation"))),
241  nCells_(0),
242  cellId_(),
243  weightFieldName_("none"),
244  writeVolume_(dict.lookupOrDefault("writeVolume", false))
245 {
246  if (!isA<fvMesh>(obr_))
247  {
249  << "objectRegistry is not an fvMesh" << exit(FatalError);
250  }
251 
252  read(dict);
253 }
254 
255 
256 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
257 
259 {}
260 
261 
262 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
263 
265 (
266  const dictionary& dict
267 )
268 {
269  fieldValue::read(dict);
270 
271  // No additional info to read
272  initialise(dict);
273 
274  return true;
275 }
276 
277 
279 {
281 
282  if (Pstream::master())
283  {
284  writeTime(file());
285  }
286 
287  if (writeVolume_)
288  {
289  volume_ = volume();
290  if (Pstream::master())
291  {
292  file() << tab << volume_;
293  }
294  Log << " total volume = " << volume_ << endl;
295  }
296 
297  forAll(fields_, i)
298  {
299  const word& fieldName = fields_[i];
300  bool processed = false;
301 
302  processed = processed || writeValues<scalar>(fieldName);
303  processed = processed || writeValues<vector>(fieldName);
304  processed = processed || writeValues<sphericalTensor>(fieldName);
305  processed = processed || writeValues<symmTensor>(fieldName);
306  processed = processed || writeValues<tensor>(fieldName);
307 
308  if (!processed)
309  {
311  << "Requested field " << fieldName
312  << " not found in database and not processed"
313  << endl;
314  }
315  }
316 
317  if (Pstream::master())
318  {
319  file()<< endl;
320  }
321 
322  Log << endl;
323 
324  return true;
325 }
326 
327 
328 // ************************************************************************* //
dictionary dict
virtual bool write()
Calculate and write.
Definition: volRegion.C:278
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const char tab
Definition: Ostream.H:261
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
addToRunTimeSelectionTable(functionObject, fieldValueDelta, dictionary)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: fieldValue.C:88
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:411
labelList identity(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
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.
void initialise(const dictionary &dict)
Initialise, e.g. cell addressing.
Definition: volRegion.C:144
bool read(const char *, int32_t &)
Definition: int32IO.C:85
static const NamedEnum< operationType, 11 > operationTypeNames_
Operation type names.
Definition: volRegion.H:259
dynamicFvMesh & mesh
Type gSum(const FieldField< Field, Type > &f)
A class for handling words, derived from string.
Definition: word.H:59
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
defineTypeNameAndDebug(fieldValueDelta, 0)
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
static const char nl
Definition: Ostream.H:262
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const NamedEnum< regionTypes, 2 > regionTypeNames_
region type names
Definition: volRegion.H:239
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:461
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool read(const dictionary &)
Read from dictionary.
Definition: volRegion.C:265
#define Log
Report write to Foam::Info if the local log switch is true.
messageStream Info
volRegion(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
Definition: volRegion.C:207
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
virtual void writeFileHeader(const label i)
Output file header information.
Definition: volRegion.C:176
Registry of regIOobjects.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Namespace for OpenFOAM.
virtual bool write()
Write to screen/file.
Definition: fieldValue.C:106
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451
operationType
Operation type enumeration.
Definition: volRegion.H:243