cellZoneSet.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2018-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 "cellZoneSet.H"
30 #include "mapPolyMesh.H"
31 #include "polyMesh.H"
32 
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineTypeNameAndDebug(cellZoneSet, 0);
43 }
44 
45 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
46 
48 {
49  labelList order(sortedOrder(addressing_));
50  inplaceReorder(order, addressing_);
51 
53  cellSet::reserve(addressing_.size());
54  cellSet::set(addressing_);
55 }
56 
57 
58 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
59 
61 (
62  const polyMesh& mesh,
63  const word& name,
66 )
67 :
68  cellSet(mesh, name, 1024), // do not read cellSet
69  mesh_(mesh),
70  addressing_()
71 {
72  const cellZoneMesh& cellZones = mesh.cellZones();
73  label zoneID = cellZones.findZoneID(name);
74 
75  if
76  (
78  || (IOobjectOption::isReadOptional(rOpt) && zoneID != -1)
79  )
80  {
81  const cellZone& fz = cellZones[zoneID];
82  addressing_ = fz;
83  }
84 
86 
87  check(mesh.nCells());
88 }
89 
90 
92 (
93  const polyMesh& mesh,
94  const word& name,
95  const label size,
97 )
98 :
99  cellSet(mesh, name, size, wOpt),
100  mesh_(mesh),
101  addressing_()
102 {
103  updateSet();
104 }
105 
106 
108 (
109  const polyMesh& mesh,
110  const word& name,
111  const topoSet& set,
113 )
114 :
115  cellSet(mesh, name, set.size(), wOpt),
116  mesh_(mesh),
117  addressing_(refCast<const cellZoneSet>(set).addressing())
118 {
119  updateSet();
120 }
121 
122 
123 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
124 
125 void Foam::cellZoneSet::invert(const label maxLen)
126 {
127  // Count
128  label n = 0;
129 
130  for (label celli = 0; celli < maxLen; ++celli)
131  {
132  if (!found(celli))
133  {
134  ++n;
135  }
136  }
137 
138  // Fill
139  addressing_.setSize(n);
140  n = 0;
141 
142  for (label celli = 0; celli < maxLen; ++celli)
143  {
144  if (!found(celli))
145  {
146  addressing_[n] = celli;
147  ++n;
148  }
149  }
150 
151  updateSet();
152 }
153 
154 
155 void Foam::cellZoneSet::subset(const topoSet& set)
156 {
157  DynamicList<label> newAddressing(addressing_.size());
158 
159  const cellZoneSet& zoneSet = refCast<const cellZoneSet>(set);
160 
161  for (const label celli : zoneSet.addressing())
162  {
163  if (found(celli))
164  {
165  newAddressing.append(celli);
166  }
167  }
168 
169  addressing_.transfer(newAddressing);
170  updateSet();
171 }
172 
173 
174 void Foam::cellZoneSet::addSet(const topoSet& set)
175 {
176  DynamicList<label> newAddressing(addressing_);
177 
178  const cellZoneSet& zoneSet = refCast<const cellZoneSet>(set);
179 
180  for (const label celli : zoneSet.addressing())
181  {
182  if (!found(celli))
183  {
184  newAddressing.append(celli);
185  }
186  }
187 
188  addressing_.transfer(newAddressing);
189  updateSet();
190 }
191 
192 
193 void Foam::cellZoneSet::subtractSet(const topoSet& set)
194 {
195  DynamicList<label> newAddressing(addressing_.size());
196 
197  const cellZoneSet& zoneSet = refCast<const cellZoneSet>(set);
198 
199  for (const label celli : addressing_)
200  {
201  if (!zoneSet.found(celli))
202  {
203  // Not found in zoneSet so add
204  newAddressing.append(celli);
205  }
206  }
207 
208  addressing_.transfer(newAddressing);
209  updateSet();
210 }
211 
212 
213 void Foam::cellZoneSet::sync(const polyMesh& mesh)
214 {
217  // Take over contents of cellSet into addressing.
218  addressing_ = sortedToc();
219  updateSet();
220 }
221 
222 
223 Foam::label Foam::cellZoneSet::maxSize(const polyMesh& mesh) const
224 {
225  return mesh.nCells();
226 }
227 
228 
230 (
231  IOstreamOption streamOpt,
232  const bool writeOnProc
233 ) const
234 {
235  // Write shadow cellSet
236  word oldTypeName = typeName;
237  const_cast<word&>(type()) = cellSet::typeName;
238  bool ok = cellSet::writeObject(streamOpt, writeOnProc);
239  const_cast<word&>(type()) = oldTypeName;
240 
241  // Modify cellZone
242  cellZoneMesh& cellZones = const_cast<polyMesh&>(mesh_).cellZones();
243  label zoneID = cellZones.findZoneID(name());
244 
245  if (zoneID == -1)
246  {
247  zoneID = cellZones.size();
248 
249  cellZones.emplace_back
250  (
251  name(),
252  addressing_,
253  zoneID,
254  cellZones
255  );
256  }
257  else
258  {
259  cellZones[zoneID] = addressing_;
260  }
261  cellZones.clearAddressing();
262 
263  return ok && cellZones.write(writeOnProc);
264 }
265 
266 
267 void Foam::cellZoneSet::updateMesh(const mapPolyMesh& morphMap)
268 {
269  // cellZone
270  labelList newAddressing(addressing_.size());
271 
272  label n = 0;
273  for (const label celli : addressing_)
274  {
275  label newCelli = morphMap.reverseCellMap()[celli];
276  if (newCelli >= 0)
277  {
278  newAddressing[n] = newCelli;
279  ++n;
280  }
281  }
282  newAddressing.resize(n);
283 
284  addressing_.transfer(newAddressing);
285 
286  updateSet();
287 }
288 
289 
291 (
292  Ostream& os,
293  const primitiveMesh& mesh,
294  const label maxLen
295 ) const
296 {
297  cellSet::writeDebug(os, mesh, maxLen);
298 }
299 
300 
301 // ************************************************************************* //
writeOption
Enumeration defining write preferences.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:815
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
Cell-face mesh analysis engine.
Definition: primitiveMesh.H:75
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: cellSet.C:229
Type & refCast(U &obj)
A dynamic_cast (for references). Generates a FatalError on failed casts and uses the virtual type() m...
Definition: typeInfo.H:159
void inplaceReorder(const labelUList &oldToNew, ListType &input, const bool prune=false)
Inplace reorder the elements of a list.
virtual bool set(const label id)
Set an index.
Definition: topoSet.C:497
virtual void updateMesh(const mapPolyMesh &morphMap)
Update any stored data for new labels.
Definition: cellZoneSet.C:260
Like cellSet but -reads data from cellZone -updates cellZone when writing.
Definition: cellZoneSet.H:47
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: cellZoneSet.C:167
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write cellZone using stream options.
Definition: cellZoneSet.C:223
A simple container for options an IOstream can normally have.
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign &#39;true&#39;).
Definition: BitOps.C:30
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
bool isReadOptional() const noexcept
True if (LAZY_READ) bits are set [same as READ_IF_PRESENT].
T & emplace_back(Args &&... args)
Construct and append an element to the end of the list, return reference to the new list element...
Definition: PtrListI.H:105
Macros for easy insertion into run-time selection tables.
virtual label maxSize(const polyMesh &mesh) const
Return max index+1.
Definition: cellZoneSet.C:216
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
label findZoneID(const word &zoneName) const
Find zone index by name, return -1 if not found.
Definition: ZoneMesh.C:629
A class for handling words, derived from Foam::string.
Definition: word.H:63
void updateSet()
Sort addressing and make cellSet part consistent with addressing.
Definition: cellZoneSet.C:40
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
virtual void subtractSet(const topoSet &set)
Subtract elements present in set.
Definition: cellZoneSet.C:186
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
virtual void invert(const label maxLen)
Invert contents.
Definition: cellZoneSet.C:118
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: cellZoneSet.C:284
bool isReadRequired() const noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
A subset of mesh cells.
Definition: cellZone.H:58
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
label nCells() const noexcept
Number of mesh cells.
A collection of cell labels.
Definition: cellSet.H:47
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
Definition: HashTable.C:712
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
label n
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
cellZoneSet(const polyMesh &mesh, const word &name, IOobjectOption::readOption rOpt=IOobjectOption::MUST_READ, IOobjectOption::writeOption wOpt=IOobjectOption::NO_WRITE)
Construct from objectRegistry and name.
Definition: cellZoneSet.C:54
virtual void sync(const polyMesh &mesh)
Sync cellSet across coupled patches; update cellZone from cellSet.
Definition: cellZoneSet.C:206
List< label > labelList
A List of labels.
Definition: List.H:62
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoSet.C:196
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: cellZoneSet.C:148
bool found
virtual void sync(const polyMesh &mesh)
Sync cellSet across coupled patches.
Definition: cellSet.H:188
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
void clearStorage()
Remove all entries from table and the table itself.
Definition: HashTable.C:750
readOption
Enumeration defining read preferences.