ensightCellsAddr.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) 2020 OpenCFD Ltd.
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 \*---------------------------------------------------------------------------*/
27 
28 #include "ensightCells.H"
29 #include "polyMesh.H"
30 #include "globalIndex.H"
31 #include "globalMeshData.H"
32 #include "ListOps.H"
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
38 {
39  const label nEstimate = 8*this->size();
40 
41  Map<label> pointMap(nEstimate);
42 
43  // Pass 1: markup used points from cells
44 
45  for (const label celli : this->cellIds())
46  {
47  for (const label facei : mesh.cells()[celli])
48  {
49  for (const label pointi : mesh.faces()[facei])
50  {
51  pointMap.insert(pointi, 0);
52  }
53  }
54  }
55 
56  // Compact point numbering, preserves the original order
57  label nPoints = 0;
58  for (const label pointi : pointMap.sortedToc())
59  {
60  pointMap(pointi) = nPoints++;
61  }
62 
63  return pointMap;
64 }
65 
66 
67 Foam::label Foam::ensightCells::meshPointMapppings
68 (
69  const polyMesh& mesh,
70  labelList& pointToGlobalRequest,
71  labelList& uniqueMeshPointLabels,
72  bool parallel
73 ) const
74 {
75  labelList pointToGlobal;
76 
77  const bool rewritePointMap = notNull(pointToGlobalRequest);
78 
79  if (notNull(pointToGlobalRequest))
80  {
81  pointToGlobal.transfer(pointToGlobalRequest);
82  }
83 
84 
85  const ensightCells& part = *this;
86 
87  parallel = parallel && Pstream::parRun();
88 
89  // Renumber the points/faces into unique points
90  label nPoints = 0; // Total number of points
91 
92  bool allCells = (part.size() == mesh.nCells());
93 
94  if (parallel)
95  {
96  Foam::reduce(allCells, andOp<bool>());
97 
98  if (allCells)
99  {
100  // All cells used, and thus all points
101 
102  autoPtr<globalIndex> globalPointsPtr =
104  (
105  pointToGlobal,
106  uniqueMeshPointLabels
107  );
108 
109  nPoints = globalPointsPtr().size(); // nPoints (global)
110  }
111  else
112  {
113  // Map mesh point index to local (compact) point index
114 
115  Map<label> meshPointMap(part.meshPointMap(mesh));
116 
117  labelList meshPoints(meshPointMap.sortedToc());
118 
119  autoPtr<globalIndex> globalPointsPtr =
121  (
122  meshPoints,
123  meshPointMap,
124  pointToGlobal,
125  uniqueMeshPointLabels
126  );
127 
128  nPoints = globalPointsPtr().size(); // nPoints (global)
129 
130  meshPointMap.clear();
131 
132  // The mergePoints returns pointToGlobal under the
133  // assumption of local addressing
134  // (eg, patch localFaces).
135  // Recast as original mesh points to new global points
136 
137  if (rewritePointMap)
138  {
139  labelList oldToNew(mesh.nPoints(), -1);
140 
141  forAll(meshPoints, i)
142  {
143  const label orig = meshPoints[i];
144  const label glob = pointToGlobal[i];
145 
146  oldToNew[orig] = glob;
147  }
148 
149  pointToGlobal.transfer(oldToNew);
150  }
151  }
152  }
153  else
154  {
155  // Non-parallel
156 
157  nPoints = mesh.nPoints();
158  pointToGlobal.resize(nPoints);
159 
160  if (allCells)
161  {
162  // All cells used, and thus all points
163 
164  uniqueMeshPointLabels.resize(nPoints);
165 
166  ListOps::identity(pointToGlobal);
167  ListOps::identity(uniqueMeshPointLabels);
168  }
169  else
170  {
171  // Mark up with -1 for unused entries
172  pointToGlobal = -1;
173 
174  nPoints = 0;
175 
176  // Pass 1: markup used points from cells
177 
178  for (const label celli : this->cellIds())
179  {
180  for (const label facei : mesh.cells()[celli])
181  {
182  for (const label pointi : mesh.faces()[facei])
183  {
184  if (pointToGlobal[pointi] == -1)
185  {
186  pointToGlobal[pointi] = nPoints++;
187  }
188  }
189  }
190  }
191 
192  // Pass 2:
193  //
194  // Compact point numbering, preserving original point order
195  uniqueMeshPointLabels.resize(nPoints);
196 
197  nPoints = 0;
198  forAll(pointToGlobal, pointi)
199  {
200  if (pointToGlobal[pointi] != -1)
201  {
202  pointToGlobal[pointi] = nPoints;
203 
204  uniqueMeshPointLabels[nPoints] = pointi;
205 
206  ++nPoints;
207  }
208  }
209  }
210  }
211 
212  if (notNull(pointToGlobalRequest))
213  {
214  pointToGlobalRequest.transfer(pointToGlobal);
215  }
216 
217  return nPoints;
218 }
219 
220 
222 (
223  const polyMesh& mesh,
224  labelList& uniqueMeshPointLabels,
225  bool parallel
226 ) const
227 {
228  return meshPointMapppings
229  (
230  mesh,
231  const_cast<labelList&>(labelList::null()), // Ignore pointToGlobal
232  uniqueMeshPointLabels,
233  parallel
234  );
235 }
236 
237 
238 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::List::null
static const List< T > & null()
Return a null List.
Definition: ListI.H:109
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
globalMeshData.H
globalIndex.H
Foam::primitiveMesh::cells
const cellList & cells() const
Definition: primitiveMeshCells.C:138
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: lumpedPointController.H:69
Foam::ensightCells::uniqueMeshPoints
label uniqueMeshPoints(const polyMesh &mesh, labelList &uniqueMeshPointLabels, bool parallel) const
Globally unique mesh points. Required when writing point fields.
Definition: ensightCellsAddr.C:222
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::primitiveMesh::nPoints
label nPoints() const noexcept
Number of mesh points.
Definition: primitiveMeshI.H:37
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::globalMeshData::mergePoints
autoPtr< globalIndex > mergePoints(labelList &pointToGlobal, labelList &uniquePoints) const
Helper for merging (collocated!) mesh point data.
Definition: globalMeshData.C:2374
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::primitiveMesh::nCells
label nCells() const noexcept
Number of mesh cells.
Definition: primitiveMeshI.H:96
Foam::ensightCells::meshPointMap
Map< label > meshPointMap(const polyMesh &mesh) const
Mesh point map.
Definition: ensightCellsAddr.C:37
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
Foam::ensightCells::cellIds
const labelList & cellIds() const
Processor-local cell ids of all elements.
Definition: ensightCellsI.H:73
Foam::notNull
bool notNull(const T *ptr)
True if ptr is not a pointer (of type T) to the nullObject.
Definition: nullObject.H:207
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::ensightPart::size
label size() const noexcept
Processor-local size of all elements.
Definition: ensightPart.H:154
Foam::polyMesh::faces
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1094
Foam::UPstream::parRun
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Foam::List< label >
Foam::ListOps::identity
void identity(labelUList &map, label start=0)
Set identity map with (map[i] == i)
Definition: ListOps.C:203
ListOps.H
Various functions to operate on Lists.
Foam::polyMesh::globalData
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1295
ensightCells.H