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-2024 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 )
117 :
118  labelList(indices),
119  name_(name),
120  zones_(zones),
121  lookupMapPtr_(nullptr)
122 {}
123 
124 
125 template<class ZoneType, class ZonesType>
127 (
128  const word& name,
129  labelList&& indices,
130  const ZonesType& zones
131 )
132 :
133  labelList(move(indices)),
134  name_(name),
135  zones_(zones),
136  lookupMapPtr_(nullptr)
137 {}
138 
139 
140 template<class ZoneType, class ZonesType>
142 (
143  const word& name,
144  const dictionary& dict,
145  const ZonesType& zones
146 )
147 :
148  labelList(dict.lookup(ZoneType::labelsName)),
149  name_(name),
150  zones_(zones),
151  lookupMapPtr_(nullptr)
152 {}
153 
154 
155 template<class ZoneType, class ZonesType>
157 (
158  const Zone& z,
159  const labelUList& indices,
160  const ZonesType& zones
161 )
162 :
163  labelList(indices),
164  name_(z.name()),
165  zones_(zones),
166  lookupMapPtr_(nullptr)
167 {}
168 
169 
170 template<class ZoneType, class ZonesType>
172 (
173  const Zone& z,
174  labelList&& indices,
175  const ZonesType& zones
176 )
177 :
178  labelList(move(indices)),
179  name_(z.name()),
180  zones_(zones),
181  lookupMapPtr_(nullptr)
182 {}
183 
184 
185 template<class ZoneType, class ZonesType>
187 (
188  const word& name,
189  const dictionary& dict,
190  const ZonesType& mz
191 )
192 {
193  if (ZoneType::debug)
194  {
196  << "Constructing " << ZoneType::typeName << " " << name << endl;
197  }
198 
199  const word type(dict.lookup("type"));
200 
201  typename dictionaryConstructorTable::iterator cstrIter =
202  dictionaryConstructorTablePtr_->find(type);
203 
204  if (cstrIter == dictionaryConstructorTablePtr_->end())
205  {
207  (
208  dict
209  ) << "Unknown " << ZoneType::typeName << " type "
210  << type << nl << nl
211  << "Valid " << ZoneType::typeName << " types are:" << nl
212  << dictionaryConstructorTablePtr_->sortedToc()
213  << exit(FatalIOError);
214  }
215 
216  return autoPtr<ZoneType>(cstrIter()(name, dict, mz));
217 }
218 
219 
220 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
221 
222 template<class ZoneType, class ZonesType>
224 {
225  clearAddressing();
226 }
227 
228 
229 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
230 
231 template<class ZoneType, class ZonesType>
233 {
234  return zones_;
235 }
236 
237 
238 template<class ZoneType, class ZonesType>
240 (
241  const label globalIndex
242 ) const
243 {
244  const Map<label>& lm = lookupMap();
245 
247 
248  if (lmIter == lm.end())
249  {
250  return -1;
251  }
252  else
253  {
254  return lmIter();
255  }
256 }
257 
258 
259 template<class ZoneType, class ZonesType>
261 {
262  if (!lookupMapPtr_)
263  {
264  calcLookupMap();
265  }
266 
267  return *lookupMapPtr_;
268 }
269 
270 
271 template<class ZoneType, class ZonesType>
273 {
274  deleteDemandDrivenData(lookupMapPtr_);
275 }
276 
277 
278 template<class ZoneType, class ZonesType>
280 (
281  const label maxSize,
282  const bool report
283 ) const
284 {
285  const labelList& indices = *this;
286 
287  bool hasError = false;
288 
289  // To check for duplicate entries
290  labelHashSet elems(size());
291 
292  forAll(indices, i)
293  {
294  if (indices[i] < 0 || indices[i] >= maxSize)
295  {
296  hasError = true;
297 
298  if (report)
299  {
301  << "Zone " << name_
302  << " contains invalid index label " << indices[i] << nl
303  << "Valid index labels are 0.."
304  << maxSize-1 << endl;
305  }
306  else
307  {
308  // w/o report - can stop checking now
309  break;
310  }
311  }
312  else if (!elems.insert(indices[i]))
313  {
314  if (report)
315  {
317  << "Zone " << name_
318  << " contains duplicate index label " << indices[i] << endl;
319  }
320  }
321  }
322 
323  return hasError;
324 }
325 
326 
327 template<class ZoneType, class ZonesType>
329 {
330  labelHashSet indices(*this);
331  indices.insert(newIndices);
332  labelList::operator=(indices.sortedToc());
333 }
334 
335 
336 template<class ZoneType, class ZonesType>
338 {
339  clearAddressing();
340  labelList::swap(z);
341 }
342 
343 
344 template<class ZoneType, class ZonesType>
346 {
347  clearAddressing();
348 }
349 
350 
351 template<class ZoneType, class ZonesType>
353 {
354  clearAddressing();
355 }
356 
357 
358 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
359 
360 template<class ZoneType, class ZonesType>
362 {
363  clearAddressing();
365 }
366 
367 
368 template<class ZoneType, class ZonesType>
370 {
371  clearAddressing();
372  labelList::operator=(move(zn));
373 }
374 
375 
376 template<class ZoneType, class ZonesType>
378 {
379  clearAddressing();
380  labelList::operator=(indices);
381 }
382 
383 
384 template<class ZoneType, class ZonesType>
386 {
387  clearAddressing();
388  labelList::operator=(move(indices));
389 }
390 
391 
392 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
393 
394 template<class ZoneType, class ZonesType>
396 {
397  z.writeDict(os);
398  os.check("Ostream& operator<<(Ostream& f, const Zone& z");
399  return os;
400 }
401 
402 
403 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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:67
const Map< label > & lookupMap() const
Return a reference to the look-up map.
Definition: Zone.C:260
void swap(Zone &)
Swap two zones.
Definition: Zone.C:337
Zone(const word &name, const labelUList &indices, const ZonesType &zones)
Construct from components.
Definition: Zone.C:112
virtual void writeDict(Ostream &) const =0
Write dictionary.
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:280
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: Zone.C:352
label localIndex(const label globalIndex) const
Map storing the local index for every global index. Used to find.
Definition: Zone.C:240
void calcLookupMap() const
Construct the look-up map.
Definition: Zone.C:56
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: Zone.C:345
virtual ~Zone()
Destructor.
Definition: Zone.C:223
void insert(const labelHashSet &newIndices)
Insert given indices into zone.
Definition: Zone.C:328
virtual void clearAddressing()
Clear addressing.
Definition: Zone.C:272
void select(const Type &zone)
Select the.
Definition: Zone.C:35
static autoPtr< ZoneType > New(const word &name, const dictionary &, const ZonesType &)
Return a pointer to a new cell zone.
Definition: Zone.C:187
void operator=(const Zone &)
Assignment operator.
Definition: Zone.C:361
const ZonesType & zones() const
Return ZonesType reference.
Definition: Zone.C:232
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:710
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 FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
#define WarningInFunction
Report a warning using Foam::Warning.
#define InfoInFunction
Report an information message using Foam::Info.
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
void deleteDemandDrivenData(DataType *&dataPtr)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
IOerror FatalIOError
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
error FatalError
static const char nl
Definition: Ostream.H:266
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112