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

removeCellLayer.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     Remove a layer of cells and prepare addressing data
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "layerAdditionRemoval.H"
00031 #include "polyMesh.H"
00032 #include "primitiveMesh.H"
00033 #include "polyTopoChange.H"
00034 #include "oppositeFace.H"
00035 #include "polyTopoChanger.H"
00036 #include "polyRemoveCell.H"
00037 #include "polyRemoveFace.H"
00038 #include "polyRemovePoint.H"
00039 #include "polyModifyFace.H"
00040 
00041 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00042 
00043 bool Foam::layerAdditionRemoval::validCollapse() const
00044 {
00045     // Check for valid layer collapse
00046     // - no boundary-to-boundary collapse
00047 
00048     if (debug)
00049     {
00050         Pout << "Checking layer collapse for object " << name() << endl;
00051     }
00052 
00053     // Grab the face collapse mapping
00054     const polyMesh& mesh = topoChanger().mesh();
00055 
00056     const labelList& ftc = facesPairing();
00057     const labelList& mf = mesh.faceZones()[faceZoneID_.index()];
00058 
00059     label nBoundaryHits = 0;
00060 
00061     forAll (mf, faceI)
00062     {
00063         if
00064         (
00065             !mesh.isInternalFace(mf[faceI])
00066          && !mesh.isInternalFace(ftc[faceI])
00067         )
00068         {
00069             nBoundaryHits++;
00070         }
00071     }
00072 
00073 
00074     if (debug)
00075     {
00076         Pout<< "Finished checking layer collapse for object "
00077             << name() <<".  Number of boundary-on-boundary hits: "
00078             << nBoundaryHits << endl;
00079     }
00080 
00081     if (returnReduce(nBoundaryHits, sumOp<label>()) > 0)
00082     {
00083         return false;
00084     }
00085     else
00086     {
00087         return true;
00088     }
00089 }
00090 
00091 void Foam::layerAdditionRemoval::removeCellLayer
00092 (
00093     polyTopoChange& ref
00094 ) const
00095 {
00096     // Algorithm for layer removal.  Second phase: topological change
00097     // 2)  Go through all the faces of the master cell layer and remove
00098     //     the ones that are not in the master face zone.
00099     //
00100     // 3)  Grab all the faces coming out of points that are collapsed
00101     //     and select the ones that are not marked for removal.  Go
00102     //     through the remaining faces and replace the point to remove by
00103     //     the equivalent point in the master face zone.
00104     if (debug)
00105     {
00106         Pout << "Removing the cell layer for object " << name() << endl;
00107     }
00108 
00109     const polyMesh& mesh = topoChanger().mesh();
00110 
00111     const labelList& ptc = pointsPairing();
00112     const labelList& ftc = facesPairing();
00113 
00114     // Remove all the cells from the master layer
00115     const labelList& mc =
00116         topoChanger().mesh().faceZones()[faceZoneID_.index()].masterCells();
00117 
00118     forAll (mc, cellI)
00119     {
00120         ref.setAction(polyRemoveCell(mc[cellI]));
00121     }
00122 
00123     // Remove all the faces from the master layer cells which are not in
00124     // the master face layer
00125     labelHashSet facesToRemoveMap(mc.size()*primitiveMesh::facesPerCell_);
00126 
00127     const cellList& cells = mesh.cells();
00128 
00129     forAll (mc, cellI)
00130     {
00131         const cell& curCell = cells[mc[cellI]];
00132 
00133         forAll (curCell, faceI)
00134         {
00135             // Check if the face is in the master zone.  If not, remove it
00136             if
00137             (
00138                 mesh.faceZones().whichZone(curCell[faceI])
00139              != faceZoneID_.index()
00140             )
00141             {
00142                 facesToRemoveMap.insert(curCell[faceI]);
00143             }
00144         }
00145     }
00146 
00147     forAllConstIter(labelHashSet, facesToRemoveMap, iter)
00148     {
00149         ref.setAction(polyRemoveFace(iter.key()));
00150     }
00151 
00152     // Remove all points that will be collapsed
00153     forAll (ptc, pointI)
00154     {
00155         ref.setAction(polyRemovePoint(ptc[pointI]));
00156     }
00157 
00158     // Grab all faces coming off points to be deleted.  If the face
00159     // has not been deleted, replace the removed point with the
00160     // equivalent from the master layer.
00161 
00162     // Make a map of all point to be removed, giving the master point label
00163     // for each of them
00164 
00165     Map<label> removedPointMap(2*ptc.size());
00166 
00167     const labelList& meshPoints =
00168         mesh.faceZones()[faceZoneID_.index()]().meshPoints();
00169 
00170     forAll (ptc, pointI)
00171     {
00172         removedPointMap.insert(ptc[pointI], meshPoints[pointI]);
00173     }
00174 
00175     const labelListList& pf = mesh.pointFaces();
00176 
00177     const faceList& faces = mesh.faces();
00178     const labelList& own = mesh.faceOwner();
00179     const labelList& nei = mesh.faceNeighbour();
00180 
00181     // Make a list of faces to be modified using the map to avoid duplicates
00182     labelHashSet facesToModify(ptc.size()*primitiveMesh::facesPerPoint_);
00183 
00184     forAll (ptc, pointI)
00185     {
00186         const labelList& curFaces = pf[ptc[pointI]];
00187 
00188         forAll (curFaces, faceI)
00189         {
00190             if (!facesToRemoveMap.found(curFaces[faceI]))
00191             {
00192                 facesToModify.insert(curFaces[faceI]);
00193             }
00194         }
00195     }
00196 
00197     labelList ftm = facesToModify.toc();
00198 
00199 //Pout << "faces to modify: " << ftm << endl;
00200 
00201     forAll (ftm, faceI)
00202     {
00203         // For every face to modify, copy the face and re-map the vertices.
00204         // It is known all the faces will be changed since they hang off
00205         // re-mapped vertices
00206         label curFaceID = ftm[faceI];
00207 
00208         face newFace(faces[curFaceID]);
00209 
00210         forAll (newFace, pointI)
00211         {
00212             Map<label>::iterator rpmIter =
00213                 removedPointMap.find(newFace[pointI]);
00214 
00215             if (rpmIter != removedPointMap.end())
00216             {
00217                 // Point mapped. Replace it
00218                 newFace[pointI] = rpmIter();
00219             }
00220         }
00221 
00222 //Pout<< "face label: " << curFaceID
00223 //    << " old face: " << faces[curFaceID]
00224 //    << " new face: " << newFace << endl;
00225 
00226         // Get face zone and its flip
00227         label modifiedFaceZone = mesh.faceZones().whichZone(curFaceID);
00228         bool modifiedFaceZoneFlip = false;
00229 
00230         if (modifiedFaceZone >= 0)
00231         {
00232             modifiedFaceZoneFlip =
00233                 mesh.faceZones()[modifiedFaceZone].flipMap()
00234                 [
00235                     mesh.faceZones()[modifiedFaceZone].whichFace(curFaceID)
00236                 ];
00237         }
00238         
00239         label newNei;
00240         
00241         if (curFaceID < mesh.nInternalFaces())
00242         {
00243             newNei = nei[curFaceID];
00244         }
00245         else
00246         {
00247             newNei = -1;
00248         }
00249 
00250         // Modify the face
00251         ref.setAction
00252         (
00253             polyModifyFace
00254             (
00255                 newFace,                // modified face
00256                 curFaceID,              // label of face being modified
00257                 own[curFaceID],         // owner
00258                 newNei,                 // neighbour
00259                 false,                  // face flip
00260                 mesh.boundaryMesh().whichPatch(curFaceID),// patch for face
00261                 false,                  // remove from zone
00262                 modifiedFaceZone,       // zone for face
00263                 modifiedFaceZoneFlip    // face flip in zone
00264             )
00265         );
00266     }
00267 
00268     // Modify the faces in the master layer to point past the removed cells
00269 
00270     const labelList& mf = mesh.faceZones()[faceZoneID_.index()];
00271     const boolList& mfFlip = mesh.faceZones()[faceZoneID_.index()].flipMap();
00272 
00273     forAll (mf, faceI)
00274     {
00275         // Grab the owner and neighbour of the faces to be collapsed and get rid
00276         // of the cell to be removed
00277         label masterSideCell = own[mf[faceI]];
00278 
00279         if (masterSideCell == mc[faceI])
00280         {
00281             // Owner cell of the face is being removed.
00282             // Grab the neighbour instead
00283             masterSideCell = nei[mf[faceI]];
00284         }
00285 
00286         label slaveSideCell = own[ftc[faceI]];
00287 
00288         if (slaveSideCell == mc[faceI])
00289         {
00290             // Owner cell of the face is being removed.
00291             // Grab the neighbour instead
00292             slaveSideCell = nei[ftc[faceI]];
00293         }
00294 
00295         // Find out if the face needs to be flipped
00296         label newOwner = -1;
00297         label newNeighbour = -1;
00298         bool flipFace = false;
00299         label newPatchID = -1;
00300         label newZoneID = -1;
00301 
00302         // Is any of the faces a boundary face?  If so, grab the patch
00303         // A boundary-to-boundary collapse is checked for in validCollapse()
00304         // and cannot happen here.  
00305 
00306         if (!mesh.isInternalFace(mf[faceI]))
00307         {
00308             // Master is the boundary face: it gets a new owner but no flip
00309             newOwner = slaveSideCell;
00310             newNeighbour = -1;
00311             flipFace = false;
00312             newPatchID = mesh.boundaryMesh().whichPatch(mf[faceI]);
00313             newZoneID = mesh.faceZones().whichZone(mf[faceI]);
00314         }
00315         else if (!mesh.isInternalFace(ftc[faceI]))
00316         {
00317             // Slave is the boundary face: grab its patch
00318             newOwner = slaveSideCell;
00319             newNeighbour = -1;
00320 
00321             // Find out if the face flip is necessary
00322             if (own[mf[faceI]] == slaveSideCell)
00323             {
00324                 flipFace = false;
00325             }
00326             else
00327             {
00328                 flipFace = true;
00329             }
00330 
00331             newPatchID = mesh.boundaryMesh().whichPatch(ftc[faceI]);
00332 
00333             // The zone of the master face is preserved
00334             newZoneID = mesh.faceZones().whichZone(mf[faceI]);
00335         }
00336         else
00337         {
00338             // Both faces are internal: flip is decided based on which of the
00339             // new cells around it has a lower label
00340             newOwner = min(masterSideCell, slaveSideCell);
00341             newNeighbour = max(masterSideCell, slaveSideCell);
00342 
00343             if (newOwner == own[mf[faceI]] || newNeighbour == nei[mf[faceI]])
00344             {
00345                 flipFace = false;
00346             }
00347             else
00348             {
00349                 flipFace = true;
00350             }
00351 
00352             newPatchID = -1;
00353 
00354             // The zone of the master face is preserved
00355             newZoneID = mesh.faceZones().whichZone(mf[faceI]);
00356         }
00357 
00358         // Modify the face and flip if necessary
00359         face newFace = faces[mf[faceI]];
00360         bool zoneFlip = mfFlip[faceI];
00361 
00362         if (flipFace)
00363         {
00364             newFace = newFace.reverseFace();
00365             zoneFlip = !zoneFlip;
00366         }
00367 
00368 // Pout<< "Modifying face " << mf[faceI]
00369 //     << " newFace: " << newFace << nl
00370 //     << " newOwner: " << newOwner
00371 //     << " newNeighbour: " << newNeighbour
00372 //     << " flipFace: " << flipFace
00373 //     << " newPatchID: " << newPatchID
00374 //     << " newZoneID: " << newZoneID << nl
00375 //     << " oldOwn: " << own[mf[faceI]]
00376 //     << " oldNei: " << nei[mf[faceI]] << endl;
00377 
00378         ref.setAction
00379         (
00380             polyModifyFace
00381             (
00382                 newFace,       // modified face
00383                 mf[faceI],     // label of face being modified
00384                 newOwner,      // owner
00385                 newNeighbour,  // neighbour
00386                 flipFace,      // flip
00387                 newPatchID,    // patch for face
00388                 false,         // remove from zone
00389                 newZoneID,     // new zone
00390                 zoneFlip       // face flip in zone
00391             )
00392         );
00393     }
00394 }
00395 
00396 
00397 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd