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-2025 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 
39 defineTypeNameAndDebug(cellZoneSet, 0);
40 
41 addToRunTimeSelectionTable(topoSet, cellZoneSet, word);
42 addToRunTimeSelectionTable(topoSet, cellZoneSet, size);
43 addToRunTimeSelectionTable(topoSet, cellZoneSet, set);
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)
83  || (r == IOobject::MUST_READ_IF_MODIFIED)
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 
97 cellZoneSet::cellZoneSet
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 
113 cellZoneSet::cellZoneSet
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 
131 cellZoneSet::~cellZoneSet()
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 
209 void cellZoneSet::deleteSet(const topoSet& set)
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 
241 label cellZoneSet::maxSize(const polyMesh& mesh) const
242 {
243  return mesh.nCells();
244 }
245 
246 
247 bool cellZoneSet::writeObject
248 (
249  IOstream::streamFormat s,
250  IOstream::versionNumber v,
251  IOstream::compressionType c,
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 
290 void cellZoneSet::topoChange(const polyTopoChangeMap& map)
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 
314 void cellZoneSet::writeDebug
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 // ************************************************************************* //
bool found
label n
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Macros for easy insertion into run-time selection tables.
bool insert(const label &key)
Insert a new entry.
Definition: HashSet.H:109
void clearStorage()
Clear the table entries and the table itself.
Definition: HashTable.C:566
void resize(const label newSize)
Resize the hash table for efficiency.
Definition: HashTable.C:506
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
cellZoneSet(const polyMesh &mesh, const word &name, readOption r=MUST_READ, writeOption w=NO_WRITE)
Construct from objectRegistry and name.
void updateSet()
Sort addressing and make cellSet part consistent with addressing.
const cellZoneList & cellZones() const
Return cell zones.
Definition: polyMesh.H:438
label nCells() const
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::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.
List< label > labelList
A List of labels.
Definition: labelList.H:56
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:141
labelList invert(const label len, const labelUList &)
Invert one-to-one map. Unmapped elements will be -1.
Definition: ListOps.C:37
String typeName(const std::type_info &info)
Return the un-mangled name given the standard type info.
int order(const scalar s)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
void sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)
ListType subset(const UList< T > &select, const T &value, const ListType &)
Extract elements of List when select is a certain value.
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.