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

meshCutAndRemove.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "meshCutAndRemove.H"
00028 #include "polyMesh.H"
00029 #include "polyTopoChange.H"
00030 #include "polyAddFace.H"
00031 #include "polyAddPoint.H"
00032 #include "polyRemovePoint.H"
00033 #include "polyRemoveFace.H"
00034 #include "polyModifyFace.H"
00035 #include "cellCuts.H"
00036 #include "mapPolyMesh.H"
00037 #include "meshTools.H"
00038 
00039 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00040 
00041 defineTypeNameAndDebug(Foam::meshCutAndRemove, 0);
00042 
00043 // * * * * * * * * * * * * * Private Static Functions  * * * * * * * * * * * //
00044 
00045 // Returns -1 or index in elems1 of first shared element.
00046 Foam::label Foam::meshCutAndRemove::firstCommon
00047 (
00048     const labelList& elems1,
00049     const labelList& elems2
00050 )
00051 {
00052     forAll(elems1, elemI)
00053     {
00054         label index1 = findIndex(elems2, elems1[elemI]);
00055 
00056         if (index1 != -1)
00057         {
00058             return index1;
00059         }
00060     }
00061     return -1;
00062 }
00063 
00064 
00065 // Check if twoCuts at two consecutive position in cuts.
00066 bool Foam::meshCutAndRemove::isIn
00067 (
00068     const edge& twoCuts,
00069     const labelList& cuts
00070 )
00071 {
00072     label index = findIndex(cuts, twoCuts[0]);
00073 
00074     if (index == -1)
00075     {
00076         return false;
00077     }
00078 
00079     return
00080     (
00081         cuts[cuts.fcIndex(index)] == twoCuts[1]
00082      || cuts[cuts.rcIndex(index)] == twoCuts[1]
00083     );
00084 }
00085 
00086 
00087 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00088 
00089 // Returns the cell in cellLabels that is cut. Or -1.
00090 Foam::label Foam::meshCutAndRemove::findCutCell
00091 (
00092     const cellCuts& cuts,
00093     const labelList& cellLabels
00094 ) const
00095 {
00096     forAll(cellLabels, labelI)
00097     {
00098         label cellI = cellLabels[labelI];
00099 
00100         if (cuts.cellLoops()[cellI].size())
00101         {
00102             return cellI;
00103         }
00104     }
00105     return -1;
00106 }
00107 
00108 
00109 //- Returns first pointI in pointLabels that uses an internal
00110 //  face. Used to find point to inflate cell/face from (has to be
00111 //  connected to internal face). Returns -1 (so inflate from nothing) if
00112 //  none found.
00113 Foam::label Foam::meshCutAndRemove::findInternalFacePoint
00114 (
00115     const labelList& pointLabels
00116 ) const
00117 {
00118     forAll(pointLabels, labelI)
00119     {
00120         label pointI = pointLabels[labelI];
00121 
00122         const labelList& pFaces = mesh().pointFaces()[pointI];
00123 
00124         forAll(pFaces, pFaceI)
00125         {
00126             label faceI = pFaces[pFaceI];
00127 
00128             if (mesh().isInternalFace(faceI))
00129             {
00130                 return pointI;
00131             }
00132         }
00133     }
00134 
00135     if (pointLabels.empty())
00136     {
00137         FatalErrorIn("meshCutAndRemove::findInternalFacePoint(const labelList&)")
00138             << "Empty pointLabels" << abort(FatalError);
00139     }
00140 
00141     return -1;
00142 }
00143 
00144 
00145 // Find point on face that is part of original mesh and that is point connected
00146 // to the patch
00147 Foam::label Foam::meshCutAndRemove::findPatchFacePoint
00148 (
00149     const face& f,
00150     const label exposedPatchI
00151 ) const
00152 {
00153     const labelListList& pointFaces = mesh().pointFaces();
00154     const polyBoundaryMesh& patches = mesh().boundaryMesh();
00155 
00156     forAll(f, fp)
00157     {
00158         label pointI = f[fp];
00159 
00160         if (pointI < mesh().nPoints())
00161         {
00162             const labelList& pFaces = pointFaces[pointI];
00163 
00164             forAll(pFaces, i)
00165             {
00166                 if (patches.whichPatch(pFaces[i]) == exposedPatchI)
00167                 {
00168                     return pointI;
00169                 }
00170             }
00171         }
00172     }
00173     return -1;
00174 }
00175 
00176 
00177 // Get new owner and neighbour of face. Checks anchor points to see if
00178 // cells have been removed.
00179 void Foam::meshCutAndRemove::faceCells
00180 (
00181     const cellCuts& cuts,
00182     const label exposedPatchI,
00183     const label faceI,
00184     label& own,
00185     label& nei,
00186     label& patchID
00187 ) const
00188 {
00189     const labelListList& anchorPts = cuts.cellAnchorPoints();
00190     const labelListList& cellLoops = cuts.cellLoops();
00191 
00192     const face& f = mesh().faces()[faceI];
00193 
00194     own = mesh().faceOwner()[faceI];
00195 
00196     if (cellLoops[own].size() && firstCommon(f, anchorPts[own]) == -1)
00197     {
00198         // owner has been split and this is the removed part.
00199         own = -1;
00200     }
00201 
00202     nei = -1;
00203 
00204     if (mesh().isInternalFace(faceI))
00205     {
00206         nei = mesh().faceNeighbour()[faceI];
00207 
00208         if (cellLoops[nei].size() && firstCommon(f, anchorPts[nei]) == -1)
00209         {
00210             nei = -1;
00211         }
00212     }
00213 
00214     patchID = mesh().boundaryMesh().whichPatch(faceI);
00215 
00216     if (patchID == -1 && (own == -1 || nei == -1))
00217     {
00218         // Face was internal but becomes external
00219         patchID = exposedPatchI;
00220     }
00221 }
00222 
00223 
00224 void Foam::meshCutAndRemove::getZoneInfo
00225 (
00226     const label faceI,
00227     label& zoneID,
00228     bool& zoneFlip
00229 ) const
00230 {
00231     zoneID = mesh().faceZones().whichZone(faceI);
00232 
00233     zoneFlip = false;
00234 
00235     if (zoneID >= 0)
00236     {
00237         const faceZone& fZone = mesh().faceZones()[zoneID];
00238 
00239         zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00240     }
00241 }
00242 
00243 
00244 // Adds a face from point.
00245 void Foam::meshCutAndRemove::addFace
00246 (
00247     polyTopoChange& meshMod,
00248     const label faceI,
00249     const label masterPointI,
00250     const face& newFace,
00251     const label own,
00252     const label nei,
00253     const label patchID
00254 )
00255 {
00256     label zoneID;
00257     bool zoneFlip;
00258 
00259     getZoneInfo(faceI, zoneID, zoneFlip);
00260 
00261     if ((nei == -1) || (own != -1 && own < nei))
00262     {
00263         // Ordering ok.
00264         if (debug & 2)
00265         {
00266             Pout<< "Adding face " << newFace
00267                 << " with new owner:" << own
00268                 << " with new neighbour:" << nei
00269                 << " patchID:" << patchID
00270                 << " anchor:" << masterPointI
00271                 << " zoneID:" << zoneID
00272                 << " zoneFlip:" << zoneFlip
00273                 << endl;
00274         }
00275 
00276         meshMod.setAction
00277         (
00278             polyAddFace
00279             (
00280                 newFace,                    // face
00281                 own,                        // owner
00282                 nei,                        // neighbour
00283                 masterPointI,               // master point
00284                 -1,                         // master edge
00285                 -1,                         // master face for addition
00286                 false,                      // flux flip
00287                 patchID,                    // patch for face
00288                 zoneID,                     // zone for face
00289                 zoneFlip                    // face zone flip
00290             )
00291         );
00292     }
00293     else
00294     {
00295         // Reverse owner/neighbour
00296         if (debug & 2)
00297         {
00298             Pout<< "Adding (reversed) face " << newFace.reverseFace()
00299                 << " with new owner:" << nei
00300                 << " with new neighbour:" << own
00301                 << " patchID:" << patchID
00302                 << " anchor:" << masterPointI
00303                 << " zoneID:" << zoneID
00304                 << " zoneFlip:" << zoneFlip
00305                 << endl;
00306         }
00307 
00308         meshMod.setAction
00309         (
00310             polyAddFace
00311             (
00312                 newFace.reverseFace(),      // face
00313                 nei,                        // owner
00314                 own,                        // neighbour
00315                 masterPointI,               // master point
00316                 -1,                         // master edge
00317                 -1,                         // master face for addition
00318                 false,                      // flux flip
00319                 patchID,                    // patch for face
00320                 zoneID,                     // zone for face
00321                 zoneFlip                    // face zone flip
00322             )
00323         );
00324     }
00325 }
00326 
00327 
00328 // Modifies existing faceI for either new owner/neighbour or new face points.
00329 void Foam::meshCutAndRemove::modFace
00330 (
00331     polyTopoChange& meshMod,
00332     const label faceI,
00333     const face& newFace,
00334     const label own,
00335     const label nei,
00336     const label patchID
00337 )
00338 {
00339     label zoneID;
00340     bool zoneFlip;
00341 
00342     getZoneInfo(faceI, zoneID, zoneFlip);
00343 
00344     if
00345     (
00346         (own != mesh().faceOwner()[faceI])
00347      || (
00348             mesh().isInternalFace(faceI)
00349          && (nei != mesh().faceNeighbour()[faceI])
00350         )
00351      || (newFace != mesh().faces()[faceI])
00352     )
00353     {
00354         if (debug & 2)
00355         {
00356             Pout<< "Modifying face " << faceI
00357                 << " old vertices:" << mesh().faces()[faceI]
00358                 << " new vertices:" << newFace
00359                 << " new owner:" << own
00360                 << " new neighbour:" << nei
00361                 << " new patch:" << patchID
00362                 << " new zoneID:" << zoneID
00363                 << " new zoneFlip:" << zoneFlip
00364                 << endl;
00365         }
00366 
00367         if ((nei == -1) || (own != -1 && own < nei))
00368         {
00369             meshMod.setAction
00370             (
00371                 polyModifyFace
00372                 (
00373                     newFace,            // modified face
00374                     faceI,              // label of face being modified
00375                     own,                // owner
00376                     nei,                // neighbour
00377                     false,              // face flip
00378                     patchID,            // patch for face
00379                     false,              // remove from zone
00380                     zoneID,             // zone for face
00381                     zoneFlip            // face flip in zone
00382                 )
00383             );
00384         }
00385         else
00386         {
00387             meshMod.setAction
00388             (
00389                 polyModifyFace
00390                 (
00391                     newFace.reverseFace(),  // modified face
00392                     faceI,                  // label of face being modified
00393                     nei,                    // owner
00394                     own,                    // neighbour
00395                     false,                  // face flip
00396                     patchID,                // patch for face
00397                     false,                  // remove from zone
00398                     zoneID,                 // zone for face
00399                     zoneFlip                // face flip in zone
00400                 )
00401             );
00402         }
00403     }
00404 }
00405 
00406 
00407 // Copies face starting from startFp up to and including endFp.
00408 void Foam::meshCutAndRemove::copyFace
00409 (
00410     const face& f,
00411     const label startFp,
00412     const label endFp,
00413     face& newFace
00414 ) const
00415 {
00416     label fp = startFp;
00417 
00418     label newFp = 0;
00419 
00420     while (fp != endFp)
00421     {
00422         newFace[newFp++] = f[fp];
00423 
00424         fp = (fp + 1) % f.size();
00425     }
00426     newFace[newFp] = f[fp];
00427 }
00428 
00429 
00430 // Actually split face in two along splitEdge v0, v1 (the two vertices in new
00431 // vertex numbering). Generates faces in same ordering
00432 // as original face. Replaces cutEdges by the points introduced on them
00433 // (addedPoints_).
00434 void Foam::meshCutAndRemove::splitFace
00435 (
00436     const face& f,
00437     const label v0,
00438     const label v1,
00439 
00440     face& f0,
00441     face& f1
00442 ) const
00443 {
00444     // Check if we find any new vertex which is part of the splitEdge.
00445     label startFp = findIndex(f, v0);
00446 
00447     if (startFp == -1)
00448     {
00449         FatalErrorIn
00450         (
00451             "meshCutAndRemove::splitFace"
00452             ", const face&, const label, const label, face&, face&)"
00453         )   << "Cannot find vertex (new numbering) " << v0
00454             << " on face " << f
00455             << abort(FatalError);
00456     }
00457 
00458     label endFp = findIndex(f, v1);
00459 
00460     if (endFp == -1)
00461     {
00462         FatalErrorIn
00463         (
00464             "meshCutAndRemove::splitFace("
00465             ", const face&, const label, const label, face&, face&)"
00466         )   << "Cannot find vertex (new numbering) " << v1
00467             << " on face " << f
00468             << abort(FatalError);
00469     }
00470 
00471 
00472     f0.setSize((endFp + 1 + f.size() - startFp) % f.size());
00473     f1.setSize(f.size() - f0.size() + 2);
00474 
00475     copyFace(f, startFp, endFp, f0);
00476     copyFace(f, endFp, startFp, f1);
00477 }
00478 
00479 
00480 // Adds additional vertices (from edge cutting) to face. Used for faces which
00481 // are not split but still might use edge that has been cut.
00482 Foam::face Foam::meshCutAndRemove::addEdgeCutsToFace(const label faceI) const
00483 {
00484     const face& f = mesh().faces()[faceI];
00485 
00486     face newFace(2 * f.size());
00487 
00488     label newFp = 0;
00489 
00490     forAll(f, fp)
00491     {
00492         // Duplicate face vertex.
00493         newFace[newFp++] = f[fp];
00494 
00495         // Check if edge has been cut.
00496         label fp1 = f.fcIndex(fp);
00497 
00498         HashTable<label, edge, Hash<edge> >::const_iterator fnd =
00499             addedPoints_.find(edge(f[fp], f[fp1]));
00500 
00501         if (fnd != addedPoints_.end())
00502         {
00503             // edge has been cut. Introduce new vertex.
00504             newFace[newFp++] = fnd();
00505         }
00506     }
00507 
00508     newFace.setSize(newFp);
00509 
00510     return newFace;
00511 }
00512 
00513 
00514 // Walk loop (loop of cuts) across circumference of cellI. Returns face in
00515 // new vertices.
00516 // Note: tricky bit is that it can use existing edges which have been split.
00517 Foam::face Foam::meshCutAndRemove::loopToFace
00518 (
00519     const label cellI,
00520     const labelList& loop
00521 ) const
00522 {
00523     face newFace(2*loop.size());
00524 
00525     label newFaceI = 0;
00526 
00527     forAll(loop, fp)
00528     {
00529         label cut = loop[fp];
00530 
00531         if (isEdge(cut))
00532         {
00533             label edgeI = getEdge(cut);
00534 
00535             const edge& e = mesh().edges()[edgeI];
00536 
00537             label vertI = addedPoints_[e];
00538 
00539             newFace[newFaceI++] = vertI;
00540         }
00541         else
00542         {
00543             // cut is vertex.
00544             label vertI = getVertex(cut);
00545 
00546             newFace[newFaceI++] = vertI;
00547 
00548             label nextCut = loop[loop.fcIndex(fp)];
00549 
00550             if (!isEdge(nextCut))
00551             {
00552                 // From vertex to vertex -> cross cut only if no existing edge.
00553                 label nextVertI = getVertex(nextCut);
00554 
00555                 label edgeI = meshTools::findEdge(mesh(), vertI, nextVertI);
00556 
00557                 if (edgeI != -1)
00558                 {
00559                     // Existing edge. Insert split-edge point if any.
00560                     HashTable<label, edge, Hash<edge> >::const_iterator fnd =
00561                         addedPoints_.find(mesh().edges()[edgeI]);
00562 
00563                     if (fnd != addedPoints_.end())
00564                     {
00565                         newFace[newFaceI++] = fnd();
00566                     }
00567                 }
00568             }
00569         }
00570     }
00571     newFace.setSize(newFaceI);
00572 
00573     return newFace;
00574 }
00575 
00576 
00577 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00578 
00579 // Construct from components
00580 Foam::meshCutAndRemove::meshCutAndRemove(const polyMesh& mesh)
00581 :
00582     edgeVertex(mesh),
00583     addedFaces_(),
00584     addedPoints_()
00585 {}
00586 
00587 
00588 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00589 
00590 void Foam::meshCutAndRemove::setRefinement
00591 (
00592     const label exposedPatchI,
00593     const cellCuts& cuts,
00594     const labelList& cutPatch,
00595     polyTopoChange& meshMod
00596 )
00597 {
00598     // Clear and size maps here since mesh size will change.
00599     addedFaces_.clear();
00600     addedFaces_.resize(cuts.nLoops());
00601 
00602     addedPoints_.clear();
00603     addedPoints_.resize(cuts.nLoops());
00604 
00605     if (cuts.nLoops() == 0)
00606     {
00607         return;
00608     }
00609 
00610     const labelListList& anchorPts = cuts.cellAnchorPoints();
00611     const labelListList& cellLoops = cuts.cellLoops();
00612     const polyBoundaryMesh& patches = mesh().boundaryMesh();
00613 
00614     if (exposedPatchI < 0 || exposedPatchI >= patches.size())
00615     {
00616         FatalErrorIn
00617         (
00618             "meshCutAndRemove::setRefinement("
00619             ", const label, const cellCuts&, const labelList&"
00620             ", polyTopoChange&)"
00621         )   << "Illegal exposed patch " << exposedPatchI
00622             << abort(FatalError);
00623     }
00624 
00625 
00626     //
00627     // Add new points along cut edges.
00628     //
00629 
00630     forAll(cuts.edgeIsCut(), edgeI)
00631     {
00632         if (cuts.edgeIsCut()[edgeI])
00633         {
00634             const edge& e = mesh().edges()[edgeI];
00635 
00636             // Check if there is any cell using this edge.
00637             if (debug && findCutCell(cuts, mesh().edgeCells()[edgeI]) == -1)
00638             {
00639                 FatalErrorIn
00640                 (
00641                     "meshCutAndRemove::setRefinement("
00642                     ", const label, const cellCuts&, const labelList&"
00643                     ", polyTopoChange&)"
00644                 )   << "Problem: cut edge but none of the cells using it is\n"
00645                     << "edge:" << edgeI << " verts:" << e
00646                     << abort(FatalError);
00647             }
00648 
00649             // One of the edge end points should be master point of nbCellI.
00650             label masterPointI = e.start();
00651 
00652             const point& v0 = mesh().points()[e.start()];
00653             const point& v1 = mesh().points()[e.end()];
00654 
00655             scalar weight = cuts.edgeWeight()[edgeI];
00656 
00657             point newPt = weight*v1 + (1.0-weight)*v0;
00658 
00659             label addedPointI =
00660                 meshMod.setAction
00661                 (
00662                     polyAddPoint
00663                     (
00664                         newPt,              // point
00665                         masterPointI,       // master point
00666                         -1,                 // zone for point
00667                         true                // supports a cell
00668                     )
00669                 );
00670 
00671             // Store on (hash of) edge.
00672             addedPoints_.insert(e, addedPointI);
00673 
00674             if (debug & 2)
00675             {
00676                 Pout<< "Added point " << addedPointI
00677                     << " to vertex "
00678                     << masterPointI << " of edge " << edgeI
00679                     << " vertices " << e << endl;
00680             }
00681         }
00682     }
00683 
00684 
00685     //
00686     // Remove all points that will not be used anymore
00687     //
00688     {
00689         boolList usedPoint(mesh().nPoints(), false);
00690 
00691         forAll(cellLoops, cellI)
00692         {
00693             const labelList& loop = cellLoops[cellI];
00694 
00695             if (loop.size())
00696             {
00697                 // Cell is cut. Uses only anchor points and loop itself.
00698                 forAll(loop, fp)
00699                 {
00700                     label cut = loop[fp];
00701 
00702                     if (!isEdge(cut))
00703                     {
00704                         usedPoint[getVertex(cut)] = true;
00705                     }
00706                 }
00707 
00708                 const labelList& anchors = anchorPts[cellI];
00709 
00710                 forAll(anchors, i)
00711                 {
00712                     usedPoint[anchors[i]] = true;
00713                 }
00714             }
00715             else
00716             {
00717                 // Cell is not cut so use all its points
00718                 const labelList& cPoints = mesh().cellPoints()[cellI];
00719 
00720                 forAll(cPoints, i)
00721                 {
00722                     usedPoint[cPoints[i]] = true;
00723                 }
00724             }
00725         }
00726 
00727 
00728         // Check
00729         const Map<edge>& faceSplitCut = cuts.faceSplitCut();
00730 
00731         forAllConstIter(Map<edge>, faceSplitCut, iter)
00732         {
00733             const edge& fCut = iter();
00734 
00735             forAll(fCut, i)
00736             {
00737                 label cut = fCut[i];
00738 
00739                 if (!isEdge(cut))
00740                 {
00741                     label pointI = getVertex(cut);
00742 
00743                     if (!usedPoint[pointI])
00744                     {
00745                         FatalErrorIn
00746                         (
00747                             "meshCutAndRemove::setRefinement("
00748                             ", const label, const cellCuts&, const labelList&"
00749                             ", polyTopoChange&)"
00750                         )   << "Problem: faceSplitCut not used by any loop"
00751                             << " or cell anchor point"
00752                             << "face:" << iter.key() << " point:" << pointI
00753                             << " coord:" << mesh().points()[pointI]
00754                             << abort(FatalError);
00755                     }
00756                 }
00757             }
00758         }
00759 
00760         forAll(cuts.pointIsCut(), pointI)
00761         {
00762             if (cuts.pointIsCut()[pointI])
00763             {
00764                 if (!usedPoint[pointI])
00765                 {
00766                     FatalErrorIn
00767                     (
00768                         "meshCutAndRemove::setRefinement("
00769                         ", const label, const cellCuts&, const labelList&"
00770                         ", polyTopoChange&)"
00771                     )   << "Problem: point is marked as cut but"
00772                         << " not used by any loop"
00773                         << " or cell anchor point"
00774                         << "point:" << pointI
00775                         << " coord:" << mesh().points()[pointI]
00776                         << abort(FatalError);
00777                 }
00778             }
00779         }
00780 
00781 
00782         // Remove unused points.
00783         forAll(usedPoint, pointI)
00784         {
00785             if (!usedPoint[pointI])
00786             {
00787                 meshMod.setAction(polyRemovePoint(pointI));
00788 
00789                 if (debug & 2)
00790                 {
00791                     Pout<< "Removing unused point " << pointI << endl;
00792                 }
00793             }
00794         }
00795     }
00796 
00797 
00798     //
00799     // For all cut cells add an internal or external face
00800     //
00801 
00802     forAll(cellLoops, cellI)
00803     {
00804         const labelList& loop = cellLoops[cellI];
00805 
00806         if (loop.size())
00807         {
00808             if (cutPatch[cellI] < 0 || cutPatch[cellI] >= patches.size())
00809             {
00810                 FatalErrorIn
00811                 (
00812                     "meshCutAndRemove::setRefinement("
00813                     ", const label, const cellCuts&, const labelList&"
00814                     ", polyTopoChange&)"
00815                 )   << "Illegal patch " << cutPatch[cellI]
00816                     << " provided for cut cell " << cellI
00817                     << abort(FatalError);
00818             }
00819 
00820             //
00821             // Convert loop (=list of cuts) into proper face.
00822             // cellCuts sets orientation is towards anchor side so reverse.
00823             //
00824             face newFace(loopToFace(cellI, loop));
00825 
00826             reverse(newFace);
00827 
00828             // Pick any anchor point on cell
00829             label masterPointI = findPatchFacePoint(newFace, exposedPatchI);
00830 
00831             label addedFaceI =
00832                 meshMod.setAction
00833                 (
00834                     polyAddFace
00835                     (
00836                         newFace,                // face
00837                         cellI,                  // owner
00838                         -1,                     // neighbour
00839                         masterPointI,           // master point
00840                         -1,                     // master edge
00841                         -1,                     // master face for addition
00842                         false,                  // flux flip
00843                         cutPatch[cellI],        // patch for face
00844                         -1,                     // zone for face
00845                         false                   // face zone flip
00846                     )
00847                 );
00848 
00849             addedFaces_.insert(cellI, addedFaceI);
00850 
00851             if (debug & 2)
00852             {
00853                 Pout<< "Added splitting face " << newFace << " index:"
00854                     << addedFaceI << " from masterPoint:" << masterPointI
00855                     << " to owner " << cellI << " with anchors:"
00856                     << anchorPts[cellI]
00857                     << " from Loop:";
00858 
00859                 // Gets edgeweights of loop
00860                 scalarField weights(loop.size());
00861                 forAll(loop, i)
00862                 {
00863                     label cut = loop[i];
00864 
00865                     weights[i] =
00866                     (
00867                         isEdge(cut)
00868                       ? cuts.edgeWeight()[getEdge(cut)]
00869                       : -GREAT
00870                     );
00871                 }
00872                 writeCuts(Pout, loop, weights);
00873                 Pout<< endl;
00874             }
00875         }
00876     }
00877 
00878 
00879     //
00880     // Modify faces to use only anchorpoints and loop points
00881     // (so throw away part without anchorpoints)
00882     //
00883 
00884 
00885     // Maintain whether face has been updated (for -split edges
00886     // -new owner/neighbour)
00887     boolList faceUptodate(mesh().nFaces(), false);
00888 
00889     const Map<edge>& faceSplitCuts = cuts.faceSplitCut();
00890 
00891     for
00892     (
00893         Map<edge>::const_iterator iter = faceSplitCuts.begin();
00894         iter != faceSplitCuts.end();
00895         ++iter
00896     )
00897     {
00898         label faceI = iter.key();
00899 
00900         // Renumber face to include split edges.
00901         face newFace(addEdgeCutsToFace(faceI));
00902 
00903         // Edge splitting the face. Convert edge to new vertex numbering.
00904         const edge& splitEdge = iter();
00905 
00906         label cut0 = splitEdge[0];
00907 
00908         label v0;
00909         if (isEdge(cut0))
00910         {
00911             label edgeI = getEdge(cut0);
00912             v0 = addedPoints_[mesh().edges()[edgeI]];
00913         }
00914         else
00915         {
00916             v0 = getVertex(cut0);
00917         }
00918 
00919         label cut1 = splitEdge[1];
00920         label v1;
00921         if (isEdge(cut1))
00922         {
00923             label edgeI = getEdge(cut1);
00924             v1 = addedPoints_[mesh().edges()[edgeI]];
00925         }
00926         else
00927         {
00928             v1 = getVertex(cut1);
00929         }
00930 
00931         // Split face along the elements of the splitEdge.
00932         face f0, f1;
00933         splitFace(newFace, v0, v1, f0, f1);
00934 
00935         label own = mesh().faceOwner()[faceI];
00936 
00937         label nei = -1;
00938 
00939         if (mesh().isInternalFace(faceI))
00940         {
00941             nei = mesh().faceNeighbour()[faceI];
00942         }
00943 
00944         if (debug & 2)
00945         {
00946             Pout<< "Split face " << mesh().faces()[faceI]
00947                 << " own:" << own << " nei:" << nei
00948                 << " into f0:" << f0
00949                 << " and f1:" << f1 << endl;
00950         }
00951 
00952 
00953         // Check which cell using face uses anchorPoints (so is kept)
00954         // and which one doesn't (gets removed)
00955 
00956         // Bit tricky. We have to know whether this faceSplit splits owner/
00957         // neighbour or both. Even if cell is cut we have to make sure this is
00958         // the one that cuts it (this face cut might not be the one splitting
00959         // the cell)
00960         // The face f gets split into two parts, f0 and f1.
00961         // Each of these can have a different owner and or neighbour.
00962 
00963         const face& f = mesh().faces()[faceI];
00964 
00965         label f0Own = -1;
00966         label f1Own = -1;
00967 
00968         if (cellLoops[own].empty())
00969         {
00970             // Owner side is not split so keep both halves.
00971             f0Own = own;
00972             f1Own = own;
00973         }
00974         else if (isIn(splitEdge, cellLoops[own]))
00975         {
00976             // Owner is cut by this splitCut. See which of f0, f1 gets
00977             // preserved and becomes owner, and which gets removed.
00978             if (firstCommon(f0, anchorPts[own]) != -1)
00979             {
00980                 // f0 preserved so f1 gets deleted
00981                 f0Own = own;
00982                 f1Own = -1;
00983             }
00984             else
00985             {
00986                 f0Own = -1;
00987                 f1Own = own;
00988             }
00989         }
00990         else
00991         {
00992             // Owner not cut by this splitCut but by another.
00993             // Check on original face whether
00994             // use anchorPts.
00995             if (firstCommon(f, anchorPts[own]) != -1)
00996             {
00997                 // both f0 and f1 owner side preserved
00998                 f0Own = own;
00999                 f1Own = own;
01000             }
01001             else
01002             {
01003                 // both f0 and f1 owner side removed
01004                 f0Own = -1;
01005                 f1Own = -1;
01006             }
01007         }
01008 
01009 
01010         label f0Nei = -1;
01011         label f1Nei = -1;
01012 
01013         if (nei != -1)
01014         {
01015             if (cellLoops[nei].empty())
01016             {
01017                 f0Nei = nei;
01018                 f1Nei = nei;
01019             }
01020             else if (isIn(splitEdge, cellLoops[nei]))
01021             {
01022                 // Neighbour is cut by this splitCut. So anchor part of it
01023                 // gets kept, non-anchor bit gets removed. See which of f0, f1
01024                 // connects to which part.
01025 
01026                 if (firstCommon(f0, anchorPts[nei]) != -1)
01027                 {
01028                     f0Nei = nei;
01029                     f1Nei = -1;
01030                 }
01031                 else
01032                 {
01033                     f0Nei = -1;
01034                     f1Nei = nei;
01035                 }
01036             }
01037             else
01038             {
01039                 // neighbour not cut by this splitCut. Check on original face
01040                 // whether use anchorPts.
01041 
01042                 if (firstCommon(f, anchorPts[nei]) != -1)
01043                 {
01044                     f0Nei = nei;
01045                     f1Nei = nei;
01046                 }
01047                 else
01048                 {
01049                     // both f0 and f1 on neighbour side removed
01050                     f0Nei = -1;
01051                     f1Nei = -1;
01052                 }
01053             }
01054         }
01055 
01056 
01057         if (debug & 2)
01058         {
01059             Pout<< "f0 own:" << f0Own << " nei:" << f0Nei
01060                 << "  f1 own:" << f1Own << " nei:" << f1Nei
01061                 << endl;
01062         }
01063 
01064 
01065         // If faces were internal but now become external set a patch.
01066         // If they were external already keep the patch.
01067         label patchID = patches.whichPatch(faceI);
01068 
01069         if (patchID == -1)
01070         {
01071             patchID = exposedPatchI;
01072         }
01073 
01074 
01075         // Do as much as possible by modifying faceI. Delay any remove
01076         // face. Keep track of whether faceI has been used.
01077 
01078         bool modifiedFaceI = false;
01079 
01080         if (f0Own == -1)
01081         {
01082             if (f0Nei != -1)
01083             {
01084                 // f0 becomes external face (note:modFace will reverse face)
01085                 modFace(meshMod, faceI, f0, f0Own, f0Nei, patchID);
01086                 modifiedFaceI = true;
01087             }
01088         }
01089         else
01090         {
01091             if (f0Nei == -1)
01092             {
01093                 // f0 becomes external face
01094                 modFace(meshMod, faceI, f0, f0Own, f0Nei, patchID);
01095                 modifiedFaceI = true;
01096             }
01097             else
01098             {
01099                 // f0 stays internal face.
01100                 modFace(meshMod, faceI, f0, f0Own, f0Nei, -1);
01101                 modifiedFaceI = true;
01102             }
01103         }
01104 
01105 
01106         // f1 is added face (if at all)
01107 
01108         if (f1Own == -1)
01109         {
01110             if (f1Nei == -1)
01111             {
01112                 // f1 not needed.
01113             }
01114             else
01115             {
01116                 // f1 becomes external face (note:modFace will reverse face)
01117                 if (!modifiedFaceI)
01118                 {
01119                     modFace(meshMod, faceI, f1, f1Own, f1Nei, patchID);
01120                     modifiedFaceI = true;
01121                 }
01122                 else
01123                 {
01124                     label masterPointI = findPatchFacePoint(f1, patchID);
01125 
01126                     addFace
01127                     (
01128                         meshMod,
01129                         faceI,          // face for zone info
01130                         masterPointI,   // inflation point
01131                         f1,             // vertices of face
01132                         f1Own,
01133                         f1Nei,
01134                         patchID         // patch for new face
01135                     );
01136                 }
01137             }
01138         }
01139         else
01140         {
01141             if (f1Nei == -1)
01142             {
01143                 // f1 becomes external face
01144                 if (!modifiedFaceI)
01145                 {
01146                     modFace(meshMod, faceI, f1, f1Own, f1Nei, patchID);
01147                     modifiedFaceI = true;
01148                 }
01149                 else
01150                 {
01151                     label masterPointI = findPatchFacePoint(f1, patchID);
01152 
01153                     addFace
01154                     (
01155                         meshMod,
01156                         faceI,
01157                         masterPointI,
01158                         f1,
01159                         f1Own,
01160                         f1Nei,
01161                         patchID
01162                     );
01163                 }
01164             }
01165             else
01166             {
01167                 // f1 is internal face.
01168                 if (!modifiedFaceI)
01169                 {
01170                     modFace(meshMod, faceI, f1, f1Own, f1Nei, -1);
01171                     modifiedFaceI = true;
01172                 }
01173                 else
01174                 {
01175                     label masterPointI = findPatchFacePoint(f1, -1);
01176 
01177                     addFace(meshMod, faceI, masterPointI, f1, f1Own, f1Nei, -1);
01178                 }
01179             }
01180         }
01181 
01182         if (f0Own == -1 && f0Nei == -1 && !modifiedFaceI)
01183         {
01184             meshMod.setAction(polyRemoveFace(faceI));
01185 
01186             if (debug & 2)
01187             {
01188                 Pout<< "Removed face " << faceI << endl;
01189             }
01190         }
01191 
01192         faceUptodate[faceI] = true;
01193     }
01194 
01195 
01196     //
01197     // Faces that have not been split but just appended to. Are guaranteed
01198     // to be reachable from an edgeCut.
01199     //
01200 
01201     const boolList& edgeIsCut = cuts.edgeIsCut();
01202 
01203     forAll(edgeIsCut, edgeI)
01204     {
01205         if (edgeIsCut[edgeI])
01206         {
01207             const labelList& eFaces = mesh().edgeFaces()[edgeI];
01208 
01209             forAll(eFaces, i)
01210             {
01211                 label faceI = eFaces[i];
01212 
01213                 if (!faceUptodate[faceI])
01214                 {
01215                     // So the face has not been split itself (i.e. its owner
01216                     // or neighbour have not been split) so it only
01217                     // borders by edge a cell which has been split.
01218 
01219                     // Get (new or original) owner and neighbour of faceI
01220                     label own, nei, patchID;
01221                     faceCells(cuts, exposedPatchI, faceI, own, nei, patchID);
01222 
01223 
01224                     if (own == -1 && nei == -1)
01225                     {
01226                         meshMod.setAction(polyRemoveFace(faceI));
01227 
01228                         if (debug & 2)
01229                         {
01230                             Pout<< "Removed face " << faceI << endl;
01231                         }
01232                     }
01233                     else
01234                     {
01235                         // Renumber face to include split edges.
01236                         face newFace(addEdgeCutsToFace(faceI));
01237 
01238                         if (debug & 2)
01239                         {
01240                             Pout<< "Added edge cuts to face " << faceI
01241                                 << " f:" << mesh().faces()[faceI]
01242                                 << " newFace:" << newFace << endl;
01243                         }
01244 
01245                         modFace
01246                         (
01247                             meshMod,
01248                             faceI,
01249                             newFace,
01250                             own,
01251                             nei,
01252                             patchID
01253                         );
01254                     }
01255 
01256                     faceUptodate[faceI] = true;
01257                 }
01258             }
01259         }
01260     }
01261 
01262 
01263     //
01264     // Remove any faces on the non-anchor side of a split cell.
01265     // Note: could loop through all cut cells only and check their faces but
01266     //       looping over all faces is cleaner and probably faster for dense
01267     //       cut patterns.
01268 
01269     const faceList& faces = mesh().faces();
01270 
01271     forAll(faces, faceI)
01272     {
01273         if (!faceUptodate[faceI])
01274         {
01275             // Get (new or original) owner and neighbour of faceI
01276             label own, nei, patchID;
01277             faceCells(cuts, exposedPatchI, faceI, own, nei, patchID);
01278 
01279             if (own == -1 && nei == -1)
01280             {
01281                 meshMod.setAction(polyRemoveFace(faceI));
01282 
01283                 if (debug & 2)
01284                 {
01285                     Pout<< "Removed face " << faceI << endl;
01286                 }
01287             }
01288             else
01289             {
01290                 modFace(meshMod, faceI, faces[faceI], own, nei, patchID);
01291             }
01292 
01293             faceUptodate[faceI] = true;
01294         }
01295     }
01296 
01297     if (debug)
01298     {
01299         Pout<< "meshCutAndRemove:" << nl
01300             << "    cells split:" << cuts.nLoops() << nl
01301             << "    faces added:" << addedFaces_.size() << nl
01302             << "    points added on edges:" << addedPoints_.size() << nl
01303             << endl;
01304     }
01305 }
01306 
01307 
01308 void Foam::meshCutAndRemove::updateMesh(const mapPolyMesh& map)
01309 {
01310     // Update stored labels for mesh change.
01311     {
01312         Map<label> newAddedFaces(addedFaces_.size());
01313 
01314         for
01315         (
01316             Map<label>::const_iterator iter = addedFaces_.begin();
01317             iter != addedFaces_.end();
01318             ++iter
01319         )
01320         {
01321             label cellI = iter.key();
01322 
01323             label newCellI = map.reverseCellMap()[cellI];
01324 
01325             label addedFaceI = iter();
01326 
01327             label newAddedFaceI = map.reverseFaceMap()[addedFaceI];
01328 
01329             if ((newCellI >= 0) && (newAddedFaceI >= 0))
01330             {
01331                 if
01332                 (
01333                     (debug & 2)
01334                  && (newCellI != cellI || newAddedFaceI != addedFaceI)
01335                 )
01336                 {
01337                     Pout<< "meshCutAndRemove::updateMesh :"
01338                         << " updating addedFace for cell " << cellI
01339                         << " from " << addedFaceI
01340                         << " to " << newAddedFaceI
01341                         << endl;
01342                 }
01343                 newAddedFaces.insert(newCellI, newAddedFaceI);
01344             }
01345         }
01346 
01347         // Copy
01348         addedFaces_.transfer(newAddedFaces);
01349     }
01350 
01351     {
01352         HashTable<label, edge, Hash<edge> > newAddedPoints(addedPoints_.size());
01353 
01354         for
01355         (
01356             HashTable<label, edge, Hash<edge> >::const_iterator iter =
01357                 addedPoints_.begin();
01358             iter != addedPoints_.end();
01359             ++iter
01360         )
01361         {
01362             const edge& e = iter.key();
01363 
01364             label newStart = map.reversePointMap()[e.start()];
01365 
01366             label newEnd = map.reversePointMap()[e.end()];
01367 
01368             label addedPointI = iter();
01369 
01370             label newAddedPointI = map.reversePointMap()[addedPointI];
01371 
01372             if ((newStart >= 0) && (newEnd >= 0) && (newAddedPointI >= 0))
01373             {
01374                 edge newE = edge(newStart, newEnd);
01375 
01376                 if
01377                 (
01378                     (debug & 2)
01379                  && (e != newE || newAddedPointI != addedPointI)
01380                 )
01381                 {
01382                     Pout<< "meshCutAndRemove::updateMesh :"
01383                         << " updating addedPoints for edge " << e
01384                         << " from " << addedPointI
01385                         << " to " << newAddedPointI
01386                         << endl;
01387                 }
01388 
01389                 newAddedPoints.insert(newE, newAddedPointI);
01390             }
01391         }
01392 
01393         // Copy
01394         addedPoints_.transfer(newAddedPoints);
01395     }
01396 }
01397 
01398 
01399 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd