initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / meshes / polyMesh / zones / cellZone / cellZone.C
blobff8041fc602e4d7d24fdeed6d2a70c310aa52cd9
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 Description
26     A subset of mesh cells.
28 \*---------------------------------------------------------------------------*/
30 #include "cellZone.H"
31 #include "addToRunTimeSelectionTable.H"
32 #include "cellZoneMesh.H"
33 #include "polyMesh.H"
34 #include "primitiveMesh.H"
35 #include "IOstream.H"
36 #include "demandDrivenData.H"
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 namespace Foam
42     defineTypeNameAndDebug(cellZone, 0);
44     defineRunTimeSelectionTable(cellZone, dictionary);
45     addToRunTimeSelectionTable(cellZone, cellZone, dictionary);
48 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
50 const Foam::Map<Foam::label>& Foam::cellZone::cellLookupMap() const
52     if (!cellLookupMapPtr_)
53     {
54         calcCellLookupMap();
55     }
57     return *cellLookupMapPtr_;
61 void Foam::cellZone::calcCellLookupMap() const
63     if (debug)
64     {
65         Info<< "void cellZone::calcCellLookupMap() const : "
66             << "Calculating cell lookup map"
67             << endl;
68     }
70     if (cellLookupMapPtr_)
71     {
72         FatalErrorIn
73         (
74             "void cellZone::calcCellLookupMap() const"
75         )   << "cell lookup map already calculated"
76             << abort(FatalError);
77     }
79     const labelList& addr = *this;
81     cellLookupMapPtr_ = new Map<label>(2*addr.size());
82     Map<label>& clm = *cellLookupMapPtr_;
84     forAll (addr, cellI)
85     {
86         clm.insert(addr[cellI], cellI);
87     }
89     if (debug)
90     {
91         Info<< "void cellZone::calcCellLookupMap() const : "
92             << "Finished calculating cell lookup map"
93             << endl;
94     }
98 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
100 // Construct from components
101 Foam::cellZone::cellZone
103     const word& name,
104     const labelList& addr,
105     const label index,
106     const cellZoneMesh& zm
109     labelList(addr),
110     name_(name),
111     index_(index),
112     zoneMesh_(zm),
113     cellLookupMapPtr_(NULL)
117 Foam::cellZone::cellZone
119     const word& name,
120     const Xfer<labelList>& addr,
121     const label index,
122     const cellZoneMesh& zm
125     labelList(addr),
126     name_(name),
127     index_(index),
128     zoneMesh_(zm),
129     cellLookupMapPtr_(NULL)
133 // Construct from dictionary
134 Foam::cellZone::cellZone
136     const word& name,
137     const dictionary& dict,
138     const label index,
139     const cellZoneMesh& zm
142     labelList(dict.lookup("cellLabels")),
143     name_(name),
144     index_(index),
145     zoneMesh_(zm),
146     cellLookupMapPtr_(NULL)
150 // Construct given the original zone and resetting the
151 //  cell list and zone mesh information
152 Foam::cellZone::cellZone
154     const cellZone& cz,
155     const labelList& addr,
156     const label index,
157     const cellZoneMesh& zm
160     labelList(addr),
161     name_(cz.name()),
162     index_(index),
163     zoneMesh_(zm),
164     cellLookupMapPtr_(NULL)
167 Foam::cellZone::cellZone
169     const cellZone& cz,
170     const Xfer<labelList>& addr,
171     const label index,
172     const cellZoneMesh& zm
175     labelList(addr),
176     name_(cz.name()),
177     index_(index),
178     zoneMesh_(zm),
179     cellLookupMapPtr_(NULL)
183 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
185 Foam::cellZone::~cellZone()
187     clearAddressing();
191 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
193 Foam::label Foam::cellZone::whichCell(const label globalCellID) const
195     const Map<label>& clm = cellLookupMap();
197     Map<label>::const_iterator clmIter = clm.find(globalCellID);
199     if (clmIter == clm.end())
200     {
201         return -1;
202     }
203     else
204     {
205         return clmIter();
206     }
210 const Foam::cellZoneMesh& Foam::cellZone::zoneMesh() const
212     return zoneMesh_;
216 void Foam::cellZone::clearAddressing()
218     deleteDemandDrivenData(cellLookupMapPtr_);
222 bool Foam::cellZone::checkDefinition(const bool report) const
224     const labelList& addr = *this;
226     bool boundaryError = false;
228     forAll(addr, i)
229     {
230         if (addr[i] < 0 || addr[i] >= zoneMesh_.mesh().nCells())
231         {
232             boundaryError = true;
234             if (report)
235             {
236                 SeriousErrorIn
237                 (
238                     "bool cellZone::checkDefinition("
239                     "const bool report) const"
240                 )   << "Zone " << name()
241                     << " contains invalid cell label " << addr[i] << nl
242                     << "Valid cell labels are 0.."
243                     << zoneMesh_.mesh().nCells()-1 << endl;
244             }
245         }
246     }
247     return boundaryError;
251 void Foam::cellZone::write(Ostream& os) const
253     os  << nl << name()
254         << nl << static_cast<const labelList&>(*this);
258 void Foam::cellZone::writeDict(Ostream& os) const
260     os  << nl << name() << nl << token::BEGIN_BLOCK << nl
261         << "    type " << type() << token::END_STATEMENT << nl;
263     writeEntry("cellLabels", os);
265     os  << token::END_BLOCK << endl;
269 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
271 void Foam::cellZone::operator=(const cellZone& cz)
273     clearAddressing();
274     labelList::operator=(cz);
278 void Foam::cellZone::operator=(const labelList& addr)
280     clearAddressing();
281     labelList::operator=(addr);
285 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
287 Foam::Ostream& Foam::operator<<(Ostream& os, const cellZone& p)
289     p.write(os);
290     os.check("Ostream& operator<<(Ostream& f, const cellZone& p");
291     return os;
295 // ************************************************************************* //