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-2024 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 #include "predicates.H"
33 
34 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 
39 template<class MatchPredicate>
40 static Map<word> names_impl
41 (
42  const Map<dictionary>& input,
43  const MatchPredicate& nameMatcher
44 )
45 {
47  output.reserve(input.size());
48 
49  forAllConstIters(input, iter)
50  {
51  word lookupName;
52  if (!iter().readIfPresent("Label", lookupName))
53  {
54  lookupName = "boundaryRegion_" + Foam::name(iter.key());
55  }
56 
57  if (nameMatcher(lookupName))
58  {
59  output.emplace(iter.key(), std::move(lookupName));
60  }
61  }
62 
63  return output;
64 }
65 
66 } // End namespace Foam
67 
68 
69 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
70 
72 (
73  const objectRegistry& obr,
74  const word& name,
75  const fileName& instance
76 )
77 {
78  readDict(obr, name, instance);
79 }
80 
81 
82 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
83 
84 Foam::label Foam::boundaryRegion::maxIndex() const
85 {
86  label maxId = -1;
87  forAllConstIters(*this, iter)
88  {
89  if (maxId < iter.key())
90  {
91  maxId = iter.key();
92  }
93  }
94 
95  return maxId;
96 }
97 
98 
99 Foam::label Foam::boundaryRegion::push_back(const dictionary& dict)
100 {
101  label maxId = this->maxIndex();
102 
103  insert(++maxId, dict);
104  return maxId;
105 }
106 
109 {
110  return names_impl(*this, predicates::always{});
111 }
112 
115 {
116  return names_impl(*this, patterns);
117 }
118 
119 
121 {
123  output.reserve(size());
124 
125  forAllConstIters(*this, iter)
126  {
127  word lookupType;
128  if (!iter().readIfPresent("BoundaryType", lookupType))
129  {
130  lookupType = "patch";
131  }
132 
133  output.emplace(iter.key(), std::move(lookupType));
134  }
135 
136  return output;
137 }
138 
139 
140 Foam::word Foam::boundaryRegion::name(const label id) const
141 {
142  word lookupName;
143 
144  const auto iter = cfind(id);
145  if (iter.good())
146  {
147  iter.val().readIfPresent("Label", lookupName);
148  }
149 
150  if (lookupName.empty() && id >= 0)
151  {
152  lookupName = "boundaryRegion_" + Foam::name(id);
153  }
154 
155  return lookupName;
156 }
157 
158 
159 Foam::label Foam::boundaryRegion::findIndex(const word& name) const
160 {
161  if (name.empty())
162  {
163  return -1;
164  }
165 
166  forAllConstIters(*this, iter)
167  {
168  const auto& dict = iter.val();
169 
170  word lookupName;
171  if (dict.readIfPresent("Label", lookupName) && (lookupName == name))
172  {
173  return iter.key();
174  }
175  }
176 
177  return -1;
178 }
179 
180 
182 {
183  word lookupType("patch");
184 
185  const label id = this->findIndex(name);
186  if (id >= 0)
187  {
188  operator[](id).readIfPresent("BoundaryType", lookupType);
189  }
190 
191  return lookupType;
192 }
193 
194 
196 (
197  const objectRegistry& obr,
198  const word& name,
199  const fileName& instance
200 )
201 {
203 
204  // Read constant/dictName
205  IOMap<dictionary> ioObj
206  (
207  IOobject
208  (
209  name,
210  instance,
211  obr,
215  )
216  );
217 
218  if (ioObj.headerOk())
219  {
220  *this = ioObj;
221  }
222  else
223  {
224  Info<< "no constant/boundaryRegion information available" << endl;
225  }
226 }
227 
228 
230 (
231  const objectRegistry& obr,
232  const word& name,
233  const fileName& instance
234 ) const
235 {
236  // write constant/dictName
237  IOMap<dictionary> ioObj
238  (
239  IOobject
240  (
241  name,
242  instance,
243  obr,
247  )
248  );
249 
250  ioObj.note() =
251  "persistent data for third-party mesh <-> OpenFOAM translation";
252 
253  Info<< "Writing " << ioObj.name() << " to "
254  << ioObj.objectRelPath() << endl;
255 
256  OFstream os(ioObj.objectPath());
257  ioObj.writeHeader(os);
258  os << *this;
261 }
262 
263 
264 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
267 {
269 }
270 
271 
273 {
275 }
276 
277 
278 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
279 
280 void Foam::boundaryRegion::rename(const dictionary& mapDict)
281 {
282  if (mapDict.empty())
283  {
284  return;
285  }
286 
287  // Use 1st pass to collect all the regions to be changed
288  // and 2nd pass to relabel regions.
289  // This avoid re-matching any renamed regions
290 
291  Map<word> mapping;
292  mapping.reserve(mapDict.size());
293 
294  for (const entry& dEntry : mapDict)
295  {
296  const word oldName(dEntry.stream());
297 
298  const label id = this->findIndex(oldName);
299  if (id >= 0)
300  {
301  mapping.insert(id, dEntry.keyword());
302  }
303  }
304 
305  forAllConstIters(mapping, iter)
306  {
307  const word& newName = iter.val();
308 
309  dictionary& dict = operator[](iter.key());
310 
311  word oldName;
312  if (!dict.readIfPresent("Label", oldName))
313  {
314  oldName = "boundaryRegion_" + Foam::name(iter.key());
315  }
316 
317  Info<< "rename patch: " << newName << " <- " << oldName << nl;
318 
319  dict.set("Label", newName);
320  }
321 }
322 
323 
324 // ************************************************************************* //
dictionary dict
A class for handling file names.
Definition: fileName.H:72
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))
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
Ignore writing from objectRegistry::writeObject()
label findIndex(const word &name) const
The index corresponding to entry with &#39;Label&#39; of given name, or -1 if not found.
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:53
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 the extracted Map of (id => type)
A class for handling words, derived from Foam::string.
Definition: word.H:63
void clear()
Remove all entries from table.
Definition: HashTable.C:749
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
boundaryRegion() noexcept=default
Default construct.
Reading is optional [identical to LAZY_READ].
Map< word > names() const
Return the extracted Map of (id => name)
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
static Ostream & writeEndDivider(Ostream &os)
Write the standard end file divider.
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
static Map< word > names_impl(const Map< dictionary > &input, const MatchPredicate &nameMatcher)
label push_back(const dictionary &dict)
Add to the end, return index.
word boundaryType(const word &name) const
Return BoundaryType corresponding to patch &#39;name&#39;, "patch" if not found.
OBJstream os(runTime.globalPath()/outputName)
void operator=(const boundaryRegion &)
Copy assignment.
void readDict(const objectRegistry &obr, const word &name="boundaryRegion", const fileName &instance="constant")
Read constant/boundaryRegion.
label maxIndex() const
The max table index, -1 if empty.
Nothing to be read.
meshDefDict readIfPresent("polyMeshPatches", polyPatchNames)
messageStream Info
Information stream (stdout output on master, null elsewhere)
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
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:517
Registry of regIOobjects.
The boundaryRegion persistent data saved as a Map<dictionary>.
word name(const label id) const
The &#39;Label&#39; name corresponding to id, or boundaryRegion_ID if not otherwise defined.
void writeDict(const objectRegistry &obr, const word &name="boundaryRegion", const fileName &instance="constant") const
Write constant/boundaryRegion for later reuse.
void operator=(const this_type &rhs)
Copy assignment.
Definition: Map.H:155
Do not request registration (bool: false)
Namespace for OpenFOAM.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
A HashTable to objects of type <T> with a label key.