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-2021 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  // Update now. Need a valid state for the cellIDs() call
65  requireUpdate_ = false;
66 
67  switch (regionType_)
68  {
69  case vrtAll:
70  {
71  nCells_ = volMesh_.globalData().nTotalCells();
72  V_ = gSum(volMesh_.V());
73  return;
74  break;
75  }
76 
77  case vrtCellSet:
78  {
79  cellIds_ = cellSet(volMesh_, regionName_).sortedToc();
80  break;
81  }
82 
83  case vrtCellZone:
84  {
86 
87  if (regionID_ < 0)
88  {
90  << "Unknown cell zone name: " << regionName_
91  << ". Valid cell zones : "
92  << flatOutput(volMesh_.cellZones().names())
93  << exit(FatalError);
94  }
95  break;
96  }
97  }
98 
99 
100  // Calculate cache value for nCells() and V()
101  const labelList& selected = this->cellIDs();
102 
103  nCells_ = selected.size();
104  V_ = 0;
105  for (const label celli : selected)
106  {
107  V_ += volMesh_.V()[celli];
108  }
109 
110  reduce(nCells_, sumOp<label>());
111  reduce(V_, sumOp<scalar>());
112 
113  if (!nCells_)
114  {
117  << '(' << regionName_ << "):" << nl
118  << " Region has no cells" << nl
119  << exit(FatalError);
120  }
121 }
122 
123 
124 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
125 
127 (
128  const writeFile& wf,
129  Ostream& file
130 ) const
131 {
132  wf.writeCommented(file, "Region");
133  file<< setw(1) << ':' << setw(1) << ' '
134  << regionTypeNames_[regionType_] << ' ' << regionName_ << endl;
135  wf.writeHeaderValue(file, "Cells", nCells_);
136  wf.writeHeaderValue(file, "Volume", V_);
137 }
138 
139 
140 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
141 
143 (
144  const fvMesh& mesh,
145  const dictionary& dict
146 )
147 :
148  volMesh_(mesh),
149  requireUpdate_(true),
150  cellIds_(),
151  nCells_(0),
152  V_(Zero),
153  regionType_
154  (
155  regionTypeNames_.getOrDefault
156  (
157  "regionType",
158  dict,
159  regionTypes::vrtAll
160  )
161  ),
162  regionName_(volMesh_.name()),
163  regionID_(-1)
164 {
165  read(dict);
166 }
167 
168 
169 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
170 
172 {
173  switch (regionType_)
174  {
175  case vrtAll:
176  {
177  regionName_ = volMesh_.name();
178  break;
179  }
180 
181  case vrtCellSet:
182  case vrtCellZone:
183  {
184  dict.readEntry("name", regionName_);
185  break;
186  }
187 
188  default:
189  {
191  << "Unknown region type. Valid region types: "
192  << flatOutput(regionTypeNames_.names()) << nl
193  << exit(FatalIOError);
194  break;
195  }
196  }
197 
198  calculateCache();
199  return true;
200 }
201 
202 
204 {
205  #ifdef FULLDEBUG
206  if (requireUpdate_)
207  {
209  << "Retrieving cached values that are not up-to-date" << nl
210  << exit(FatalError);
211  }
212  #endif
213 
214  switch (regionType_)
215  {
216  case vrtCellSet:
217  return cellIds_;
218  break;
219 
220  case vrtCellZone:
221  return volMesh_.cellZones()[regionID_];
222  break;
223 
224  default:
225  break;
226  }
227 
228  return labelList::null();
229 }
230 
231 
233 {
234  if (requireUpdate_)
235  {
236  calculateCache();
237  return true;
238  }
239 
240  return false;
241 }
242 
243 
245 {
246  requireUpdate_ = true;
247 }
248 
249 
251 {
252  requireUpdate_ = true;
253 }
254 
255 
256 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
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::null
static const List< T > & null()
Return a null List.
Definition: ListI.H:109
Foam::functionObjects::volRegion::update
bool update()
Update the cached values as required.
Definition: volRegion.C:232
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::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:369
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:244
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:171
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::polyMesh::cellZones
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:492
Foam::functionObjects::volRegion::volRegion
volRegion(const fvMesh &mesh, const dictionary &dict)
Construct from fvMesh and dictionary.
Definition: volRegion.C:143
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:123
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
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:216
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:519
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:305
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:453
Foam::functionObjects::writeFile::writeCommented
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:272
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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:250
Foam::List< label >
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Foam::functionObjects::volRegion::cellIDs
const labelList & cellIDs() const
Return the local list of cell IDs.
Definition: volRegion.C:203
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:127
Foam::globalMeshData::nTotalCells
label nTotalCells() const noexcept
Return total number of cells in decomposed mesh.
Definition: globalMeshData.H:371
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:473
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