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

calcPointCells.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     calculate point cells - ie, the cells attached to each point
00027 
00028     - remove unused points, adjust pointCells and cellFaces accordingly
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "meshReader.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 void Foam::meshReader::calcPointCells() const
00036 {
00037     const static label UNIT_POINT_CELLS = 12;
00038 
00039     if (pointCellsPtr_)
00040     {
00041         FatalErrorIn("meshReader::calcPointCells() const")
00042             << "pointCells already calculated"
00043             << abort(FatalError);
00044     }
00045 
00046     label nPoints = points_.size();
00047 
00048     pointCellsPtr_ = new labelListList(nPoints);
00049     labelListList& ptCells = *pointCellsPtr_;
00050 
00051     forAll(ptCells, i)
00052     {
00053         ptCells[i].setSize(UNIT_POINT_CELLS);
00054     }
00055 
00056     // Initialize the list of labels which will hold the count of the
00057     // actual number of cells per point during the analysis
00058     labelList cellCount(nPoints, 0);
00059 
00060     // Note. Unlike the standard point-cell algorithm, which asks the cell for
00061     // the supporting point labels, we need to work based on the cell faces.
00062     // This is because some of the faces do not come from the cell shape.
00063     // It is also advantageous to remove duplicates from the point-cell
00064     // addressing, because this removes a lot of waste later.
00065 
00066     faceListList& cFaces = cellFaces();
00067 
00068     // For each cell
00069     forAll(cFaces, cellI)
00070     {
00071         const faceList& faces = cFaces[cellI];
00072 
00073         forAll(faces, i)
00074         {
00075             // For each vertex
00076             const labelList& labels = faces[i];
00077 
00078             forAll(labels, j)
00079             {
00080                 // Set working point label
00081                 label curPoint = labels[j];
00082                 labelList& curPointCells = ptCells[curPoint];
00083                 label curCount = cellCount[curPoint];
00084 
00085                 // check if the cell has been added before
00086                 bool found = false;
00087 
00088                 for (label f = 0; f < curCount; f++)
00089                 {
00090                     if (curPointCells[f] == cellI)
00091                     {
00092                         found = true;
00093                         break;
00094                     }
00095                 }
00096 
00097                 if (!found)
00098                 {
00099                     // If the list of pointCells is not big enough, double it
00100                     if (curPointCells.size() <= curCount)
00101                     {
00102                         curPointCells.setSize(curPointCells.size()*2);
00103                     }
00104 
00105                     // Enter the cell label in the point's cell list
00106                     curPointCells[curCount] = cellI;
00107 
00108                     // Increment the cell count for the point addressed
00109                     cellCount[curPoint]++;
00110                 }
00111             }
00112         }
00113     }
00114 
00115     // report and remove unused points
00116     // - adjust points, pointCells, and cellFaces accordingly
00117     label pointI = 0;
00118     labelList oldToNew(nPoints, -1);
00119 
00120     forAll(ptCells, i)
00121     {
00122         ptCells[i].setSize(cellCount[i]);
00123         if (cellCount[i] > 0)
00124         {
00125             oldToNew[i] = pointI++;
00126         }
00127     }
00128 
00129     // report unused points
00130     if (nPoints > pointI)
00131     {
00132         Info<< "removing " << (nPoints - pointI) << " unused points" << endl;
00133 
00134         nPoints = pointI;
00135 
00136         // adjust points and truncate - bend const-ness
00137         pointField& adjustedPoints = const_cast<pointField&>(points_);
00138 
00139         inplaceReorder(oldToNew, adjustedPoints);
00140         adjustedPoints.setSize(nPoints);
00141 
00142         // adjust pointCells and truncate
00143         inplaceReorder(oldToNew, ptCells);
00144         ptCells.setSize(nPoints);
00145 
00146         // adjust cellFaces - this could be faster
00147         // For each cell
00148         forAll(cFaces, cellI)
00149         {
00150             faceList& faces = cFaces[cellI];
00151 
00152             // For each face
00153             forAll(faces, i)
00154             {
00155                 inplaceRenumber(oldToNew, faces[i]);
00156             }
00157         }
00158     }
00159 }
00160 
00161 
00162 const Foam::labelListList& Foam::meshReader::pointCells() const
00163 {
00164     if (!pointCellsPtr_)
00165     {
00166         calcPointCells();
00167     }
00168 
00169     return *pointCellsPtr_;
00170 }
00171 
00172 
00173 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd