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  neighbours.appendUniq(nbr);
111  }
112 
113  // Add all point-only neighbours by linear searching in edge neighbours.
114  // Assumes that point-only neighbours are not using multiple points on
115  // face.
116 
117  const face& f = patch.localFaces()[patchFacei];
118 
119  forAll(f, fp)
120  {
121  label pointi = f[fp];
122 
123  const labelList& pointNbs = patch.pointFaces()[pointi];
124 
125  for (const label facei : pointNbs)
126  {
127  // Check for facei in edge-neighbours part of neighbours
128  neighbours.appendUniq(facei);
129  }
130  }
131 
132 
133  if (debug)
134  {
135  // Check for duplicates
136 
137  // Use hashSet to determine nbs.
138  labelHashSet nbs(4*f.size());
139 
140  forAll(f, fp)
141  {
142  const labelList& pointNbs = patch.pointFaces()[f[fp]];
143  nbs.insert(pointNbs);
144  }
145 
146  // Subtract ours.
147  for (const label nb : neighbours)
148  {
149  if (!nbs.found(nb))
150  {
152  << "getPointNeighbours : patchFacei:" << patchFacei
153  << " verts:" << f << endl;
154 
155  forAll(f, fp)
156  {
158  << "point:" << f[fp] << " pointFaces:"
159  << patch.pointFaces()[f[fp]] << endl;
160  }
161 
162  for (const label facei : neighbours)
163  {
165  << "fast nbr:" << facei
166  << endl;
167  }
168 
170  << "Problem: fast pointNeighbours routine included " << nb
171  << " which is not in proper neighbour list " << nbs.toc()
172  << abort(FatalError);
173  }
174  nbs.erase(nb);
175  }
176 
177  if (nbs.size())
178  {
180  << "Problem: fast pointNeighbours routine did not find "
181  << nbs.toc() << abort(FatalError);
182  }
183  }
184 }
185 
186 
187 // size of largest patch (out of supplied subset of patches)
189 (
190  const labelHashSet& patchIDs
191 ) const
192 {
193  label maxSize = 0;
194 
195  forAll(mesh().boundaryMesh(), patchi)
196  {
197  if (patchIDs.found(patchi))
198  {
199  const polyPatch& patch = mesh().boundaryMesh()[patchi];
200 
201  maxSize = Foam::max(maxSize, patch.size());
202  }
203  }
204  return maxSize;
205 }
206 
207 
208 // sum of patch sizes (out of supplied subset of patches)
210 (
211  const labelHashSet& patchIDs
212 )
213 const
214 {
215  label sum = 0;
216 
217  forAll(mesh().boundaryMesh(), patchi)
218  {
219  if (patchIDs.found(patchi))
220  {
221  const polyPatch& patch = mesh().boundaryMesh()[patchi];
222 
223  sum += patch.size();
224  }
225  }
226  return sum;
227 }
228 
229 
230 // Gets nearest wall for cells next to wall
232 (
233  const labelHashSet& patchIDs,
234  scalarField& wallDistCorrected,
235  Map<label>& nearestFace
236 ) const
237 {
238  // Size neighbours array for maximum possible (= size of largest patch)
239  DynamicList<label> neighbours(maxPatchSize(patchIDs));
240 
241  // Correct all cells with face on wall
242  const vectorField& cellCentres = mesh().cellCentres();
243  const labelList& faceOwner = mesh().faceOwner();
244 
245  forAll(mesh().boundaryMesh(), patchi)
246  {
247  if (patchIDs.found(patchi))
248  {
249  const polyPatch& patch = mesh().boundaryMesh()[patchi];
250 
251  // Check cells with face on wall
252  forAll(patch, patchFacei)
253  {
254  getPointNeighbours(patch, patchFacei, neighbours);
255 
256  label celli = faceOwner[patch.start() + patchFacei];
257 
258  label minFacei = -1;
259 
260  wallDistCorrected[celli] = smallestDist
261  (
262  cellCentres[celli],
263  patch,
264  neighbours,
265  minFacei
266  );
267 
268  // Store wallCell and its nearest neighbour
269  nearestFace.insert(celli, minFacei);
270  }
271  }
272  }
273 }
274 
275 
276 // Correct all cells connected to wall (via point) and not in nearestFace
278 (
279  const labelHashSet& patchIDs,
280  scalarField& wallDistCorrected,
281  Map<label>& nearestFace
282 ) const
283 {
284  // Correct all (non-visited) cells with point on wall
285 
286  const vectorField& cellCentres = mesh().cellCentres();
287 
288  forAll(mesh().boundaryMesh(), patchi)
289  {
290  if (patchIDs.found(patchi))
291  {
292  const polyPatch& patch = mesh().boundaryMesh()[patchi];
293 
294  const labelList& meshPoints = patch.meshPoints();
295  const labelListList& pointFaces = patch.pointFaces();
296 
297  forAll(meshPoints, meshPointi)
298  {
299  const label vertI = meshPoints[meshPointi];
300 
301  const labelList& neighbours = mesh().pointCells(vertI);
302 
303  for (const label celli : neighbours)
304  {
305  if (!nearestFace.found(celli))
306  {
307  const labelList& wallFaces = pointFaces[meshPointi];
308 
309  label minFacei = -1;
310 
311  wallDistCorrected[celli] = smallestDist
312  (
313  cellCentres[celli],
314  patch,
315  wallFaces,
316  minFacei
317  );
318 
319  // Store wallCell and its nearest neighbour
320  nearestFace.insert(celli, minFacei);
321  }
322  }
323  }
324  }
325  }
326 }
327 
328 
329 // ************************************************************************* //
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:189
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:369
cellDistFuncs.H
polyMesh.H
Foam::HashSet< label, Hash< label > >
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
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:278
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:511
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:210
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:306
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:453
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:864
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:232
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::DynamicList::appendUniq
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: DynamicListI.H:689
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:79