calcPointCells.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Description
27  calculate point cells - ie, the cells attached to each point
28 
29  - remove unused points, adjust pointCells and cellFaces accordingly
30 \*---------------------------------------------------------------------------*/
31 
32 #include "meshReader.H"
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 void Foam::meshReader::calcPointCells() const
37 {
38  static const label UNIT_POINT_CELLS = 12;
39 
40  if (pointCellsPtr_)
41  {
43  << "pointCells already calculated"
44  << abort(FatalError);
45  }
46 
47  label nPoints = points_.size();
48 
49  pointCellsPtr_ = new labelListList(nPoints);
50  labelListList& ptCells = *pointCellsPtr_;
51 
52  forAll(ptCells, i)
53  {
54  ptCells[i].setSize(UNIT_POINT_CELLS);
55  }
56 
57  // Initialize the list of labels which will hold the count of the
58  // actual number of cells per point during the analysis
59  labelList cellCount(nPoints, Zero);
60 
61  // Note. Unlike the standard point-cell algorithm, which asks the cell for
62  // the supporting point labels, we need to work based on the cell faces.
63  // This is because some of the faces do not come from the cell shape.
64  // It is also advantageous to remove duplicates from the point-cell
65  // addressing, because this removes a lot of waste later.
66 
67  faceListList& cFaces = cellFaces();
68 
69  // For each cell
70  forAll(cFaces, celli)
71  {
72  const faceList& faces = cFaces[celli];
73 
74  forAll(faces, i)
75  {
76  // For each vertex
77  const labelList& labels = faces[i];
78 
79  forAll(labels, j)
80  {
81  // Set working point label
82  label curPoint = labels[j];
83  labelList& curPointCells = ptCells[curPoint];
84  label curCount = cellCount[curPoint];
85 
86  // check if the cell has been added before
87  bool found = false;
88 
89  for (label f = 0; f < curCount; f++)
90  {
91  if (curPointCells[f] == celli)
92  {
93  found = true;
94  break;
95  }
96  }
97 
98  if (!found)
99  {
100  // If the list of pointCells is not big enough, double it
101  if (curPointCells.size() <= curCount)
102  {
103  curPointCells.setSize(curPointCells.size()*2);
104  }
105 
106  // Enter the cell label in the point's cell list
107  curPointCells[curCount] = celli;
108 
109  // Increment the cell count for the point addressed
110  cellCount[curPoint]++;
111  }
112  }
113  }
114  }
115 
116  // report and remove unused points
117  // - adjust points, pointCells, and cellFaces accordingly
118  label pointi = 0;
119  labelList oldToNew(nPoints, -1);
120 
121  forAll(ptCells, i)
122  {
123  ptCells[i].setSize(cellCount[i]);
124  if (cellCount[i] > 0)
125  {
126  oldToNew[i] = pointi++;
127  }
128  }
129 
130  // report unused points
131  if (nPoints > pointi)
132  {
133  Info<< "removing " << (nPoints - pointi) << " unused points" << endl;
134 
135  nPoints = pointi;
136 
137  // adjust points and truncate - bend const-ness
138  pointField& adjustedPoints = const_cast<pointField&>(points_);
139 
140  inplaceReorder(oldToNew, adjustedPoints);
141  adjustedPoints.setSize(nPoints);
142 
143  // adjust pointCells and truncate
144  inplaceReorder(oldToNew, ptCells);
145  ptCells.setSize(nPoints);
146 
147  // adjust cellFaces - this could be faster
148  // For each cell
149  forAll(cFaces, celli)
150  {
151  faceList& faces = cFaces[celli];
152 
153  // For each face
154  forAll(faces, i)
155  {
156  inplaceRenumber(oldToNew, faces[i]);
157  }
158  }
159  }
160 }
161 
162 
163 const Foam::labelListList& Foam::meshReader::pointCells() const
164 {
165  if (!pointCellsPtr_)
166  {
167  calcPointCells();
168  }
169 
170  return *pointCellsPtr_;
171 }
172 
173 
174 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::inplaceRenumber
void inplaceRenumber(const labelUList &oldToNew, IntListType &input)
Inplace renumber the values (not the indices) of a list.
Definition: ListOpsTemplates.C:61
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::labelListList
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:56
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::faceListList
List< faceList > faceListList
A List of faceList.
Definition: faceListFwd.H:49
f
labelList f(nPoints)
Foam::faceList
List< face > faceList
A List of faces.
Definition: faceListFwd.H:47
Foam::List< labelList >
Foam::inplaceReorder
void inplaceReorder(const labelUList &oldToNew, ListType &input, const bool prune=false)
Inplace reorder the elements of a list.
Definition: ListOpsTemplates.C:124
meshReader.H
Foam::meshReader::points_
pointField points_
Points supporting the mesh.
Definition: meshReader.H:216