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-2021 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 (returnReduce(updates.none(), andOp<bool>()))
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 * * * * * * * * * * * * * //
214 
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 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::topoSetSource::ADD
Add elements to current set.
Definition: topoSetSource.H:103
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::topoSetSource::addToUsageTable
Class with constructor to add usage string to table.
Definition: topoSetSource.H:129
Foam::haloToCell::applyToSet
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
Definition: haloToCell.C:230
Foam::primitiveMesh::cells
const cellList & cells() const
Definition: primitiveMeshCells.C:138
haloToCell.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::topoSetSource::setAction
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:100
Foam::topoSetSource::NEW
Create a new set and ADD elements to it.
Definition: topoSetSource.H:104
polyMesh.H
syncTools.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::syncTools::syncFaceList
static void syncFaceList(const polyMesh &mesh, UList< T > &faceValues, const CombineOp &cop)
Synchronize values on all mesh faces.
Definition: syncTools.H:396
Foam::haloToCell
A topoSetCellSource to select cells attached to the outside of this cellSet, and add into/remove from...
Definition: haloToCell.H:148
Foam::topoSetSource::verbose_
bool verbose_
Output verbosity (default: true)
Definition: topoSetSource.H:159
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::ListListOps::combine
AccessType combine(const UList< T > &lists, AccessOp aop=accessOp< T >())
Combines sub-lists into a single list.
Definition: ListListOps.C:69
Foam::polyMesh::faceOwner
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1107
Foam::addNamedToRunTimeSelectionTable
addNamedToRunTimeSelectionTable(topoSetCellSource, badQualityToCell, word, badQuality)
Foam::cellList
List< cell > cellList
A List of cells.
Definition: cellListFwd.H:47
Foam::topoSet
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:63
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:939
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::topoSetSource::SUBTRACT
Subtract elements from current set.
Definition: topoSetSource.H:105
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::primitiveMesh::isInternalFace
bool isInternalFace(const label faceIndex) const noexcept
Return true if given face label is internal to the mesh.
Definition: primitiveMeshI.H:103
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::topoSetCellSource
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells.
Definition: topoSetCellSource.H:54
Foam::List::set
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition: List.H:341
Foam::haloToCell::steps
label steps() const noexcept
The number of steps to grow/shrink.
Definition: haloToCell.C:215
Foam::topoSetSource::addOrDelete
void addOrDelete(topoSet &set, const label id, const bool add) const
Add or delete id from set. Add when 'add' is true.
Definition: topoSetSource.C:171
Foam::primitiveMesh::nFaces
label nFaces() const noexcept
Number of mesh faces.
Definition: primitiveMeshI.H:90
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
cellSet.H
Foam::topoSetSource::mesh_
const polyMesh & mesh_
Reference to the mesh.
Definition: topoSetSource.H:156
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::haloToCell::haloToCell
haloToCell(const polyMesh &mesh, const label nsteps=1)
Construct from components.
Definition: haloToCell.C:183
topoBitSet.H
Foam::polyMesh::faceNeighbour
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1113