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

autoRefineMesh.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     Utility to refine cells near to a surface.
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "argList.H"
00031 #include "Time.H"
00032 #include "polyMesh.H"
00033 #include "twoDPointCorrector.H"
00034 #include "OFstream.H"
00035 #include "multiDirRefinement.H"
00036 
00037 #include "triSurface.H"
00038 #include "triSurfaceSearch.H"
00039 
00040 #include "cellSet.H"
00041 #include "pointSet.H"
00042 #include "surfaceToCell.H"
00043 #include "surfaceToPoint.H"
00044 #include "cellToPoint.H"
00045 #include "pointToCell.H"
00046 #include "cellToCell.H"
00047 #include "surfaceSets.H"
00048 #include "polyTopoChange.H"
00049 #include "polyTopoChanger.H"
00050 #include "mapPolyMesh.H"
00051 #include "labelIOList.H"
00052 #include "emptyPolyPatch.H"
00053 #include "removeCells.H"
00054 #include "meshSearch.H"
00055 
00056 using namespace Foam;
00057 
00058 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00059 
00060 
00061 // Max cos angle for edges to be considered aligned with axis.
00062 static const scalar edgeTol = 1E-3;
00063 
00064 
00065 void writeSet(const cellSet& cells, const string& msg)
00066 {
00067     Info<< "Writing " << msg << " (" << cells.size() << ") to cellSet "
00068         << cells.instance()/cells.local()/cells.name()
00069         << endl;
00070     cells.write();
00071 }
00072 
00073 
00074 direction getNormalDir(const twoDPointCorrector* correct2DPtr)
00075 {
00076     direction dir = 3;
00077 
00078     if (correct2DPtr)
00079     {
00080         const vector& normal = correct2DPtr->planeNormal();
00081 
00082         if (mag(normal & vector(1, 0, 0)) > 1-edgeTol)
00083         {
00084             dir = 0;
00085         }
00086         else if (mag(normal & vector(0, 1, 0)) > 1-edgeTol)
00087         {
00088             dir = 1;
00089         }
00090         else if (mag(normal & vector(0, 0, 1)) > 1-edgeTol)
00091         {
00092             dir = 2;
00093         }
00094     }
00095     return dir;
00096 }
00097 
00098 
00099 
00100 // Calculate some edge statistics on mesh. Return min. edge length over all
00101 // directions but exclude component (0=x, 1=y, 2=z, other=none)
00102 scalar getEdgeStats(const primitiveMesh& mesh, const direction excludeCmpt)
00103 {
00104     label nX = 0;
00105     label nY = 0;
00106     label nZ = 0;
00107 
00108     scalar minX = GREAT;
00109     scalar maxX = -GREAT;
00110     vector x(1, 0, 0);
00111 
00112     scalar minY = GREAT;
00113     scalar maxY = -GREAT;
00114     vector y(0, 1, 0);
00115 
00116     scalar minZ = GREAT;
00117     scalar maxZ = -GREAT;
00118     vector z(0, 0, 1);
00119 
00120     scalar minOther = GREAT;
00121     scalar maxOther = -GREAT;
00122 
00123     const edgeList& edges = mesh.edges();
00124 
00125     forAll(edges, edgeI)
00126     {
00127         const edge& e = edges[edgeI];
00128 
00129         vector eVec(e.vec(mesh.points()));
00130 
00131         scalar eMag = mag(eVec);
00132 
00133         eVec /= eMag;
00134 
00135         if (mag(eVec & x) > 1-edgeTol)
00136         {
00137             minX = min(minX, eMag);
00138             maxX = max(maxX, eMag);
00139             nX++;
00140         }
00141         else if (mag(eVec & y) > 1-edgeTol)
00142         {
00143             minY = min(minY, eMag);
00144             maxY = max(maxY, eMag);
00145             nY++;
00146         }
00147         else if (mag(eVec & z) > 1-edgeTol)
00148         {
00149             minZ = min(minZ, eMag);
00150             maxZ = max(maxZ, eMag);
00151             nZ++;
00152         }
00153         else
00154         {
00155             minOther = min(minOther, eMag);
00156             maxOther = max(maxOther, eMag);
00157         }
00158     }
00159 
00160     Info<< "Mesh bounding box:" << boundBox(mesh.points()) << nl << nl
00161         << "Mesh edge statistics:" << nl
00162         << "    x aligned :  number:" << nX << "\tminLen:" << minX
00163         << "\tmaxLen:" << maxX << nl
00164         << "    y aligned :  number:" << nY << "\tminLen:" << minY
00165         << "\tmaxLen:" << maxY << nl
00166         << "    z aligned :  number:" << nZ << "\tminLen:" << minZ
00167         << "\tmaxLen:" << maxZ << nl
00168         << "    other     :  number:" << mesh.nEdges() - nX - nY - nZ
00169         << "\tminLen:" << minOther
00170         << "\tmaxLen:" << maxOther << nl << endl;
00171 
00172     if (excludeCmpt == 0)
00173     {
00174         return min(minY, min(minZ, minOther));
00175     }
00176     else if (excludeCmpt == 1)
00177     {
00178         return min(minX, min(minZ, minOther));
00179     }
00180     else if (excludeCmpt == 2)
00181     {
00182         return min(minX, min(minY, minOther));
00183     }
00184     else
00185     {
00186         return min(minX, min(minY, min(minZ, minOther)));
00187     }
00188 }
00189 
00190 
00191 // Adds empty patch if not yet there. Returns patchID.
00192 label addPatch(polyMesh& mesh, const word& patchName)
00193 {
00194     label patchI = mesh.boundaryMesh().findPatchID(patchName);
00195 
00196     if (patchI == -1)
00197     {
00198         const polyBoundaryMesh& patches = mesh.boundaryMesh();
00199 
00200         List<polyPatch*> newPatches(patches.size() + 1);
00201 
00202         // Add empty patch as 0th entry (Note: only since subsetMesh wants this)
00203         patchI = 0;
00204 
00205         newPatches[patchI] =
00206             new emptyPolyPatch
00207             (
00208                 Foam::word(patchName),
00209                 0,
00210                 mesh.nInternalFaces(),
00211                 patchI,
00212                 patches
00213             );
00214 
00215         forAll(patches, i)
00216         {
00217             const polyPatch& pp = patches[i];
00218 
00219             newPatches[i+1] =
00220                 pp.clone
00221                 (
00222                     patches,
00223                     i+1,
00224                     pp.size(),
00225                     pp.start()
00226                 ).ptr();
00227         }
00228 
00229         mesh.removeBoundary();
00230         mesh.addPatches(newPatches);
00231 
00232         Info<< "Created patch oldInternalFaces at " << patchI << endl;
00233     }
00234     else
00235     {
00236         Info<< "Reusing patch oldInternalFaces at " << patchI << endl;
00237     }
00238 
00239 
00240     return patchI;
00241 }
00242 
00243 
00244 // Take surface and select cells based on surface curvature.
00245 void selectCurvatureCells
00246 (
00247     const polyMesh& mesh,
00248     const fileName& surfName,
00249     const triSurfaceSearch& querySurf,
00250     const scalar nearDist,
00251     const scalar curvature,
00252     cellSet& cells
00253 )
00254 {
00255     // Use surfaceToCell to do actual calculation.
00256 
00257     // Since we're adding make sure set is on disk.
00258     cells.write();
00259 
00260     // Take centre of cell 0 as outside point since info not used.
00261 
00262     surfaceToCell cutSource
00263     (
00264         mesh,
00265         surfName,
00266         querySurf.surface(),
00267         querySurf,
00268         pointField(1, mesh.cellCentres()[0]),
00269         false,
00270         false,
00271         false,
00272         nearDist,
00273         curvature
00274     );
00275 
00276     cutSource.applyToSet(topoSetSource::ADD, cells);
00277 }
00278 
00279 
00280 // cutCells contains currently selected cells to be refined. Add neighbours
00281 // on the inside or outside to them.
00282 void addCutNeighbours
00283 (
00284     const polyMesh& mesh,
00285     const bool selectInside,
00286     const bool selectOutside,
00287     const labelHashSet& inside,
00288     const labelHashSet& outside,
00289     labelHashSet& cutCells
00290 )
00291 {
00292     // Pick up face neighbours of cutCells
00293 
00294     labelHashSet addCutFaces(cutCells.size());
00295 
00296     for
00297     (
00298         labelHashSet::const_iterator iter = cutCells.begin();
00299         iter != cutCells.end();
00300         ++iter
00301     )
00302     {
00303         label cellI = iter.key();
00304 
00305         const labelList& cFaces = mesh.cells()[cellI];
00306 
00307         forAll(cFaces, i)
00308         {
00309             label faceI = cFaces[i];
00310 
00311             if (mesh.isInternalFace(faceI))
00312             {
00313                 label nbr = mesh.faceOwner()[faceI];
00314 
00315                 if (nbr == cellI)
00316                 {
00317                     nbr = mesh.faceNeighbour()[faceI];
00318                 }
00319 
00320                 if (selectInside && inside.found(nbr))
00321                 {
00322                     addCutFaces.insert(nbr);
00323                 }
00324                 else if (selectOutside && outside.found(nbr))
00325                 {
00326                     addCutFaces.insert(nbr);
00327                 }
00328             }
00329         }
00330     }
00331 
00332     Info<< "    Selected an additional " << addCutFaces.size()
00333         << " neighbours of cutCells to refine" << endl;
00334 
00335     for
00336     (
00337         labelHashSet::const_iterator iter = addCutFaces.begin();
00338         iter != addCutFaces.end();
00339         ++iter
00340     )
00341     {
00342         cutCells.insert(iter.key());
00343     }
00344 }
00345 
00346 
00347 // Return true if any cells had to be split to keep a difference between
00348 // neighbouring refinement levels < limitDiff.
00349 // Gets cells which will be refined (so increase the refinement level) and
00350 // updates it.
00351 bool limitRefinementLevel
00352 (
00353     const primitiveMesh& mesh,
00354     const label limitDiff,
00355     const labelHashSet& excludeCells,
00356     const labelList& refLevel,
00357     labelHashSet& cutCells
00358 )
00359 {
00360     // Do simple check on validity of refinement level.
00361     forAll(refLevel, cellI)
00362     {
00363         if (!excludeCells.found(cellI))
00364         {
00365             const labelList& cCells = mesh.cellCells()[cellI];
00366 
00367             forAll(cCells, i)
00368             {
00369                 label nbr = cCells[i];
00370 
00371                 if (!excludeCells.found(nbr))
00372                 {
00373                     if (refLevel[cellI] - refLevel[nbr] >= limitDiff)
00374                     {
00375                         FatalErrorIn("limitRefinementLevel")
00376                             << "Level difference between neighbouring cells "
00377                             << cellI << " and " << nbr
00378                             << " greater than or equal to " << limitDiff << endl
00379                             << "refLevels:" << refLevel[cellI] << ' '
00380                             <<  refLevel[nbr] << abort(FatalError);
00381                     }
00382                 }
00383             }
00384         }
00385     }
00386 
00387 
00388     labelHashSet addCutCells(cutCells.size());
00389 
00390     for
00391     (
00392         labelHashSet::const_iterator iter = cutCells.begin();
00393         iter != cutCells.end();
00394         ++iter
00395     )
00396     {
00397         // cellI will be refined.
00398         label cellI = iter.key();
00399 
00400         const labelList& cCells = mesh.cellCells()[cellI];
00401 
00402         forAll(cCells, i)
00403         {
00404             label nbr = cCells[i];
00405 
00406             if (!excludeCells.found(nbr) && !cutCells.found(nbr))
00407             {
00408                 if (refLevel[cellI] + 1 - refLevel[nbr] >= limitDiff)
00409                 {
00410                     addCutCells.insert(nbr);
00411                 }
00412             }
00413         }
00414     }
00415 
00416     if (addCutCells.size())
00417     {
00418         // Add cells to cutCells.
00419 
00420         Info<< "Added an additional " << addCutCells.size() << " cells"
00421             << " to satisfy 1:" << limitDiff << " refinement level"
00422             << endl;
00423         for
00424         (
00425             labelHashSet::const_iterator iter = addCutCells.begin();
00426             iter != addCutCells.end();
00427             ++iter
00428         )
00429         {
00430             cutCells.insert(iter.key());
00431         }
00432         return true;
00433     }
00434     else
00435     {
00436         Info<< "Added no additional cells"
00437             << " to satisfy 1:" << limitDiff << " refinement level"
00438             << endl;
00439 
00440         return false;
00441     }
00442 }
00443 
00444 
00445 // Do all refinement (i.e. refCells) according to refineDict and update
00446 // refLevel afterwards for added cells
00447 void doRefinement
00448 (
00449     polyMesh& mesh,
00450     const dictionary& refineDict,
00451     const labelHashSet& refCells,
00452     labelList& refLevel
00453 )
00454 {
00455     label oldCells = mesh.nCells();
00456 
00457     // Multi-iteration, multi-direction topology modifier.
00458     multiDirRefinement multiRef
00459     (
00460         mesh,
00461         refCells.toc(),
00462         refineDict
00463     );
00464 
00465     //
00466     // Update refLevel for split cells
00467     //
00468 
00469     refLevel.setSize(mesh.nCells());
00470 
00471     for (label cellI = oldCells; cellI < mesh.nCells(); cellI++)
00472     {
00473         refLevel[cellI] = 0;
00474     }
00475 
00476     const labelListList& addedCells = multiRef.addedCells();
00477 
00478     forAll(addedCells, oldCellI)
00479     {
00480         const labelList& added = addedCells[oldCellI];
00481 
00482         if (added.size())
00483         {
00484             // Give all cells resulting from split the refinement level
00485             // of the master.
00486             label masterLevel = ++refLevel[oldCellI];
00487 
00488             forAll(added, i)
00489             {
00490                 refLevel[added[i]] = masterLevel;
00491             }
00492         }
00493     }
00494 }
00495 
00496 
00497 // Subset mesh and update refLevel and cutCells
00498 void subsetMesh
00499 (
00500     polyMesh& mesh,
00501     const label writeMesh,
00502     const label patchI,                 // patchID for exposed faces
00503     const labelHashSet& cellsToRemove,
00504     cellSet& cutCells,
00505     labelIOList& refLevel
00506 )
00507 {
00508     removeCells cellRemover(mesh);
00509 
00510     labelList cellLabels(cellsToRemove.toc());
00511 
00512     Info<< "Mesh has:" << mesh.nCells() << " cells."
00513         << " Removing:" << cellLabels.size() << " cells" << endl;
00514 
00515     // exposed faces
00516     labelList exposedFaces(cellRemover.getExposedFaces(cellLabels));
00517 
00518     polyTopoChange meshMod(mesh);
00519     cellRemover.setRefinement
00520     (
00521         cellLabels,
00522         exposedFaces,
00523         labelList(exposedFaces.size(), patchI),
00524         meshMod
00525     );
00526 
00527     // Do all changes
00528     Info<< "Morphing ..." << endl;
00529 
00530     const Time& runTime = mesh.time();
00531 
00532     autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
00533 
00534     if (morphMap().hasMotionPoints())
00535     {
00536         mesh.movePoints(morphMap().preMotionPoints());
00537     }
00538 
00539     // Update topology on cellRemover
00540     cellRemover.updateMesh(morphMap());
00541 
00542     // Update refLevel for removed cells.
00543     const labelList& cellMap = morphMap().cellMap();
00544 
00545     labelList newRefLevel(cellMap.size());
00546 
00547     forAll(cellMap, i)
00548     {
00549         newRefLevel[i] = refLevel[cellMap[i]];
00550     }
00551 
00552     // Transfer back to refLevel
00553     refLevel.transfer(newRefLevel);
00554 
00555     if (writeMesh)
00556     {
00557         Info<< "Writing refined mesh to time " << runTime.timeName() << nl
00558             << endl;
00559 
00560         IOstream::defaultPrecision(10);
00561         mesh.write();
00562         refLevel.write();
00563     }
00564 
00565     // Update cutCells for removed cells.
00566     cutCells.updateMesh(morphMap());
00567 }
00568 
00569 
00570 // Divide the cells according to status compared to surface. Constructs sets:
00571 // - cutCells : all cells cut by surface
00572 // - inside   : all cells inside surface
00573 // - outside  :   ,,      outside ,,
00574 // and a combined set:
00575 // - selected : sum of above according to selectCut/Inside/Outside flags.
00576 void classifyCells
00577 (
00578     const polyMesh& mesh,
00579     const fileName& surfName,
00580     const triSurfaceSearch& querySurf,
00581     const pointField& outsidePts,
00582 
00583     const bool selectCut,
00584     const bool selectInside,
00585     const bool selectOutside,
00586 
00587     const label nCutLayers,
00588 
00589     cellSet& inside,
00590     cellSet& outside,
00591     cellSet& cutCells,
00592     cellSet& selected
00593 )
00594 {
00595     // Cut faces with surface and classify cells
00596     surfaceSets::getSurfaceSets
00597     (
00598         mesh,
00599         surfName,
00600         querySurf.surface(),
00601         querySurf,
00602         outsidePts,
00603 
00604         nCutLayers,
00605 
00606         inside,
00607         outside,
00608         cutCells
00609     );
00610 
00611     // Combine wanted parts into selected
00612     if (selectCut)
00613     {
00614         selected.addSet(cutCells);
00615     }
00616     if (selectInside)
00617     {
00618         selected.addSet(inside);
00619     }
00620     if (selectOutside)
00621     {
00622         selected.addSet(outside);
00623     }
00624 
00625     Info<< "Determined cell status:" << endl
00626         << "    inside  :" << inside.size() << endl
00627         << "    outside :" << outside.size() << endl
00628         << "    cutCells:" << cutCells.size() << endl
00629         << "    selected:" << selected.size() << endl
00630         << endl;
00631 
00632     writeSet(inside, "inside cells");
00633     writeSet(outside, "outside cells");
00634     writeSet(cutCells, "cut cells");
00635     writeSet(selected, "selected cells");
00636 }
00637 
00638 
00639 // Main program:
00640 
00641 int main(int argc, char *argv[])
00642 {
00643     argList::noParallel();
00644 
00645 #   include "setRootCase.H"
00646 #   include "createTime.H"
00647 #   include "createPolyMesh.H"
00648 
00649     // If nessecary add oldInternalFaces patch
00650     label newPatchI = addPatch(mesh, "oldInternalFaces");
00651 
00652 
00653     //
00654     // Read motionProperties dictionary
00655     //
00656 
00657     Info<< "Checking for motionProperties\n" << endl;
00658 
00659     IOobject motionObj
00660     (
00661         "motionProperties",
00662         runTime.constant(),
00663         mesh,
00664         IOobject::MUST_READ,
00665         IOobject::NO_WRITE
00666     );
00667 
00668     // corrector for mesh motion
00669     twoDPointCorrector* correct2DPtr = NULL;
00670 
00671     if (motionObj.headerOk())
00672     {
00673         Info<< "Reading " << runTime.constant() / "motionProperties"
00674             << endl << endl;
00675 
00676         IOdictionary motionProperties(motionObj);
00677 
00678         Switch twoDMotion(motionProperties.lookup("twoDMotion"));
00679 
00680         if (twoDMotion)
00681         {
00682             Info<< "Correcting for 2D motion" << endl << endl;
00683             correct2DPtr = new twoDPointCorrector(mesh);
00684         }
00685     }
00686 
00687     IOdictionary refineDict
00688     (
00689         IOobject
00690         (
00691             "autoRefineMeshDict",
00692             runTime.system(),
00693             mesh,
00694             IOobject::MUST_READ,
00695             IOobject::NO_WRITE
00696         )
00697     );
00698 
00699     fileName surfName(refineDict.lookup("surface"));
00700     surfName.expand();
00701     label nCutLayers(readLabel(refineDict.lookup("nCutLayers")));
00702     label cellLimit(readLabel(refineDict.lookup("cellLimit")));
00703     bool selectCut(readBool(refineDict.lookup("selectCut")));
00704     bool selectInside(readBool(refineDict.lookup("selectInside")));
00705     bool selectOutside(readBool(refineDict.lookup("selectOutside")));
00706     bool selectHanging(readBool(refineDict.lookup("selectHanging")));
00707 
00708     scalar minEdgeLen(readScalar(refineDict.lookup("minEdgeLen")));
00709     scalar maxEdgeLen(readScalar(refineDict.lookup("maxEdgeLen")));
00710     scalar curvature(readScalar(refineDict.lookup("curvature")));
00711     scalar curvDist(readScalar(refineDict.lookup("curvatureDistance")));
00712     pointField outsidePts(refineDict.lookup("outsidePoints"));
00713     label refinementLimit(readLabel(refineDict.lookup("splitLevel")));
00714     bool writeMesh(readBool(refineDict.lookup("writeMesh")));
00715 
00716     Info<< "Cells to be used for meshing (0=false, 1=true):" << nl
00717         << "    cells cut by surface            : " << selectCut << nl
00718         << "    cells inside of surface         : " << selectInside << nl
00719         << "    cells outside of surface        : " << selectOutside << nl
00720         << "    hanging cells                   : " << selectHanging << nl
00721         << endl;
00722 
00723 
00724     if (nCutLayers > 0 && selectInside)
00725     {
00726         WarningIn(args.executable())
00727             << "Illogical settings : Both nCutLayers>0 and selectInside true."
00728             << endl
00729             << "This would mean that inside cells get removed but should be"
00730             << " included in final mesh" << endl;
00731     }
00732 
00733     // Surface.
00734     triSurface surf(surfName);
00735 
00736     // Search engine on surface
00737     triSurfaceSearch querySurf(surf);
00738 
00739     // Search engine on mesh. No face decomposition since mesh unwarped.
00740     meshSearch queryMesh(mesh, false);
00741 
00742     // Check all 'outside' points
00743     forAll(outsidePts, outsideI)
00744     {
00745         const point& outsidePoint = outsidePts[outsideI];
00746 
00747         if (queryMesh.findCell(outsidePoint, -1, false) == -1)
00748         {
00749             FatalErrorIn(args.executable())
00750                 << "outsidePoint " << outsidePoint
00751                 << " is not inside any cell"
00752                 << exit(FatalError);
00753         }
00754     }
00755 
00756 
00757 
00758     // Current refinement level. Read if present.
00759     labelIOList refLevel
00760     (
00761         IOobject
00762         (
00763             "refinementLevel",
00764             runTime.timeName(),
00765             polyMesh::defaultRegion,
00766             mesh,
00767             IOobject::READ_IF_PRESENT,
00768             IOobject::AUTO_WRITE
00769         ),
00770         labelList(mesh.nCells(), 0)
00771     );
00772 
00773     label maxLevel = max(refLevel);
00774 
00775     if (maxLevel > 0)
00776     {
00777         Info<< "Read existing refinement level from file "
00778             << refLevel.objectPath() << nl
00779             << "   min level : " << min(refLevel) << nl
00780             << "   max level : " << maxLevel << nl
00781             << endl;
00782     }
00783     else
00784     {
00785         Info<< "Created zero refinement level in file "
00786             << refLevel.objectPath() << nl
00787             << endl;
00788     }
00789 
00790 
00791 
00792 
00793     // Print edge stats on original mesh. Leave out 2d-normal direction
00794     direction normalDir(getNormalDir(correct2DPtr));
00795     scalar meshMinEdgeLen = getEdgeStats(mesh, normalDir);
00796 
00797     while (meshMinEdgeLen > minEdgeLen)
00798     {
00799         // Get inside/outside/cutCells cellSets.
00800         cellSet inside(mesh, "inside", mesh.nCells()/10);
00801         cellSet outside(mesh, "outside", mesh.nCells()/10);
00802         cellSet cutCells(mesh, "cutCells", mesh.nCells()/10);
00803         cellSet selected(mesh, "selected", mesh.nCells()/10);
00804 
00805         classifyCells
00806         (
00807             mesh,
00808             surfName,
00809             querySurf,
00810             outsidePts,
00811 
00812             selectCut,      // for determination of selected
00813             selectInside,   // for determination of selected
00814             selectOutside,  // for determination of selected
00815 
00816             nCutLayers,     // How many layers of cutCells to include
00817 
00818             inside,
00819             outside,
00820             cutCells,
00821             selected        // not used but determined anyway.
00822         );
00823 
00824         Info<< "    Selected " << cutCells.name() << " with "
00825             << cutCells.size() << " cells" << endl;
00826 
00827         if ((curvDist > 0) && (meshMinEdgeLen < maxEdgeLen))
00828         {
00829             // Done refining enough close to surface. Now switch to more
00830             // intelligent refinement where only cutCells on surface curvature
00831             // are refined.
00832             cellSet curveCells(mesh, "curveCells", mesh.nCells()/10);
00833 
00834             selectCurvatureCells
00835             (
00836                 mesh,
00837                 surfName,
00838                 querySurf,
00839                 maxEdgeLen,
00840                 curvature,
00841                 curveCells
00842             );
00843 
00844             Info<< "    Selected " << curveCells.name() << " with "
00845                 << curveCells.size() << " cells" << endl;
00846 
00847             // Add neighbours to cutCells. This is if selectCut is not
00848             // true and so outside and/or inside cells get exposed there is
00849             // also refinement in them.
00850             if (!selectCut)
00851             {
00852                 addCutNeighbours
00853                 (
00854                     mesh,
00855                     selectInside,
00856                     selectOutside,
00857                     inside,
00858                     outside,
00859                     cutCells
00860                 );
00861             }
00862 
00863             // Subset cutCells to only curveCells
00864             cutCells.subset(curveCells);
00865 
00866             Info<< "    Removed cells not on surface curvature. Selected "
00867                 << cutCells.size() << endl;
00868         }
00869 
00870 
00871         if (nCutLayers > 0)
00872         {
00873             // Subset mesh to remove inside cells altogether. Updates cutCells,
00874             // refLevel.
00875             subsetMesh(mesh, writeMesh, newPatchI, inside, cutCells, refLevel);
00876         }
00877 
00878 
00879         // Added cells from 2:1 refinement level restriction.
00880         while
00881         (
00882             limitRefinementLevel
00883             (
00884                 mesh,
00885                 refinementLimit,
00886                 labelHashSet(),
00887                 refLevel,
00888                 cutCells
00889             )
00890         )
00891         {}
00892 
00893 
00894         Info<< "    Current cells           : " << mesh.nCells() << nl
00895             << "    Selected for refinement :" << cutCells.size() << nl
00896             << endl;
00897 
00898         if (cutCells.empty())
00899         {
00900             Info<< "Stopping refining since 0 cells selected to be refined ..."
00901                 << nl << endl;
00902             break;
00903         }
00904 
00905         if ((mesh.nCells() + 8*cutCells.size()) > cellLimit)
00906         {
00907             Info<< "Stopping refining since cell limit reached ..." << nl
00908                 << "Would refine from " << mesh.nCells()
00909                 << " to " << mesh.nCells() + 8*cutCells.size() << " cells."
00910                 << nl << endl;
00911             break;
00912         }
00913 
00914         doRefinement
00915         (
00916             mesh,
00917             refineDict,
00918             cutCells,
00919             refLevel
00920         );
00921 
00922         Info<< "    After refinement:" << mesh.nCells() << nl
00923             << endl;
00924 
00925         if (writeMesh)
00926         {
00927             Info<< "    Writing refined mesh to time " << runTime.timeName()
00928                 << nl << endl;
00929 
00930             IOstream::defaultPrecision(10);
00931             mesh.write();
00932             refLevel.write();
00933         }
00934 
00935         // Update mesh edge stats.
00936         meshMinEdgeLen = getEdgeStats(mesh, normalDir);
00937     }
00938 
00939 
00940     if (selectHanging)
00941     {
00942         // Get inside/outside/cutCells cellSets.
00943         cellSet inside(mesh, "inside", mesh.nCells()/10);
00944         cellSet outside(mesh, "outside", mesh.nCells()/10);
00945         cellSet cutCells(mesh, "cutCells", mesh.nCells()/10);
00946         cellSet selected(mesh, "selected", mesh.nCells()/10);
00947 
00948         classifyCells
00949         (
00950             mesh,
00951             surfName,
00952             querySurf,
00953             outsidePts,
00954 
00955             selectCut,
00956             selectInside,
00957             selectOutside,
00958 
00959             nCutLayers,
00960 
00961             inside,
00962             outside,
00963             cutCells,
00964             selected
00965         );
00966 
00967 
00968         // Find any cells which have all their points on the outside of the
00969         // selected set and refine them
00970         labelHashSet hanging = surfaceSets::getHangingCells(mesh, selected);
00971 
00972         Info<< "Detected " << hanging.size() << " hanging cells"
00973             << " (cells with all points on"
00974             << " outside of cellSet 'selected').\nThese would probably result"
00975             << " in flattened cells when snapping the mesh to the surface"
00976             << endl;
00977 
00978         Info<< "Refining " << hanging.size() << " hanging cells" << nl
00979             << endl;
00980 
00981         // Added cells from 2:1 refinement level restriction.
00982         while
00983         (
00984             limitRefinementLevel
00985             (
00986                 mesh,
00987                 refinementLimit,
00988                 labelHashSet(),
00989                 refLevel,
00990                 hanging
00991             )
00992         )
00993         {}
00994 
00995         doRefinement(mesh, refineDict, hanging, refLevel);
00996 
00997         Info<< "Writing refined mesh to time " << runTime.timeName() << nl
00998             << endl;
00999 
01000         // Write final mesh
01001         IOstream::defaultPrecision(10);
01002         mesh.write();
01003         refLevel.write();
01004 
01005     }
01006     else if (!writeMesh)
01007     {
01008         Info<< "Writing refined mesh to time " << runTime.timeName() << nl
01009             << endl;
01010 
01011         // Write final mesh. (will have been written already if writeMesh=true)
01012         IOstream::defaultPrecision(10);
01013         mesh.write();
01014         refLevel.write();
01015     }
01016 
01017 
01018     Info << "End\n" << endl;
01019 
01020     return 0;
01021 }
01022 
01023 
01024 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd