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-2022 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  cellIds_.clear();
62  regionIDs_.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  {
85  regionIDs_ = volMesh_.cellZones().indices(regionName_);
86 
87  if (regionIDs_.empty())
88  {
90  << "Unknown cell zone: " << regionName_ << nl
91  << " Valid zones : "
92  << flatOutput(volMesh_.cellZones().names()) << nl
93  << " Valid groups: "
94  << flatOutput(volMesh_.cellZones().groupNames()) << nl
95  << exit(FatalError);
96  }
97 
98  if (regionIDs_.size() > 1)
99  {
100  cellIds_ =
101  volMesh_.cellZones().selection(regionIDs_).sortedToc();
102  }
103  break;
104  }
105  }
106 
107 
108  // Calculate cache value for nCells() and V()
109  const labelList& selected = this->cellIDs();
110 
111  V_ = 0;
112  for (const label celli : selected)
113  {
114  V_ += volMesh_.V()[celli];
115  }
116 
117  nCells_ = returnReduce(selected.size(), sumOp<label>());
118  reduce(V_, sumOp<scalar>());
119 
120  if (!nCells_)
121  {
124  << '(' << regionName_ << "):" << nl
125  << " Region has no cells" << nl
126  << exit(FatalError);
127  }
128 }
129 
130 
131 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
132 
134 (
135  const writeFile& wf,
136  Ostream& file
137 ) const
138 {
139  wf.writeCommented(file, "Region");
140  file<< setw(1) << ':' << setw(1) << ' '
141  << regionTypeNames_[regionType_] << ' ' << regionName_ << endl;
142  wf.writeHeaderValue(file, "Cells", nCells_);
143  wf.writeHeaderValue(file, "Volume", V_);
144 }
145 
146 
147 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
148 
150 (
151  const fvMesh& mesh,
152  const dictionary& dict
153 )
154 :
155  volMesh_(mesh),
156  cellIds_(),
157  regionIDs_(),
158  nCells_(0),
159  V_(Zero),
160  requireUpdate_(true),
161  regionType_
162  (
163  regionTypeNames_.getOrDefault
164  (
165  "regionType",
166  dict,
167  regionTypes::vrtAll
168  )
169  ),
170  regionName_(volMesh_.name())
171 {
172  read(dict);
173 }
174 
175 
176 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
177 
179 {
180  switch (regionType_)
181  {
182  case vrtAll:
183  {
184  regionName_ = volMesh_.name();
185  break;
186  }
187 
188  case vrtCellSet:
189  case vrtCellZone:
190  {
191  dict.readEntry("name", regionName_);
192  break;
193  }
194 
195  default:
196  {
198  << "Unknown region type. Valid region types: "
199  << flatOutput(regionTypeNames_.names()) << nl
200  << exit(FatalIOError);
201  break;
202  }
203  }
204 
205  calculateCache();
206  return true;
207 }
208 
209 
211 {
212  #ifdef FULLDEBUG
213  if (requireUpdate_)
214  {
216  << "Retrieving cached values that are not up-to-date" << nl
217  << exit(FatalError);
218  }
219  #endif
220 
221  switch (regionType_)
222  {
223  case vrtCellSet:
224  {
225  return cellIds_;
226  break;
227  }
228 
229  case vrtCellZone:
230  {
231  if (regionIDs_.size() == 1)
232  {
233  return volMesh_.cellZones()[regionIDs_.first()];
234  }
235  else
236  {
237  return cellIds_;
238  }
239  break;
240  }
241 
242  default:
243  break;
244  }
245 
246  return labelList::null();
247 }
248 
249 
251 {
252  if (requireUpdate_)
253  {
254  calculateCache();
255  return true;
256  }
257 
258  return false;
259 }
260 
263 {
264  requireUpdate_ = true;
265 }
266 
267 
269 {
270  requireUpdate_ = true;
271 }
272 
273 
274 // ************************************************************************* //
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
defineTypeNameAndDebug(ObukhovLength, 0)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
void writeFileHeader(const writeFile &wf, Ostream &file) const
Output file header information.
Definition: volRegion.C:127
static const Enum< regionTypes > regionTypeNames_
Region type names.
Definition: volRegion.H:130
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: volRegion.C:171
virtual void movePoints(const polyMesh &)
Update for mesh point-motion.
Definition: volRegion.C:261
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:666
labelList sortedToc() const
The indices of the on bits as a sorted labelList.
Definition: bitSetI.H:447
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual void updateMesh(const mapPolyMesh &)
Update for changes of mesh.
Definition: volRegion.C:255
static const List< label > & null()
Return a null List.
Definition: ListI.H:130
const DimensionedField< scalar, volMesh > & V() const
Return cell volumes.
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:157
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
dynamicFvMesh & mesh
Type gSum(const FieldField< Field, Type > &f)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
labelList indices(const wordRe &matcher, const bool useGroups=true) const
Return (sorted) zone indices for all matches.
Definition: ZoneMesh.C:435
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:137
const globalMeshData & globalData() const
Return parallel info (demand-driven)
Definition: polyMesh.C:1311
regionTypes
Region type enumeration.
Definition: volRegion.H:120
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
wordRe regionName_
Region name (cellSet, cellZone, ...)
Definition: volRegion.H:188
label nTotalCells() const noexcept
Return total number of cells in decomposed mesh.
volRegion(const fvMesh &mesh, const dictionary &dict)
Construct from fvMesh and dictionary.
Definition: volRegion.C:143
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
const labelList & cellIDs() const
Return the local list of cell IDs.
Definition: volRegion.C:203
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
bool update()
Update the cached values as required.
Definition: volRegion.C:243
wordList groupNames() const
A list of the zone group names (if any)
Definition: ZoneMesh.C:369
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:678
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.
regionTypes regionType_
Region type.
Definition: volRegion.H:183
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:362
bitSet selection(const labelUList &zoneIds) const
Return all elements (cells, faces, points) contained in the listed zones.
Definition: ZoneMesh.C:706
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
List< label > labelList
A List of labels.
Definition: List.H:62
Namespace for OpenFOAM.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127