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