Zone.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Zone.H"
27 #include "HashSet.H"
28 #include "pointField.H"
29 #include "demandDrivenData.H"
30 
31 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
32 
33 template<class ZoneType, class ZonesType>
34 template<class Type>
36 {
37  const pointField& ctrs = zone.meshCentres();
38 
39  labelList& indices = *this;
40  indices.setSize(ctrs.size());
41 
42  label nInZone = 0;
43  forAll(ctrs, i)
44  {
45  if (zone.contains(ctrs[i]))
46  {
47  indices[nInZone++] = i;
48  }
49  }
50 
51  indices.setSize(nInZone);
52 }
53 
54 
55 template<class ZoneType, class ZonesType>
57 {
58  if (lookupMapPtr_)
59  {
61  << "Lookup map already calculated" << nl
62  << abort(FatalError);
63  }
64 
65  const labelList& indices = *this;
66 
67  lookupMapPtr_ = new Map<label>(2*indices.size());
68  Map<label>& lm = *lookupMapPtr_;
69 
70  forAll(indices, i)
71  {
72  lm.insert(indices[i], i);
73  }
74 }
75 
76 
77 template<class ZoneType, class ZonesType>
79 (
80  const labelList& map,
81  const labelList& reverseMap
82 )
83 {
84  clearAddressing();
85 
86  labelHashSet indices;
87 
88  forAll(map, i)
89  {
90  if (map[i] >= 0 && localIndex(map[i]) != -1)
91  {
92  indices.insert(i);
93  }
94  }
95 
96  forAll(reverseMap, i)
97  {
98  if (reverseMap[i] >= 0 && localIndex(i) != -1)
99  {
100  indices.insert(reverseMap[i]);
101  }
102  }
103 
104  labelList::operator=(indices.sortedToc());
105 }
106 
107 
108 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
109 
110 template<class ZoneType, class ZonesType>
112 (
113  const word& name,
114  const labelUList& indices,
115  const ZonesType& zones,
116  const bool moveUpdate,
117  const bool topoUpdate
118 )
119 :
120  labelList(indices),
121  name_(name),
122  zones_(zones),
123  moveUpdate_(moveUpdate),
124  topoUpdate_(topoUpdate),
125  lookupMapPtr_(nullptr)
126 {}
127 
128 
129 template<class ZoneType, class ZonesType>
131 (
132  const word& name,
133  labelList&& indices,
134  const ZonesType& zones,
135  const bool moveUpdate,
136  const bool topoUpdate
137 )
138 :
139  labelList(move(indices)),
140  name_(name),
141  zones_(zones),
142  moveUpdate_(moveUpdate),
143  topoUpdate_(topoUpdate),
144  lookupMapPtr_(nullptr)
145 {}
146 
147 
148 template<class ZoneType, class ZonesType>
150 (
151  const word& name,
152  const dictionary& dict,
153  const ZonesType& zones
154 )
155 :
156  labelList(dict.lookup(ZoneType::labelsName)),
157  name_(name),
158  zones_(zones),
159  moveUpdate_(false),
160  topoUpdate_(false),
161  lookupMapPtr_(nullptr)
162 {}
163 
164 
165 template<class ZoneType, class ZonesType>
167 (
168  const Zone& z,
169  const word& name,
170  const labelUList& indices,
171  const ZonesType& zones
172 )
173 :
174  labelList(indices),
175  name_(name),
176  zones_(zones),
177  moveUpdate_(z.moveUpdate_),
178  topoUpdate_(z.topoUpdate_),
179  lookupMapPtr_(nullptr)
180 {}
181 
182 
183 template<class ZoneType, class ZonesType>
185 (
186  const Zone& z,
187  const word& name,
188  labelList&& indices,
189  const ZonesType& zones
190 )
191 :
192  labelList(move(indices)),
193  name_(name),
194  zones_(zones),
195  moveUpdate_(z.moveUpdate_),
196  topoUpdate_(z.topoUpdate_),
197  lookupMapPtr_(nullptr)
198 {}
199 
200 
201 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
202 
203 template<class ZoneType, class ZonesType>
205 {
206  clearAddressing();
207 }
208 
209 
210 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
211 
212 template<class ZoneType, class ZonesType>
214 {
215  return zones_;
216 }
217 
218 
219 template<class ZoneType, class ZonesType>
221 (
222  const label globalIndex
223 ) const
224 {
225  const Map<label>& lm = lookupMap();
226 
228 
229  if (lmIter == lm.end())
230  {
231  return -1;
232  }
233  else
234  {
235  return lmIter();
236  }
237 }
238 
239 
240 template<class ZoneType, class ZonesType>
242 {
243  if (!lookupMapPtr_)
244  {
245  calcLookupMap();
246  }
247 
248  return *lookupMapPtr_;
249 }
250 
251 
252 template<class ZoneType, class ZonesType>
254 {
255  deleteDemandDrivenData(lookupMapPtr_);
256 }
257 
258 
259 template<class ZoneType, class ZonesType>
261 (
262  const label maxSize,
263  const bool report
264 ) const
265 {
266  const labelList& indices = *this;
267 
268  bool hasError = false;
269 
270  // To check for duplicate entries
271  labelHashSet elems(size());
272 
273  forAll(indices, i)
274  {
275  if (indices[i] < 0 || indices[i] >= maxSize)
276  {
277  hasError = true;
278 
279  if (report)
280  {
282  << "Zone " << name_
283  << " contains invalid index label " << indices[i] << nl
284  << "Valid index labels are 0.."
285  << maxSize-1 << endl;
286  }
287  else
288  {
289  // w/o report - can stop checking now
290  break;
291  }
292  }
293  else if (!elems.insert(indices[i]))
294  {
295  if (report)
296  {
298  << "Zone " << name_
299  << " contains duplicate index label " << indices[i] << endl;
300  }
301  }
302  }
303 
304  return hasError;
305 }
306 
307 
308 template<class ZoneType, class ZonesType>
310 {
311  clearAddressing();
312  labelHashSet indices(*this);
313  indices.insert(newIndices);
314  labelList::operator=(indices.sortedToc());
315 }
316 
317 
318 template<class ZoneType, class ZonesType>
320 {
321  clearAddressing();
322  labelList::swap(z);
323 }
324 
325 
326 template<class ZoneType, class ZonesType>
328 {
329  clearAddressing();
330 }
331 
332 
333 template<class ZoneType, class ZonesType>
335 {
336  clearAddressing();
337 }
338 
339 
340 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
341 
342 template<class ZoneType, class ZonesType>
344 {
345  moveUpdate_ = zn.moveUpdate_;
346  topoUpdate_ = zn.topoUpdate_;
347  clearAddressing();
349 }
350 
351 
352 template<class ZoneType, class ZonesType>
354 {
355  moveUpdate_ = zn.moveUpdate_;
356  topoUpdate_ = zn.topoUpdate_;
357  clearAddressing();
358  labelList::operator=(move(zn));
359 }
360 
361 
362 template<class ZoneType, class ZonesType>
364 {
365  clearAddressing();
366  labelList::operator=(indices);
367 }
368 
369 
370 template<class ZoneType, class ZonesType>
372 {
373  clearAddressing();
374  labelList::operator=(move(indices));
375 }
376 
377 
378 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
379 
380 template<class ZoneType, class ZonesType>
382 {
383  z.writeDict(os);
384  os.check("Ostream& operator<<(Ostream& f, const Zone& z");
385  return os;
386 }
387 
388 
389 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:109
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:242
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:167
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void operator=(const UList< label > &)
Assignment to UList operator. Takes linear time.
Definition: List.C:376
void setSize(const label)
Reset size of List.
Definition: List.C:281
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
void swap(UList< label > &)
Swap two ULists of the same type in constant time.
Definition: UList.C:90
Base class for zones.
Definition: Zone.H:68
const Map< label > & lookupMap() const
Return a reference to the look-up map.
Definition: Zone.C:241
void swap(Zone &)
Swap two zones.
Definition: Zone.C:319
void topoChange(const labelList &map, const labelList &reverseMap)
Update the addressing using the maps provided.
Definition: Zone.C:79
bool checkDefinition(const label maxSize, const bool report=false) const
Check zone definition with max size given. Return true if in error.
Definition: Zone.C:261
void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: Zone.C:334
label localIndex(const label globalIndex) const
Map storing the local index for every global index. Used to find.
Definition: Zone.C:221
void calcLookupMap() const
Construct the look-up map.
Definition: Zone.C:56
void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: Zone.C:327
bool topoUpdate_
Flag indicating the zone is updated by the generator.
Definition: Zone.H:86
~Zone()
Destructor.
Definition: Zone.C:204
void insert(const labelHashSet &newIndices)
Insert given indices into zone.
Definition: Zone.C:309
void clearAddressing()
Clear addressing.
Definition: Zone.C:253
Zone(const word &name, const labelUList &indices, const ZonesType &zones, const bool moveUpdate=false, const bool topoUpdate=false)
Construct from components.
Definition: Zone.C:112
void select(const Type &zone)
Select the.
Definition: Zone.C:35
bool moveUpdate_
Flag indicating the zone is updated by the generator.
Definition: Zone.H:82
void operator=(const Zone &)
Assignment operator.
Definition: Zone.C:343
const ZonesType & zones() const
Return ZonesType reference.
Definition: Zone.C:213
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:64
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
A class for handling words, derived from string.
Definition: word.H:62
Template functions to aid in the implementation of demand driven data.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
#define WarningInFunction
Report a warning using Foam::Warning.
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
void deleteDemandDrivenData(DataType *&dataPtr)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
error FatalError
static const char nl
Definition: Ostream.H:267
dictionary dict
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112