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

cellZone.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 Description
00025     A subset of mesh cells.
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "cellZone.H"
00030 #include <OpenFOAM/addToRunTimeSelectionTable.H>
00031 #include <OpenFOAM/cellZoneMesh.H>
00032 #include <OpenFOAM/polyMesh.H>
00033 #include <OpenFOAM/primitiveMesh.H>
00034 #include <OpenFOAM/IOstream.H>
00035 #include <OpenFOAM/demandDrivenData.H>
00036 
00037 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00038 
00039 namespace Foam
00040 {
00041     defineTypeNameAndDebug(cellZone, 0);
00042 
00043     defineRunTimeSelectionTable(cellZone, dictionary);
00044     addToRunTimeSelectionTable(cellZone, cellZone, dictionary);
00045 }
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 const Foam::Map<Foam::label>& Foam::cellZone::cellLookupMap() const
00050 {
00051     if (!cellLookupMapPtr_)
00052     {
00053         calcCellLookupMap();
00054     }
00055 
00056     return *cellLookupMapPtr_;
00057 }
00058 
00059 
00060 void Foam::cellZone::calcCellLookupMap() const
00061 {
00062     if (debug)
00063     {
00064         Info<< "void cellZone::calcCellLookupMap() const : "
00065             << "Calculating cell lookup map"
00066             << endl;
00067     }
00068 
00069     if (cellLookupMapPtr_)
00070     {
00071         FatalErrorIn
00072         (
00073             "void cellZone::calcCellLookupMap() const"
00074         )   << "cell lookup map already calculated"
00075             << abort(FatalError);
00076     }
00077 
00078     const labelList& addr = *this;
00079 
00080     cellLookupMapPtr_ = new Map<label>(2*addr.size());
00081     Map<label>& clm = *cellLookupMapPtr_;
00082 
00083     forAll (addr, cellI)
00084     {
00085         clm.insert(addr[cellI], cellI);
00086     }
00087 
00088     if (debug)
00089     {
00090         Info<< "void cellZone::calcCellLookupMap() const : "
00091             << "Finished calculating cell lookup map"
00092             << endl;
00093     }
00094 }
00095 
00096 
00097 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00098 
00099 // Construct from components
00100 Foam::cellZone::cellZone
00101 (
00102     const word& name,
00103     const labelList& addr,
00104     const label index,
00105     const cellZoneMesh& zm
00106 )
00107 :
00108     labelList(addr),
00109     name_(name),
00110     index_(index),
00111     zoneMesh_(zm),
00112     cellLookupMapPtr_(NULL)
00113 {}
00114 
00115 
00116 Foam::cellZone::cellZone
00117 (
00118     const word& name,
00119     const Xfer<labelList>& addr,
00120     const label index,
00121     const cellZoneMesh& zm
00122 )
00123 :
00124     labelList(addr),
00125     name_(name),
00126     index_(index),
00127     zoneMesh_(zm),
00128     cellLookupMapPtr_(NULL)
00129 {}
00130 
00131 
00132 // Construct from dictionary
00133 Foam::cellZone::cellZone
00134 (
00135     const word& name,
00136     const dictionary& dict,
00137     const label index,
00138     const cellZoneMesh& zm
00139 )
00140 :
00141     labelList(dict.lookup("cellLabels")),
00142     name_(name),
00143     index_(index),
00144     zoneMesh_(zm),
00145     cellLookupMapPtr_(NULL)
00146 {}
00147 
00148 
00149 // Construct given the original zone and resetting the
00150 //  cell list and zone mesh information
00151 Foam::cellZone::cellZone
00152 (
00153     const cellZone& cz,
00154     const labelList& addr,
00155     const label index,
00156     const cellZoneMesh& zm
00157 )
00158 :
00159     labelList(addr),
00160     name_(cz.name()),
00161     index_(index),
00162     zoneMesh_(zm),
00163     cellLookupMapPtr_(NULL)
00164 {}
00165 
00166 Foam::cellZone::cellZone
00167 (
00168     const cellZone& cz,
00169     const Xfer<labelList>& addr,
00170     const label index,
00171     const cellZoneMesh& zm
00172 )
00173 :
00174     labelList(addr),
00175     name_(cz.name()),
00176     index_(index),
00177     zoneMesh_(zm),
00178     cellLookupMapPtr_(NULL)
00179 {}
00180 
00181 
00182 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00183 
00184 Foam::cellZone::~cellZone()
00185 {
00186     clearAddressing();
00187 }
00188 
00189 
00190 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00191 
00192 Foam::label Foam::cellZone::whichCell(const label globalCellID) const
00193 {
00194     const Map<label>& clm = cellLookupMap();
00195 
00196     Map<label>::const_iterator clmIter = clm.find(globalCellID);
00197 
00198     if (clmIter == clm.end())
00199     {
00200         return -1;
00201     }
00202     else
00203     {
00204         return clmIter();
00205     }
00206 }
00207 
00208 
00209 const Foam::cellZoneMesh& Foam::cellZone::zoneMesh() const
00210 {
00211     return zoneMesh_;
00212 }
00213 
00214 
00215 void Foam::cellZone::clearAddressing()
00216 {
00217     deleteDemandDrivenData(cellLookupMapPtr_);
00218 }
00219 
00220 
00221 bool Foam::cellZone::checkDefinition(const bool report) const
00222 {
00223     const labelList& addr = *this;
00224 
00225     bool boundaryError = false;
00226 
00227     forAll(addr, i)
00228     {
00229         if (addr[i] < 0 || addr[i] >= zoneMesh_.mesh().nCells())
00230         {
00231             boundaryError = true;
00232 
00233             if (report)
00234             {
00235                 SeriousErrorIn
00236                 (
00237                     "bool cellZone::checkDefinition("
00238                     "const bool report) const"
00239                 )   << "Zone " << name()
00240                     << " contains invalid cell label " << addr[i] << nl
00241                     << "Valid cell labels are 0.."
00242                     << zoneMesh_.mesh().nCells()-1 << endl;
00243             }
00244         }
00245     }
00246     return boundaryError;
00247 }
00248 
00249 
00250 void Foam::cellZone::write(Ostream& os) const
00251 {
00252     os  << nl << name()
00253         << nl << static_cast<const labelList&>(*this);
00254 }
00255 
00256 
00257 void Foam::cellZone::writeDict(Ostream& os) const
00258 {
00259     os  << nl << name() << nl << token::BEGIN_BLOCK << nl
00260         << "    type " << type() << token::END_STATEMENT << nl;
00261 
00262     writeEntry("cellLabels", os);
00263 
00264     os  << token::END_BLOCK << endl;
00265 }
00266 
00267 
00268 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00269 
00270 void Foam::cellZone::operator=(const cellZone& cz)
00271 {
00272     clearAddressing();
00273     labelList::operator=(cz);
00274 }
00275 
00276 
00277 void Foam::cellZone::operator=(const labelList& addr)
00278 {
00279     clearAddressing();
00280     labelList::operator=(addr);
00281 }
00282 
00283 
00284 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
00285 
00286 Foam::Ostream& Foam::operator<<(Ostream& os, const cellZone& p)
00287 {
00288     p.write(os);
00289     os.check("Ostream& operator<<(Ostream& f, const cellZone& p");
00290     return os;
00291 }
00292 
00293 
00294 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines