OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

polyTopoChange.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::polyTopoChange
00027 
00028 Description
00029     Direct mesh changes based on v1.3 polyTopoChange syntax.
00030 
00031     Instead of recording changes and executing them all in one go (as did
00032     v1.3 polyTopoChange) this class actually holds the current
00033     points/faces/cells and does the change immediately.
00034     It can be asked to compress out all unused points/faces/cells and
00035     renumber everything to be consistent.
00036 
00037     Note:
00038     - polyTopoChange can be copied.
00039     - adding a face using non-existing cells causes all intermediate cells
00040     to be added. So always first add cells/points and then faces.
00041     (or set strict checking)
00042     - strict checking:
00043         - any added/modified face can only use already existing vertices
00044         - any added face can only use already existing cells
00045         - no item can be removed more than once.
00046     - removed cell: cell set to 0 faces.
00047     - removed face: face set to 0 vertices.
00048     - removed point: coordinate set to greatPoint (GREAT,GREAT,GREAT).
00049     Note that this might give problems if this value is used already.
00050     To see if point is equal to above value we don't use == (which might give
00051     problems with roundoff error) but instead compare the individual component
00052     with >.
00053     - coupled patches: the reorderCoupledFaces routine (borrowed from
00054     the couplePatches utility) reorders coupled patch faces and
00055     uses the cyclicPolyPatch,processorPolyPatch functionality.
00056 
00057 SourceFiles
00058     polyTopoChange.C
00059     polyTopoChangeI.H
00060     polyTopoChangeTemplates.C
00061 
00062 \*---------------------------------------------------------------------------*/
00063 
00064 #ifndef polyTopoChange_H
00065 #define polyTopoChange_H
00066 
00067 #include "autoPtr.H"
00068 #include "DynamicList.H"
00069 #include "labelList.H"
00070 #include "IOobject.H"
00071 #include "typeInfo.H"
00072 #include "pointField.H"
00073 #include "PtrList.H"
00074 #include "cellList.H"
00075 #include "Map.H"
00076 #include "HashSet.H"
00077 #include "mapPolyMesh.H"
00078 #include "CompactListList.H"
00079 #include "PackedBoolList.H"
00080 
00081 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00082 
00083 namespace Foam
00084 {
00085 
00086 // Forward declaration of classes
00087 class face;
00088 class primitiveMesh;
00089 class polyMesh;
00090 class fvMesh;
00091 class Time;
00092 class fileName;
00093 class polyBoundaryMesh;
00094 class polyPatch;
00095 class dictionary;
00096 class topoAction;
00097 class objectMap;
00098 
00099 /*---------------------------------------------------------------------------*\
00100                            Class polyTopoChange Declaration
00101 \*---------------------------------------------------------------------------*/
00102 
00103 class polyTopoChange
00104 {
00105     // Static data members
00106 
00107         //- Value of deleted point
00108         static const point greatPoint;
00109 
00110 
00111     // Private data
00112 
00113         //- Whether to allow referencing illegal points/cells/faces
00114         //  when adding/removing data.
00115         bool strict_;
00116 
00117 
00118         // Patches
00119 
00120             //- Number of patches
00121             label nPatches_;
00122 
00123 
00124         // Points
00125 
00126             //- Current point set
00127             DynamicList<point> points_;
00128 
00129             //- Original point label (or masterpoint for added points)
00130             DynamicList<label> pointMap_;
00131 
00132             //- For all original and added points contains new point label.
00133             //  (used to map return value of addPoint to new mesh point)
00134             DynamicList<label> reversePointMap_;
00135 
00136             //- Zone of point
00137             Map<label> pointZone_;
00138 
00139             //- Retired points
00140             labelHashSet retiredPoints_;
00141 
00142 
00143         // Faces
00144 
00145             //- Current faceList
00146             DynamicList<face> faces_;
00147 
00148             //- Patch for every external face (-1 for internal faces)
00149             DynamicList<label> region_;
00150 
00151             //- Owner for all faces
00152             DynamicList<label> faceOwner_;
00153 
00154             //- Neighbour for internal faces (-1 for external faces)
00155             DynamicList<label> faceNeighbour_;
00156 
00157             //- Original face label. Or master face for added-from-faces;
00158             //  -1 for faces added-from-edge or added-from-point)
00159             DynamicList<label> faceMap_;
00160 
00161             //- For all original and added faces contains new face label
00162             //  (used to map return value of addFace to new mesh face)
00163             DynamicList<label> reverseFaceMap_;
00164 
00165             //- Faces added from point (corresponding faceMap_ will
00166             //  be -1)
00167             Map<label> faceFromPoint_;
00168 
00169             //- Faces added from edge (corresponding faceMap_ will
00170             //  be -1)
00171             Map<label> faceFromEdge_;
00172 
00173             //- In mapping whether to reverse the flux.
00174             PackedBoolList flipFaceFlux_;
00175 
00176             //- Zone of face
00177             Map<label> faceZone_;
00178 
00179             //- Orientation of face in zone
00180             PackedBoolList faceZoneFlip_;
00181 
00182             //- Active faces
00183             label nActiveFaces_;
00184 
00185 
00186         // Cells
00187 
00188             //- Original cell label or master cell for added-from-cell;
00189             //  -1 for cells added from face or edge.
00190             DynamicList<label> cellMap_;
00191 
00192             //- For all original and added cells contains new cell label
00193             //  (used to map return value of addCell to new mesh cell)
00194             DynamicList<label> reverseCellMap_;
00195 
00196             //- Cells added from point
00197             Map<label> cellFromPoint_;
00198 
00199             //- Cells added from edge
00200             Map<label> cellFromEdge_;
00201 
00202             //- Cells added from face
00203             Map<label> cellFromFace_;
00204 
00205             //- Zone of cell
00206             DynamicList<label> cellZone_;
00207 
00208 
00209     // Private Member Functions
00210 
00211         //- Reorder contents of container according to map
00212         template<class T>
00213         static void reorder(const labelList& map, DynamicList<T>&);
00214         template<class T>
00215         static void reorder(const labelList& map, List<DynamicList<T> >&);
00216         template<class T>
00217         static void renumberKey(const labelList& map, Map<T>&);
00218 
00219         //- Renumber elements of container according to map
00220         static void renumber(const labelList&, labelHashSet&);
00221         //- Special handling of reverse maps which have <-1 in them
00222         static void renumberReverseMap(const labelList&, DynamicList<label>&);
00223 
00224         //- Renumber & compact elements of list according to map
00225         static void renumberCompact(const labelList&, labelList&);
00226 
00227         //- Get all set elements as a labelHashSet
00228         static labelHashSet getSetIndices(const PackedBoolList&);
00229 
00230         //- Count number of added and removed quantities from maps.
00231         static void countMap
00232         (
00233             const labelList& map,
00234             const labelList& reverseMap,
00235             label& nAdd,
00236             label& nInflate,
00237             label& nMerge,
00238             label& nRemove
00239         );
00240 
00241         //- Print some stats about mesh
00242         static void writeMeshStats(const polyMesh& mesh, Ostream&);
00243 
00244         //- Calculate object maps. Requires reverseMap to have destination
00245         //  to be marked with <-1.
00246         static void getMergeSets
00247         (
00248             const labelList& reverseCellMap,
00249             const labelList& cellMap,
00250             List<objectMap>& cellsFromCells
00251         );
00252 
00253         //- Check inputs to modFace or addFace
00254         void checkFace
00255         (
00256             const face&,
00257             const label faceI,
00258             const label own,
00259             const label nei,
00260             const label patchI,
00261             const label zoneI
00262         ) const;
00263 
00264         //- Construct cells (in packed storage)
00265         void makeCells
00266         (
00267             const label nActiveFaces,
00268             labelList& cellFaces,
00269             labelList& cellFaceOffsets
00270         ) const;
00271 
00272         //- Construct cellCells (in packed storage)
00273         void makeCellCells
00274         (
00275             const label nActiveFaces,
00276             CompactListList<label>& cellCells
00277         ) const;
00278 
00279         //- Cell ordering (bandCompression). Returns number of remaining cells.
00280         label getCellOrder(const CompactListList<label>&, labelList&) const;
00281 
00282         //- Do upper-triangular ordering and patch ordering.
00283         void getFaceOrder
00284         (
00285             const label nActiveFaces,
00286             const labelList& cellFaces,
00287             const labelList& cellFaceOffsets,
00288 
00289             labelList& oldToNew,
00290             labelList& patchSizes,
00291             labelList& patchStarts
00292         ) const;
00293 
00294         //- Compact and reorder faces according to map
00295         void reorderCompactFaces
00296         (
00297             const label newSize,
00298             const labelList& oldToNew
00299         );
00300 
00301         //- Remove all unused/removed points/faces/cells and update
00302         //  face ordering (always), cell ordering (bandcompression,
00303         //  orderCells=true),
00304         //  point ordering (sorted into internal and boundary points,
00305         //  orderPoints=true)
00306         void compact
00307         (
00308             const bool orderCells,
00309             const bool orderPoints,
00310             label& nInternalPoints,
00311             labelList& patchSizes,
00312             labelList& patchStarts
00313         );
00314 
00315         //- Select either internal or external faces out of faceLabels
00316         static labelList selectFaces
00317         (
00318             const primitiveMesh&,
00319             const labelList& faceLabels,
00320             const bool internalFacesOnly
00321         );
00322 
00323         //- Calculate mapping for patchpoints only
00324         void calcPatchPointMap
00325         (
00326             const List<Map<label> >&,
00327             const polyBoundaryMesh&,
00328             labelListList&
00329         ) const;
00330 
00331         void calcFaceInflationMaps
00332         (
00333             const polyMesh&,
00334             List<objectMap>&,
00335             List<objectMap>&,
00336             List<objectMap>&
00337         ) const;
00338 
00339         void calcCellInflationMaps
00340         (
00341             const polyMesh&,
00342             List<objectMap>&,
00343             List<objectMap>&,
00344             List<objectMap>&,
00345             List<objectMap>&
00346         ) const;
00347 
00348         void resetZones
00349         (
00350             const polyMesh&,        // mesh to get existing info from
00351             polyMesh&,              // mesh to change zones on
00352             labelListList&,
00353             labelListList&,
00354             labelListList&
00355         ) const;
00356 
00357         void calcFaceZonePointMap
00358         (
00359             const polyMesh&,
00360             const List<Map<label> >&,
00361             labelListList&
00362         ) const;
00363 
00364 
00365         // Coupling
00366 
00367             //- Rotate face by number of positions
00368             static face rotateFace(const face& f, const label nPos);
00369 
00370             //- Do all coupled patch face reordering
00371             void reorderCoupledFaces
00372             (
00373                 const bool syncParallel,
00374                 const polyBoundaryMesh&,
00375                 const labelList& patchStarts,
00376                 const labelList& patchSizes,
00377                 const pointField& points
00378             );
00379 
00380         void compactAndReorder
00381         (
00382             const polyMesh&,
00383             const bool syncParallel,
00384             const bool orderCells,
00385             const bool orderPoints,
00386             label& nInternalPoints,
00387             pointField& newPoints,
00388             labelList& patchSizes,
00389             labelList& patchStarts,
00390             List<objectMap>& pointsFromPoints,
00391             List<objectMap>& facesFromPoints,
00392             List<objectMap>& facesFromEdges,
00393             List<objectMap>& facesFromFaces,
00394             List<objectMap>& cellsFromPoints,
00395             List<objectMap>& cellsFromEdges,
00396             List<objectMap>& cellsFromFaces,
00397             List<objectMap>& cellsFromCells,
00398             List<Map<label> >& oldPatchMeshPointMaps,
00399             labelList& oldPatchNMeshPoints,
00400             labelList& oldPatchStarts,
00401             List<Map<label> >& oldFaceZoneMeshPointMaps
00402         );
00403 
00404 public:
00405 
00406     //- Runtime type information
00407     ClassName("polyTopoChange");
00408 
00409 
00410 
00411     // Constructors
00412 
00413         //- Construct without mesh. Either specify nPatches or use
00414         //  setNumPatches before trying to make a mesh (makeMesh, changeMesh)
00415         polyTopoChange(const label nPatches, const bool strict = true);
00416 
00417         //- Construct from mesh. Adds all points/face/cells from mesh.
00418         polyTopoChange(const polyMesh& mesh, const bool strict = true);
00419 
00420 
00421     // Member Functions
00422 
00423         // Access
00424 
00425             //- Points. Shrunk after constructing mesh (or calling of compact())
00426             const DynamicList<point>& points() const
00427             {
00428                 return points_;
00429             }
00430 
00431             const DynamicList<face>& faces() const
00432             {
00433                 return faces_;
00434             }
00435 
00436             const DynamicList<label>& region() const
00437             {
00438                 return region_;
00439             }
00440 
00441             const DynamicList<label>& faceOwner() const
00442             {
00443                 return faceOwner_;
00444             }
00445 
00446             const DynamicList<label>& faceNeighbour() const
00447             {
00448                 return faceNeighbour_;
00449             }
00450 
00451             //- Is point removed?
00452             inline bool pointRemoved(const label pointI) const;
00453             //- Is face removed?
00454             inline bool faceRemoved(const label faceI) const;
00455             //- Is cell removed?
00456             inline bool cellRemoved(const label cellI) const;
00457 
00458 
00459         // Edit
00460 
00461             //- Clear all storage
00462             void clear();
00463 
00464             //- Add all points/faces/cells of mesh. Additional offset for patch
00465             //  or zone ids.
00466             void addMesh
00467             (
00468                 const polyMesh&,
00469                 const labelList& patchMap,
00470                 const labelList& pointZoneMap,
00471                 const labelList& faceZoneMap,
00472                 const labelList& cellZoneMap
00473             );
00474 
00475             //- Move all points. Incompatible with other topology changes.
00476             void movePoints(const pointField& newPoints);
00477 
00478             //- For compatibility with polyTopoChange: set topological action.
00479             label setAction(const topoAction& action);
00480 
00481             //- Add point. Return new point label.
00482             //  Notes:
00483             //  - masterPointID can be < 0 (appended points)
00484             //  - inCell = false: add retired point (to end of point list)
00485             label addPoint
00486             (
00487                 const point&,
00488                 const label masterPointID,
00489                 const label zoneID,
00490                 const bool inCell
00491             );
00492 
00493             //- Modify coordinate.
00494             //  Notes:
00495             //  - inCell = false: add retired point (to end of point list)
00496             void modifyPoint
00497             (
00498                 const label,
00499                 const point&,
00500                 const label newZoneID,
00501                 const bool inCell
00502             );
00503 
00504             //- Remove/merge point.
00505             void removePoint(const label, const label);
00506 
00507             //- Add face to cells. Return new face label.
00508             //  own,nei<0, zoneID>=0 : add inactive face (to end of face list)
00509             label addFace
00510             (
00511                 const face& f,
00512                 const label own,
00513                 const label nei,
00514                 const label masterPointID,
00515                 const label masterEdgeID,
00516                 const label masterFaceID,
00517                 const bool flipFaceFlux,
00518                 const label patchID,
00519                 const label zoneID,
00520                 const bool zoneFlip
00521             );
00522 
00523             //- Modify vertices or cell of face.
00524             void modifyFace
00525             (
00526                 const face& f,
00527                 const label faceI,
00528                 const label own,
00529                 const label nei,
00530                 const bool flipFaceFlux,
00531                 const label patchID,
00532                 const label zoneID,
00533                 const bool zoneFlip
00534             );
00535 
00536             //- Remove/merge face.
00537             void removeFace(const label, const label);
00538 
00539             //- Add cell. Return new cell label.
00540             label addCell
00541             (
00542                 const label masterPointID,
00543                 const label masterEdgeID,
00544                 const label masterFaceID,
00545                 const label masterCellID,
00546                 const label zoneID
00547             );
00548 
00549             //- Modify zone of cell
00550             void modifyCell(const label, const label zoneID);
00551 
00552             //- Remove/merge cell.
00553             void removeCell(const label, const label);
00554 
00555             //- Explicitly set the number of patches if construct-without-mesh
00556             //  used.
00557             inline void setNumPatches(const label nPatches);
00558 
00559         // Other
00560 
00561             //- Inplace changes mesh without change of patches.
00562             //  Adapts patch start/end and by default does parallel matching.
00563             //  Clears all data. Returns map.
00564             //  inflate = true : keep old mesh points. Put new points into the
00565             //  returned map (preMotionPoints) so we can use inflation. Any
00566             //  points out of nothing (appended points) are vector::zero.
00567             //  inflate = false: set mesh points directly. Empty preMotionPoints
00568             //  in the map.
00569             //  orderCells :  whether to order the cells (see bandCompression.H)
00570             //  orderPoints : whether to order the points into internal first
00571             //  followed by boundary points. This is not fully consistent
00572             //  with upper-triangular ordering of points and edges so
00573             //  is only done when explicitly asked for.
00574             autoPtr<mapPolyMesh> changeMesh
00575             (
00576                 polyMesh& mesh,
00577                 const bool inflate,
00578                 const bool syncParallel = true,
00579                 const bool orderCells = false,
00580                 const bool orderPoints = false
00581             );
00582 
00583             //- Create new mesh with old mesh patches
00584             autoPtr<mapPolyMesh> makeMesh
00585             (
00586                 autoPtr<fvMesh>& newMesh,
00587                 const IOobject& io,
00588                 const fvMesh& mesh,
00589                 const bool syncParallel = true,
00590                 const bool orderCells = false,
00591                 const bool orderPoints = false
00592             );
00593 
00594 };
00595 
00596 
00597 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00598 
00599 } // End namespace Foam
00600 
00601 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00602 
00603 #include "polyTopoChangeI.H"
00604 
00605 #ifdef NoRepository
00606 #   include "polyTopoChangeTemplates.C"
00607 #endif
00608 
00609 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00610 
00611 #endif
00612 
00613 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd