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 " << current.count()
148  << " by " << updates.count() << endl;
149  }
150 
151  // Add to current set for the next loop
152  current |= updates;
153  }
154  else
155  {
156  // Restrict to cells already in the current set
157  updates &= current;
158 
159  if (verbose_)
160  {
161  Info<< " Shrink " << current.count()
162  << " by " << updates.count() << endl;
163  }
164 
165  // Remove from current set for the next loop
166  current -= updates;
167  }
168 
169  // Could have early exit, but needs to be parallel-synchronized
170  // if (returnReduceAnd(updates.none()))
171  // {
172  // break;
173  // }
174 
175  addOrDelete(set, updates, add);
176  }
177 }
178 
179 
180 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
181 
183 (
184  const polyMesh& mesh,
185  const label nsteps
186 )
187 :
189  steps_(nsteps)
190 {}
191 
192 
194 (
195  const polyMesh& mesh,
196  const dictionary& dict
197 )
198 :
199  haloToCell(mesh, dict.getOrDefault<label>("steps", 1))
200 {}
201 
202 
204 (
205  const polyMesh& mesh,
206  Istream& is
207 )
208 :
209  haloToCell(mesh, 1)
210 {}
211 
212 
213 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
215 Foam::label Foam::haloToCell::steps() const noexcept
216 {
217  return steps_;
218 }
219 
220 
221 Foam::label Foam::haloToCell::steps(const label nsteps) noexcept
222 {
223  label old(steps_);
224  steps_ = nsteps;
225  return old;
226 }
227 
228 
230 (
231  const topoSetSource::setAction action,
232  topoSet& set
233 ) const
234 {
235  if (action == topoSetSource::NEW)
236  {
237  if (verbose_)
238  {
239  Info<< " action=new option is not available for haloToCell" << nl
240  << " Cannot create new of halo (needs a starting set)"
241  << endl;
242  }
243 
244  set.clear();
245  }
246  else if (action == topoSetSource::ADD)
247  {
248  if (verbose_)
249  {
250  Info<< " Adding halo cells to the current set, using "
251  << steps_ << " step ..." << endl;
252  }
253 
254  combine(set, true);
255  }
256  else if (action == topoSetSource::SUBTRACT)
257  {
258  if (verbose_)
259  {
260  Info<< " Removing cells on the perimeter of current set, using "
261  << steps_ << " step ..." << endl;
262  }
263 
264  combine(set, false);
265  }
266 }
267 
268 
269 // ************************************************************************* //
dictionary dict
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:120
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1110
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:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
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.
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:1104
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:223
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:208
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:59
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
haloToCell(const polyMesh &mesh, const label nsteps=1)
Construct from components.
Definition: haloToCell.C:176
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:73
List< label > labelList
A List of labels.
Definition: List.H:62
List< cell > cellList
A List of cells.
Definition: cellListFwd.H:41
Namespace for OpenFOAM.