OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

ZoneMesh.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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "ZoneMesh.H"
00028 #include "entry.H"
00029 #include "demandDrivenData.H"
00030 
00031 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035 
00036 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00037 
00038 template<class ZoneType, class MeshType>
00039 void ZoneMesh<ZoneType, MeshType>::calcZoneMap() const
00040 {
00041     // It is an error to attempt to recalculate cellEdges
00042     // if the pointer is already set
00043     if (zoneMapPtr_)
00044     {
00045         FatalErrorIn("void ZoneMesh<ZoneType>::calcZoneMap() const")
00046             << "zone map already calculated"
00047             << abort(FatalError);
00048     }
00049     else
00050     {
00051         // Count number of objects in all zones
00052         label nObjects = 0;
00053 
00054         forAll (*this, zoneI)
00055         {
00056             nObjects += this->operator[](zoneI).size();
00057         }
00058 
00059         zoneMapPtr_ = new Map<label>(2*nObjects);
00060         Map<label>& zm = *zoneMapPtr_;
00061 
00062         // Fill in objects of all zones into the map.  The key is the global
00063         // object index and the result is the zone index
00064         forAll (*this, zoneI)
00065         {
00066             const labelList& zoneObjects = this->operator[](zoneI);
00067 
00068             forAll (zoneObjects, objI)
00069             {
00070                 zm.insert(zoneObjects[objI], zoneI);
00071             }
00072         }
00073     }
00074 }
00075 
00076 
00077 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00078 
00079 // Read constructor given IOobject and a MeshType reference
00080 template<class ZoneType, class MeshType>
00081 ZoneMesh<ZoneType, MeshType>::ZoneMesh
00082 (
00083     const IOobject& io,
00084     const MeshType& mesh
00085 )
00086 :
00087     PtrList<ZoneType>(),
00088     regIOobject(io),
00089     mesh_(mesh),
00090     zoneMapPtr_(NULL)
00091 {
00092     if
00093     (
00094         readOpt() == IOobject::MUST_READ
00095      || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
00096     )
00097     {
00098         PtrList<ZoneType>& zones = *this;
00099 
00100         // Read zones
00101         Istream& is = readStream(typeName);
00102 
00103         PtrList<entry> patchEntries(is);
00104         zones.setSize(patchEntries.size());
00105 
00106         forAll(zones, zoneI)
00107         {
00108             zones.set
00109             (
00110                 zoneI,
00111                 ZoneType::New
00112                 (
00113                     patchEntries[zoneI].keyword(),
00114                     patchEntries[zoneI].dict(),
00115                     zoneI,
00116                     *this
00117                 )
00118             );
00119         }
00120 
00121         // Check state of IOstream
00122         is.check
00123         (
00124             "ZoneMesh::ZoneMesh"
00125             "(const IOobject&, const MeshType&)"
00126         );
00127 
00128         close();
00129     }
00130     else
00131     {
00132         // No files found.  Force a write of zero-sized zones
00133         // write();
00134     }
00135 }
00136 
00137 
00138 // Construct given size. Zones will be set later
00139 template<class ZoneType, class MeshType>
00140 ZoneMesh<ZoneType, MeshType>::ZoneMesh
00141 (
00142     const IOobject& io,
00143     const MeshType& mesh,
00144     const label size
00145 )
00146 :
00147     PtrList<ZoneType>(size),
00148     regIOobject(io),
00149     mesh_(mesh),
00150     zoneMapPtr_(NULL)
00151 {}
00152 
00153 
00154 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00155 
00156 template<class ZoneType, class MeshType>
00157 ZoneMesh<ZoneType, MeshType>::~ZoneMesh()
00158 {
00159     clearAddressing();
00160 }
00161 
00162 
00163 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00164 
00165 // Map of zones for quick zone lookup
00166 template<class ZoneType, class MeshType>
00167 const Map<label>& ZoneMesh<ZoneType, MeshType>::zoneMap() const
00168 {
00169     if (!zoneMapPtr_)
00170     {
00171         calcZoneMap();
00172     }
00173 
00174     return *zoneMapPtr_;
00175 }
00176 
00177 
00178 // Given a global object index, return the zone it is in.
00179 // If object does not belong to any zones, return -1
00180 template<class ZoneType, class MeshType>
00181 label ZoneMesh<ZoneType, MeshType>::whichZone(const label objectIndex) const
00182 {
00183     const Map<label>& zm = zoneMap();
00184     Map<label>::const_iterator zmIter = zm.find(objectIndex);
00185 
00186     if (zmIter == zm.end())
00187     {
00188         return -1;
00189     }
00190     else
00191     {
00192         return zmIter();
00193     }
00194 }
00195 
00196 
00197 // Return a list of zone names
00198 template<class ZoneType, class MeshType>
00199 wordList ZoneMesh<ZoneType, MeshType>::types() const
00200 {
00201     const PtrList<ZoneType>& zones = *this;
00202 
00203     wordList t(zones.size());
00204 
00205     forAll (zones, zoneI)
00206     {
00207         t[zoneI] = zones[zoneI].type();
00208     }
00209 
00210     return t;
00211 }
00212 
00213 
00214 // Return a list of zone names
00215 template<class ZoneType, class MeshType>
00216 wordList ZoneMesh<ZoneType, MeshType>::names() const
00217 {
00218     const PtrList<ZoneType>& zones = *this;
00219 
00220     wordList t(zones.size());
00221 
00222     forAll (zones, zoneI)
00223     {
00224         t[zoneI] = zones[zoneI].name();
00225     }
00226 
00227     return t;
00228 }
00229 
00230 
00231 template<class ZoneType, class MeshType>
00232 label ZoneMesh<ZoneType, MeshType>::findZoneID(const word& zoneName) const
00233 {
00234     const PtrList<ZoneType>& zones = *this;
00235 
00236     forAll (zones, zoneI)
00237     {
00238         if (zones[zoneI].name() == zoneName)
00239         {
00240             return zoneI;
00241         }
00242     }
00243 
00244     // Zone not found
00245     if (debug)
00246     {
00247         Info<< "label ZoneMesh<ZoneType>::findZoneID(const word& "
00248             << "zoneName) const : "
00249             << "Zone named " << zoneName << " not found.  "
00250             << "List of available zone names: " << names() << endl;
00251     }
00252 
00253     // A dummy return to kep the compiler happy
00254     return -1;
00255 }
00256 
00257 
00258 template<class ZoneType, class MeshType>
00259 void ZoneMesh<ZoneType, MeshType>::clearAddressing()
00260 {
00261     deleteDemandDrivenData(zoneMapPtr_);
00262 
00263     PtrList<ZoneType>& zones = *this;
00264 
00265     forAll (zones, zoneI)
00266     {
00267         zones[zoneI].clearAddressing();
00268     }
00269 }
00270 
00271 
00272 template<class ZoneType, class MeshType>
00273 void ZoneMesh<ZoneType, MeshType>::clear()
00274 {
00275     clearAddressing();
00276     PtrList<ZoneType>::clear();
00277 }
00278 
00279 
00280 // Check zone definition
00281 template<class ZoneType, class MeshType>
00282 bool ZoneMesh<ZoneType, MeshType>::checkDefinition(const bool report) const
00283 {
00284     bool inError = false;
00285 
00286     const PtrList<ZoneType>& zones = *this;
00287 
00288     forAll (zones, zoneI)
00289     {
00290         inError |= zones[zoneI].checkDefinition(report);
00291     }
00292     return inError;
00293 }
00294 
00295 
00296 // Correct zone mesh after moving points
00297 template<class ZoneType, class MeshType>
00298 void ZoneMesh<ZoneType, MeshType>::movePoints(const pointField& p)
00299 {
00300     PtrList<ZoneType>& zones = *this;
00301 
00302     forAll (zones, zoneI)
00303     {
00304         zones[zoneI].movePoints(p);
00305     }
00306 }
00307 
00308 
00309 // writeData member function required by regIOobject
00310 template<class ZoneType, class MeshType>
00311 bool ZoneMesh<ZoneType, MeshType>::writeData(Ostream& os) const
00312 {
00313     os << *this;
00314     return os.good();
00315 }
00316 
00317 
00318 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00319 
00320 template<class ZoneType, class MeshType>
00321 Ostream& operator<<(Ostream& os, const ZoneMesh<ZoneType, MeshType>& zones)
00322 {
00323     os  << zones.size() << nl << token::BEGIN_LIST;
00324 
00325     forAll(zones, zoneI)
00326     {
00327         zones[zoneI].writeDict(os);
00328     }
00329 
00330     os  << token::END_LIST;
00331 
00332     return os;
00333 }
00334 
00335 
00336 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00337 
00338 } // End namespace Foam
00339 
00340 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd