regionToFace.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) 2012-2017 OpenFOAM Foundation
9  Copyright (C) 2018-2024 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 "regionToFace.H"
30 #include "polyMesh.H"
31 #include "faceSet.H"
32 #include "mappedPatchBase.H"
33 #include "indirectPrimitivePatch.H"
34 #include "PatchTools.H"
36 #include "PatchEdgeFaceWave.H"
37 #include "edgeTopoDistanceData.H"
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43  defineTypeNameAndDebug(regionToFace, 0);
44  addToRunTimeSelectionTable(topoSetSource, regionToFace, word);
45  addToRunTimeSelectionTable(topoSetSource, regionToFace, istream);
46  addToRunTimeSelectionTable(topoSetFaceSource, regionToFace, word);
47  addToRunTimeSelectionTable(topoSetFaceSource, regionToFace, istream);
49  (
50  topoSetFaceSource,
51  regionToFace,
52  word,
53  region
54  );
56  (
57  topoSetFaceSource,
58  regionToFace,
59  istream,
60  region
61  );
62 }
63 
64 
65 Foam::topoSetSource::addToUsageTable Foam::regionToFace::usage_
66 (
67  regionToFace::typeName,
68  "\n Usage: regionToFace <faceSet> (x y z)\n\n"
69  " Select all faces in the connected region of the faceSet"
70  " starting from the point.\n"
71 );
72 
73 
74 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
75 
76 void Foam::regionToFace::markZone
77 (
79  const label proci,
80  const label facei,
81  const label zonei,
82  labelList& faceZone
83 ) const
84 {
85  // Data on all edges and faces
86  List<edgeTopoDistanceData<label>> allEdgeInfo(patch.nEdges());
87  List<edgeTopoDistanceData<label>> allFaceInfo(patch.size());
88 
89  DynamicList<label> changedEdges;
90  DynamicList<edgeTopoDistanceData<label>> changedInfo;
91 
92  if (Pstream::myProcNo() == proci)
93  {
94  const labelList& fEdges = patch.faceEdges()[facei];
95  for (const label edgei : fEdges)
96  {
97  changedEdges.append(edgei);
98  changedInfo.append
99  (
100  edgeTopoDistanceData<label>
101  (
102  0, // distance
103  zonei
104  )
105  );
106  }
107  }
108 
109  // Walk
110  PatchEdgeFaceWave
111  <
113  edgeTopoDistanceData<label>
114  > calc
115  (
116  mesh_,
117  patch,
118  changedEdges,
119  changedInfo,
120  allEdgeInfo,
121  allFaceInfo,
122  returnReduce(patch.nEdges(), sumOp<label>())
123  );
124 
125  forAll(allFaceInfo, facei)
126  {
127  if
128  (
129  allFaceInfo[facei].valid(calc.data())
130  && allFaceInfo[facei].data() == zonei
131  )
132  {
133  faceZone[facei] = zonei;
134  }
135  }
136 }
137 
138 
139 void Foam::regionToFace::combine
140 (
141  topoSet& set,
142  const bool add,
143  const labelUList& ids
144 ) const
145 {
146  if (verbose_)
147  {
148  Info<< " Loading subset " << setName_
149  << " to delimit search region." << nl;
150  }
151 
153  (
154  IndirectList<face>(mesh_.faces(), ids),
155  mesh_.points()
156  );
157 
159  (
160  pointIndexHit(false, Zero, -1),
161  Tuple2<scalar, label>
162  (
163  sqr(GREAT),
165  )
166  );
167 
168  forAll(patch, i)
169  {
170  const point& fc = patch.faceCentres()[i];
171  scalar d2 = magSqr(fc-nearPoint_);
172 
173  if (!ni.first().hit() || d2 < ni.second().first())
174  {
175  ni.second().first() = d2;
176  ni.first().hitPoint(fc);
177  ni.first().setIndex(i);
178  }
179  }
180 
181  // Globally reduce
182  Pstream::combineReduce(ni, mappedPatchBase::nearestEqOp());
183 
184  if (verbose_)
185  {
186  Info<< " Found nearest face at " << ni.first().point()
187  << " on processor " << ni.second().second()
188  << " face " << ni.first().index()
189  << " distance " << Foam::sqrt(ni.second().first()) << endl;
190  }
191 
192  labelList faceRegion(patch.size(), -1);
193  markZone
194  (
195  patch,
196  ni.second().second(), // proci
197  ni.first().index(), // start face
198  0, // currentZone
199  faceRegion
200  );
201 
202  forAll(faceRegion, facei)
203  {
204  if (faceRegion[facei] == 0)
205  {
206  addOrDelete(set, patch.addressing()[facei], add);
207  }
208  }
209 }
210 
211 
212 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
213 
215 (
216  const polyMesh& mesh,
217  const word& setName,
218  const point& nearPoint
219 )
220 :
221  topoSetFaceSource(mesh),
222  setName_(setName),
223  isZone_(false),
224  nearPoint_(nearPoint)
225 {}
226 
227 
229 (
230  const polyMesh& mesh,
231  const dictionary& dict
232 )
233 :
235  setName_(),
236  isZone_(false),
237  nearPoint_(dict.get<point>("nearPoint"))
238 {
239  // A single "set" or "zone" only
240  if (!dict.readIfPresent("set", setName_))
241  {
242  // Mandatory entry
243  dict.readEntry("zone", setName_);
244  isZone_ = true;
245  }
246 }
247 
248 
250 (
251  const polyMesh& mesh,
252  Istream& is
253 )
254 :
255  topoSetFaceSource(mesh),
256  setName_(checkIs(is)),
257  isZone_(false),
258  nearPoint_(checkIs(is))
259 {}
260 
261 
262 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
263 
265 (
266  const topoSetSource::setAction action,
267  topoSet& set
268 ) const
269 {
270  if (action == topoSetSource::ADD || action == topoSetSource::NEW)
271  {
272  if (verbose_)
273  {
274  Info<< " Adding all faces of connected region of "
275  << (isZone_ ? "zone " : "set ")
276  << setName_ << " starting from point " << nearPoint_
277  << " ..." << endl;
278  }
279 
280  if (isZone_)
281  {
282  combine(set, true, mesh_.faceZones()[setName_].addressing());
283  }
284  else
285  {
286  faceSet subSet(mesh_, setName_, IOobject::NO_REGISTER);
287  combine(set, true, subSet.sortedToc());
288  }
289  }
290  else if (action == topoSetSource::SUBTRACT)
291  {
292  if (verbose_)
293  {
294  Info<< " Removing all cells of connected region of "
295  << (isZone_ ? "zone " : "set ")
296  << setName_ << " starting from point " << nearPoint_
297  << " ..." << endl;
298  }
299 
300  if (isZone_)
301  {
302  combine(set, false, mesh_.faceZones()[setName_].addressing());
303  }
304  else
305  {
306  faceSet subSet(mesh_, setName_, IOobject::NO_REGISTER);
307  combine(set, false, subSet.sortedToc());
308  }
309  }
310 }
311 
312 
313 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
dictionary dict
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual void applyToSet(const topoSetSource::setAction action, topoSet &) const
Apply specified action to the topoSet.
Definition: regionToFace.C:258
Create a new set and ADD elements to it.
Add elements to current set.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
dimensionedScalar sqrt(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
PointIndexHit< point > pointIndexHit
A PointIndexHit with a 3D point.
Definition: pointIndexHit.H:58
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Can be negative if the process i...
Definition: UPstream.H:1086
PrimitivePatch< IndirectList< face >, const pointField & > indirectPrimitivePatch
A PrimitivePatch with an IndirectList for the faces, const reference for the point field...
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
Macros for easy insertion into run-time selection tables.
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
addNamedToRunTimeSelectionTable(topoSetCellSource, badQualityToCell, word, badQuality)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
AccessType combine(const UList< T > &lists, AccessOp aop=accessOp< T >())
Combines sub-lists into a single list.
Definition: ListListOps.C:62
dynamicFvMesh & mesh
The topoSetFaceSource is a intermediate class for handling topoSet sources for selecting faces...
static void combineReduce(T &value, const CombineOp &cop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce) applying cop to inplace combine value from different processors...
setAction
Enumeration defining various actions.
const polyMesh & mesh_
Reference to the mesh.
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
defineTypeNameAndDebug(combustionModel, 0)
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
Subtract elements from current set.
regionToFace(const polyMesh &mesh, const word &setName, const point &nearPoint)
Construct from components.
Definition: regionToFace.C:208
Tuple2< pointIndexHit, Tuple2< scalar, label > > nearInfo
Helper class for finding nearest.
vector point
Point is a vector.
Definition: point.H:37
Class with constructor to add usage string to table.
const std::string patch
OpenFOAM patch number as a std::string.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
List< label > labelList
A List of labels.
Definition: List.H:62
List< wallPoints > allFaceInfo(mesh_.nFaces())
Do not request registration (bool: false)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127