cellTable.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "cellTable.H"
27 #include "IOMap.H"
28 #include "OFstream.H"
29 #include "wordList.H"
30 #include "stringListOps.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const char* const Foam::cellTable::defaultMaterial_ = "fluid";
35 
36 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
37 
38 Foam::Map<Foam::label> Foam::cellTable::zoneMap() const
39 {
40  Map<label> lookup;
41 
42  label zoneI = 0;
43  forAllConstIter(Map<dictionary>, *this, iter)
44  {
45  lookup.insert(iter.key(), zoneI++);
46  }
47 
48  return lookup;
49 }
50 
51 
52 Foam::wordList Foam::cellTable::namesList() const
53 {
54  Map<word> lookup = names();
55  wordList lst(lookup.size());
56 
57  label zoneI = 0;
58  forAllConstIter(Map<word>, lookup, iter)
59  {
60  lst[zoneI++] = iter();
61  }
62 
63  return lst;
64 }
65 
66 
67 void Foam::cellTable::addDefaults()
68 {
69  forAllIter(Map<dictionary>, *this, iter)
70  {
71  if (!iter().found("MaterialType"))
72  {
73  iter().add("MaterialType", defaultMaterial_);
74  }
75  }
76 }
77 
78 
79 void Foam::cellTable::setEntry
80 (
81  const label id,
82  const word& keyWord,
83  const word& value
84 )
85 {
86  dictionary dict;
87  dict.add(keyWord, value);
88 
89  iterator iter = find(id);
90  if (iter != end())
91  {
92  iter().merge(dict);
93  }
94  else
95  {
96  insert(id, dict);
97  }
98 }
99 
100 
101 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
102 
104 :
105  Map<dictionary>()
106 {}
107 
108 
110 (
111  const objectRegistry& registry,
112  const word& name,
113  const fileName& instance
114 )
115 :
117 {
118  readDict(registry, name, instance);
119 }
120 
121 
122 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
123 
125 {}
126 
127 
128 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
129 
131 {
132  label maxId = -1;
133  forAllConstIter(Map<dictionary>, *this, iter)
134  {
135  if (maxId < iter.key())
136  {
137  maxId = iter.key();
138  }
139  }
140 
141  insert(++maxId, dict);
142  return maxId;
143 }
144 
145 
147 {
149 
150  forAllConstIter(Map<dictionary>, *this, iter)
151  {
152  lookup.insert
153  (
154  iter.key(),
155  iter().lookupOrDefault<word>
156  (
157  "Label",
158  "cellTable_" + Foam::name(iter.key())
159  )
160  );
161  }
162 
163  return lookup;
164 }
165 
166 
168 (
169  const UList<wordRe>& patterns
170 ) const
171 {
173 
174  forAllConstIter(Map<dictionary>, *this, iter)
175  {
176  word lookupName = iter().lookupOrDefault<word>
177  (
178  "Label",
179  "cellTable_" + Foam::name(iter.key())
180  );
181 
182  if (findStrings(patterns, lookupName))
183  {
184  lookup.insert(iter.key(), lookupName);
185  }
186  }
187 
188  return lookup;
189 }
190 
191 
193 {
194  word theName("cellTable_" + Foam::name(id));
195 
196  const_iterator iter = find(id);
197  if (iter != end())
198  {
199  iter().readIfPresent("Label", theName);
200  }
201 
202  return theName;
203 }
204 
205 
207 {
208  if (name.empty())
209  {
210  return -1;
211  }
212 
213  forAllConstIter(Map<dictionary>, *this, iter)
214  {
215  if (iter().lookupOrDefault<word>("Label", word::null) == name)
216  {
217  return iter.key();
218  }
219  }
220 
221  return -1;
222 }
223 
224 
226 {
228 
229  forAllConstIter(Map<dictionary>, *this, iter)
230  {
231  lookup.insert
232  (
233  iter.key(),
234  iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
235  );
236  }
237 
238  return lookup;
239 }
240 
241 
243 {
245 
246  forAllConstIter(Map<dictionary>, *this, iter)
247  {
248  if
249  (
250  matl
251  == iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
252  )
253  {
254  lookup.insert
255  (
256  iter.key(),
257  iter().lookupOrDefault<word>
258  (
259  "Label",
260  "cellTable_" + Foam::name(iter.key())
261  )
262  );
263  }
264  }
265 
266  return lookup;
267 }
268 
269 
271 {
272  return selectType("fluid");
273 }
274 
275 
277 {
278  return selectType("solid");
279 }
280 
281 
283 {
284  return selectType("shell");
285 }
286 
287 
288 
289 void Foam::cellTable::setMaterial(const label id, const word& matlType)
290 {
291  setEntry(id, "MaterialType", matlType);
292 }
293 
294 
295 void Foam::cellTable::setName(const label id, const word& name)
296 {
297  setEntry(id, "Label", name);
298 }
299 
300 
302 {
303  iterator iter = find(id);
304 
305  if (iter == end() || !iter().found("Label"))
306  {
307  setName(id, "cellTable_" + Foam::name(id));
308  }
309 }
310 
311 
313 (
314  const objectRegistry& registry,
315  const word& name,
316  const fileName& instance
317 )
318 {
319  clear();
320 
321  // read constant/dictName
322  IOMap<dictionary> ioObj
323  (
324  IOobject
325  (
326  name,
327  instance,
328  registry,
331  false
332  )
333  );
334 
335  if (ioObj.headerOk())
336  {
337  *this = ioObj;
338  addDefaults();
339  }
340  else
341  {
342  Info<< "no constant/cellTable information available" << endl;
343  }
344 }
345 
346 
348 (
349  const objectRegistry& registry,
350  const word& name,
351  const fileName& instance
352 ) const
353 {
354  // write constant/dictName
355  IOMap<dictionary> ioObj
356  (
357  IOobject
358  (
359  name,
360  instance,
361  registry,
364  false
365  )
366  );
367 
368  ioObj.note() =
369  "persistent data for thirdParty mesh <-> OpenFOAM translation";
370 
371  Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
372 
373  OFstream os(ioObj.objectPath());
374  ioObj.writeHeader(os);
375  os << *this;
376 }
377 
378 
379 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
380 
382 {
384  addDefaults();
385 }
386 
387 
389 {
391  addDefaults();
392 }
393 
394 
396 {
397  Map<dictionary> zoneDict;
398 
399  // create cellTableId and cellTable based on cellZones
400  label nZoneCells = 0;
401 
402  wordList zoneNames = mesh.cellZones().names();
403  label unZonedType = zoneNames.size() + 1;
404 
405  // do cell zones
406  forAll(mesh.cellZones(), zoneI)
407  {
408  const cellZone& cZone = mesh.cellZones()[zoneI];
409  nZoneCells += cZone.size();
410 
412  dict.add("Label", zoneNames[zoneI]);
413  zoneDict.insert(zoneI + 1, dict);
414  }
415 
416  // collect unzoned cells
417  // special case: no zones at all - do entire mesh
418  if (nZoneCells == 0)
419  {
420  zoneDict.clear();
421  unZonedType = 1;
422  }
423 
424  if (mesh.nCells() > nZoneCells)
425  {
426  zoneDict.insert
427  (
428  unZonedType,
429  dictionary(IStringStream("Label cells;")())
430  );
431  }
432 
433  Map<dictionary>::operator=(zoneDict);
434  addDefaults();
435 }
436 
437 
438 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
439 
441 (
442  polyMesh& mesh,
443  const labelList& tableIds
444 ) const
445 {
446  Map<label> typeToZone = zoneMap();
447  List<DynamicList<label>> zoneCells(size());
448 
449  forAll(tableIds, celli)
450  {
451  Map<label>::const_iterator iter = typeToZone.find(tableIds[celli]);
452  if (iter != typeToZone.end())
453  {
454  zoneCells[iter()].append(celli);
455  }
456  }
457 
458  // track which zones were actually used
459  labelList zoneUsed(zoneCells.size());
460  wordList zoneNames(namesList());
461 
462  label nZone = 0;
463  forAll(zoneCells, zoneI)
464  {
465  zoneCells[zoneI].shrink();
466  if (zoneCells[zoneI].size())
467  {
468  zoneUsed[nZone++] = zoneI;
469  }
470  }
471  zoneUsed.setSize(nZone);
472 
473  meshCellZones& czMesh = mesh.cellZones();
474 
475  czMesh.clear();
476  if (nZone <= 1)
477  {
478  Info<< "cellZones not used" << endl;
479  return;
480  }
481  czMesh.setSize(nZone);
482 
483  forAll(zoneUsed, zoneI)
484  {
485  const label origZoneI = zoneUsed[zoneI];
486 
487  Info<< "cellZone " << zoneI
488  << " (size: " << zoneCells[origZoneI].size()
489  << ") name: " << zoneNames[origZoneI] << endl;
490 
491  czMesh.set
492  (
493  zoneI,
494  new cellZone
495  (
496  zoneNames[origZoneI],
497  zoneCells[origZoneI],
498  zoneI,
499  czMesh
500  )
501  );
502  }
503  czMesh.writeOpt() = IOobject::AUTO_WRITE;
504 }
505 
506 
507 void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
508 {
509  if (mapDict.empty())
510  {
511  return;
512  }
513 
514  Map<word> origNames(names());
515  labelList mapping(identity(max(origNames.toc()) + 1));
516 
517  bool remap = false;
518  forAllConstIter(dictionary, mapDict, iter)
519  {
520  wordReList patterns(iter().stream());
521 
522  // find all matches
523  Map<word> matches;
524  forAllConstIter(Map<word>, origNames, namesIter)
525  {
526  if (findStrings(patterns, namesIter()))
527  {
528  matches.insert(namesIter.key(), namesIter());
529  }
530  }
531 
532  if (matches.size())
533  {
534  label targetId = this->findIndex(iter().keyword());
535 
536  Info<< "combine cellTable: " << iter().keyword();
537  if (targetId < 0)
538  {
539  // not found - reuse 1st element but with different name
540  targetId = min(matches.toc());
541  operator[](targetId).set("Label", iter().keyword());
542 
543  Info<< " = (";
544  }
545  else
546  {
547  Info<< " += (";
548  }
549 
550 
551  // the mapping and name for targetId is already okay
552  matches.erase(targetId);
553  origNames.erase(targetId);
554 
555  // remove matched names, leaving targetId on 'this'
556  this->erase(matches);
557  origNames.erase(matches);
558 
559  forAllConstIter(Map<word>, matches, matchIter)
560  {
561  mapping[matchIter.key()] = targetId;
562  Info<< " " << matchIter();
563  }
564  Info<< " )" << endl;
565 
566  remap = true;
567  }
568  }
569 
570  if (remap)
571  {
572  inplaceRenumber(mapping, tableIds);
573  }
574 }
575 
576 // ************************************************************************* //
label append(const dictionary &)
Append to the end, return index.
Definition: cellTable.C:130
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const word & name() const
Return name.
Definition: IOobject.H:303
A class for handling file names.
Definition: fileName.H:79
bool set(const label) const
Is element set.
Definition: PtrListI.H:65
void inplaceRenumber(const labelUList &oldToNew, ListType &)
Inplace renumber the values of a list.
~cellTable()
Destructor.
Definition: cellTable.C:124
An STL-conforming const_iterator.
Definition: HashTable.H:481
Map< word > shells() const
Return a Map of (id => name) for shells.
Definition: cellTable.C:282
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112
const meshCellZones & cellZones() const
Return cell zones.
Definition: polyMesh.H:482
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
HashTable< dictionary, label, Hash< label > >::iterator iterator
Definition: Map.H:56
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:459
Output to file stream.
Definition: OFstream.H:82
void setName(const label, const word &)
Assign name.
Definition: cellTable.C:295
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
label nCells() const
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Map< word > solids() const
Return a Map of (id => name) for solids.
Definition: cellTable.C:276
bool headerOk()
Read and check header info.
Definition: regIOobject.C:397
labelList identity(const label len)
Create identity map (map[i] == i) of given length.
Definition: ListOps.C:104
A Map of objects of type <T> with automated input and output. Is a global object; i...
Definition: IOMap.H:51
Operations on lists of strings.
void clear()
Clear the zones.
Definition: MeshZones.C:401
void setMaterial(const label, const word &)
Assign material Type.
Definition: cellTable.C:289
void operator=(const cellTable &)
Assignment.
Definition: cellTable.C:381
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1133
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:371
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
void writeDict(const objectRegistry &, const word &name="cellTable", const fileName &instance="constant") const
Write constant/cellTable for later reuse.
Definition: cellTable.C:348
wordList names() const
Return a list of zone names.
Definition: MeshZones.C:256
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:142
An STL-conforming iterator.
Definition: HashTable.H:426
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:321
void readDict(const objectRegistry &, const word &name="cellTable", const fileName &instance="constant")
Read constant/cellTable.
Definition: cellTable.C:313
stressControl lookup("compactNormalStress") >> compactNormalStress
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:113
A class for handling words, derived from string.
Definition: word.H:59
void clear()
Clear all entries from table.
Definition: HashTable.C:468
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
void combine(const dictionary &, labelList &tableIds)
Combine tableIds together.
Definition: cellTable.C:507
Map< word > names() const
Return a Map of (id => name)
Definition: cellTable.C:146
Map< word > materialTypes() const
Return a Map of (id => fluid|solid|shell)
Definition: cellTable.C:225
static const word null
An empty word.
Definition: word.H:77
Map< word > fluids() const
Return a Map of (id => name) for fluids.
Definition: cellTable.C:270
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
The cellTable persistent data saved as a Map<dictionary>.
Definition: cellTable.H:77
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
word name(const label id) const
Return the name corresponding to id.
Definition: cellTable.C:192
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
A subset of mesh cells.
Definition: cellZone.H:61
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
List< word > wordList
A List of words.
Definition: fileName.H:54
void setSize(const label)
Reset size of List.
Definition: List.C:281
writeOption writeOpt() const
Definition: IOobject.H:363
Input from memory buffer stream.
Definition: IStringStream.H:49
bool writeHeader(Ostream &) const
Write header.
messageStream Info
void addCellZones(polyMesh &, const labelList &tableIds) const
Classify tableIds into cellZones according to the cellTable.
Definition: cellTable.C:441
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
T & operator[](const Key &)
Find and return a hashedEntry.
Definition: HashTableI.H:103
List< label > toc() const
Return the table of contents.
Definition: HashTable.C:202
Registry of regIOobjects.
label findIndex(const word &name) const
Return index corresponding to name.
Definition: cellTable.C:206
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:419
Map< word > selectType(const word &materialType) const
Return a Map of (id => name) for materialType.
Definition: cellTable.C:242
void operator=(const Map< T > &map)
Definition: Map.H:102
cellTable()
Construct null.
Definition: cellTable.C:103
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49