cellTable.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-2016 OpenFOAM Foundation
9 Copyright (C) 2019-2021 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 "cellTable.H"
30#include "IOMap.H"
31#include "OFstream.H"
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35const char* const Foam::cellTable::defaultMaterial_ = "fluid";
36
37
38// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39
40Foam::Map<Foam::label> Foam::cellTable::zoneMap() const
41{
42 Map<label> lookup;
43
44 label zonei = 0;
45 forAllConstIters(*this, iter)
46 {
47 lookup.insert(iter.key(), zonei++);
48 }
49
50 return lookup;
51}
52
53
55{
56 Map<word> lookup = names();
57 wordList list(lookup.size());
58
59 label zonei = 0;
60 forAllConstIters(lookup, iter)
61 {
62 list[zonei++] = *iter;
63 }
64
65 return list;
66}
67
68
69void Foam::cellTable::addDefaults()
70{
71 forAllIters(*this, iter)
72 {
73 if (!iter().found("MaterialType"))
74 {
75 iter().add("MaterialType", defaultMaterial_);
76 }
77 }
78}
79
80
81void Foam::cellTable::setEntry
82(
83 const label id,
84 const word& keyWord,
85 const word& value
86)
87{
88 dictionary dict;
89 dict.add(keyWord, value);
90
91 iterator iter = find(id);
92 if (iter.found())
93 {
94 iter().merge(dict);
95 }
96 else
97 {
98 insert(id, dict);
99 }
100}
101
102
103// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
104
106:
107 Map<dictionary>()
108{}
109
110
112(
113 const objectRegistry& registry,
114 const word& name,
115 const fileName& instance
116)
117:
118 Map<dictionary>()
119{
120 readDict(registry, name, instance);
121}
122
123
124// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
125
127{
128 label maxId = -1;
129 forAllConstIters(*this, iter)
130 {
131 if (maxId < iter.key())
132 {
133 maxId = iter.key();
134 }
135 }
136
137 insert(++maxId, dict);
138 return maxId;
139}
140
141
143{
145
146 forAllConstIters(*this, iter)
147 {
148 lookup.insert
149 (
150 iter.key(),
151 iter().getOrDefault<word>
152 (
153 "Label",
154 "cellTable_" + Foam::name(iter.key())
155 )
156 );
157 }
158
159 return lookup;
160}
161
162
164(
165 const wordRes& patterns
166) const
167{
169
170 forAllConstIters(*this, iter)
171 {
172 const word lookupName = iter().getOrDefault<word>
173 (
174 "Label",
175 "cellTable_" + Foam::name(iter.key())
176 );
177
178 if (patterns.match(lookupName))
179 {
180 lookup.insert(iter.key(), lookupName);
181 }
182 }
183
184 return lookup;
185}
186
187
189{
190 word theName("cellTable_" + Foam::name(id));
191
192 const_iterator iter = cfind(id);
193 if (iter.found())
194 {
195 iter().readIfPresent("Label", theName);
196 }
197
198 return theName;
199}
200
201
202Foam::label Foam::cellTable::findIndex(const word& name) const
203{
204 if (name.empty())
205 {
206 return -1;
207 }
208
209 forAllConstIters(*this, iter)
210 {
211 if (iter().getOrDefault<word>("Label", word::null) == name)
212 {
213 return iter.key();
214 }
215 }
216
217 return -1;
218}
219
220
222{
224
225 forAllConstIters(*this, iter)
226 {
227 lookup.insert
228 (
229 iter.key(),
230 iter().getOrDefault<word>("MaterialType", defaultMaterial_)
231 );
232 }
233
234 return lookup;
235}
236
237
239{
241
242 forAllConstIters(*this, iter)
243 {
244 const label index = iter.key();
245 const dictionary& dict = iter.val();
246
247 if
248 (
249 matl
250 == dict.getOrDefault<word>("MaterialType", defaultMaterial_)
251 )
252 {
253 lookup.insert
254 (
255 index,
257 (
258 "Label",
259 "cellTable_" + Foam::name(iter.key())
260 )
261 );
262 }
263 }
264
265 return lookup;
266}
267
268
270{
271 return selectType("fluid");
272}
273
274
276{
277 return selectType("solid");
278}
279
280
282{
283 return selectType("shell");
284}
285
286
287
288void Foam::cellTable::setMaterial(const label id, const word& matlType)
289{
290 setEntry(id, "MaterialType", matlType);
291}
292
293
294void Foam::cellTable::setName(const label id, const word& name)
295{
296 setEntry(id, "Label", name);
297}
298
299
300void Foam::cellTable::setName(const label id)
301{
302 iterator iter = find(id);
303
304 if (!iter.found() || !iter().found("Label"))
305 {
306 setName(id, "cellTable_" + Foam::name(id));
307 }
308}
309
310
312(
313 const objectRegistry& registry,
314 const word& name,
315 const fileName& instance
316)
317{
318 clear();
319
320 // read constant/dictName
322 (
324 (
325 name,
326 instance,
327 registry,
330 false
331 )
332 );
333
334 if (ioObj.headerOk())
335 {
336 *this = ioObj;
337 addDefaults();
338 }
339 else
340 {
341 Info<< "no constant/cellTable information available" << endl;
342 }
343}
344
345
347(
348 const objectRegistry& registry,
349 const word& name,
350 const fileName& instance
351) const
352{
353 // write constant/dictName
355 (
357 (
358 name,
359 instance,
360 registry,
363 false
364 )
365 );
366
367 ioObj.note() =
368 "persistent data for thirdParty mesh <-> OpenFOAM translation";
369
370 Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
371
372 OFstream os(ioObj.objectPath());
373 ioObj.writeHeader(os);
374 os << *this;
375}
376
377
378// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
379
381{
383 addDefaults();
384}
385
386
388{
390 addDefaults();
391}
392
393
395{
396 Map<dictionary> zoneDict;
397
398 // create cellTableId and cellTable based on cellZones
399 label nZoneCells = 0;
400
401 wordList zoneNames = mesh.cellZones().names();
402 label unZonedType = zoneNames.size() + 1;
403
404 // do cell zones
405 forAll(mesh.cellZones(), zoneI)
406 {
407 const cellZone& cZone = mesh.cellZones()[zoneI];
408 nZoneCells += cZone.size();
409
411 dict.add("Label", zoneNames[zoneI]);
412 zoneDict.insert(zoneI + 1, dict);
413 }
414
415 // collect unzoned cells
416 // special case: no zones at all - do entire mesh
417 if (nZoneCells == 0)
418 {
419 zoneDict.clear();
420 unZonedType = 1;
421 }
422
423 if (mesh.nCells() > nZoneCells)
424 {
425 zoneDict.insert
426 (
427 unZonedType,
428 dictionary(IStringStream("Label cells;")())
429 );
430 }
431
433 addDefaults();
434}
435
436
437// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
438
440(
441 polyMesh& mesh,
442 const labelList& tableIds
443) const
444{
445 Map<label> typeToZone = zoneMap();
446 List<DynamicList<label>> zoneCells(size());
447
448 forAll(tableIds, celli)
449 {
450 const auto iter = typeToZone.cfind(tableIds[celli]);
451 if (iter.found())
452 {
453 zoneCells[*iter].append(celli);
454 }
455 }
456
457 // track which zones were actually used
458 labelList zoneUsed(zoneCells.size());
459 wordList zoneNames(namesList());
460
461 label nZone = 0;
462 forAll(zoneCells, zoneI)
463 {
464 zoneCells[zoneI].shrink();
465 if (zoneCells[zoneI].size())
466 {
467 zoneUsed[nZone++] = zoneI;
468 }
469 }
470 zoneUsed.setSize(nZone);
471
472 cellZoneMesh& czMesh = mesh.cellZones();
473
474 czMesh.clear();
475 if (nZone <= 1)
476 {
477 Info<< "cellZones not used" << endl;
478 return;
479 }
480 czMesh.setSize(nZone);
481
482 forAll(zoneUsed, zoneI)
483 {
484 const label origZoneI = zoneUsed[zoneI];
485
486 Info<< "cellZone " << zoneI
487 << " (size: " << zoneCells[origZoneI].size()
488 << ") name: " << zoneNames[origZoneI] << endl;
489
490 czMesh.set
491 (
492 zoneI,
493 new cellZone
494 (
495 zoneNames[origZoneI],
496 zoneCells[origZoneI],
497 zoneI,
498 czMesh
499 )
500 );
501 }
503}
504
505
506void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
507{
508 if (mapDict.empty())
509 {
510 return;
511 }
512
513 Map<word> origNames(names());
514 labelList mapping(identity(max(origNames.toc()) + 1));
515
516 bool remap = false;
517 forAllConstIters(mapDict, iter)
518 {
519 wordRes patterns(iter().stream());
520
521 // find all matches
522 Map<word> matches;
523 forAllConstIters(origNames, namesIter)
524 {
525 if (patterns.match(namesIter()))
526 {
527 matches.insert(namesIter.key(), namesIter());
528 }
529 }
530
531 if (matches.size())
532 {
533 label targetId = this->findIndex(iter().keyword());
534
535 Info<< "combine cellTable: " << iter().keyword();
536 if (targetId < 0)
537 {
538 // not found - reuse 1st element but with different name
539 targetId = min(matches.toc());
540 operator[](targetId).set("Label", iter().keyword());
541
542 Info<< " = (";
543 }
544 else
545 {
546 Info<< " += (";
547 }
548
549
550 // the mapping and name for targetId is already okay
551 matches.erase(targetId);
552 origNames.erase(targetId);
553
554 // remove matched names, leaving targetId on 'this'
555 this->erase(matches);
556 origNames.erase(matches);
557
558 forAllConstIters(matches, matchIter)
559 {
560 mapping[matchIter.key()] = targetId;
561 Info<< " " << matchIter();
562 }
563 Info<< " )" << endl;
564
565 remap = true;
566 }
567 }
568
569 if (remap)
570 {
571 inplaceRenumber(mapping, tableIds);
572 }
573}
574
575// ************************************************************************* //
bool found
bool found() const noexcept
True if iterator points to an entry - same as good()
Forward iterator with const access.
Definition: HashTable.H:794
Forward iterator with non-const access.
Definition: HashTable.H:720
List< Key > toc() const
The table of contents (the keys) in unsorted order.
Definition: HashTable.C:122
const T & lookup(const Key &key, const T &deflt) const
Return hashed entry if it exists, or return the given default.
Definition: HashTableI.H:224
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition: HashTableI.H:141
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:180
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:440
void clear()
Clear all entries from table.
Definition: HashTable.C:678
A Map of objects of type <T> with automated input and output. Is a global object; i....
Definition: IOMap.H:58
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:95
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
bool writeHeader(Ostream &os) const
Write header with current type()
writeOption writeOpt() const noexcept
The write option.
Definition: IOobjectI.H:179
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:112
void setSize(const label n)
Alias for resize()
Definition: List.H:218
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
A HashTable to objects of type <T> with a label key.
Definition: Map.H:60
void operator=(const this_type &rhs)
Copy assignment.
Definition: Map.H:117
Output to file stream, using an OSstream.
Definition: OFstream.H:57
const T * set(const label i) const
Definition: PtrList.H:138
void setSize(const label newLen)
Same as resize()
Definition: PtrList.H:151
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
void clear()
Clear the zones.
Definition: ZoneMesh.C:730
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:304
A linked-list that is searchable by the 'name()' of the items.
The cellTable persistent data saved as a Map<dictionary>.
Definition: cellTable.H:83
Map< word > solids() const
Return a Map of (id => name) for solids.
Definition: cellTable.C:275
void setMaterial(const label, const word &)
Assign material Type.
Definition: cellTable.C:288
label findIndex(const word &name) const
Return index corresponding to name.
Definition: cellTable.C:202
void readDict(const objectRegistry &, const word &name="cellTable", const fileName &instance="constant")
Read constant/cellTable.
Definition: cellTable.C:312
Map< word > shells() const
Return a Map of (id => name) for shells.
Definition: cellTable.C:281
cellTable()
Construct null.
Definition: cellTable.C:105
word name(const label id) const
Return the name corresponding to id.
Definition: cellTable.C:188
Map< word > fluids() const
Return a Map of (id => name) for fluids.
Definition: cellTable.C:269
void setName(const label, const word &)
Assign name.
Definition: cellTable.C:294
Map< word > selectType(const word &materialType) const
Return a Map of (id => name) for materialType.
Definition: cellTable.C:238
void operator=(const cellTable &)
Assignment.
Definition: cellTable.C:380
void addCellZones(polyMesh &, const labelList &tableIds) const
Classify tableIds into cellZones according to the cellTable.
Definition: cellTable.C:440
Map< word > names() const
Return a Map of (id => name)
Definition: cellTable.C:142
Map< word > materialTypes() const
Return a Map of (id => fluid|solid|shell)
Definition: cellTable.C:221
A subset of mesh cells.
Definition: cellZone.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
A class for handling file names.
Definition: fileName.H:76
Foam::dictionary writeDict() const
Write to dictionary.
Registry of regIOobjects.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:504
label nCells() const noexcept
Number of mesh cells.
Lookup type of boundary radiation properties.
Definition: lookup.H:66
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:438
bool append() const noexcept
True if output format uses an append mode.
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:54
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
A class for handling words, derived from Foam::string.
Definition: word.H:68
patchWriters clear()
dynamicFvMesh & mesh
OBJstream os(runTime.globalPath()/outputName)
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:653
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List< word > wordList
A List of words.
Definition: fileName.H:63
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
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:398
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:38
void inplaceRenumber(const labelUList &oldToNew, IntListType &input)
Inplace renumber the values (not the indices) of a list.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
srcOptions erase("case")
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:260
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278