1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
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
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
26 Divides external faces into patches based on (user supplied) feature
29 \*---------------------------------------------------------------------------*/
34 #include "boundaryMesh.H"
35 #include "repatchPolyTopoChanger.H"
36 #include "mathematicalConstants.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 // Get all feature edges.
45 void collectFeatureEdges(const boundaryMesh& bMesh, labelList& markedEdges)
47 markedEdges.setSize(bMesh.mesh().nEdges());
51 forAll(bMesh.featureSegments(), i)
53 const labelList& segment = bMesh.featureSegments()[i];
57 label featEdgeI = segment[j];
59 label meshEdgeI = bMesh.featureToEdge()[featEdgeI];
61 markedEdges[markedI++] = meshEdgeI;
64 markedEdges.setSize(markedI);
70 int main(int argc, char *argv[])
72 argList::noParallel();
73 argList::validArgs.append("feature angle[0-180]");
74 argList::validOptions.insert("overwrite", "");
76 # include "setRootCase.H"
77 # include "createTime.H"
78 runTime.functionObjects().off();
79 # include "createPolyMesh.H"
80 const word oldInstance = mesh.pointsInstance();
82 Info<< "Mesh read in = "
83 << runTime.cpuTimeIncrement()
84 << " s\n" << endl << endl;
88 // Use boundaryMesh to reuse all the featureEdge stuff in there.
93 scalar featureAngle(readScalar(IStringStream(args.additionalArgs()[0])()));
94 bool overwrite = args.optionFound("overwrite");
96 scalar minCos = Foam::cos(featureAngle * mathematicalConstant::pi/180.0);
98 Info<< "Feature:" << featureAngle << endl
99 << "minCos :" << minCos << endl
104 // Set feature angle (calculate feature edges)
105 bMesh.setFeatureEdges(minCos);
107 // Collect all feature edges as edge labels
108 labelList markedEdges;
110 collectFeatureEdges(bMesh, markedEdges);
114 // (new) patch ID for every face in mesh.
115 labelList patchIDs(bMesh.mesh().size(), -1);
118 // Fill patchIDs with values for every face by floodfilling without
119 // crossing feature edge.
122 // Current patch number.
123 label newPatchI = bMesh.patches().size();
129 // Find first unset face.
130 label unsetFaceI = findIndex(patchIDs, -1);
132 if (unsetFaceI == -1)
134 // All faces have patchID set. Exit.
138 // Found unset face. Create patch for it.
142 patchName = "auto" + name(suffix++);
144 while (bMesh.findPatchID(patchName) != -1);
146 bMesh.addPatch(patchName);
148 bMesh.changePatchType(patchName, "patch");
151 // Fill visited with all faces reachable from unsetFaceI.
152 boolList visited(bMesh.mesh().size());
154 bMesh.markFaces(markedEdges, unsetFaceI, visited);
157 // Assign all visited faces to current patch
160 forAll(visited, faceI)
166 patchIDs[faceI] = newPatchI;
170 Info<< "Assigned " << nVisited << " faces to patch " << patchName
178 const PtrList<boundaryPatch>& patches = bMesh.patches();
180 // Create new list of patches with old ones first
181 List<polyPatch*> newPatchPtrList(patches.size());
186 forAll(mesh.boundaryMesh(), patchI)
188 const polyPatch& patch = mesh.boundaryMesh()[patchI];
190 newPatchPtrList[newPatchI] =
202 // Add new ones with empty size.
203 for (label patchI = newPatchI; patchI < patches.size(); patchI++)
205 const boundaryPatch& bp = patches[patchI];
207 newPatchPtrList[newPatchI] = polyPatch::New
227 repatchPolyTopoChanger polyMeshRepatcher(mesh);
228 polyMeshRepatcher.changePatches(newPatchPtrList);
231 // Change face ordering
233 // Since bMesh read from mesh there is one to one mapping so we don't
234 // have to do the geometric stuff.
235 const labelList& meshFace = bMesh.meshFace();
237 forAll(patchIDs, faceI)
239 label meshFaceI = meshFace[faceI];
241 polyMeshRepatcher.changePatchID(meshFaceI, patchIDs[faceI]);
244 polyMeshRepatcher.repatch();
246 // Write resulting mesh
249 mesh.setInstance(oldInstance);
254 Info<< "End\n" << endl;
260 // ************************************************************************* //