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

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