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

selectCells.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     Select cells in relation to surface.
00027 
00028     Divides cells into three sets:
00029     - cutCells : cells cut by surface or close to surface.
00030     - outside  : cells not in cutCells and reachable from set of
00031       user-defined points (outsidePoints)
00032     - inside   : same but not reachable.
00033 
00034     Finally the wanted sets are combined into a cellSet 'selected'. Apart
00035     from straightforward adding the contents there are a few extra rules to
00036     make sure that the surface of the 'outside' of the mesh is singly
00037     connected.
00038 
00039 \*---------------------------------------------------------------------------*/
00040 
00041 #include "argList.H"
00042 #include "Time.H"
00043 #include "polyMesh.H"
00044 #include "IOdictionary.H"
00045 #include "twoDPointCorrector.H"
00046 #include "OFstream.H"
00047 #include "meshTools.H"
00048 
00049 #include "triSurface.H"
00050 #include "triSurfaceSearch.H"
00051 #include "meshSearch.H"
00052 #include "cellClassification.H"
00053 #include "cellSet.H"
00054 #include "cellInfo.H"
00055 #include "MeshWave.H"
00056 #include "edgeStats.H"
00057 #include "treeDataTriSurface.H"
00058 #include "indexedOctree.H"
00059 
00060 using namespace Foam;
00061 
00062 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00063 
00064 // cellType for cells included/not included in mesh.
00065 static const label MESH = cellClassification::INSIDE;
00066 static const label NONMESH = cellClassification::OUTSIDE;
00067 
00068 
00069 void writeSet(const cellSet& cells, const string& msg)
00070 {
00071     Info<< "Writing " << msg << " (" << cells.size() << ") to cellSet "
00072         << cells.instance()/cells.local()/cells.name()
00073         << endl << endl;
00074     cells.write();
00075 }
00076 
00077 
00078 void getType(const labelList& elems, const label type, labelHashSet& set)
00079 {
00080     forAll(elems, i)
00081     {
00082         if (elems[i] == type)
00083         {
00084             set.insert(i);
00085         }
00086     }
00087 }
00088 
00089 
00090 void cutBySurface
00091 (
00092     const polyMesh& mesh,
00093     const meshSearch& queryMesh,
00094     const triSurfaceSearch& querySurf,
00095 
00096     const pointField& outsidePts,
00097     const bool selectCut,
00098     const bool selectInside,
00099     const bool selectOutside,
00100     const scalar nearDist,
00101 
00102     cellClassification& cellType
00103 )
00104 {
00105     // Cut with surface and classify as inside/outside/cut
00106     cellType =
00107         cellClassification
00108         (
00109             mesh,
00110             queryMesh,
00111             querySurf,
00112             outsidePts
00113         );
00114 
00115     // Get inside/outside/cutCells cellSets.
00116     cellSet inside(mesh, "inside", mesh.nCells()/10);
00117     getType(cellType, cellClassification::INSIDE, inside);
00118     writeSet(inside, "inside cells");
00119 
00120     cellSet outside(mesh, "outside", mesh.nCells()/10);
00121     getType(cellType, cellClassification::OUTSIDE, outside);
00122     writeSet(outside, "outside cells");
00123 
00124     cellSet cutCells(mesh, "cutCells", mesh.nCells()/10);
00125     getType(cellType, cellClassification::CUT, cutCells);
00126     writeSet(cutCells, "cells cut by surface");
00127 
00128 
00129     // Change cellType to reflect selected part of mesh. Use
00130     // MESH to denote selected part, NONMESH for all
00131     // other cells.
00132     // Is a bit of a hack but allows us to reuse all the functionality
00133     // in cellClassification.
00134 
00135     forAll(cellType, cellI)
00136     {
00137         label cType = cellType[cellI];
00138 
00139         if (cType == cellClassification::CUT)
00140         {
00141             if (selectCut)
00142             {
00143                 cellType[cellI] = MESH;
00144             }
00145             else
00146             {
00147                 cellType[cellI] = NONMESH;
00148             }
00149         }
00150         else if (cType == cellClassification::INSIDE)
00151         {
00152             if (selectInside)
00153             {
00154                 cellType[cellI] = MESH;
00155             }
00156             else
00157             {
00158                 cellType[cellI] = NONMESH;
00159             }
00160         }
00161         else if (cType == cellClassification::OUTSIDE)
00162         {
00163             if (selectOutside)
00164             {
00165                 cellType[cellI] = MESH;
00166             }
00167             else
00168             {
00169                 cellType[cellI] = NONMESH;
00170             }
00171         }
00172         else
00173         {
00174             FatalErrorIn("cutBySurface")
00175                 << "Multiple mesh regions in original mesh" << endl
00176                 << "Please use splitMeshRegions to separate these"
00177                 << exit(FatalError);
00178         }
00179     }
00180 
00181 
00182     if (nearDist > 0)
00183     {
00184         Info<< "Removing cells with points closer than " << nearDist
00185             << " to the surface ..." << nl << endl;
00186 
00187         const pointField& pts = mesh.points();
00188         const indexedOctree<treeDataTriSurface>& tree = querySurf.tree();
00189 
00190         label nRemoved = 0;
00191 
00192         forAll(pts, pointI)
00193         {
00194             const point& pt = pts[pointI];
00195 
00196             pointIndexHit hitInfo = tree.findNearest(pt, sqr(nearDist));
00197 
00198             if (hitInfo.hit())
00199             {
00200                 const labelList& pCells = mesh.pointCells()[pointI];
00201 
00202                 forAll(pCells, i)
00203                 {
00204                     if (cellType[pCells[i]] != NONMESH)
00205                     {
00206                         cellType[pCells[i]] = NONMESH;
00207                         nRemoved++;
00208                     }
00209                 }
00210             }
00211         }
00212 
00213 //        tmp<pointField> tnearest = querySurf.calcNearest(pts);
00214 //        const pointField& nearest = tnearest();
00215 //
00216 //        label nRemoved = 0;
00217 //
00218 //        forAll(nearest, pointI)
00219 //        {
00220 //            if (mag(nearest[pointI] - pts[pointI]) < nearDist)
00221 //            {
00222 //                const labelList& pCells = mesh.pointCells()[pointI];
00223 //
00224 //                forAll(pCells, i)
00225 //                {
00226 //                    if (cellType[pCells[i]] != NONMESH)
00227 //                    {
00228 //                        cellType[pCells[i]] = NONMESH;
00229 //                        nRemoved++;
00230 //                    }
00231 //                }
00232 //            }
00233 //        }
00234 
00235         Info<< "Removed " << nRemoved << " cells since too close to surface"
00236             << nl << endl;
00237     }
00238 }
00239 
00240 
00241 
00242 // We're meshing the outside. Subset the currently selected mesh cells with the
00243 // ones reachable from the outsidepoints.
00244 label selectOutsideCells
00245 (
00246     const polyMesh& mesh,
00247     const meshSearch& queryMesh,
00248     const pointField& outsidePts,
00249     cellClassification& cellType
00250 )
00251 {
00252     //
00253     // Check all outsidePts and for all of them inside a mesh cell
00254     // collect the faces to start walking from
00255     //
00256 
00257     // Outside faces
00258     labelHashSet outsideFacesMap(outsidePts.size() * 6 * 2);
00259     DynamicList<label> outsideFaces(outsideFacesMap.size());
00260     // CellInfo on outside faces
00261     DynamicList<cellInfo> outsideFacesInfo(outsideFacesMap.size());
00262 
00263     // cellInfo for mesh cell
00264     const cellInfo meshInfo(MESH);
00265 
00266     forAll(outsidePts, outsidePtI)
00267     {
00268         // Find cell containing point. Linear search.
00269         label cellI = queryMesh.findCell(outsidePts[outsidePtI], -1, false);
00270 
00271         if (cellType[cellI] == MESH)
00272         {
00273             Info<< "Marking cell " << cellI << " containing outside point "
00274                 << outsidePts[outsidePtI] << " with type " << cellType[cellI]
00275                 << " ..." << endl;
00276 
00277             //
00278             // Mark this cell and its faces to start walking from
00279             //
00280 
00281             // Mark faces of cellI
00282             const labelList& cFaces = mesh.cells()[cellI];
00283             forAll(cFaces, i)
00284             {
00285                 label faceI = cFaces[i];
00286 
00287                 if (outsideFacesMap.insert(faceI))
00288                 {
00289                     outsideFaces.append(faceI);
00290                     outsideFacesInfo.append(meshInfo);
00291                 }
00292             }
00293         }
00294     }
00295 
00296     // Floodfill starting from outsideFaces (of type meshInfo)
00297     MeshWave<cellInfo> regionCalc
00298     (
00299         mesh,
00300         outsideFaces.shrink(),
00301         outsideFacesInfo.shrink(),
00302         mesh.nCells()  // max iterations
00303     );
00304 
00305     // Now regionCalc should hold info on cells that are reachable from
00306     // changedFaces. Use these to subset cellType
00307     const List<cellInfo>& allCellInfo = regionCalc.allCellInfo();
00308 
00309     label nChanged = 0;
00310 
00311     forAll(allCellInfo, cellI)
00312     {
00313         if (cellType[cellI] == MESH)
00314         {
00315             // Original cell was selected for meshing. Check if cell was
00316             // reached from outsidePoints
00317             if (allCellInfo[cellI].type() != MESH)
00318             {
00319                 cellType[cellI] = NONMESH;
00320                 nChanged++;
00321             }
00322         }
00323     }
00324 
00325     return nChanged;
00326 }
00327 
00328 
00329 // Main program:
00330 
00331 int main(int argc, char *argv[])
00332 {
00333     argList::noParallel();
00334 
00335 #   include "setRootCase.H"
00336 #   include "createTime.H"
00337 #   include "createPolyMesh.H"
00338 
00339     // Mesh edge statistics calculator
00340     edgeStats edgeCalc(mesh);
00341 
00342 
00343     IOdictionary refineDict
00344     (
00345         IOobject
00346         (
00347             "selectCellsDict",
00348             runTime.system(),
00349             mesh,
00350             IOobject::MUST_READ,
00351             IOobject::NO_WRITE
00352         )
00353     );
00354 
00355     fileName surfName(refineDict.lookup("surface"));
00356     pointField outsidePts(refineDict.lookup("outsidePoints"));
00357     bool useSurface(readBool(refineDict.lookup("useSurface")));
00358     bool selectCut(readBool(refineDict.lookup("selectCut")));
00359     bool selectInside(readBool(refineDict.lookup("selectInside")));
00360     bool selectOutside(readBool(refineDict.lookup("selectOutside")));
00361     scalar nearDist(readScalar(refineDict.lookup("nearDistance")));
00362 
00363 
00364     if (useSurface)
00365     {
00366         Info<< "Cells to be used for meshing (0=false, 1=true):" << nl
00367             << "    cells cut by surface            : " << selectCut << nl
00368             << "    cells inside of surface         : " << selectInside << nl
00369             << "    cells outside of surface        : " << selectOutside << nl
00370             << "    cells with points further than  : " << nearDist << nl
00371             << endl;
00372     }
00373     else
00374     {
00375         Info<< "Cells to be used for meshing (0=false, 1=true):" << nl
00376             << "    cells reachable from outsidePoints:" << selectOutside << nl
00377             << endl;
00378     }
00379 
00380     // Print edge stats on original mesh.
00381     (void)edgeCalc.minLen(Info);
00382 
00383     // Search engine on mesh. Face decomposition since faces might be warped.
00384     meshSearch queryMesh(mesh, true);
00385 
00386     // Check all 'outside' points
00387     forAll(outsidePts, outsideI)
00388     {
00389         const point& outsidePoint = outsidePts[outsideI];
00390 
00391         if (queryMesh.findCell(outsidePoint, -1, false) == -1)
00392         {
00393             FatalErrorIn(args.executable())
00394                 << "outsidePoint " << outsidePoint
00395                 << " is not inside any cell"
00396                 << exit(FatalError);
00397         }
00398     }
00399 
00400     // Cell status (compared to surface if provided): inside/outside/cut.
00401     // Start off from everything selected and cut later.
00402     cellClassification cellType
00403     (
00404         mesh,
00405         labelList
00406         (
00407             mesh.nCells(),
00408             cellClassification::MESH
00409         )
00410     );
00411 
00412 
00413     // Surface
00414     autoPtr<triSurface> surf(NULL);
00415     // Search engine on surface.
00416     autoPtr<triSurfaceSearch> querySurf(NULL);
00417 
00418     if (useSurface)
00419     {
00420         surf.reset(new triSurface(surfName));
00421 
00422         // Dump some stats
00423         surf().writeStats(Info);
00424 
00425         // Search engine on surface.
00426         querySurf.reset(new triSurfaceSearch(surf));
00427 
00428         // Set cellType[cellI] according to relation to surface
00429         cutBySurface
00430         (
00431             mesh,
00432             queryMesh,
00433             querySurf,
00434 
00435             outsidePts,
00436             selectCut,
00437             selectInside,
00438             selectOutside,
00439             nearDist,
00440 
00441             cellType
00442         );
00443     }
00444 
00445 
00446     // Now 'trim' all the corners from the mesh so meshing/surface extraction
00447     // becomes easier.
00448 
00449     label nHanging, nRegionEdges, nRegionPoints, nOutside;
00450 
00451     do
00452     {
00453         Info<< "Removing cells which after subsetting would have all points"
00454             << " on outside ..." << nl << endl;
00455 
00456         nHanging = cellType.fillHangingCells
00457         (
00458             MESH,       // meshType
00459             NONMESH,    // fill type
00460             mesh.nCells()
00461         );
00462 
00463 
00464         Info<< "Removing edges connecting cells unconnected by faces ..."
00465             << nl << endl;
00466 
00467         nRegionEdges = cellType.fillRegionEdges
00468         (
00469             MESH,       // meshType
00470             NONMESH,    // fill type
00471             mesh.nCells()
00472         ); 
00473 
00474 
00475         Info<< "Removing points connecting cells unconnected by faces ..."
00476             << nl << endl;
00477 
00478         nRegionPoints = cellType.fillRegionPoints
00479         (
00480             MESH,       // meshType
00481             NONMESH,    // fill type
00482             mesh.nCells()
00483         );
00484 
00485         nOutside = 0;
00486         if (selectOutside)
00487         {
00488             // Since we're selecting the cells reachable from outsidePoints
00489             // and the set might have changed, redo the outsideCells
00490             // calculation
00491             nOutside = selectOutsideCells
00492             (
00493                 mesh,
00494                 queryMesh,
00495                 outsidePts,
00496                 cellType
00497             );
00498         }
00499     } while
00500     (
00501         nHanging != 0
00502      || nRegionEdges != 0
00503      || nRegionPoints != 0
00504      || nOutside != 0
00505     );
00506 
00507     cellSet selectedCells(mesh, "selected", mesh.nCells()/10);
00508     getType(cellType, MESH, selectedCells);
00509 
00510     writeSet(selectedCells, "cells selected for meshing");
00511 
00512 
00513     Info << "End\n" << endl;
00514 
00515     return 0;
00516 }
00517 
00518 
00519 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd