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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016 OpenFOAM Foundation
9  Copyright (C) 2016-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 "volRegion.H"
30 #include "volMesh.H"
31 #include "cellSet.H"
32 #include "globalMeshData.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40  defineTypeNameAndDebug(volRegion, 0);
41 }
42 }
43 
44 
45 const Foam::Enum
46 <
48 >
50 ({
51  { regionTypes::vrtAll, "all" },
52  { regionTypes::vrtCellSet, "cellSet" },
53  { regionTypes::vrtCellZone, "cellZone" },
54 });
55 
56 
57 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
58 
59 void Foam::functionObjects::volRegion::calculateCache()
60 {
61  regionID_ = -1;
62  cellIds_.clear();
63 
64  switch (regionType_)
65  {
66  case vrtAll:
67  {
68  nCells_ = volMesh_.globalData().nTotalCells();
69  V_ = gSum(volMesh_.V());
70  return;
71  break;
72  }
73 
74  case vrtCellSet:
75  {
76  cellIds_ = cellSet(volMesh_, regionName_).sortedToc();
77  break;
78  }
79 
80  case vrtCellZone:
81  {
83 
84  if (regionID_ < 0)
85  {
87  << "Unknown cell zone name: " << regionName_
88  << ". Valid cell zones : "
89  << flatOutput(volMesh_.cellZones().names())
90  << exit(FatalError);
91  }
92  break;
93  }
94  }
95 
96  // Cached value for nCells()
97  nCells_ = returnReduce(cellIDs().size(), sumOp<label>());
98 
99  // Cached value for V()
100  V_ = 0;
101  for (const label celli : cellIDs())
102  {
103  V_ += volMesh_.V()[celli];
104  }
105  reduce(V_, sumOp<scalar>());
106 
107  if (!nCells_)
108  {
111  << "(" << regionName_ << "):" << nl
112  << " Region has no cells"
113  << exit(FatalError);
114  }
115 
116  requireUpdate_ = false;
117 }
118 
119 
120 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
121 
123 (
124  const writeFile& wf,
125  Ostream& file
126 ) const
127 {
128  wf.writeCommented(file, "Region");
129  file<< setw(1) << ':' << setw(1) << ' '
130  << regionTypeNames_[regionType_] << " " << regionName_ << endl;
131  wf.writeHeaderValue(file, "Cells", nCells_);
132  wf.writeHeaderValue(file, "Volume", V_);
133 }
134 
135 
136 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
137 
139 (
140  const fvMesh& mesh,
141  const dictionary& dict
142 )
143 :
144  volMesh_(mesh),
145  requireUpdate_(true),
146  cellIds_(),
147  nCells_(0),
148  V_(Zero),
149  regionType_
150  (
151  regionTypeNames_.getOrDefault
152  (
153  "regionType",
154  dict,
155  regionTypes::vrtAll
156  )
157  ),
158  regionName_(volMesh_.name()),
159  regionID_(-1)
160 {
161  read(dict);
162 }
163 
164 
165 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166 
168 {
169  switch (regionType_)
170  {
171  case vrtAll:
172  {
173  regionName_ = volMesh_.name();
174  break;
175  }
176 
177  case vrtCellSet:
178  case vrtCellZone:
179  {
180  dict.readEntry("name", regionName_);
181  break;
182  }
183 
184  default:
185  {
187  << "Unknown region type. Valid region types are:"
188  << flatOutput(regionTypeNames_.names()) << nl
189  << exit(FatalIOError);
190  break;
191  }
192  }
193 
194  calculateCache();
195  return true;
196 }
197 
198 
200 {
201  #ifdef FULLDEBUG
202  if (requireUpdate_)
203  {
205  << "Retrieving cached values that are not up-to-date" << nl
206  << exit(FatalError);
207  }
208  #endif
209 
210  switch (regionType_)
211  {
212  case vrtCellSet:
213  return cellIds_;
214  break;
215 
216  case vrtCellZone:
217  return volMesh_.cellZones()[regionID_];
218  break;
219 
220  default:
221  break;
222  }
223 
224  return labelList::null();
225 }
226 
227 
229 {
230  if (requireUpdate_)
231  {
232  calculateCache();
233  return true;
234  }
235 
236  return false;
237 }
238 
239 
241 {
242  requireUpdate_ = true;
243 }
244 
245 
247 {
248  requireUpdate_ = true;
249 }
250 
251 
252 // ************************************************************************* //
Foam::functionObjects::volRegion::regionID_
label regionID_
Region ID (zone ID, ...)
Definition: volRegion.H:167
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::functionObjects::volRegion::vrtCellZone
A cellZone.
Definition: volRegion.H:149
Foam::List< label >::null
static const List< label > & null()
Return a null List.
Definition: ListI.H:108
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::regionName_
word regionName_
Region name (cellSet, cellZone, ...)
Definition: volRegion.H:164
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
globalMeshData.H
volMesh.H
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::globalMeshData::nTotalCells
label nTotalCells() const
Return total number of cells in decomposed mesh.
Definition: globalMeshData.H:371
Foam::polyMesh::cellZones
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:492
Foam::functionObjects::volRegion::vrtAll
All cells.
Definition: volRegion.H:147
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::gSum
Type gSum(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:594
Foam::functionObjects::volRegion::regionTypes
regionTypes
Region type enumeration.
Definition: volRegion.H:145
Foam::functionObjects::writeFile::writeHeaderValue
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
Definition: writeFileTemplates.C:32
Foam::functionObjects::volRegion::updateMesh
virtual void updateMesh(const mapPolyMesh &)
Update for changes of mesh.
Definition: volRegion.C:240
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::functionObjects::volRegion::regionType_
regionTypes regionType_
Region type.
Definition: volRegion.H:161
Foam::functionObjects::volRegion::read
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: volRegion.C:167
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::functionObjects::volRegion::volRegion
volRegion(const fvMesh &mesh, const dictionary &dict)
Construct from fvMesh and dictionary.
Definition: volRegion.C:139
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
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:83
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::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::ZoneMesh::findZoneID
label findZoneID(const word &zoneName) const
Find zone index by name, return -1 if not found.
Definition: ZoneMesh.C:484
Foam::functionObjects::volRegion::vrtCellSet
A cellSet.
Definition: volRegion.H:148
Foam::ZoneMesh::names
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:340
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
volRegion.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::functionObjects::writeFile::writeCommented
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:273
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::functionObjects::volRegion::regionTypeNames_
static const Enum< regionTypes > regionTypeNames_
Region type names.
Definition: volRegion.H:153
Foam::functionObjects::volRegion::movePoints
virtual void movePoints(const polyMesh &)
Update for mesh point-motion.
Definition: volRegion.C:246
Foam::List< label >
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:115
Foam::functionObjects::volRegion::cellIDs
const labelList & cellIDs() const
Return the local list of cell IDs.
Definition: volRegion.C:199
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:161
Foam::functionObjects::volRegion::writeFileHeader
void writeFileHeader(const writeFile &wf, Ostream &file) const
Output file header information.
Definition: volRegion.C:123
Foam::functionObjects::writeFile
Base class for writing single files from the function objects.
Definition: writeFile.H:119
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
cellSet.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::polyMesh::globalData
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1295
Foam::fvMesh::V
const DimensionedField< scalar, volMesh > & V() const
Return cell volumes.
Definition: fvMeshGeometry.C:179