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 -------------------------------------------------------------------------------
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 "cellTable.H"
30 #include "IOMap.H"
31 #include "OFstream.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 const char* const Foam::cellTable::defaultMaterial_ = "fluid";
36 
37 
38 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 
40 Foam::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 
54 Foam::wordList Foam::cellTable::namesList() const
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 
69 void 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 
81 void Foam::cellTable::setEntry
82 (
83  const label id,
84  const word& keyWord,
85  const word& value
86 )
87 {
89  dict.add(keyWord, value);
90 
91  iterator iter = find(id);
92  if (iter.good())
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 
126 Foam::label Foam::cellTable::append(const dictionary& dict)
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 {
144  Map<word> lookup;
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 {
168  Map<word> lookup;
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 
188 Foam::word Foam::cellTable::name(const label id) const
189 {
190  word theName("cellTable_" + Foam::name(id));
191 
192  const_iterator iter = cfind(id);
193  if (iter.good())
194  {
195  iter().readIfPresent("Label", theName);
196  }
197 
198  return theName;
199 }
200 
201 
202 Foam::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 {
223  Map<word> lookup;
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 {
240  Map<word> lookup;
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,
256  dict.getOrDefault<word>
257  (
258  "Label",
259  "cellTable_" + Foam::name(iter.key())
260  )
261  );
262  }
263  }
264 
265  return lookup;
266 }
267 
270 {
271  return selectType("fluid");
272 }
273 
276 {
277  return selectType("solid");
278 }
279 
280 
282 {
283  return selectType("shell");
284 }
285 
286 
288 void Foam::cellTable::setMaterial(const label id, const word& matlType)
289 {
290  setEntry(id, "MaterialType", matlType);
291 }
292 
294 void Foam::cellTable::setName(const label id, const word& name)
295 {
296  setEntry(id, "Label", name);
297 }
298 
299 
300 void Foam::cellTable::setName(const label id)
301 {
302  iterator iter = find(id);
303 
304  if (!iter.good() || !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
321  IOMap<dictionary> ioObj
322  (
323  IOobject
324  (
325  name,
326  instance,
327  registry,
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
354  IOMap<dictionary> ioObj
355  (
356  IOobject
357  (
358  name,
359  instance,
360  registry,
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 
432  Map<dictionary>::operator=(zoneDict);
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.good())
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  }
502  czMesh.writeOpt(IOobject::AUTO_WRITE);
503 }
504 
505 
506 void 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 // ************************************************************************* //
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
label append(const dictionary &)
Append to the end, return index.
Definition: cellTable.C:119
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
A class for handling file names.
Definition: fileName.H:72
void clear()
Clear the zones.
Definition: ZoneMesh.C:841
Map< word > shells() const
Return a Map of (id => name) for shells.
Definition: cellTable.C:274
srcOptions erase("case")
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
void setName(const label, const word &)
Assign name.
Definition: cellTable.C:287
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Map< word > solids() const
Return a Map of (id => name) for solids.
Definition: cellTable.C:268
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:625
Ignore writing from objectRegistry::writeObject()
Lookup type of boundary radiation properties.
Definition: lookup.H:57
void setMaterial(const label, const word &)
Assign material Type.
Definition: cellTable.C:281
void operator=(const cellTable &)
Assignment.
Definition: cellTable.C:373
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
void writeDict(const objectRegistry &, const word &name="cellTable", const fileName &instance="constant") const
Write constant/cellTable for later reuse.
Definition: cellTable.C:340
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:152
void readDict(const objectRegistry &, const word &name="cellTable", const fileName &instance="constant")
Read constant/cellTable.
Definition: cellTable.C:305
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition: HashTableI.H:113
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
Definition: labelLists.C:44
A class for handling words, derived from Foam::string.
Definition: word.H:63
void clear()
Remove all entries from table.
Definition: HashTable.C:725
Map< word > names() const
Return a Map of (id => name)
Definition: cellTable.C:135
Map< word > materialTypes() const
Return a Map of (id => fluid|solid|shell)
Definition: cellTable.C:214
Reading is optional [identical to LAZY_READ].
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:336
static const word null
An empty word.
Definition: word.H:84
patchWriters clear()
Map< word > fluids() const
Return a Map of (id => name) for fluids.
Definition: cellTable.C:262
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
The cellTable persistent data saved as a Map<dictionary>.
Definition: cellTable.H:76
word name(const label id) const
Return the name corresponding to id.
Definition: cellTable.C:181
OBJstream os(runTime.globalPath()/outputName)
A subset of mesh cells.
Definition: cellZone.H:58
List< word > wordList
List of word.
Definition: fileName.H:59
void inplaceRenumber(const labelUList &oldToNew, IntListType &input)
Inplace renumber the values (not the indices) of a list.
const T & lookup(const Key &key, const T &deflt) const
Return hashed entry if it exists, or return the given default.
Definition: HashTableI.H:222
label nCells() const noexcept
Number of mesh cells.
bool match(const std::string &text, bool literal=false) const
Smart match as literal or regex, stopping on the first match.
Definition: wordResI.H:84
Nothing to be read.
Automatically write from objectRegistry::writeObject()
void combine(const dictionary &mapDict, labelList &tableIds)
Combine tableIds together.
Definition: cellTable.C:499
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:678
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:362
messageStream Info
Information stream (stdout output on master, null elsewhere)
void addCellZones(polyMesh &, const labelList &tableIds) const
Classify tableIds into cellZones according to the cellTable.
Definition: cellTable.C:433
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
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:485
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
List< label > labelList
A List of labels.
Definition: List.H:62
Registry of regIOobjects.
label findIndex(const word &name) const
Return index corresponding to name.
Definition: cellTable.C:195
bool found
void operator=(const this_type &rhs)
Copy assignment.
Definition: Map.H:134
Map< word > selectType(const word &materialType) const
Return a Map of (id => name) for materialType.
Definition: cellTable.C:231
cellTable()
Construct null.
Definition: cellTable.C:98
Do not request registration (bool: false)
ZoneMesh< cellZone, polyMesh > cellZoneMesh
A ZoneMesh with the type cellZone.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
A HashTable to objects of type <T> with a label key.