FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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