FreeFOAM The Cross-Platform CFD Toolkit
cellZone.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  A subset of mesh cells.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "cellZone.H"
31 #include <OpenFOAM/cellZoneMesh.H>
32 #include <OpenFOAM/polyMesh.H>
33 #include <OpenFOAM/primitiveMesh.H>
34 #include <OpenFOAM/IOstream.H>
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41  defineTypeNameAndDebug(cellZone, 0);
42 
43  defineRunTimeSelectionTable(cellZone, dictionary);
44  addToRunTimeSelectionTable(cellZone, cellZone, dictionary);
45 }
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 const Foam::Map<Foam::label>& Foam::cellZone::cellLookupMap() const
50 {
51  if (!cellLookupMapPtr_)
52  {
53  calcCellLookupMap();
54  }
55 
56  return *cellLookupMapPtr_;
57 }
58 
59 
60 void Foam::cellZone::calcCellLookupMap() const
61 {
62  if (debug)
63  {
64  Info<< "void cellZone::calcCellLookupMap() const : "
65  << "Calculating cell lookup map"
66  << endl;
67  }
68 
69  if (cellLookupMapPtr_)
70  {
72  (
73  "void cellZone::calcCellLookupMap() const"
74  ) << "cell lookup map already calculated"
75  << abort(FatalError);
76  }
77 
78  const labelList& addr = *this;
79 
80  cellLookupMapPtr_ = new Map<label>(2*addr.size());
81  Map<label>& clm = *cellLookupMapPtr_;
82 
83  forAll (addr, cellI)
84  {
85  clm.insert(addr[cellI], cellI);
86  }
87 
88  if (debug)
89  {
90  Info<< "void cellZone::calcCellLookupMap() const : "
91  << "Finished calculating cell lookup map"
92  << endl;
93  }
94 }
95 
96 
97 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98 
99 // Construct from components
100 Foam::cellZone::cellZone
101 (
102  const word& name,
103  const labelList& addr,
104  const label index,
105  const cellZoneMesh& zm
106 )
107 :
108  labelList(addr),
109  name_(name),
110  index_(index),
111  zoneMesh_(zm),
112  cellLookupMapPtr_(NULL)
113 {}
114 
115 
116 Foam::cellZone::cellZone
117 (
118  const word& name,
119  const Xfer<labelList>& addr,
120  const label index,
121  const cellZoneMesh& zm
122 )
123 :
124  labelList(addr),
125  name_(name),
126  index_(index),
127  zoneMesh_(zm),
128  cellLookupMapPtr_(NULL)
129 {}
130 
131 
132 // Construct from dictionary
133 Foam::cellZone::cellZone
134 (
135  const word& name,
136  const dictionary& dict,
137  const label index,
138  const cellZoneMesh& zm
139 )
140 :
141  labelList(dict.lookup("cellLabels")),
142  name_(name),
143  index_(index),
144  zoneMesh_(zm),
145  cellLookupMapPtr_(NULL)
146 {}
147 
148 
149 // Construct given the original zone and resetting the
150 // cell list and zone mesh information
151 Foam::cellZone::cellZone
152 (
153  const cellZone& cz,
154  const labelList& addr,
155  const label index,
156  const cellZoneMesh& zm
157 )
158 :
159  labelList(addr),
160  name_(cz.name()),
161  index_(index),
162  zoneMesh_(zm),
163  cellLookupMapPtr_(NULL)
164 {}
165 
166 Foam::cellZone::cellZone
167 (
168  const cellZone& cz,
169  const Xfer<labelList>& addr,
170  const label index,
171  const cellZoneMesh& zm
172 )
173 :
174  labelList(addr),
175  name_(cz.name()),
176  index_(index),
177  zoneMesh_(zm),
178  cellLookupMapPtr_(NULL)
179 {}
180 
181 
182 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
183 
185 {
186  clearAddressing();
187 }
188 
189 
190 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
191 
192 Foam::label Foam::cellZone::whichCell(const label globalCellID) const
193 {
194  const Map<label>& clm = cellLookupMap();
195 
196  Map<label>::const_iterator clmIter = clm.find(globalCellID);
197 
198  if (clmIter == clm.end())
199  {
200  return -1;
201  }
202  else
203  {
204  return clmIter();
205  }
206 }
207 
208 
210 {
211  return zoneMesh_;
212 }
213 
214 
216 {
217  deleteDemandDrivenData(cellLookupMapPtr_);
218 }
219 
220 
221 bool Foam::cellZone::checkDefinition(const bool report) const
222 {
223  const labelList& addr = *this;
224 
225  bool boundaryError = false;
226 
227  forAll(addr, i)
228  {
229  if (addr[i] < 0 || addr[i] >= zoneMesh_.mesh().nCells())
230  {
231  boundaryError = true;
232 
233  if (report)
234  {
236  (
237  "bool cellZone::checkDefinition("
238  "const bool report) const"
239  ) << "Zone " << name()
240  << " contains invalid cell label " << addr[i] << nl
241  << "Valid cell labels are 0.."
242  << zoneMesh_.mesh().nCells()-1 << endl;
243  }
244  }
245  }
246  return boundaryError;
247 }
248 
249 
251 {
252  os << nl << name()
253  << nl << static_cast<const labelList&>(*this);
254 }
255 
256 
258 {
259  os << nl << name() << nl << token::BEGIN_BLOCK << nl
260  << " type " << type() << token::END_STATEMENT << nl;
261 
262  writeEntry("cellLabels", os);
263 
264  os << token::END_BLOCK << endl;
265 }
266 
267 
268 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
269 
271 {
272  clearAddressing();
274 }
275 
276 
278 {
279  clearAddressing();
280  labelList::operator=(addr);
281 }
282 
283 
284 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
285 
287 {
288  p.write(os);
289  os.check("Ostream& operator<<(Ostream& f, const cellZone& p");
290  return os;
291 }
292 
293 
294 // ************************ vim: set sw=4 sts=4 et: ************************ //