FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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