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

surfaceMeshTriangulate.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     Extracts triSurface from a polyMesh. Triangulates all boundary faces.
00027     Region numbers on triangles are the patch numbers of the polyMesh.
00028     Optionally only triangulates named patches.
00029 
00030     If run in parallel the processor patches get filtered out by default and
00031     the mesh gets merged. (based on vertex position, not topology, so might go
00032     wrong!).
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #include "argList.H"
00037 #include "Time.H"
00038 #include "polyMesh.H"
00039 #include "triSurface.H"
00040 #include "triSurfaceTools.H"
00041 #include "processorPolyPatch.H"
00042 #include "ListListOps.H"
00043 
00044 using namespace Foam;
00045 
00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00047 
00048 // Main program:
00049 
00050 int main(int argc, char *argv[])
00051 {
00052     argList::validArgs.append("output file");
00053 #   include "addRegionOption.H"
00054     argList::validOptions.insert("excludeProcPatches", "");
00055     argList::validOptions.insert("patches", "(patch0 .. patchN)");
00056 
00057 #   include "setRootCase.H"
00058 #   include "createTime.H"
00059 
00060     fileName outFileName(runTime.path()/args.additionalArgs()[0]);
00061 
00062     Info<< "Extracting triSurface from boundaryMesh ..."
00063         << endl << endl;
00064 
00065     Pout<< "Reading mesh from time " << runTime.value() << endl;
00066 
00067 #   include "createNamedPolyMesh.H"
00068 
00069     bool includeProcPatches =
00070        !(
00071             args.optionFound("excludeProcPatches")
00072          || Pstream::parRun()
00073         );
00074 
00075     // Create local surface from:
00076     // - explicitly named patches only (-patches option)
00077     // - all patches (default in sequential mode)
00078     // - all non-processor patches (default in parallel mode)
00079     // - all non-processor patches (sequential mode, -excludeProcPatches option)
00080 
00081     // Construct table of patches to include.
00082     const polyBoundaryMesh& bMesh = mesh.boundaryMesh();
00083 
00084     labelHashSet includePatches(bMesh.size());
00085 
00086     if (args.optionFound("patches"))
00087     {
00088         wordList patchNames(args.optionLookup("patches")());
00089 
00090         forAll(patchNames, patchNameI)
00091         {
00092             const word& patchName = patchNames[patchNameI];
00093 
00094             label patchI = bMesh.findPatchID(patchName);
00095 
00096             if (patchI == -1)
00097             {
00098                 FatalErrorIn(args.executable()) << "No such patch "
00099                     << patchName << endl << "Patches are " << bMesh.names()
00100                     << exit(FatalError);
00101             }
00102             includePatches.insert(patchI);
00103         }
00104     }
00105     else
00106     {
00107         forAll(bMesh, patchI)
00108         {
00109             const polyPatch& patch = bMesh[patchI];
00110 
00111             if
00112             (
00113                 includeProcPatches
00114              || (patch.type() != processorPolyPatch::typeName)
00115             )
00116             {
00117                 includePatches.insert(patchI);
00118             }
00119             else
00120             {
00121                 Pout<< patch.name() << " : skipped since processorPatch"
00122                     << endl;
00123             }
00124         }
00125     }
00126 
00127     triSurface localSurface
00128     (
00129         triSurfaceTools::triangulate
00130         (
00131             mesh.boundaryMesh(),
00132             includePatches
00133         )
00134     );
00135 
00136 
00137 
00138     if (!Pstream::parRun())
00139     {
00140         Info<< "Writing surface to " << outFileName << endl;
00141 
00142         localSurface.write(outFileName);
00143     }
00144     else
00145     {
00146         // Write local surface
00147         fileName localPath = runTime.path()/runTime.caseName() + ".ftr";
00148 
00149         Pout<< "Writing local surface to " << localPath << endl;
00150 
00151         localSurface.write(localPath);
00152 
00153         Info<< endl;
00154 
00155 
00156         // Gather all points on master
00157         List<pointField> gatheredPoints(Pstream::nProcs());
00158 
00159         gatheredPoints[Pstream::myProcNo()] = localSurface.points();
00160 
00161         Pstream::gatherList(gatheredPoints);
00162 
00163 
00164         // Gather all localSurface patches
00165         List<geometricSurfacePatchList> gatheredPatches(Pstream::nProcs());
00166 
00167         gatheredPatches[Pstream::myProcNo()] = localSurface.patches();
00168 
00169         Pstream::gatherList(gatheredPatches);
00170 
00171 
00172         // Gather all faces
00173         List<List<labelledTri> > gatheredFaces(Pstream::nProcs());
00174 
00175         gatheredFaces[Pstream::myProcNo()] = localSurface;
00176 
00177         Pstream::gatherList(gatheredFaces);
00178 
00179 
00180         if (Pstream::master())
00181         {
00182             // On master combine all points
00183             pointField allPoints =
00184                 ListListOps::combine<pointField>
00185                 (
00186                     gatheredPoints,
00187                     accessOp<pointField>()
00188                 );
00189 
00190             // Count number of patches.
00191             label nPatches = 0;
00192 
00193             forAll(gatheredPatches, procI)
00194             {
00195                 nPatches += gatheredPatches[procI].size();
00196             }
00197 
00198             // Count number of faces.
00199             label nFaces = 0;
00200 
00201             forAll(gatheredFaces, procI)
00202             {
00203                 nFaces += gatheredFaces[procI].size();
00204             }
00205 
00206 
00207 
00208             // Loop over all processors and
00209             // - construct mapping from local to global patches
00210             // - relabel faces (both points and regions)
00211 
00212             label newPatchI = 0;
00213 
00214             // Name to new patchI
00215             HashTable<label> nameToIndex(2*nPatches);
00216 
00217             // Storage (oversized) for all patches
00218             geometricSurfacePatchList allPatches(nPatches);
00219 
00220             label newFaceI = 0;
00221 
00222             // Storage for all faces
00223             List<labelledTri> allFaces(nFaces);
00224 
00225             // Offset into allPoints for current processor
00226             label pointOffset = 0;
00227 
00228             for (label procI = 0; procI < Pstream::nProcs(); procI++)
00229             {
00230                 Info<< "Processor " << procI << endl
00231                     << "-----------" << endl;
00232 
00233                 const geometricSurfacePatchList& patches =
00234                     gatheredPatches[procI];
00235 
00236                 // From local patch numbering to global
00237                 labelList localToGlobal(patches.size());
00238 
00239                 forAll(patches, patchI)
00240                 {
00241                     const geometricSurfacePatch& sp = patches[patchI];
00242 
00243                     if (!nameToIndex.found(sp.name()))
00244                     {
00245                         nameToIndex.insert(sp.name(), newPatchI);
00246 
00247                         localToGlobal[patchI] = newPatchI;
00248 
00249                         allPatches[newPatchI] = sp;
00250 
00251                         newPatchI++;
00252                     }
00253                     else
00254                     {
00255                         localToGlobal[patchI] = nameToIndex[sp.name()];
00256                     }
00257                 }
00258 
00259                 Info<< "Local patch to global patch mapping:"
00260                     << endl;
00261 
00262                 forAll(patches, patchI)
00263                 {
00264                     Info<< "    name   : " << patches[patchI].name() << endl
00265                         << "    local  : " << patchI << endl
00266                         << "    global : " << localToGlobal[patchI]
00267                         << endl;
00268                 }
00269 
00270                 Info<< "Local points added in global points starting at "
00271                     << pointOffset
00272                     << endl;
00273 
00274 
00275                 // Collect and relabel faces
00276                 const List<labelledTri>& localFaces = gatheredFaces[procI];
00277 
00278 
00279                 forAll(localFaces, faceI)
00280                 {
00281                     const labelledTri& f = localFaces[faceI];
00282 
00283                     allFaces[newFaceI++] =
00284                         labelledTri
00285                         (
00286                             f[0] + pointOffset,
00287                             f[1] + pointOffset,
00288                             f[2] + pointOffset,
00289                             localToGlobal[f.region()]
00290                         );
00291                 }
00292 
00293                 pointOffset += gatheredPoints[procI].size();
00294 
00295                 Info<< endl;
00296             }
00297             allPatches.setSize(newPatchI);
00298 
00299             // We now have allPoints, allFaces and allPatches.
00300             // Construct overall (yet unmerged) surface from these.
00301 
00302             triSurface allSurf(allFaces, allPatches, allPoints);
00303 
00304             // Cleanup (which does point merge as well
00305             allSurf.cleanup(false);
00306 
00307             // Write surface mesh
00308 
00309             label slashIndex = runTime.caseName().find_last_of('/');
00310 
00311             fileName globalCasePath(runTime.caseName().substr(0, slashIndex));
00312 
00313             Info<< "Writing merged surface to " << globalCasePath << endl;
00314 
00315             // create database for the sequential run
00316             fileName globalPath
00317             (
00318                 runTime.rootPath()
00319               / globalCasePath
00320               / args.additionalArgs()[0]
00321             );
00322 
00323             allSurf.write(globalPath);
00324         }
00325     }
00326 
00327     Info << "End\n" << endl;
00328 
00329     return 0;
00330 }
00331 
00332 
00333 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd