plane_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) 2011-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 "plane_zoneGenerator.H"
27 #include "polyMesh.H"
29 #include "PatchTools.H"
30 #include "syncTools.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  namespace zoneGenerators
38  {
41  (
43  plane,
45  );
46  }
47 }
48 
51 {
52  "all",
53  "closest"
54 };
55 
56 
57 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
58 
60 (
61  const word& name,
62  const polyMesh& mesh,
63  const dictionary& dict
64 )
65 :
67  point_(dict.lookup<vector>("point", dimLength)),
68  normal_(dict.lookup<vector>("normal", dimless)),
69  include_(includeNames.lookupOrDefault("include", dict, include::all))
70 {}
71 
72 
73 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
74 
76 {}
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
82 {
83  // Mark all cells with centres above the plane
84  boolList cellIsAbovePlane(mesh_.nCells());
85  forAll(mesh_.cells(), celli)
86  {
87  cellIsAbovePlane[celli] =
88  ((mesh_.cellCentres()[celli] - point_) & normal_) > 0;
89  }
90 
91  // Mark all coupled neighbour cells with centres above the plane
92  boolList bFaceNbrCellIsAbovePlane(mesh_.nFaces() - mesh_.nInternalFaces());
93  {
94  vectorField bFaceNbrCellCentres;
96  (
97  mesh_,
98  mesh_.cellCentres(),
99  bFaceNbrCellCentres
100  );
101  forAll(bFaceNbrCellIsAbovePlane, bFacei)
102  {
103  bFaceNbrCellIsAbovePlane[bFacei] =
104  ((bFaceNbrCellCentres[bFacei] - point_) & normal_) > 0;
105  }
106  }
107 
108  // Mark all faces that sit between cells above and below the plane
109  boolList faceIsOnPlane(mesh_.nFaces(), false);
110  forAll(mesh_.faceNeighbour(), facei)
111  {
112  faceIsOnPlane[facei] =
113  cellIsAbovePlane[mesh_.faceOwner()[facei]]
114  != cellIsAbovePlane[mesh_.faceNeighbour()[facei]];
115  }
116  forAll(mesh_.boundaryMesh(), patchi)
117  {
118  const polyPatch& patch = mesh_.boundaryMesh()[patchi];
119 
120  if (!patch.coupled()) continue;
121 
122  forAll(patch, patchFacei)
123  {
124  const label facei = patch.start() + patchFacei;
125  faceIsOnPlane[facei] =
126  cellIsAbovePlane[mesh_.faceOwner()[facei]]
127  != bFaceNbrCellIsAbovePlane[facei - mesh_.nInternalFaces()];
128  }
129  }
130 
131  // Ensure consistency across couplings
132  syncTools::syncFaceList(mesh_, faceIsOnPlane, orEqOp<bool>());
133 
134  // Convert marked faces to a list of indices
135  labelList faceIndices(findIndices(faceIsOnPlane, true));
136 
137  // If constructing a single contiguous set, remove all faces except those
138  // connected to the contiguous region closest to the specified point
139  if (include_ == include::closest)
140  {
141  // Step 1: Get locally contiguous regions for the new face set and the
142  // total number of regions across all processors.
143  labelList newSetFaceRegions(faceIndices.size(), -1);
144  label nRegions = -1;
145  {
146  // Create a patch of the set faces
147  const uindirectPrimitivePatch newSetPatch
148  (
149  UIndirectList<face>(mesh_.faces(), faceIndices),
150  mesh_.points()
151  );
152 
153  // Get the region ID-s and store the total number of regions on
154  // each processor
155  labelList procNRegions(Pstream::nProcs(), -1);
156  procNRegions[Pstream::myProcNo()] =
158  (
159  newSetPatch,
160  boolList(newSetPatch.nEdges(), false),
161  newSetFaceRegions
162  );
163  Pstream::gatherList(procNRegions);
164  Pstream::scatterList(procNRegions);
165 
166  // Cumulative sum the number of regions on each processor to get an
167  // offset which makes the local region ID-s globally unique
168  labelList procRegionOffset(Pstream::nProcs(), 0);
169  for (label proci = 1; proci < Pstream::nProcs(); proci++)
170  {
171  procRegionOffset[proci] +=
172  procRegionOffset[proci - 1]
173  + procNRegions[proci - 1];
174  }
175 
176  // Apply the offset
177  forAll(faceIndices, fi)
178  {
179  newSetFaceRegions[fi] +=
180  procRegionOffset[Pstream::myProcNo()];
181  }
182 
183  // Store the total number of regions across all processors
184  nRegions = procRegionOffset.last() + procNRegions.last();
185  }
186 
187  // Step 2: Create a region map which combines regions which are
188  // connected across coupled interfaces
189  labelList regionMap(identityMap(nRegions));
190  {
191  // Put region labels on connected boundary edges and synchronise to
192  // create a list of all regions connected to a given edge
193  labelListList meshEdgeRegions(mesh_.nEdges(), labelList());
194  forAll(faceIndices, fi)
195  {
196  const label facei = faceIndices[fi];
197  const label regioni = newSetFaceRegions[fi];
198  forAll(mesh_.faceEdges()[facei], faceEdgei)
199  {
200  const label edgei = mesh_.faceEdges()[facei][faceEdgei];
201  meshEdgeRegions[edgei] = labelList(1, regioni);
202  }
203  }
205  (
206  mesh_,
207  meshEdgeRegions,
209  labelList()
210  );
211 
212  // Combine edge regions to create a list of what regions a given
213  // region is connected to
214  List<labelHashSet> regionRegions(nRegions);
215  forAll(faceIndices, fi)
216  {
217  const label facei = faceIndices[fi];
218  const label regioni = newSetFaceRegions[fi];
219  forAll(mesh_.faceEdges()[facei], faceEdgei)
220  {
221  const label edgei = mesh_.faceEdges()[facei][faceEdgei];
222  forAll(meshEdgeRegions[edgei], edgeRegioni)
223  {
224  if (meshEdgeRegions[edgei][edgeRegioni] != regioni)
225  {
226  regionRegions[regioni].insert
227  (
228  meshEdgeRegions[edgei][edgeRegioni]
229  );
230  }
231  }
232  }
233  }
235  Pstream::listCombineScatter(regionRegions);
236 
237  // Collapse the region connections into a map between each region
238  // and the lowest numbered region that it connects to
239  forAll(regionRegions, regioni)
240  {
241  forAllConstIter(labelHashSet, regionRegions[regioni], iter)
242  {
243  regionMap[iter.key()] =
244  min(regionMap[iter.key()], regionMap[regioni]);
245  }
246  }
247  }
248 
249  // Step 3: Combine connected regions
250  labelList regionNFaces;
251  {
252  // Remove duplicates from the region map
253  label regioni0 = 0;
254  forAll(regionMap, regioni)
255  {
256  if (regionMap[regioni] > regioni0)
257  {
258  regioni0++;
259  regionMap[regioni] = regioni0;
260  }
261  }
262 
263  // Recompute the number of regions
264  nRegions = regioni0 + 1;
265 
266  // Renumber the face region ID-s
267  newSetFaceRegions =
268  IndirectList<label>(regionMap, newSetFaceRegions);
269 
270  // Report the final number and size of the regions
271  regionNFaces = labelList(nRegions, 0);
272  forAll(faceIndices, fi)
273  {
274  regionNFaces[newSetFaceRegions[fi]] ++;
275  }
277  Pstream::listCombineScatter(regionNFaces);
278  Info<< " Found " << nRegions << " contiguous regions with "
279  << regionNFaces << " faces" << endl;
280  }
281 
282  // Step 4: Choose the closest region to output
283  label selectedRegioni = -1;
284  {
285  // Compute the region centres
286  scalarField regionMagAreas(nRegions, 0);
287  pointField regionCentres(nRegions, Zero);
288  forAll(faceIndices, fi)
289  {
290  const label facei = faceIndices[fi];
291  const label regioni = newSetFaceRegions[fi];
292 
293  const vector& a = mesh_.faceAreas()[facei];
294  const point& c = mesh_.faceCentres()[facei];
295 
296  regionMagAreas[regioni] += mag(a);
297  regionCentres[regioni] += mag(a)*c;
298  }
299  Pstream::listCombineGather(regionMagAreas, plusEqOp<scalar>());
301  Pstream::listCombineScatter(regionMagAreas);
302  Pstream::listCombineScatter(regionCentres);
303  regionCentres /= regionMagAreas;
304 
305  // Find the region centroid closest to the reference point
306  selectedRegioni = returnReduce
307  (
308  findMin(mag(regionCentres - point_)()),
309  minOp<label>()
310  );
311 
312  // Report the selection
313  Info<< " Selecting region " << selectedRegioni << " with "
314  << regionNFaces[selectedRegioni]
315  << " faces as the closest to point " << point_ << endl;
316  }
317 
318  // Step 5: Remove any faces from the set list not in the selected region
319  {
320  // Remove faces from the list by shuffling up and resizing
321  label fi0 = 0;
322  forAll(faceIndices, fi)
323  {
324  faceIndices[fi0] = faceIndices[fi];
325 
326  if (newSetFaceRegions[fi] == selectedRegioni)
327  {
328  fi0++;
329  }
330  }
331  faceIndices.resize(fi0);
332  }
333  }
334 
335  boolList flipMap(faceIndices.size());
336 
337  // Construct the flipMap
338  forAll(faceIndices, fi)
339  {
340  flipMap[fi] = cellIsAbovePlane[mesh_.faceOwner()[faceIndices[fi]]];
341  }
342 
343  return zoneSet
344  (
345  new faceZone
346  (
347  zoneName_,
348  faceIndices,
349  flipMap,
350  mesh_.faceZones(),
351  moveUpdate_,
352  true
353  )
354  );
355 }
356 
357 
358 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
Macros for easy insertion into run-time selection tables.
A List with indirect addressing.
Definition: IndirectList.H:105
void resize(const label)
Alias for setSize(const label)
Definition: ListI.H:138
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:55
static label markZones(const PrimitivePatch< FaceList, PointField > &, const BoolListType &borderEdge, labelList &faceZone)
Size and fills faceZone with zone of face.
A list of faces which address into the list of points.
label nEdges() const
Return number of edges in patch.
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
A List with indirect addressing.
Definition: UIndirectList.H:60
T & last()
Return the last element of the list.
Definition: UListI.H:128
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
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
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:70
static void syncEdgeList(const polyMesh &, List< T > &, const CombineOp &cop, const T &nullValue, const TransformOp &top)
Synchronise values on all mesh edges.
static void syncFaceList(const polyMesh &mesh, UList< T > &l, const CombineOp &cop)
Synchronise values on all mesh faces.
Definition: syncTools.H:387
static void swapBoundaryCellPositions(const polyMesh &mesh, const UList< point > &cellData, List< point > &neighbourCellData)
Swap to obtain neighbour cell positions for all boundary faces.
Definition: syncTools.C:31
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
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 constructs a faceZone from a set of patches.
A zoneGenerator which selects faces based on the adjacent cell centres spanning a given plane....
virtual zoneSet generate() const
Generate and return the zoneSet.
static const NamedEnum< include, 2 > includeNames
Include option names.
include
Enumeration for what to include.
plane(const word &name, const polyMesh &mesh, const dictionary &dict)
Construct from dictionary.
A zoneGenerator which converts the point, cell and face zones from a list of zoneGenerators into a po...
Zone container returned by zoneGenerator::generate.
Definition: zoneSet.H:94
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
label patchi
const dimensionedScalar c
Speed of light in a vacuum.
defineTypeNameAndDebug(cylinderHeadPoints, 0)
addToRunTimeSelectionTable(zoneGenerator, cylinderHeadPoints, dictionary)
Namespace for OpenFOAM.
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
labelList findIndices(const ListType &, typename ListType::const_reference, const label start=0)
Find all occurrences of given element. Linear search.
const dimensionSet dimless
messageStream Info
const dimensionSet dimLength
List< bool > boolList
Bool container classes.
Definition: boolList.H:50
void mag(LagrangianPatchField< scalar > &f, const LagrangianPatchField< Type > &f1)
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
label findMin(const ListType &, const label start=0)
Find index of min element (and less than given element).
labelList identityMap(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
dictionary dict