1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 \*---------------------------------------------------------------------------*/
29 #include "cellTable.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::word Foam::cellTable::name(const label& id) const
171 word theName("cellTable_" + Foam::name(id));
173 const_iterator iter = find(id);
176 iter().readIfPresent("Label", theName);
183 Foam::label Foam::cellTable::findIndex(const word& name) const
190 forAllConstIter(Map<dictionary>, *this, iter)
192 if (iter().lookupOrDefault<word>("Label", word::null) == name)
202 Foam::Map<Foam::word> Foam::cellTable::materialTypes() const
206 forAllConstIter(Map<dictionary>, *this, iter)
211 iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
219 Foam::Map<Foam::word> Foam::cellTable::selectType(const word& matl) const
223 forAllConstIter(Map<dictionary>, *this, iter)
228 == iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
234 iter().lookupOrDefault<word>
237 "cellTable_" + Foam::name(iter.key())
247 Foam::Map<Foam::word> Foam::cellTable::fluids() const
249 return selectType("fluid");
253 Foam::Map<Foam::word> Foam::cellTable::solids() const
255 return selectType("solid");
259 Foam::Map<Foam::word> Foam::cellTable::shells() const
261 return selectType("shell");
266 void Foam::cellTable::setMaterial(const label& id, const word& matlType)
268 setEntry(id, "MaterialType", matlType);
272 void Foam::cellTable::setName(const label& id, const word& name)
274 setEntry(id, "Label", name);
278 void Foam::cellTable::setName(const label& id)
280 iterator iter = find(id);
282 if (iter == end() || !iter().found("Label"))
284 setName(id, "cellTable_" + ::Foam::name(id));
289 void Foam::cellTable::readDict
291 const objectRegistry& registry,
293 const fileName& instance
298 // read constant/dictName
299 IOMap<dictionary> ioObj
306 IOobject::READ_IF_PRESENT,
312 if (ioObj.headerOk())
319 Info<< "no constant/cellTable information available" << endl;
324 void Foam::cellTable::writeDict
326 const objectRegistry& registry,
328 const fileName& instance
331 // write constant/dictName
332 IOMap<dictionary> ioObj
346 "persistent data for thirdParty mesh <-> OpenFOAM translation";
348 Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
350 OFstream os(ioObj.objectPath());
351 ioObj.writeHeader(os);
356 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
358 void Foam::cellTable::operator=(const cellTable& rhs)
360 Map<dictionary>::operator=(rhs);
365 void Foam::cellTable::operator=(const Map<dictionary>& rhs)
367 Map<dictionary>::operator=(rhs);
372 void Foam::cellTable::operator=(const polyMesh& mesh)
374 Map<dictionary> zoneDict;
376 // create cellTableId and cellTable based on cellZones
377 label nZoneCells = 0;
379 wordList zoneNames = mesh.cellZones().names();
380 label unZonedType = zoneNames.size() + 1;
383 forAll(mesh.cellZones(), zoneI)
385 const cellZone& cZone = mesh.cellZones()[zoneI];
386 nZoneCells += cZone.size();
389 dict.add("Label", zoneNames[zoneI]);
390 zoneDict.insert(zoneI + 1, dict);
393 // collect unzoned cells
394 // special case: no zones at all - do entire mesh
401 if (mesh.nCells() > nZoneCells)
406 dictionary(IStringStream("Label cells;")())
410 Map<dictionary>::operator=(zoneDict);
415 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
417 void Foam::cellTable::addCellZones
420 const labelList& tableIds
423 Map<label> typeToZone = zoneMap();
424 List<DynamicList<label> > zoneCells(size());
426 forAll(tableIds, cellI)
428 Map<label>::const_iterator iter = typeToZone.find(tableIds[cellI]);
429 if (iter != typeToZone.end())
431 zoneCells[iter()].append(cellI);
435 // track which zones were actually used
436 labelList zoneUsed(zoneCells.size());
437 wordList zoneNames(namesList());
440 forAll(zoneCells, zoneI)
442 zoneCells[zoneI].shrink();
443 if (zoneCells[zoneI].size() > 0)
445 zoneUsed[nZone++] = zoneI;
448 zoneUsed.setSize(nZone);
450 cellZoneMesh& czMesh = mesh.cellZones();
455 Info<< "cellZones not used" << endl;
458 czMesh.setSize(nZone);
460 forAll(zoneUsed, zoneI)
462 const label origZoneI = zoneUsed[zoneI];
464 Info<< "cellZone " << zoneI
465 << " (size: " << zoneCells[origZoneI].size()
466 << ") name: " << zoneNames[origZoneI] << endl;
473 zoneNames[origZoneI],
474 zoneCells[origZoneI],
480 czMesh.writeOpt() = IOobject::AUTO_WRITE;
484 void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
492 labelList mapping(identity(max(this->toc()) + 1));
494 forAllConstIter (dictionary, mapDict, iter)
496 wordList zoneNames(iter().stream());
497 labelList zoneIndex(zoneNames.size());
500 forAll(zoneNames, zoneI)
502 zoneIndex[nElem] = this->findIndex(zoneNames[zoneI]);
503 if (zoneIndex[nElem] >= 0)
507 zoneNames[nElem] = zoneNames[zoneI];
513 zoneIndex.setSize(nElem);
514 zoneNames.setSize(nElem);
519 label targetId = this->findIndex(iter().keyword());
521 Info<< "combine cellTable: " << iter().keyword();
530 forAll(zoneNames, zoneI)
532 Info<< " " << zoneNames[zoneI];
536 // re-use the first element if possible
539 targetId = min(zoneIndex);
540 operator[](targetId).set("Label", iter().keyword());
543 forAll(zoneIndex, zoneI)
545 label idx = zoneIndex[zoneI];
546 if (idx != targetId && idx >= 0)
548 mapping[idx] = targetId;
557 inplaceRenumber(mapping, tableIds);
561 // ************************************************************************* //