OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

topoSet.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "topoSet.H"
00028 #include "mapPolyMesh.H"
00029 #include "polyMesh.H"
00030 #include "boundBox.H"
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036 
00037 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00038 
00039 defineTypeNameAndDebug(topoSet, 0);
00040 defineRunTimeSelectionTable(topoSet, word);
00041 defineRunTimeSelectionTable(topoSet, size);
00042 
00043 
00044 // Construct named object from existing set.
00045 autoPtr<topoSet> topoSet::New
00046 (
00047     const word& setType,
00048     const polyMesh& mesh,
00049     const word& name,
00050     readOption r,
00051     writeOption w
00052 )
00053 {
00054     wordConstructorTable::iterator cstrIter =
00055         wordConstructorTablePtr_
00056             ->find(setType);
00057 
00058     if (cstrIter == wordConstructorTablePtr_->end())
00059     {
00060         FatalErrorIn
00061         (
00062             "topoSet::New(const word&, "
00063             "const polyMesh&, const word&, readOption, writeOption)"
00064         )   << "Unknown set type " << setType
00065             << endl << endl
00066             << "Valid set types : " << endl
00067             << wordConstructorTablePtr_->toc()
00068             << exit(FatalError);
00069     }
00070 
00071     return autoPtr<topoSet>(cstrIter()(mesh, name, r, w));
00072 }
00073 
00074 
00075 // Construct named object from size (non-existing set).
00076 autoPtr<topoSet> topoSet::New
00077 (
00078     const word& setType,
00079     const polyMesh& mesh,
00080     const word& name,
00081     const label size,
00082     writeOption w
00083 )
00084 {
00085     sizeConstructorTable::iterator cstrIter =
00086         sizeConstructorTablePtr_
00087             ->find(setType);
00088 
00089     if (cstrIter == sizeConstructorTablePtr_->end())
00090     {
00091         FatalErrorIn
00092         (
00093             "topoSet::New(const word&, "
00094             "const polyMesh&, const word&, const label, writeOption)"
00095         )   << "Unknown set type " << setType
00096             << endl << endl
00097             << "Valid set types : " << endl
00098             << sizeConstructorTablePtr_->toc()
00099             << exit(FatalError);
00100     }
00101 
00102     return autoPtr<topoSet>(cstrIter()(mesh, name, size, w));
00103 }
00104 
00105 
00106 Foam::fileName topoSet::topoSet::localPath
00107 (
00108     const polyMesh& mesh,
00109     const word& name
00110 )
00111 {
00112     return mesh.pointsInstance()/polyMesh::meshSubDir/"sets"/name;
00113 }
00114 
00115 
00116 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00117 
00118 // Update stored cell numbers using map.
00119 // Do in two passes to prevent allocation if nothing changed.
00120 void topoSet::topoSet::updateLabels(const labelList& map)
00121 {
00122     // Iterate over map to see if anything changed
00123     bool changed = false;
00124 
00125     for
00126     (
00127         labelHashSet::const_iterator iter = begin();
00128         iter != end();
00129         ++iter
00130     )
00131     {
00132         if ((iter.key() < 0) || (iter.key() > map.size()))
00133         {
00134             FatalErrorIn
00135             (
00136                 "topoSet::updateLabels(const labelList&, labelHashSet)"
00137             )   << "Illegal content " << iter.key() << " of set:" << name()
00138                 << " of type " << type() << endl
00139                 << "Value should be between 0 and " << map.size()-1
00140                 << abort(FatalError);
00141         }
00142 
00143         label newCellI = map[iter.key()];
00144 
00145         if (newCellI != iter.key())
00146         {
00147             changed = true;
00148 
00149             break;
00150         }
00151     }
00152 
00153     // Relabel (use second Map to prevent overlapping)
00154     if (changed)
00155     {
00156         labelHashSet newSet(2*size());
00157 
00158         for
00159         (
00160             labelHashSet::const_iterator iter = begin();
00161             iter != end();
00162             ++iter
00163         )
00164         {
00165             label newCellI = map[iter.key()];
00166 
00167             if (newCellI >= 0)
00168             {
00169                 newSet.insert(newCellI);
00170             }
00171         }
00172 
00173         transfer(newSet);
00174     }
00175 }
00176 
00177 
00178 void topoSet::topoSet::check(const label maxLabel)
00179 {
00180     for
00181     (
00182         topoSet::const_iterator iter = begin();
00183         iter != end();
00184         ++iter
00185     )
00186     {
00187         if ((iter.key() < 0) || (iter.key() > maxLabel))
00188         {
00189             FatalErrorIn("topoSet::check(const label)")
00190                 << "Illegal content " << iter.key() << " of set:" << name()
00191                 << " of type " << type() << endl
00192                 << "Value should be between 0 and " << maxLabel
00193                 << abort(FatalError);
00194         }
00195     }
00196 }
00197 
00198 
00199 // Write maxElem elements, starting at iter. Updates iter and elemI.
00200 void topoSet::writeDebug
00201 (
00202     Ostream& os,
00203     const label maxElem,
00204     topoSet::const_iterator& iter,
00205     label& elemI
00206 ) const
00207 {    
00208     label n = 0;
00209 
00210     for (; (iter != end()) && (n < maxElem); ++iter)
00211     {
00212         if ((n != 0) && ((n % 10) == 0))
00213         {
00214             os << endl;
00215         }
00216         os << iter.key() << ' ';
00217 
00218         n++;
00219         elemI++;
00220     }
00221 }
00222 
00223 
00224 // Write maxElem elements, starting at iter. Updates iter and elemI.
00225 void topoSet::writeDebug
00226 (
00227     Ostream& os,
00228     const pointField& coords,
00229     const label maxElem,
00230     topoSet::const_iterator& iter,
00231     label& elemI
00232 ) const
00233 {    
00234     label n = 0;
00235 
00236     for (; (iter != end()) && (n < maxElem); ++iter)
00237     {
00238         if ((n != 0) && ((n % 3) == 0))
00239         {
00240             os << endl;
00241         }
00242         os << iter.key() << coords[iter.key()] << ' ';
00243 
00244         n++;
00245         elemI++;
00246     }
00247 }
00248 
00249 
00250 void topoSet::writeDebug
00251 (
00252     Ostream& os,
00253     const pointField& coords,
00254     const label maxLen
00255 ) const
00256 {
00257     // Bounding box of contents.
00258     boundBox bb(pointField(coords, toc()));
00259 
00260     Pout<< "Set bounding box: min = "
00261         << bb.min() << "    max = " << bb.max() << " meters. " << endl << endl;
00262 
00263     label n = 0;
00264 
00265     topoSet::const_iterator iter = begin();
00266 
00267     if (size() <= maxLen)
00268     {
00269         writeDebug(os, coords, maxLen, iter, n);
00270     }
00271     else
00272     {
00273         label halfLen = maxLen/2;
00274 
00275         os  << "Size larger than " << maxLen << ". Printing first and last "
00276             << halfLen << " elements:" << endl << endl;
00277 
00278         writeDebug(os, coords, halfLen, iter, n);
00279 
00280         os<< endl
00281           << "  .." << endl
00282           << endl;
00283 
00284         for (; n < size() - halfLen; ++n)
00285         {
00286             ++iter;
00287         }
00288 
00289         writeDebug(os, coords, halfLen, iter, n);
00290     }
00291 }
00292 
00293 
00294 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00295 
00296 topoSet::topoSet(const IOobject& obj, const word& wantedType)
00297 :
00298     regIOobject(obj)
00299 {
00300     if 
00301     (
00302         readOpt() == IOobject::MUST_READ
00303      || (
00304             readOpt() == IOobject::READ_IF_PRESENT
00305          && headerOk()
00306         )
00307     )
00308     {
00309         if (readStream(wantedType).good())
00310         {
00311             readStream(wantedType) >> static_cast<labelHashSet&>(*this);
00312 
00313             close();
00314         }
00315     }
00316 }
00317 
00318 
00319 topoSet::topoSet
00320 (
00321     const polyMesh& mesh,
00322     const word& wantedType,
00323     const word& name,
00324     readOption r,
00325     writeOption w
00326 )
00327 :
00328     regIOobject
00329     (
00330         IOobject
00331         (
00332             name,
00333             mesh.pointsInstance(),
00334             polyMesh::meshSubDir/"sets",
00335             mesh,
00336             r,
00337             w
00338         )
00339     )
00340 {
00341     if 
00342     (
00343         readOpt() == IOobject::MUST_READ
00344      || (
00345             readOpt() == IOobject::READ_IF_PRESENT
00346          && headerOk()
00347         )
00348     )
00349     {
00350         if (readStream(wantedType).good())
00351         {
00352             readStream(wantedType) >> static_cast<labelHashSet&>(*this);
00353 
00354             close();
00355         }
00356     }
00357 }
00358 
00359 
00360 topoSet::topoSet
00361 (
00362     const polyMesh& mesh,
00363     const word& name,
00364     const label size,
00365     writeOption w
00366 )
00367 :
00368     regIOobject
00369     (
00370         IOobject
00371         (
00372             name,
00373             mesh.pointsInstance(),
00374             polyMesh::meshSubDir/"sets",
00375             mesh,
00376             NO_READ,
00377             w
00378         )
00379     ),
00380     labelHashSet(size)
00381 {}
00382 
00383 
00384 topoSet::topoSet
00385 (
00386     const polyMesh& mesh,
00387     const word& name,
00388     const labelHashSet& set,
00389     writeOption w
00390 )
00391 :
00392     regIOobject
00393     (
00394         IOobject
00395         (
00396             name,
00397             mesh.pointsInstance(),
00398             polyMesh::meshSubDir/"sets",
00399             mesh,
00400             NO_READ,
00401             w
00402         )
00403     ),
00404     labelHashSet(set)
00405 {}
00406 
00407 
00408 topoSet::topoSet(const IOobject& obj, const label size)
00409 :
00410     regIOobject(obj),
00411     labelHashSet(size)
00412 {}
00413 
00414 
00415 topoSet::topoSet(const IOobject& obj, const labelHashSet& set)
00416 :
00417     regIOobject(obj),
00418     labelHashSet(set)
00419 {}
00420 
00421 
00422 
00423 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00424 
00425 topoSet::~topoSet()
00426 {}
00427 
00428 
00429 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00430 
00431 void topoSet::invert(const label maxLen)
00432 {
00433     // Keep copy of current set.
00434     labelHashSet currentSet(*this);
00435 
00436     clear();
00437     resize(2*(maxLen - currentSet.size()));
00438 
00439     for (label cellI = 0; cellI < maxLen; cellI++)
00440     {
00441         if (!currentSet.found(cellI))
00442         {
00443             insert(cellI);
00444         }
00445     }
00446     
00447 }
00448 
00449 
00450 void topoSet::subset(const topoSet& set)
00451 {
00452     // Keep copy of current set.
00453     labelHashSet currentSet(*this);
00454 
00455     clear();
00456     resize(2*min(currentSet.size(), set.size()));
00457 
00458     for
00459     (
00460         labelHashSet::const_iterator iter = currentSet.begin();
00461         iter != currentSet.end();
00462         ++iter
00463     )
00464     {
00465         if (set.found(iter.key()))
00466         {
00467             // element present in both currentSet and set.
00468             insert(iter.key());
00469         }
00470     }
00471 }
00472 
00473 
00474 void topoSet::addSet(const topoSet& set)
00475 {
00476     for
00477     (
00478         topoSet::const_iterator iter = set.begin();
00479         iter != set.end();
00480         ++iter
00481     )
00482     {
00483         insert(iter.key());
00484     }
00485 }
00486 
00487 
00488 void topoSet::deleteSet(const topoSet& set)
00489 {
00490     for
00491     (
00492         topoSet::const_iterator iter = set.begin();
00493         iter != set.end();
00494         ++iter
00495     )
00496     {
00497         erase(iter.key());
00498     }
00499 }
00500 
00501 
00502 void topoSet::sync(const polyMesh&)
00503 {
00504     notImplemented("topoSet::sync(const polyMesh&)");
00505 }
00506 
00507 
00508 void topoSet::writeDebug(Ostream& os, const label maxLen) const
00509 {
00510     label n = 0;
00511 
00512     topoSet::const_iterator iter = begin();
00513 
00514     if (size() <= maxLen)
00515     {
00516         writeDebug(os, maxLen, iter, n);
00517     }
00518     else
00519     {
00520         label halfLen = maxLen/2;
00521 
00522         os  << "Size larger than " << maxLen << ". Printing first and last "
00523             << halfLen << " elements:" << endl << endl;
00524 
00525         writeDebug(os, halfLen, iter, n);
00526 
00527         os<< endl
00528           << "  .." << endl
00529           << endl;
00530 
00531         for (; n < size() - halfLen; ++n)
00532         {
00533             ++iter;
00534         }
00535 
00536         writeDebug(os, halfLen, iter, n);
00537     }
00538 }
00539 
00540 
00541 void topoSet::writeDebug
00542 (
00543     Ostream&,
00544     const primitiveMesh&,
00545     const label
00546 ) const
00547 {
00548     notImplemented
00549     (
00550         "topoSet::writeDebug(Ostream&, const primitiveMesh&, const label)"
00551     );
00552 }
00553 
00554 
00555 bool topoSet::writeData(Ostream& os) const
00556 {
00557     return (os << *this).good();
00558 }
00559 
00560 
00561 void topoSet::updateMesh(const mapPolyMesh&)
00562 {
00563     notImplemented("topoSet::updateMesh(const mapPolyMesh&)");
00564 }
00565 
00566 
00567 //- Return max index+1.
00568 label topoSet::maxSize(const polyMesh&) const
00569 {
00570     notImplemented("topoSet::maxSize(const polyMesh&)");
00571 
00572     return -1;
00573 }
00574 
00575 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00576 
00577 void topoSet::operator=(const topoSet& rhs)
00578 {
00579     labelHashSet::operator=(rhs);
00580 }
00581 
00582 
00583 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00584 
00585 } // End namespace Foam
00586 
00587 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd