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

splitCells.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 split cells with flat faces.
00027 
00028     Uses a geometric cut with a plane dividing the edge angle into two so
00029     might produce funny cells. For hexes it will use by default a cut from
00030     edge onto opposite edge (i.e. purely topological).
00031 
00032     Options:
00033     - split cells from cellSet only
00034     - use geometric cut for hexes as well
00035 
00036     The angle is the angle between two faces sharing an edge as seen from
00037     inside each cell. So a cube will have all angles 90. If you want
00038     to split cells with cell centre outside use e.g. angle 200
00039 
00040 \*---------------------------------------------------------------------------*/
00041 
00042 #include "argList.H"
00043 #include "Time.H"
00044 #include "polyTopoChange.H"
00045 #include "polyTopoChanger.H"
00046 #include "mapPolyMesh.H"
00047 #include "polyMesh.H"
00048 #include "cellCuts.H"
00049 #include "cellSet.H"
00050 #include "cellModeller.H"
00051 #include "meshCutter.H"
00052 #include "mathematicalConstants.H"
00053 #include "geomCellLooper.H"
00054 #include "plane.H"
00055 #include "edgeVertex.H"
00056 #include "meshTools.H"
00057 #include "ListOps.H"
00058 
00059 using namespace Foam;
00060 
00061 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00062 
00063 
00064 labelList pack(const boolList& lst)
00065 {
00066     labelList packedLst(lst.size());
00067     label packedI = 0;
00068 
00069     forAll(lst, i)
00070     {
00071         if (lst[i])
00072         {
00073             packedLst[packedI++] = i;
00074         }
00075     }
00076     packedLst.setSize(packedI);
00077 
00078     return packedLst;
00079 }
00080 
00081 
00082 scalarField pack(const boolList& lst, const scalarField& elems)
00083 {
00084     scalarField packedElems(lst.size());
00085     label packedI = 0;
00086 
00087     forAll(lst, i)
00088     {
00089         if (lst[i])
00090         {
00091             packedElems[packedI++] = elems[i];
00092         }
00093     }
00094     packedElems.setSize(packedI);
00095 
00096     return packedElems;
00097 }
00098 
00099 
00100 // Given sin and cos of max angle between normals calculate whether f0 and f1
00101 // on cellI make larger angle. Uses sinAngle only for quadrant detection.
00102 bool largerAngle
00103 (
00104     const primitiveMesh& mesh,
00105     const scalar cosAngle,
00106     const scalar sinAngle,
00107 
00108     const label cellI,
00109     const label f0,             // face label
00110     const label f1,
00111 
00112     const vector& n0,           // normal at f0
00113     const vector& n1
00114 )
00115 {
00116     const labelList& own = mesh.faceOwner();
00117 
00118     bool sameFaceOrder = !((own[f0] == cellI) ^ (own[f1] == cellI));
00119 
00120     // Get cos between faceArea vectors. Correct so flat angle (180 degrees)
00121     // gives -1.
00122     scalar normalCosAngle = n0 & n1;
00123 
00124     if (sameFaceOrder)
00125     {
00126         normalCosAngle = -normalCosAngle;
00127     }
00128 
00129 
00130     // Get cos between faceCentre and normal vector to determine in
00131     // which quadrant angle is. (Is correct for unwarped faces only!)
00132     // Correct for non-outwards pointing normal.
00133     vector c1c0(mesh.faceCentres()[f1] - mesh.faceCentres()[f0]);
00134     c1c0 /= mag(c1c0) + VSMALL;
00135 
00136     scalar fcCosAngle = n0 & c1c0;
00137 
00138     if (own[f0] != cellI)
00139     {
00140         fcCosAngle = -fcCosAngle;
00141     }
00142 
00143     if (sinAngle < 0.0)
00144     {
00145         // Looking for concave angles (quadrant 3 or 4)
00146         if (fcCosAngle <= 0)
00147         {
00148             // Angle is convex so smaller.
00149             return false;
00150         }
00151         else
00152         {
00153             if (normalCosAngle < cosAngle)
00154             {
00155                 return false;
00156             }
00157             else
00158             {
00159                 return true;
00160             }
00161         }
00162     }
00163     else
00164     {
00165         // Looking for convex angles (quadrant 1 or 2)
00166         if (fcCosAngle > 0)
00167         {
00168             // Concave angle
00169             return true;
00170         }
00171         else
00172         {
00173             // Convex. Check cos of normal vectors.
00174             if (normalCosAngle > cosAngle)
00175             {
00176                 return false;
00177             }
00178             else
00179             {
00180                 return true;
00181             }
00182         }
00183     }
00184 }
00185 
00186 
00187 // Split hex (and hex only) along edgeI creating two prisms
00188 bool splitHex
00189 (
00190     const polyMesh& mesh,
00191     const label cellI,
00192     const label edgeI,
00193 
00194     DynamicList<label>& cutCells,
00195     DynamicList<labelList>& cellLoops,
00196     DynamicList<scalarField>& cellEdgeWeights
00197 )
00198 {
00199     // cut handling functions
00200     edgeVertex ev(mesh);
00201 
00202     const edgeList& edges = mesh.edges();
00203     const faceList& faces = mesh.faces();
00204 
00205     const edge& e = edges[edgeI];
00206 
00207     // Get faces on the side, i.e. faces not using edge but still using one of
00208     // the edge endpoints.
00209 
00210     label leftI = -1;
00211     label rightI = -1;
00212     label leftFp = -1;
00213     label rightFp = -1;
00214 
00215     const cell& cFaces = mesh.cells()[cellI];
00216 
00217     forAll(cFaces, i)
00218     {
00219         label faceI = cFaces[i];
00220 
00221         const face& f = faces[faceI];
00222 
00223         label fp0 = findIndex(f, e[0]);
00224         label fp1 = findIndex(f, e[1]);
00225 
00226         if (fp0 == -1)
00227         {
00228             if (fp1 != -1)
00229             {
00230                 // Face uses e[1] but not e[0]
00231                 rightI = faceI;
00232                 rightFp = fp1;
00233 
00234                 if (leftI != -1)
00235                 {
00236                     // Have both faces so exit
00237                     break;
00238                 }
00239             }
00240         }
00241         else
00242         {
00243             if (fp1 != -1)
00244             {
00245                 // Face uses both e[1] and e[0]
00246             }
00247             else
00248             {
00249                 leftI = faceI;
00250                 leftFp = fp0;
00251 
00252                 if (rightI != -1)
00253                 {
00254                     break;
00255                 }
00256             }
00257         }
00258     }
00259 
00260     if (leftI == -1 || rightI == -1)
00261     {
00262         FatalErrorIn("splitHex") << "Problem : leftI:" << leftI
00263             << " rightI:" << rightI << abort(FatalError);
00264     }
00265 
00266     // Walk two vertices further on faces.
00267 
00268     const face& leftF = faces[leftI];
00269 
00270     label leftV = leftF[(leftFp + 2) % leftF.size()];
00271 
00272     const face& rightF = faces[rightI];
00273 
00274     label rightV = rightF[(rightFp + 2) % rightF.size()];
00275 
00276     labelList loop(4);
00277     loop[0] = ev.vertToEVert(e[0]);
00278     loop[1] = ev.vertToEVert(leftV);
00279     loop[2] = ev.vertToEVert(rightV);
00280     loop[3] = ev.vertToEVert(e[1]);
00281 
00282     scalarField loopWeights(4);
00283     loopWeights[0] = -GREAT;
00284     loopWeights[1] = -GREAT;
00285     loopWeights[2] = -GREAT;
00286     loopWeights[3] = -GREAT;
00287 
00288     cutCells.append(cellI);
00289     cellLoops.append(loop);
00290     cellEdgeWeights.append(loopWeights);
00291 
00292     return true;
00293 }
00294 
00295 
00296 // Split cellI along edgeI with a plane along halfNorm direction.
00297 bool splitCell
00298 (
00299     const polyMesh& mesh,
00300     const geomCellLooper& cellCutter,
00301 
00302     const label cellI,
00303     const label edgeI,
00304     const vector& halfNorm,
00305 
00306     const boolList& vertIsCut,
00307     const boolList& edgeIsCut,
00308     const scalarField& edgeWeight,
00309 
00310     DynamicList<label>& cutCells,
00311     DynamicList<labelList>& cellLoops,
00312     DynamicList<scalarField>& cellEdgeWeights
00313 )
00314 {
00315     const edge& e = mesh.edges()[edgeI];
00316 
00317     vector eVec = e.vec(mesh.points());
00318     eVec /= mag(eVec);
00319 
00320     vector planeN = eVec ^ halfNorm;
00321 
00322     // Slightly tilt plane to make it not cut edges exactly
00323     // halfway on fully regular meshes (since we want cuts
00324     // to be snapped to vertices)
00325     planeN += 0.01*halfNorm;
00326 
00327     planeN /= mag(planeN);
00328 
00329     // Define plane through edge
00330     plane cutPlane(mesh.points()[e.start()], planeN);
00331 
00332     labelList loop;
00333     scalarField loopWeights;
00334 
00335     if
00336     (
00337         cellCutter.cut
00338         (
00339             cutPlane,
00340             cellI,
00341             vertIsCut,
00342             edgeIsCut,
00343             edgeWeight,
00344             loop,
00345             loopWeights
00346         )
00347     )
00348     {
00349         // Did manage to cut cell. Copy into overall list.
00350         cutCells.append(cellI);
00351         cellLoops.append(loop);
00352         cellEdgeWeights.append(loopWeights);
00353 
00354         return true;
00355     }
00356     else
00357     {
00358         return false;
00359     }
00360 }
00361 
00362 
00363 // Collects cuts for all cells in cellSet
00364 void collectCuts
00365 (
00366     const polyMesh& mesh,
00367     const geomCellLooper& cellCutter,
00368     const bool geometry,
00369     const scalar minCos,
00370     const scalar minSin,
00371     const cellSet& cellsToCut,
00372 
00373     DynamicList<label>& cutCells,
00374     DynamicList<labelList>& cellLoops,
00375     DynamicList<scalarField>& cellEdgeWeights
00376 )
00377 {
00378     // Get data from mesh
00379     const cellShapeList& cellShapes = mesh.cellShapes();
00380     const labelList& own = mesh.faceOwner();
00381     const labelListList& cellEdges = mesh.cellEdges();
00382     const vectorField& faceAreas = mesh.faceAreas();
00383 
00384     // Hex shape
00385     const cellModel& hex = *(cellModeller::lookup("hex"));
00386 
00387     // cut handling functions
00388     edgeVertex ev(mesh);
00389 
00390 
00391     // Cut information per mesh entity
00392     boolList vertIsCut(mesh.nPoints(), false);
00393     boolList edgeIsCut(mesh.nEdges(), false);
00394     scalarField edgeWeight(mesh.nEdges(), -GREAT);
00395 
00396     for
00397     (
00398         cellSet::const_iterator iter = cellsToCut.begin();
00399         iter != cellsToCut.end();
00400         ++iter
00401     )
00402     {
00403         label cellI = iter.key();
00404 
00405         const labelList& cEdges = cellEdges[cellI];
00406 
00407         forAll(cEdges, i)
00408         {
00409             label edgeI = cEdges[i];
00410 
00411             label f0, f1;
00412             meshTools::getEdgeFaces(mesh, cellI, edgeI, f0, f1);
00413 
00414             vector n0 = faceAreas[f0];
00415             n0 /= mag(n0);
00416 
00417             vector n1 = faceAreas[f1];
00418             n1 /= mag(n1);
00419 
00420             if
00421             (
00422                 largerAngle
00423                 (
00424                     mesh,
00425                     minCos,
00426                     minSin,
00427 
00428                     cellI,
00429                     f0,
00430                     f1,
00431                     n0,
00432                     n1
00433                 )
00434             )
00435             {
00436                 bool splitOk = false;
00437 
00438                 if (!geometry && cellShapes[cellI].model() == hex)
00439                 {
00440                     splitOk =
00441                         splitHex
00442                         (
00443                             mesh,
00444                             cellI,
00445                             edgeI,
00446 
00447                             cutCells,
00448                             cellLoops,
00449                             cellEdgeWeights
00450                         );
00451                 }
00452                 else
00453                 {
00454                     vector halfNorm;
00455 
00456                     if ((own[f0] == cellI) ^ (own[f1] == cellI))
00457                     {
00458                         // Opposite owner orientation
00459                         halfNorm = 0.5*(n0 - n1);
00460                     }
00461                     else
00462                     {
00463                         // Faces have same owner or same neighbour so
00464                         // normals point in same direction
00465                         halfNorm = 0.5*(n0 + n1);
00466                     }
00467 
00468                     splitOk =
00469                         splitCell
00470                         (
00471                             mesh,
00472                             cellCutter,
00473                             cellI,
00474                             edgeI,
00475                             halfNorm,
00476 
00477                             vertIsCut,
00478                             edgeIsCut,
00479                             edgeWeight,
00480 
00481                             cutCells,
00482                             cellLoops,
00483                             cellEdgeWeights
00484                         );
00485                 }
00486 
00487 
00488                 if (splitOk)
00489                 {
00490                     // Update cell/edge/vertex wise info.
00491                     label index = cellLoops.size() - 1;
00492                     const labelList& loop = cellLoops[index];
00493                     const scalarField& loopWeights = cellEdgeWeights[index];
00494 
00495                     forAll(loop, i)
00496                     {
00497                         label cut = loop[i];
00498 
00499                         if (ev.isEdge(cut))
00500                         {
00501                             edgeIsCut[ev.getEdge(cut)] = true;
00502                             edgeWeight[ev.getEdge(cut)] = loopWeights[i];
00503                         }
00504                         else
00505                         {
00506                             vertIsCut[ev.getVertex(cut)] = true;
00507                         }
00508                     }
00509 
00510                     // Stop checking edges for this cell.
00511                     break;
00512                 }
00513             }
00514         }
00515     }
00516 
00517     cutCells.shrink();
00518     cellLoops.shrink();
00519     cellEdgeWeights.shrink();
00520 }
00521 
00522 
00523 // Main program:
00524 
00525 int main(int argc, char *argv[])
00526 {
00527     argList::noParallel();
00528     argList::validOptions.insert("set", "cellSet name");
00529     argList::validOptions.insert("geometry", "");
00530     argList::validOptions.insert("tol", "edge snap tolerance");
00531     argList::validOptions.insert("overwrite", "");
00532     argList::validArgs.append("edge angle [0..360]");
00533 
00534 #   include "setRootCase.H"
00535 #   include "createTime.H"
00536     runTime.functionObjects().off();
00537 #   include "createPolyMesh.H"
00538     const word oldInstance = mesh.pointsInstance();
00539 
00540     scalar featureAngle(readScalar(IStringStream(args.additionalArgs()[0])()));
00541 
00542     scalar radAngle = featureAngle * mathematicalConstant::pi/180.0;
00543     scalar minCos = Foam::cos(radAngle);
00544     scalar minSin = Foam::sin(radAngle);
00545 
00546     bool readSet   = args.optionFound("set");
00547     bool geometry  = args.optionFound("geometry");
00548     bool overwrite = args.optionFound("overwrite");
00549 
00550     scalar edgeTol = 0.2;
00551     args.optionReadIfPresent("tol", edgeTol);
00552 
00553     Info<< "Trying to split cells with internal angles > feature angle\n" << nl
00554         << "featureAngle      : " << featureAngle << nl
00555         << "edge snapping tol : " << edgeTol << nl;
00556     if (readSet)
00557     {
00558         Info<< "candidate cells   : cellSet " << args.option("set") << nl;
00559     }
00560     else
00561     {
00562         Info<< "candidate cells   : all cells" << nl;
00563     }
00564     if (geometry)
00565     {
00566         Info<< "hex cuts          : geometric; using edge tolerance" << nl;
00567     }
00568     else
00569     {
00570         Info<< "hex cuts          : topological; cut to opposite edge" << nl;
00571     }
00572     Info<< endl;
00573 
00574 
00575     // Cell circumference cutter
00576     geomCellLooper cellCutter(mesh);
00577     // Snap all edge cuts close to endpoints to vertices.
00578     geomCellLooper::setSnapTol(edgeTol);
00579 
00580     // Candidate cells to cut
00581     cellSet cellsToCut(mesh, "cellsToCut", mesh.nCells()/100);
00582 
00583     if (readSet)
00584     {
00585         // Read cells to cut from cellSet
00586         cellSet cells(mesh, args.option("set"));
00587 
00588         cellsToCut = cells;
00589     }
00590 
00591     while (true)
00592     {
00593         if (!readSet)
00594         {
00595             // Try all cells for cutting
00596             for (label cellI = 0; cellI < mesh.nCells(); cellI++)
00597             {
00598                 cellsToCut.insert(cellI);
00599             }
00600         }
00601 
00602 
00603         // Cut information per cut cell
00604         DynamicList<label> cutCells(mesh.nCells()/10 + 10);
00605         DynamicList<labelList> cellLoops(mesh.nCells()/10 + 10);
00606         DynamicList<scalarField> cellEdgeWeights(mesh.nCells()/10 + 10);
00607 
00608         collectCuts
00609         (
00610             mesh,
00611             cellCutter,
00612             geometry,
00613             minCos,
00614             minSin,
00615             cellsToCut,
00616 
00617             cutCells,
00618             cellLoops,
00619             cellEdgeWeights
00620         );
00621 
00622         cellSet cutSet(mesh, "cutSet", cutCells.size());
00623         forAll(cutCells, i)
00624         {
00625             cutSet.insert(cutCells[i]);
00626         }
00627 
00628         // Gets cuts across cells from cuts through edges.
00629         Info<< "Writing " << cutSet.size() << " cells to cut to cellSet "
00630             << cutSet.instance()/cutSet.local()/cutSet.name()
00631             << endl << endl;
00632         cutSet.write();
00633 
00634         // Analyze cuts for clashes.
00635         cellCuts cuts
00636         (
00637             mesh,
00638             cutCells,       // cells candidate for cutting
00639             cellLoops,
00640             cellEdgeWeights
00641         );
00642 
00643         Info<< "Actually cut cells:" << cuts.nLoops() << nl << endl;
00644 
00645         if (cuts.nLoops() == 0)
00646         {
00647             break;
00648         }
00649 
00650         // Remove cut cells from cellsToCut  (Note:only relevant if -readSet)
00651         forAll(cuts.cellLoops(), cellI)
00652         {
00653             if (cuts.cellLoops()[cellI].size())
00654             {
00655                 //Info<< "Removing cut cell " << cellI << " from wishlist"
00656                 //    << endl;
00657                 cellsToCut.erase(cellI);
00658             }
00659         }
00660 
00661         // At least some cells are cut.
00662         polyTopoChange meshMod(mesh);
00663 
00664         // Cutting engine
00665         meshCutter cutter(mesh);
00666 
00667         // Insert mesh refinement into polyTopoChange.
00668         cutter.setRefinement(cuts, meshMod);
00669 
00670         // Do all changes
00671         Info<< "Morphing ..." << endl;
00672 
00673         if (!overwrite)
00674         {
00675             runTime++;
00676         }
00677 
00678         autoPtr<mapPolyMesh> morphMap = meshMod.changeMesh(mesh, false);
00679 
00680         if (morphMap().hasMotionPoints())
00681         {
00682             mesh.movePoints(morphMap().preMotionPoints());
00683         }
00684 
00685         // Update stored labels on meshCutter
00686         cutter.updateMesh(morphMap());
00687 
00688         // Update cellSet
00689         cellsToCut.updateMesh(morphMap());
00690 
00691         Info<< "Remaining:" << cellsToCut.size() << endl;
00692 
00693         // Write resulting mesh
00694         if (overwrite)
00695         {
00696             mesh.setInstance(oldInstance);
00697         }
00698 
00699         Info<< "Writing refined morphMesh to time " << runTime.timeName()
00700             << endl;
00701 
00702         mesh.write();
00703     }
00704 
00705     Info << "End\n" << endl;
00706 
00707     return 0;
00708 }
00709 
00710 
00711 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd