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

autoPatch.C

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 Description
00026     Divides external faces into patches based on (user supplied) feature
00027     angle.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "argList.H"
00032 #include "polyMesh.H"
00033 #include "Time.H"
00034 #include "boundaryMesh.H"
00035 #include "repatchPolyTopoChanger.H"
00036 #include "mathematicalConstants.H"
00037 #include "OFstream.H"
00038 #include "ListOps.H"
00039 
00040 using namespace Foam;
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 // Get all feature edges.
00045 void collectFeatureEdges(const boundaryMesh& bMesh, labelList& markedEdges)
00046 {
00047     markedEdges.setSize(bMesh.mesh().nEdges());
00048 
00049     label markedI = 0;
00050 
00051     forAll(bMesh.featureSegments(), i)
00052     {
00053         const labelList& segment = bMesh.featureSegments()[i];
00054 
00055         forAll(segment, j)
00056         {
00057             label featEdgeI = segment[j];
00058 
00059             label meshEdgeI = bMesh.featureToEdge()[featEdgeI];
00060 
00061             markedEdges[markedI++] = meshEdgeI;
00062         }
00063     }
00064     markedEdges.setSize(markedI);
00065 }
00066 
00067 
00068 // Main program:
00069 
00070 int main(int argc, char *argv[])
00071 {
00072     argList::noParallel();
00073     argList::validArgs.append("feature angle[0-180]");
00074     argList::validOptions.insert("overwrite", "");
00075 
00076 #   include "setRootCase.H"
00077 #   include "createTime.H"
00078     runTime.functionObjects().off();
00079 #   include "createPolyMesh.H"
00080     const word oldInstance = mesh.pointsInstance();
00081 
00082     Info<< "Mesh read in = "
00083         << runTime.cpuTimeIncrement()
00084         << " s\n" << endl << endl;
00085 
00086 
00087     //
00088     // Use boundaryMesh to reuse all the featureEdge stuff in there.
00089     //
00090 
00091     boundaryMesh bMesh;
00092 
00093     scalar featureAngle(readScalar(IStringStream(args.additionalArgs()[0])()));
00094     bool overwrite = args.optionFound("overwrite");
00095 
00096     scalar minCos = Foam::cos(featureAngle * mathematicalConstant::pi/180.0);
00097 
00098     Info<< "Feature:" << featureAngle << endl
00099         << "minCos :" << minCos << endl
00100         << endl;
00101 
00102     bMesh.read(mesh);
00103 
00104     // Set feature angle (calculate feature edges)
00105     bMesh.setFeatureEdges(minCos);
00106 
00107     // Collect all feature edges as edge labels
00108     labelList markedEdges;
00109 
00110     collectFeatureEdges(bMesh, markedEdges);
00111 
00112 
00113 
00114     // (new) patch ID for every face in mesh.
00115     labelList patchIDs(bMesh.mesh().size(), -1);
00116 
00117     //
00118     // Fill patchIDs with values for every face by floodfilling without
00119     // crossing feature edge.
00120     //
00121 
00122     // Current patch number.
00123     label newPatchI = bMesh.patches().size();
00124 
00125     label suffix = 0;
00126 
00127     while (true)
00128     {
00129         // Find first unset face.
00130         label unsetFaceI = findIndex(patchIDs, -1);
00131 
00132         if (unsetFaceI == -1)
00133         {
00134             // All faces have patchID set. Exit.
00135             break;
00136         }
00137 
00138         // Found unset face. Create patch for it.
00139         word patchName;
00140         do
00141         {
00142             patchName = "auto" + name(suffix++);
00143         }
00144         while (bMesh.findPatchID(patchName) != -1);
00145 
00146         bMesh.addPatch(patchName);
00147 
00148         bMesh.changePatchType(patchName, "patch");
00149 
00150 
00151         // Fill visited with all faces reachable from unsetFaceI.
00152         boolList visited(bMesh.mesh().size());
00153 
00154         bMesh.markFaces(markedEdges, unsetFaceI, visited);
00155 
00156 
00157         // Assign all visited faces to current patch
00158         label nVisited = 0;
00159 
00160         forAll(visited, faceI)
00161         {
00162             if (visited[faceI])
00163             {
00164                 nVisited++;
00165 
00166                 patchIDs[faceI] = newPatchI;
00167             }
00168         }
00169 
00170         Info<< "Assigned " << nVisited << " faces to patch " << patchName
00171             << endl << endl;
00172 
00173         newPatchI++;
00174     }
00175 
00176 
00177 
00178     const PtrList<boundaryPatch>& patches = bMesh.patches();
00179 
00180     // Create new list of patches with old ones first
00181     List<polyPatch*> newPatchPtrList(patches.size());
00182 
00183     newPatchI = 0;
00184 
00185     // Copy old patches
00186     forAll(mesh.boundaryMesh(), patchI)
00187     {
00188         const polyPatch& patch = mesh.boundaryMesh()[patchI];
00189 
00190         newPatchPtrList[newPatchI] =
00191             patch.clone
00192             (
00193                 mesh.boundaryMesh(),
00194                 newPatchI,
00195                 patch.size(),
00196                 patch.start()
00197             ).ptr();
00198 
00199         newPatchI++;
00200     }
00201 
00202     // Add new ones with empty size.
00203     for (label patchI = newPatchI; patchI < patches.size(); patchI++)
00204     {
00205         const boundaryPatch& bp = patches[patchI];
00206 
00207         newPatchPtrList[newPatchI] = polyPatch::New
00208         (
00209             polyPatch::typeName,
00210             bp.name(),
00211             0,
00212             mesh.nFaces(),
00213             newPatchI,
00214             mesh.boundaryMesh()
00215         ).ptr();
00216 
00217         newPatchI++;
00218     }
00219 
00220     if (!overwrite)
00221     {
00222         runTime++;
00223     }
00224 
00225 
00226     // Change patches
00227     repatchPolyTopoChanger polyMeshRepatcher(mesh);
00228     polyMeshRepatcher.changePatches(newPatchPtrList);
00229 
00230 
00231     // Change face ordering
00232 
00233     // Since bMesh read from mesh there is one to one mapping so we don't
00234     // have to do the geometric stuff.
00235     const labelList& meshFace = bMesh.meshFace();
00236 
00237     forAll(patchIDs, faceI)
00238     {
00239         label meshFaceI = meshFace[faceI];
00240 
00241         polyMeshRepatcher.changePatchID(meshFaceI, patchIDs[faceI]);
00242     }
00243 
00244     polyMeshRepatcher.repatch();
00245 
00246     // Write resulting mesh
00247     if (overwrite)
00248     {
00249         mesh.setInstance(oldInstance);
00250     }
00251     mesh.write();
00252 
00253 
00254     Info<< "End\n" << endl;
00255 
00256     return 0;
00257 }
00258 
00259 
00260 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd