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

surfaceToPatch.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     Reads surface and applies surface regioning to a mesh. Uses boundaryMesh
00027     to do the hard work.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "argList.H"
00032 #include "Time.H"
00033 #include "boundaryMesh.H"
00034 #include "polyMesh.H"
00035 #include "faceSet.H"
00036 #include "polyTopoChange.H"
00037 #include "polyModifyFace.H"
00038 #include "globalMeshData.H"
00039 
00040 using namespace Foam;
00041 
00042 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00043 
00044 // Adds empty patch if not yet there. Returns patchID.
00045 label addPatch(polyMesh& mesh, const word& patchName)
00046 {
00047     label patchI = mesh.boundaryMesh().findPatchID(patchName);
00048 
00049     if (patchI == -1)
00050     {
00051         const polyBoundaryMesh& patches = mesh.boundaryMesh();
00052 
00053         List<polyPatch*> newPatches(patches.size() + 1);
00054 
00055         patchI = 0;
00056 
00057         // Copy all old patches
00058         forAll(patches, i)
00059         {
00060             const polyPatch& pp = patches[i];
00061 
00062             newPatches[patchI] =
00063                 pp.clone
00064                 (
00065                     patches,
00066                     patchI,
00067                     pp.size(),
00068                     pp.start()
00069                 ).ptr();
00070 
00071             patchI++;
00072         }
00073 
00074         // Add zero-sized patch
00075         newPatches[patchI] =
00076             new polyPatch
00077             (
00078                 patchName,
00079                 0,
00080                 mesh.nFaces(),
00081                 patchI,
00082                 patches
00083             );
00084 
00085         mesh.removeBoundary();
00086         mesh.addPatches(newPatches);
00087 
00088         Pout<< "Created patch " << patchName << " at " << patchI << endl;
00089     }
00090     else
00091     {
00092         Pout<< "Reusing patch " << patchName << " at " << patchI << endl;
00093     }
00094 
00095     return patchI;
00096 }
00097 
00098 
00099 // Repatch single face. Return true if patch changed.
00100 bool repatchFace
00101 (
00102     const polyMesh& mesh,
00103     const boundaryMesh& bMesh,
00104     const labelList& nearest,
00105     const labelList& surfToMeshPatch,
00106     const label faceI,
00107     polyTopoChange& meshMod
00108 )
00109 {
00110     bool changed = false;
00111 
00112     label bFaceI = faceI - mesh.nInternalFaces();
00113 
00114     if (nearest[bFaceI] != -1)
00115     {
00116         // Use boundary mesh one.
00117         label bMeshPatchID = bMesh.whichPatch(nearest[bFaceI]);
00118 
00119         label patchID = surfToMeshPatch[bMeshPatchID];
00120 
00121         if (patchID != mesh.boundaryMesh().whichPatch(faceI))
00122         {
00123             label own = mesh.faceOwner()[faceI];
00124 
00125             label zoneID = mesh.faceZones().whichZone(faceI);
00126 
00127             bool zoneFlip = false;
00128 
00129             if (zoneID >= 0)
00130             {
00131                 const faceZone& fZone = mesh.faceZones()[zoneID];
00132 
00133                 zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00134             }
00135 
00136             meshMod.setAction
00137             (
00138                 polyModifyFace
00139                 (
00140                     mesh.faces()[faceI],// modified face
00141                     faceI,              // label of face being modified
00142                     own,                // owner
00143                     -1,                 // neighbour
00144                     false,              // face flip
00145                     patchID,            // patch for face
00146                     false,              // remove from zone
00147                     zoneID,             // zone for face
00148                     zoneFlip            // face flip in zone
00149                 )
00150             );
00151 
00152             changed = true;
00153         }
00154     }
00155     else
00156     {
00157         changed = false;
00158     }
00159     return changed;
00160 }
00161 
00162 
00163 // Main program:
00164 
00165 int main(int argc, char *argv[])
00166 {
00167     argList::noParallel();
00168     argList::validArgs.append("surface file");
00169     argList::validOptions.insert("faceSet", "faceSet name");
00170     argList::validOptions.insert("tol", "fraction of mesh size");
00171 
00172 #   include "setRootCase.H"
00173 #   include "createTime.H"
00174 #   include "createPolyMesh.H"
00175 
00176     fileName surfName(args.additionalArgs()[0]);
00177 
00178     Info<< "Reading surface from " << surfName << " ..." << endl;
00179 
00180     bool readSet = args.optionFound("faceSet");
00181     word setName;
00182 
00183     if (readSet)
00184     {
00185         setName = args.option("faceSet");
00186 
00187         Info<< "Repatching only the faces in faceSet " << setName
00188             << " according to nearest surface triangle ..." << endl;
00189     }
00190     else
00191     {
00192         Info<< "Patching all boundary faces according to nearest surface"
00193             << " triangle ..." << endl;
00194     }
00195 
00196     scalar searchTol = 1E-3;
00197     args.optionReadIfPresent("tol", searchTol);
00198 
00199     // Get search box. Anything not within this box will not be considered.
00200     const boundBox& meshBb = mesh.globalData().bb();
00201     const vector searchSpan = searchTol * meshBb.span();
00202 
00203     Info<< "All boundary faces further away than " << searchTol
00204         << " of mesh bounding box " << meshBb
00205         << " will keep their patch label ..." << endl;
00206 
00207 
00208     Info<< "Before patching:" << nl
00209         << "    patch\tsize" << endl;
00210 
00211     forAll(mesh.boundaryMesh(), patchI)
00212     {
00213         Info<< "    " << mesh.boundaryMesh()[patchI].name() << '\t'
00214             << mesh.boundaryMesh()[patchI].size() << endl;
00215     }
00216     Info<< endl;
00217 
00218 
00219 
00220     boundaryMesh bMesh;
00221 
00222     // Load in the surface.
00223     bMesh.readTriSurface(surfName);
00224 
00225     // Add all the boundaryMesh patches to the mesh.
00226     const PtrList<boundaryPatch>& bPatches = bMesh.patches();
00227 
00228     // Map from surface patch ( = boundaryMesh patch) to polyMesh patch
00229     labelList patchMap(bPatches.size());
00230 
00231     forAll(bPatches, i)
00232     {
00233         patchMap[i] = addPatch(mesh, bPatches[i].name());
00234     }
00235 
00236     // Obtain nearest face in bMesh for each boundary face in mesh that
00237     // is within search span.
00238     // Note: should only determine for faceSet if working with that.
00239     labelList nearest(bMesh.getNearest(mesh, searchSpan));
00240 
00241     {
00242         // Dump unmatched faces to faceSet for debugging.
00243         faceSet unmatchedFaces(mesh, "unmatchedFaces", nearest.size()/100);
00244 
00245         forAll(nearest, bFaceI)
00246         {
00247             if (nearest[bFaceI] == -1)
00248             {
00249                 unmatchedFaces.insert(mesh.nInternalFaces() + bFaceI);
00250             }
00251         }
00252 
00253         Pout<< "Writing all " << unmatchedFaces.size()
00254             << " unmatched faces to faceSet "
00255             << unmatchedFaces.name()
00256             << endl;
00257 
00258         unmatchedFaces.write();
00259     }
00260 
00261 
00262     polyTopoChange meshMod(mesh);
00263 
00264     label nChanged = 0;
00265 
00266     if (readSet)
00267     {
00268         faceSet faceLabels(mesh, setName);
00269         Info<< "Read " << faceLabels.size() << " faces to repatch ..." << endl;
00270 
00271         forAllConstIter(faceSet, faceLabels, iter)
00272         {
00273             label faceI = iter.key();
00274 
00275             if (repatchFace(mesh, bMesh, nearest, patchMap, faceI, meshMod))
00276             {
00277                 nChanged++;
00278             }
00279         }
00280     }
00281     else
00282     {
00283         forAll(nearest, bFaceI)
00284         {
00285             label faceI = mesh.nInternalFaces() + bFaceI;
00286 
00287             if (repatchFace(mesh, bMesh, nearest, patchMap, faceI, meshMod))
00288             {
00289                 nChanged++;
00290             }
00291         }
00292     }
00293 
00294     Pout<< "Changed " << nChanged << " boundary faces." << nl << endl;
00295 
00296     if (nChanged > 0)
00297     {
00298         meshMod.changeMesh(mesh, false);
00299 
00300         Info<< "After patching:" << nl
00301             << "    patch\tsize" << endl;
00302 
00303         forAll(mesh.boundaryMesh(), patchI)
00304         {
00305             Info<< "    " << mesh.boundaryMesh()[patchI].name() << '\t'
00306                 << mesh.boundaryMesh()[patchI].size() << endl;
00307         }
00308         Info<< endl;
00309 
00310 
00311         runTime++;
00312 
00313         // Write resulting mesh
00314         Info << "Writing modified mesh to time " << runTime.value() << endl;
00315         mesh.write();
00316     }
00317 
00318 
00319     Info<< "End\n" << endl;
00320 
00321     return 0;
00322 }
00323 
00324 
00325 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd