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-2024 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  const auto* topoBitsPtr = isA<topoBitSet>(set);
87 
88  if (topoBitsPtr)
89  {
90  current |= topoBitsPtr->addressing();
91  }
92  else
93  {
94  for (const label celli : set)
95  {
96  current.set(celli);
97  }
98  }
99 
100  // The perimeter faces of the cell set
101  bitSet outsideFaces(mesh_.nFaces());
102 
103  bitSet updates(cells.size());
104 
105  for (label stepi = 0; stepi < steps_; ++stepi)
106  {
107  // Mark up perimeter faces. Each mesh face is attached exactly
108  // (0,1,2) times to a cell in the set. Using flip() each time means
109  // the only 'on' bits correspond to faces that are attached once to
110  // the cell set - ie, faces on the perimeter of the set.
111 
112  outsideFaces.reset();
113  for (const label celli : current)
114  {
115  for (const label facei : cells[celli])
116  {
117  outsideFaces.flip(facei);
118  }
119  }
120 
121  // Use xor to eliminate perimeter faces that are actually attached
122  // on both sides of the interface.
123 
125  (
126  mesh_,
127  outsideFaces,
128  bitXorEqOp<unsigned int>()
129  );
130 
131  // Select all cells attached to the perimeter faces.
132  updates.reset();
133  for (const label facei : outsideFaces)
134  {
135  updates.set(faceOwn[facei]);
136  if (mesh_.isInternalFace(facei))
137  {
138  updates.set(faceNei[facei]);
139  }
140  }
141 
142  if (add)
143  {
144  // Restrict to cells not already in the current set
145  updates -= current;
146 
147  if (verbose_)
148  {
149  Info<< " Grow "
150  << returnReduce(current.count(), sumOp<label>()) << " by "
151  << returnReduce(updates.count(), sumOp<label>()) << endl;
152  }
153 
154  // Add to current set for the next loop
155  current |= updates;
156  }
157  else
158  {
159  // Restrict to cells already in the current set
160  updates &= current;
161 
162  if (verbose_)
163  {
164  Info<< " Shrink "
165  << returnReduce(current.count(), sumOp<label>()) << " by "
166  << returnReduce(updates.count(), sumOp<label>()) << endl;
167  }
168 
169  // Remove from current set for the next loop
170  current -= updates;
171  }
172 
173  // Could have early exit, but needs to be parallel-synchronized
174  // if (returnReduceAnd(updates.none()))
175  // {
176  // break;
177  // }
178 
179  addOrDelete(set, updates, add);
180  }
181 }
182 
183 
184 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
185 
187 (
188  const polyMesh& mesh,
189  const label nsteps
190 )
191 :
193  steps_(nsteps)
194 {}
195 
196 
198 (
199  const polyMesh& mesh,
200  const dictionary& dict
201 )
202 :
204  steps_(dict.getOrDefault<label>("steps", 1))
205 {}
206 
207 
209 (
210  const polyMesh& mesh,
211  Istream& is
212 )
213 :
214  haloToCell(mesh, 1)
215 {}
216 
217 
218 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
220 Foam::label Foam::haloToCell::steps() const noexcept
221 {
222  return steps_;
223 }
224 
225 
226 Foam::label Foam::haloToCell::steps(const label nsteps) noexcept
227 {
228  label old(steps_);
229  steps_ = nsteps;
230  return old;
231 }
232 
233 
235 (
236  const topoSetSource::setAction action,
237  topoSet& set
238 ) const
239 {
240  if (action == topoSetSource::NEW)
241  {
242  if (verbose_)
243  {
244  Info<< " action=new option is not available for haloToCell" << nl
245  << " Cannot create new of halo (needs a starting set)"
246  << endl;
247  }
248 
249  set.clear();
250  }
251  else if (action == topoSetSource::ADD)
252  {
253  if (verbose_)
254  {
255  Info<< " Adding halo cells to the current set, using "
256  << steps_ << " step ..." << endl;
257  }
258 
259  combine(set, true);
260  }
261  else if (action == topoSetSource::SUBTRACT)
262  {
263  if (verbose_)
264  {
265  Info<< " Removing cells on the perimeter of current set, using "
266  << steps_ << " step ..." << endl;
267  }
268 
269  combine(set, false);
270  }
271 }
272 
273 
274 // ************************************************************************* //
static void syncFaceList(const polyMesh &mesh, UList< T > &faceValues, const CombineOp &cop, const bool parRun=UPstream::parRun())
Synchronize values on all mesh faces.
Definition: syncTools.H:465
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:1107
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:529
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:1101
T returnReduce(const T &value, BinaryOp bop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
const polyMesh & mesh_
Reference to the mesh.
const direction noexcept
Definition: scalarImpl.H:255
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
Definition: haloToCell.C:228
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:213
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:180
Subtract elements from current set.
Class with constructor to add usage string to table.
messageStream Info
Information stream (stdout output on master, null elsewhere)
std::enable_if_t< std::is_same_v< bool, TypeT >, bool > set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition: List.H:498
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
List< label > labelList
A List of labels.
Definition: List.H:61
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)