FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "topoSet.H"
00027 #include <OpenFOAM/mapPolyMesh.H>
00028 #include <OpenFOAM/polyMesh.H>
00029 #include <OpenFOAM/boundBox.H>
00030 
00031 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035 
00036 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00037 
00038 defineTypeNameAndDebug(topoSet, 0);
00039 defineRunTimeSelectionTable(topoSet, word);
00040 defineRunTimeSelectionTable(topoSet, size);
00041 defineRunTimeSelectionTable(topoSet, set);
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_->sortedToc()
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_->sortedToc()
00099             << exit(FatalError);
00100     }
00101 
00102     return autoPtr<topoSet>(cstrIter()(mesh, name, size, w));
00103 }
00104 
00105 
00106 // Construct named object from existing set.
00107 autoPtr<topoSet> topoSet::New
00108 (
00109     const word& setType,
00110     const polyMesh& mesh,
00111     const word& name,
00112     const topoSet& set,
00113     writeOption w
00114 )
00115 {
00116     setConstructorTable::iterator cstrIter =
00117         setConstructorTablePtr_
00118             ->find(setType);
00119 
00120     if (cstrIter == setConstructorTablePtr_->end())
00121     {
00122         FatalErrorIn
00123         (
00124             "topoSet::New(const word&, "
00125             "const polyMesh&, const word&, const topoSet&, writeOption)"
00126         )   << "Unknown set type " << setType
00127             << endl << endl
00128             << "Valid set types : " << endl
00129             << setConstructorTablePtr_->sortedToc()
00130             << exit(FatalError);
00131     }
00132 
00133     return autoPtr<topoSet>(cstrIter()(mesh, name, set, w));
00134 }
00135 
00136 
00137 Foam::fileName topoSet::topoSet::localPath
00138 (
00139     const polyMesh& mesh,
00140     const word& name
00141 )
00142 {
00143     return mesh.pointsInstance()/polyMesh::meshSubDir/"sets"/name;
00144 }
00145 
00146 
00147 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00148 
00149 // Update stored cell numbers using map.
00150 // Do in two passes to prevent allocation if nothing changed.
00151 void topoSet::topoSet::updateLabels(const labelList& map)
00152 {
00153     // Iterate over map to see if anything changed
00154     bool changed = false;
00155 
00156     for
00157     (
00158         labelHashSet::const_iterator iter = begin();
00159         iter != end();
00160         ++iter
00161     )
00162     {
00163         if ((iter.key() < 0) || (iter.key() > map.size()))
00164         {
00165             FatalErrorIn
00166             (
00167                 "topoSet::updateLabels(const labelList&, labelHashSet)"
00168             )   << "Illegal content " << iter.key() << " of set:" << name()
00169                 << " of type " << type() << endl
00170                 << "Value should be between 0 and " << map.size()-1
00171                 << abort(FatalError);
00172         }
00173 
00174         label newCellI = map[iter.key()];
00175 
00176         if (newCellI != iter.key())
00177         {
00178             changed = true;
00179 
00180             break;
00181         }
00182     }
00183 
00184     // Relabel (use second Map to prevent overlapping)
00185     if (changed)
00186     {
00187         labelHashSet newSet(2*size());
00188 
00189         for
00190         (
00191             labelHashSet::const_iterator iter = begin();
00192             iter != end();
00193             ++iter
00194         )
00195         {
00196             label newCellI = map[iter.key()];
00197 
00198             if (newCellI >= 0)
00199             {
00200                 newSet.insert(newCellI);
00201             }
00202         }
00203 
00204         transfer(newSet);
00205     }
00206 }
00207 
00208 
00209 void topoSet::topoSet::check(const label maxLabel)
00210 {
00211     for
00212     (
00213         topoSet::const_iterator iter = begin();
00214         iter != end();
00215         ++iter
00216     )
00217     {
00218         if ((iter.key() < 0) || (iter.key() > maxLabel))
00219         {
00220             FatalErrorIn("topoSet::check(const label)")
00221                 << "Illegal content " << iter.key() << " of set:" << name()
00222                 << " of type " << type() << endl
00223                 << "Value should be between 0 and " << maxLabel
00224                 << abort(FatalError);
00225         }
00226     }
00227 }
00228 
00229 
00230 // Write maxElem elements, starting at iter. Updates iter and elemI.
00231 void topoSet::writeDebug
00232 (
00233     Ostream& os,
00234     const label maxElem,
00235     topoSet::const_iterator& iter,
00236     label& elemI
00237 ) const
00238 {
00239     label n = 0;
00240 
00241     for (; (iter != end()) && (n < maxElem); ++iter)
00242     {
00243         if ((n != 0) && ((n % 10) == 0))
00244         {
00245             os << endl;
00246         }
00247         os << iter.key() << ' ';
00248 
00249         n++;
00250         elemI++;
00251     }
00252 }
00253 
00254 
00255 // Write maxElem elements, starting at iter. Updates iter and elemI.
00256 void topoSet::writeDebug
00257 (
00258     Ostream& os,
00259     const pointField& coords,
00260     const label maxElem,
00261     topoSet::const_iterator& iter,
00262     label& elemI
00263 ) const
00264 {
00265     label n = 0;
00266 
00267     for (; (iter != end()) && (n < maxElem); ++iter)
00268     {
00269         if ((n != 0) && ((n % 3) == 0))
00270         {
00271             os << endl;
00272         }
00273         os << iter.key() << coords[iter.key()] << ' ';
00274 
00275         n++;
00276         elemI++;
00277     }
00278 }
00279 
00280 
00281 void topoSet::writeDebug
00282 (
00283     Ostream& os,
00284     const pointField& coords,
00285     const label maxLen
00286 ) const
00287 {
00288     // Bounding box of contents.
00289     boundBox bb(pointField(coords, toc()), true);
00290 
00291     os  << "Set bounding box: min = "
00292         << bb.min() << "    max = " << bb.max() << " meters. " << endl << endl;
00293 
00294     label n = 0;
00295 
00296     topoSet::const_iterator iter = begin();
00297 
00298     if (size() <= maxLen)
00299     {
00300         writeDebug(os, coords, maxLen, iter, n);
00301     }
00302     else
00303     {
00304         label halfLen = maxLen/2;
00305 
00306         os  << "Size larger than " << maxLen << ". Printing first and last "
00307             << halfLen << " elements:" << endl << endl;
00308 
00309         writeDebug(os, coords, halfLen, iter, n);
00310 
00311         os<< endl
00312           << "  .." << endl
00313           << endl;
00314 
00315         for (; n < size() - halfLen; ++n)
00316         {
00317             ++iter;
00318         }
00319 
00320         writeDebug(os, coords, halfLen, iter, n);
00321     }
00322 }
00323 
00324 
00325 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00326 
00327 topoSet::topoSet(const IOobject& obj, const word& wantedType)
00328 :
00329     regIOobject(obj)
00330 {
00331     if
00332     (
00333         readOpt() == IOobject::MUST_READ
00334      || (
00335             readOpt() == IOobject::READ_IF_PRESENT
00336          && headerOk()
00337         )
00338     )
00339     {
00340         if (readStream(wantedType).good())
00341         {
00342             readStream(wantedType) >> static_cast<labelHashSet&>(*this);
00343 
00344             close();
00345         }
00346     }
00347 }
00348 
00349 
00350 topoSet::topoSet
00351 (
00352     const polyMesh& mesh,
00353     const word& wantedType,
00354     const word& name,
00355     readOption r,
00356     writeOption w
00357 )
00358 :
00359     regIOobject
00360     (
00361         IOobject
00362         (
00363             name,
00364             mesh.pointsInstance(),
00365             polyMesh::meshSubDir/"sets",
00366             mesh,
00367             r,
00368             w
00369         )
00370     )
00371 {
00372     if
00373     (
00374         readOpt() == IOobject::MUST_READ
00375      || (
00376             readOpt() == IOobject::READ_IF_PRESENT
00377          && headerOk()
00378         )
00379     )
00380     {
00381         if (readStream(wantedType).good())
00382         {
00383             readStream(wantedType) >> static_cast<labelHashSet&>(*this);
00384 
00385             close();
00386         }
00387     }
00388 }
00389 
00390 
00391 topoSet::topoSet
00392 (
00393     const polyMesh& mesh,
00394     const word& name,
00395     const label size,
00396     writeOption w
00397 )
00398 :
00399     regIOobject
00400     (
00401         IOobject
00402         (
00403             name,
00404             mesh.pointsInstance(),
00405             polyMesh::meshSubDir/"sets",
00406             mesh,
00407             NO_READ,
00408             w
00409         )
00410     ),
00411     labelHashSet(size)
00412 {}
00413 
00414 
00415 topoSet::topoSet
00416 (
00417     const polyMesh& mesh,
00418     const word& name,
00419     const labelHashSet& set,
00420     writeOption w
00421 )
00422 :
00423     regIOobject
00424     (
00425         IOobject
00426         (
00427             name,
00428             mesh.pointsInstance(),
00429             polyMesh::meshSubDir/"sets",
00430             mesh,
00431             NO_READ,
00432             w
00433         )
00434     ),
00435     labelHashSet(set)
00436 {}
00437 
00438 
00439 topoSet::topoSet(const IOobject& obj, const label size)
00440 :
00441     regIOobject(obj),
00442     labelHashSet(size)
00443 {}
00444 
00445 
00446 topoSet::topoSet(const IOobject& obj, const labelHashSet& set)
00447 :
00448     regIOobject(obj),
00449     labelHashSet(set)
00450 {}
00451 
00452 
00453 
00454 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00455 
00456 topoSet::~topoSet()
00457 {}
00458 
00459 
00460 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00461 
00462 void topoSet::invert(const label maxLen)
00463 {
00464     // Keep copy of current set.
00465     labelHashSet currentSet(*this);
00466 
00467     clear();
00468     resize(2*(maxLen - currentSet.size()));
00469 
00470     for (label cellI = 0; cellI < maxLen; cellI++)
00471     {
00472         if (!currentSet.found(cellI))
00473         {
00474             insert(cellI);
00475         }
00476     }
00477 
00478 }
00479 
00480 
00481 void topoSet::subset(const topoSet& set)
00482 {
00483     // Keep copy of current set.
00484     labelHashSet currentSet(*this);
00485 
00486     clear();
00487     resize(2*min(currentSet.size(), set.size()));
00488 
00489     for
00490     (
00491         labelHashSet::const_iterator iter = currentSet.begin();
00492         iter != currentSet.end();
00493         ++iter
00494     )
00495     {
00496         if (set.found(iter.key()))
00497         {
00498             // element present in both currentSet and set.
00499             insert(iter.key());
00500         }
00501     }
00502 }
00503 
00504 
00505 void topoSet::addSet(const topoSet& set)
00506 {
00507     for
00508     (
00509         topoSet::const_iterator iter = set.begin();
00510         iter != set.end();
00511         ++iter
00512     )
00513     {
00514         insert(iter.key());
00515     }
00516 }
00517 
00518 
00519 void topoSet::deleteSet(const topoSet& set)
00520 {
00521     for
00522     (
00523         topoSet::const_iterator iter = set.begin();
00524         iter != set.end();
00525         ++iter
00526     )
00527     {
00528         erase(iter.key());
00529     }
00530 }
00531 
00532 
00533 void topoSet::sync(const polyMesh&)
00534 {
00535     notImplemented("topoSet::sync(const polyMesh&)");
00536 }
00537 
00538 
00539 void topoSet::writeDebug(Ostream& os, const label maxLen) const
00540 {
00541     label n = 0;
00542 
00543     topoSet::const_iterator iter = begin();
00544 
00545     if (size() <= maxLen)
00546     {
00547         writeDebug(os, maxLen, iter, n);
00548     }
00549     else
00550     {
00551         label halfLen = maxLen/2;
00552 
00553         os  << "Size larger than " << maxLen << ". Printing first and last "
00554             << halfLen << " elements:" << endl << endl;
00555 
00556         writeDebug(os, halfLen, iter, n);
00557 
00558         os<< endl
00559           << "  .." << endl
00560           << endl;
00561 
00562         for (; n < size() - halfLen; ++n)
00563         {
00564             ++iter;
00565         }
00566 
00567         writeDebug(os, halfLen, iter, n);
00568     }
00569 }
00570 
00571 
00572 void topoSet::writeDebug
00573 (
00574     Ostream&,
00575     const primitiveMesh&,
00576     const label
00577 ) const
00578 {
00579     notImplemented
00580     (
00581         "topoSet::writeDebug(Ostream&, const primitiveMesh&, const label)"
00582     );
00583 }
00584 
00585 
00586 bool topoSet::writeData(Ostream& os) const
00587 {
00588     return (os << *this).good();
00589 }
00590 
00591 
00592 void topoSet::updateMesh(const mapPolyMesh&)
00593 {
00594     notImplemented("topoSet::updateMesh(const mapPolyMesh&)");
00595 }
00596 
00597 
00598 //- Return max index+1.
00599 label topoSet::maxSize(const polyMesh&) const
00600 {
00601     notImplemented("topoSet::maxSize(const polyMesh&)");
00602 
00603     return -1;
00604 }
00605 
00606 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00607 
00608 void topoSet::operator=(const topoSet& rhs)
00609 {
00610     labelHashSet::operator=(rhs);
00611 }
00612 
00613 
00614 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00615 
00616 } // End namespace Foam
00617 
00618 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines