initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / sets / topoSets / topoSet.C
blob152193313ec3e5315bf897744643f9e67ac7792a
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 \*---------------------------------------------------------------------------*/
27 #include "topoSet.H"
28 #include "mapPolyMesh.H"
29 #include "polyMesh.H"
30 #include "boundBox.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 namespace Foam
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
47     const word& setType,
48     const polyMesh& mesh,
49     const word& name,
50     readOption r,
51     writeOption w
54     wordConstructorTable::iterator cstrIter =
55         wordConstructorTablePtr_
56             ->find(setType);
58     if (cstrIter == wordConstructorTablePtr_->end())
59     {
60         FatalErrorIn
61         (
62             "topoSet::New(const word&, "
63             "const polyMesh&, const word&, readOption, writeOption)"
64         )   << "Unknown set type " << setType
65             << endl << endl
66             << "Valid set types : " << endl
67             << wordConstructorTablePtr_->toc()
68             << exit(FatalError);
69     }
71     return autoPtr<topoSet>(cstrIter()(mesh, name, r, w));
75 // Construct named object from size (non-existing set).
76 autoPtr<topoSet> topoSet::New
78     const word& setType,
79     const polyMesh& mesh,
80     const word& name,
81     const label size,
82     writeOption w
85     sizeConstructorTable::iterator cstrIter =
86         sizeConstructorTablePtr_
87             ->find(setType);
89     if (cstrIter == sizeConstructorTablePtr_->end())
90     {
91         FatalErrorIn
92         (
93             "topoSet::New(const word&, "
94             "const polyMesh&, const word&, const label, writeOption)"
95         )   << "Unknown set type " << setType
96             << endl << endl
97             << "Valid set types : " << endl
98             << sizeConstructorTablePtr_->toc()
99             << exit(FatalError);
100     }
102     return autoPtr<topoSet>(cstrIter()(mesh, name, size, w));
106 Foam::fileName topoSet::topoSet::localPath
108     const polyMesh& mesh,
109     const word& name
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;
125     for
126     (
127         labelHashSet::const_iterator iter = begin();
128         iter != end();
129         ++iter
130     )
131     {
132         if ((iter.key() < 0) || (iter.key() > map.size()))
133         {
134             FatalErrorIn
135             (
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);
141         }
143         label newCellI = map[iter.key()];
145         if (newCellI != iter.key())
146         {
147             changed = true;
149             break;
150         }
151     }
153     // Relabel (use second Map to prevent overlapping)
154     if (changed)
155     {
156         labelHashSet newSet(2*size());
158         for
159         (
160             labelHashSet::const_iterator iter = begin();
161             iter != end();
162             ++iter
163         )
164         {
165             label newCellI = map[iter.key()];
167             if (newCellI >= 0)
168             {
169                 newSet.insert(newCellI);
170             }
171         }
173         transfer(newSet);
174     }
178 void topoSet::topoSet::check(const label maxLabel)
180     for
181     (
182         topoSet::const_iterator iter = begin();
183         iter != end();
184         ++iter
185     )
186     {
187         if ((iter.key() < 0) || (iter.key() > maxLabel))
188         {
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);
194         }
195     }
199 // Write maxElem elements, starting at iter. Updates iter and elemI.
200 void topoSet::writeDebug
202     Ostream& os,
203     const label maxElem,
204     topoSet::const_iterator& iter,
205     label& elemI
206 ) const
207 {    
208     label n = 0;
210     for (; (iter != end()) && (n < maxElem); ++iter)
211     {
212         if ((n != 0) && ((n % 10) == 0))
213         {
214             os << endl;
215         }
216         os << iter.key() << ' ';
218         n++;
219         elemI++;
220     }
224 // Write maxElem elements, starting at iter. Updates iter and elemI.
225 void topoSet::writeDebug
227     Ostream& os,
228     const pointField& coords,
229     const label maxElem,
230     topoSet::const_iterator& iter,
231     label& elemI
232 ) const
233 {    
234     label n = 0;
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()] << ' ';
244         n++;
245         elemI++;
246     }
250 void topoSet::writeDebug
252     Ostream& os,
253     const pointField& coords,
254     const label maxLen
255 ) const
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;
263     label n = 0;
265     topoSet::const_iterator iter = begin();
267     if (size() <= maxLen)
268     {
269         writeDebug(os, coords, maxLen, iter, n);
270     }
271     else
272     {
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);
280         os<< endl
281           << "  .." << endl
282           << endl;
284         for (; n < size() - halfLen; ++n)
285         {
286             ++iter;
287         }
289         writeDebug(os, coords, halfLen, iter, n);
290     }
294 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
296 topoSet::topoSet(const IOobject& obj, const word& wantedType)
298     regIOobject(obj)
300     if 
301     (
302         readOpt() == IOobject::MUST_READ
303      || (
304             readOpt() == IOobject::READ_IF_PRESENT
305          && headerOk()
306         )
307     )
308     {
309         if (readStream(wantedType).good())
310         {
311             readStream(wantedType) >> static_cast<labelHashSet&>(*this);
313             close();
314         }
315     }
319 topoSet::topoSet
321     const polyMesh& mesh,
322     const word& wantedType,
323     const word& name,
324     readOption r,
325     writeOption w
328     regIOobject
329     (
330         IOobject
331         (
332             name,
333             mesh.pointsInstance(),
334             polyMesh::meshSubDir/"sets",
335             mesh,
336             r,
337             w
338         )
339     )
341     if 
342     (
343         readOpt() == IOobject::MUST_READ
344      || (
345             readOpt() == IOobject::READ_IF_PRESENT
346          && headerOk()
347         )
348     )
349     {
350         if (readStream(wantedType).good())
351         {
352             readStream(wantedType) >> static_cast<labelHashSet&>(*this);
354             close();
355         }
356     }
360 topoSet::topoSet
362     const polyMesh& mesh,
363     const word& name,
364     const label size,
365     writeOption w
368     regIOobject
369     (
370         IOobject
371         (
372             name,
373             mesh.pointsInstance(),
374             polyMesh::meshSubDir/"sets",
375             mesh,
376             NO_READ,
377             w
378         )
379     ),
380     labelHashSet(size)
384 topoSet::topoSet
386     const polyMesh& mesh,
387     const word& name,
388     const labelHashSet& set,
389     writeOption w
392     regIOobject
393     (
394         IOobject
395         (
396             name,
397             mesh.pointsInstance(),
398             polyMesh::meshSubDir/"sets",
399             mesh,
400             NO_READ,
401             w
402         )
403     ),
404     labelHashSet(set)
408 topoSet::topoSet(const IOobject& obj, const label size)
410     regIOobject(obj),
411     labelHashSet(size)
415 topoSet::topoSet(const IOobject& obj, const labelHashSet& set)
417     regIOobject(obj),
418     labelHashSet(set)
423 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
425 topoSet::~topoSet()
429 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
431 void topoSet::invert(const label maxLen)
433     // Keep copy of current set.
434     labelHashSet currentSet(*this);
436     clear();
437     resize(2*(maxLen - currentSet.size()));
439     for (label cellI = 0; cellI < maxLen; cellI++)
440     {
441         if (!currentSet.found(cellI))
442         {
443             insert(cellI);
444         }
445     }
446     
450 void topoSet::subset(const topoSet& set)
452     // Keep copy of current set.
453     labelHashSet currentSet(*this);
455     clear();
456     resize(2*min(currentSet.size(), set.size()));
458     for
459     (
460         labelHashSet::const_iterator iter = currentSet.begin();
461         iter != currentSet.end();
462         ++iter
463     )
464     {
465         if (set.found(iter.key()))
466         {
467             // element present in both currentSet and set.
468             insert(iter.key());
469         }
470     }
474 void topoSet::addSet(const topoSet& set)
476     for
477     (
478         topoSet::const_iterator iter = set.begin();
479         iter != set.end();
480         ++iter
481     )
482     {
483         insert(iter.key());
484     }
488 void topoSet::deleteSet(const topoSet& set)
490     for
491     (
492         topoSet::const_iterator iter = set.begin();
493         iter != set.end();
494         ++iter
495     )
496     {
497         erase(iter.key());
498     }
502 void topoSet::sync(const polyMesh&)
504     notImplemented("topoSet::sync(const polyMesh&)");
508 void topoSet::writeDebug(Ostream& os, const label maxLen) const
510     label n = 0;
512     topoSet::const_iterator iter = begin();
514     if (size() <= maxLen)
515     {
516         writeDebug(os, maxLen, iter, n);
517     }
518     else
519     {
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);
527         os<< endl
528           << "  .." << endl
529           << endl;
531         for (; n < size() - halfLen; ++n)
532         {
533             ++iter;
534         }
536         writeDebug(os, halfLen, iter, n);
537     }
541 void topoSet::writeDebug
543     Ostream&,
544     const primitiveMesh&,
545     const label
546 ) const
548     notImplemented
549     (
550         "topoSet::writeDebug(Ostream&, const primitiveMesh&, const label)"
551     );
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&)");
572     return -1;
575 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
577 void topoSet::operator=(const topoSet& rhs)
579     labelHashSet::operator=(rhs);
583 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
585 } // End namespace Foam
587 // ************************************************************************* //