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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9  Copyright (C) 2019-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "boundaryRegion.H"
30 #include "IOMap.H"
31 #include "OFstream.H"
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
36 :
37  Map<dictionary>()
38 {}
39 
40 
42 (
43  const objectRegistry& registry,
44  const word& name,
45  const fileName& instance
46 )
47 :
48  Map<dictionary>()
49 {
50  readDict(registry, name, instance);
51 }
52 
53 
54 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
55 
57 {
58  label maxId = -1;
59  forAllConstIters(*this, iter)
60  {
61  if (maxId < iter.key())
62  {
63  maxId = iter.key();
64  }
65  }
66 
67  insert(++maxId, dict);
68  return maxId;
69 }
70 
71 
73 {
74  Map<word> lookup;
75 
76  forAllConstIters(*this, iter)
77  {
78  lookup.insert
79  (
80  iter.key(),
81  iter().getOrDefault<word>
82  (
83  "Label",
84  "boundaryRegion_" + Foam::name(iter.key())
85  )
86  );
87  }
88 
89  return lookup;
90 }
91 
92 
94 (
95  const wordRes& patterns
96 ) const
97 {
98  Map<word> lookup;
99 
100  forAllConstIters(*this, iter)
101  {
102  const word lookupName = iter().getOrDefault<word>
103  (
104  "Label",
105  "boundaryRegion_" + Foam::name(iter.key())
106  );
107 
108  if (patterns.match(lookupName))
109  {
110  lookup.insert(iter.key(), lookupName);
111  }
112  }
113 
114  return lookup;
115 }
116 
117 
119 {
120  Map<word> lookup;
121 
122  forAllConstIters(*this, iter)
123  {
124  lookup.insert
125  (
126  iter.key(),
127  iter().getOrDefault<word>("BoundaryType", "patch")
128  );
129  }
130 
131  return lookup;
132 }
133 
134 
135 Foam::label Foam::boundaryRegion::findIndex(const word& name) const
136 {
137  if (name.empty())
138  {
139  return -1;
140  }
141 
142  forAllConstIters(*this, iter)
143  {
144  if (iter().getOrDefault<word>("Label", word::null) == name)
145  {
146  return iter.key();
147  }
148  }
149 
150  return -1;
151 }
152 
153 
155 {
156  word bndType("patch");
157 
158  label id = this->findIndex(name);
159  if (id >= 0)
160  {
161  operator[](id).readIfPresent<word>("BoundaryType", bndType);
162  }
163 
164  return bndType;
165 }
166 
167 
169 (
170  const objectRegistry& registry,
171  const word& name,
172  const fileName& instance
173 )
174 {
175  clear();
176 
177  // read constant/dictName
178  IOMap<dictionary> ioObj
179  (
180  IOobject
181  (
182  name,
183  instance,
184  registry,
188  )
189  );
190 
191  if (ioObj.headerOk())
192  {
193  *this = ioObj;
194  }
195  else
196  {
197  Info<< "no constant/boundaryRegion information available" << endl;
198  }
199 }
200 
201 
203 (
204  const objectRegistry& registry,
205  const word& name,
206  const fileName& instance
207 ) const
208 {
209  // write constant/dictName
210  IOMap<dictionary> ioObj
211  (
212  IOobject
213  (
214  name,
215  instance,
216  registry,
220  )
221  );
222 
223  ioObj.note() =
224  "persistent data for thirdParty mesh <-> OpenFOAM translation";
225 
226  Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
227 
228  OFstream os(ioObj.objectPath());
229  ioObj.writeHeader(os);
230  os << *this;
231 }
232 
233 
234 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
237 {
239 }
240 
241 
243 {
245 }
246 
247 
248 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
249 
250 void Foam::boundaryRegion::rename(const dictionary& mapDict)
251 {
252  if (mapDict.empty())
253  {
254  return;
255  }
256 
257  // Use 1st pass to collect all the regions to be changed
258  // and 2nd pass to relabel regions.
259  // This avoid re-matching any renamed regions
260 
261  Map<word> mapping;
262  for (const entry& dEntry : mapDict)
263  {
264  const word oldName(dEntry.stream());
265 
266  const label id = this->findIndex(oldName);
267  if (id >= 0)
268  {
269  mapping.insert(id, dEntry.keyword());
270  }
271  }
272 
273  forAllConstIters(mapping, iter)
274  {
275  dictionary& dict = operator[](iter.key());
276 
277  Info<< "rename patch: " << iter()
278  << " <- " << dict.get<word>("Label") << nl;
279 
280  dict.set("Label", iter());
281  }
282 }
283 
284 
285 // ************************************************************************* //
dictionary dict
A class for handling file names.
Definition: fileName.H:72
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 a number of values (eg...
Definition: dictionary.H:129
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
label append(const dictionary &)
Append to the end, return index.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
boundaryRegion()
Construct null.
Ignore writing from objectRegistry::writeObject()
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
Lookup type of boundary radiation properties.
Definition: lookup.H:57
label findIndex(const word &name) const
Return index corresponding to patch &#39;name&#39;.
void rename(const dictionary &)
Rename regions.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Map< word > boundaryTypes() const
Return a Map of (id => type)
A class for handling words, derived from Foam::string.
Definition: word.H:63
Reading is optional [identical to LAZY_READ].
static const word null
An empty word.
Definition: word.H:84
Map< word > names() const
Return a Map of (id => name)
patchWriters clear()
word boundaryType(const word &name) const
Return BoundaryType corresponding to patch &#39;name&#39;.
OBJstream os(runTime.globalPath()/outputName)
void operator=(const boundaryRegion &)
Assignment.
void readDict(const objectRegistry &, const word &name="boundaryRegion", const fileName &instance="constant")
Read constant/boundaryRegion.
Nothing to be read.
messageStream Info
Information stream (stdout output on master, null elsewhere)
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:765
label findIndex(const ListType &input, typename ListType::const_reference val, const label start=0)
Deprecated(2017-10) search for first occurrence of the given element.
Definition: ListOps.H:485
Registry of regIOobjects.
The boundaryRegion persistent data saved as a Map<dictionary>.
void operator=(const this_type &rhs)
Copy assignment.
Definition: Map.H:134
Do not request registration (bool: false)
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
A HashTable to objects of type <T> with a label key.