initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / conversion / meshReader / calcPointCells.C
blob59d47a2f3b88ba65564ee860a5bb3e0c8346abe5
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 // for transition - in case someone really relied on the old behaviour
34 #undef LEAVE_UNUSED_POINTS
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 void Foam::meshReader::calcPointCells() const
40     const static label UNIT_POINT_CELLS = 12;
42     if (pointCellsPtr_)
43     {
44         FatalErrorIn("meshReader::calcPointCells() const")
45             << "pointCells already calculated"
46             << abort(FatalError);
47     }
49     label nPoints = points().size();
51     pointCellsPtr_ = new labelListList(nPoints);
52     labelListList& ptCells = *pointCellsPtr_;
54     forAll(ptCells, i)
55     {
56         ptCells[i].setSize(UNIT_POINT_CELLS);
57     }
59     // Initialize the list of labels which will hold the count of the
60     // actual number of cells per point during the analysis
61     labelList cellCount(nPoints, 0);
63     // Note. Unlike the standard point-cell algorithm, which asks the cell for
64     // the supporting point labels, we need to work based on the cell faces.
65     // This is because some of the faces do not come from the cell shape.
66     // It is also advantageous to remove duplicates from the point-cell
67     // addressing, because this removes a lot of waste later.
69     faceListList& cFaces = cellFaces();
71     // For each cell
72     forAll(cFaces, cellI)
73     {
74         const faceList& faces = cFaces[cellI];
76         forAll(faces, i)
77         {
78             // For each vertex
79             const labelList& labels = faces[i];
81             forAll(labels, j)
82             {
83                 // Set working point label
84                 label curPoint = labels[j];
85                 labelList& curPointCells = ptCells[curPoint];
86                 label curCount = cellCount[curPoint];
88                 // check if the cell has been added before
89                 bool found = false;
91                 for (label f = 0; f < curCount; f++)
92                 {
93                     if (curPointCells[f] == cellI)
94                     {
95                         found = true;
96                         break;
97                     }
98                 }
100                 if (!found)
101                 {
102                     // If the list of pointCells is not big enough, double it
103                     if (curPointCells.size() <= curCount)
104                     {
105                         curPointCells.setSize(curPointCells.size()*2);
106                     }
108                     // Enter the cell label in the point's cell list
109                     curPointCells[curCount] = cellI;
111                     // Increment the cell count for the point addressed
112                     cellCount[curPoint]++;
113                 }
114             }
115         }
116     }
118     // report and remove unused points
119     // - adjust points, pointCells, and cellFaces accordingly
120     label pointI = 0;
121     labelList oldToNew(nPoints, -1);
123     forAll(ptCells, i)
124     {
125         ptCells[i].setSize(cellCount[i]);
126         if (cellCount[i] > 0)
127         {
128             oldToNew[i] = pointI++;
129         }
130     }
132     // report unused points
133     if (nPoints > pointI)
134     {
135 #ifdef LEAVE_UNUSED_POINTS
136         FatalErrorIn("meshReader::calcPointCells() const")
137             << "mesh has " << (nPoints - pointI)
138             << " points that were declared but not used" << endl;
139 #else
141         Info<< "removing " << (nPoints - pointI) << " unused points" << endl;
143         nPoints = pointI;
145         // adjust points and truncate
146         inplaceReorder(oldToNew, points());
147         points().setSize(nPoints);
149         // adjust pointCells and truncate
150         inplaceReorder(oldToNew, ptCells);
151         ptCells.setSize(nPoints);
153         // adjust cellFaces - this could be faster
154         // For each cell
155         forAll(cFaces, cellI)
156         {
157             faceList& faces = cFaces[cellI];
159             // For each face
160             forAll(faces, i)
161             {
162                 inplaceRenumber(oldToNew, faces[i]);
163             }
164         }
165 #endif
166     }
170 const Foam::labelListList& Foam::meshReader::pointCells() const
172     if (!pointCellsPtr_)
173     {
174         calcPointCells();
175     }
177     return *pointCellsPtr_;
181 // ************************************************************************* //