1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
28 #include "mapPolyMesh.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 defineTypeNameAndDebug(topoSet, 0);
40 defineRunTimeSelectionTable(topoSet, word);
41 defineRunTimeSelectionTable(topoSet, size);
42 defineRunTimeSelectionTable(topoSet, set);
45 // Construct named object from existing set.
46 autoPtr<topoSet> topoSet::New
55 wordConstructorTable::iterator cstrIter =
56 wordConstructorTablePtr_->find(setType);
58 if (cstrIter == wordConstructorTablePtr_->end())
62 "topoSet::New(const word&, "
63 "const polyMesh&, const word&, readOption, writeOption)"
64 ) << "Unknown set type " << setType
66 << "Valid set types : " << endl
67 << wordConstructorTablePtr_->toc()
71 return autoPtr<topoSet>(cstrIter()(mesh, name, r, w));
75 // Construct named object from size (non-existing set).
76 autoPtr<topoSet> topoSet::New
85 sizeConstructorTable::iterator cstrIter =
86 sizeConstructorTablePtr_->find(setType);
88 if (cstrIter == sizeConstructorTablePtr_->end())
92 "topoSet::New(const word&, "
93 "const polyMesh&, const word&, const label, writeOption)"
94 ) << "Unknown set type " << setType
96 << "Valid set types : " << endl
97 << sizeConstructorTablePtr_->toc()
101 return autoPtr<topoSet>(cstrIter()(mesh, name, size, w));
105 // Construct named object from existing set.
106 autoPtr<topoSet> topoSet::New
108 const polyMesh& mesh,
114 setConstructorTable::iterator cstrIter =
115 setConstructorTablePtr_->find(set.type());
117 if (cstrIter == setConstructorTablePtr_->end())
121 "topoSet::New(const polyMesh&, const word&, "
122 "const topoSet&, writeOption)"
123 ) << "Unknown set type " << set.type()
125 << "Valid set types : " << endl
126 << setConstructorTablePtr_->toc()
130 return autoPtr<topoSet>(cstrIter()(mesh, name, set, w));
134 Foam::fileName topoSet::topoSet::localPath
136 const polyMesh& mesh,
140 return mesh.pointsInstance()/polyMesh::meshSubDir/"sets"/name;
144 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
146 // Update stored cell numbers using map.
147 // Do in two passes to prevent allocation if nothing changed.
148 void topoSet::topoSet::updateLabels(const labelList& map)
150 // Iterate over map to see if anything changed
151 bool changed = false;
155 labelHashSet::const_iterator iter = begin();
160 if ((iter.key() < 0) || (iter.key() > map.size()))
164 "topoSet::updateLabels(const labelList&, labelHashSet)"
165 ) << "Illegal content " << iter.key() << " of set:" << name()
166 << " of type " << type() << endl
167 << "Value should be between 0 and " << map.size()-1
168 << abort(FatalError);
171 label newCellI = map[iter.key()];
173 if (newCellI != iter.key())
181 // Relabel (use second Map to prevent overlapping)
184 labelHashSet newSet(2*size());
188 labelHashSet::const_iterator iter = begin();
193 label newCellI = map[iter.key()];
197 newSet.insert(newCellI);
206 void topoSet::topoSet::check(const label maxLabel)
210 topoSet::const_iterator iter = begin();
215 if ((iter.key() < 0) || (iter.key() > maxLabel))
217 FatalErrorIn("topoSet::check(const label)")
218 << "Illegal content " << iter.key() << " of set:" << name()
219 << " of type " << type() << endl
220 << "Value should be between 0 and " << maxLabel
221 << abort(FatalError);
227 // Write maxElem elements, starting at iter. Updates iter and elemI.
228 void topoSet::writeDebug
232 topoSet::const_iterator& iter,
238 for (; (iter != end()) && (n < maxElem); ++iter)
240 if ((n != 0) && ((n % 10) == 0))
244 os << iter.key() << ' ';
252 // Write maxElem elements, starting at iter. Updates iter and elemI.
253 void topoSet::writeDebug
256 const pointField& coords,
258 topoSet::const_iterator& iter,
264 for (; (iter != end()) && (n < maxElem); ++iter)
266 if ((n != 0) && ((n % 3) == 0))
270 os << iter.key() << coords[iter.key()] << ' ';
278 void topoSet::writeDebug
281 const pointField& coords,
285 // Bounding box of contents.
286 boundBox bb(pointField(coords, toc()), true);
288 os << "Set bounding box: min = "
289 << bb.min() << " max = " << bb.max() << " meters. " << endl << endl;
293 topoSet::const_iterator iter = begin();
295 if (size() <= maxLen)
297 writeDebug(os, coords, maxLen, iter, n);
301 label halfLen = maxLen/2;
303 os << "Size larger than " << maxLen << ". Printing first and last "
304 << halfLen << " elements:" << endl << endl;
306 writeDebug(os, coords, halfLen, iter, n);
312 for (; n < size() - halfLen; ++n)
317 writeDebug(os, coords, halfLen, iter, n);
322 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
324 topoSet::topoSet(const IOobject& obj, const word& wantedType)
330 readOpt() == IOobject::MUST_READ
332 readOpt() == IOobject::READ_IF_PRESENT
337 if (readStream(wantedType).good())
339 readStream(wantedType) >> static_cast<labelHashSet&>(*this);
349 const polyMesh& mesh,
350 const word& wantedType,
361 mesh.pointsInstance(),
362 polyMesh::meshSubDir/"sets",
371 readOpt() == IOobject::MUST_READ
373 readOpt() == IOobject::READ_IF_PRESENT
378 if (readStream(wantedType).good())
380 readStream(wantedType) >> static_cast<labelHashSet&>(*this);
390 const polyMesh& mesh,
401 mesh.pointsInstance(),
402 polyMesh::meshSubDir/"sets",
414 const polyMesh& mesh,
416 const labelHashSet& set,
425 mesh.pointsInstance(),
426 polyMesh::meshSubDir/"sets",
436 topoSet::topoSet(const IOobject& obj, const label size)
443 topoSet::topoSet(const IOobject& obj, const labelHashSet& set)
451 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
457 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
459 void topoSet::invert(const label maxLen)
461 // Keep copy of current set.
462 labelHashSet currentSet(*this);
465 resize(2*(maxLen - currentSet.size()));
467 for (label cellI = 0; cellI < maxLen; cellI++)
469 if (!currentSet.found(cellI))
478 void topoSet::subset(const topoSet& set)
480 // Keep copy of current set.
481 labelHashSet currentSet(*this);
484 resize(2*min(currentSet.size(), set.size()));
488 labelHashSet::const_iterator iter = currentSet.begin();
489 iter != currentSet.end();
493 if (set.found(iter.key()))
495 // element present in both currentSet and set.
502 void topoSet::addSet(const topoSet& set)
506 topoSet::const_iterator iter = set.begin();
516 void topoSet::deleteSet(const topoSet& set)
520 topoSet::const_iterator iter = set.begin();
530 void topoSet::sync(const polyMesh&)
532 notImplemented("topoSet::sync(const polyMesh&)");
536 void topoSet::writeDebug(Ostream& os, const label maxLen) const
540 topoSet::const_iterator iter = begin();
542 if (size() <= maxLen)
544 writeDebug(os, maxLen, iter, n);
548 label halfLen = maxLen/2;
550 os << "Size larger than " << maxLen << ". Printing first and last "
551 << halfLen << " elements:" << endl << endl;
553 writeDebug(os, halfLen, iter, n);
559 for (; n < size() - halfLen; ++n)
564 writeDebug(os, halfLen, iter, n);
569 void topoSet::writeDebug
572 const primitiveMesh&,
578 "topoSet::writeDebug(Ostream&, const primitiveMesh&, const label)"
583 bool topoSet::writeData(Ostream& os) const
585 return (os << *this).good();
589 void topoSet::updateMesh(const mapPolyMesh&)
591 notImplemented("topoSet::updateMesh(const mapPolyMesh&)");
595 //- Return max index+1.
596 label topoSet::maxSize(const polyMesh&) const
598 notImplemented("topoSet::maxSize(const polyMesh&)");
603 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
605 void topoSet::operator=(const topoSet& rhs)
607 labelHashSet::operator=(rhs);
611 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
613 } // End namespace Foam
615 // ************************************************************************* //