Loading...
Searching...
No Matches
topoBoolSet.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) 2018 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 "topoBoolSet.H"
29#include "polyMesh.H"
30#include "Time.H"
31
32// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
33
34// Update stored cell numbers using map.
35// Do in two passes to prevent allocation if nothing changed.
37{
38 boolList& labels = selected_;
39
40 // Iterate over map to see if anything changed
41 // Must iterate over ALL elements, to properly trap bounds errors
42
43 bool changed = false;
44
45 forAll(labels, oldId)
46 {
47 if (!labels.test(oldId))
48 {
49 continue;
50 }
51
52 if (oldId >= map.size())
53 {
55 << "Illegal content " << oldId << " of set:" << name()
56 << " of type " << type() << nl
57 << "Value should be between [0," << map.size() << ')'
58 << endl
59 << abort(FatalError);
60 }
61
62 const label newId = map[oldId];
63
64 if (newId != oldId)
65 {
66 changed = true;
67 #ifdef FULLDEBUG
68 continue; // Check all elements in FULLDEBUG mode
69 #endif
70 break;
71 }
72 }
73
74 if (!changed)
75 {
76 return;
77 }
78
79
80 // Relabel. Use second boolList to prevent overlapping.
81
82 // The new length is given by the map
83 const label len = map.size();
84
85 boolList newLabels(len, false);
86
87 forAll(labels, oldId)
88 {
89 const label newId = map[oldId];
90
91 if (newId >= 0)
92 {
93 newLabels.set(newId); // Ignores -ve indices
94 }
95 }
96
97 labels.transfer(newLabels);
98}
99
100
101void Foam::topoBoolSet::check(const label maxSize)
102{
103 const boolList& labels = selected_;
104
105 const label oldId = labels.rfind(true);
106
107 if (oldId >= maxSize)
108 {
110 << "Illegal content " << oldId << " of set:" << name()
111 << " of type " << type() << nl
112 << "Value should be between [0," << maxSize << ')'
113 << endl
115 }
116}
117
118
119// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
120
122(
123 const polyMesh& mesh,
124 const word& setName
125)
126:
127 topoSet
128 (
129 IOobject
130 (
131 setName,
132 mesh.time().constant(),
133 mesh,
134 IOobject::NO_READ,
135 IOobject::NO_WRITE,
137 ),
138 Foam::zero{} // Empty labelHashSet (initialCapacity = 0)
139 )
140{}
141
142
144(
145 const polyMesh& mesh,
146 const word& setName,
147 const label size,
148 const bool val
149)
151 topoBoolSet(mesh, setName)
152{
153 selected_.resize(size, val);
154}
155
156
158(
159 const polyMesh& mesh,
160 const word& setName,
161 const label size,
162 const boolList& bools
163)
164:
166{
167 selected_ = bools;
168 selected_.resize(size);
169}
170
171
173(
174 const polyMesh& mesh,
175 const word& setName,
176 const label size,
177 boolList&& bools
178)
179:
180 topoBoolSet(mesh, setName)
181{
182 selected_ = std::move(bools);
183 selected_.resize(size);
184}
185
186
187// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
189bool Foam::topoBoolSet::contains(const label id) const
190{
191 return selected_.test(id);
192}
193
195bool Foam::topoBoolSet::found(const label id) const
196{
197 return selected_.test(id);
198}
199
201bool Foam::topoBoolSet::set(const label id)
202{
203 return selected_.set(id);
204}
205
207bool Foam::topoBoolSet::unset(const label id)
208{
209 return selected_.unset(id);
210}
211
212
213void Foam::topoBoolSet::set(const labelUList& labels)
214{
215 for (const label id : labels)
216 {
217 selected_[id] = true;
218 }
219}
220
221
222void Foam::topoBoolSet::unset(const labelUList& labels)
223{
224 for (const label id : labels)
225 {
226 selected_.unset(id);
227 }
228}
229
230
231void Foam::topoBoolSet::invert(const label maxLen)
232{
233 selected_.resize(maxLen);
234 for (bool& b : selected_)
235 {
236 b = !b;
237 }
238}
239
240
241void Foam::topoBoolSet::subset(const topoSet& set)
242{
243 // Only retain entries found in both sets
244 if (set.empty())
245 {
246 selected_ = false;
247 }
248 else
249 {
250 forAll(selected_, i)
252 selected_[i] = (selected_[i] && set.found(i));
253 }
254 }
255}
256
257
259{
260 // Only retain entries found in both sets
261 if (set.empty())
262 {
263 selected_ = false;
264 }
265 else
266 {
267 const boolList oldSelected(selected_);
268 selected_ = false;
269 for (const label id : set)
271 selected_[id] = oldSelected[id];
272 }
273 }
274}
275
276
277void Foam::topoBoolSet::addSet(const topoSet& set)
278{
279 // Add entries to the set
280 for (const label id : set)
281 {
282 selected_[id] = true;
283 }
284}
285
286
288{
289 // Add entries to the set
290 for (const label id : set)
291 {
292 selected_[id] = true;
293 }
294}
295
296
297void Foam::topoBoolSet::subtractSet(const topoSet& set)
298{
299 // Subtract entries from the set
300 for (const label id : set)
301 {
302 selected_.unset(id);
303 }
304}
305
306
308{
309 // Subtract entries from the set
310 for (const label id : set)
311 {
312 selected_.unset(id);
313 }
314}
315
316
317// ************************************************************************* //
label size() const noexcept
The number of elements in table.
Definition HashTable.H:358
@ NO_REGISTER
Do not request registration (bool: false).
@ NO_READ
Nothing to be read.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
const Time & time() const noexcept
Return Time associated with the objectRegistry.
Definition IOobject.C:456
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition List.C:347
bool set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition List.H:469
bool test(const label i) const
Test bool value at specified position, always false for out-of-range access.
Definition UList.H:852
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition UList.C:199
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
constant condensation/saturation model.
Mesh consisting of general polyhedral cells.
Definition polyMesh.H:79
Base for a special purpose topoSet using labels stored as a boolList.
Definition topoBoolSet.H:50
virtual void invert(const label maxLen)
Invert contents.
virtual bool unset(const label id)
Unset an index.
virtual bool found(const label id) const
Has the given index?
virtual void subset(const labelUList &elems)
Subset contents. Only elements present in both sets remain.
virtual void subtractSet(const labelUList &elems)
Subtract given elements from the set.
topoBoolSet(const polyMesh &mesh, const word &setName)
Construct (no-read) with empty selection.
virtual bool contains(const label id) const
Has the given index?
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition topoBoolSet.C:29
virtual bool set(const label id)
Set an index.
virtual void check(const label maxSize)
Check limits on addressable range.
Definition topoBoolSet.C:94
virtual void addSet(const labelUList &elems)
Add given elements to the set.
General set of labels of mesh quantity (points, cells, faces).
Definition topoSet.H:63
topoSet(const topoSet &)=delete
No copy construct.
virtual label maxSize(const polyMesh &mesh) const =0
Return max allowable index (+1). Not implemented.
A class for handling words, derived from Foam::string.
Definition word.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition BitOps.C:35
Different types of constants.
Namespace for OpenFOAM.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
List< bool > boolList
A List of bools.
Definition List.H:60
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
UList< label > labelUList
A UList of labels.
Definition UList.H:75
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
volScalarField & b
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299