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  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "cellDistFuncs.H"
30 #include "polyMesh.H"
31 #include "polyBoundaryMesh.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 defineTypeNameAndDebug(cellDistFuncs, 0);
38 }
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
42 Foam::cellDistFuncs::cellDistFuncs(const polyMesh& mesh)
43 :
44  mesh_(mesh)
45 {}
46 
47 
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 
51 (
53 ) const
54 {
55  return mesh().boundaryMesh().patchSet(patchNames, false);
56 }
57 
58 
59 // Return smallest true distance from p to any of wallFaces.
60 // Note that even if normal hits face we still check other faces.
61 // Note that wallFaces is untruncated and we explicitly pass in size.
63 (
64  const point& p,
65  const polyPatch& patch,
66  const labelUList& wallFaces,
67  label& minFacei
68 ) const
69 {
70  const pointField& points = patch.points();
71 
72  scalar minDist = GREAT;
73  minFacei = -1;
74 
75  for (const label patchFacei : wallFaces)
76  {
77  const pointHit curHit = patch[patchFacei].nearestPoint(p, points);
78 
79  if (curHit.distance() < minDist)
80  {
81  minDist = curHit.distance();
82  minFacei = patch.start() + patchFacei;
83  }
84  }
85 
86  return minDist;
87 }
88 
89 
90 // Get point neighbours of facei (including facei). Returns number of faces.
91 // Note: does not allocate storage but does use linear search to determine
92 // uniqueness. For polygonal faces this might be quite inefficient.
94 (
95  const primitivePatch& patch,
96  const label patchFacei,
97  DynamicList<label>& neighbours
98 ) const
99 {
100  neighbours.clear();
101 
102  // Add myself
103  neighbours.append(patchFacei);
104 
105  // Add all face neighbours
106  const labelList& faceNeighbours = patch.faceFaces()[patchFacei];
107 
108  for (const label nbr : faceNeighbours)
109  {
110  if (!neighbours.found(nbr))
111  {
112  neighbours.append(nbr);
113  }
114  }
115 
116  // Add all point-only neighbours by linear searching in edge neighbours.
117  // Assumes that point-only neighbours are not using multiple points on
118  // face.
119 
120  const face& f = patch.localFaces()[patchFacei];
121 
122  forAll(f, fp)
123  {
124  label pointi = f[fp];
125 
126  const labelList& pointNbs = patch.pointFaces()[pointi];
127 
128  for (const label facei : pointNbs)
129  {
130  // Check for facei in edge-neighbours part of neighbours
131  if (!neighbours.found(facei))
132  {
133  neighbours.append(facei);
134  }
135  }
136  }
137 
138 
139  if (debug)
140  {
141  // Check for duplicates
142 
143  // Use hashSet to determine nbs.
144  labelHashSet nbs(4*f.size());
145 
146  forAll(f, fp)
147  {
148  const labelList& pointNbs = patch.pointFaces()[f[fp]];
149  nbs.insert(pointNbs);
150  }
151 
152  // Subtract ours.
153  for (const label nb : neighbours)
154  {
155  if (!nbs.found(nb))
156  {
158  << "getPointNeighbours : patchFacei:" << patchFacei
159  << " verts:" << f << endl;
160 
161  forAll(f, fp)
162  {
164  << "point:" << f[fp] << " pointFaces:"
165  << patch.pointFaces()[f[fp]] << endl;
166  }
167 
168  for (const label facei : neighbours)
169  {
171  << "fast nbr:" << facei
172  << endl;
173  }
174 
176  << "Problem: fast pointNeighbours routine included " << nb
177  << " which is not in proper neighbour list " << nbs.toc()
178  << abort(FatalError);
179  }
180  nbs.erase(nb);
181  }
182 
183  if (nbs.size())
184  {
186  << "Problem: fast pointNeighbours routine did not find "
187  << nbs.toc() << abort(FatalError);
188  }
189  }
190 }
191 
192 
193 // size of largest patch (out of supplied subset of patches)
195 (
196  const labelHashSet& patchIDs
197 ) const
198 {
199  label maxSize = 0;
200 
201  forAll(mesh().boundaryMesh(), patchi)
202  {
203  if (patchIDs.found(patchi))
204  {
205  const polyPatch& patch = mesh().boundaryMesh()[patchi];
206 
207  maxSize = Foam::max(maxSize, patch.size());
208  }
209  }
210  return maxSize;
211 }
212 
213 
214 // sum of patch sizes (out of supplied subset of patches)
216 (
217  const labelHashSet& patchIDs
218 )
219 const
220 {
221  label sum = 0;
222 
223  forAll(mesh().boundaryMesh(), patchi)
224  {
225  if (patchIDs.found(patchi))
226  {
227  const polyPatch& patch = mesh().boundaryMesh()[patchi];
228 
229  sum += patch.size();
230  }
231  }
232  return sum;
233 }
234 
235 
236 // Gets nearest wall for cells next to wall
238 (
239  const labelHashSet& patchIDs,
240  scalarField& wallDistCorrected,
241  Map<label>& nearestFace
242 ) const
243 {
244  // Size neighbours array for maximum possible (= size of largest patch)
245  DynamicList<label> neighbours(maxPatchSize(patchIDs));
246 
247  // Correct all cells with face on wall
248  const vectorField& cellCentres = mesh().cellCentres();
249  const labelList& faceOwner = mesh().faceOwner();
250 
251  forAll(mesh().boundaryMesh(), patchi)
252  {
253  if (patchIDs.found(patchi))
254  {
255  const polyPatch& patch = mesh().boundaryMesh()[patchi];
256 
257  // Check cells with face on wall
258  forAll(patch, patchFacei)
259  {
260  getPointNeighbours(patch, patchFacei, neighbours);
261 
262  label celli = faceOwner[patch.start() + patchFacei];
263 
264  label minFacei = -1;
265 
266  wallDistCorrected[celli] = smallestDist
267  (
268  cellCentres[celli],
269  patch,
270  neighbours,
271  minFacei
272  );
273 
274  // Store wallCell and its nearest neighbour
275  nearestFace.insert(celli, minFacei);
276  }
277  }
278  }
279 }
280 
281 
282 // Correct all cells connected to wall (via point) and not in nearestFace
284 (
285  const labelHashSet& patchIDs,
286  scalarField& wallDistCorrected,
287  Map<label>& nearestFace
288 ) const
289 {
290  // Correct all (non-visited) cells with point on wall
291 
292  const vectorField& cellCentres = mesh().cellCentres();
293 
294  forAll(mesh().boundaryMesh(), patchi)
295  {
296  if (patchIDs.found(patchi))
297  {
298  const polyPatch& patch = mesh().boundaryMesh()[patchi];
299 
300  const labelList& meshPoints = patch.meshPoints();
301  const labelListList& pointFaces = patch.pointFaces();
302 
303  forAll(meshPoints, meshPointi)
304  {
305  const label vertI = meshPoints[meshPointi];
306 
307  const labelList& neighbours = mesh().pointCells(vertI);
308 
309  for (const label celli : neighbours)
310  {
311  if (!nearestFace.found(celli))
312  {
313  const labelList& wallFaces = pointFaces[meshPointi];
314 
315  label minFacei = -1;
316 
317  wallDistCorrected[celli] = smallestDist
318  (
319  cellCentres[celli],
320  patch,
321  wallFaces,
322  minFacei
323  );
324 
325  // Store wallCell and its nearest neighbour
326  nearestFace.insert(celli, minFacei);
327  }
328  }
329  }
330  }
331  }
332 }
333 
334 
335 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::PointHit
Describes the interaction of a face and a point. It carries the info of a successful hit and (if succ...
Definition: PointHit.H:53
Foam::DynamicList< label >
Foam::cellDistFuncs::maxPatchSize
label maxPatchSize(const labelHashSet &patchIDs) const
Size of largest patch (out of supplied subset of patches)
Definition: cellDistFuncs.C:195
Foam::PointHit::distance
scalar distance() const noexcept
Return distance to hit.
Definition: PointHit.H:139
Foam::Map< label >
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:444
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
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:296
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:284
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:68
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:474
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
Foam::polyMesh::faceOwner
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1107
Foam::cellDistFuncs::sumPatchSize
label sumPatchSize(const labelHashSet &patchIDs) const
Sum of patch sizes (out of supplied subset of patches).
Definition: cellDistFuncs.C:216
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:281
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::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:144
Foam::primitiveMesh::cellCentres
const vectorField & cellCentres() const
Definition: primitiveMeshCellCentresAndVols.C:84
Foam::cellDistFuncs::smallestDist
scalar smallestDist(const point &p, const polyPatch &patch, const labelUList &wallFaces, label &meshFacei) const
Calculate smallest true distance (and face index)
Definition: cellDistFuncs.C:63
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
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:862
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:62
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:72
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:238
Foam::HashTable::found
bool found(const Key &key) const
Return true if hashed entry is found in table.
Definition: HashTableI.H:100
Foam::cellDistFuncs::getPointNeighbours
void getPointNeighbours(const primitivePatch &, const label patchFacei, DynamicList< label > &) const
Get faces sharing point with face on patch.
Definition: cellDistFuncs.C:94
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:85