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-2022 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  void compact
310  (
311  const bool orderCells,
312  const bool orderPoints,
313  label& nInternalPoints,
314  labelList& patchSizes,
315  labelList& patchStarts
316  );
317 
318  //- Select either internal or external faces out of faceLabels
319  static labelList selectFaces
320  (
321  const primitiveMesh&,
322  const labelList& faceLabels,
323  const bool internalFacesOnly
324  );
325 
326  //- Calculate mapping for patchpoints only
327  void calcPatchPointMap
328  (
329  const List<Map<label>>&,
330  const polyBoundaryMesh&,
332  ) const;
333 
334  void calcFaceInflationMaps
335  (
336  const polyMesh&,
340  ) const;
341 
342  void calcCellInflationMaps
343  (
344  const polyMesh&,
349  ) const;
350 
351  void resetZones
352  (
353  const polyMesh&, // mesh to get existing info from
354  polyMesh&, // mesh to change zones on
355  labelListList&,
356  labelListList&,
358  ) const;
359 
360  void calcFaceZonePointMap
361  (
362  const polyMesh&,
363  const List<Map<label>>&,
365  ) const;
366 
367 
368  // Coupling
369 
370  //- Do all coupled patch face reordering
371  void reorderCoupledFaces
372  (
373  const bool syncParallel,
374  const polyBoundaryMesh&,
375  const labelList& patchStarts,
376  const labelList& patchSizes,
377  const pointField& points
378  );
379 
380  void compactAndReorder
381  (
382  const polyMesh&,
383  const bool syncParallel,
384  const bool orderCells,
385  const bool orderPoints,
386  label& nInternalPoints,
387  pointField& newPoints,
388  labelList& patchSizes,
389  labelList& patchStarts,
390  List<objectMap>& pointsFromPoints,
391  List<objectMap>& facesFromPoints,
392  List<objectMap>& facesFromEdges,
393  List<objectMap>& facesFromFaces,
394  List<objectMap>& cellsFromPoints,
395  List<objectMap>& cellsFromEdges,
396  List<objectMap>& cellsFromFaces,
397  List<objectMap>& cellsFromCells,
398  List<Map<label>>& oldPatchMeshPointMaps,
399  labelList& oldPatchNMeshPoints,
400  labelList& oldPatchStarts,
401  List<Map<label>>& oldMeshFaceZonesPointMaps
402  );
403 
404 public:
405 
406  //- Runtime type information
407  ClassName("polyTopoChange");
408 
409 
410 
411  // Constructors
412 
413  //- Construct without mesh. Either specify nPatches or use
414  // setNumPatches before trying to make a mesh (makeMesh, changeMesh)
415  polyTopoChange(const label nPatches, const bool strict = true);
416 
417  //- Construct from mesh. Adds all points/face/cells from mesh.
418  polyTopoChange(const polyMesh& mesh, const bool strict = true);
419 
420 
421  // Member Functions
422 
423  // Access
424 
425  //- Points. Shrunk after constructing mesh (or calling of compact())
426  const DynamicList<point>& points() const
427  {
428  return points_;
429  }
431  const DynamicList<face>& faces() const
432  {
433  return faces_;
434  }
436  const DynamicList<label>& region() const
437  {
438  return region_;
439  }
441  const DynamicList<label>& faceOwner() const
442  {
443  return faceOwner_;
444  }
446  const DynamicList<label>& faceNeighbour() const
447  {
448  return faceNeighbour_;
449  }
450 
451  //- Is point removed?
452  inline bool pointRemoved(const label pointi) const;
453 
454  //- Is face removed?
455  inline bool faceRemoved(const label facei) const;
456 
457  //- Is cell removed?
458  inline bool cellRemoved(const label celli) const;
459 
460 
461  // Edit
462 
463  //- Clear all storage
464  void clear();
465 
466  //- Add all points/faces/cells of mesh. Additional offset for patch
467  // or zone ids.
468  void addMesh
469  (
470  const polyMesh&,
471  const labelList& patchMap,
472  const labelList& pointZoneMap,
473  const labelList& faceZoneMap,
474  const labelList& cellZoneMap
475  );
476 
477  //- Explicitly pre-size the dynamic storage for expected mesh
478  // size for if construct-without-mesh
479  void setCapacity
480  (
481  const label nPoints,
482  const label nFaces,
483  const label nCells
484  );
485 
486  //- Move all points. Incompatible with other topology changes.
487  void movePoints(const pointField& newPoints);
488 
489  //- For compatibility with polyTopoChange: set topological action.
490  label setAction(const topoAction& action);
491 
492  //- Add point. Return new point label.
493  // Notes:
494  // - masterPointID can be < 0 (appended points)
495  // - inCell = false: add retired point (to end of point list)
497  (
498  const point&,
499  const label masterPointID,
500  const label zoneID,
501  const bool inCell
502  );
503 
504  //- Modify coordinate.
505  // Notes:
506  // - inCell = false: add retired point (to end of point list)
507  void modifyPoint
508  (
509  const label,
510  const point&,
511  const label newZoneID,
512  const bool inCell
513  );
514 
515  // Explicitly provided 'old' point location
516 
517  //- Add point with original position. Return new point label.
518  // Notes:
519  // - masterPointID can be < 0 (appended points)
520  // - inCell = false: add retired point (to end of point list)
522  (
523  const point& newPosition,
524  const point& oldPosition,
525  const label masterPointID,
526  const label zoneID
527  );
528 
529  //- Modify coordinate.
530  // Notes:
531  // - inCell = false: add retired point (to end of point list)
532  void modifyPoint
533  (
534  const label pointi,
535  const point& newPosition,
536  const point& oldPosition,
537  const label newZoneID
538  );
539 
540  //- Remove/merge point.
541  void removePoint(const label, const label);
542 
543  //- Add face to cells. Return new face label.
544  // own,nei<0, zoneID>=0 : add inactive face (to end of face list)
545  label addFace
546  (
547  const face& f,
548  const label own,
549  const label nei,
550  const label masterPointID,
551  const label masterEdgeID,
552  const label masterFaceID,
553  const bool flipFaceFlux,
554  const label patchID,
555  const label zoneID,
556  const bool zoneFlip
557  );
558 
559  //- Modify vertices or cell of face.
560  void modifyFace
561  (
562  const face& f,
563  const label facei,
564  const label own,
565  const label nei,
566  const bool flipFaceFlux,
567  const label patchID,
568  const label zoneID,
569  const bool zoneFlip
570  );
571 
572  //- Remove/merge face.
573  void removeFace(const label, const label);
574 
575  //- Add cell. Return new cell label.
576  label addCell
577  (
578  const label masterPointID,
579  const label masterEdgeID,
580  const label masterFaceID,
581  const label masterCellID,
582  const label zoneID
583  );
584 
585  //- Modify zone of cell
586  void modifyCell(const label, const label zoneID);
587 
588  //- Remove/merge cell.
589  void removeCell(const label, const label);
590 
591  //- Explicitly set the number of patches if construct-without-mesh
592  // used.
593  inline void setNumPatches(const label nPatches);
594 
595  // Other
596 
597  //- Inplace changes mesh without change of patches.
598  // Adapts patch start/end and by default does parallel matching.
599  // Clears all data. Returns map.
600  // inflate = true : keep old mesh points. Put new points into the
601  // returned map (preMotionPoints) so we can use inflation. Any
602  // points out of nothing (appended points) are vector::zero.
603  // inflate = false: set mesh points directly. Empty preMotionPoints
604  // in the map.
605  // orderCells : whether to order the cells (see bandCompression.H)
606  // orderPoints : whether to order the points into internal first
607  // followed by boundary points. This is not fully consistent
608  // with upper-triangular ordering of points and edges so
609  // is only done when explicitly asked for.
611  (
612  polyMesh& mesh,
613  const bool inflate,
614  const bool syncParallel = true,
615  const bool orderCells = false,
616  const bool orderPoints = false
617  );
618 
619  //- Create new mesh with old mesh patches
621  (
622  autoPtr<fvMesh>& newMesh,
623  const IOobject& io,
624  const polyMesh& mesh,
625  const bool syncParallel = true,
626  const bool orderCells = false,
627  const bool orderPoints = false
628  );
629 
630 };
631 
632 
633 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
634 
635 } // End namespace Foam
636 
637 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
638 
639 #include "polyTopoChangeI.H"
640 
641 #ifdef NoRepository
642  #include "polyTopoChangeTemplates.C"
643 #endif
644 
645 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
646 
647 #endif
648 
649 // ************************************************************************* //
label nPatches
Definition: readKivaGrid.H:402
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.
const DynamicList< label > & faceNeighbour() const
const DynamicList< face > & faces() const
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
polyTopoChange(const label nPatches, const bool strict=true)
Construct without mesh. Either specify nPatches or use.
Cell-face mesh analysis engine.
Definition: primitiveMesh.H:74
const DynamicList< label > & region() const
void removePoint(const label, const label)
Remove/merge point.
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.
fvMesh & mesh
void removeFace(const label, const label)
Remove/merge face.
void clear()
Clear all storage.
label addCell(const label masterPointID, const label masterEdgeID, const label masterFaceID, const label masterCellID, const label zoneID)
Add cell. Return new cell label.
bool cellRemoved(const label celli) const
Is cell removed?
ClassName("polyTopoChange")
Runtime type information.
void setNumPatches(const label nPatches)
Explicitly set the number of patches if construct-without-mesh.
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.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
label nPoints
const DynamicList< point > & points() const
Points. Shrunk after constructing mesh (or calling of compact())
label addPoint(const point &, const label masterPointID, const label zoneID, const bool inCell)
Add point. Return new point label.
A virtual base class for topological actions.
Definition: topoAction.H:48
Foam::polyBoundaryMesh.
void setCapacity(const label nPoints, const label nFaces, const label nCells)
Explicitly pre-size the dynamic storage for expected mesh.
A packed storage unstructured matrix of objects of type <T> using an offset table for access...
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
void movePoints(const pointField &newPoints)
Move all points. Incompatible with other topology changes.
labelList f(nPoints)
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.
A bit-packed bool list.
label patchi
Direct mesh changes based on v1.3 polyTopoChange syntax.
void modifyPoint(const label, const point &, const label newZoneID, const bool inCell)
Modify coordinate.
bool pointRemoved(const label pointi) const
Is point removed?
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:76
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.
void modifyCell(const label, const label zoneID)
Modify zone of cell.
bool faceRemoved(const label facei) const
Is face removed?
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
const DynamicList< label > & faceOwner() const
void removeCell(const label, const label)
Remove/merge cell.
Namespace for OpenFOAM.
label setAction(const topoAction &action)
For compatibility with polyTopoChange: set topological action.