Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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"
34// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35
36namespace Foam
37{
38
39template<class MatchPredicate>
41(
42 const Map<dictionary>& input,
43 const MatchPredicate& nameMatcher
44)
45{
46 Map<word> output;
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;
65
66} // End namespace Foam
67
68
69// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
70
72(
73 const objectRegistry& obr,
74 const word& name,
75 const fileName& instance
76)
78 readDict(obr, name, instance);
79}
80
81
82// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
83
84Foam::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
100{
101 label maxId = this->maxIndex();
102
103 insert(++maxId, dict);
104 return maxId;
105}
106
111}
112
115{
116 return names_impl(*this, patterns);
117}
118
119
121{
122 Map<word> output;
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
140Foam::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
159Foam::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();
175 }
176
177 return -1;
178}
179
180
181Foam::word Foam::boundaryRegion::boundaryType(const word& name) const
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;
263
264// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
269}
270
271
275}
276
277
278// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
279
280void 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// ************************************************************************* //
propsDict readIfPresent("fields", acceptFields)
bool empty() const noexcept
True if the list is empty.
Definition DLListBase.H:189
label size() const noexcept
The number of elements in list.
Definition DLListBase.H:194
const_iterator cfind(const label &key) const
Definition HashTableI.H:113
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
Definition HashTable.C:729
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition HashTableI.H:152
label size() const noexcept
The number of elements in table.
Definition HashTable.H:358
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition HashTableI.H:129
A Map of objects of type <T> with automated input and output. Is a global object; i....
Definition IOMap.H:54
@ NO_REGISTER
Do not request registration (bool: false).
@ NO_READ
Nothing to be read.
@ READ_IF_PRESENT
Reading is optional [identical to LAZY_READ].
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
static Ostream & writeEndDivider(Ostream &os)
Write the standard end file divider.
fileName objectRelPath() const
The object path relative to the case.
Definition IOobject.C:581
const string & note() const noexcept
Return the optional note.
Definition IOobjectI.H:235
fileName objectPath() const
The complete path + object name.
Definition IOobjectI.H:313
bool writeHeader(Ostream &os) const
Write header with current type().
A HashTable to objects of type <T> with a label key.
Definition Map.H:54
constexpr Map() noexcept=default
void operator=(const this_type &rhs)
Copy assignment.
Definition Map.H:152
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
word boundaryType(const word &name) const
Return BoundaryType corresponding to patch 'name', "patch" if not found.
label findIndex(const word &name) const
The index corresponding to entry with 'Label' of given name, or -1 if not found.
label maxIndex() const
The max table index, -1 if empty.
boundaryRegion() noexcept=default
Default construct.
void readDict(const objectRegistry &obr, const word &name="boundaryRegion", const fileName &instance="constant")
Read constant/boundaryRegion.
word name(const label id) const
The 'Label' name corresponding to id, or boundaryRegion_ID if not otherwise defined.
Map< word > boundaryTypes() const
Return the extracted Map of (id => type).
Map< word > names() const
Return the extracted Map of (id => name).
void writeDict(const objectRegistry &obr, const word &name="boundaryRegion", const fileName &instance="constant") const
Write constant/boundaryRegion for later reuse.
label push_back(const dictionary &dict)
Add to the end, return index.
boundaryRegion(const boundaryRegion &)=delete
No copy construct.
void rename(const dictionary &)
Rename regions.
void operator=(const boundaryRegion &)
Copy assignment.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
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...
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition dictionary.C:765
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
A class for handling file names.
Definition fileName.H:75
Registry of regIOobjects.
bool headerOk()
Read and check header info. Does not check the headerClassName.
A List of wordRe with additional matching capabilities.
Definition wordRes.H:56
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
auto & name
Namespace for OpenFOAM.
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
messageStream Info
Information stream (stdout output on master, null elsewhere).
static Map< word > names_impl(const Map< dictionary > &input, const MatchPredicate &nameMatcher)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
nonInt insert("surfaceSum(((S|magSf)*S)")
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition stdFoam.H:235
Unary and binary predicates that always return true, useful for templating.
Definition predicates.H:54