ENH: polyTopoChange : store zone as DynamicList<label> to avoid HashTable::erase...
[OpenFOAM-1.6.x.git] / src / dynamicMesh / polyTopoChange / polyTopoChange / polyTopoChange.H
blob00e8e0a6ffcb9f50af7867e1d8a7c2af1905a642
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::polyTopoChange
28 Description
29     Direct mesh changes based on v1.3 polyTopoChange syntax.
31     Instead of recording changes and executing them all in one go (as did
32     v1.3 polyTopoChange) this class actually holds the current
33     points/faces/cells and does the change immediately.
34     It can be asked to compress out all unused points/faces/cells and
35     renumber everything to be consistent.
37     Note:
38     - polyTopoChange can be copied.
39     - adding a face using non-existing cells causes all intermediate cells
40     to be added. So always first add cells/points and then faces.
41     (or set strict checking)
42     - strict checking:
43         - any added/modified face can only use already existing vertices
44         - any added face can only use already existing cells
45         - no item can be removed more than once.
46     - removed cell: cell set to 0 faces.
47     - removed face: face set to 0 vertices.
48     - removed point: coordinate set to greatPoint (GREAT,GREAT,GREAT).
49     Note that this might give problems if this value is used already.
50     To see if point is equal to above value we don't use == (which might give
51     problems with roundoff error) but instead compare the individual component
52     with >.
53     - coupled patches: the reorderCoupledFaces routine (borrowed from
54     the couplePatches utility) reorders coupled patch faces and
55     uses the cyclicPolyPatch,processorPolyPatch functionality.
57 SourceFiles
58     polyTopoChange.C
59     polyTopoChangeI.H
60     polyTopoChangeTemplates.C
62 \*---------------------------------------------------------------------------*/
64 #ifndef polyTopoChange_H
65 #define polyTopoChange_H
67 #include "autoPtr.H"
68 #include "DynamicList.H"
69 #include "labelList.H"
70 #include "IOobject.H"
71 #include "typeInfo.H"
72 #include "pointField.H"
73 #include "PtrList.H"
74 #include "cellList.H"
75 #include "Map.H"
76 #include "HashSet.H"
77 #include "mapPolyMesh.H"
78 #include "CompactListList.H"
79 #include "PackedBoolList.H"
81 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
83 namespace Foam
86 // Forward declaration of classes
87 class face;
88 class primitiveMesh;
89 class polyMesh;
90 class fvMesh;
91 class Time;
92 class fileName;
93 class polyBoundaryMesh;
94 class polyPatch;
95 class dictionary;
96 class topoAction;
97 class objectMap;
99 /*---------------------------------------------------------------------------*\
100                            Class polyTopoChange Declaration
101 \*---------------------------------------------------------------------------*/
103 class polyTopoChange
105     // Static data members
107         //- Value of deleted point
108         static const point greatPoint;
111     // Private data
113         //- Whether to allow referencing illegal points/cells/faces
114         //  when adding/removing data.
115         bool strict_;
118         // Patches
120             //- Number of patches
121             label nPatches_;
124         // Points
126             //- Current point set
127             DynamicList<point> points_;
129             //- Original point label (or masterpoint for added points)
130             DynamicList<label> pointMap_;
132             //- For all original and added points contains new point label.
133             //  (used to map return value of addPoint to new mesh point)
134             DynamicList<label> reversePointMap_;
136             //- Zone of point
137             DynamicList<label> pointZone_;
139             //- Retired points
140             labelHashSet retiredPoints_;
143         // Faces
145             //- Current faceList
146             DynamicList<face> faces_;
148             //- Patch for every external face (-1 for internal faces)
149             DynamicList<label> region_;
151             //- Owner for all faces
152             DynamicList<label> faceOwner_;
154             //- Neighbour for internal faces (-1 for external faces)
155             DynamicList<label> faceNeighbour_;
157             //- Original face label. Or master face for added-from-faces;
158             //  -1 for faces added-from-edge or added-from-point)
159             DynamicList<label> faceMap_;
161             //- For all original and added faces contains new face label
162             //  (used to map return value of addFace to new mesh face)
163             DynamicList<label> reverseFaceMap_;
165             //- Faces added from point (corresponding faceMap_ will
166             //  be -1)
167             Map<label> faceFromPoint_;
169             //- Faces added from edge (corresponding faceMap_ will
170             //  be -1)
171             Map<label> faceFromEdge_;
173             //- In mapping whether to reverse the flux.
174             PackedBoolList flipFaceFlux_;
176             //- Zone of face
177             DynamicList<label> faceZone_;
179             //- Orientation of face in zone
180             PackedBoolList faceZoneFlip_;
182             //- Active faces
183             label nActiveFaces_;
186         // Cells
188             //- Original cell label or master cell for added-from-cell;
189             //  -1 for cells added from face or edge.
190             DynamicList<label> cellMap_;
192             //- For all original and added cells contains new cell label
193             //  (used to map return value of addCell to new mesh cell)
194             DynamicList<label> reverseCellMap_;
196             //- Cells added from point
197             Map<label> cellFromPoint_;
199             //- Cells added from edge
200             Map<label> cellFromEdge_;
202             //- Cells added from face
203             Map<label> cellFromFace_;
205             //- Zone of cell
206             DynamicList<label> cellZone_;
209     // Private Member Functions
211         //- Reorder contents of container according to map
212         template<class T>
213         static void reorder(const labelList& map, DynamicList<T>&);
214         template<class T>
215         static void reorder(const labelList& map, List<DynamicList<T> >&);
216         template<class T>
217         static void renumberKey(const labelList& map, Map<T>&);
219         //- Renumber elements of container according to map
220         static void renumber(const labelList&, labelHashSet&);
221         //- Special handling of reverse maps which have <-1 in them
222         static void renumberReverseMap(const labelList&, DynamicList<label>&);
224         //- Renumber & compact elements of list according to map
225         static void renumberCompact(const labelList&, labelList&);
227         //- Get all set elements as a labelHashSet
228         static labelHashSet getSetIndices(const PackedBoolList&);
230         //- Count number of added and removed quantities from maps.
231         static void countMap
232         (
233             const labelList& map,
234             const labelList& reverseMap,
235             label& nAdd,
236             label& nInflate,
237             label& nMerge,
238             label& nRemove
239         );
241         //- Print some stats about mesh
242         static void writeMeshStats(const polyMesh& mesh, Ostream&);
244         //- Calculate object maps. Requires reverseMap to have destination
245         //  to be marked with <-1.
246         static void getMergeSets
247         (
248             const labelList& reverseCellMap,
249             const labelList& cellMap,
250             List<objectMap>& cellsFromCells
251         );
253         //- Check inputs to modFace or addFace
254         void checkFace
255         (
256             const face&,
257             const label faceI,
258             const label own,
259             const label nei,
260             const label patchI,
261             const label zoneI
262         ) const;
264         //- Construct cells (in packed storage)
265         void makeCells
266         (
267             const label nActiveFaces,
268             labelList& cellFaces,
269             labelList& cellFaceOffsets
270         ) const;
272         //- Construct cellCells (in packed storage)
273         void makeCellCells
274         (
275             const label nActiveFaces,
276             CompactListList<label>& cellCells
277         ) const;
279         //- Cell ordering (bandCompression). Returns number of remaining cells.
280         label getCellOrder(const CompactListList<label>&, labelList&) const;
282         //- Do upper-triangular ordering and patch ordering.
283         void getFaceOrder
284         (
285             const label nActiveFaces,
286             const labelList& cellFaces,
287             const labelList& cellFaceOffsets,
289             labelList& oldToNew,
290             labelList& patchSizes,
291             labelList& patchStarts
292         ) const;
294         //- Compact and reorder faces according to map
295         void reorderCompactFaces
296         (
297             const label newSize,
298             const labelList& oldToNew
299         );
301         //- Remove all unused/removed points/faces/cells and update
302         //  face ordering (always), cell ordering (bandcompression,
303         //  orderCells=true),
304         //  point ordering (sorted into internal and boundary points,
305         //  orderPoints=true)
306         void compact
307         (
308             const bool orderCells,
309             const bool orderPoints,
310             label& nInternalPoints,
311             labelList& patchSizes,
312             labelList& patchStarts
313         );
315         //- Select either internal or external faces out of faceLabels
316         static labelList selectFaces
317         (
318             const primitiveMesh&,
319             const labelList& faceLabels,
320             const bool internalFacesOnly
321         );
323         //- Calculate mapping for patchpoints only
324         void calcPatchPointMap
325         (
326             const List<Map<label> >&,
327             const polyBoundaryMesh&,
328             labelListList&
329         ) const;
331         void calcFaceInflationMaps
332         (
333             const polyMesh&,
334             List<objectMap>&,
335             List<objectMap>&,
336             List<objectMap>&
337         ) const;
339         void calcCellInflationMaps
340         (
341             const polyMesh&,
342             List<objectMap>&,
343             List<objectMap>&,
344             List<objectMap>&,
345             List<objectMap>&
346         ) const;
348         void resetZones
349         (
350             const polyMesh&,        // mesh to get existing info from
351             polyMesh&,              // mesh to change zones on
352             labelListList&,
353             labelListList&,
354             labelListList&
355         ) const;
357         void calcFaceZonePointMap
358         (
359             const polyMesh&,
360             const List<Map<label> >&,
361             labelListList&
362         ) const;
365         // Coupling
367             //- Rotate face by number of positions
368             static face rotateFace(const face& f, const label nPos);
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             );
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> >& oldFaceZoneMeshPointMaps
402         );
404 public:
406     //- Runtime type information
407     ClassName("polyTopoChange");
411     // Constructors
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);
417         //- Construct from mesh. Adds all points/face/cells from mesh.
418         polyTopoChange(const polyMesh& mesh, const bool strict = true);
421     // Member Functions
423         // Access
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             }
451             //- Is point removed?
452             inline bool pointRemoved(const label pointI) const;
453             //- Is face removed?
454             inline bool faceRemoved(const label faceI) const;
455             //- Is cell removed?
456             inline bool cellRemoved(const label cellI) const;
459         // Edit
461             //- Clear all storage
462             void clear();
464             //- Add all points/faces/cells of mesh. Additional offset for patch
465             //  or zone ids.
466             void addMesh
467             (
468                 const polyMesh&,
469                 const labelList& patchMap,
470                 const labelList& pointZoneMap,
471                 const labelList& faceZoneMap,
472                 const labelList& cellZoneMap
473             );
475             //- Explicitly pre-size the dynamic storage for expected mesh
476             //  size for if construct-without-mesh
477             void setCapacity
478             (
479                 const label nPoints,
480                 const label nFaces,
481                 const label nCells
482             );
484             //- Move all points. Incompatible with other topology changes.
485             void movePoints(const pointField& newPoints);
487             //- For compatibility with polyTopoChange: set topological action.
488             label setAction(const topoAction& action);
490             //- Add point. Return new point label.
491             //  Notes:
492             //  - masterPointID can be < 0 (appended points)
493             //  - inCell = false: add retired point (to end of point list)
494             label addPoint
495             (
496                 const point&,
497                 const label masterPointID,
498                 const label zoneID,
499                 const bool inCell
500             );
502             //- Modify coordinate.
503             //  Notes:
504             //  - inCell = false: add retired point (to end of point list)
505             void modifyPoint
506             (
507                 const label,
508                 const point&,
509                 const label newZoneID,
510                 const bool inCell
511             );
513             //- Remove/merge point.
514             void removePoint(const label, const label);
516             //- Add face to cells. Return new face label.
517             //  own,nei<0, zoneID>=0 : add inactive face (to end of face list)
518             label addFace
519             (
520                 const face& f,
521                 const label own,
522                 const label nei,
523                 const label masterPointID,
524                 const label masterEdgeID,
525                 const label masterFaceID,
526                 const bool flipFaceFlux,
527                 const label patchID,
528                 const label zoneID,
529                 const bool zoneFlip
530             );
532             //- Modify vertices or cell of face.
533             void modifyFace
534             (
535                 const face& f,
536                 const label faceI,
537                 const label own,
538                 const label nei,
539                 const bool flipFaceFlux,
540                 const label patchID,
541                 const label zoneID,
542                 const bool zoneFlip
543             );
545             //- Remove/merge face.
546             void removeFace(const label, const label);
548             //- Add cell. Return new cell label.
549             label addCell
550             (
551                 const label masterPointID,
552                 const label masterEdgeID,
553                 const label masterFaceID,
554                 const label masterCellID,
555                 const label zoneID
556             );
558             //- Modify zone of cell
559             void modifyCell(const label, const label zoneID);
561             //- Remove/merge cell.
562             void removeCell(const label, const label);
564             //- Explicitly set the number of patches if construct-without-mesh
565             //  used.
566             inline void setNumPatches(const label nPatches);
569         // Other
571             //- Inplace changes mesh without change of patches.
572             //  Adapts patch start/end and by default does parallel matching.
573             //  Clears all data. Returns map.
574             //  inflate = true : keep old mesh points. Put new points into the
575             //  returned map (preMotionPoints) so we can use inflation. Any
576             //  points out of nothing (appended points) are vector::zero.
577             //  inflate = false: set mesh points directly. Empty preMotionPoints
578             //  in the map.
579             //  orderCells :  whether to order the cells (see bandCompression.H)
580             //  orderPoints : whether to order the points into internal first
581             //  followed by boundary points. This is not fully consistent
582             //  with upper-triangular ordering of points and edges so
583             //  is only done when explicitly asked for.
584             autoPtr<mapPolyMesh> changeMesh
585             (
586                 polyMesh& mesh,
587                 const bool inflate,
588                 const bool syncParallel = true,
589                 const bool orderCells = false,
590                 const bool orderPoints = false
591             );
593             //- Create new mesh with old mesh patches
594             autoPtr<mapPolyMesh> makeMesh
595             (
596                 autoPtr<fvMesh>& newMesh,
597                 const IOobject& io,
598                 const fvMesh& mesh,
599                 const bool syncParallel = true,
600                 const bool orderCells = false,
601                 const bool orderPoints = false
602             );
607 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
609 } // End namespace Foam
611 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
613 #include "polyTopoChangeI.H"
615 #ifdef NoRepository
616 #   include "polyTopoChangeTemplates.C"
617 #endif
619 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
621 #endif
623 // ************************************************************************* //