topoSet.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 | Copyright (C) 2011-2016 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 "topoSet.H"
27 #include "mapPolyMesh.H"
28 #include "polyMesh.H"
29 #include "boundBox.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(topoSet, 0);
37  defineRunTimeSelectionTable(topoSet, word);
38  defineRunTimeSelectionTable(topoSet, size);
39  defineRunTimeSelectionTable(topoSet, set);
40 }
41 
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
46 (
47  const word& setType,
48  const polyMesh& mesh,
49  const word& name,
50  readOption r,
51  writeOption w
52 )
53 {
54  wordConstructorTable::iterator cstrIter =
55  wordConstructorTablePtr_->find(setType);
56 
57  if (cstrIter == wordConstructorTablePtr_->end())
58  {
60  << "Unknown set type " << setType
61  << endl << endl
62  << "Valid set types : " << endl
63  << wordConstructorTablePtr_->sortedToc()
64  << exit(FatalError);
65  }
66 
67  return autoPtr<topoSet>(cstrIter()(mesh, name, r, w));
68 }
69 
70 
72 (
73  const word& setType,
74  const polyMesh& mesh,
75  const word& name,
76  const label size,
77  writeOption w
78 )
79 {
80  sizeConstructorTable::iterator cstrIter =
81  sizeConstructorTablePtr_->find(setType);
82 
83  if (cstrIter == sizeConstructorTablePtr_->end())
84  {
86  << "Unknown set type " << setType
87  << endl << endl
88  << "Valid set types : " << endl
89  << sizeConstructorTablePtr_->sortedToc()
90  << exit(FatalError);
91  }
92 
93  return autoPtr<topoSet>(cstrIter()(mesh, name, size, w));
94 }
95 
96 
98 (
99  const word& setType,
100  const polyMesh& mesh,
101  const word& name,
102  const topoSet& set,
103  writeOption w
104 )
105 {
106  setConstructorTable::iterator cstrIter =
107  setConstructorTablePtr_->find(setType);
108 
109  if (cstrIter == setConstructorTablePtr_->end())
110  {
112  << "Unknown set type " << setType
113  << endl << endl
114  << "Valid set types : " << endl
115  << setConstructorTablePtr_->sortedToc()
116  << exit(FatalError);
117  }
118 
119  return autoPtr<topoSet>(cstrIter()(mesh, name, set, w));
120 }
121 
122 
124 (
125  const polyMesh& mesh,
126  const word& name
127 )
128 {
129  return mesh.facesInstance()/mesh.dbDir()/polyMesh::meshSubDir/"sets"/name;
130 }
131 
132 
133 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
134 
135 // Update stored cell numbers using map.
136 // Do in two passes to prevent allocation if nothing changed.
138 {
139  // Iterate over map to see if anything changed
140  bool changed = false;
141 
142  forAllConstIter(labelHashSet, *this, iter)
143  {
144  if ((iter.key() < 0) || (iter.key() > map.size()))
145  {
147  << "Illegal content " << iter.key() << " of set:" << name()
148  << " of type " << type() << endl
149  << "Value should be between 0 and " << map.size()-1
150  << abort(FatalError);
151  }
152 
153  const label newCelli = map[iter.key()];
154 
155  if (newCelli != iter.key())
156  {
157  changed = true;
158 
159  break;
160  }
161  }
162 
163  // Relabel (use second Map to prevent overlapping)
164  if (changed)
165  {
166  labelHashSet newSet(2*size());
167 
168  forAllConstIter(labelHashSet, *this, iter)
169  {
170  const label newCelli = map[iter.key()];
171 
172  if (newCelli >= 0)
173  {
174  newSet.insert(newCelli);
175  }
176  }
177 
178  transfer(newSet);
179  }
180 }
181 
182 
183 void Foam::topoSet::check(const label maxLabel)
184 {
185  forAllConstIter(topoSet, *this, iter)
186  {
187  if ((iter.key() < 0) || (iter.key() > maxLabel))
188  {
190  << "Illegal content " << iter.key() << " of set:" << name()
191  << " of type " << type() << endl
192  << "Value should be between 0 and " << maxLabel
193  << abort(FatalError);
194  }
195  }
196 }
197 
198 
199 // Write maxElem elements, starting at iter. Updates iter and elemI.
201 (
202  Ostream& os,
203  const label maxElem,
205  label& elemI
206 ) const
207 {
208  label n = 0;
209 
210  for (; (iter != end()) && (n < maxElem); ++iter)
211  {
212  if ((n != 0) && ((n % 10) == 0))
213  {
214  os << endl;
215  }
216  os << iter.key() << ' ';
217 
218  n++;
219  elemI++;
220  }
221 }
222 
223 
224 // Write maxElem elements, starting at iter. Updates iter and elemI.
226 (
227  Ostream& os,
228  const pointField& coords,
229  const label maxElem,
231  label& elemI
232 ) const
233 {
234  label n = 0;
235 
236  for (; (iter != end()) && (n < maxElem); ++iter)
237  {
238  if ((n != 0) && ((n % 3) == 0))
239  {
240  os << endl;
241  }
242  os << iter.key() << coords[iter.key()] << ' ';
243 
244  n++;
245  elemI++;
246  }
247 }
248 
249 
251 (
252  Ostream& os,
253  const pointField& coords,
254  const label maxLen
255 ) const
256 {
257  // Bounding box of contents.
258  boundBox bb(pointField(coords, toc()), true);
259 
260  os << "Set bounding box: min = "
261  << bb.min() << " max = " << bb.max() << " metres. " << endl << endl;
262 
263  label n = 0;
264 
265  topoSet::const_iterator iter = begin();
266 
267  if (size() <= maxLen)
268  {
269  writeDebug(os, coords, maxLen, iter, n);
270  }
271  else
272  {
273  label halfLen = maxLen/2;
274 
275  os << "Size larger than " << maxLen << ". Printing first and last "
276  << halfLen << " elements:" << endl << endl;
277 
278  writeDebug(os, coords, halfLen, iter, n);
279 
280  os << nl << " .." << nl << endl;
281 
282  for (; n < size() - halfLen; ++n)
283  {
284  ++iter;
285  }
286 
287  writeDebug(os, coords, halfLen, iter, n);
288  }
289 }
290 
291 
292 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
293 
294 Foam::topoSet::topoSet(const IOobject& obj, const word& wantedType)
295 :
296  regIOobject(obj)
297 {
298  if
299  (
302  || (
304  && headerOk()
305  )
306  )
307  {
308  if (readStream(wantedType).good())
309  {
310  readStream(wantedType) >> static_cast<labelHashSet&>(*this);
311 
312  close();
313  }
314  }
315 }
316 
317 
319 (
320  const polyMesh& mesh,
321  const word& wantedType,
322  const word& name,
323  readOption r,
324  writeOption w
325 )
326 :
328  (
329  IOobject
330  (
331  name,
332  mesh.time().findInstance
333  (
334  mesh.dbDir()/polyMesh::meshSubDir/"sets",
335  word::null,
336  r, //IOobject::MUST_READ,
337  mesh.facesInstance()
338  ),
339  polyMesh::meshSubDir/"sets",
340  mesh,
341  r,
342  w
343  )
344  )
345 {
346  if
347  (
350  || (
352  && headerOk()
353  )
354  )
355  {
356  if (readStream(wantedType).good())
357  {
358  readStream(wantedType) >> static_cast<labelHashSet&>(*this);
359 
360  close();
361  }
362  }
363 }
364 
365 
367 (
368  const polyMesh& mesh,
369  const word& name,
370  const label size,
371  writeOption w
372 )
373 :
375  (
376  IOobject
377  (
378  name,
379  mesh.time().findInstance
380  (
381  mesh.dbDir()/polyMesh::meshSubDir/"sets",
382  word::null,
384  mesh.facesInstance()
385  ),
386  polyMesh::meshSubDir/"sets",
387  mesh,
388  IOobject::NO_READ,
389  w
390  )
391  ),
392  labelHashSet(size)
393 {}
394 
395 
397 (
398  const polyMesh& mesh,
399  const word& name,
400  const labelHashSet& set,
401  writeOption w
402 )
403 :
405  (
406  IOobject
407  (
408  name,
409  mesh.time().findInstance
410  (
411  mesh.dbDir()/polyMesh::meshSubDir/"sets",
412  word::null,
414  mesh.facesInstance()
415  ),
416  polyMesh::meshSubDir/"sets",
417  mesh,
418  IOobject::NO_READ,
419  w
420  )
421  ),
422  labelHashSet(set)
423 {}
424 
425 
426 Foam::topoSet::topoSet(const IOobject& obj, const label size)
427 :
428  regIOobject(obj),
429  labelHashSet(size)
430 {}
431 
432 
434 :
435  regIOobject(obj),
437 {}
438 
439 
440 
441 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
442 
444 {}
445 
446 
447 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
448 
449 void Foam::topoSet::invert(const label maxLen)
450 {
451  // Keep copy of current set.
452  labelHashSet currentSet(*this);
453 
454  clear();
455  resize(2*(maxLen - currentSet.size()));
456 
457  for (label celli = 0; celli < maxLen; celli++)
458  {
459  if (!currentSet.found(celli))
460  {
461  insert(celli);
462  }
463  }
464 }
465 
466 
468 {
469  // Keep copy of current set.
470  labelHashSet currentSet(*this);
471 
472  clear();
473  resize(2*min(currentSet.size(), set.size()));
474 
475  forAllConstIter(labelHashSet, currentSet, iter)
476  {
477  if (set.found(iter.key()))
478  {
479  // element present in both currentSet and set.
480  insert(iter.key());
481  }
482  }
483 }
484 
485 
487 {
488  forAllConstIter(topoSet, set, iter)
489  {
490  insert(iter.key());
491  }
492 }
493 
494 
496 {
497  forAllConstIter(topoSet, set, iter)
498  {
499  erase(iter.key());
500  }
501 }
502 
503 
505 {
507 }
508 
509 
510 void Foam::topoSet::writeDebug(Ostream& os, const label maxLen) const
511 {
512  label n = 0;
513 
515 
516  if (size() <= maxLen)
517  {
518  writeDebug(os, maxLen, iter, n);
519  }
520  else
521  {
522  label halfLen = maxLen/2;
523 
524  os << "Size larger than " << maxLen << ". Printing first and last "
525  << halfLen << " elements:" << endl << endl;
526 
527  writeDebug(os, halfLen, iter, n);
528 
529  os << nl << " .." << nl << endl;
530 
531  for (; n < size() - halfLen; ++n)
532  {
533  ++iter;
534  }
535 
536  writeDebug(os, halfLen, iter, n);
537  }
538 }
539 
540 
542 {
543  return (os << *this).good();
544 }
545 
546 
548 {
550 }
551 
552 
553 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
554 
556 {
558 }
559 
560 
561 // ************************************************************************* //
const Time & time() const
Return time.
bool set(const label &key)
Same as insert (cannot overwrite nil content)
Definition: HashSet.H:126
void operator=(const topoSet &)
Copy labelHashSet part only.
Definition: topoSet.C:555
writeOption
Enumeration defining the write options.
Definition: IOobject.H:115
HashTable< nil, label, Hash< label > >::const_iterator const_iterator
Definition: HashSet.H:67
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
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
error FatalError
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:778
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:309
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
void operator=(const HashTable< nil, label, Hash< label > > &)
Assignment.
static autoPtr< topoSet > New(const word &setType, const polyMesh &mesh, const word &name, readOption r=MUST_READ, writeOption w=NO_WRITE)
Return a pointer to a toposet read from file.
Definition: topoSet.C:46
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:60
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:58
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
void check(const label maxLabel)
Check validity of contents.
Definition: topoSet.C:183
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
regIOobject(const IOobject &, const bool isTime=false)
Construct from IOobject. Optional flag for if IOobject is the.
Definition: regIOobject.C:119
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
virtual bool writeData(Ostream &) const
Write contents.
Definition: topoSet.C:541
HashSet< label, Hash< label > > labelHashSet
A HashSet with label keys.
Definition: HashSet.H:210
void updateLabels(const labelList &map)
Update map from map. Used to update cell/face labels.
Definition: topoSet.C:137
virtual ~topoSet()
Destructor.
Definition: topoSet.C:443
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Return the location of "dir" containing the file "name".
Definition: findInstance.C:38
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
Istream & readStream(const word &)
Return Istream and check object type against that given.
dynamicFvMesh & mesh
virtual void addSet(const topoSet &set)
Add elements present in set.
Definition: topoSet.C:486
void writeDebug(Ostream &os, const label maxElem, topoSet::const_iterator &iter, label &elemI) const
Write part of contents nicely formatted. Prints labels only.
Definition: topoSet.C:201
void close()
Close Istream.
A class for handling words, derived from string.
Definition: word.H:59
void clear()
Clear all entries from table.
virtual const fileName & dbDir() const
Override the objectRegistry dbDir for a single-region case.
Definition: polyMesh.C:753
static const word null
An empty word.
Definition: word.H:77
virtual void subset(const topoSet &set)
Subset contents. Only elements present in both sets remain.
Definition: topoSet.C:467
iterator begin()
Iterator set to the beginning of the HashTable.
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:109
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:262
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
defineTypeNameAndDebug(combustionModel, 0)
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:61
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual void deleteSet(const topoSet &set)
Delete elements present in set.
Definition: topoSet.C:495
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:461
bool good() const
Definition: IOobject.H:406
readOption readOpt() const
Definition: IOobject.H:304
virtual void sync(const polyMesh &mesh)
Sync set across coupled patches.
Definition: topoSet.C:504
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
virtual void invert(const label maxLen)
Invert contents. (insert all members 0..maxLen-1 which were not in.
Definition: topoSet.C:449
bool headerOk()
Read and check header info.
Definition: IOobject.C:400
label n
void resize(const label newSize)
Resize the hash table for efficiency.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
topoSet(const topoSet &)
Disallow default bitwise copy construct.
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:366
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
virtual void updateMesh(const mapPolyMesh &morphMap)
Update any stored data for new labels. Not implemented.
Definition: topoSet.C:547
static fileName localPath(const polyMesh &mesh, const word &name)
Name of file set will use.
Definition: topoSet.C:124
Namespace for OpenFOAM.