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
114 << abort(FatalError);
115 }
116}
117
118
119// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
120
122(
123 const polyMesh& mesh,
124 const word& setName
125)
126:
127 topoSet
128 (
130 (
131 setName,
132 mesh.time().constant(),
133 mesh,
134 IOobject::NO_READ,
135 IOobject::NO_WRITE,
136 false
137 ),
138 0 // zero-sized (unallocated) labelHashSet
139 ),
140 selected_()
141{}
142
143
145(
146 const polyMesh& mesh,
147 const word& setName,
148 const label size,
149 const bool val
150)
151:
152 topoBoolSet(mesh, setName)
153{
154 selected_.resize(size, val);
155}
156
157
159(
160 const polyMesh& mesh,
161 const word& setName,
162 const label size,
163 const boolList& bools
164)
165:
166 topoBoolSet(mesh, setName)
167{
168 selected_ = bools;
170}
171
172
174(
175 const polyMesh& mesh,
176 const word& setName,
177 const label size,
178 boolList&& bools
179)
180:
181 topoBoolSet(mesh, setName)
182{
183 selected_ = std::move(bools);
185}
186
187
188// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
189
190bool Foam::topoBoolSet::found(const label id) const
191{
192 return selected_.test(id);
193}
194
195
196bool Foam::topoBoolSet::set(const label id)
197{
198 return selected_.set(id);
199}
200
201
202bool Foam::topoBoolSet::unset(const label id)
203{
204 return selected_.unset(id);
205}
206
207
209{
210 for (const label id : labels)
211 {
212 selected_[id] = true;
213 }
214}
215
216
218{
219 for (const label id : labels)
220 {
221 selected_.unset(id);
222 }
223}
224
225
226void Foam::topoBoolSet::invert(const label maxLen)
227{
228 selected_.resize(maxLen);
229 for (bool& b : selected_)
230 {
231 b = !b;
232 }
233}
234
235
237{
238 // Only retain entries found in both sets
239 if (set.empty())
240 {
241 selected_ = false;
242 }
243 else
244 {
245 forAll(selected_, i)
246 {
247 selected_[i] = (selected_[i] && set.found(i));
248 }
249 }
250}
251
252
254{
255 // Add entries to the set
256 for (const label id : set)
257 {
258 selected_[id] = true;
259 }
260}
261
262
264{
265 // Subtract entries from the set
266 for (const label id : set)
267 {
268 selected_.unset(id);
269 }
270}
271
272
273// ************************************************************************* //
bool found
void invert()
Return the matrix inverse into itself if no elem is equal to zero.
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
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:330
void transfer(List< T > &list)
Definition: List.C:447
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type test(const label i) const
Definition: UList.H:518
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: UList.C:236
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
constant condensation/saturation model.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
Base for a special purpose topoSet using labels stored as a boolList.
Definition: topoBoolSet.H:54
virtual void subtractSet(const topoSet &set)
Subtract elements present in set.
Definition: topoBoolSet.C:263
boolList selected_
Definition: topoBoolSet.H:59
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: topoBoolSet.C:253
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: topoBoolSet.C:236
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition: topoBoolSet.C:36
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoBoolSet.C:101
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:67
static const triad unset
Definition: triad.H:97
bool set() const
Are all the vector set.
Definition: triadI.H:76
A class for handling words, derived from Foam::string.
Definition: word.H:68
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
volScalarField & b
Definition: createFields.H:27
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333