orient_zoneGenerator.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "orient_zoneGenerator.H"
27 #include "polyMesh.H"
28 #include "syncTools.H"
29 #include "patchFaceOrientation.H"
30 #include "PatchEdgeFaceWave.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  namespace zoneGenerators
38  {
41  (
43  orient,
45  );
46  }
47 }
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 Foam::boolList Foam::zoneGenerators::orient::normalOrientation
53 (
54  const labelList& faceIndices
55 ) const
56 {
57  const vectorField& faceAreas = mesh_.faceAreas();
58 
59  boolList flipMap(faceIndices.size(), false);
60 
61  forAll(faceIndices, fi)
62  {
63  const label facei = faceIndices[fi];
64 
65  if ((faceAreas[facei] & normal_) < 0)
66  {
67  flipMap[fi] = true;
68  }
69  }
70 
71  return flipMap;
72 }
73 
74 
75 Foam::boolList Foam::zoneGenerators::orient::pointOrientation
76 (
77  const faceZone& fZone
78 ) const
79 {
80  if (fZone.checkParallelSync())
81  {
83  << "Face zone " << fZone.name()
84  << " is not parallel synchronised."
85  << " Any coupled face also needs its coupled version to be included"
86  << " and with opposite flipMap."
87  << exit(FatalError);
88  }
89 
90  const labelList& faceIndices = fZone;
91 
92  const indirectPrimitivePatch patch
93  (
94  IndirectList<face>(mesh_.faces(), faceIndices),
95  mesh_.points()
96  );
97 
98  const PackedBoolList isMasterFace(syncTools::getMasterFaces(mesh_));
99 
100  // Data on all edges and faces
101  List<patchFaceOrientation> allEdgeInfo(patch.nEdges());
102  List<patchFaceOrientation> allFaceInfo(patch.size());
103 
104  // Make sure we don't walk through
105  // - slaves of coupled faces
106  // - non-manifold edges
107  {
108  const polyBoundaryMesh& bm = mesh_.boundaryMesh();
109 
110  label nProtected = 0;
111 
112  forAll(faceIndices, facei)
113  {
114  const label meshFacei = faceIndices[facei];
115  const label patchi = bm.whichPatch(meshFacei);
116 
117  if
118  (
119  patchi != -1
120  && bm[patchi].coupled()
121  && !isMasterFace[meshFacei]
122  )
123  {
124  // Slave side. Mark so doesn't get visited.
125  allFaceInfo[facei] = orientedSurface::NOFLIP;
126  nProtected++;
127  }
128  }
129 
130  if (debug)
131  {
132  Info<< "Protected from visiting "
133  << returnReduce(nProtected, sumOp<label>())
134  << " slaves of coupled faces" << nl << endl;
135  }
136  }
137  {
138  // Number of (master)faces per edge
139  labelList nMasterFaces(patch.nEdges(), 0);
140 
141  forAll(faceIndices, facei)
142  {
143  const label meshFacei = faceIndices[facei];
144 
145  if (isMasterFace[meshFacei])
146  {
147  const labelList& fEdges = patch.faceEdges()[facei];
148  forAll(fEdges, fEdgei)
149  {
150  nMasterFaces[fEdges[fEdgei]]++;
151  }
152  }
153  }
154 
156  (
157  mesh_,
158  patch.meshEdges(mesh_.edges(), mesh_.pointEdges()),
159  nMasterFaces,
160  plusEqOp<label>(),
161  label(0)
162  );
163 
164 
165  label nProtected = 0;
166 
167  forAll(nMasterFaces, edgei)
168  {
169  if (nMasterFaces[edgei] > 2)
170  {
171  allEdgeInfo[edgei] = orientedSurface::NOFLIP;
172  nProtected++;
173  }
174  }
175 
176  if (debug)
177  {
178  Info<< "Protected from visiting "
179  << returnReduce(nProtected, sumOp<label>())
180  << " non-manifold edges" << nl << endl;
181  }
182  }
183 
184 
185  DynamicList<label> changedEdges;
186  DynamicList<patchFaceOrientation> changedInfo;
187 
188  const scalar tol = PatchEdgeFaceWave
189  <
191  patchFaceOrientation
192  >::propagationTol();
193 
194  int dummyTrackData;
195 
196  globalIndex globalFaces(patch.size());
197 
198  while (true)
199  {
200  // Pick an unset face
201  label unsetFacei = labelMax;
202  forAll(allFaceInfo, facei)
203  {
204  if (allFaceInfo[facei] == orientedSurface::UNVISITED)
205  {
206  unsetFacei = globalFaces.toGlobal(facei);
207  break;
208  }
209  }
210 
211  reduce(unsetFacei, minOp<label>());
212 
213  if (unsetFacei == labelMax)
214  {
215  break;
216  }
217 
218  const label proci = globalFaces.whichProcID(unsetFacei);
219  const label seedFacei = globalFaces.toLocal(proci, unsetFacei);
220 
221  if (debug)
222  {
223  Info<< "Seeding from processor " << proci << " face " << seedFacei
224  << endl;
225  }
226 
227  if (proci == Pstream::myProcNo())
228  {
229  // Determine orientation of seedFace
230 
231  const vector d = outsidePoint_ - patch.faceCentres()[seedFacei];
232  const vector& fn = patch.faceNormals()[seedFacei];
233 
234  // Set information to correct orientation
235  patchFaceOrientation& faceInfo = allFaceInfo[seedFacei];
236  faceInfo = orientedSurface::NOFLIP;
237 
238  if ((fn&d) < 0)
239  {
240  faceInfo.flip();
241 
242  if (debug)
243  {
244  Pout<< "Face " << seedFacei << " at "
245  << patch.faceCentres()[seedFacei]
246  << " with normal " << fn
247  << " needs to be flipped." << endl;
248  }
249  }
250  else if (debug)
251  {
252  Pout<< "Face " << seedFacei << " at "
253  << patch.faceCentres()[seedFacei]
254  << " with normal " << fn
255  << " points in positive direction (cos = " << (fn&d)/mag(d)
256  << ")" << endl;
257  }
258 
259 
260  const labelList& fEdges = patch.faceEdges()[seedFacei];
261  forAll(fEdges, fEdgei)
262  {
263  const label edgei = fEdges[fEdgei];
264 
265  patchFaceOrientation& edgeInfo = allEdgeInfo[edgei];
266 
267  if
268  (
269  edgeInfo.updateEdge<int>
270  (
271  mesh_,
272  patch,
273  edgei,
274  seedFacei,
275  faceInfo,
276  tol,
277  dummyTrackData
278  )
279  )
280  {
281  changedEdges.append(edgei);
282  changedInfo.append(edgeInfo);
283  }
284  }
285  }
286 
287 
288  if (returnReduce(changedEdges.size(), sumOp<label>()) == 0)
289  {
290  break;
291  }
292 
293 
294 
295  // Walk
296  PatchEdgeFaceWave
297  <
299  patchFaceOrientation
300  > calc
301  (
302  mesh_,
303  patch,
304  changedEdges,
305  changedInfo,
306  allEdgeInfo,
307  allFaceInfo,
308  returnReduce(patch.nEdges(), sumOp<label>())
309  );
310  }
311 
312 
313  // Push master zone info over to slave (since slave faces never visited)
314  {
315  const polyBoundaryMesh& bm = mesh_.boundaryMesh();
316 
317  labelList neiStatus
318  (
319  mesh_.nFaces()-mesh_.nInternalFaces(),
321  );
322 
323  forAll(faceIndices, i)
324  {
325  const label meshFacei = faceIndices[i];
326  if (!mesh_.isInternalFace(meshFacei))
327  {
328  neiStatus[meshFacei-mesh_.nInternalFaces()] =
329  allFaceInfo[i].flipStatus();
330  }
331  }
332  syncTools::swapBoundaryFaceList(mesh_, neiStatus);
333 
334  forAll(faceIndices, i)
335  {
336  const label meshFacei = faceIndices[i];
337  const label patchi = bm.whichPatch(meshFacei);
338 
339  if
340  (
341  patchi != -1
342  && bm[patchi].coupled()
343  && !isMasterFace[meshFacei]
344  )
345  {
346  // Slave side. Take flipped from neighbour
347  const label bFacei = meshFacei-mesh_.nInternalFaces();
348 
349  if (neiStatus[bFacei] == orientedSurface::NOFLIP)
350  {
351  allFaceInfo[i] = orientedSurface::FLIP;
352  }
353  else if (neiStatus[bFacei] == orientedSurface::FLIP)
354  {
355  allFaceInfo[i] = orientedSurface::NOFLIP;
356  }
357  else
358  {
360  << "Incorrect status for face " << meshFacei
361  << abort(FatalError);
362  }
363  }
364  }
365  }
366 
367 
368  // Convert to flipMap
369 
370  boolList flipMap(faceIndices.size());
371 
372  forAll(allFaceInfo, facei)
373  {
374  if (allFaceInfo[facei] == orientedSurface::NOFLIP)
375  {
376  flipMap[facei] = false;
377  }
378  else if (allFaceInfo[facei] == orientedSurface::FLIP)
379  {
380  flipMap[facei] = true;
381  }
382  else
383  {
385  << "Problem : unvisited face " << facei
386  << " centre:" << mesh_.faceCentres()[faceIndices[facei]]
387  << abort(FatalError);
388  }
389  }
390 
391  return flipMap;
392 }
393 
394 
395 Foam::boolList Foam::zoneGenerators::orient::orientation
396 (
397  const faceZone& fZone
398 ) const
399 {
400  if (magSqr(normal_) > 0)
401  {
402  return normalOrientation(fZone);
403  }
404  else
405  {
406  return pointOrientation(fZone);
407  }
408 }
409 
410 
411 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
412 
414 (
415  const word& name,
416  const polyMesh& mesh,
417  const dictionary& dict
418 )
419 :
421  zoneGenerator_(zoneGenerator::New(mesh, dict)),
422  normal_(dict.lookupOrDefault<vector>("normal", Zero)),
423  outsidePoint_
424  (
425  !dict.found("normal")
426  ? dict.lookup<vector>("outsidePoint")
427  : Zero
428  )
429 {}
430 
431 
432 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
433 
435 {}
436 
437 
438 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
439 
441 {
442  zoneSet zs(zoneGenerator_->generate());
443  const faceZone& fZone = zs.fZone();
444 
445  return zoneSet
446  (
447  new faceZone
448  (
449  zoneName_,
450  fZone,
451  orientation(fZone),
452  mesh_.faceZones(),
453  moveUpdate_,
454  true
455  )
456  );
457 }
458 
459 
460 // ************************************************************************* //
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
Macros for easy insertion into run-time selection tables.
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Named list of face indices representing a sub-set of the mesh faces.
Definition: faceZone.H:66
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const vectorField & faceAreas() const
static void swapBoundaryFaceList(const polyMesh &mesh, UList< T > &l)
Swap coupled boundary face values.
Definition: syncTools.H:436
static PackedBoolList getMasterFaces(const polyMesh &)
Get per face whether it is uncoupled or a master of a.
Definition: syncTools.C:153
static void syncEdgeList(const polyMesh &, List< T > &, const CombineOp &cop, const T &nullValue, const TransformOp &top)
Synchronise values on all mesh edges.
A class for handling words, derived from string.
Definition: word.H:62
Abstract base class for all zoneGenerators, providing runtime selection.
Definition: zoneGenerator.H:57
const polyMesh & mesh_
Reference to the polyMesh.
Definition: zoneGenerator.H:72
A zoneGenerator which looks-up and returns a zoneSet containing point, and/or cell and/or faces zones...
Definition: lookup.H:139
A zoneGenerator which sets the face orientation flipMap.
virtual zoneSet generate() const
Generate and return the zoneSet.
orient(const word &name, const polyMesh &mesh, const dictionary &dict)
Construct from dictionary.
Zone container returned by zoneGenerator::generate.
Definition: zoneSet.H:94
const faceZone & fZone() const
Return a reference to the faceZone if allocated.
Definition: zoneSetI.H:230
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
label patchi
defineTypeNameAndDebug(cylinderHeadPoints, 0)
addToRunTimeSelectionTable(zoneGenerator, cylinderHeadPoints, dictionary)
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static const zero Zero
Definition: zero.H:97
List< label > labelList
A List of labels.
Definition: labelList.H:56
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:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
void mag(LagrangianPatchField< scalar > &f, const LagrangianPatchField< Type > &f1)
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Field< vector > vectorField
Specialisation of Field<T> for vector.
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
error FatalError
void magSqr(LagrangianPatchField< scalar > &f, const LagrangianPatchField< Type > &f1)
static const label labelMax
Definition: label.H:62
PrimitivePatch< IndirectList< face >, const pointField & > indirectPrimitivePatch
Foam::indirectPrimitivePatch.
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
static const char nl
Definition: Ostream.H:267
dictionary dict