boundaryRegion.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
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 "IOMap.H"
28 #include "OFstream.H"
29 #include "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 
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 {
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 UList<wordRe>& patterns
100 ) const
101 {
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 {
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 
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 // ************************************************************************* //
~boundaryRegion()
Destructor.
dictionary dict
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const word & name() const
Return name.
Definition: IOobject.H:295
A class for handling file names.
Definition: fileName.H:79
void writeDict(const objectRegistry &, const word &name="boundaryRegion", const fileName &instance="constant") const
Write constant/boundaryRegion for later reuse.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
Output to file stream.
Definition: OFstream.H:82
label append(const dictionary &)
Append to the end, return index.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool headerOk()
Read and check header info.
Definition: regIOobject.C:431
A Map of objects of type <T> with automated input and output. Is a global object; i...
Definition: IOMap.H:51
Operations on lists of strings.
boundaryRegion()
Construct null.
label findIndex(const word &name) const
Return index corresponding to patch &#39;name&#39;.
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:313
void rename(const dictionary &)
Rename regions.
stressControl lookup("compactNormalStress") >> compactNormalStress
Map< word > boundaryTypes() const
Return a Map of (id => type)
A class for handling words, derived from string.
Definition: word.H:59
void clear()
Clear all entries from table.
Definition: HashTable.C:468
static const word null
An empty word.
Definition: word.H:77
Map< word > names() const
Return a Map of (id => name)
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
static const char nl
Definition: Ostream.H:265
word boundaryType(const word &name) const
Return BoundaryType corresponding to patch &#39;name&#39;.
void operator=(const boundaryRegion &)
Assignment.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void readDict(const objectRegistry &, const word &name="boundaryRegion", const fileName &instance="constant")
Read constant/boundaryRegion.
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:948
bool writeHeader(Ostream &) const
Write header.
messageStream Info
T & operator[](const Key &)
Find and return a hashedEntry.
Definition: HashTableI.H:103
Registry of regIOobjects.
The boundaryRegion persistent data saved as a Map<dictionary>.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:404
void operator=(const Map< T > &map)
Definition: Map.H:102
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:583
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49