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

setsToZones.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     Add pointZones/faceZones/cellZones to the mesh from similar named
00027     pointSets/faceSets/cellSets.
00028 
00029     There is one catch: for faceZones you also need to specify a flip
00030     condition which basically denotes the side of the face. In this app
00031     it reads a cellSet (xxxCells if 'xxx' is the name of the faceSet) which
00032     is the masterCells of the zone.
00033     There are lots of situations in which this will go wrong but it is the
00034     best I can think of for now.
00035 
00036     If one is not interested in sideNess specify the -noFlipMap
00037     command line option.
00038 
00039 \*---------------------------------------------------------------------------*/
00040 
00041 #include "argList.H"
00042 #include "Time.H"
00043 #include "polyMesh.H"
00044 #include "IStringStream.H"
00045 #include "cellSet.H"
00046 #include "faceSet.H"
00047 #include "pointSet.H"
00048 #include "OFstream.H"
00049 #include "IFstream.H"
00050 #include "IOobjectList.H"
00051 #include "SortableList.H"
00052 
00053 using namespace Foam;
00054 
00055 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00056 
00057 // Main program:
00058 
00059 int main(int argc, char *argv[])
00060 {
00061     argList::validOptions.insert("noFlipMap", "");
00062 
00063 #   include "addTimeOptions.H"
00064 #   include "setRootCase.H"
00065 #   include "createTime.H"
00066 
00067     bool noFlipMap = args.optionFound("noFlipMap");
00068 
00069     // Get times list
00070     instantList Times = runTime.times();
00071 
00072     label startTime = Times.size()-1;
00073     label endTime = Times.size();
00074 
00075     // check -time and -latestTime options
00076 #   include "checkTimeOption.H"
00077 
00078     runTime.setTime(Times[startTime], startTime);
00079 
00080 #   include "createPolyMesh.H"
00081 
00082     // Search for list of objects for the time of the mesh
00083     IOobjectList objects
00084     (
00085         mesh,
00086         mesh.pointsInstance(),
00087         polyMesh::meshSubDir/"sets"
00088     );
00089 
00090     Info<< "Searched : " << mesh.pointsInstance()/polyMesh::meshSubDir/"sets"
00091         << nl
00092         << "Found    : " << objects.names() << nl
00093         << endl;
00094 
00095 
00096     IOobjectList pointObjects(objects.lookupClass(pointSet::typeName));
00097 
00098     //Pout<< "pointSets:" << pointObjects.names() << endl;
00099 
00100     for
00101     (
00102         IOobjectList::const_iterator iter = pointObjects.begin();
00103         iter != pointObjects.end();
00104         ++iter
00105     )
00106     {
00107         // Not in memory. Load it.
00108         pointSet set(*iter());
00109         SortableList<label> pointLabels(set.toc());
00110 
00111         label zoneID = mesh.pointZones().findZoneID(set.name());
00112         if (zoneID == -1)
00113         {
00114             Info<< "Adding set " << set.name() << " as a pointZone." << endl;
00115             label sz = mesh.pointZones().size();
00116             mesh.pointZones().setSize(sz+1);
00117             mesh.pointZones().set
00118             (
00119                 sz,
00120                 new pointZone
00121                 (
00122                     set.name(),             //name
00123                     pointLabels,            //addressing
00124                     sz,                     //index
00125                     mesh.pointZones()       //pointZoneMesh
00126                 )
00127             );
00128             mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
00129             mesh.pointZones().instance() = mesh.facesInstance();
00130         }
00131         else
00132         {
00133             Info<< "Overwriting contents of existing pointZone " << zoneID
00134                 << " with that of set " << set.name() << "." << endl;
00135             mesh.pointZones()[zoneID] = pointLabels;
00136             mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
00137             mesh.pointZones().instance() = mesh.facesInstance();
00138         }
00139     }
00140 
00141 
00142 
00143     IOobjectList faceObjects(objects.lookupClass(faceSet::typeName));
00144 
00145     HashSet<word> slaveCellSets;
00146 
00147     //Pout<< "faceSets:" << faceObjects.names() << endl;
00148 
00149     for
00150     (
00151         IOobjectList::const_iterator iter = faceObjects.begin();
00152         iter != faceObjects.end();
00153         ++iter
00154     )
00155     {
00156         // Not in memory. Load it.
00157         faceSet set(*iter());
00158         SortableList<label> faceLabels(set.toc());
00159 
00160         DynamicList<label> addressing(set.size());
00161         DynamicList<bool> flipMap(set.size());
00162 
00163         if (!noFlipMap)
00164         {
00165             word setName(set.name() + "SlaveCells");
00166 
00167             Info<< "Trying to load cellSet " << setName
00168                 << " to find out the slave side of the zone." << nl
00169                 << "If you do not care about the flipMap"
00170                 << " (i.e. do not use the sideness)" << nl
00171                 << "use the -noFlipMap command line option."
00172                 << endl;
00173 
00174             // Load corresponding cells
00175             cellSet cells(mesh, setName);
00176 
00177             // Store setName to exclude from cellZones further on
00178             slaveCellSets.insert(setName);
00179 
00180             forAll(faceLabels, i)
00181             {
00182                 label faceI = faceLabels[i];
00183 
00184                 bool flip = false;
00185 
00186                 if (mesh.isInternalFace(faceI))
00187                 {
00188                     if
00189                     (
00190                         cells.found(mesh.faceOwner()[faceI])
00191                     && !cells.found(mesh.faceNeighbour()[faceI])
00192                     )
00193                     {
00194                         flip = false;
00195                     }
00196                     else if
00197                     (
00198                        !cells.found(mesh.faceOwner()[faceI])
00199                      && cells.found(mesh.faceNeighbour()[faceI])
00200                     )
00201                     {
00202                         flip = true;
00203                     }
00204                     else
00205                     {
00206                         FatalErrorIn(args.executable())
00207                             << "One of owner or neighbour of internal face "
00208                             << faceI << " should be in cellSet " << cells.name()
00209                             << " to be able to determine orientation." << endl
00210                             << "Face:" << faceI
00211                             << " own:" << mesh.faceOwner()[faceI]
00212                             << " OwnInCellSet:"
00213                             << cells.found(mesh.faceOwner()[faceI])
00214                             << " nei:" << mesh.faceNeighbour()[faceI]
00215                             << " NeiInCellSet:"
00216                             << cells.found(mesh.faceNeighbour()[faceI])
00217                             << abort(FatalError);
00218                     }
00219                 }
00220                 else
00221                 {
00222                     if (cells.found(mesh.faceOwner()[faceI]))
00223                     {
00224                         flip = false;
00225                     }
00226                     else
00227                     {
00228                         flip = true;
00229                     }
00230                 }
00231 
00232                 addressing.append(faceI);
00233                 flipMap.append(flip);
00234             }
00235         }
00236         else
00237         {
00238             // No flip map.
00239             forAll(faceLabels, i)
00240             {
00241                 label faceI = faceLabels[i];
00242                 addressing.append(faceI);
00243                 flipMap.append(false);
00244             }
00245         }
00246 
00247         label zoneID = mesh.faceZones().findZoneID(set.name());
00248         if (zoneID == -1)
00249         {
00250             Info<< "Adding set " << set.name() << " as a faceZone." << endl;
00251             label sz = mesh.faceZones().size();
00252             mesh.faceZones().setSize(sz+1);
00253             mesh.faceZones().set
00254             (
00255                 sz,
00256                 new faceZone
00257                 (
00258                     set.name(),             //name
00259                     addressing.shrink(),    //addressing
00260                     flipMap.shrink(),       //flipmap
00261                     sz,                     //index
00262                     mesh.faceZones()        //pointZoneMesh
00263                 )
00264             );
00265             mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
00266             mesh.faceZones().instance() = mesh.facesInstance();
00267         }
00268         else
00269         {
00270             Info<< "Overwriting contents of existing faceZone " << zoneID
00271                 << " with that of set " << set.name() << "." << endl;
00272             mesh.faceZones()[zoneID].resetAddressing
00273             (
00274                 addressing.shrink(),
00275                 flipMap.shrink()
00276             );
00277             mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
00278             mesh.faceZones().instance() = mesh.facesInstance();
00279         }
00280     }
00281 
00282 
00283 
00284     IOobjectList cellObjects(objects.lookupClass(cellSet::typeName));
00285 
00286     //Pout<< "cellSets:" << cellObjects.names() << endl;
00287 
00288     for
00289     (
00290         IOobjectList::const_iterator iter = cellObjects.begin();
00291         iter != cellObjects.end();
00292         ++iter
00293     )
00294     {
00295         if (!slaveCellSets.found(iter.key()))
00296         {
00297             // Not in memory. Load it.
00298             cellSet set(*iter());
00299             SortableList<label> cellLabels(set.toc());
00300 
00301             label zoneID = mesh.cellZones().findZoneID(set.name());
00302             if (zoneID == -1)
00303             {
00304                 Info<< "Adding set " << set.name() << " as a cellZone." << endl;
00305                 label sz = mesh.cellZones().size();
00306                 mesh.cellZones().setSize(sz+1);
00307                 mesh.cellZones().set
00308                 (
00309                     sz,
00310                     new cellZone
00311                     (
00312                         set.name(),             //name
00313                         cellLabels,             //addressing
00314                         sz,                     //index
00315                         mesh.cellZones()        //pointZoneMesh
00316                     )
00317                 );
00318                 mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
00319                 mesh.cellZones().instance() = mesh.facesInstance();
00320             }
00321             else
00322             {
00323                 Info<< "Overwriting contents of existing cellZone " << zoneID
00324                     << " with that of set " << set.name() << "." << endl;
00325                 mesh.cellZones()[zoneID] = cellLabels;
00326                 mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
00327                 mesh.cellZones().instance() = mesh.facesInstance();
00328             }
00329         }
00330     }
00331 
00332 
00333 
00334     Info<< "Writing mesh." << endl;
00335 
00336     if (!mesh.write())
00337     {
00338         FatalErrorIn(args.executable())
00339             << "Failed writing polyMesh."
00340             << exit(FatalError);
00341     }
00342 
00343     Info<< nl << "End" << endl;
00344 
00345     return 0;
00346 }
00347 
00348 
00349 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd