CV2D.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) 2013-2015 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::CV2D
26 
27 Description
28  Conformal-Voronoi 2D automatic mesher with grid or read initial points
29  and point position relaxation with optional "squarification".
30 
31  There are a substantial number of options to this mesher read from
32  CV2DMesherDict file e.g.:
33 
34  // Min cell size used in tolerances when inserting points for
35  // boundary conforming.
36  // Also used to as the grid spacing usind in insertGrid.
37  minCellSize 0.05;
38 
39  // Feature angle used to inser feature points
40  // 0 = all features, 180 = no features
41  featureAngle 45;
42 
43  // Maximum quadrant angle allowed at a concave corner before
44  // additional "mitering" lines are added
45  maxQuadAngle 110;
46 
47  // Should the mesh be square-dominated or of unbiased hexagons
48  squares yes;
49 
50  // Near-wall region where cells are aligned with the wall specified as a
51  // number of cell layers
52  nearWallAlignedDist 3;
53 
54  // Chose if the cell orientation should relax during the iterations
55  // or remain fixed to the x-y directions
56  relaxOrientation no;
57 
58  // Insert near-boundary point mirror or point-pairs
59  insertSurfaceNearestPointPairs yes;
60 
61  // Mirror near-boundary points rather than insert point-pairs
62  mirrorPoints no;
63 
64  // Insert point-pairs vor dual-cell vertices very near the surface
65  insertSurfaceNearPointPairs yes;
66 
67  // Choose if to randomise the initial grid created by insertGrid.
68  randomiseInitialGrid yes;
69 
70  // Perturbation fraction, 1 = cell-size.
71  randomPurturbation 0.1;
72 
73  // Number of relaxation iterations.
74  nIterations 5;
75 
76  // Relaxation factor at the start of the iteration sequence.
77  // 0.5 is a sensible maximum and < 0.2 converges better.
78  relaxationFactorStart 0.8;
79 
80  // Relaxation factor at the end of the iteration sequence.
81  // Should be <= relaxationFactorStart
82  relaxationFactorEnd 0;
83 
84  writeInitialTriangulation no;
85  writeFeatureTriangulation no;
86  writeNearestTriangulation no;
87  writeInsertedPointPairs no;
88  writeFinalTriangulation yes;
89 
90  // Maximum number of iterations used in boundaryConform.
91  maxBoundaryConformingIter 5;
92 
93  minEdgeLenCoeff 0.5;
94  maxNotchLenCoeff 0.3;
95  minNearPointDistCoeff 0.25;
96  ppDistCoeff 0.05;
97 
98 SourceFiles
99  CGALTriangulation2Ddefs.H
100  indexedVertex.H
101  indexedFace.H
102  CV2DI.H
103  CV2D.C
104  CV2DIO.C
105  tolerances.C
106  controls.C
107  insertFeaturePoints.C
108  insertSurfaceNearestPointPairs.C
109  insertSurfaceNearPointPairs.C
110  insertBoundaryConformPointPairs.C
111 
112 \*---------------------------------------------------------------------------*/
113 
114 #ifndef CV2D_H
115 #define CV2D_H
117 #define CGAL_INEXACT
118 #define CGAL_HIERARCHY
119 
120 // Include uint.H before CGAL headers to define __STDC_LIMIT_MACROS
121 #include "uint.H"
122 #include "CGALTriangulation2Ddefs.H"
123 #include "Time.H"
124 #include "point2DFieldFwd.H"
125 #include "dictionary.H"
126 #include "Switch.H"
127 #include "PackedBoolList.H"
128 #include "EdgeMap.H"
129 #include "cv2DControls.H"
130 #include "tolerances.H"
131 #include "meshTools.H"
132 #include "triSurface.H"
133 #include "searchableSurfaces.H"
134 #include "conformationSurfaces.H"
135 #include "relaxationModel.H"
137 
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
139 
140 namespace Foam
141 {
142 
143 /*---------------------------------------------------------------------------*\
144  Class CV2D Declaration
145 \*---------------------------------------------------------------------------*/
147 class CV2D
148 :
149  public Delaunay
150 {
151 
152 private:
153 
154  // Private data
155 
156  //- The time registry of the application
157  const Time& runTime_;
158 
159  mutable Random rndGen_;
160 
161  //- The surface to mesh
162  //const querySurface& qSurf_;
163  //- All geometry of the meshing process, including surfaces to be
164  // conformed to and those to be used for refinement
165  searchableSurfaces allGeometry_;
166 
167  conformationSurfaces qSurf_;
168 
169  //- Meshing controls
170  cv2DControls controls_;
171 
172  //- The cell size control object
173  cellSizeAndAlignmentControls cellSizeControl_;
174 
175  //- Relaxation coefficient model. Runtime selectable.
176  autoPtr<relaxationModel> relaxationModel_;
177 
178  //- z-level
179  scalar z_;
180 
181  //- Keep track of the start of the internal points
182  label startOfInternalPoints_;
183 
184  //- Keep track of the start of the surface point-pairs
185  label startOfSurfacePointPairs_;
186 
187  //- Keep track of the boundary conform point-pairs
188  // stored after the insertion of the surface point-pairs in case
189  // the boundary conform function is called more than once without
190  // removing and insertin the surface point-pairs
191  label startOfBoundaryConformPointPairs_;
192 
193  //- Store the feature points
194  std::list<Vb> featurePoints_;
195 
196  //- Temporary storage for a dual-cell
197  static const label maxNvert = 20;
198  mutable point2D vertices[maxNvert+1];
199  mutable vector2D edges[maxNvert+1];
200 
201 
202  // Private Member Functions
203 
204  //- Disallow default bitwise copy construct
205  CV2D(const CV2D&);
206 
207  //- Disallow default bitwise assignment
208  void operator=(const CV2D&);
209 
210 
211  //- Insert point and return it's index
212  inline label insertPoint
213  (
214  const point2D& pt,
215  const label type
216  );
217 
218  //- Insert point and return it's index
219  inline label insertPoint
220  (
221  const point2D& pt,
222  const label index,
223  const label type
224  );
225 
226  inline label insertPoint
227  (
228  const Point& p,
229  const label index,
230  const label type
231  );
232 
233  inline bool insertMirrorPoint
234  (
235  const point2D& nearSurfPt,
236  const point2D& surfPt
237  );
238 
239  //- Insert a point-pair at a distance ppDist either side of
240  // surface point point surfPt in the direction n
241  inline void insertPointPair
242  (
243  const scalar mirrorDist,
244  const point2D& surfPt,
245  const vector2D& n
246  );
247 
248  //- Create the initial mesh from the bounding-box
249  void insertBoundingBox();
250 
251  //- Check if a point is within a line.
252  bool on2DLine(const point2D& p, const linePointRef& line);
253 
254  //- Insert point groups at the feature points.
255  void insertFeaturePoints();
256 
257  //- Re-insert point groups at the feature points.
258  void reinsertFeaturePoints();
259 
260  //- Insert point-pairs at the given set of points using the surface
261  // normals corresponding to the given set of surface triangles
262  // and write the inserted point locations to the given file.
263  void insertPointPairs
264  (
265  const DynamicList<point2D>& nearSurfacePoints,
266  const DynamicList<point2D>& surfacePoints,
267  const DynamicList<label>& surfaceTris,
268  const DynamicList<label>& surfaceHits,
269  const fileName fName
270  );
271 
272  //- Check to see if dual cell specified by given vertex iterator
273  // intersects the boundary and hence reqires a point-pair.
274  bool dualCellSurfaceIntersection
275  (
276  const Triangulation::Finite_vertices_iterator& vit
277  ) const;
278 
279  //- Insert point-pairs at the nearest points on the surface to the
280  // control vertex of dual-cells which intersect the boundary in order
281  // to provide a boundary-layer mesh.
282  // NB: This is not guaranteed to close the boundary
283  void insertSurfaceNearestPointPairs();
284 
285  //- Insert point-pairs at small dual-cell edges on the surface in order
286  // to improve the boundary-layer mesh generated by
287  // insertSurfaceNearestPointPairs.
288  void insertSurfaceNearPointPairs();
289 
290  //- Insert point-pair and correcting the Finite_vertices_iterator
291  // to account for the additional vertices
292  void insertPointPair
293  (
294  Triangulation::Finite_vertices_iterator& vit,
295  const point2D& p,
296  const label trii,
297  const label hitSurface
298  );
299 
300  //- Insert point-pair at the best intersection point between the lines
301  // from the dual-cell real centroid and it's vertices and the surface.
302  bool insertPointPairAtIntersection
303  (
304  Triangulation::Finite_vertices_iterator& vit,
305  const point2D& defVert,
306  const point2D vertices[],
307  const scalar maxProtSize
308  );
309 
310  //- Insert point-pairs corresponding to dual-cells which intersect
311  // the boundary surface
312  label insertBoundaryConformPointPairs(const fileName& fName);
313 
314  void markNearBoundaryPoints();
315 
316  //- Restore the Delaunay contraint
317  void fast_restore_Delaunay(Vertex_handle vh);
318 
319  // Flip operations used by fast_restore_Delaunay
320  void external_flip(Face_handle& f, int i);
321  bool internal_flip(Face_handle& f, int i);
322 
323  //- Write all the faces and all the triangles at a particular stage.
324  void write(const word& stage) const;
325 
326 
327 public:
328 
329  //- Runtime type information
330  ClassName("CV2D");
331 
332 
333  // Constructors
334 
335  //- Construct for given surface
336  CV2D(const Time& runTime, const dictionary& controlDict);
337 
338 
339  //- Destructor
340  ~CV2D();
341 
342 
343  // Member Functions
344 
345  // Access
346 
347  inline const cv2DControls& meshControls() const;
348 
349 
350  // Conversion functions between point2D, point and Point
351 
352  inline const point2D& toPoint2D(const point&) const;
353  inline const point2DField toPoint2D(const pointField&) const;
354  inline point toPoint3D(const point2D&) const;
355 
356  #ifdef CGAL_INEXACT
357  typedef const point2D& point2DFromPoint;
358  typedef const Point& PointFromPoint2D;
359  #else
360  typedef point2D point2DFromPoint;
361  typedef Point PointFromPoint2D;
362  #endif
363 
364  inline point2DFromPoint toPoint2D(const Point&) const;
365  inline PointFromPoint2D toPoint(const point2D&) const;
366  inline point toPoint3D(const Point&) const;
367 
368 
369  // Point insertion
370 
371  //- Create the initial mesh from the given internal points.
372  // Points must be inside the boundary by at least nearness
373  // otherwise they are ignored.
374  void insertPoints
375  (
376  const point2DField& points,
377  const scalar nearness
378  );
379 
380  //- Create the initial mesh from the internal points in the given
381  // file. Points outside the geometry are ignored.
382  void insertPoints(const fileName& pointFileName);
383 
384  //- Create the initial mesh as a regular grid of points.
385  // Points outside the geometry are ignored.
386  void insertGrid();
387 
388  //- Insert all surface point-pairs from
389  // insertSurfaceNearestPointPairs and
390  // findIntersectionForOutsideCentroid
392 
393  //- Insert point-pairs where there are protrusions into
394  // or out of the surface
395  void boundaryConform();
396 
397 
398  // Point removal
399 
400  //- Remove the point-pairs introduced by insertSurfacePointPairs
401  // and boundaryConform
403 
404 
405  // Point motion
406 
407  inline void movePoint(const Vertex_handle& vh, const Point& P);
408 
409  //- Move the internal points to the given new locations and update
410  // the triangulation to ensure it is Delaunay
411  // void moveInternalPoints(const point2DField& newPoints);
412 
413  //- Calculate the displacements to create the new points
414  void newPoints();
415 
416  //- Extract patch names and sizes.
417  void extractPatches
418  (
420  labelList& patchSizes,
421  EdgeMap<label>& mapEdgesRegion,
422  EdgeMap<label>& indirectPatchEdge
423  ) const;
424 
425 
426  // Write
427 
428  //- Write internal points to .obj file
429  void writePoints(const fileName& fName, bool internalOnly) const;
430 
431  //- Write triangles as .obj file
432  void writeTriangles(const fileName& fName, bool internalOnly) const;
433 
434  //- Write dual faces as .obj file
435  void writeFaces(const fileName& fName, bool internalOnly) const;
436 
437  //- Calculates dual points (circumcentres of tets) and faces
438  // (point-cell walk of tets).
439  // Returns:
440  // - dualPoints (in triangle ordering)
441  // - dualFaces (compacted)
442  void calcDual
443  (
444  point2DField& dualPoints,
445  faceList& dualFaces,
447  labelList& patchSizes,
448  EdgeMap<label>& mapEdgesRegion,
449  EdgeMap<label>& indirectPatchEdge
450  ) const;
451 
452  //- Write patch
453  void writePatch(const fileName& fName) const;
454 
455  void write() const;
456 };
457 
458 
459 inline bool boundaryTriangle(const CV2D::Face_handle fc);
460 inline bool outsideTriangle(const CV2D::Face_handle fc);
461 
462 
463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464 
465 } // End namespace Foam
466 
467 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
468 
469 #include "CV2DI.H"
470 
471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
472 
473 #endif
474 
475 // ************************************************************************* //
CGAL::Delaunay_triangulation_3< K, Tds, CompactLocator > Delaunay
void writeFaces(const fileName &fName, bool internalOnly) const
Write dual faces as .obj file.
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
A line primitive.
Definition: line.H:56
A class for handling file names.
Definition: fileName.H:69
runTime controlDict().lookup("adjustTimeStep") >> adjustTimeStep
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
~CV2D()
Destructor.
void insertSurfacePointPairs()
Insert all surface point-pairs from.
bool outsideTriangle(const CV2D::Face_handle fc)
Definition: CV2DI.H:216
point toPoint3D(const point2D &) const
Definition: CV2DI.H:141
void newPoints()
Move the internal points to the given new locations and update.
const cv2DControls & meshControls() const
Definition: CV2DI.H:118
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
void write() const
void insertGrid()
Create the initial mesh as a regular grid of points.
void calcDual(point2DField &dualPoints, faceList &dualFaces, wordList &patchNames, labelList &patchSizes, EdgeMap< label > &mapEdgesRegion, EdgeMap< label > &indirectPatchEdge) const
Calculates dual points (circumcentres of tets) and faces.
const pointField & points
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
Pre-declare SubField and related Field type.
Definition: Field.H:57
System uinteger.
A class for handling words, derived from string.
Definition: word.H:59
void writePatch(const fileName &fName) const
Write patch.
wordList patchNames(nPatches)
Container for searchableSurfaces.
const point2D & toPoint2D(const point &) const
Definition: CV2DI.H:124
void writeTriangles(const fileName &fName, bool internalOnly) const
Write triangles as .obj file.
Simple random number generator.
Definition: Random.H:49
const point2D & point2DFromPoint
Definition: CV2D.H:356
void insertPoints(const point2DField &points, const scalar nearness)
Create the initial mesh from the given internal points.
void extractPatches(wordList &patchNames, labelList &patchSizes, EdgeMap< label > &mapEdgesRegion, EdgeMap< label > &indirectPatchEdge) const
Extract patch names and sizes.
labelList f(nPoints)
Controls for the 2D CV mesh generator.
Definition: cv2DControls.H:58
ClassName("CV2D")
Runtime type information.
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:461
void movePoint(const Vertex_handle &vh, const Point &P)
Definition: CV2DI.H:182
PointFromPoint2D toPoint(const point2D &) const
Definition: CV2DI.H:168
label n
void writePoints(const fileName &fName, bool internalOnly) const
Write internal points to .obj file.
Conformal-Voronoi 2D automatic mesher with grid or read initial points and point position relaxation ...
Definition: CV2D.H:146
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
bool boundaryTriangle(const CV2D::Face_handle fc)
Definition: CV2DI.H:205
CGAL data structures used for 2D Delaunay meshing.
volScalarField & p
const Point & PointFromPoint2D
Definition: CV2D.H:357
void removeSurfacePointPairs()
Remove the point-pairs introduced by insertSurfacePointPairs.
void boundaryConform()
Insert point-pairs where there are protrusions into.
Namespace for OpenFOAM.