polyTopoChange.H
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 Class
25  Foam::polyTopoChange
26 
27 Description
28  Direct mesh changes based on v1.3 polyTopoChange syntax.
29 
30  Instead of recording changes and executing them all in one go (as did
31  v1.3 polyTopoChange) this class actually holds the current
32  points/faces/cells and does the change immediately.
33  It can be asked to compress out all unused points/faces/cells and
34  renumber everything to be consistent.
35 
36  Note:
37  - polyTopoChange can be copied.
38  - adding a face using non-existing cells causes all intermediate cells
39  to be added. So always first add cells/points and then faces.
40  (or set strict checking)
41  - strict checking:
42  - any added/modified face can only use already existing vertices
43  - any added face can only use already existing cells
44  - no item can be removed more than once.
45  - removed cell: cell set to 0 faces.
46  - removed face: face set to 0 vertices.
47  - removed point: coordinate set to vector::max (vGreat,vGreat,vGreat).
48  Note that this might give problems if this value is used already.
49  To see if point is equal to above value we don't use == (which might give
50  problems with roundoff error) but instead compare the individual component
51  with >.
52  - coupled patches: the reorderCoupledFaces routine (borrowed from
53  the couplePatches utility) reorders coupled patch faces and
54  uses the cyclicPolyPatch,processorPolyPatch functionality.
55 
56 SourceFiles
57  polyTopoChange.C
58  polyTopoChangeI.H
59  polyTopoChangeTemplates.C
60 
61 \*---------------------------------------------------------------------------*/
62 
63 #ifndef polyTopoChange_H
64 #define polyTopoChange_H
65 
66 #include "DynamicList.H"
67 #include "labelList.H"
68 #include "pointField.H"
69 #include "Map.H"
70 #include "HashSet.H"
71 #include "polyTopoChangeMap.H"
72 #include "PackedBoolList.H"
73 
74 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
75 
76 namespace Foam
77 {
78 
79 // Forward declaration of classes
80 class face;
81 class primitiveMesh;
82 class polyMesh;
83 class fvMesh;
84 class Time;
85 class fileName;
86 class polyBoundaryMesh;
87 class polyPatch;
88 class dictionary;
89 class topoAction;
90 class objectMap;
91 class IOobject;
92 template<class T> class CompactListList;
93 
94 /*---------------------------------------------------------------------------*\
95  Class polyTopoChange Declaration
96 \*---------------------------------------------------------------------------*/
97 
98 class polyTopoChange
99 {
100  // Private Data
101 
102  //- Whether to allow referencing illegal points/cells/faces
103  // when adding/removing data
104  bool strict_;
105 
106 
107  // Patches
108 
109  //- Number of patches
110  label nPatches_;
111 
112 
113  // Points
114 
115  //- Current point set
116  DynamicList<point> points_;
117 
118  //- Original point label (or masterpoint for added points)
119  DynamicList<label> pointMap_;
120 
121  //- For all original and added points contains new point label
122  // (used to map return value of addPoint to new mesh point)
123  DynamicList<label> reversePointMap_;
124 
125  //- Retired points
126  labelHashSet retiredPoints_;
127 
128  //- Explicitly provided old location for e.g. added points without
129  // masterPoint
130  Map<point> oldPoints_;
131 
132 
133  // Faces
134 
135  //- Current faceList
136  DynamicList<face> faces_;
137 
138  //- Patch for every external face (-1 for internal faces)
139  DynamicList<label> region_;
140 
141  //- Owner for all faces
142  DynamicList<label> faceOwner_;
143 
144  //- Neighbour for internal faces (-1 for external faces)
145  DynamicList<label> faceNeighbour_;
146 
147  //- Original face label. Or master face for added-from-faces;
148  // -1 for faces added-from-edge or added-from-point)
149  DynamicList<label> faceMap_;
150 
151  //- For all original and added faces contains new face label
152  // (used to map return value of addFace to new mesh face)
153  DynamicList<label> reverseFaceMap_;
154 
155  //- In mapping whether to reverse the flux
156  PackedBoolList flipFaceFlux_;
157 
158  //- Active faces
159  label nActiveFaces_;
160 
161 
162  // Cells
163 
164  //- Original cell label or master cell for added-from-cell;
165  // -1 for cells added from face or edge
166  DynamicList<label> cellMap_;
167 
168  //- For all original and added cells contains new cell label
169  // (used to map return value of addCell to new mesh cell)
170  DynamicList<label> reverseCellMap_;
171 
172 
173  // Private Member Functions
174 
175  //- Reorder contents of container according to map
176  template<class T>
177  static void reorder(const labelList& map, DynamicList<T>&);
178 
179  template<class T>
180  static void reorder(const labelList& map, List<DynamicList<T>>&);
181 
182  template<class T>
183  static void renumberKey(const labelList& map, Map<T>&);
184 
185  //- Renumber elements of container according to map
186  static void renumber(const labelList&, labelHashSet&);
187 
188  //- Special handling of reverse maps which have <-1 in them
189  static void renumberReverseMap(const labelList&, DynamicList<label>&);
190 
191  //- Renumber & compact elements of list according to map
192  static void renumberCompact(const labelList&, labelList&);
193 
194  //- Get all set elements as a labelHashSet
195  static labelHashSet getSetIndices(const PackedBoolList&);
196 
197  //- Count number of added and removed quantities from maps
198  static void countMap
199  (
200  const labelList& map,
201  const labelList& reverseMap,
202  label& nSplit,
203  label& nInserted,
204  label& nMerge,
205  label& nRemove
206  );
207 
208  //- Print some stats about mesh
209  static void writeMeshStats(const polyMesh& mesh, Ostream&);
210 
211  //- Calculate object maps. Requires reverseMap to have destination
212  // to be marked with < -1
213  static void getMergeSets
214  (
215  const labelList& reverseCellMap,
216  const labelList& cellMap,
217  List<objectMap>& cellsFromCells
218  );
219 
220  //- Are all face vertices valid
221  bool hasValidPoints(const face&) const;
222 
223  //- Return face points
224  pointField facePoints(const face& f) const;
225 
226  //- Check inputs to modifyFace or addFace
227  void checkFace
228  (
229  const face&,
230  const label facei,
231  const label own,
232  const label nei,
233  const label patchi
234  ) const;
235 
236  //- Construct cells (in packed storage)
237  void makeCells
238  (
239  const label nActiveFaces,
240  labelList& cellFaces,
241  labelList& cellFaceOffsets
242  ) const;
243 
244  //- Construct cellCells (in packed storage)
245  void makeCellCells
246  (
247  const label nActiveFaces,
248  CompactListList<label>& cellCells
249  ) const;
250 
251  //- Cell ordering (bandCompression). Returns number of remaining cells
252  label getCellOrder
253  (
254  const CompactListList<label>&,
255  labelList&
256  ) const;
257 
258  //- Do upper-triangular ordering and patch ordering
259  void getFaceOrder
260  (
261  const label nActiveFaces,
262  const labelList& cellFaces,
263  const labelList& cellFaceOffsets,
264 
265  labelList& oldToNew,
266  labelList& patchSizes,
267  labelList& patchStarts
268  ) const;
269 
270  //- Compact and reorder faces according to map
271  void reorderCompactFaces
272  (
273  const label newSize,
274  const labelList& oldToNew
275  );
276 
277  //- Remove all unused/removed points/faces/cells and update
278  // face ordering (always), cell ordering (bandcompression,
279  // orderCells=true),
280  // point ordering (sorted into internal and boundary points,
281  // orderPoints=true)
282  //
283  // Compact all and orders points and faces:
284  // - points into internal followed by external points
285  // - internalfaces upper-triangular
286  // - externalfaces after internal ones.
287  void compact
288  (
289  const bool orderCells,
290  const bool orderPoints,
291  label& nInternalPoints,
292  labelList& patchSizes,
293  labelList& patchStarts
294  );
295 
296  //- Select either internal or external faces out of faceLabels
297  //
298  // Find faces to interpolate to create value for new face. Internal
299  // faces should only be created from internal faces, external faces only
300  // from external faces (and ideally the same patch) Is bit problematic
301  // if there are no faces to select, i.e. in polyDualMesh an internal
302  // face can be created from a boundary edge with no internal faces
303  // connected to it.
304  static labelList selectFaces
305  (
306  const primitiveMesh&,
307  const labelList& faceLabels,
308  const bool internalFacesOnly
309  );
310 
311  //- Calculate mapping for patchpoints only
312  void calcPatchPointMap
313  (
314  const List<Map<label>>&,
315  const polyBoundaryMesh&,
317  ) const;
318 
319  //- Do all coupled patch face reordering
320  void reorderCoupledFaces
321  (
322  const bool syncParallel,
323  const polyBoundaryMesh&,
324  const labelList& patchStarts,
325  const labelList& patchSizes,
326  const pointField& points
327  );
328 
329  void compactAndReorder
330  (
331  const polyMesh&,
332  const bool syncParallel,
333  const bool orderCells,
334  const bool orderPoints,
335  label& nInternalPoints,
336  pointField& newPoints,
337  labelList& patchSizes,
338  labelList& patchStarts,
339  List<objectMap>& pointsFromPoints,
340  List<objectMap>& facesFromFaces,
341  List<objectMap>& cellsFromCells,
342  List<Map<label>>& oldPatchMeshPointMaps,
343  labelList& oldPatchNMeshPoints,
344  labelList& oldPatchSizes,
345  labelList& oldPatchStarts
346  );
347 
348 
349 public:
350 
351  //- Runtime type information
352  ClassName("polyTopoChange");
353 
354 
355  // Constructors
356 
357  //- Construct without mesh. Either specify nPatches or use
358  // setNumPatches before trying to make a mesh (makeMesh, changeMesh)
359  polyTopoChange(const label nPatches, const bool strict = true);
360 
361  //- Construct from mesh. Adds all points/face/cells from mesh
362  polyTopoChange(const polyMesh& mesh, const bool strict = true);
363 
364 
365  // Member Functions
366 
367  // Access
368 
369  //- Points. Shrunk after constructing mesh (or calling of compact())
370  const DynamicList<point>& points() const
371  {
372  return points_;
373  }
374 
375  const DynamicList<face>& faces() const
376  {
377  return faces_;
378  }
379 
380  const DynamicList<label>& region() const
381  {
382  return region_;
383  }
384 
385  const DynamicList<label>& faceOwner() const
386  {
387  return faceOwner_;
388  }
389 
390  const DynamicList<label>& faceNeighbour() const
391  {
392  return faceNeighbour_;
393  }
394 
395  //- Is point removed?
396  inline bool pointRemoved(const label pointi) const;
397 
398  //- Is face removed?
399  inline bool faceRemoved(const label facei) const;
400 
401  //- Is cell removed?
402  inline bool cellRemoved(const label celli) const;
403 
404 
405  // Edit
406 
407  //- Clear all storage
408  void clear();
409 
410  //- Explicitly pre-size the dynamic storage for expected mesh
411  // size for if construct-without-mesh
412  void setCapacity
413  (
414  const label nPoints,
415  const label nFaces,
416  const label nCells
417  );
418 
419  //- Add point and return new point index
420  // Notes:
421  // - masterPointID can be < 0 (appended points)
422  // - inCell = false: add retired point (to end of point list)
424  (
425  const point&,
426  const label masterPointID,
427  const bool inCell
428  );
429 
430  //- Modify coordinate
431  // Notes:
432  // - inCell = false: add retired point (to end of point list)
433  void modifyPoint
434  (
435  const label,
436  const point&,
437  const bool inCell
438  );
439 
440  //- Remove point / merge points
441  void removePoint(const label, const label);
442 
443  //- Add face to cells and return new face index
444  // own, nei <0 >=0 : add inactive face (to end of face list)
445  label addFace
446  (
447  const face& f,
448  const label own,
449  const label nei,
450  const label masterFaceID,
451  const bool flipFaceFlux,
452  const label patchID
453  );
454 
455  //- Modify vertices or cell of face
456  void modifyFace
457  (
458  const face& f,
459  const label facei,
460  const label own,
461  const label nei,
462  const bool flipFaceFlux,
463  const label patchID
464  );
465 
466  //- Remove face / merge faces
467  void removeFace(const label, const label);
468 
469  //- Add cell and return new cell index
470  label addCell(const label masterCellID);
471 
472  //- Remove cell / merge cells
473  void removeCell(const label, const label);
474 
475  //- Explicitly set the number of patches if construct-without-mesh
476  // used
477  inline void setNumPatches(const label nPatches);
478 
479 
480  // Other
481 
482  //- Inplace changes mesh without change of patches.
483  // Adapts patch start/end and by default does parallel matching.
484  // Clears all data. Returns map.
485  // orderCells : whether to order the cells (see bandCompression.H)
486  // orderPoints : whether to order the points into internal first
487  // followed by boundary points. This is not fully consistent
488  // with upper-triangular ordering of points and edges so
489  // is only done when explicitly asked for.
491  (
492  polyMesh& mesh,
493  const bool syncParallel = true,
494  const bool orderCells = false,
495  const bool orderPoints = false
496  );
497 
498  //- Create new mesh with old mesh patches
500  (
501  autoPtr<fvMesh>& newMesh,
502  const IOobject& io,
503  const polyMesh& mesh,
504  const bool syncParallel = true,
505  const bool orderCells = false,
506  const bool orderPoints = false
507  );
508 };
509 
510 
511 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
512 
513 } // End namespace Foam
514 
515 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
516 
517 #include "polyTopoChangeI.H"
518 
519 #ifdef NoRepository
520  #include "polyTopoChangeTemplates.C"
521 #endif
522 
523 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
524 
525 #endif
526 
527 // ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
A HashTable to objects of type <T> with a label key.
Definition: Map.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A bit-packed bool list.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:76
Foam::polyBoundaryMesh.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
Direct mesh changes based on v1.3 polyTopoChange syntax.
void modifyPoint(const label, const point &, const bool inCell)
Modify coordinate.
void removePoint(const label, const label)
Remove point / merge points.
autoPtr< polyTopoChangeMap > makeMesh(autoPtr< fvMesh > &newMesh, const IOobject &io, const polyMesh &mesh, const bool syncParallel=true, const bool orderCells=false, const bool orderPoints=false)
Create new mesh with old mesh patches.
bool cellRemoved(const label celli) const
Is cell removed?
polyTopoChange(const label nPatches, const bool strict=true)
Construct without mesh. Either specify nPatches or use.
bool pointRemoved(const label pointi) const
Is point removed?
const DynamicList< face > & faces() const
const DynamicList< point > & points() const
Points. Shrunk after constructing mesh (or calling of compact())
void removeFace(const label, const label)
Remove face / merge faces.
void setNumPatches(const label nPatches)
Explicitly set the number of patches if construct-without-mesh.
void setCapacity(const label nPoints, const label nFaces, const label nCells)
Explicitly pre-size the dynamic storage for expected mesh.
label addCell(const label masterCellID)
Add cell and return new cell index.
bool faceRemoved(const label facei) const
Is face removed?
const DynamicList< label > & faceOwner() const
autoPtr< polyTopoChangeMap > changeMesh(polyMesh &mesh, const bool syncParallel=true, const bool orderCells=false, const bool orderPoints=false)
Inplace changes mesh without change of patches.
ClassName("polyTopoChange")
Runtime type information.
const DynamicList< label > & faceNeighbour() const
label addPoint(const point &, const label masterPointID, const bool inCell)
Add point and return new point index.
void clear()
Clear all storage.
void removeCell(const label, const label)
Remove cell / merge cells.
const DynamicList< label > & region() const
void modifyFace(const face &f, const label facei, const label own, const label nei, const bool flipFaceFlux, const label patchID)
Modify vertices or cell of face.
label addFace(const face &f, const label own, const label nei, const label masterFaceID, const bool flipFaceFlux, const label patchID)
Add face to cells and return new face index.
Cell-face mesh analysis engine.
Definition: primitiveMesh.H:75
label patchi
label nPoints
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
ListType reorder(const label size, const typename ListType::value_type &defaultValue, const labelUList &oldToNew, const ListType &lst)
labelList f(nPoints)
label nPatches
Definition: readKivaGrid.H:396