pointZoneSet.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 "pointZoneSet.H"
30 #include "mapPolyMesh.H"
31 #include "polyMesh.H"
32 #include "processorPolyPatch.H"
33 #include "cyclicPolyPatch.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40  defineTypeName(pointZoneSet);
41  addToRunTimeSelectionTable(topoSet, pointZoneSet, word);
44 }
45 
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
50 {
51  Foam::sort(addressing_);
52 
54  pointSet::reserve(addressing_.size());
55  pointSet::set(addressing_);
56 }
57 
58 
59 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 
62 (
63  const polyMesh& mesh,
64  const word& name,
65  const label initialCapacity,
67 )
68 :
69  pointSet(mesh, name, initialCapacity, wOpt), // Construct no-read
70  mesh_(mesh)
71 {}
72 
73 
75 (
76  const polyMesh& mesh,
77  const word& name,
80 )
81 :
82  pointZoneSet(mesh, name, label(0), wOpt) // Construct no-read
83 {
84  const auto& zones = mesh.pointZones();
85  const auto* zonePtr = zones.cfindZone(name);
86 
87  if (!zonePtr)
88  {
90  {
92  << "Zone named " << name << " not found. "
93  << "List of available zone names: " << zones.names() << nl
94  << exit(FatalError);
95  }
96  }
97  else if (IOobjectOption::isAnyRead(rOpt))
98  {
99  const auto& zn = *zonePtr;
100  addressing_ = zn;
101  }
103  updateSet();
104  check(mesh.nPoints());
105 }
106 
107 
109 (
110  const polyMesh& mesh,
111  const word& name,
112  const topoSet& set,
114 )
115 :
116  pointZoneSet(mesh, name, label(0), wOpt) // Construct no-read
117 {
118  const auto* zonePtr = isA<pointZoneSet>(set);
119 
120  if (zonePtr)
121  {
122  addressing_ = zonePtr->addressing();
123  }
124  else
125  {
126  addressing_ = set.sortedToc();
127  }
129  updateSet();
130 }
131 
132 
133 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
134 
135 void Foam::pointZoneSet::invert(const label maxLen)
136 {
137  // Count
138  label n = 0;
139 
140  for (label id = 0; id < maxLen; ++id)
141  {
142  if (!found(id))
143  {
144  ++n;
145  }
146  }
147 
148  // Fill
149  addressing_.resize_nocopy(n);
150  n = 0;
151 
152  for (label id = 0; id < maxLen; ++id)
153  {
154  if (!found(id))
155  {
156  addressing_[n] = id;
157  ++n;
158  }
159  }
160  updateSet();
161 }
162 
163 
164 void Foam::pointZoneSet::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::pointZoneSet::subset(const topoSet& set)
182 {
183  const auto* zonePtr = isA<pointZoneSet>(set);
184 
185  if (zonePtr)
186  {
187  // Is a pointZoneSet
188  this->subset(zonePtr->addressing());
189  }
190  else
191  {
192  // Assume a pointSet
193  this->subset(refCast<const pointSet>(set).sortedToc());
194  }
195 }
196 
197 
198 void Foam::pointZoneSet::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::pointZoneSet::addSet(const topoSet& set)
216 {
217  const auto* zonePtr = isA<pointZoneSet>(set);
218 
219  if (zonePtr)
220  {
221  // Is a pointZoneSet
222  this->addSet(zonePtr->addressing());
223  }
224  else
225  {
226  // Assume a pointSet
227  this->addSet(refCast<const pointSet>(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::pointZoneSet::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  // Retain if not in the topoSet (parameter)
261  newAddressing.push_back(id);
262  }
263  }
264 
265  addressing_.transfer(newAddressing);
266  updateSet();
267 }
268 
269 
270 void Foam::pointZoneSet::sync(const polyMesh& mesh)
271 {
274  // Take over contents of pointSet into addressing.
275  addressing_ = sortedToc();
276  updateSet();
277 }
278 
279 
280 Foam::label Foam::pointZoneSet::maxSize(const polyMesh& mesh) const
281 {
282  return mesh.nPoints();
283 }
284 
285 
287 (
288  IOstreamOption streamOpt,
289  const bool writeOnProc
290 ) const
291 {
292  // Write shadow pointSet
293  const word oldTypeName = typeName;
294  const_cast<word&>(type()) = pointSet::typeName;
295  bool ok = pointSet::writeObject(streamOpt, writeOnProc);
296  const_cast<word&>(type()) = oldTypeName;
297 
298  // Modify pointZone
299  auto& zones = const_cast<polyMesh&>(mesh_).pointZones();
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::pointZoneSet::updateMesh(const mapPolyMesh& morphMap)
323 {
324  DynamicList<label> newAddressing(addressing_.size());
325 
326  for (const label pointi : addressing_)
327  {
328  const label newPointi = morphMap.reversePointMap()[pointi];
329  if (newPointi >= 0)
330  {
331  newAddressing.push_back(newPointi);
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  pointSet::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.
A set of point labels.
Definition: pointSet.H:47
label nPoints() const noexcept
Number of mesh points.
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
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
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.
void updateSet()
Sort addressing and make pointSet part consistent with addressing.
Definition: pointZoneSet.C:42
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: pointZoneSet.C:334
Macros for easy insertion into run-time selection tables.
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
virtual void addSet(const labelUList &elems)
Add given elements to the set.
Definition: pointZoneSet.C:191
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition: HashSet.H:85
defineTypeName(manifoldCellsMeshObject)
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
virtual label maxSize(const polyMesh &mesh) const
Return max index+1.
Definition: pointZoneSet.C:273
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
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
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write pointZone using stream options.
Definition: pointZoneSet.C:280
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 newPointi
Definition: readKivaGrid.H:496
virtual void updateMesh(const mapPolyMesh &morphMap)
Update any stored data for new labels.
Definition: pointZoneSet.C:315
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 subtractSet(const labelUList &elems)
Subtract given elements from the set.
Definition: pointZoneSet.C:225
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
virtual void invert(const label maxLen)
Invert contents.
Definition: pointZoneSet.C:128
label n
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
virtual void sync(const polyMesh &mesh)
Sync set across coupled patches. Adds coupled points to set.
Definition: pointSet.C:153
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: pointSet.C:240
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoSet.C:252
virtual void subset(const labelUList &elems)
Subset contents. Only elements present in both sets remain.
Definition: pointZoneSet.C:157
bool found
virtual void sync(const polyMesh &mesh)
Sync pointZoneSet across coupled patches.
Definition: pointZoneSet.C:263
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
void clearStorage()
Remove all entries from table and the table itself.
Definition: HashTable.C:774
Like pointSet but -reads data from pointZone -updates pointZone when writing.
Definition: pointZoneSet.H:48
pointZoneSet(const polyMesh &mesh, const word &name, IOobjectOption::readOption rOpt=IOobjectOption::MUST_READ, IOobjectOption::writeOption wOpt=IOobjectOption::NO_WRITE)
Construct from objectRegistry and name.
Definition: pointZoneSet.C:68
readOption
Enumeration defining read preferences.