cellDistFuncs.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 \*---------------------------------------------------------------------------*/
27 
28 #include "cellDistFuncs.H"
29 #include "polyMesh.H"
30 #include "wallPolyPatch.H"
31 #include "polyBoundaryMesh.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 defineTypeNameAndDebug(cellDistFuncs, 0);
38 }
39 
40 
41 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
42 
43 // Find val in first nElems elements of elems.
44 Foam::label Foam::cellDistFuncs::findIndex
45 (
46  const label nElems,
47  const labelList& elems,
48  const label val
49 )
50 {
51  for (label i = 0; i < nElems; i++)
52  {
53  if (elems[i] == val)
54  {
55  return i;
56  }
57  }
58  return -1;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
64 Foam::cellDistFuncs::cellDistFuncs(const polyMesh& mesh)
65 :
66  mesh_(mesh)
67 {}
68 
69 
70 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
71 
73 (
75 ) const
76 {
77  return mesh().boundaryMesh().patchSet(patchNames, false);
78 }
79 
80 
81 // Return smallest true distance from p to any of wallFaces.
82 // Note that even if normal hits face we still check other faces.
83 // Note that wallFaces is untruncated and we explicitly pass in size.
85 (
86  const point& p,
87  const polyPatch& patch,
88  const label nWallFaces,
89  const labelList& wallFaces,
90  label& minFacei
91 ) const
92 {
93  const pointField& points = patch.points();
94 
95  scalar minDist = GREAT;
96  minFacei = -1;
97 
98  for (label wallFacei = 0; wallFacei < nWallFaces; wallFacei++)
99  {
100  label patchFacei = wallFaces[wallFacei];
101 
102  pointHit curHit = patch[patchFacei].nearestPoint(p, points);
103 
104  if (curHit.distance() < minDist)
105  {
106  minDist = curHit.distance();
107  minFacei = patch.start() + patchFacei;
108  }
109  }
110 
111  return minDist;
112 }
113 
114 
115 // Get point neighbours of facei (including facei). Returns number of faces.
116 // Note: does not allocate storage but does use linear search to determine
117 // uniqueness. For polygonal faces this might be quite inefficient.
119 (
120  const primitivePatch& patch,
121  const label patchFacei,
122  labelList& neighbours
123 ) const
124 {
125  label nNeighbours = 0;
126 
127  // Add myself
128  neighbours[nNeighbours++] = patchFacei;
129 
130  // Add all face neighbours
131  const labelList& faceNeighbours = patch.faceFaces()[patchFacei];
132 
133  forAll(faceNeighbours, faceNeighbourI)
134  {
135  neighbours[nNeighbours++] = faceNeighbours[faceNeighbourI];
136  }
137 
138  // Remember part of neighbours that contains edge-connected faces.
139  label nEdgeNbs = nNeighbours;
140 
141 
142  // Add all point-only neighbours by linear searching in edge neighbours.
143  // Assumes that point-only neighbours are not using multiple points on
144  // face.
145 
146  const face& f = patch.localFaces()[patchFacei];
147 
148  forAll(f, fp)
149  {
150  label pointi = f[fp];
151 
152  const labelList& pointNbs = patch.pointFaces()[pointi];
153 
154  forAll(pointNbs, nbI)
155  {
156  label facei = pointNbs[nbI];
157 
158  // Check for facei in edge-neighbours part of neighbours
159  if (findIndex(nEdgeNbs, neighbours, facei) == -1)
160  {
161  neighbours[nNeighbours++] = facei;
162  }
163  }
164  }
165 
166 
167  if (debug)
168  {
169  // Check for duplicates
170 
171  // Use hashSet to determine nbs.
172  labelHashSet nbs(4*f.size());
173 
174  forAll(f, fp)
175  {
176  const labelList& pointNbs = patch.pointFaces()[f[fp]];
177  nbs.insert(pointNbs);
178  }
179 
180  // Subtract ours.
181  for (label i = 0; i < nNeighbours; i++)
182  {
183  label nb = neighbours[i];
184 
185  if (!nbs.found(nb))
186  {
188  << "getPointNeighbours : patchFacei:" << patchFacei
189  << " verts:" << f << endl;
190 
191  forAll(f, fp)
192  {
194  << "point:" << f[fp] << " pointFaces:"
195  << patch.pointFaces()[f[fp]] << endl;
196  }
197 
198  for (label i = 0; i < nNeighbours; i++)
199  {
201  << "fast nbr:" << neighbours[i]
202  << endl;
203  }
204 
206  << "Problem: fast pointNeighbours routine included " << nb
207  << " which is not in proper neighbour list " << nbs.toc()
208  << abort(FatalError);
209  }
210  nbs.erase(nb);
211  }
212 
213  if (nbs.size())
214  {
216  << "Problem: fast pointNeighbours routine did not find "
217  << nbs.toc() << abort(FatalError);
218  }
219  }
220 
221  return nNeighbours;
222 }
223 
224 
225 // size of largest patch (out of supplied subset of patches)
227 (
228  const labelHashSet& patchIDs
229 ) const
230 {
231  label maxSize = 0;
232 
233  forAll(mesh().boundaryMesh(), patchi)
234  {
235  if (patchIDs.found(patchi))
236  {
237  const polyPatch& patch = mesh().boundaryMesh()[patchi];
238 
239  maxSize = Foam::max(maxSize, patch.size());
240  }
241  }
242  return maxSize;
243 }
244 
245 
246 // sum of patch sizes (out of supplied subset of patches)
248 (
249  const labelHashSet& patchIDs
250 )
251 const
252 {
253  label sum = 0;
254 
255  forAll(mesh().boundaryMesh(), patchi)
256  {
257  if (patchIDs.found(patchi))
258  {
259  const polyPatch& patch = mesh().boundaryMesh()[patchi];
260 
261  sum += patch.size();
262  }
263  }
264  return sum;
265 }
266 
267 
268 // Gets nearest wall for cells next to wall
270 (
271  const labelHashSet& patchIDs,
272  scalarField& wallDistCorrected,
273  Map<label>& nearestFace
274 ) const
275 {
276  // Size neighbours array for maximum possible (= size of largest patch)
277  label maxPointNeighbours = maxPatchSize(patchIDs);
278 
279  labelList neighbours(maxPointNeighbours);
280 
281 
282  // Correct all cells with face on wall
283  const vectorField& cellCentres = mesh().cellCentres();
284  const labelList& faceOwner = mesh().faceOwner();
285 
286  forAll(mesh().boundaryMesh(), patchi)
287  {
288  if (patchIDs.found(patchi))
289  {
290  const polyPatch& patch = mesh().boundaryMesh()[patchi];
291 
292  // Check cells with face on wall
293  forAll(patch, patchFacei)
294  {
295  label nNeighbours = getPointNeighbours
296  (
297  patch,
298  patchFacei,
299  neighbours
300  );
301 
302  label celli = faceOwner[patch.start() + patchFacei];
303 
304  label minFacei = -1;
305 
306  wallDistCorrected[celli] = smallestDist
307  (
308  cellCentres[celli],
309  patch,
310  nNeighbours,
311  neighbours,
312  minFacei
313  );
314 
315  // Store wallCell and its nearest neighbour
316  nearestFace.insert(celli, minFacei);
317  }
318  }
319  }
320 
321 }
322 
323 
324 // Correct all cells connected to wall (via point) and not in nearestFace
326 (
327  const labelHashSet& patchIDs,
328  scalarField& wallDistCorrected,
329  Map<label>& nearestFace
330 ) const
331 {
332  // Correct all (non-visited) cells with point on wall
333 
334  const vectorField& cellCentres = mesh().cellCentres();
335 
336  forAll(mesh().boundaryMesh(), patchi)
337  {
338  if (patchIDs.found(patchi))
339  {
340  const polyPatch& patch = mesh().boundaryMesh()[patchi];
341 
342  const labelList& meshPoints = patch.meshPoints();
343  const labelListList& pointFaces = patch.pointFaces();
344 
345  forAll(meshPoints, meshPointi)
346  {
347  label vertI = meshPoints[meshPointi];
348 
349  const labelList& neighbours = mesh().pointCells(vertI);
350 
351  forAll(neighbours, neighbourI)
352  {
353  label celli = neighbours[neighbourI];
354 
355  if (!nearestFace.found(celli))
356  {
357  const labelList& wallFaces = pointFaces[meshPointi];
358 
359  label minFacei = -1;
360 
361  wallDistCorrected[celli] = smallestDist
362  (
363  cellCentres[celli],
364  patch,
365  wallFaces.size(),
366  wallFaces,
367  minFacei
368  );
369 
370  // Store wallCell and its nearest neighbour
371  nearestFace.insert(celli, minFacei);
372  }
373  }
374  }
375  }
376  }
377 }
378 
379 
380 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
p
volScalarField & p
Definition: createFieldRefs.H:8
nWallFaces
label nWallFaces(0)
Foam::PointHit< point >
Foam::cellDistFuncs::maxPatchSize
label maxPatchSize(const labelHashSet &patchIDs) const
Size of largest patch (out of supplied subset of patches)
Definition: cellDistFuncs.C:227
Foam::cellDistFuncs::smallestDist
scalar smallestDist(const point &p, const polyPatch &patch, const label nWallFaces, const labelList &wallFaces, label &meshFacei) const
Calculate smallest true distance (and face index)
Definition: cellDistFuncs.C:85
wallPolyPatch.H
Foam::Map< label >
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:435
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
cellDistFuncs.H
polyMesh.H
Foam::HashSet< label, Hash< label > >
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::cellDistFuncs::correctBoundaryPointCells
void correctBoundaryPointCells(const labelHashSet &patchIDs, scalarField &wallDistCorrected, Map< label > &nearestFace) const
Correct all cells connected to wall (via point). Sets values in.
Definition: cellDistFuncs.C:326
Foam::PointHit::distance
scalar distance() const
Return distance to hit.
Definition: PointHit.H:143
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::Field< vector >
Foam::cellDistFuncs::getPatchIDs
labelHashSet getPatchIDs() const
Get patchIDs of/derived off certain type (e.g. 'processorPolyPatch')
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
Foam::polyMesh::faceOwner
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1076
Foam::cellDistFuncs::sumPatchSize
label sumPatchSize(const labelHashSet &patchIDs) const
Sum of patch sizes (out of supplied subset of patches).
Definition: cellDistFuncs.C:248
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:272
patchNames
wordList patchNames(nPatches)
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::cellDistFuncs::getPointNeighbours
label getPointNeighbours(const primitivePatch &, const label patchFacei, labelList &) const
Get faces sharing point with face on patch.
Definition: cellDistFuncs.C:119
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::primitiveMesh::cellCentres
const vectorField & cellCentres() const
Definition: primitiveMeshCellCentresAndVols.C:176
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
f
labelList f(nPoints)
Foam::foamVersion::patch
const std::string patch
OpenFOAM patch number as a std::string.
Foam::Vector< scalar >
Foam::List< label >
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::polyBoundaryMesh::patchSet
labelHashSet patchSet(const UList< wordRe > &patchNames, const bool warnNotFound=true, const bool useGroups=true) const
Return the set of patch IDs corresponding to the given names.
Definition: polyBoundaryMesh.C:857
Foam::primitiveMesh::pointCells
const labelListList & pointCells() const
Definition: primitiveMeshPointCells.C:110
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
polyBoundaryMesh.H
Foam::boundaryMesh
Addressing for all faces on surface of mesh. Can either be read from polyMesh or from triSurface....
Definition: boundaryMesh.H:61
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:74
Foam::cellDistFuncs::correctBoundaryFaceCells
void correctBoundaryFaceCells(const labelHashSet &patchIDs, scalarField &wallDistCorrected, Map< label > &nearestFace) const
Correct all cells connected to boundary (via face). Sets values in.
Definition: cellDistFuncs.C:270
Foam::HashTable::found
bool found(const Key &key) const
Return true if hashed entry is found in table.
Definition: HashTableI.H:100
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:90