1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 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"
30 #include <OpenFOAM/IOMap.H>
31 #include <OpenFOAM/OFstream.H>
32 #include <OpenFOAM/wordList.H>
33 #include <OpenFOAM/stringListOps.H>
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 const char* const Foam::cellTable::defaultMaterial_ = "fluid";
39 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 Foam::Map<Foam::label> Foam::cellTable::zoneMap() const
46 forAllConstIter(Map<dictionary>, *this, iter)
48 lookup.insert(iter.key(), zoneI++);
55 Foam::wordList Foam::cellTable::namesList() const
57 Map<word> lookup = names();
58 wordList lst(lookup.size());
61 forAllConstIter(Map<word>, lookup, iter)
63 lst[zoneI++] = iter();
70 void Foam::cellTable::addDefaults()
72 forAllIter(Map<dictionary>, *this, iter)
74 if (!iter().found("MaterialType"))
76 iter().add("MaterialType", defaultMaterial_);
82 void Foam::cellTable::setEntry
90 dict.add(keyWord, value);
92 iterator iter = find(id);
104 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
106 Foam::cellTable::cellTable()
112 Foam::cellTable::cellTable
114 const objectRegistry& registry,
116 const fileName& instance
121 readDict(registry, name, instance);
125 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
127 Foam::cellTable::~cellTable()
131 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
133 Foam::label Foam::cellTable::append(const dictionary& dict)
136 forAllConstIter(Map<dictionary>, *this, iter)
138 if (maxId < iter.key())
144 insert(++maxId, dict);
149 Foam::Map<Foam::word> Foam::cellTable::names() const
153 forAllConstIter(Map<dictionary>, *this, iter)
158 iter().lookupOrDefault<word>
161 "cellTable_" + Foam::name(iter.key())
170 Foam::Map<Foam::word> Foam::cellTable::names
172 const List<wordRe>& patterns
177 forAllConstIter(Map<dictionary>, *this, iter)
179 word lookupName = iter().lookupOrDefault<word>
182 "cellTable_" + Foam::name(iter.key())
185 if (findStrings(patterns, lookupName))
187 lookup.insert(iter.key(), lookupName);
195 Foam::word Foam::cellTable::name(const label& id) const
197 word theName("cellTable_" + Foam::name(id));
199 const_iterator iter = find(id);
202 iter().readIfPresent("Label", theName);
209 Foam::label Foam::cellTable::findIndex(const word& name) const
216 forAllConstIter(Map<dictionary>, *this, iter)
218 if (iter().lookupOrDefault<word>("Label", word::null) == name)
228 Foam::Map<Foam::word> Foam::cellTable::materialTypes() const
232 forAllConstIter(Map<dictionary>, *this, iter)
237 iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
245 Foam::Map<Foam::word> Foam::cellTable::selectType(const word& matl) const
249 forAllConstIter(Map<dictionary>, *this, iter)
254 == iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
260 iter().lookupOrDefault<word>
263 "cellTable_" + Foam::name(iter.key())
273 Foam::Map<Foam::word> Foam::cellTable::fluids() const
275 return selectType("fluid");
279 Foam::Map<Foam::word> Foam::cellTable::solids() const
281 return selectType("solid");
285 Foam::Map<Foam::word> Foam::cellTable::shells() const
287 return selectType("shell");
292 void Foam::cellTable::setMaterial(const label& id, const word& matlType)
294 setEntry(id, "MaterialType", matlType);
298 void Foam::cellTable::setName(const label& id, const word& name)
300 setEntry(id, "Label", name);
304 void Foam::cellTable::setName(const label& id)
306 iterator iter = find(id);
308 if (iter == end() || !iter().found("Label"))
310 setName(id, "cellTable_" + Foam::name(id));
315 void Foam::cellTable::readDict
317 const objectRegistry& registry,
319 const fileName& instance
324 // read constant/dictName
325 IOMap<dictionary> ioObj
332 IOobject::READ_IF_PRESENT,
338 if (ioObj.headerOk())
345 Info<< "no constant/cellTable information available" << endl;
350 void Foam::cellTable::writeDict
352 const objectRegistry& registry,
354 const fileName& instance
357 // write constant/dictName
358 IOMap<dictionary> ioObj
372 "persistent data for thirdParty mesh <-> OpenFOAM translation";
374 Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
376 OFstream os(ioObj.objectPath());
377 ioObj.writeHeader(os);
382 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
384 void Foam::cellTable::operator=(const cellTable& rhs)
386 Map<dictionary>::operator=(rhs);
391 void Foam::cellTable::operator=(const Map<dictionary>& rhs)
393 Map<dictionary>::operator=(rhs);
398 void Foam::cellTable::operator=(const polyMesh& mesh)
400 Map<dictionary> zoneDict;
402 // create cellTableId and cellTable based on cellZones
403 label nZoneCells = 0;
405 wordList zoneNames = mesh.cellZones().names();
406 label unZonedType = zoneNames.size() + 1;
409 forAll(mesh.cellZones(), zoneI)
411 const cellZone& cZone = mesh.cellZones()[zoneI];
412 nZoneCells += cZone.size();
415 dict.add("Label", zoneNames[zoneI]);
416 zoneDict.insert(zoneI + 1, dict);
419 // collect unzoned cells
420 // special case: no zones at all - do entire mesh
427 if (mesh.nCells() > nZoneCells)
432 dictionary(IStringStream("Label cells;")())
436 Map<dictionary>::operator=(zoneDict);
441 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
443 void Foam::cellTable::addCellZones
446 const labelList& tableIds
449 Map<label> typeToZone = zoneMap();
450 List<DynamicList<label> > zoneCells(size());
452 forAll(tableIds, cellI)
454 Map<label>::const_iterator iter = typeToZone.find(tableIds[cellI]);
455 if (iter != typeToZone.end())
457 zoneCells[iter()].append(cellI);
461 // track which zones were actually used
462 labelList zoneUsed(zoneCells.size());
463 wordList zoneNames(namesList());
466 forAll(zoneCells, zoneI)
468 zoneCells[zoneI].shrink();
469 if (zoneCells[zoneI].size())
471 zoneUsed[nZone++] = zoneI;
474 zoneUsed.setSize(nZone);
476 cellZoneMesh& czMesh = mesh.cellZones();
481 Info<< "cellZones not used" << endl;
484 czMesh.setSize(nZone);
486 forAll(zoneUsed, zoneI)
488 const label origZoneI = zoneUsed[zoneI];
490 Info<< "cellZone " << zoneI
491 << " (size: " << zoneCells[origZoneI].size()
492 << ") name: " << zoneNames[origZoneI] << endl;
499 zoneNames[origZoneI],
500 zoneCells[origZoneI],
506 czMesh.writeOpt() = IOobject::AUTO_WRITE;
510 void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
517 Map<word> origNames(names());
518 labelList mapping(identity(max(origNames.toc()) + 1));
521 forAllConstIter(dictionary, mapDict, iter)
523 wordReList patterns(iter().stream());
527 forAllConstIter(Map<word>, origNames, namesIter)
529 if (findStrings(patterns, namesIter()))
531 matches.insert(namesIter.key(), namesIter());
537 label targetId = this->findIndex(iter().keyword());
539 Info<< "combine cellTable: " << iter().keyword();
542 // not found - reuse 1st element but with different name
543 targetId = min(matches.toc());
544 operator[](targetId).set("Label", iter().keyword());
554 // the mapping and name for targetId is already okay
555 matches.erase(targetId);
556 origNames.erase(targetId);
558 // remove matched names, leaving targetId on 'this'
559 this->erase(matches);
560 origNames.erase(matches);
562 forAllConstIter(Map<word>, matches, matchIter)
564 mapping[matchIter.key()] = targetId;
565 Info<< " " << matchIter();
575 inplaceRenumber(mapping, tableIds);
579 // ************************ vim: set sw=4 sts=4 et: ************************ //