1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
26 \*---------------------------------------------------------------------------*/
28 #include "cellTable.H"
32 #include "stringListOps.H"
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 const char* const Foam::cellTable::defaultMaterial_ = "fluid";
38 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
40 Foam::Map<Foam::label> Foam::cellTable::zoneMap() const
45 forAllConstIter(Map<dictionary>, *this, iter)
47 lookup.insert(iter.key(), zoneI++);
54 Foam::wordList Foam::cellTable::namesList() const
56 Map<word> lookup = names();
57 wordList lst(lookup.size());
60 forAllConstIter(Map<word>, lookup, iter)
62 lst[zoneI++] = iter();
69 void Foam::cellTable::addDefaults()
71 forAllIter(Map<dictionary>, *this, iter)
73 if (!iter().found("MaterialType"))
75 iter().add("MaterialType", defaultMaterial_);
81 void Foam::cellTable::setEntry
89 dict.add(keyWord, value);
91 iterator iter = find(id);
103 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
105 Foam::cellTable::cellTable()
111 Foam::cellTable::cellTable
113 const objectRegistry& registry,
115 const fileName& instance
120 readDict(registry, name, instance);
124 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
126 Foam::cellTable::~cellTable()
130 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
132 Foam::label Foam::cellTable::append(const dictionary& dict)
135 forAllConstIter(Map<dictionary>, *this, iter)
137 if (maxId < iter.key())
143 insert(++maxId, dict);
148 Foam::Map<Foam::word> Foam::cellTable::names() const
152 forAllConstIter(Map<dictionary>, *this, iter)
157 iter().lookupOrDefault<word>
160 "cellTable_" + Foam::name(iter.key())
169 Foam::Map<Foam::word> Foam::cellTable::names
171 const List<wordRe>& patterns
176 forAllConstIter(Map<dictionary>, *this, iter)
178 word lookupName = iter().lookupOrDefault<word>
181 "cellTable_" + Foam::name(iter.key())
184 if (findStrings(patterns, lookupName))
186 lookup.insert(iter.key(), lookupName);
194 Foam::word Foam::cellTable::name(const label& id) const
196 word theName("cellTable_" + Foam::name(id));
198 const_iterator iter = find(id);
201 iter().readIfPresent("Label", theName);
208 Foam::label Foam::cellTable::findIndex(const word& name) const
215 forAllConstIter(Map<dictionary>, *this, iter)
217 if (iter().lookupOrDefault<word>("Label", word::null) == name)
227 Foam::Map<Foam::word> Foam::cellTable::materialTypes() const
231 forAllConstIter(Map<dictionary>, *this, iter)
236 iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
244 Foam::Map<Foam::word> Foam::cellTable::selectType(const word& matl) const
248 forAllConstIter(Map<dictionary>, *this, iter)
253 == iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
259 iter().lookupOrDefault<word>
262 "cellTable_" + Foam::name(iter.key())
272 Foam::Map<Foam::word> Foam::cellTable::fluids() const
274 return selectType("fluid");
278 Foam::Map<Foam::word> Foam::cellTable::solids() const
280 return selectType("solid");
284 Foam::Map<Foam::word> Foam::cellTable::shells() const
286 return selectType("shell");
291 void Foam::cellTable::setMaterial(const label& id, const word& matlType)
293 setEntry(id, "MaterialType", matlType);
297 void Foam::cellTable::setName(const label& id, const word& name)
299 setEntry(id, "Label", name);
303 void Foam::cellTable::setName(const label& id)
305 iterator iter = find(id);
307 if (iter == end() || !iter().found("Label"))
309 setName(id, "cellTable_" + Foam::name(id));
314 void Foam::cellTable::readDict
316 const objectRegistry& registry,
318 const fileName& instance
323 // read constant/dictName
324 IOMap<dictionary> ioObj
331 IOobject::READ_IF_PRESENT,
337 if (ioObj.headerOk())
344 Info<< "no constant/cellTable information available" << endl;
349 void Foam::cellTable::writeDict
351 const objectRegistry& registry,
353 const fileName& instance
356 // write constant/dictName
357 IOMap<dictionary> ioObj
371 "persistent data for thirdParty mesh <-> foam translation";
373 Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
375 OFstream os(ioObj.objectPath());
376 ioObj.writeHeader(os);
381 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
383 void Foam::cellTable::operator=(const cellTable& rhs)
385 Map<dictionary>::operator=(rhs);
390 void Foam::cellTable::operator=(const Map<dictionary>& rhs)
392 Map<dictionary>::operator=(rhs);
397 void Foam::cellTable::operator=(const polyMesh& mesh)
399 Map<dictionary> zoneDict;
401 // create cellTableId and cellTable based on cellZones
402 label nZoneCells = 0;
404 wordList zoneNames = mesh.cellZones().names();
405 label unZonedType = zoneNames.size() + 1;
408 forAll(mesh.cellZones(), zoneI)
410 const cellZone& cZone = mesh.cellZones()[zoneI];
411 nZoneCells += cZone.size();
414 dict.add("Label", zoneNames[zoneI]);
415 zoneDict.insert(zoneI + 1, dict);
418 // collect unzoned cells
419 // special case: no zones at all - do entire mesh
426 if (mesh.nCells() > nZoneCells)
431 dictionary(IStringStream("Label cells;")())
435 Map<dictionary>::operator=(zoneDict);
440 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
442 void Foam::cellTable::addCellZones
445 const labelList& tableIds
448 Map<label> typeToZone = zoneMap();
449 List<dynamicLabelList > zoneCells(size());
451 forAll(tableIds, cellI)
453 Map<label>::const_iterator iter = typeToZone.find(tableIds[cellI]);
454 if (iter != typeToZone.end())
456 zoneCells[iter()].append(cellI);
460 // track which zones were actually used
461 labelList zoneUsed(zoneCells.size());
462 wordList zoneNames(namesList());
465 forAll(zoneCells, zoneI)
467 zoneCells[zoneI].shrink();
468 if (zoneCells[zoneI].size())
470 zoneUsed[nZone++] = zoneI;
473 zoneUsed.setSize(nZone);
475 cellZoneMesh& czMesh = mesh.cellZones();
480 Info<< "cellZones not used" << endl;
483 czMesh.setSize(nZone);
485 forAll(zoneUsed, zoneI)
487 const label origZoneI = zoneUsed[zoneI];
489 Info<< "cellZone " << zoneI
490 << " (size: " << zoneCells[origZoneI].size()
491 << ") name: " << zoneNames[origZoneI] << endl;
498 zoneNames[origZoneI],
499 zoneCells[origZoneI],
505 czMesh.writeOpt() = IOobject::AUTO_WRITE;
509 void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
516 Map<word> origNames(names());
517 labelList mapping(identity(max(origNames.toc()) + 1));
520 forAllConstIter(dictionary, mapDict, iter)
522 wordReList patterns(iter().stream());
526 forAllConstIter(Map<word>, origNames, namesIter)
528 if (findStrings(patterns, namesIter()))
530 matches.insert(namesIter.key(), namesIter());
536 label targetId = this->findIndex(iter().keyword());
538 Info<< "combine cellTable: " << iter().keyword();
541 // not found - reuse 1st element but with different name
542 targetId = min(matches.toc());
543 operator[](targetId).set("Label", iter().keyword());
553 // the mapping and name for targetId is already okay
554 matches.erase(targetId);
555 origNames.erase(targetId);
557 // remove matched names, leaving targetId on 'this'
558 this->erase(matches);
559 origNames.erase(matches);
561 forAllConstIter(Map<word>, matches, matchIter)
563 mapping[matchIter.key()] = targetId;
564 Info<< " " << matchIter();
574 inplaceRenumber(mapping, tableIds);
578 // ************************************************************************* //