initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / autoMesh / autoHexMesh / meshRefinement / meshRefinementMerge.C
blobd8ddb69bcece7841e39e71751245cc21efd5d5ee
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 \*----------------------------------------------------------------------------*/
27 #include "meshRefinement.H"
28 #include "combineFaces.H"
29 #include "polyTopoChange.H"
30 #include "removePoints.H"
33 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
36 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
38 // Merge faces that are in-line.
39 Foam::label Foam::meshRefinement::mergePatchFaces
41     const scalar minCos,
42     const scalar concaveCos,
43     const labelList& patchIDs
46     // Patch face merging engine
47     combineFaces faceCombiner(mesh_);
49     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
51     // Pick up all candidate cells on boundary
52     labelHashSet boundaryCells(mesh_.nFaces()-mesh_.nInternalFaces());
54     forAll(patchIDs, i)
55     {
56         label patchI = patchIDs[i];
58         const polyPatch& patch = patches[patchI];
60         if (!patch.coupled())
61         {
62             forAll(patch, i)
63             {
64                 boundaryCells.insert(mesh_.faceOwner()[patch.start()+i]);
65             }
66         }
67     }
69     // Get all sets of faces that can be merged
70     labelListList mergeSets
71     (
72         faceCombiner.getMergeSets
73         (
74             minCos,
75             concaveCos,
76             boundaryCells
77         )
78     );
80     label nFaceSets = returnReduce(mergeSets.size(), sumOp<label>());
82     Info<< "mergePatchFaces : Merging " << nFaceSets
83         << " sets of faces." << endl;
85     if (nFaceSets > 0)
86     {
87         // Topology changes container
88         polyTopoChange meshMod(mesh_);
90         // Merge all faces of a set into the first face of the set. Remove
91         // unused points.
92         faceCombiner.setRefinement(mergeSets, meshMod);
94         // Change the mesh (no inflation)
95         autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh_, false, true);
97         // Update fields
98         mesh_.updateMesh(map);
100         // Move mesh (since morphing does not do this)
101         if (map().hasMotionPoints())
102         {
103             mesh_.movePoints(map().preMotionPoints());
104         }
105         else
106         {
107             // Delete mesh volumes. No other way to do this?
108             mesh_.clearOut();
109         }
111         faceCombiner.updateMesh(map);
113         // Get the kept faces that need to be recalculated.
114         // Merging two boundary faces might shift the cell centre
115         // (unless the faces are absolutely planar)
116         labelHashSet retestFaces(6*mergeSets.size());
118         forAll(mergeSets, setI)
119         {
120             label oldMasterI = mergeSets[setI][0];
122             label faceI = map().reverseFaceMap()[oldMasterI];
124             // faceI is always uncoupled boundary face
125             const cell& cFaces = mesh_.cells()[mesh_.faceOwner()[faceI]];
127             forAll(cFaces, i)
128             {
129                 retestFaces.insert(cFaces[i]);
130             }
131         }
132         updateMesh(map, retestFaces.toc());
133     }
136     return nFaceSets;
140 // Remove points not used by any face or points used by only two faces where
141 // the edges are in line
142 Foam::autoPtr<Foam::mapPolyMesh> Foam::meshRefinement::mergeEdges
144     const scalar minCos
147     // Point removal analysis engine
148     removePoints pointRemover(mesh_);
150     // Count usage of points
151     boolList pointCanBeDeleted;
152     label nRemove = pointRemover.countPointUsage(minCos, pointCanBeDeleted);
154     Info<< "Removing " << nRemove
155         << " straight edge points." << endl;
157     autoPtr<mapPolyMesh> map;
159     if (nRemove > 0)
160     {
161         // Save my local faces that will change. These changed faces might
162         // cause a shift in the cell centre which needs to be retested.
163         // Have to do this before changing mesh since point will be removed.
164         labelHashSet retestOldFaces(nRemove / Pstream::nProcs());
166         {
167             const faceList& faces = mesh_.faces();
169             forAll(faces, faceI)
170             {
171                 const face& f = faces[faceI];
173                 forAll(f, fp)
174                 {
175                     if (pointCanBeDeleted[f[fp]])
176                     {
177                         retestOldFaces.insert(faceI);
178                         break;
179                     }
180                 }
181             }
182         }
184         // Topology changes container
185         polyTopoChange meshMod(mesh_);
187         pointRemover.setRefinement(pointCanBeDeleted, meshMod);
189         // Change the mesh (no inflation)
190         map = meshMod.changeMesh(mesh_, false, true);
192         // Update fields
193         mesh_.updateMesh(map);
195         // Move mesh (since morphing does not do this)
196         if (map().hasMotionPoints())
197         {
198             mesh_.movePoints(map().preMotionPoints());
199         }
200         else
201         {
202             // Delete mesh volumes. No other way to do this?
203             mesh_.clearOut();
204         }
206         pointRemover.updateMesh(map);
208         // Get the kept faces that need to be recalculated.
209         labelHashSet retestFaces(6*retestOldFaces.size());
211         const cellList& cells = mesh_.cells();
213         forAllConstIter(labelHashSet, retestOldFaces, iter)
214         {
215             label faceI = map().reverseFaceMap()[iter.key()];
217             const cell& ownFaces = cells[mesh_.faceOwner()[faceI]];
219             forAll(ownFaces, i)
220             {
221                 retestFaces.insert(ownFaces[i]);
222             }
224             if (mesh_.isInternalFace(faceI))
225             {
226                 const cell& neiFaces = cells[mesh_.faceNeighbour()[faceI]];
228                 forAll(neiFaces, i)
229                 {
230                     retestFaces.insert(neiFaces[i]);
231                 }
232             }
233         }
234         updateMesh(map, retestFaces.toc());
235     }
237     return map;
241 // ************************************************************************* //