FreeFOAM The Cross-Platform CFD Toolkit
ZoneMesh.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 \*---------------------------------------------------------------------------*/
25 
26 #include "ZoneMesh.H"
27 #include <OpenFOAM/entry.H>
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 template<class ZoneType, class MeshType>
38 void ZoneMesh<ZoneType, MeshType>::calcZoneMap() const
39 {
40  // It is an error to attempt to recalculate cellEdges
41  // if the pointer is already set
42  if (zoneMapPtr_)
43  {
44  FatalErrorIn("void ZoneMesh<ZoneType>::calcZoneMap() const")
45  << "zone map already calculated"
46  << abort(FatalError);
47  }
48  else
49  {
50  // Count number of objects in all zones
51  label nObjects = 0;
52 
53  forAll (*this, zoneI)
54  {
55  nObjects += this->operator[](zoneI).size();
56  }
57 
58  zoneMapPtr_ = new Map<label>(2*nObjects);
59  Map<label>& zm = *zoneMapPtr_;
60 
61  // Fill in objects of all zones into the map. The key is the global
62  // object index and the result is the zone index
63  forAll (*this, zoneI)
64  {
65  const labelList& zoneObjects = this->operator[](zoneI);
66 
67  forAll (zoneObjects, objI)
68  {
69  zm.insert(zoneObjects[objI], zoneI);
70  }
71  }
72  }
73 }
74 
75 
76 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
77 
78 // Read constructor given IOobject and a MeshType reference
79 template<class ZoneType, class MeshType>
80 ZoneMesh<ZoneType, MeshType>::ZoneMesh
81 (
82  const IOobject& io,
83  const MeshType& mesh
84 )
85 :
87  regIOobject(io),
88  mesh_(mesh),
89  zoneMapPtr_(NULL)
90 {
91  if
92  (
93  readOpt() == IOobject::MUST_READ
94  || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
95  )
96  {
97  PtrList<ZoneType>& zones = *this;
98 
99  // Read zones
100  Istream& is = readStream(typeName);
101 
102  PtrList<entry> patchEntries(is);
103  zones.setSize(patchEntries.size());
104 
105  forAll(zones, zoneI)
106  {
107  zones.set
108  (
109  zoneI,
110  ZoneType::New
111  (
112  patchEntries[zoneI].keyword(),
113  patchEntries[zoneI].dict(),
114  zoneI,
115  *this
116  )
117  );
118  }
119 
120  // Check state of IOstream
121  is.check
122  (
123  "ZoneMesh::ZoneMesh"
124  "(const IOobject&, const MeshType&)"
125  );
126 
127  close();
128  }
129  else
130  {
131  // No files found. Force a write of zero-sized zones
132  // write();
133  }
134 }
135 
136 
137 // Construct given size. Zones will be set later
138 template<class ZoneType, class MeshType>
140 (
141  const IOobject& io,
142  const MeshType& mesh,
143  const label size
144 )
145 :
146  PtrList<ZoneType>(size),
147  regIOobject(io),
148  mesh_(mesh),
149  zoneMapPtr_(NULL)
150 {}
151 
152 
153 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
154 
155 template<class ZoneType, class MeshType>
157 {
158  clearAddressing();
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
163 
164 // Map of zones for quick zone lookup
165 template<class ZoneType, class MeshType>
167 {
168  if (!zoneMapPtr_)
169  {
170  calcZoneMap();
171  }
172 
173  return *zoneMapPtr_;
174 }
175 
176 
177 // Given a global object index, return the zone it is in.
178 // If object does not belong to any zones, return -1
179 template<class ZoneType, class MeshType>
180 label ZoneMesh<ZoneType, MeshType>::whichZone(const label objectIndex) const
181 {
182  const Map<label>& zm = zoneMap();
183  Map<label>::const_iterator zmIter = zm.find(objectIndex);
184 
185  if (zmIter == zm.end())
186  {
187  return -1;
188  }
189  else
190  {
191  return zmIter();
192  }
193 }
194 
195 
196 // Return a list of zone names
197 template<class ZoneType, class MeshType>
199 {
200  const PtrList<ZoneType>& zones = *this;
201 
202  wordList t(zones.size());
203 
204  forAll (zones, zoneI)
205  {
206  t[zoneI] = zones[zoneI].type();
207  }
208 
209  return t;
210 }
211 
212 
213 // Return a list of zone names
214 template<class ZoneType, class MeshType>
216 {
217  const PtrList<ZoneType>& zones = *this;
218 
219  wordList t(zones.size());
220 
221  forAll (zones, zoneI)
222  {
223  t[zoneI] = zones[zoneI].name();
224  }
225 
226  return t;
227 }
228 
229 
230 template<class ZoneType, class MeshType>
231 label ZoneMesh<ZoneType, MeshType>::findZoneID(const word& zoneName) const
232 {
233  const PtrList<ZoneType>& zones = *this;
234 
235  forAll (zones, zoneI)
236  {
237  if (zones[zoneI].name() == zoneName)
238  {
239  return zoneI;
240  }
241  }
242 
243  // Zone not found
244  if (debug)
245  {
246  Info<< "label ZoneMesh<ZoneType>::findZoneID(const word& "
247  << "zoneName) const : "
248  << "Zone named " << zoneName << " not found. "
249  << "List of available zone names: " << names() << endl;
250  }
251 
252  // A dummy return to kep the compiler happy
253  return -1;
254 }
255 
256 
257 template<class ZoneType, class MeshType>
259 {
260  deleteDemandDrivenData(zoneMapPtr_);
261 
262  PtrList<ZoneType>& zones = *this;
263 
264  forAll (zones, zoneI)
265  {
266  zones[zoneI].clearAddressing();
267  }
268 }
269 
270 
271 template<class ZoneType, class MeshType>
273 {
274  clearAddressing();
276 }
277 
278 
279 // Check zone definition
280 template<class ZoneType, class MeshType>
282 {
283  bool inError = false;
284 
285  const PtrList<ZoneType>& zones = *this;
286 
287  forAll (zones, zoneI)
288  {
289  inError |= zones[zoneI].checkDefinition(report);
290  }
291  return inError;
292 }
293 
294 
295 // Correct zone mesh after moving points
296 template<class ZoneType, class MeshType>
298 {
299  PtrList<ZoneType>& zones = *this;
300 
301  forAll (zones, zoneI)
302  {
303  zones[zoneI].movePoints(p);
304  }
305 }
306 
307 
308 // writeData member function required by regIOobject
309 template<class ZoneType, class MeshType>
311 {
312  os << *this;
313  return os.good();
314 }
315 
316 
317 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
318 
319 template<class ZoneType, class MeshType>
320 Ostream& operator<<(Ostream& os, const ZoneMesh<ZoneType, MeshType>& zones)
321 {
322  os << zones.size() << nl << token::BEGIN_LIST;
323 
324  forAll(zones, zoneI)
325  {
326  zones[zoneI].writeDict(os);
327  }
328 
329  os << token::END_LIST;
330 
331  return os;
332 }
333 
334 
335 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
336 
337 } // End namespace Foam
338 
339 // ************************ vim: set sw=4 sts=4 et: ************************ //