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

boundaryRegion.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 "boundaryRegion.H"
00027 #include <OpenFOAM/IOMap.H>
00028 #include <OpenFOAM/OFstream.H>
00029 #include <OpenFOAM/stringListOps.H>
00030 
00031 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00032 
00033 Foam::boundaryRegion::boundaryRegion()
00034 :
00035     Map<dictionary>()
00036 {}
00037 
00038 
00039 Foam::boundaryRegion::boundaryRegion
00040 (
00041     const objectRegistry& registry,
00042     const word& name,
00043     const fileName& instance
00044 )
00045 :
00046     Map<dictionary>()
00047 {
00048     readDict(registry, name, instance);
00049 }
00050 
00051 
00052 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00053 
00054 Foam::boundaryRegion::~boundaryRegion()
00055 {}
00056 
00057 
00058 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00059 
00060 Foam::label Foam::boundaryRegion::append(const dictionary& dict)
00061 {
00062     label maxId = -1;
00063     forAllConstIter(Map<dictionary>, *this, iter)
00064     {
00065         if (maxId < iter.key())
00066         {
00067             maxId = iter.key();
00068         }
00069     }
00070 
00071     insert(++maxId, dict);
00072     return maxId;
00073 }
00074 
00075 
00076 Foam::Map<Foam::word> Foam::boundaryRegion::names() const
00077 {
00078     Map<word> lookup;
00079 
00080     forAllConstIter(Map<dictionary>, *this, iter)
00081     {
00082         lookup.insert
00083         (
00084             iter.key(),
00085             iter().lookupOrDefault<word>
00086             (
00087                 "Label",
00088                 "boundaryRegion_" + Foam::name(iter.key())
00089             )
00090         );
00091     }
00092 
00093     return lookup;
00094 }
00095 
00096 
00097 Foam::Map<Foam::word> Foam::boundaryRegion::names
00098 (
00099     const List<wordRe>& patterns
00100 ) const
00101 {
00102     Map<word> lookup;
00103 
00104     forAllConstIter(Map<dictionary>, *this, iter)
00105     {
00106         word lookupName = iter().lookupOrDefault<word>
00107         (
00108             "Label",
00109             "boundaryRegion_" + Foam::name(iter.key())
00110         );
00111 
00112         if (findStrings(patterns, lookupName))
00113         {
00114             lookup.insert(iter.key(), lookupName);
00115         }
00116     }
00117 
00118     return lookup;
00119 }
00120 
00121 
00122 Foam::Map<Foam::word> Foam::boundaryRegion::boundaryTypes() const
00123 {
00124     Map<word> lookup;
00125 
00126     forAllConstIter(Map<dictionary>, *this, iter)
00127     {
00128         lookup.insert
00129         (
00130             iter.key(),
00131             iter().lookupOrDefault<word>("BoundaryType", "patch")
00132         );
00133     }
00134 
00135     return lookup;
00136 }
00137 
00138 
00139 Foam::label Foam::boundaryRegion::findIndex(const word& name) const
00140 {
00141     if (name.empty())
00142     {
00143         return -1;
00144     }
00145 
00146     forAllConstIter(Map<dictionary>, *this, iter)
00147     {
00148         if (iter().lookupOrDefault<word>("Label", word::null) == name)
00149         {
00150             return iter.key();
00151         }
00152     }
00153 
00154     return -1;
00155 }
00156 
00157 
00158 Foam::word Foam::boundaryRegion::boundaryType(const word& name) const
00159 {
00160     word bndType("patch");
00161 
00162     label id = this->findIndex(name);
00163     if (id >= 0)
00164     {
00165         operator[](id).readIfPresent<word>("BoundaryType", bndType);
00166     }
00167 
00168     return bndType;
00169 }
00170 
00171 
00172 void Foam::boundaryRegion::readDict
00173 (
00174     const objectRegistry& registry,
00175     const word& name,
00176     const fileName& instance
00177 )
00178 {
00179     clear();
00180 
00181     // read constant/dictName
00182     IOMap<dictionary> ioObj
00183     (
00184         IOobject
00185         (
00186             name,
00187             instance,
00188             registry,
00189             IOobject::READ_IF_PRESENT,
00190             IOobject::NO_WRITE,
00191             false
00192         )
00193     );
00194 
00195     if (ioObj.headerOk())
00196     {
00197         *this = ioObj;
00198     }
00199     else
00200     {
00201         Info<< "no constant/boundaryRegion information available" << endl;
00202     }
00203 }
00204 
00205 
00206 void Foam::boundaryRegion::writeDict
00207 (
00208     const objectRegistry& registry,
00209     const word& name,
00210     const fileName& instance
00211 ) const
00212 {
00213     // write constant/dictName
00214     IOMap<dictionary> ioObj
00215     (
00216         IOobject
00217         (
00218             name,
00219             instance,
00220             registry,
00221             IOobject::NO_READ,
00222             IOobject::NO_WRITE,
00223             false
00224         )
00225     );
00226 
00227     ioObj.note() =
00228         "persistent data for thirdParty mesh <-> OpenFOAM translation";
00229 
00230     Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
00231 
00232     OFstream os(ioObj.objectPath());
00233     ioObj.writeHeader(os);
00234     os << *this;
00235 }
00236 
00237 
00238 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00239 
00240 void Foam::boundaryRegion::operator=(const boundaryRegion& rhs)
00241 {
00242     Map<dictionary>::operator=(rhs);
00243 }
00244 
00245 
00246 void Foam::boundaryRegion::operator=(const Map<dictionary>& rhs)
00247 {
00248     Map<dictionary>::operator=(rhs);
00249 }
00250 
00251 
00252 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00253 
00254 void Foam::boundaryRegion::rename(const dictionary& mapDict)
00255 {
00256     if (mapDict.empty())
00257     {
00258         return;
00259     }
00260 
00261     // Use 1st pass to collect all the regions to be changed
00262     // and 2nd pass to relabel regions.
00263     // This avoid re-matching any renamed regions
00264 
00265     Map<word> mapping;
00266     forAllConstIter(dictionary, mapDict, iter)
00267     {
00268         word oldName(iter().stream());
00269 
00270         label id = this->findIndex(oldName);
00271         if (id >= 0)
00272         {
00273             mapping.insert(id, iter().keyword());
00274         }
00275     }
00276 
00277     forAllConstIter(Map<word>, mapping, iter)
00278     {
00279         dictionary& dict = operator[](iter.key());
00280 
00281         Info<< "rename patch: " << iter()
00282             << " <- " << word(dict.lookup("Label")) << nl;
00283 
00284         dict.set("Label", iter());
00285     }
00286 }
00287 
00288 
00289 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines