Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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"
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
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 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
191:
193 steps_(nsteps)
194{}
195
196
198(
199 const polyMesh& mesh,
200 const dictionary& dict
202:
204 steps_(dict.getOrDefault<label>("steps", 1))
205{}
206
207
209(
210 const polyMesh& mesh,
211 Istream& is
212)
214 haloToCell(mesh, 1)
215{}
216
217
218// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
220Foam::label Foam::haloToCell::steps() const noexcept
221{
222 return steps_;
223}
224
225
226Foam::label Foam::haloToCell::steps(const label nsteps) noexcept
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// ************************************************************************* //
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:60
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A topoSetCellSource to select cells attached to the outside of this cellSet, and add into/remove from...
Definition haloToCell.H:147
label steps() const noexcept
The number of steps to grow/shrink.
Definition haloToCell.C:213
virtual void applyToSet(const topoSetSource::setAction action, topoSet &set) const
Apply specified action to the topoSet.
Definition haloToCell.C:228
haloToCell(const polyMesh &mesh, const label nsteps=1)
Construct from components.
Definition haloToCell.C:180
Mesh consisting of general polyhedral cells.
Definition polyMesh.H:79
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
The topoSetCellSource is a intermediate class for handling topoSet sources for selecting cells.
topoSetCellSource(const polyMesh &mesh)
Construct from mesh.
Class with constructor to add usage string to table.
Base class of a source for a topoSet.
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() const noexcept
Reference to the mesh.
const polyMesh & mesh_
Reference to the mesh.
General set of labels of mesh quantity (points, cells, faces).
Definition topoSet.H:63
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
dynamicFvMesh & mesh
const cellShapeList & cells
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition List.H:62
messageStream Info
Information stream (stdout output on master, null elsewhere).
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.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
void add(DimensionedField< scalar, GeoMesh > &result, const dimensioned< scalar > &dt1, const DimensionedField< scalar, GeoMesh > &f2)
List< cell > cellList
List of cell.
Definition cellListFwd.H:41
const Type * isA(const U &obj)
Attempt dynamic_cast to Type.
Definition typeInfo.H:87
const direction noexcept
Definition scalarImpl.H:265
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dict add("bounds", meshBb)
dictionary dict