Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / conversion / meshTables / boundaryRegion.C
blobb7b862cd55f68fee7a63a407a8ecbe7c46b2f6ad
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "boundaryRegion.H"
27 #include "IOMap.H"
28 #include "OFstream.H"
29 #include "stringListOps.H"
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 Foam::boundaryRegion::boundaryRegion()
35     Map<dictionary>()
39 Foam::boundaryRegion::boundaryRegion
41     const objectRegistry& registry,
42     const word& name,
43     const fileName& instance
46     Map<dictionary>()
48     readDict(registry, name, instance);
52 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
54 Foam::boundaryRegion::~boundaryRegion()
58 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
60 Foam::label Foam::boundaryRegion::append(const dictionary& dict)
62     label maxId = -1;
63     forAllConstIter(Map<dictionary>, *this, iter)
64     {
65         if (maxId < iter.key())
66         {
67             maxId = iter.key();
68         }
69     }
71     insert(++maxId, dict);
72     return maxId;
76 Foam::Map<Foam::word> Foam::boundaryRegion::names() const
78     Map<word> lookup;
80     forAllConstIter(Map<dictionary>, *this, iter)
81     {
82         lookup.insert
83         (
84             iter.key(),
85             iter().lookupOrDefault<word>
86             (
87                 "Label",
88                 "boundaryRegion_" + Foam::name(iter.key())
89             )
90         );
91     }
93     return lookup;
97 Foam::Map<Foam::word> Foam::boundaryRegion::names
99     const UList<wordRe>& patterns
100 ) const
102     Map<word> lookup;
104     forAllConstIter(Map<dictionary>, *this, iter)
105     {
106         word lookupName = iter().lookupOrDefault<word>
107         (
108             "Label",
109             "boundaryRegion_" + Foam::name(iter.key())
110         );
112         if (findStrings(patterns, lookupName))
113         {
114             lookup.insert(iter.key(), lookupName);
115         }
116     }
118     return lookup;
122 Foam::Map<Foam::word> Foam::boundaryRegion::boundaryTypes() const
124     Map<word> lookup;
126     forAllConstIter(Map<dictionary>, *this, iter)
127     {
128         lookup.insert
129         (
130             iter.key(),
131             iter().lookupOrDefault<word>("BoundaryType", "patch")
132         );
133     }
135     return lookup;
139 Foam::label Foam::boundaryRegion::findIndex(const word& name) const
141     if (name.empty())
142     {
143         return -1;
144     }
146     forAllConstIter(Map<dictionary>, *this, iter)
147     {
148         if (iter().lookupOrDefault<word>("Label", word::null) == name)
149         {
150             return iter.key();
151         }
152     }
154     return -1;
158 Foam::word Foam::boundaryRegion::boundaryType(const word& name) const
160     word bndType("patch");
162     label id = this->findIndex(name);
163     if (id >= 0)
164     {
165         operator[](id).readIfPresent<word>("BoundaryType", bndType);
166     }
168     return bndType;
172 void Foam::boundaryRegion::readDict
174     const objectRegistry& registry,
175     const word& name,
176     const fileName& instance
179     clear();
181     // read constant/dictName
182     IOMap<dictionary> ioObj
183     (
184         IOobject
185         (
186             name,
187             instance,
188             registry,
189             IOobject::READ_IF_PRESENT,
190             IOobject::NO_WRITE,
191             false
192         )
193     );
195     if (ioObj.headerOk())
196     {
197         *this = ioObj;
198     }
199     else
200     {
201         Info<< "no constant/boundaryRegion information available" << endl;
202     }
206 void Foam::boundaryRegion::writeDict
208     const objectRegistry& registry,
209     const word& name,
210     const fileName& instance
211 ) const
213     // write constant/dictName
214     IOMap<dictionary> ioObj
215     (
216         IOobject
217         (
218             name,
219             instance,
220             registry,
221             IOobject::NO_READ,
222             IOobject::NO_WRITE,
223             false
224         )
225     );
227     ioObj.note() =
228         "persistent data for thirdParty mesh <-> OpenFOAM translation";
230     Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
232     OFstream os(ioObj.objectPath());
233     ioObj.writeHeader(os);
234     os << *this;
238 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
240 void Foam::boundaryRegion::operator=(const boundaryRegion& rhs)
242     Map<dictionary>::operator=(rhs);
246 void Foam::boundaryRegion::operator=(const Map<dictionary>& rhs)
248     Map<dictionary>::operator=(rhs);
252 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
254 void Foam::boundaryRegion::rename(const dictionary& mapDict)
256     if (mapDict.empty())
257     {
258         return;
259     }
261     // Use 1st pass to collect all the regions to be changed
262     // and 2nd pass to relabel regions.
263     // This avoid re-matching any renamed regions
265     Map<word> mapping;
266     forAllConstIter(dictionary, mapDict, iter)
267     {
268         word oldName(iter().stream());
270         label id = this->findIndex(oldName);
271         if (id >= 0)
272         {
273             mapping.insert(id, iter().keyword());
274         }
275     }
277     forAllConstIter(Map<word>, mapping, iter)
278     {
279         dictionary& dict = operator[](iter.key());
281         Info<< "rename patch: " << iter()
282             << " <- " << word(dict.lookup("Label")) << nl;
284         dict.set("Label", iter());
285     }
289 // ************************************************************************* //