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-2024 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  defineTypeName(cellZoneSet);
40  addToRunTimeSelectionTable(topoSet, cellZoneSet, word);
43 }
44 
45 
46 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47 
49 {
50  Foam::sort(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,
64  const label initialCapacity,
66 )
67 :
68  cellSet(mesh, name, initialCapacity, wOpt), // Construct no-read
69  mesh_(mesh)
70 {}
71 
72 
74 (
75  const polyMesh& mesh,
76  const word& name,
79 )
80 :
81  cellZoneSet(mesh, name, label(0), wOpt) // Construct no-read
82 {
83  const auto& zones = mesh.cellZones();
84  const auto* zonePtr = zones.cfindZone(name);
85 
86  if (!zonePtr)
87  {
89  {
91  << "Zone named " << name << " not found. "
92  << "List of available zone names: " << zones.names() << nl
93  << exit(FatalError);
94  }
95  }
96  else if (IOobjectOption::isAnyRead(rOpt))
97  {
98  const auto& zn = *zonePtr;
99  addressing_ = zn;
100  }
102  updateSet();
103  check(mesh.nCells());
104 }
105 
106 
108 (
109  const polyMesh& mesh,
110  const word& name,
111  const topoSet& set,
113 )
114 :
115  cellZoneSet(mesh, name, label(0), wOpt) // Construct no-read
116 {
117  const auto* zonePtr = isA<cellZoneSet>(set);
118 
119  if (zonePtr)
120  {
121  addressing_ = zonePtr->addressing();
122  }
123  else
124  {
125  addressing_ = set.sortedToc();
126  }
128  updateSet();
129 }
130 
131 
132 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 
134 void Foam::cellZoneSet::invert(const label maxLen)
135 {
136  // Count
137  label n = 0;
138 
139  for (label celli = 0; celli < maxLen; ++celli)
140  {
141  if (!found(celli))
142  {
143  ++n;
144  }
145  }
146 
147  // Fill
148  addressing_.resize_nocopy(n);
149  n = 0;
150 
151  for (label celli = 0; celli < maxLen; ++celli)
152  {
153  if (!found(celli))
154  {
155  addressing_[n] = celli;
156  ++n;
157  }
158  }
159 
160  updateSet();
161 }
162 
163 
164 void Foam::cellZoneSet::subset(const labelUList& elems)
165 {
166  DynamicList<label> newAddressing(addressing_.size());
167 
168  for (const label id : elems)
169  {
170  if (found(id))
171  {
172  newAddressing.push_back(id);
173  }
174  }
175 
176  addressing_.transfer(newAddressing);
177  updateSet();
178 }
179 
180 
181 void Foam::cellZoneSet::subset(const topoSet& set)
182 {
183  const auto* zonePtr = isA<cellZoneSet>(set);
184 
185  if (zonePtr)
186  {
187  // Is a cellZoneSet
188  this->subset(zonePtr->addressing());
189  }
190  else
191  {
192  // Assume a cellSet
193  this->subset(refCast<const cellSet>(set).sortedToc());
194  }
195 }
196 
197 
198 void Foam::cellZoneSet::addSet(const labelUList& elems)
199 {
200  DynamicList<label> newAddressing(addressing_);
201 
202  for (const label id : elems)
203  {
204  if (!found(id))
205  {
206  newAddressing.push_back(id);
207  }
208  }
209 
210  addressing_.transfer(newAddressing);
211  updateSet();
212 }
213 
214 
215 void Foam::cellZoneSet::addSet(const topoSet& set)
216 {
217  const auto* zonePtr = isA<cellZoneSet>(set);
218 
219  if (zonePtr)
220  {
221  // Is a cellZoneSet
222  this->addSet(zonePtr->addressing());
223  }
224  else
225  {
226  // Assume a cellSet
227  this->addSet(refCast<const cellSet>(set).sortedToc());
228  }
229 }
230 
231 
233 {
234  DynamicList<label> newAddressing(addressing_.size());
235 
236  const labelHashSet set(elems);
237 
238  for (const label id : addressing_)
239  {
240  if (!set.found(id))
241  {
242  // Retain if not in the topoSet (parameter)
243  newAddressing.push_back(id);
244  }
245  }
246 
247  addressing_.transfer(newAddressing);
248  updateSet();
249 }
250 
251 
252 void Foam::cellZoneSet::subtractSet(const topoSet& set)
253 {
254  DynamicList<label> newAddressing(addressing_.size());
255 
256  for (const label id : addressing_)
257  {
258  if (!set.found(id))
259  {
260  // Not found in zoneSet so add
261  newAddressing.push_back(id);
262  }
263  }
264 
265  addressing_.transfer(newAddressing);
266  updateSet();
267 }
268 
269 
270 void Foam::cellZoneSet::sync(const polyMesh& mesh)
271 {
274  // Take over contents of cellSet into addressing.
275  addressing_ = sortedToc();
276  updateSet();
277 }
278 
279 
280 Foam::label Foam::cellZoneSet::maxSize(const polyMesh& mesh) const
281 {
282  return mesh.nCells();
283 }
284 
285 
287 (
288  IOstreamOption streamOpt,
289  const bool writeOnProc
290 ) const
291 {
292  // Write shadow cellSet
293  const word oldTypeName = typeName;
294  const_cast<word&>(type()) = cellSet::typeName;
295  bool ok = cellSet::writeObject(streamOpt, writeOnProc);
296  const_cast<word&>(type()) = oldTypeName;
297 
298  // Modify cellZone
299  auto& zones = const_cast<polyMesh&>(mesh_).cellZones();
300  auto* zonePtr = zones.findZone(name());
301 
302  if (zonePtr)
303  {
304  zonePtr->resetAddressing(addressing_);
305  }
306  else
307  {
308  zones.emplace_back
309  (
310  name(),
311  addressing_,
312  zones.size(), // zoneID
313  zones
314  );
315  }
316  zones.clearAddressing();
317 
318  return ok && zones.write(writeOnProc);
319 }
320 
321 
322 void Foam::cellZoneSet::updateMesh(const mapPolyMesh& morphMap)
323 {
324  DynamicList<label> newAddressing(addressing_.size());
325 
326  for (const label celli : addressing_)
327  {
328  label newCelli = morphMap.reverseCellMap()[celli];
329  if (newCelli >= 0)
330  {
331  newAddressing.push_back(newCelli);
332  }
333  }
335  addressing_.transfer(newAddressing);
336  updateSet();
337 }
338 
339 
341 (
342  Ostream& os,
343  const primitiveMesh& mesh,
344  const label maxLen
345 ) const
346 {
347  cellSet::writeDebug(os, mesh, maxLen);
348 }
349 
350 
351 // ************************************************************************* //
writeOption
Enumeration defining write preferences.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void clearAddressing(const bool isMeshUpdate=false)
Clear addressing.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
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:256
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
virtual bool set(const label id)
Set an index.
Definition: topoSet.C:545
virtual void updateMesh(const mapPolyMesh &morphMap)
Update any stored data for new labels.
Definition: cellZoneSet.C:315
Like cellSet but -reads data from cellZone -updates cellZone when writing.
Definition: cellZoneSet.H:47
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write cellZone using stream options.
Definition: cellZoneSet.C:280
A simple container for options an IOstream can normally have.
bool isAnyRead() const noexcept
True if any reading may be required (ie, != NO_READ)
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
virtual void subtractSet(const labelUList &elems)
Subtract given elements from the set.
Definition: cellZoneSet.C:225
Macros for easy insertion into run-time selection tables.
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition: HashSet.H:85
defineTypeName(manifoldCellsMeshObject)
virtual label maxSize(const polyMesh &mesh) const
Return max index+1.
Definition: cellZoneSet.C:273
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
A class for handling words, derived from Foam::string.
Definition: word.H:63
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
void updateSet()
Sort addressing and make cellSet part consistent with addressing.
Definition: cellZoneSet.C:41
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:127
OBJstream os(runTime.globalPath()/outputName)
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: cellZoneSet.C:334
bool isReadRequired() const noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
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:736
void transfer(HashTable< T, Key, Hash > &rhs)
Transfer contents into this table.
Definition: HashTable.C:801
virtual void addSet(const labelUList &elems)
Add given elements to the set.
Definition: cellZoneSet.C:191
virtual void subset(const labelUList &elems)
Subset contents. Only elements present in both sets remain.
Definition: cellZoneSet.C:157
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:75
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:67
virtual void sync(const polyMesh &mesh)
Sync cellSet across coupled patches; update cellZone from cellSet.
Definition: cellZoneSet.C:263
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoSet.C:252
bool found
virtual void sync(const polyMesh &mesh)
Sync cellSet across coupled patches.
Definition: cellSet.H:227
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
void clearStorage()
Remove all entries from table and the table itself.
Definition: HashTable.C:774
readOption
Enumeration defining read preferences.