initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / meshes / polyMesh / zones / ZoneMesh / ZoneMesh.C
blobd7ad6945b8051fd60b34aa1e22d1a06aeb82fe13
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 \*---------------------------------------------------------------------------*/
27 #include "ZoneMesh.H"
28 #include "entry.H"
29 #include "demandDrivenData.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
38 template<class ZoneType, class MeshType>
39 void ZoneMesh<ZoneType, MeshType>::calcZoneMap() const
41     // It is an error to attempt to recalculate cellEdges
42     // if the pointer is already set
43     if (zoneMapPtr_)
44     {
45         FatalErrorIn("void ZoneMesh<ZoneType>::calcZoneMap() const")
46             << "zone map already calculated"
47             << abort(FatalError);
48     }
49     else
50     {
51         // Count number of objects in all zones
52         label nObjects = 0;
54         forAll (*this, zoneI)
55         {
56             nObjects += this->operator[](zoneI).size();
57         }
59         zoneMapPtr_ = new Map<label>(2*nObjects);
60         Map<label>& zm = *zoneMapPtr_;
62         // Fill in objects of all zones into the map.  The key is the global
63         // object index and the result is the zone index
64         forAll (*this, zoneI)
65         {
66             const labelList& zoneObjects = this->operator[](zoneI);
68             forAll (zoneObjects, objI)
69             {
70                 zm.insert(zoneObjects[objI], zoneI);
71             }
72         }
73     }
77 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
79 // Read constructor given IOobject and a MeshType reference
80 template<class ZoneType, class MeshType>
81 ZoneMesh<ZoneType, MeshType>::ZoneMesh
83     const IOobject& io,
84     const MeshType& mesh
87     PtrList<ZoneType>(),
88     regIOobject(io),
89     mesh_(mesh),
90     zoneMapPtr_(NULL)
92     if
93     (
94         readOpt() == IOobject::MUST_READ
95      || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
96     )
97     {
98         PtrList<ZoneType>& zones = *this;
100         // Read zones
101         Istream& is = readStream(typeName);
103         PtrList<entry> patchEntries(is);
104         zones.setSize(patchEntries.size());
106         forAll(zones, zoneI)
107         {
108             zones.set
109             (
110                 zoneI,
111                 ZoneType::New
112                 (
113                     patchEntries[zoneI].keyword(),
114                     patchEntries[zoneI].dict(),
115                     zoneI,
116                     *this
117                 )
118             );
119         }
121         // Check state of IOstream
122         is.check
123         (
124             "ZoneMesh::ZoneMesh"
125             "(const IOobject&, const MeshType&)"
126         );
128         close();
129     }
130     else
131     {
132         // No files found.  Force a write of zero-sized zones
133         // write();
134     }
138 // Construct given size. Zones will be set later
139 template<class ZoneType, class MeshType>
140 ZoneMesh<ZoneType, MeshType>::ZoneMesh
142     const IOobject& io,
143     const MeshType& mesh,
144     const label size
147     PtrList<ZoneType>(size),
148     regIOobject(io),
149     mesh_(mesh),
150     zoneMapPtr_(NULL)
154 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
156 template<class ZoneType, class MeshType>
157 ZoneMesh<ZoneType, MeshType>::~ZoneMesh()
159     clearAddressing();
163 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
165 // Map of zones for quick zone lookup
166 template<class ZoneType, class MeshType>
167 const Map<label>& ZoneMesh<ZoneType, MeshType>::zoneMap() const
169     if (!zoneMapPtr_)
170     {
171         calcZoneMap();
172     }
174     return *zoneMapPtr_;
178 // Given a global object index, return the zone it is in.
179 // If object does not belong to any zones, return -1
180 template<class ZoneType, class MeshType>
181 label ZoneMesh<ZoneType, MeshType>::whichZone(const label objectIndex) const
183     const Map<label>& zm = zoneMap();
184     Map<label>::const_iterator zmIter = zm.find(objectIndex);
186     if (zmIter == zm.end())
187     {
188         return -1;
189     }
190     else
191     {
192         return zmIter();
193     }
197 // Return a list of zone names
198 template<class ZoneType, class MeshType>
199 wordList ZoneMesh<ZoneType, MeshType>::types() const
201     const PtrList<ZoneType>& zones = *this;
203     wordList t(zones.size());
205     forAll (zones, zoneI)
206     {
207         t[zoneI] = zones[zoneI].type();
208     }
210     return t;
214 // Return a list of zone names
215 template<class ZoneType, class MeshType>
216 wordList ZoneMesh<ZoneType, MeshType>::names() const
218     const PtrList<ZoneType>& zones = *this;
220     wordList t(zones.size());
222     forAll (zones, zoneI)
223     {
224         t[zoneI] = zones[zoneI].name();
225     }
227     return t;
231 template<class ZoneType, class MeshType>
232 label ZoneMesh<ZoneType, MeshType>::findZoneID(const word& zoneName) const
234     const PtrList<ZoneType>& zones = *this;
236     forAll (zones, zoneI)
237     {
238         if (zones[zoneI].name() == zoneName)
239         {
240             return zoneI;
241         }
242     }
244     // Zone not found
245     if (debug)
246     {
247         Info<< "label ZoneMesh<ZoneType>::findZoneID(const word& "
248             << "zoneName) const : "
249             << "Zone named " << zoneName << " not found.  "
250             << "List of available zone names: " << names() << endl;
251     }
253     // A dummy return to kep the compiler happy
254     return -1;
258 template<class ZoneType, class MeshType>
259 void ZoneMesh<ZoneType, MeshType>::clearAddressing()
261     deleteDemandDrivenData(zoneMapPtr_);
263     PtrList<ZoneType>& zones = *this;
265     forAll (zones, zoneI)
266     {
267         zones[zoneI].clearAddressing();
268     }
272 template<class ZoneType, class MeshType>
273 void ZoneMesh<ZoneType, MeshType>::clear()
275     clearAddressing();
276     PtrList<ZoneType>::clear();
280 // Check zone definition
281 template<class ZoneType, class MeshType>
282 bool ZoneMesh<ZoneType, MeshType>::checkDefinition(const bool report) const
284     bool inError = false;
286     const PtrList<ZoneType>& zones = *this;
288     forAll (zones, zoneI)
289     {
290         inError |= zones[zoneI].checkDefinition(report);
291     }
292     return inError;
296 // Correct zone mesh after moving points
297 template<class ZoneType, class MeshType>
298 void ZoneMesh<ZoneType, MeshType>::movePoints(const pointField& p)
300     PtrList<ZoneType>& zones = *this;
302     forAll (zones, zoneI)
303     {
304         zones[zoneI].movePoints(p);
305     }
309 // writeData member function required by regIOobject
310 template<class ZoneType, class MeshType>
311 bool ZoneMesh<ZoneType, MeshType>::writeData(Ostream& os) const
313     os << *this;
314     return os.good();
318 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
320 template<class ZoneType, class MeshType>
321 Ostream& operator<<(Ostream& os, const ZoneMesh<ZoneType, MeshType>& zones)
323     os  << zones.size() << nl << token::BEGIN_LIST;
325     forAll(zones, zoneI)
326     {
327         zones[zoneI].writeDict(os);
328     }
330     os  << token::END_LIST;
332     return os;
336 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338 } // End namespace Foam
340 // ************************************************************************* //