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 :
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 {
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 {
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 {
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,
187  false
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,
219  false
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 * * * * * * * * * * * * * //
235 
237 {
239 }
240 
241 
243 {
245 }
246 
247 
248 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
249 
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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::boundaryRegion::boundaryTypes
Map< word > boundaryTypes() const
Return a Map of (id => type)
Definition: boundaryRegion.C:118
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::boundaryRegion::boundaryType
word boundaryType(const word &name) const
Return BoundaryType corresponding to patch 'name'.
Definition: boundaryRegion.C:154
Foam::boundaryRegion::rename
void rename(const dictionary &)
Rename regions.
Definition: boundaryRegion.C:250
Foam::Map::operator=
void operator=(const this_type &rhs)
Copy assignment.
Definition: Map.H:117
Foam::boundaryRegion
The boundaryRegion persistent data saved as a Map<dictionary>.
Definition: boundaryRegion.H:72
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: lumpedPointController.H:69
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
OFstream.H
Foam::wordRes::match
bool match(const std::string &text, bool literal=false) const
Smart match as literal or regex, stopping on the first match.
Definition: wordResI.H:91
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::boundaryRegion::operator=
void operator=(const boundaryRegion &)
Assignment.
Definition: boundaryRegion.C:236
Foam::boundaryRegion::readDict
void readDict(const objectRegistry &, const word &name="boundaryRegion", const fileName &instance="constant")
Read constant/boundaryRegion.
Definition: boundaryRegion.C:169
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::findIndex
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:406
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:187
IOMap.H
Foam::radiation::lookup
Lookup type of boundary radiation properties.
Definition: lookup.H:63
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::boundaryRegion::findIndex
label findIndex(const word &name) const
Return index corresponding to patch 'name'.
Definition: boundaryRegion.C:135
os
OBJstream os(runTime.globalPath()/outputName)
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::IOobject::note
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:95
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
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:203
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header with current type()
Definition: IOobjectWriteHeader.C:277
Foam::boundaryRegion::names
Map< word > names() const
Return a Map of (id => name)
Definition: boundaryRegion.C:72
Foam::IOobject::objectPath
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
Foam::IOobject::NO_READ
Definition: IOobject.H:188
boundaryRegion.H
Foam::boundaryRegion::append
label append(const dictionary &)
Append to the end, return index.
Definition: boundaryRegion.C:56
Foam::boundaryRegion::boundaryRegion
boundaryRegion()
Construct null.
Definition: boundaryRegion.C:35
Foam::regIOobject::headerOk
bool headerOk()
Read and check header info.
Definition: regIOobject.C:436
Foam::IOMap
A Map of objects of type <T> with automated input and output. Is a global object; i....
Definition: IOMap.H:54