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-------------------------------------------------------------------------------
11License
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
36namespace Foam
37{
38namespace functionObjects
39{
41}
42}
43
44
45const 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
59void 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
261
263{
264 requireUpdate_ = true;
265}
266
267
269{
270 requireUpdate_ = true;
271}
272
273
274// ************************************************************************* //
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
static const List< label > & null()
Return a null List.
Definition: ListI.H:109
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
bitSet selection(const labelUList &zoneIds) const
Definition: ZoneMesh.C:605
wordList groupNames() const
A list of the zone group names (if any)
Definition: ZoneMesh.C:311
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:304
labelList indices(const wordRe &matcher, const bool useGroups=true) const
Return (sorted) zone indices for all matches.
Definition: ZoneMesh.C:377
labelList sortedToc() const
The indices of the on bits as a sorted labelList.
Definition: bitSetI.H:533
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Volume (cell) region selection class.
Definition: volRegion.H:116
wordRe regionName_
Region name (cellSet, cellZone, ...)
Definition: volRegion.H:170
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: volRegion.C:178
static const Enum< regionTypes > regionTypeNames_
Region type names.
Definition: volRegion.H:130
bool update()
Update the cached values as required.
Definition: volRegion.C:250
regionTypes regionType_
Region type.
Definition: volRegion.H:167
regionTypes
Region type enumeration.
Definition: volRegion.H:123
void writeFileHeader(const writeFile &wf, Ostream &file) const
Output file header information.
Definition: volRegion.C:134
const labelList & cellIDs() const
Return the local list of cell IDs.
Definition: volRegion.C:210
Base class for writing single files from the function objects.
Definition: writeFile.H:120
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:269
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
const DimensionedField< scalar, volMesh > & V() const
Return cell volumes.
label nTotalCells() const noexcept
Return total number of cells in decomposed mesh.
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:162
void movePoints()
Update for new mesh geometry.
void updateMesh()
Update for new mesh topology.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1310
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:504
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
dynamicFvMesh & mesh
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Namespace for OpenFOAM.
Type gSum(const FieldField< Field, Type > &f)
List< label > labelList
A List of labels.
Definition: List.H:66
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
IOerror FatalIOError
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce (copy) and return value.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict