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

removeCells.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 "removeCells.H"
00028 #include "polyMesh.H"
00029 #include "polyTopoChange.H"
00030 #include "polyRemoveCell.H"
00031 #include "polyRemoveFace.H"
00032 #include "polyModifyFace.H"
00033 #include "polyRemovePoint.H"
00034 #include "syncTools.H"
00035 
00036 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00037 
00038 namespace Foam
00039 {
00040 
00041 defineTypeNameAndDebug(removeCells, 0);
00042 
00043 }
00044 
00045 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00046 
00047 // Remove count of elements of f.
00048 void Foam::removeCells::uncount
00049 (
00050     const labelList& f,
00051     labelList& nUsage
00052 )
00053 {
00054     forAll(f, fp)
00055     {
00056         nUsage[f[fp]]--;
00057     }
00058 }
00059 
00060 
00061 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00062 
00063 // Construct from mesh
00064 Foam::removeCells::removeCells
00065 (
00066     const polyMesh& mesh,
00067     const bool syncPar
00068 )
00069 :
00070     mesh_(mesh),
00071     syncPar_(syncPar)
00072 {}
00073 
00074 
00075 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00076 
00077 //- Get labels of exposed faces. These are
00078 //  - internal faces that become boundary faces
00079 //  - coupled faces that become uncoupled (since on of the sides
00080 //    gets deleted)
00081 Foam::labelList Foam::removeCells::getExposedFaces
00082 (
00083     const labelList& cellLabels
00084 ) const
00085 {
00086     // Create list of cells to be removed
00087     boolList removedCell(mesh_.nCells(), false);
00088 
00089     // Go from labelList of cells-to-remove to a boolList.
00090     forAll(cellLabels, i)
00091     {
00092         removedCell[cellLabels[i]] = true;
00093     }
00094 
00095 
00096     const labelList& faceOwner = mesh_.faceOwner();
00097     const labelList& faceNeighbour = mesh_.faceNeighbour();
00098 
00099     // Count cells using face.
00100     labelList nCellsUsingFace(mesh_.nFaces(), 0);
00101 
00102     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00103     {
00104         label own = faceOwner[faceI];
00105         label nei = faceNeighbour[faceI];
00106 
00107         if (!removedCell[own])
00108         {
00109             nCellsUsingFace[faceI]++;
00110         }
00111         if (!removedCell[nei])
00112         {
00113             nCellsUsingFace[faceI]++;
00114         }
00115     }
00116 
00117     for (label faceI = mesh_.nInternalFaces(); faceI < mesh_.nFaces(); faceI++)
00118     {
00119         label own = faceOwner[faceI];
00120 
00121         if (!removedCell[own])
00122         {
00123             nCellsUsingFace[faceI]++;
00124         }
00125     }
00126 
00127     // Coupled faces: add number of cells using face across couple.
00128     if (syncPar_)
00129     {
00130         syncTools::syncFaceList
00131         (
00132             mesh_,
00133             nCellsUsingFace,
00134             plusEqOp<label>(),
00135             false
00136         );
00137     }
00138 
00139     // Now nCellsUsingFace:
00140     // 0 : internal face whose both cells get deleted
00141     //     boundary face whose all cells get deleted
00142     // 1 : internal face that gets exposed
00143     //     unaffected (uncoupled) boundary face
00144     //     coupled boundary face that gets exposed ('uncoupled')
00145     // 2 : unaffected internal face
00146     //     unaffected coupled boundary face
00147 
00148     DynamicList<label> exposedFaces(mesh_.nFaces()/10);
00149 
00150     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00151     {
00152         if (nCellsUsingFace[faceI] == 1)
00153         {
00154             exposedFaces.append(faceI);
00155         }
00156     }
00157 
00158     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00159 
00160     forAll(patches, patchI)
00161     {
00162         const polyPatch& pp = patches[patchI];
00163 
00164         if (pp.coupled())
00165         {
00166             label faceI = pp.start();
00167 
00168             forAll(pp, i)
00169             {
00170                 label own = faceOwner[faceI];
00171 
00172                 if (nCellsUsingFace[faceI] == 1 && !removedCell[own])
00173                 {
00174                     // My owner not removed but other side is so has to become
00175                     // normal, uncoupled, boundary face
00176                     exposedFaces.append(faceI);
00177                 }
00178 
00179                 faceI++;
00180             }
00181         }
00182     }
00183 
00184     return exposedFaces.shrink();
00185 }
00186 
00187 
00188 void Foam::removeCells::setRefinement
00189 (
00190     const labelList& cellLabels,
00191     const labelList& exposedFaceLabels,
00192     const labelList& exposedPatchIDs,
00193     polyTopoChange& meshMod
00194 ) const
00195 {
00196     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00197 
00198     if (exposedFaceLabels.size() != exposedPatchIDs.size())
00199     {
00200         FatalErrorIn
00201         (
00202             "removeCells::setRefinement(const labelList&"
00203             ", const labelList&, const labelList&, polyTopoChange&)"
00204         )   << "Size of exposedFaceLabels " << exposedFaceLabels.size()
00205             << " differs from size of exposedPatchIDs "
00206             << exposedPatchIDs.size()
00207             << abort(FatalError);
00208     }
00209 
00210     // List of new patchIDs
00211     labelList newPatchID(mesh_.nFaces(), -1);
00212 
00213     forAll(exposedFaceLabels, i)
00214     {
00215         label patchI = exposedPatchIDs[i];
00216 
00217         if (patchI < 0 || patchI >= patches.size())
00218         {
00219             FatalErrorIn
00220             (
00221                 "removeCells::setRefinement(const labelList&"
00222                 ", const labelList&, const labelList&, polyTopoChange&)"
00223             )   << "Invalid patch " << patchI
00224                 << " for exposed face " << exposedFaceLabels[i] << endl
00225                 << "Valid patches 0.." << patches.size()-1
00226                 << abort(FatalError);
00227         }
00228 
00229         if (patches[patchI].coupled())
00230         {
00231             FatalErrorIn
00232             (
00233                 "removeCells::setRefinement(const labelList&"
00234                 ", const labelList&, const labelList&, polyTopoChange&)"
00235             )   << "Trying to put exposed face " << exposedFaceLabels[i]
00236                 << " into a coupled patch : " << patches[patchI].name()
00237                 << endl
00238                 << "This is illegal."
00239                 << abort(FatalError);
00240         }
00241 
00242         newPatchID[exposedFaceLabels[i]] = patchI;
00243     }
00244 
00245 
00246     // Create list of cells to be removed
00247     boolList removedCell(mesh_.nCells(), false);
00248 
00249     // Go from labelList of cells-to-remove to a boolList and remove all
00250     // cells mentioned.
00251     forAll(cellLabels, i)
00252     {
00253         label cellI = cellLabels[i];
00254 
00255         removedCell[cellI] = true;
00256 
00257         //Pout<< "Removing cell " << cellI
00258         //    << " cc:" << mesh_.cellCentres()[cellI] << endl;
00259 
00260         meshMod.setAction(polyRemoveCell(cellI));
00261     }
00262 
00263 
00264     // Remove faces that are no longer used. Modify faces that
00265     // are used by one cell only.
00266 
00267     const faceList& faces = mesh_.faces();
00268     const labelList& faceOwner = mesh_.faceOwner();
00269     const labelList& faceNeighbour = mesh_.faceNeighbour();
00270     const faceZoneMesh& faceZones = mesh_.faceZones();
00271 
00272     // Count starting number of faces using each point. Keep up to date whenever
00273     // removing a face.
00274     labelList nFacesUsingPoint(mesh_.nPoints(), 0);
00275 
00276     forAll(faces, faceI)
00277     {
00278         const face& f = faces[faceI];
00279 
00280         forAll(f, fp)
00281         {
00282             nFacesUsingPoint[f[fp]]++;
00283         }
00284     }
00285 
00286 
00287     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00288     {
00289         const face& f = faces[faceI];
00290         label own = faceOwner[faceI];
00291         label nei = faceNeighbour[faceI];
00292 
00293         if (removedCell[own])
00294         {
00295             if (removedCell[nei])
00296             {
00297                 // Face no longer used
00298                 //Pout<< "Removing internal face " << faceI
00299                 //    << " fc:" << mesh_.faceCentres()[faceI] << endl;
00300 
00301                 meshMod.setAction(polyRemoveFace(faceI));
00302                 uncount(f, nFacesUsingPoint);
00303             }
00304             else
00305             {
00306                 if (newPatchID[faceI] == -1)
00307                 {
00308                     FatalErrorIn
00309                     (
00310                         "removeCells::setRefinement(const labelList&"
00311                         ", const labelList&, const labelList&"
00312                         ", polyTopoChange&)"
00313                     )   << "No patchID provided for exposed face " << faceI
00314                         << " on cell " << nei << nl
00315                         << "Did you provide patch IDs for all exposed faces?"
00316                         << abort(FatalError);
00317                 }
00318 
00319                 // nei is remaining cell. FaceI becomes external cell
00320 
00321                 label zoneID = faceZones.whichZone(faceI);
00322                 bool zoneFlip = false;
00323 
00324                 if (zoneID >= 0)
00325                 {
00326                     const faceZone& fZone = faceZones[zoneID];
00327                     zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00328                 }
00329 
00330                 //Pout<< "Putting exposed internal face " << faceI
00331                 //    << " fc:" << mesh_.faceCentres()[faceI]
00332                 //    << " into patch " << newPatchID[faceI] << endl;
00333 
00334                 meshMod.setAction
00335                 (
00336                     polyModifyFace
00337                     (
00338                         f.reverseFace(),        // modified face
00339                         faceI,                  // label of face being modified
00340                         nei,                    // owner
00341                         -1,                     // neighbour
00342                         false,                  // face flip
00343                         newPatchID[faceI],      // patch for face
00344                         false,                  // remove from zone
00345                         zoneID,                 // zone for face
00346                         zoneFlip                // face flip in zone
00347                     )
00348                 );
00349             }
00350         }
00351         else if (removedCell[nei])
00352         {
00353             if (newPatchID[faceI] == -1)
00354             {
00355                 FatalErrorIn
00356                 (
00357                     "removeCells::setRefinement(const labelList&"
00358                     ", const labelList&, const labelList&"
00359                     ", polyTopoChange&)"
00360                 )   << "No patchID provided for exposed face " << faceI
00361                     << " on cell " << own << nl
00362                     << "Did you provide patch IDs for all exposed faces?"
00363                     << abort(FatalError);
00364             }
00365 
00366             //Pout<< "Putting exposed internal face " << faceI
00367             //    << " fc:" << mesh_.faceCentres()[faceI]
00368             //    << " into patch " << newPatchID[faceI] << endl;
00369 
00370             // own is remaining cell. FaceI becomes external cell.
00371             label zoneID = faceZones.whichZone(faceI);
00372             bool zoneFlip = false;
00373 
00374             if (zoneID >= 0)
00375             {
00376                 const faceZone& fZone = faceZones[zoneID];
00377                 zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00378             }
00379 
00380             meshMod.setAction
00381             (
00382                 polyModifyFace
00383                 (
00384                     f,                      // modified face
00385                     faceI,                  // label of face being modified
00386                     own,                    // owner
00387                     -1,                     // neighbour
00388                     false,                  // face flip
00389                     newPatchID[faceI],      // patch for face
00390                     false,                  // remove from zone
00391                     zoneID,                 // zone for face
00392                     zoneFlip                // face flip in zone
00393                 )
00394             );
00395         }
00396     }
00397 
00398     forAll(patches, patchI)
00399     {
00400         const polyPatch& pp = patches[patchI];
00401 
00402         if (pp.coupled())
00403         {
00404             label faceI = pp.start();
00405 
00406             forAll(pp, i)
00407             {
00408                 if (newPatchID[faceI] != -1)
00409                 {
00410                     //Pout<< "Putting uncoupled coupled face " << faceI
00411                     //    << " fc:" << mesh_.faceCentres()[faceI]
00412                     //    << " into patch " << newPatchID[faceI] << endl;
00413 
00414                     label zoneID = faceZones.whichZone(faceI);
00415                     bool zoneFlip = false;
00416 
00417                     if (zoneID >= 0)
00418                     {
00419                         const faceZone& fZone = faceZones[zoneID];
00420                         zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
00421                     }
00422 
00423                     meshMod.setAction
00424                     (
00425                         polyModifyFace
00426                         (
00427                             faces[faceI],           // modified face
00428                             faceI,                  // label of face
00429                             faceOwner[faceI],       // owner
00430                             -1,                     // neighbour
00431                             false,                  // face flip
00432                             newPatchID[faceI],      // patch for face
00433                             false,                  // remove from zone
00434                             zoneID,                 // zone for face
00435                             zoneFlip                // face flip in zone
00436                         )
00437                     );
00438                 }
00439                 else if (removedCell[faceOwner[faceI]])
00440                 {
00441                     // Face no longer used
00442                     //Pout<< "Removing boundary face " << faceI
00443                     //    << " fc:" << mesh_.faceCentres()[faceI]
00444                     //    << endl;
00445 
00446                     meshMod.setAction(polyRemoveFace(faceI));
00447                     uncount(faces[faceI], nFacesUsingPoint);
00448                 }
00449 
00450                 faceI++;
00451             }
00452         }
00453         else
00454         {
00455             label faceI = pp.start();
00456 
00457             forAll(pp, i)
00458             {
00459                 if (newPatchID[faceI] != -1)
00460                 {
00461                     FatalErrorIn
00462                     (
00463                         "removeCells::setRefinement(const labelList&"
00464                         ", const labelList&, const labelList&"
00465                         ", polyTopoChange&)"
00466                     )   << "new patchID provided for boundary face " << faceI
00467                         << " even though it is not on a coupled face."
00468                         << abort(FatalError);
00469                 }
00470 
00471                 if (removedCell[faceOwner[faceI]])
00472                 {
00473                     // Face no longer used
00474                     //Pout<< "Removing boundary face " << faceI
00475                     //    << " fc:" << mesh_.faceCentres()[faceI]
00476                     //    << endl;
00477 
00478                     meshMod.setAction(polyRemoveFace(faceI));
00479                     uncount(faces[faceI], nFacesUsingPoint);
00480                 }
00481 
00482                 faceI++;
00483             }
00484         }
00485     }    
00486 
00487 
00488     // Remove points that are no longer used.
00489     // Loop rewritten to not use pointFaces.
00490 
00491     forAll(nFacesUsingPoint, pointI)
00492     {
00493         if (nFacesUsingPoint[pointI] == 0)
00494         {
00495             //Pout<< "Removing unused point " << pointI
00496             //    << " at:" << mesh_.points()[pointI] << endl;
00497 
00498             meshMod.setAction(polyRemovePoint(pointI));
00499         }
00500         else if (nFacesUsingPoint[pointI] == 1)
00501         {
00502             WarningIn
00503             (
00504                 "removeCells::setRefinement(const labelList&"
00505                 ", const labelList&, const labelList&"
00506                 ", polyTopoChange&)"
00507             )   << "point " << pointI << " at coordinate "
00508                 << mesh_.points()[pointI]
00509                 << " is only used by 1 face after removing cells."
00510                 << " This probably results in an illegal mesh."
00511                 << endl;
00512         }
00513     }
00514 }
00515 
00516 
00517 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd