haloToCell.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) 2019-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "haloToCell.H"
29 #include "polyMesh.H"
30 #include "cellSet.H"
31 #include "topoBitSet.H"
32 #include "syncTools.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  defineTypeNameAndDebug(haloToCell, 0);
40  addToRunTimeSelectionTable(topoSetSource, haloToCell, word);
41  addToRunTimeSelectionTable(topoSetSource, haloToCell, istream);
42  addToRunTimeSelectionTable(topoSetCellSource, haloToCell, word);
43  addToRunTimeSelectionTable(topoSetCellSource, haloToCell, istream);
45  (
46  topoSetCellSource,
47  haloToCell,
48  word,
49  halo
50  );
52  (
53  topoSetCellSource,
54  haloToCell,
55  istream,
56  halo
57  );
58 }
59 
60 
61 Foam::topoSetSource::addToUsageTable Foam::haloToCell::usage_
62 (
63  haloToCell::typeName,
64  "\n Usage: haloToCell\n\n"
65  " Select halo cells\n\n"
66 );
67 
68 
69 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
70 
71 void Foam::haloToCell::combine(topoSet& set, const bool add) const
72 {
73  if (steps_ < 1)
74  {
75  return; // Nothing to do
76  }
77 
78  const cellList& cells = mesh_.cells();
79  const labelList& faceOwn = mesh_.faceOwner();
80  const labelList& faceNei = mesh_.faceNeighbour();
81 
82 
83  // The starting set of cells
84  bitSet current(cells.size());
85 
86  if (isA<topoBitSet>(set))
87  {
88  current |= refCast<const topoBitSet>(set).addressing();
89  }
90  else
91  {
92  for (const label celli : set)
93  {
94  current.set(celli);
95  }
96  }
97 
98  // The perimeter faces of the cell set
99  bitSet outsideFaces(mesh_.nFaces());
100 
101  bitSet updates(cells.size());
102 
103  for (label stepi = 0; stepi < steps_; ++stepi)
104  {
105  // Mark up perimeter faces. Each mesh face is attached exactly
106  // (0,1,2) times to a cell in the set. Using flip() each time means
107  // the only 'on' bits correspond to faces that are attached once to
108  // the cell set - ie, faces on the perimeter of the set.
109 
110  outsideFaces.reset();
111  for (const label celli : current)
112  {
113  for (const label facei : cells[celli])
114  {
115  outsideFaces.flip(facei);
116  }
117  }
118 
119  // Use xor to eliminate perimeter faces that are actually attached
120  // on both sides of the interface.
121 
123  (
124  mesh_,
125  outsideFaces,
126  bitXorEqOp<unsigned int>()
127  );
128 
129  // Select all cells attached to the perimeter faces.
130  updates.reset();
131  for (const label facei : outsideFaces)
132  {
133  updates.set(faceOwn[facei]);
134  if (mesh_.isInternalFace(facei))
135  {
136  updates.set(faceNei[facei]);
137  }
138  }
139 
140  if (add)
141  {
142  // Restrict to cells not already in the current set
143  updates -= current;
144 
145  if (verbose_)
146  {
147  Info<< " Grow "
148  << returnReduce(current.count(), sumOp<label>()) << " by "
149  << returnReduce(updates.count(), sumOp<label>()) << endl;
150  }
151 
152  // Add to current set for the next loop
153  current |= updates;
154  }
155  else
156  {
157  // Restrict to cells already in the current set
158  updates &= current;
159 
160  if (verbose_)
161  {
162  Info<< " Shrink "
163  << returnReduce(current.count(), sumOp<label>()) << " by "
164  << returnReduce(updates.count(), sumOp<label>()) << endl;
165  }
166 
167  // Remove from current set for the next loop
168  current -= updates;
169  }
170 
171  // Could have early exit, but needs to be parallel-synchronized
172  // if (returnReduceAnd(updates.none()))
173  // {
174  // break;
175  // }
176 
177  addOrDelete(set, updates, add);
178  }
179 }
180 
181 
182 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
183 
185 (
186  const polyMesh& mesh,
187  const label nsteps
188 )
189 :
191  steps_(nsteps)
192 {}
193 
194 
196 (
197  const polyMesh& mesh,
198  const dictionary& dict
199 )
200 :
201  haloToCell(mesh, dict.getOrDefault<label>("steps", 1))
202 {}
203 
204 
206 (
207  const polyMesh& mesh,
208  Istream& is
209 )
210 :
211  haloToCell(mesh, 1)
212 {}
213 
214 
215 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
217 Foam::label Foam::haloToCell::steps() const noexcept
218 {
219  return steps_;
220 }
221 
222 
223 Foam::label Foam::haloToCell::steps(const label nsteps) noexcept
224 {
225  label old(steps_);
226  steps_ = nsteps;
227  return old;
228 }
229 
230 
232 (
233  const topoSetSource::setAction action,
234  topoSet& set
235 ) const
236 {
237  if (action == topoSetSource::NEW)
238  {
239  if (verbose_)
240  {
241  Info<< " action=new option is not available for haloToCell" << nl
242  << " Cannot create new of halo (needs a starting set)"
243  << endl;
244  }
245 
246  set.clear();
247  }
248  else if (action == topoSetSource::ADD)
249  {
250  if (verbose_)
251  {
252  Info<< " Adding halo cells to the current set, using "
253  << steps_ << " step ..." << endl;
254  }
255 
256  combine(set, true);
257  }
258  else if (action == topoSetSource::SUBTRACT)
259  {
260  if (verbose_)
261  {
262  Info<< " Removing cells on the perimeter of current set, using "
263  << steps_ << " step ..." << endl;
264  }
265 
266  combine(set, false);
267  }
268 }
269 
270 
271 // ************************************************************************* //
dictionary dict
List< cell > cellList
List of cell.
Definition: cellListFwd.H:39
bool verbose_
Output verbosity (default: true)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1122
Create a new set and ADD elements to it.
Add elements to current set.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void syncFaceList(const polyMesh &mesh, UList< T > &faceValues, const CombineOp &cop)
Synchronize values on all mesh faces.
Definition: syncTools.H:432
A topoSetCellSource to select cells attached to the outside of this cellSet, and add into/remove from...
Definition: haloToCell.H:144
const cellList & cells() const
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells...
label nFaces() const noexcept
Number of mesh faces.
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.
void addOrDelete(topoSet &set, const label id, const bool add) const
Add or delete id from set. Add when &#39;add&#39; is true.
Macros for easy insertion into run-time selection tables.
bool isInternalFace(const label faceIndex) const noexcept
Return true if given face label is internal to the mesh.
addNamedToRunTimeSelectionTable(topoSetCellSource, badQualityToCell, word, badQuality)
AccessType combine(const UList< T > &lists, AccessOp aop=accessOp< T >())
Combines sub-lists into a single list.
Definition: ListListOps.C:62
dynamicFvMesh & mesh
const cellShapeList & cells
setAction
Enumeration defining various actions.
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1116
const polyMesh & mesh_
Reference to the mesh.
const direction noexcept
Definition: Scalar.H:258
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
Definition: haloToCell.C:225
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
defineTypeNameAndDebug(combustionModel, 0)
label steps() const noexcept
The number of steps to grow/shrink.
Definition: haloToCell.C:210
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
haloToCell(const polyMesh &mesh, const label nsteps=1)
Construct from components.
Definition: haloToCell.C:178
Subtract elements from current set.
Class with constructor to add usage string to table.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
List< label > labelList
A List of labels.
Definition: List.H:62
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)