initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / conversion / meshReader / calcPointCells.C
blob0efe47552b73225b3bf85fede3890f46d3436c3c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     calculate point cells - ie, the cells attached to each point
28     - remove unused points, adjust pointCells and cellFaces accordingly
29 \*---------------------------------------------------------------------------*/
31 #include "meshReader.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 void Foam::meshReader::calcPointCells() const
37     const static label UNIT_POINT_CELLS = 12;
39     if (pointCellsPtr_)
40     {
41         FatalErrorIn("meshReader::calcPointCells() const")
42             << "pointCells already calculated"
43             << abort(FatalError);
44     }
46     label nPoints = points_.size();
48     pointCellsPtr_ = new labelListList(nPoints);
49     labelListList& ptCells = *pointCellsPtr_;
51     forAll(ptCells, i)
52     {
53         ptCells[i].setSize(UNIT_POINT_CELLS);
54     }
56     // Initialize the list of labels which will hold the count of the
57     // actual number of cells per point during the analysis
58     labelList cellCount(nPoints, 0);
60     // Note. Unlike the standard point-cell algorithm, which asks the cell for
61     // the supporting point labels, we need to work based on the cell faces.
62     // This is because some of the faces do not come from the cell shape.
63     // It is also advantageous to remove duplicates from the point-cell
64     // addressing, because this removes a lot of waste later.
66     faceListList& cFaces = cellFaces();
68     // For each cell
69     forAll(cFaces, cellI)
70     {
71         const faceList& faces = cFaces[cellI];
73         forAll(faces, i)
74         {
75             // For each vertex
76             const labelList& labels = faces[i];
78             forAll(labels, j)
79             {
80                 // Set working point label
81                 label curPoint = labels[j];
82                 labelList& curPointCells = ptCells[curPoint];
83                 label curCount = cellCount[curPoint];
85                 // check if the cell has been added before
86                 bool found = false;
88                 for (label f = 0; f < curCount; f++)
89                 {
90                     if (curPointCells[f] == cellI)
91                     {
92                         found = true;
93                         break;
94                     }
95                 }
97                 if (!found)
98                 {
99                     // If the list of pointCells is not big enough, double it
100                     if (curPointCells.size() <= curCount)
101                     {
102                         curPointCells.setSize(curPointCells.size()*2);
103                     }
105                     // Enter the cell label in the point's cell list
106                     curPointCells[curCount] = cellI;
108                     // Increment the cell count for the point addressed
109                     cellCount[curPoint]++;
110                 }
111             }
112         }
113     }
115     // report and remove unused points
116     // - adjust points, pointCells, and cellFaces accordingly
117     label pointI = 0;
118     labelList oldToNew(nPoints, -1);
120     forAll(ptCells, i)
121     {
122         ptCells[i].setSize(cellCount[i]);
123         if (cellCount[i] > 0)
124         {
125             oldToNew[i] = pointI++;
126         }
127     }
129     // report unused points
130     if (nPoints > pointI)
131     {
132         Info<< "removing " << (nPoints - pointI) << " unused points" << endl;
134         nPoints = pointI;
136         // adjust points and truncate - bend const-ness
137         pointField& adjustedPoints = const_cast<pointField&>(points_);
139         inplaceReorder(oldToNew, adjustedPoints);
140         adjustedPoints.setSize(nPoints);
142         // adjust pointCells and truncate
143         inplaceReorder(oldToNew, ptCells);
144         ptCells.setSize(nPoints);
146         // adjust cellFaces - this could be faster
147         // For each cell
148         forAll(cFaces, cellI)
149         {
150             faceList& faces = cFaces[cellI];
152             // For each face
153             forAll(faces, i)
154             {
155                 inplaceRenumber(oldToNew, faces[i]);
156             }
157         }
158     }
162 const Foam::labelListList& Foam::meshReader::pointCells() const
164     if (!pointCellsPtr_)
165     {
166         calcPointCells();
167     }
169     return *pointCellsPtr_;
173 // ************************************************************************* //