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-------------------------------------------------------------------------------
10License
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
37namespace Foam
38{
45 (
48 word,
49 halo
50 );
52 (
55 istream,
56 halo
57 );
58}
59
60
61Foam::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
71void 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
216{
217 return steps_;
218}
219
220
221Foam::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// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addNamedToRunTimeSelectionTable(baseType, thisType, argNames, lookupName)
Add to construction table with 'lookupName' as the key.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A topoSetCellSource to select cells attached to the outside of this cellSet, and add into/remove from...
Definition: haloToCell.H:151
label steps() const noexcept
The number of steps to grow/shrink.
Definition: haloToCell.C:215
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
Definition: haloToCell.C:230
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1121
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1127
bool isInternalFace(const label faceIndex) const noexcept
Return true if given face label is internal to the mesh.
label nFaces() const noexcept
Number of mesh faces.
const cellList & cells() const
static void syncFaceList(const polyMesh &mesh, UList< T > &faceValues, const CombineOp &cop)
Synchronize values on all mesh faces.
Definition: syncTools.H:396
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells.
Class with constructor to add usage string to table.
Base class of a source for a topoSet.
Definition: topoSetSource.H:68
void addOrDelete(topoSet &set, const label id, const bool add) const
Add or delete id from set. Add when 'add' is true.
setAction
Enumeration defining various actions.
@ SUBTRACT
Subtract elements from current set.
@ ADD
Add elements to current set.
@ NEW
Create a new set and ADD elements to it.
bool verbose_
Output verbosity (default: true)
const polyMesh & mesh_
Reference to the mesh.
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:67
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
dynamicFvMesh & mesh
const cellShapeList & cells
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition: List.H:66
List< cell > cellList
A List of cells.
Definition: cellListFwd.H:47
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
const direction noexcept
Definition: Scalar.H:223
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dict add("bounds", meshBb)
dictionary dict