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-2023 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  //- Zone of point
126  Map<label> pointZone_;
127 
128  //- Retired points
129  labelHashSet retiredPoints_;
130 
131  //- Explicitly provided old location for e.g. added points without
132  // masterPoint
133  Map<point> oldPoints_;
134 
135 
136  // Faces
137 
138  //- Current faceList
139  DynamicList<face> faces_;
140 
141  //- Patch for every external face (-1 for internal faces)
142  DynamicList<label> region_;
143 
144  //- Owner for all faces
145  DynamicList<label> faceOwner_;
146 
147  //- Neighbour for internal faces (-1 for external faces)
148  DynamicList<label> faceNeighbour_;
149 
150  //- Original face label. Or master face for added-from-faces;
151  // -1 for faces added-from-edge or added-from-point)
152  DynamicList<label> faceMap_;
153 
154  //- For all original and added faces contains new face label
155  // (used to map return value of addFace to new mesh face)
156  DynamicList<label> reverseFaceMap_;
157 
158  //- Faces added from point (corresponding faceMap_ will
159  // be -1)
160  Map<label> faceFromPoint_;
161 
162  //- Faces added from edge (corresponding faceMap_ will
163  // be -1)
164  Map<label> faceFromEdge_;
165 
166  //- In mapping whether to reverse the flux.
167  PackedBoolList flipFaceFlux_;
168 
169  //- Zone of face
170  Map<label> faceZone_;
171 
172  //- Orientation of face in zone
173  PackedBoolList faceZoneFlip_;
174 
175  //- Active faces
176  label nActiveFaces_;
177 
178 
179  // Cells
180 
181  //- Original cell label or master cell for added-from-cell;
182  // -1 for cells added from face or edge.
183  DynamicList<label> cellMap_;
184 
185  //- For all original and added cells contains new cell label
186  // (used to map return value of addCell to new mesh cell)
187  DynamicList<label> reverseCellMap_;
188 
189  //- Cells added from point
190  Map<label> cellFromPoint_;
191 
192  //- Cells added from edge
193  Map<label> cellFromEdge_;
194 
195  //- Cells added from face
196  Map<label> cellFromFace_;
197 
198  //- Zone of cell
199  DynamicList<label> cellZone_;
200 
201 
202  // Private Member Functions
203 
204  //- Reorder contents of container according to map
205  template<class T>
206  static void reorder(const labelList& map, DynamicList<T>&);
207  template<class T>
208  static void reorder(const labelList& map, List<DynamicList<T>>&);
209  template<class T>
210  static void renumberKey(const labelList& map, Map<T>&);
211 
212  //- Renumber elements of container according to map
213  static void renumber(const labelList&, labelHashSet&);
214  //- Special handling of reverse maps which have <-1 in them
215  static void renumberReverseMap(const labelList&, DynamicList<label>&);
216 
217  //- Renumber & compact elements of list according to map
218  static void renumberCompact(const labelList&, labelList&);
219 
220  //- Get all set elements as a labelHashSet
221  static labelHashSet getSetIndices(const PackedBoolList&);
222 
223  //- Count number of added and removed quantities from maps.
224  static void countMap
225  (
226  const labelList& map,
227  const labelList& reverseMap,
228  label& nAdd,
229  label& nInflate,
230  label& nMerge,
231  label& nRemove
232  );
233 
234  //- Print some stats about mesh
235  static void writeMeshStats(const polyMesh& mesh, Ostream&);
236 
237  //- Calculate object maps. Requires reverseMap to have destination
238  // to be marked with <-1.
239  static void getMergeSets
240  (
241  const labelList& reverseCellMap,
242  const labelList& cellMap,
243  List<objectMap>& cellsFromCells
244  );
245 
246  //- Are all face vertices valid
247  bool hasValidPoints(const face&) const;
248 
249  //- Return face points
250  pointField facePoints(const face& f) const;
251 
252  //- Check inputs to modFace or addFace
253  void checkFace
254  (
255  const face&,
256  const label facei,
257  const label own,
258  const label nei,
259  const label patchi,
260  const label zoneI
261  ) const;
262 
263  //- Construct cells (in packed storage)
264  void makeCells
265  (
266  const label nActiveFaces,
267  labelList& cellFaces,
268  labelList& cellFaceOffsets
269  ) const;
270 
271  //- Construct cellCells (in packed storage)
272  void makeCellCells
273  (
274  const label nActiveFaces,
275  CompactListList<label>& cellCells
276  ) const;
277 
278  //- Cell ordering (bandCompression). Returns number of remaining cells.
279  label getCellOrder
280  (
281  const CompactListList<label>&,
282  labelList&
283  ) const;
284 
285  //- Do upper-triangular ordering and patch ordering.
286  void getFaceOrder
287  (
288  const label nActiveFaces,
289  const labelList& cellFaces,
290  const labelList& cellFaceOffsets,
291 
292  labelList& oldToNew,
293  labelList& patchSizes,
294  labelList& patchStarts
295  ) const;
296 
297  //- Compact and reorder faces according to map
298  void reorderCompactFaces
299  (
300  const label newSize,
301  const labelList& oldToNew
302  );
303 
304  //- Remove all unused/removed points/faces/cells and update
305  // face ordering (always), cell ordering (bandcompression,
306  // orderCells=true),
307  // point ordering (sorted into internal and boundary points,
308  // orderPoints=true)
309  //
310  // Compact all and orders points and faces:
311  // - points into internal followed by external points
312  // - internalfaces upper-triangular
313  // - externalfaces after internal ones.
314  void compact
315  (
316  const bool orderCells,
317  const bool orderPoints,
318  label& nInternalPoints,
319  labelList& patchSizes,
320  labelList& patchStarts
321  );
322 
323  //- Select either internal or external faces out of faceLabels
324  //
325  // Find faces to interpolate to create value for new face. Only used if
326  // face was inflated from edge or point. Internal faces should only be
327  // created from internal faces, external faces only from external faces
328  // (and ideally the same patch)
329  // Is bit problematic if there are no faces to select, i.e. in
330  // polyDualMesh an internal face can be created from a boundary edge
331  // with no internal faces connected to it.
332  static labelList selectFaces
333  (
334  const primitiveMesh&,
335  const labelList& faceLabels,
336  const bool internalFacesOnly
337  );
338 
339  //- Calculate mapping for patchpoints only
340  void calcPatchPointMap
341  (
342  const List<Map<label>>&,
343  const polyBoundaryMesh&,
345  ) const;
346 
347  void calcFaceInflationMaps
348  (
349  const polyMesh&,
353  ) const;
354 
355  void calcCellInflationMaps
356  (
357  const polyMesh&,
362  ) const;
363 
364  void resetZones
365  (
366  const polyMesh&, // mesh to get existing info from
367  polyMesh&, // mesh to change zones on
368  labelListList&,
369  labelListList&,
371  ) const;
372 
373  void calcFaceZonePointMap
374  (
375  const polyMesh&,
376  const List<Map<label>>&,
378  ) const;
379 
380 
381  // Coupling
382 
383  //- Do all coupled patch face reordering
384  void reorderCoupledFaces
385  (
386  const bool syncParallel,
387  const polyBoundaryMesh&,
388  const labelList& patchStarts,
389  const labelList& patchSizes,
390  const pointField& points
391  );
392 
393  void compactAndReorder
394  (
395  const polyMesh&,
396  const bool syncParallel,
397  const bool orderCells,
398  const bool orderPoints,
399  label& nInternalPoints,
400  pointField& newPoints,
401  labelList& patchSizes,
402  labelList& patchStarts,
403  List<objectMap>& pointsFromPoints,
404  List<objectMap>& facesFromPoints,
405  List<objectMap>& facesFromEdges,
406  List<objectMap>& facesFromFaces,
407  List<objectMap>& cellsFromPoints,
408  List<objectMap>& cellsFromEdges,
409  List<objectMap>& cellsFromFaces,
410  List<objectMap>& cellsFromCells,
411  List<Map<label>>& oldPatchMeshPointMaps,
412  labelList& oldPatchNMeshPoints,
413  labelList& oldPatchStarts,
414  List<Map<label>>& oldMeshFaceZonesPointMaps
415  );
416 
417 public:
418 
419  //- Runtime type information
420  ClassName("polyTopoChange");
421 
422 
423 
424  // Constructors
425 
426  //- Construct without mesh. Either specify nPatches or use
427  // setNumPatches before trying to make a mesh (makeMesh, changeMesh)
428  polyTopoChange(const label nPatches, const bool strict = true);
429 
430  //- Construct from mesh. Adds all points/face/cells from mesh.
431  polyTopoChange(const polyMesh& mesh, const bool strict = true);
432 
433 
434  // Member Functions
435 
436  // Access
437 
438  //- Points. Shrunk after constructing mesh (or calling of compact())
439  const DynamicList<point>& points() const
440  {
441  return points_;
442  }
443 
444  const DynamicList<face>& faces() const
445  {
446  return faces_;
447  }
448 
449  const DynamicList<label>& region() const
450  {
451  return region_;
452  }
453 
454  const DynamicList<label>& faceOwner() const
455  {
456  return faceOwner_;
457  }
458 
459  const DynamicList<label>& faceNeighbour() const
460  {
461  return faceNeighbour_;
462  }
463 
464  //- Is point removed?
465  inline bool pointRemoved(const label pointi) const;
466 
467  //- Is face removed?
468  inline bool faceRemoved(const label facei) const;
469 
470  //- Is cell removed?
471  inline bool cellRemoved(const label celli) const;
472 
473 
474  // Edit
475 
476  //- Clear all storage
477  void clear();
478 
479  //- Add all points/faces/cells of mesh. Additional offset for patch
480  // or zone ids.
481  void addMesh
482  (
483  const polyMesh&,
484  const labelList& patchMap,
485  const labelList& pointZoneMap,
486  const labelList& faceZoneMap,
487  const labelList& cellZoneMap
488  );
489 
490  //- Explicitly pre-size the dynamic storage for expected mesh
491  // size for if construct-without-mesh
492  void setCapacity
493  (
494  const label nPoints,
495  const label nFaces,
496  const label nCells
497  );
498 
499  //- Move all points. Incompatible with other topology changes.
500  void movePoints(const pointField& newPoints);
501 
502  //- For compatibility with polyTopoChange: set topological action.
503  label setAction(const topoAction& action);
504 
505  //- Add point. Return new point label.
506  // Notes:
507  // - masterPointID can be < 0 (appended points)
508  // - inCell = false: add retired point (to end of point list)
510  (
511  const point&,
512  const label masterPointID,
513  const label zoneID,
514  const bool inCell
515  );
516 
517  //- Modify coordinate.
518  // Notes:
519  // - inCell = false: add retired point (to end of point list)
520  void modifyPoint
521  (
522  const label,
523  const point&,
524  const label newZoneID,
525  const bool inCell
526  );
527 
528  // Explicitly provided 'old' point location
529 
530  //- Add point with original position. Return new point label.
531  // Notes:
532  // - masterPointID can be < 0 (appended points)
533  // - inCell = false: add retired point (to end of point list)
535  (
536  const point& newPosition,
537  const point& oldPosition,
538  const label masterPointID,
539  const label zoneID
540  );
541 
542  //- Modify coordinate.
543  // Notes:
544  // - inCell = false: add retired point (to end of point list)
545  void modifyPoint
546  (
547  const label pointi,
548  const point& newPosition,
549  const point& oldPosition,
550  const label newZoneID
551  );
552 
553  //- Remove/merge point.
554  void removePoint(const label, const label);
555 
556  //- Add face to cells. Return new face label.
557  // own,nei<0, zoneID>=0 : add inactive face (to end of face list)
558  label addFace
559  (
560  const face& f,
561  const label own,
562  const label nei,
563  const label masterPointID,
564  const label masterEdgeID,
565  const label masterFaceID,
566  const bool flipFaceFlux,
567  const label patchID,
568  const label zoneID,
569  const bool zoneFlip
570  );
571 
572  //- Modify vertices or cell of face.
573  void modifyFace
574  (
575  const face& f,
576  const label facei,
577  const label own,
578  const label nei,
579  const bool flipFaceFlux,
580  const label patchID,
581  const label zoneID,
582  const bool zoneFlip
583  );
584 
585  //- Remove/merge face.
586  void removeFace(const label, const label);
587 
588  //- Add cell. Return new cell label.
589  label addCell
590  (
591  const label masterPointID,
592  const label masterEdgeID,
593  const label masterFaceID,
594  const label masterCellID,
595  const label zoneID
596  );
597 
598  //- Modify zone of cell
599  void modifyCell(const label, const label zoneID);
600 
601  //- Remove/merge cell.
602  void removeCell(const label, const label);
603 
604  //- Explicitly set the number of patches if construct-without-mesh
605  // used.
606  inline void setNumPatches(const label nPatches);
607 
608  // Other
609 
610  //- Inplace changes mesh without change of patches.
611  // Adapts patch start/end and by default does parallel matching.
612  // Clears all data. Returns map.
613  // inflate = true : keep old mesh points. Put new points into the
614  // returned map (preMotionPoints) so we can use inflation. Any
615  // points out of nothing (appended points) are vector::zero.
616  // inflate = false: set mesh points directly. Empty preMotionPoints
617  // in the map.
618  // orderCells : whether to order the cells (see bandCompression.H)
619  // orderPoints : whether to order the points into internal first
620  // followed by boundary points. This is not fully consistent
621  // with upper-triangular ordering of points and edges so
622  // is only done when explicitly asked for.
624  (
625  polyMesh& mesh,
626  const bool inflate,
627  const bool syncParallel = true,
628  const bool orderCells = false,
629  const bool orderPoints = false
630  );
631 
632  //- Create new mesh with old mesh patches
634  (
635  autoPtr<fvMesh>& newMesh,
636  const IOobject& io,
637  const polyMesh& mesh,
638  const bool syncParallel = true,
639  const bool orderCells = false,
640  const bool orderPoints = false
641  );
642 
643 };
644 
645 
646 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
647 
648 } // End namespace Foam
649 
650 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
651 
652 #include "polyTopoChangeI.H"
653 
654 #ifdef NoRepository
655  #include "polyTopoChangeTemplates.C"
656 #endif
657 
658 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
659 
660 #endif
661 
662 // ************************************************************************* //
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
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.
autoPtr< polyTopoChangeMap > changeMesh(polyMesh &mesh, const bool inflate, const bool syncParallel=true, const bool orderCells=false, const bool orderPoints=false)
Inplace changes mesh without change of patches.
void removePoint(const label, const label)
Remove/merge point.
void movePoints(const pointField &newPoints)
Move all points. Incompatible with other topology changes.
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.
void addMesh(const polyMesh &, const labelList &patchMap, const labelList &pointZoneMap, const labelList &faceZoneMap, const labelList &cellZoneMap)
Add all points/faces/cells of mesh. Additional offset for patch.
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
label setAction(const topoAction &action)
For compatibility with polyTopoChange: set topological action.
const DynamicList< point > & points() const
Points. Shrunk after constructing mesh (or calling of compact())
void removeFace(const label, const label)
Remove/merge face.
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 masterPointID, const label masterEdgeID, const label masterFaceID, const label masterCellID, const label zoneID)
Add cell. Return new cell label.
bool faceRemoved(const label facei) const
Is face removed?
void modifyFace(const face &f, const label facei, const label own, const label nei, const bool flipFaceFlux, const label patchID, const label zoneID, const bool zoneFlip)
Modify vertices or cell of face.
void modifyCell(const label, const label zoneID)
Modify zone of cell.
label addPoint(const point &, const label masterPointID, const label zoneID, const bool inCell)
Add point. Return new point label.
const DynamicList< label > & faceOwner() const
label addFace(const face &f, const label own, const label nei, const label masterPointID, const label masterEdgeID, const label masterFaceID, const bool flipFaceFlux, const label patchID, const label zoneID, const bool zoneFlip)
Add face to cells. Return new face label.
ClassName("polyTopoChange")
Runtime type information.
const DynamicList< label > & faceNeighbour() const
void clear()
Clear all storage.
void removeCell(const label, const label)
Remove/merge cell.
const DynamicList< label > & region() const
void modifyPoint(const label, const point &, const label newZoneID, const bool inCell)
Modify coordinate.
Cell-face mesh analysis engine.
Definition: primitiveMesh.H:75
A virtual base class for topological actions.
Definition: topoAction.H:49
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 labelUList &oldToNew, const ListType &)
Reorder the elements (indices, not values) of a list.
labelList f(nPoints)
label nPatches
Definition: readKivaGrid.H:396