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 {
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 :
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 
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.found())
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 {
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 
288 void Foam::cellTable::setMaterial(const label id, const word& matlType)
289 {
290  setEntry(id, "MaterialType", matlType);
291 }
292 
293 
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.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
321  IOMap<dictionary> ioObj
322  (
323  IOobject
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
354  IOMap<dictionary> ioObj
355  (
356  IOobject
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 
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.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  }
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 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::cellTable::setName
void setName(const label, const word &)
Assign name.
Definition: cellTable.C:294
Foam::ZoneMesh::clear
void clear()
Clear the zones.
Definition: ZoneMesh.C:724
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::Map< dictionary >::const_iterator
typename parent_type::const_iterator const_iterator
Definition: Map.H:70
Foam::IOobject::AUTO_WRITE
Definition: IOobject.H:194
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::cellTable::selectType
Map< word > selectType(const word &materialType) const
Return a Map of (id => name) for materialType.
Definition: cellTable.C:238
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::cellTable::writeDict
void writeDict(const objectRegistry &, const word &name="cellTable", const fileName &instance="constant") const
Write constant/cellTable for later reuse.
Definition: cellTable.C:347
Foam::cellTable::readDict
void readDict(const objectRegistry &, const word &name="cellTable", const fileName &instance="constant")
Read constant/cellTable.
Definition: cellTable.C:312
Foam::cellTable::operator=
void operator=(const cellTable &)
Assignment.
Definition: cellTable.C:380
Foam::Map::operator=
void operator=(const this_type &rhs)
Copy assignment.
Definition: Map.H:117
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: lumpedPointController.H:69
Foam::cellTable::names
Map< word > names() const
Return a Map of (id => name)
Definition: cellTable.C:142
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
Foam::cellTable::materialTypes
Map< word > materialTypes() const
Return a Map of (id => fluid|solid|shell)
Definition: cellTable.C:221
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
cellTable.H
erase
srcOptions erase("case")
Foam::cellTable::append
label append(const dictionary &)
Append to the end, return index.
Definition: cellTable.C:126
Foam::Map< dictionary >::iterator
typename parent_type::iterator iterator
Definition: Map.H:69
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::inplaceRenumber
void inplaceRenumber(const labelUList &oldToNew, IntListType &input)
Inplace renumber the values (not the indices) of a list.
Definition: ListOpsTemplates.C:61
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::cellZone
A subset of mesh cells.
Definition: cellZone.H:62
OFstream.H
Foam::wordRes::match
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
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::cellTable::fluids
Map< word > fluids() const
Return a Map of (id => name) for fluids.
Definition: cellTable.C:269
Foam::primitiveMesh::nCells
label nCells() const noexcept
Number of mesh cells.
Definition: primitiveMeshI.H:96
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::polyMesh::cellZones
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:492
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
Foam::cellTable::name
word name(const label id) const
Return the name corresponding to id.
Definition: cellTable.C:188
Foam::cellTable
The cellTable persistent data saved as a Map<dictionary>.
Definition: cellTable.H:80
Foam::findIndex
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:406
Foam::cellTable::solids
Map< word > solids() const
Return a Map of (id => name) for solids.
Definition: cellTable.C:275
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:187
IOMap.H
Foam::ZoneMesh< cellZone, polyMesh >
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::radiation::lookup
Lookup type of boundary radiation properties.
Definition: lookup.H:63
Foam::cellTable::combine
void combine(const dictionary &mapDict, labelList &tableIds)
Combine tableIds together.
Definition: cellTable.C:506
forAllIters
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:223
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::cellTable::setMaterial
void setMaterial(const label, const word &)
Assign material Type.
Definition: cellTable.C:288
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::IStringStream
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:108
Foam::cellTable::findIndex
label findIndex(const word &name) const
Return index corresponding to name.
Definition: cellTable.C:202
Foam::ZoneMesh::names
wordList names() const
A list of the zone names.
Definition: ZoneMesh.C:305
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::IOobject::note
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:95
clear
patchWriters clear()
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::cellTable::cellTable
cellTable()
Construct null.
Definition: cellTable.C:105
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::cellTable::shells
Map< word > shells() const
Return a Map of (id => name) for shells.
Definition: cellTable.C:281
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header with current type()
Definition: IOobjectWriteHeader.C:277
Foam::IOobject::objectPath
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::IOobject::NO_READ
Definition: IOobject.H:188
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Foam::regIOobject::headerOk
bool headerOk()
Read and check header info.
Definition: regIOobject.C:436
Foam::cellTable::addCellZones
void addCellZones(polyMesh &, const labelList &tableIds) const
Classify tableIds into cellZones according to the cellTable.
Definition: cellTable.C:440
Foam::IOMap
A Map of objects of type <T> with automated input and output. Is a global object; i....
Definition: IOMap.H:54