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 -------------------------------------------------------------------------------
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 "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 
101 void 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  (
129  IOobject
130  (
131  setName,
132  mesh.time().constant(),
133  mesh,
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;
169  selected_.resize(size);
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);
184  selected_.resize(size);
185 }
186 
187 
188 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
189 
190 bool Foam::topoBoolSet::found(const label id) const
191 {
192  return selected_.test(id);
193 }
194 
195 
196 bool Foam::topoBoolSet::set(const label id)
197 {
198  return selected_.set(id);
199 }
200 
201 
202 bool 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 
226 void 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 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::HashSetOps::bools
List< bool > bools(const labelHashSet &locations)
Definition: HashOps.C:81
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::topoBoolSet::check
virtual void check(const label maxSize)
Check limits on addressable range.
Definition: topoBoolSet.C:101
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::topoBoolSet::invert
virtual void invert(const label maxLen)
Invert contents.
Definition: topoBoolSet.C:226
polyMesh.H
Foam::topoBoolSet::subtractSet
virtual void subtractSet(const topoSet &set)
Subtract elements present in set.
Definition: topoBoolSet.C:263
Foam::topoBoolSet::updateLabels
virtual void updateLabels(const labelUList &map)
Update map from map.
Definition: topoBoolSet.C:36
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
Foam::topoSet
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:63
Foam::topoBoolSet::set
virtual bool set(const label id)
Set an index.
Definition: topoBoolSet.C:196
Foam::topoBoolSet
Base for a special purpose topoSet using labels stored as a boolList.
Definition: topoBoolSet.H:51
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
Foam::topoBoolSet::found
virtual bool found(const label id) const
Has the given index?
Definition: topoBoolSet.C:190
topoBoolSet.H
Foam::topoBoolSet::addSet
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: topoBoolSet.C:253
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::topoBoolSet::selected_
boolList selected_
Definition: topoBoolSet.H:59
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< bool >
Foam::topoBoolSet::unset
virtual bool unset(const label id)
Unset an index.
Definition: topoBoolSet.C:202
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::topoBoolSet::topoBoolSet
topoBoolSet(const polyMesh &mesh, const word &setName)
Construct with empty selection.
Definition: topoBoolSet.C:122
Foam::UList< label >
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::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:280
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
Foam::IOobject::NO_READ
Definition: IOobject.H:188
Foam::topoBoolSet::subset
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: topoBoolSet.C:236