ENH: sampledSet: make starting cell/position consistent with lagrangian tracking
[OpenFOAM-2.0.x.git] / src / conversion / meshReader / calcPointCells.C
blob4da904faa710182efbd191b1d6027459384827c7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 Description
25     calculate point cells - ie, the cells attached to each point
27     - remove unused points, adjust pointCells and cellFaces accordingly
28 \*---------------------------------------------------------------------------*/
30 #include "meshReader.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 void Foam::meshReader::calcPointCells() const
36     static const label UNIT_POINT_CELLS = 12;
38     if (pointCellsPtr_)
39     {
40         FatalErrorIn("meshReader::calcPointCells() const")
41             << "pointCells already calculated"
42             << abort(FatalError);
43     }
45     label nPoints = points_.size();
47     pointCellsPtr_ = new labelListList(nPoints);
48     labelListList& ptCells = *pointCellsPtr_;
50     forAll(ptCells, i)
51     {
52         ptCells[i].setSize(UNIT_POINT_CELLS);
53     }
55     // Initialize the list of labels which will hold the count of the
56     // actual number of cells per point during the analysis
57     labelList cellCount(nPoints, 0);
59     // Note. Unlike the standard point-cell algorithm, which asks the cell for
60     // the supporting point labels, we need to work based on the cell faces.
61     // This is because some of the faces do not come from the cell shape.
62     // It is also advantageous to remove duplicates from the point-cell
63     // addressing, because this removes a lot of waste later.
65     faceListList& cFaces = cellFaces();
67     // For each cell
68     forAll(cFaces, cellI)
69     {
70         const faceList& faces = cFaces[cellI];
72         forAll(faces, i)
73         {
74             // For each vertex
75             const labelList& labels = faces[i];
77             forAll(labels, j)
78             {
79                 // Set working point label
80                 label curPoint = labels[j];
81                 labelList& curPointCells = ptCells[curPoint];
82                 label curCount = cellCount[curPoint];
84                 // check if the cell has been added before
85                 bool found = false;
87                 for (label f = 0; f < curCount; f++)
88                 {
89                     if (curPointCells[f] == cellI)
90                     {
91                         found = true;
92                         break;
93                     }
94                 }
96                 if (!found)
97                 {
98                     // If the list of pointCells is not big enough, double it
99                     if (curPointCells.size() <= curCount)
100                     {
101                         curPointCells.setSize(curPointCells.size()*2);
102                     }
104                     // Enter the cell label in the point's cell list
105                     curPointCells[curCount] = cellI;
107                     // Increment the cell count for the point addressed
108                     cellCount[curPoint]++;
109                 }
110             }
111         }
112     }
114     // report and remove unused points
115     // - adjust points, pointCells, and cellFaces accordingly
116     label pointI = 0;
117     labelList oldToNew(nPoints, -1);
119     forAll(ptCells, i)
120     {
121         ptCells[i].setSize(cellCount[i]);
122         if (cellCount[i] > 0)
123         {
124             oldToNew[i] = pointI++;
125         }
126     }
128     // report unused points
129     if (nPoints > pointI)
130     {
131         Info<< "removing " << (nPoints - pointI) << " unused points" << endl;
133         nPoints = pointI;
135         // adjust points and truncate - bend const-ness
136         pointField& adjustedPoints = const_cast<pointField&>(points_);
138         inplaceReorder(oldToNew, adjustedPoints);
139         adjustedPoints.setSize(nPoints);
141         // adjust pointCells and truncate
142         inplaceReorder(oldToNew, ptCells);
143         ptCells.setSize(nPoints);
145         // adjust cellFaces - this could be faster
146         // For each cell
147         forAll(cFaces, cellI)
148         {
149             faceList& faces = cFaces[cellI];
151             // For each face
152             forAll(faces, i)
153             {
154                 inplaceRenumber(oldToNew, faces[i]);
155             }
156         }
157     }
161 const Foam::labelListList& Foam::meshReader::pointCells() const
163     if (!pointCellsPtr_)
164     {
165         calcPointCells();
166     }
168     return *pointCellsPtr_;
172 // ************************************************************************* //