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) 2011 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 // ************************************************************************* //
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::boundaryRegion::boundaryTypes
Map< word > boundaryTypes() const
Return a Map of (id => type)
Definition: boundaryRegion.C:122
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::boundaryRegion::boundaryType
word boundaryType(const word &name) const
Return BoundaryType corresponding to patch 'name'.
Definition: boundaryRegion.C:158
clear
UEqn clear()
Foam::findIndex
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurence of given element and return index,.
Foam::boundaryRegion::rename
void rename(const dictionary &)
Rename regions.
Definition: boundaryRegion.C:254
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::boundaryRegion
The boundaryRegion persistent data saved as a Map<dictionary>.
Definition: boundaryRegion.H:70
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: PrimitivePatchTemplate.H:68
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
OFstream.H
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::IOobject::objectPath
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:376
Foam::IOobject::headerOk
bool headerOk()
Read and check header info.
Definition: IOobject.C:439
Foam::IOobject::writeHeader
bool writeHeader(Ostream &) const
Write header.
Definition: IOobjectWriteHeader.C:67
Foam::boundaryRegion::~boundaryRegion
~boundaryRegion()
Destructor.
Definition: boundaryRegion.C:54
Foam::boundaryRegion::operator=
void operator=(const boundaryRegion &)
Assignment.
Definition: boundaryRegion.C:240
Foam::label
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
Foam::boundaryRegion::readDict
void readDict(const objectRegistry &, const word &name="boundaryRegion", const fileName &instance="constant")
Read constant/boundaryRegion.
Definition: boundaryRegion.C:173
Foam::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:111
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
IOMap.H
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobject.H:273
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::boundaryRegion::findIndex
label findIndex(const word &name) const
Return index corresponding to patch 'name'.
Definition: boundaryRegion.C:139
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
Foam::findStrings
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
Foam::UList
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
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
Foam::boundaryRegion::writeDict
void writeDict(const objectRegistry &, const word &name="boundaryRegion", const fileName &instance="constant") const
Write constant/boundaryRegion for later reuse.
Definition: boundaryRegion.C:207
stringListOps.H
Operations on lists of strings.
Foam::boundaryRegion::names
Map< word > names() const
Return a Map of (id => name)
Definition: boundaryRegion.C:76
boundaryRegion.H
Foam::boundaryRegion::append
label append(const dictionary &)
Append to the end, return index.
Definition: boundaryRegion.C:60
Foam::IOobject::READ_IF_PRESENT
@ READ_IF_PRESENT
Definition: IOobject.H:110
Foam::boundaryRegion::boundaryRegion
boundaryRegion()
Construct null.
Definition: boundaryRegion.C:33
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
insert
timeIndices insert(timeIndex, timeDirs[timeI].value())
Foam::IOobject::note
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:285
lookup
stressControl lookup("compactNormalStress") >> compactNormalStress
Foam::dictionary::set
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:856
Foam::IOMap
A Map of objects of type <T> with automated input and output.
Definition: IOMap.H:50