FreeFOAM The Cross-Platform CFD Toolkit
polyTopoChange.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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 greatPoint (GREAT,GREAT,GREAT).
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 <OpenFOAM/autoPtr.H>
67 #include <OpenFOAM/DynamicList.H>
68 #include <OpenFOAM/labelList.H>
69 #include <OpenFOAM/IOobject.H>
70 #include <OpenFOAM/typeInfo.H>
71 #include <OpenFOAM/pointField.H>
72 #include <OpenFOAM/PtrList.H>
73 #include <OpenFOAM/cellList.H>
74 #include <OpenFOAM/Map.H>
75 #include <OpenFOAM/HashSet.H>
76 #include <OpenFOAM/mapPolyMesh.H>
79 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 namespace Foam
83 {
84 
85 // Forward declaration of classes
86 class face;
87 class primitiveMesh;
88 class polyMesh;
89 class fvMesh;
90 class Time;
91 class fileName;
92 class polyBoundaryMesh;
93 class polyPatch;
94 class dictionary;
95 class topoAction;
96 class objectMap;
97 
98 /*---------------------------------------------------------------------------*\
99  Class polyTopoChange Declaration
100 \*---------------------------------------------------------------------------*/
101 
103 {
104  // Static data members
105 
106  //- Value of deleted point
107  static const point greatPoint;
108 
109 
110  // Private data
111 
112  //- Whether to allow referencing illegal points/cells/faces
113  // when adding/removing data.
114  bool strict_;
115 
116 
117  // Patches
118 
119  //- Number of patches
120  label nPatches_;
121 
122 
123  // Points
124 
125  //- Current point set
126  DynamicList<point> points_;
127 
128  //- Original point label (or masterpoint for added points)
129  DynamicList<label> pointMap_;
130 
131  //- For all original and added points contains new point label.
132  // (used to map return value of addPoint to new mesh point)
133  DynamicList<label> reversePointMap_;
134 
135  //- Zone of point
136  DynamicList<label> pointZone_;
137 
138  //- Retired points
139  labelHashSet retiredPoints_;
140 
141 
142  // Faces
143 
144  //- Current faceList
145  DynamicList<face> faces_;
146 
147  //- Patch for every external face (-1 for internal faces)
148  DynamicList<label> region_;
149 
150  //- Owner for all faces
151  DynamicList<label> faceOwner_;
152 
153  //- Neighbour for internal faces (-1 for external faces)
154  DynamicList<label> faceNeighbour_;
155 
156  //- Original face label. Or master face for added-from-faces;
157  // -1 for faces added-from-edge or added-from-point)
158  DynamicList<label> faceMap_;
159 
160  //- For all original and added faces contains new face label
161  // (used to map return value of addFace to new mesh face)
162  DynamicList<label> reverseFaceMap_;
163 
164  //- Faces added from point (corresponding faceMap_ will
165  // be -1)
166  Map<label> faceFromPoint_;
167 
168  //- Faces added from edge (corresponding faceMap_ will
169  // be -1)
170  Map<label> faceFromEdge_;
171 
172  //- In mapping whether to reverse the flux.
173  PackedBoolList flipFaceFlux_;
174 
175  //- Zone of face
176  DynamicList<label> faceZone_;
177 
178  //- Orientation of face in zone
179  PackedBoolList faceZoneFlip_;
180 
181  //- Active faces
182  label nActiveFaces_;
183 
184 
185  // Cells
186 
187  //- Original cell label or master cell for added-from-cell;
188  // -1 for cells added from face or edge.
189  DynamicList<label> cellMap_;
190 
191  //- For all original and added cells contains new cell label
192  // (used to map return value of addCell to new mesh cell)
193  DynamicList<label> reverseCellMap_;
194 
195  //- Cells added from point
196  Map<label> cellFromPoint_;
197 
198  //- Cells added from edge
199  Map<label> cellFromEdge_;
200 
201  //- Cells added from face
202  Map<label> cellFromFace_;
203 
204  //- Zone of cell
205  DynamicList<label> cellZone_;
206 
207 
208  // Private Member Functions
209 
210  //- Reorder contents of container according to map
211  template<class T>
212  static void reorder(const labelList& map, DynamicList<T>&);
213  template<class T>
214  static void reorder(const labelList& map, List<DynamicList<T> >&);
215  template<class T>
216  static void renumberKey(const labelList& map, Map<T>&);
217 
218  //- Renumber elements of container according to map
219  static void renumber(const labelList&, labelHashSet&);
220  //- Special handling of reverse maps which have <-1 in them
221  static void renumberReverseMap(const labelList&, DynamicList<label>&);
222 
223  //- Renumber & compact elements of list according to map
224  static void renumberCompact(const labelList&, labelList&);
225 
226  //- Get all set elements as a labelHashSet
227  static labelHashSet getSetIndices(const PackedBoolList&);
228 
229  //- Count number of added and removed quantities from maps.
230  static void countMap
231  (
232  const labelList& map,
233  const labelList& reverseMap,
234  label& nAdd,
235  label& nInflate,
236  label& nMerge,
237  label& nRemove
238  );
239 
240  //- Print some stats about mesh
241  static void writeMeshStats(const polyMesh& mesh, Ostream&);
242 
243  //- Calculate object maps. Requires reverseMap to have destination
244  // to be marked with <-1.
245  static void getMergeSets
246  (
247  const labelList& reverseCellMap,
248  const labelList& cellMap,
249  List<objectMap>& cellsFromCells
250  );
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(const CompactListList<label>&, labelList&) 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  //- Rotate face by number of positions
367  static face rotateFace(const face& f, const label nPos);
368 
369  //- Do all coupled patch face reordering
370  void reorderCoupledFaces
371  (
372  const bool syncParallel,
373  const polyBoundaryMesh&,
374  const labelList& patchStarts,
375  const labelList& patchSizes,
376  const pointField& points
377  );
378 
379  void compactAndReorder
380  (
381  const polyMesh&,
382  const bool syncParallel,
383  const bool orderCells,
384  const bool orderPoints,
385  label& nInternalPoints,
386  pointField& newPoints,
387  labelList& patchSizes,
388  labelList& patchStarts,
389  List<objectMap>& pointsFromPoints,
390  List<objectMap>& facesFromPoints,
391  List<objectMap>& facesFromEdges,
392  List<objectMap>& facesFromFaces,
393  List<objectMap>& cellsFromPoints,
394  List<objectMap>& cellsFromEdges,
395  List<objectMap>& cellsFromFaces,
396  List<objectMap>& cellsFromCells,
397  List<Map<label> >& oldPatchMeshPointMaps,
398  labelList& oldPatchNMeshPoints,
399  labelList& oldPatchStarts,
400  List<Map<label> >& oldFaceZoneMeshPointMaps
401  );
402 
403 public:
404 
405  //- Runtime type information
406  ClassName("polyTopoChange");
407 
408 
409 
410  // Constructors
411 
412  //- Construct without mesh. Either specify nPatches or use
413  // setNumPatches before trying to make a mesh (makeMesh, changeMesh)
414  polyTopoChange(const label nPatches, const bool strict = true);
415 
416  //- Construct from mesh. Adds all points/face/cells from mesh.
417  polyTopoChange(const polyMesh& mesh, const bool strict = true);
418 
419 
420  // Member Functions
421 
422  // Access
423 
424  //- Points. Shrunk after constructing mesh (or calling of compact())
425  const DynamicList<point>& points() const
426  {
427  return points_;
428  }
429 
430  const DynamicList<face>& faces() const
431  {
432  return faces_;
433  }
434 
435  const DynamicList<label>& region() const
436  {
437  return region_;
438  }
439 
441  {
442  return faceOwner_;
443  }
444 
446  {
447  return faceNeighbour_;
448  }
449 
450  //- Is point removed?
451  inline bool pointRemoved(const label pointI) const;
452  //- Is face removed?
453  inline bool faceRemoved(const label faceI) const;
454  //- Is cell removed?
455  inline bool cellRemoved(const label cellI) const;
456 
457 
458  // Edit
459 
460  //- Clear all storage
461  void clear();
462 
463  //- Add all points/faces/cells of mesh. Additional offset for patch
464  // or zone ids.
465  void addMesh
466  (
467  const polyMesh&,
468  const labelList& patchMap,
469  const labelList& pointZoneMap,
470  const labelList& faceZoneMap,
471  const labelList& cellZoneMap
472  );
473 
474  //- Explicitly pre-size the dynamic storage for expected mesh
475  // size for if construct-without-mesh
476  void setCapacity
477  (
478  const label nPoints,
479  const label nFaces,
480  const label nCells
481  );
482 
483  //- Move all points. Incompatible with other topology changes.
484  void movePoints(const pointField& newPoints);
485 
486  //- For compatibility with polyTopoChange: set topological action.
487  label setAction(const topoAction& action);
488 
489  //- Add point. Return new point label.
490  // Notes:
491  // - masterPointID can be < 0 (appended points)
492  // - inCell = false: add retired point (to end of point list)
493  label addPoint
494  (
495  const point&,
496  const label masterPointID,
497  const label zoneID,
498  const bool inCell
499  );
500 
501  //- Modify coordinate.
502  // Notes:
503  // - inCell = false: add retired point (to end of point list)
504  void modifyPoint
505  (
506  const label,
507  const point&,
508  const label newZoneID,
509  const bool inCell
510  );
511 
512  //- Remove/merge point.
513  void removePoint(const label, const label);
514 
515  //- Add face to cells. Return new face label.
516  // own,nei<0, zoneID>=0 : add inactive face (to end of face list)
517  label addFace
518  (
519  const face& f,
520  const label own,
521  const label nei,
522  const label masterPointID,
523  const label masterEdgeID,
524  const label masterFaceID,
525  const bool flipFaceFlux,
526  const label patchID,
527  const label zoneID,
528  const bool zoneFlip
529  );
530 
531  //- Modify vertices or cell of face.
532  void modifyFace
533  (
534  const face& f,
535  const label faceI,
536  const label own,
537  const label nei,
538  const bool flipFaceFlux,
539  const label patchID,
540  const label zoneID,
541  const bool zoneFlip
542  );
543 
544  //- Remove/merge face.
545  void removeFace(const label, const label);
546 
547  //- Add cell. Return new cell label.
548  label addCell
549  (
550  const label masterPointID,
551  const label masterEdgeID,
552  const label masterFaceID,
553  const label masterCellID,
554  const label zoneID
555  );
556 
557  //- Modify zone of cell
558  void modifyCell(const label, const label zoneID);
559 
560  //- Remove/merge cell.
561  void removeCell(const label, const label);
562 
563  //- Explicitly set the number of patches if construct-without-mesh
564  // used.
565  inline void setNumPatches(const label nPatches);
566 
567 
568  // Other
569 
570  //- Inplace changes mesh without change of patches.
571  // Adapts patch start/end and by default does parallel matching.
572  // Clears all data. Returns map.
573  // inflate = true : keep old mesh points. Put new points into the
574  // returned map (preMotionPoints) so we can use inflation. Any
575  // points out of nothing (appended points) are vector::zero.
576  // inflate = false: set mesh points directly. Empty preMotionPoints
577  // in the map.
578  // orderCells : whether to order the cells (see bandCompression.H)
579  // orderPoints : whether to order the points into internal first
580  // followed by boundary points. This is not fully consistent
581  // with upper-triangular ordering of points and edges so
582  // is only done when explicitly asked for.
584  (
585  polyMesh& mesh,
586  const bool inflate,
587  const bool syncParallel = true,
588  const bool orderCells = false,
589  const bool orderPoints = false
590  );
591 
592  //- Create new mesh with old mesh patches
594  (
595  autoPtr<fvMesh>& newMesh,
596  const IOobject& io,
597  const fvMesh& mesh,
598  const bool syncParallel = true,
599  const bool orderCells = false,
600  const bool orderPoints = false
601  );
602 
603 };
604 
605 
606 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
607 
608 } // End namespace Foam
609 
610 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
611 
612 #include "polyTopoChangeI.H"
613 
614 #ifdef NoRepository
615 # include "polyTopoChangeTemplates.C"
616 #endif
617 
618 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
619 
620 #endif
621 
622 // ************************ vim: set sw=4 sts=4 et: ************************ //