OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

cellTable.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "cellTable.H"
00030 #include "IOMap.H"
00031 #include "OFstream.H"
00032 #include "wordList.H"
00033 #include "stringListOps.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 const char* const Foam::cellTable::defaultMaterial_ = "fluid";
00038 
00039 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00040 
00041 Foam::Map<Foam::label> Foam::cellTable::zoneMap() const
00042 {
00043     Map<label> lookup;
00044 
00045     label zoneI = 0;
00046     forAllConstIter(Map<dictionary>, *this, iter)
00047     {
00048         lookup.insert(iter.key(), zoneI++);
00049     }
00050 
00051     return lookup;
00052 }
00053 
00054 
00055 Foam::wordList Foam::cellTable::namesList() const
00056 {
00057     Map<word> lookup = names();
00058     wordList lst(lookup.size());
00059 
00060     label zoneI = 0;
00061     forAllConstIter(Map<word>, lookup, iter)
00062     {
00063         lst[zoneI++] = iter();
00064     }
00065 
00066     return lst;
00067 }
00068 
00069 
00070 void Foam::cellTable::addDefaults()
00071 {
00072     forAllIter(Map<dictionary>, *this, iter)
00073     {
00074         if (!iter().found("MaterialType"))
00075         {
00076             iter().add("MaterialType", defaultMaterial_);
00077         }
00078     }
00079 }
00080 
00081 
00082 void Foam::cellTable::setEntry
00083 (
00084     const label& id,
00085     const word& keyWord,
00086     const word& value
00087 )
00088 {
00089     dictionary dict;
00090     dict.add(keyWord, value);
00091 
00092     iterator iter = find(id);
00093     if (iter != end())
00094     {
00095         iter().merge(dict);
00096     }
00097     else
00098     {
00099         insert(id, dict);
00100     }
00101 }
00102 
00103 
00104 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00105 
00106 Foam::cellTable::cellTable()
00107 :
00108     Map<dictionary>()
00109 {}
00110 
00111 
00112 Foam::cellTable::cellTable
00113 (
00114     const objectRegistry& registry,
00115     const word& name,
00116     const fileName& instance
00117 )
00118 :
00119     Map<dictionary>()
00120 {
00121     readDict(registry, name, instance);
00122 }
00123 
00124 
00125 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00126 
00127 Foam::cellTable::~cellTable()
00128 {}
00129 
00130 
00131 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
00132 
00133 Foam::label Foam::cellTable::append(const dictionary& dict)
00134 {
00135     label maxId = -1;
00136     forAllConstIter(Map<dictionary>, *this, iter)
00137     {
00138         if (maxId < iter.key())
00139         {
00140             maxId = iter.key();
00141         }
00142     }
00143 
00144     insert(++maxId, dict);
00145     return maxId;
00146 }
00147 
00148 
00149 Foam::Map<Foam::word> Foam::cellTable::names() const
00150 {
00151     Map<word> lookup;
00152 
00153     forAllConstIter(Map<dictionary>, *this, iter)
00154     {
00155         lookup.insert
00156         (
00157             iter.key(),
00158             iter().lookupOrDefault<word>
00159             (
00160                 "Label",
00161                 "cellTable_" + Foam::name(iter.key())
00162             )
00163         );
00164     }
00165 
00166     return lookup;
00167 }
00168 
00169 
00170 Foam::Map<Foam::word> Foam::cellTable::names
00171 (
00172     const List<wordRe>& patterns
00173 ) const
00174 {
00175     Map<word> lookup;
00176 
00177     forAllConstIter(Map<dictionary>, *this, iter)
00178     {
00179         word lookupName = iter().lookupOrDefault<word>
00180         (
00181             "Label",
00182             "cellTable_" + Foam::name(iter.key())
00183         );
00184 
00185         if (findStrings(patterns, lookupName))
00186         {
00187             lookup.insert(iter.key(), lookupName);
00188         }
00189     }
00190 
00191     return lookup;
00192 }
00193 
00194 
00195 Foam::word Foam::cellTable::name(const label& id) const
00196 {
00197     word theName("cellTable_" + Foam::name(id));
00198 
00199     const_iterator iter = find(id);
00200     if (iter != end())
00201     {
00202         iter().readIfPresent("Label", theName);
00203     }
00204 
00205     return theName;
00206 }
00207 
00208 
00209 Foam::label Foam::cellTable::findIndex(const word& name) const
00210 {
00211     if (name.empty())
00212     {
00213         return -1;
00214     }
00215 
00216     forAllConstIter(Map<dictionary>, *this, iter)
00217     {
00218         if (iter().lookupOrDefault<word>("Label", word::null) == name)
00219         {
00220             return iter.key();
00221         }
00222     }
00223 
00224     return -1;
00225 }
00226 
00227 
00228 Foam::Map<Foam::word> Foam::cellTable::materialTypes() const
00229 {
00230     Map<word> lookup;
00231 
00232     forAllConstIter(Map<dictionary>, *this, iter)
00233     {
00234         lookup.insert
00235         (
00236             iter.key(),
00237             iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
00238         );
00239     }
00240 
00241     return lookup;
00242 }
00243 
00244 
00245 Foam::Map<Foam::word> Foam::cellTable::selectType(const word& matl) const
00246 {
00247     Map<word> lookup;
00248 
00249     forAllConstIter(Map<dictionary>, *this, iter)
00250     {
00251         if
00252         (
00253             matl
00254          == iter().lookupOrDefault<word>("MaterialType", defaultMaterial_)
00255         )
00256         {
00257             lookup.insert
00258             (
00259                 iter.key(),
00260                 iter().lookupOrDefault<word>
00261                 (
00262                     "Label",
00263                     "cellTable_" + Foam::name(iter.key())
00264                 )
00265             );
00266         }
00267     }
00268 
00269     return lookup;
00270 }
00271 
00272 
00273 Foam::Map<Foam::word> Foam::cellTable::fluids() const
00274 {
00275     return selectType("fluid");
00276 }
00277 
00278 
00279 Foam::Map<Foam::word> Foam::cellTable::solids() const
00280 {
00281     return selectType("solid");
00282 }
00283 
00284 
00285 Foam::Map<Foam::word> Foam::cellTable::shells() const
00286 {
00287     return selectType("shell");
00288 }
00289 
00290 
00291 
00292 void Foam::cellTable::setMaterial(const label& id, const word& matlType)
00293 {
00294     setEntry(id, "MaterialType", matlType);
00295 }
00296 
00297 
00298 void Foam::cellTable::setName(const label& id, const word& name)
00299 {
00300     setEntry(id, "Label", name);
00301 }
00302 
00303 
00304 void Foam::cellTable::setName(const label& id)
00305 {
00306     iterator iter = find(id);
00307 
00308     if (iter == end() || !iter().found("Label"))
00309     {
00310         setName(id, "cellTable_" + Foam::name(id));
00311     }
00312 }
00313 
00314 
00315 void Foam::cellTable::readDict
00316 (
00317     const objectRegistry& registry,
00318     const word& name,
00319     const fileName& instance
00320 )
00321 {
00322     clear();
00323 
00324     // read constant/dictName
00325     IOMap<dictionary> ioObj
00326     (
00327         IOobject
00328         (
00329             name,
00330             instance,
00331             registry,
00332             IOobject::READ_IF_PRESENT,
00333             IOobject::NO_WRITE,
00334             false
00335         )
00336     );
00337 
00338     if (ioObj.headerOk())
00339     {
00340         *this = ioObj;
00341         addDefaults();
00342     }
00343     else
00344     {
00345         Info<< "no constant/cellTable information available" << endl;
00346     }
00347 }
00348 
00349 
00350 void Foam::cellTable::writeDict
00351 (
00352     const objectRegistry& registry,
00353     const word& name,
00354     const fileName& instance
00355 ) const
00356 {
00357     // write constant/dictName
00358     IOMap<dictionary> ioObj
00359     (
00360         IOobject
00361         (
00362             name,
00363             instance,
00364             registry,
00365             IOobject::NO_READ,
00366             IOobject::NO_WRITE,
00367             false
00368         )
00369     );
00370 
00371     ioObj.note() =
00372         "persistent data for thirdParty mesh <-> OpenFOAM translation";
00373 
00374     Info<< "Writing " << ioObj.name() << " to " << ioObj.objectPath() << endl;
00375 
00376     OFstream os(ioObj.objectPath());
00377     ioObj.writeHeader(os);
00378     os << *this;
00379 }
00380 
00381 
00382 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00383 
00384 void Foam::cellTable::operator=(const cellTable& rhs)
00385 {
00386     Map<dictionary>::operator=(rhs);
00387     addDefaults();
00388 }
00389 
00390 
00391 void Foam::cellTable::operator=(const Map<dictionary>& rhs)
00392 {
00393     Map<dictionary>::operator=(rhs);
00394     addDefaults();
00395 }
00396 
00397 
00398 void Foam::cellTable::operator=(const polyMesh& mesh)
00399 {
00400     Map<dictionary> zoneDict;
00401 
00402     // create cellTableId and cellTable based on cellZones
00403     label nZoneCells = 0;
00404 
00405     wordList zoneNames = mesh.cellZones().names();
00406     label unZonedType = zoneNames.size() + 1;
00407 
00408     // do cell zones
00409     forAll(mesh.cellZones(), zoneI)
00410     {
00411         const cellZone& cZone = mesh.cellZones()[zoneI];
00412         nZoneCells += cZone.size();
00413 
00414         dictionary dict;
00415         dict.add("Label", zoneNames[zoneI]);
00416         zoneDict.insert(zoneI + 1, dict);
00417     }
00418 
00419     // collect unzoned cells
00420     // special case: no zones at all - do entire mesh
00421     if (nZoneCells == 0)
00422     {
00423         zoneDict.clear();
00424         unZonedType = 1;
00425     }
00426 
00427     if (mesh.nCells() > nZoneCells)
00428     {
00429         zoneDict.insert
00430         (
00431             unZonedType,
00432             dictionary(IStringStream("Label cells;")())
00433         );
00434     }
00435 
00436     Map<dictionary>::operator=(zoneDict);
00437     addDefaults();
00438 }
00439 
00440 
00441 // * * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * //
00442 
00443 void Foam::cellTable::addCellZones
00444 (
00445     polyMesh& mesh,
00446     const labelList& tableIds
00447 ) const
00448 {
00449     Map<label> typeToZone = zoneMap();
00450     List<DynamicList<label> > zoneCells(size());
00451 
00452     forAll(tableIds, cellI)
00453     {
00454         Map<label>::const_iterator iter = typeToZone.find(tableIds[cellI]);
00455         if (iter != typeToZone.end())
00456         {
00457             zoneCells[iter()].append(cellI);
00458         }
00459     }
00460 
00461     // track which zones were actually used
00462     labelList zoneUsed(zoneCells.size());
00463     wordList  zoneNames(namesList());
00464 
00465     label nZone = 0;
00466     forAll(zoneCells, zoneI)
00467     {
00468         zoneCells[zoneI].shrink();
00469         if (zoneCells[zoneI].size())
00470         {
00471             zoneUsed[nZone++] = zoneI;
00472         }
00473     }
00474     zoneUsed.setSize(nZone);
00475 
00476     cellZoneMesh& czMesh = mesh.cellZones();
00477 
00478     czMesh.clear();
00479     if (nZone <= 1)
00480     {
00481         Info<< "cellZones not used" << endl;
00482         return;
00483     }
00484     czMesh.setSize(nZone);
00485 
00486     forAll(zoneUsed, zoneI)
00487     {
00488         const label origZoneI = zoneUsed[zoneI];
00489 
00490         Info<< "cellZone " << zoneI
00491             << " (size: "  << zoneCells[origZoneI].size()
00492             << ") name: "  << zoneNames[origZoneI] << endl;
00493 
00494         czMesh.set
00495         (
00496             zoneI,
00497             new cellZone
00498             (
00499                 zoneNames[origZoneI],
00500                 zoneCells[origZoneI],
00501                 zoneI,
00502                 czMesh
00503             )
00504         );
00505     }
00506     czMesh.writeOpt() = IOobject::AUTO_WRITE;
00507 }
00508 
00509 
00510 void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
00511 {
00512     if (mapDict.empty())
00513     {
00514         return;
00515     }
00516 
00517     Map<word> origNames(names());
00518     labelList mapping(identity(max(origNames.toc()) + 1));
00519 
00520     bool remap = false;
00521     forAllConstIter(dictionary, mapDict, iter)
00522     {
00523         wordReList patterns(iter().stream());
00524 
00525         // find all matches
00526         Map<word> matches;
00527         forAllConstIter(Map<word>, origNames, namesIter)
00528         {
00529             if (findStrings(patterns, namesIter()))
00530             {
00531                 matches.insert(namesIter.key(), namesIter());
00532             }
00533         }
00534 
00535         if (matches.size())
00536         {
00537             label targetId = this->findIndex(iter().keyword());
00538 
00539             Info<< "combine cellTable: " << iter().keyword();
00540             if (targetId < 0)
00541             {
00542                 // not found - reuse 1st element but with different name
00543                 targetId = min(matches.toc());
00544                 operator[](targetId).set("Label", iter().keyword());
00545 
00546                 Info<< " = (";
00547             }
00548             else
00549             {
00550                 Info<< " += (";
00551             }
00552 
00553 
00554             // the mapping and name for targetId is already okay
00555             matches.erase(targetId);
00556             origNames.erase(targetId);
00557 
00558             // remove matched names, leaving targetId on 'this'
00559             this->erase(matches);
00560             origNames.erase(matches);
00561 
00562             forAllConstIter(Map<word>, matches, matchIter)
00563             {
00564                 mapping[matchIter.key()] = targetId;
00565                 Info<< " " << matchIter();
00566             }
00567             Info<< " )" << endl;
00568 
00569             remap = true;
00570         }
00571     }
00572 
00573     if (remap)
00574     {
00575         inplaceRenumber(mapping, tableIds);
00576     }
00577 }
00578 
00579 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd