FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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