1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
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);
44 // Construct named object from existing set.
45 autoPtr<topoSet> topoSet::New
54 wordConstructorTable::iterator cstrIter =
55 wordConstructorTablePtr_
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_
89 if (cstrIter == sizeConstructorTablePtr_->end())
93 "topoSet::New(const word&, "
94 "const polyMesh&, const word&, const label, writeOption)"
95 ) << "Unknown set type " << setType
97 << "Valid set types : " << endl
98 << sizeConstructorTablePtr_->toc()
102 return autoPtr<topoSet>(cstrIter()(mesh, name, size, w));
106 Foam::fileName topoSet::topoSet::localPath
108 const polyMesh& mesh,
112 return mesh.pointsInstance()/polyMesh::meshSubDir/"sets"/name;
116 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
118 // Update stored cell numbers using map.
119 // Do in two passes to prevent allocation if nothing changed.
120 void topoSet::topoSet::updateLabels(const labelList& map)
122 // Iterate over map to see if anything changed
123 bool changed = false;
127 labelHashSet::const_iterator iter = begin();
132 if ((iter.key() < 0) || (iter.key() > map.size()))
136 "topoSet::updateLabels(const labelList&, labelHashSet)"
137 ) << "Illegal content " << iter.key() << " of set:" << name()
138 << " of type " << type() << endl
139 << "Value should be between 0 and " << map.size()-1
140 << abort(FatalError);
143 label newCellI = map[iter.key()];
145 if (newCellI != iter.key())
153 // Relabel (use second Map to prevent overlapping)
156 labelHashSet newSet(2*size());
160 labelHashSet::const_iterator iter = begin();
165 label newCellI = map[iter.key()];
169 newSet.insert(newCellI);
178 void topoSet::topoSet::check(const label maxLabel)
182 topoSet::const_iterator iter = begin();
187 if ((iter.key() < 0) || (iter.key() > maxLabel))
189 FatalErrorIn("topoSet::check(const label)")
190 << "Illegal content " << iter.key() << " of set:" << name()
191 << " of type " << type() << endl
192 << "Value should be between 0 and " << maxLabel
193 << abort(FatalError);
199 // Write maxElem elements, starting at iter. Updates iter and elemI.
200 void topoSet::writeDebug
204 topoSet::const_iterator& iter,
210 for (; (iter != end()) && (n < maxElem); ++iter)
212 if ((n != 0) && ((n % 10) == 0))
216 os << iter.key() << ' ';
224 // Write maxElem elements, starting at iter. Updates iter and elemI.
225 void topoSet::writeDebug
228 const pointField& coords,
230 topoSet::const_iterator& iter,
236 for (; (iter != end()) && (n < maxElem); ++iter)
238 if ((n != 0) && ((n % 3) == 0))
242 os << iter.key() << coords[iter.key()] << ' ';
250 void topoSet::writeDebug
253 const pointField& coords,
257 // Bounding box of contents.
258 boundBox bb(pointField(coords, toc()));
260 Pout<< "Set bounding box: min = "
261 << bb.min() << " max = " << bb.max() << " meters. " << endl << endl;
265 topoSet::const_iterator iter = begin();
267 if (size() <= maxLen)
269 writeDebug(os, coords, maxLen, iter, n);
273 label halfLen = maxLen/2;
275 os << "Size larger than " << maxLen << ". Printing first and last "
276 << halfLen << " elements:" << endl << endl;
278 writeDebug(os, coords, halfLen, iter, n);
284 for (; n < size() - halfLen; ++n)
289 writeDebug(os, coords, halfLen, iter, n);
294 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
296 topoSet::topoSet(const IOobject& obj, const word& wantedType)
302 readOpt() == IOobject::MUST_READ
304 readOpt() == IOobject::READ_IF_PRESENT
309 if (readStream(wantedType).good())
311 readStream(wantedType) >> static_cast<labelHashSet&>(*this);
321 const polyMesh& mesh,
322 const word& wantedType,
333 mesh.pointsInstance(),
334 polyMesh::meshSubDir/"sets",
343 readOpt() == IOobject::MUST_READ
345 readOpt() == IOobject::READ_IF_PRESENT
350 if (readStream(wantedType).good())
352 readStream(wantedType) >> static_cast<labelHashSet&>(*this);
362 const polyMesh& mesh,
373 mesh.pointsInstance(),
374 polyMesh::meshSubDir/"sets",
386 const polyMesh& mesh,
388 const labelHashSet& set,
397 mesh.pointsInstance(),
398 polyMesh::meshSubDir/"sets",
408 topoSet::topoSet(const IOobject& obj, const label size)
415 topoSet::topoSet(const IOobject& obj, const labelHashSet& set)
423 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
429 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
431 void topoSet::invert(const label maxLen)
433 // Keep copy of current set.
434 labelHashSet currentSet(*this);
437 resize(2*(maxLen - currentSet.size()));
439 for (label cellI = 0; cellI < maxLen; cellI++)
441 if (!currentSet.found(cellI))
450 void topoSet::subset(const topoSet& set)
452 // Keep copy of current set.
453 labelHashSet currentSet(*this);
456 resize(2*min(currentSet.size(), set.size()));
460 labelHashSet::const_iterator iter = currentSet.begin();
461 iter != currentSet.end();
465 if (set.found(iter.key()))
467 // element present in both currentSet and set.
474 void topoSet::addSet(const topoSet& set)
478 topoSet::const_iterator iter = set.begin();
488 void topoSet::deleteSet(const topoSet& set)
492 topoSet::const_iterator iter = set.begin();
502 void topoSet::sync(const polyMesh&)
504 notImplemented("topoSet::sync(const polyMesh&)");
508 void topoSet::writeDebug(Ostream& os, const label maxLen) const
512 topoSet::const_iterator iter = begin();
514 if (size() <= maxLen)
516 writeDebug(os, maxLen, iter, n);
520 label halfLen = maxLen/2;
522 os << "Size larger than " << maxLen << ". Printing first and last "
523 << halfLen << " elements:" << endl << endl;
525 writeDebug(os, halfLen, iter, n);
531 for (; n < size() - halfLen; ++n)
536 writeDebug(os, halfLen, iter, n);
541 void topoSet::writeDebug
544 const primitiveMesh&,
550 "topoSet::writeDebug(Ostream&, const primitiveMesh&, const label)"
555 bool topoSet::writeData(Ostream& os) const
557 return (os << *this).good();
561 void topoSet::updateMesh(const mapPolyMesh&)
563 notImplemented("topoSet::updateMesh(const mapPolyMesh&)");
567 //- Return max index+1.
568 label topoSet::maxSize(const polyMesh&) const
570 notImplemented("topoSet::maxSize(const polyMesh&)");
575 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
577 void topoSet::operator=(const topoSet& rhs)
579 labelHashSet::operator=(rhs);
583 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
585 } // End namespace Foam
587 // ************************************************************************* //