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