cellZoneSet.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "cellZoneSet.H"
27 #include "polyTopoChangeMap.H"
28 #include "polyMesh.H"
29 
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
40 
44 
45 
46 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47 
49 {
51  sortedOrder(addressing_, order);
52  inplaceReorder(order, addressing_);
53 
55  cellSet::resize(2*addressing_.size());
56  forAll(addressing_, i)
57  {
58  cellSet::insert(addressing_[i]);
59  }
60 }
61 
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
66 (
67  const polyMesh& mesh,
68  const word& name,
69  readOption r,
70  writeOption w
71 )
72 :
73  cellSet(mesh, name, 1000), // do not read cellSet
74  mesh_(mesh),
75  addressing_(0)
76 {
77  const cellZoneList& cellZones = mesh.cellZones();
78  label zoneID = cellZones.findIndex(name);
79 
80  if
81  (
82  (r == IOobject::MUST_READ)
84  || (r == IOobject::READ_IF_PRESENT && zoneID != -1)
85  )
86  {
87  const cellZone& fz = cellZones[zoneID];
88  addressing_ = fz;
89  }
90 
91  updateSet();
92 
93  check(mesh.nCells());
94 }
95 
96 
98 (
99  const polyMesh& mesh,
100  const word& name,
101  const label size,
102  writeOption w
103 )
104 :
105  cellSet(mesh, name, size, w),
106  mesh_(mesh),
107  addressing_(0)
108 {
109  updateSet();
110 }
111 
112 
114 (
115  const polyMesh& mesh,
116  const word& name,
117  const topoSet& set,
118  writeOption w
119 )
120 :
121  cellSet(mesh, name, set.size(), w),
122  mesh_(mesh),
123  addressing_(refCast<const cellZoneSet>(set).addressing())
124 {
125  updateSet();
126 }
127 
128 
129 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
130 
132 {}
133 
134 
135 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
136 
137 void cellZoneSet::invert(const label maxLen)
138 {
139  // Count
140  label n = 0;
141 
142  for (label celli = 0; celli < maxLen; celli++)
143  {
144  if (!found(celli))
145  {
146  n++;
147  }
148  }
149 
150  // Fill
151  addressing_.setSize(n);
152  n = 0;
153 
154  for (label celli = 0; celli < maxLen; celli++)
155  {
156  if (!found(celli))
157  {
158  addressing_[n] = celli;
159  n++;
160  }
161  }
162 
163  updateSet();
164 }
165 
166 
167 void cellZoneSet::subset(const topoSet& set)
168 {
169  DynamicList<label> newAddressing(addressing_.size());
170 
171  const cellZoneSet& fSet = refCast<const cellZoneSet>(set);
172 
173  forAll(fSet.addressing(), i)
174  {
175  label celli = fSet.addressing()[i];
176 
177  if (found(celli))
178  {
179  newAddressing.append(celli);
180  }
181  }
182 
183  addressing_.transfer(newAddressing);
184  updateSet();
185 }
186 
187 
188 void cellZoneSet::addSet(const topoSet& set)
189 {
190  DynamicList<label> newAddressing(addressing_);
191 
192  const cellZoneSet& fSet = refCast<const cellZoneSet>(set);
193 
194  forAll(fSet.addressing(), i)
195  {
196  label celli = fSet.addressing()[i];
197 
198  if (!found(celli))
199  {
200  newAddressing.append(celli);
201  }
202  }
203 
204  addressing_.transfer(newAddressing);
205  updateSet();
206 }
207 
208 
210 {
211  DynamicList<label> newAddressing(addressing_.size());
212 
213  const cellZoneSet& fSet = refCast<const cellZoneSet>(set);
214 
215  forAll(addressing_, i)
216  {
217  label celli = addressing_[i];
218 
219  if (!fSet.found(celli))
220  {
221  // Not found in fSet so add
222  newAddressing.append(celli);
223  }
224  }
225 
226  addressing_.transfer(newAddressing);
227  updateSet();
228 }
229 
230 
231 void cellZoneSet::sync(const polyMesh& mesh)
232 {
233  cellSet::sync(mesh);
234 
235  // Take over contents of cellSet into addressing.
236  addressing_ = sortedToc();
237  updateSet();
238 }
239 
240 
242 {
243  return mesh.nCells();
244 }
245 
246 
248 (
252  const bool write
253 ) const
254 {
255  // Write shadow cellSet
256  word oldTypeName = typeName;
257  const_cast<word&>(type()) = cellSet::typeName;
258  bool ok = cellSet::writeObject(s, v, c, write);
259  const_cast<word&>(type()) = oldTypeName;
260 
261  // Modify cellZone
262  cellZoneList& cellZones = const_cast<polyMesh&>(mesh_).cellZones();
263  label zoneID = cellZones.findIndex(name());
264 
265  if (zoneID == -1)
266  {
267  zoneID = cellZones.size();
268 
269  cellZones.setSize(zoneID+1);
270  cellZones.set
271  (
272  zoneID,
273  new cellZone
274  (
275  name(),
276  addressing_,
277  cellZones
278  )
279  );
280  }
281  else
282  {
283  cellZones[zoneID] = addressing_;
284  }
285 
286  return ok && cellZones.write(write);
287 }
288 
289 
291 {
292  // cellZone
293  labelList newAddressing(addressing_.size());
294 
295  label n = 0;
296  forAll(addressing_, i)
297  {
298  label celli = addressing_[i];
299  label newCelli = map.reverseCellMap()[celli];
300  if (newCelli >= 0)
301  {
302  newAddressing[n] = newCelli;
303  n++;
304  }
305  }
306  newAddressing.setSize(n);
307 
308  addressing_.transfer(newAddressing);
309 
310  updateSet();
311 }
312 
313 
315 (
316  Ostream& os,
317  const primitiveMesh& mesh,
318  const label maxLen
319 ) const
320 {
321  cellSet::writeDebug(os, mesh, maxLen);
322 }
323 
324 
325 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
326 
327 } // End namespace Foam
328 
329 // ************************************************************************* //
label n
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
bool insert(const label &key)
Insert a new entry.
Definition: HashSet.H:109
bool set(const label &key)
Same as insert (cannot overwrite nil content)
Definition: HashSet.H:123
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:242
void clearStorage()
Clear the table entries and the table itself.
Definition: HashTable.C:566
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:138
void resize(const label newSize)
Resize the hash table for efficiency.
Definition: HashTable.C:506
readOption
Enumeration defining the read options.
Definition: IOobject.H:117
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:119
const word & name() const
Return name.
Definition: IOobject.H:310
writeOption
Enumeration defining the write options.
Definition: IOobject.H:126
Version number type.
Definition: IOstream.H:97
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void setSize(const label)
Reset size of List.
Definition: List.C:281
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
autoPtr< T > set(const label, const word &key, T *)
Set element to pointer provided and return old element.
label findIndex(const word &key) const
Return the index of the given the key or -1 if not found.
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
A collection of cell labels.
Definition: cellSet.H:51
virtual void sync(const polyMesh &mesh)
Sync cellSet across coupled patches.
Definition: cellSet.H:140
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: cellSet.C:232
Like cellSet but -reads data from cellZone -updates cellZone when writing.
Definition: cellZoneSet.H:52
virtual label maxSize(const polyMesh &mesh) const
Return max index+1.
Definition: cellZoneSet.C:241
virtual void invert(const label maxLen)
Invert contents. (insert all members 0..maxLen-1 which were not in.
Definition: cellZoneSet.C:137
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write=true) const
Write cellZone.
Definition: cellZoneSet.C:248
virtual void deleteSet(const topoSet &set)
Delete elements present in set.
Definition: cellZoneSet.C:209
cellZoneSet(const polyMesh &mesh, const word &name, readOption r=MUST_READ, writeOption w=NO_WRITE)
Construct from objectRegistry and name.
Definition: cellZoneSet.C:66
virtual void sync(const polyMesh &mesh)
Sync cellSet across coupled patches; update cellZone from cellSet.
Definition: cellZoneSet.C:231
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: cellZoneSet.C:188
const labelList & addressing() const
Definition: cellZoneSet.H:103
virtual void writeDebug(Ostream &os, const primitiveMesh &, const label maxLen) const
Write maxLen items with label and coordinates.
Definition: cellZoneSet.C:315
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: cellZoneSet.C:167
virtual void topoChange(const polyTopoChangeMap &map)
Update any stored data for new labels.
Definition: cellZoneSet.C:290
virtual ~cellZoneSet()
Destructor.
Definition: cellZoneSet.C:131
void updateSet()
Sort addressing and make cellSet part consistent with addressing.
Definition: cellZoneSet.C:48
A subset of mesh cells.
Definition: cellZone.H:58
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const cellZoneList & cellZones() const
Return cell zones.
Definition: polyMesh.H:452
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
const labelList & reverseCellMap() const
Reverse cell map.
Cell-face mesh analysis engine.
Definition: primitiveMesh.H:75
label nCells() const
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
virtual bool write(const bool write=true) const
Write using setting from DB.
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:65
void check(const label maxLabel)
Check validity of contents.
A class for handling words, derived from string.
Definition: word.H:62
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
const dimensionedScalar c
Speed of light in a vacuum.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
addToRunTimeSelectionTable(polyPatch, mergedCyclicPolyPatch, word)
To & refCast(From &r)
Reference type cast template function.
Definition: typeInfo.H:129
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
int order(const scalar s)
defineTypeNameAndDebug(combustionModel, 0)
void sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
void inplaceReorder(const labelUList &oldToNew, ListType &)
Inplace reorder the elements of a list.