FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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