FreeFOAM The Cross-Platform CFD Toolkit
boundaryRegion.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "boundaryRegion.H"
27 #include <OpenFOAM/IOMap.H>
28 #include <OpenFOAM/OFstream.H>
29 #include <OpenFOAM/stringListOps.H>
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 :
35  Map<dictionary>()
36 {}
37 
38 
40 (
41  const objectRegistry& registry,
42  const word& name,
43  const fileName& instance
44 )
45 :
47 {
48  readDict(registry, name, instance);
49 }
50 
51 
52 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
53 
55 {}
56 
57 
58 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
59 
60 Foam::label Foam::boundaryRegion::append(const dictionary& dict)
61 {
62  label maxId = -1;
63  forAllConstIter(Map<dictionary>, *this, iter)
64  {
65  if (maxId < iter.key())
66  {
67  maxId = iter.key();
68  }
69  }
70 
71  insert(++maxId, dict);
72  return maxId;
73 }
74 
75 
77 {
78  Map<word> lookup;
79 
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  }
92 
93  return lookup;
94 }
95 
96 
98 (
99  const List<wordRe>& patterns
100 ) const
101 {
102  Map<word> lookup;
103 
104  forAllConstIter(Map<dictionary>, *this, iter)
105  {
106  word lookupName = iter().lookupOrDefault<word>
107  (
108  "Label",
109  "boundaryRegion_" + Foam::name(iter.key())
110  );
111 
112  if (findStrings(patterns, lookupName))
113  {
114  lookup.insert(iter.key(), lookupName);
115  }
116  }
117 
118  return lookup;
119 }
120 
121 
123 {
124  Map<word> lookup;
125 
126  forAllConstIter(Map<dictionary>, *this, iter)
127  {
128  lookup.insert
129  (
130  iter.key(),
131  iter().lookupOrDefault<word>("BoundaryType", "patch")
132  );
133  }
134 
135  return lookup;
136 }
137 
138 
139 Foam::label Foam::boundaryRegion::findIndex(const word& name) const
140 {
141  if (name.empty())
142  {
143  return -1;
144  }
145 
146  forAllConstIter(Map<dictionary>, *this, iter)
147  {
148  if (iter().lookupOrDefault<word>("Label", word::null) == name)
149  {
150  return iter.key();
151  }
152  }
153 
154  return -1;
155 }
156 
157 
159 {
160  word bndType("patch");
161 
162  label id = this->findIndex(name);
163  if (id >= 0)
164  {
165  operator[](id).readIfPresent<word>("BoundaryType", bndType);
166  }
167 
168  return bndType;
169 }
170 
171 
173 (
174  const objectRegistry& registry,
175  const word& name,
176  const fileName& instance
177 )
178 {
179  clear();
180 
181  // read constant/dictName
182  IOMap<dictionary> ioObj
183  (
184  IOobject
185  (
186  name,
187  instance,
188  registry,
191  false
192  )
193  );
194 
195  if (ioObj.headerOk())
196  {
197  *this = ioObj;
198  }
199  else
200  {
201  Info<< "no constant/boundaryRegion information available" << endl;
202  }
203 }
204 
205 
207 (
208  const objectRegistry& registry,
209  const word& name,
210  const fileName& instance
211 ) const
212 {
213  // write constant/dictName
214  IOMap<dictionary> ioObj
215  (
216  IOobject
217  (
218  name,
219  instance,
220  registry,
223  false
224  )
225  );
226 
227  ioObj.note() =
228  "persistent data for thirdParty mesh <-> OpenFOAM translation";
229 
230  Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
231 
232  OFstream os(ioObj.objectPath());
233  ioObj.writeHeader(os);
234  os << *this;
235 }
236 
237 
238 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
239 
241 {
243 }
244 
245 
247 {
249 }
250 
251 
252 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
253 
255 {
256  if (mapDict.empty())
257  {
258  return;
259  }
260 
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
264 
265  Map<word> mapping;
266  forAllConstIter(dictionary, mapDict, iter)
267  {
268  word oldName(iter().stream());
269 
270  label id = this->findIndex(oldName);
271  if (id >= 0)
272  {
273  mapping.insert(id, iter().keyword());
274  }
275  }
276 
277  forAllConstIter(Map<word>, mapping, iter)
278  {
279  dictionary& dict = operator[](iter.key());
280 
281  Info<< "rename patch: " << iter()
282  << " <- " << word(dict.lookup("Label")) << nl;
283 
284  dict.set("Label", iter());
285  }
286 }
287 
288 
289 // ************************ vim: set sw=4 sts=4 et: ************************ //