1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "cellTable.H"
30 #include "stringListOps.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 const char* const Foam::cellTable::defaultMaterial_ = "fluid";
36 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
38 Foam::Map<Foam::label> Foam::cellTable::zoneMap() const
43 forAllConstIter(Map<dictionary>, *this, iter)
45 lookup.insert(iter.key(), zoneI++);
52 Foam::wordList Foam::cellTable::namesList() const
54 Map<word> lookup = names();
55 wordList lst(lookup.size());
58 forAllConstIter(Map<word>, lookup, iter)
60 lst[zoneI++] = iter();
67 void Foam::cellTable::addDefaults()
69 forAllIter(Map<dictionary>, *this, iter)
71 if (!iter().found("MaterialType"))
73 iter().add("MaterialType", defaultMaterial_);
79 void Foam::cellTable::setEntry
87 dict.add(keyWord, value);
89 iterator iter = find(id);
101 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
103 Foam::cellTable::cellTable()
109 Foam::cellTable::cellTable
111 const objectRegistry& registry,
113 const fileName& instance
118 readDict(registry, name, instance);
122 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
124 Foam::cellTable::~cellTable()
128 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
130 Foam::label Foam::cellTable::append(const dictionary& dict)
133 forAllConstIter(Map<dictionary>, *this, iter)
135 if (maxId < iter.key())
141 insert(++maxId, dict);
146 Foam::Map<Foam::word> Foam::cellTable::names() const
150 forAllConstIter(Map<dictionary>, *this, iter)
155 iter().lookupOrDefault<word>
158 "cellTable_" + Foam::name(iter.key())
167 Foam::Map<Foam::word> Foam::cellTable::names
169 const UList<wordRe>& patterns
174 forAllConstIter(Map<dictionary>, *this, iter)
176 word lookupName = iter().lookupOrDefault<word>
179 "cellTable_" + Foam::name(iter.key())
182 if (findStrings(patterns, lookupName))
184 lookup.insert(iter.key(), lookupName);
192 Foam::word Foam::cellTable::name(const label id) const
194 word theName("cellTable_" + Foam::name(id));
196 const_iterator iter = find(id);
199 iter().readIfPresent("Label", theName);
206 Foam::label Foam::cellTable::findIndex(const word& name) const
213 forAllConstIter(Map<dictionary>, *this, iter)
215 if (iter().lookupOrDefault<word>("Label", word::null) == name)
225 Foam::Map<Foam::word> Foam::cellTable::materialTypes() const
229 forAllConstIter(Map<dictionary>, *this, iter)
234 iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
242 Foam::Map<Foam::word> Foam::cellTable::selectType(const word& matl) const
246 forAllConstIter(Map<dictionary>, *this, iter)
251 == iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
257 iter().lookupOrDefault<word>
260 "cellTable_" + Foam::name(iter.key())
270 Foam::Map<Foam::word> Foam::cellTable::fluids() const
272 return selectType("fluid");
276 Foam::Map<Foam::word> Foam::cellTable::solids() const
278 return selectType("solid");
282 Foam::Map<Foam::word> Foam::cellTable::shells() const
284 return selectType("shell");
289 void Foam::cellTable::setMaterial(const label id, const word& matlType)
291 setEntry(id, "MaterialType", matlType);
295 void Foam::cellTable::setName(const label id, const word& name)
297 setEntry(id, "Label", name);
301 void Foam::cellTable::setName(const label id)
303 iterator iter = find(id);
305 if (iter == end() || !iter().found("Label"))
307 setName(id, "cellTable_" + Foam::name(id));
312 void Foam::cellTable::readDict
314 const objectRegistry& registry,
316 const fileName& instance
321 // read constant/dictName
322 IOMap<dictionary> ioObj
329 IOobject::READ_IF_PRESENT,
335 if (ioObj.headerOk())
342 Info<< "no constant/cellTable information available" << endl;
347 void Foam::cellTable::writeDict
349 const objectRegistry& registry,
351 const fileName& instance
354 // write constant/dictName
355 IOMap<dictionary> ioObj
369 "persistent data for thirdParty mesh <-> OpenFOAM translation";
371 Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
373 OFstream os(ioObj.objectPath());
374 ioObj.writeHeader(os);
379 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
381 void Foam::cellTable::operator=(const cellTable& rhs)
383 Map<dictionary>::operator=(rhs);
388 void Foam::cellTable::operator=(const Map<dictionary>& rhs)
390 Map<dictionary>::operator=(rhs);
395 void Foam::cellTable::operator=(const polyMesh& mesh)
397 Map<dictionary> zoneDict;
399 // create cellTableId and cellTable based on cellZones
400 label nZoneCells = 0;
402 wordList zoneNames = mesh.cellZones().names();
403 label unZonedType = zoneNames.size() + 1;
406 forAll(mesh.cellZones(), zoneI)
408 const cellZone& cZone = mesh.cellZones()[zoneI];
409 nZoneCells += cZone.size();
412 dict.add("Label", zoneNames[zoneI]);
413 zoneDict.insert(zoneI + 1, dict);
416 // collect unzoned cells
417 // special case: no zones at all - do entire mesh
424 if (mesh.nCells() > nZoneCells)
429 dictionary(IStringStream("Label cells;")())
433 Map<dictionary>::operator=(zoneDict);
438 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
440 void Foam::cellTable::addCellZones
443 const labelList& tableIds
446 Map<label> typeToZone = zoneMap();
447 List<DynamicList<label> > zoneCells(size());
449 forAll(tableIds, cellI)
451 Map<label>::const_iterator iter = typeToZone.find(tableIds[cellI]);
452 if (iter != typeToZone.end())
454 zoneCells[iter()].append(cellI);
458 // track which zones were actually used
459 labelList zoneUsed(zoneCells.size());
460 wordList zoneNames(namesList());
463 forAll(zoneCells, zoneI)
465 zoneCells[zoneI].shrink();
466 if (zoneCells[zoneI].size())
468 zoneUsed[nZone++] = zoneI;
471 zoneUsed.setSize(nZone);
473 cellZoneMesh& czMesh = mesh.cellZones();
478 Info<< "cellZones not used" << endl;
481 czMesh.setSize(nZone);
483 forAll(zoneUsed, zoneI)
485 const label origZoneI = zoneUsed[zoneI];
487 Info<< "cellZone " << zoneI
488 << " (size: " << zoneCells[origZoneI].size()
489 << ") name: " << zoneNames[origZoneI] << endl;
496 zoneNames[origZoneI],
497 zoneCells[origZoneI],
503 czMesh.writeOpt() = IOobject::AUTO_WRITE;
507 void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
514 Map<word> origNames(names());
515 labelList mapping(identity(max(origNames.toc()) + 1));
518 forAllConstIter(dictionary, mapDict, iter)
520 wordReList patterns(iter().stream());
524 forAllConstIter(Map<word>, origNames, namesIter)
526 if (findStrings(patterns, namesIter()))
528 matches.insert(namesIter.key(), namesIter());
534 label targetId = this->findIndex(iter().keyword());
536 Info<< "combine cellTable: " << iter().keyword();
539 // not found - reuse 1st element but with different name
540 targetId = min(matches.toc());
541 operator[](targetId).set("Label", iter().keyword());
551 // the mapping and name for targetId is already okay
552 matches.erase(targetId);
553 origNames.erase(targetId);
555 // remove matched names, leaving targetId on 'this'
556 this->erase(matches);
557 origNames.erase(matches);
559 forAllConstIter(Map<word>, matches, matchIter)
561 mapping[matchIter.key()] = targetId;
562 Info<< " " << matchIter();
572 inplaceRenumber(mapping, tableIds);
576 // ************************************************************************* //