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

blockMeshApp.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 Application
00026     blockMesh
00027 
00028 Description
00029     A multi-block mesh generator.
00030 
00031     Uses the block mesh description found in
00032     @a constant/polyMesh/blockMeshDict
00033     (or @a constant/<region>/polyMesh/blockMeshDict).
00034 
00035 Usage
00036 
00037     - blockMesh [OPTION]
00038 
00039     @param -blockTopology \n
00040     Write the topology as a set of edges in OBJ format.
00041 
00042     @param -region <name> \n
00043     Specify an alternative mesh region.
00044 
00045     @param -dict <dictionary> \n
00046     Specify an alternative dictionary for the block mesh description.
00047 
00048 \*---------------------------------------------------------------------------*/
00049 
00050 #include "Time.H"
00051 #include "IOdictionary.H"
00052 #include "IOPtrList.H"
00053 
00054 #include "blockMesh.H"
00055 #include "attachPolyTopoChanger.H"
00056 #include "preservePatchTypes.H"
00057 #include "emptyPolyPatch.H"
00058 #include "cellSet.H"
00059 
00060 #include "argList.H"
00061 #include "OSspecific.H"
00062 #include "OFstream.H"
00063 
00064 #include "Pair.H"
00065 #include "slidingInterface.H"
00066 
00067 using namespace Foam;
00068 
00069 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00070 // Main program:
00071 
00072 int main(int argc, char *argv[])
00073 {
00074     argList::noParallel();
00075     argList::validOptions.insert("blockTopology", "");
00076     argList::validOptions.insert("dict", "dictionary");
00077 #   include "addRegionOption.H"
00078 #   include "setRootCase.H"
00079 #   include "createTime.H"
00080 
00081     const word dictName("blockMeshDict");
00082 
00083     word regionName;
00084     fileName polyMeshDir;
00085 
00086     if (args.optionFound("region"))
00087     {
00088         // constant/<region>/polyMesh/blockMeshDict
00089         regionName  = args.option("region");
00090         polyMeshDir = regionName/polyMesh::meshSubDir;
00091 
00092         Info<< nl << "Generating mesh for region " << regionName << endl;
00093     }
00094     else
00095     {
00096         // constant/polyMesh/blockMeshDict
00097         regionName  = polyMesh::defaultRegion;
00098         polyMeshDir = polyMesh::meshSubDir;
00099     }
00100 
00101     autoPtr<IOobject> meshDictIoPtr;
00102 
00103     if (args.optionFound("dict"))
00104     {
00105         fileName dictPath(args.option("dict"));
00106 
00107         meshDictIoPtr.set
00108         (
00109             new IOobject
00110             (
00111                 (
00112                     isDir(dictPath)
00113                   ? dictPath/dictName
00114                   : dictPath
00115                 ),
00116                 runTime,
00117                 IOobject::MUST_READ,
00118                 IOobject::NO_WRITE,
00119                 false
00120             )
00121         );
00122     }
00123     else
00124     {
00125         meshDictIoPtr.set
00126         (
00127             new IOobject
00128             (
00129                 dictName,
00130                 runTime.constant(),
00131                 polyMeshDir,
00132                 runTime,
00133                 IOobject::MUST_READ,
00134                 IOobject::NO_WRITE,
00135                 false
00136             )
00137         );
00138     }
00139 
00140     if (!meshDictIoPtr->headerOk())
00141     {
00142         FatalErrorIn(args.executable())
00143             << "Cannot open mesh description file\n    "
00144             << meshDictIoPtr->objectPath()
00145             << nl
00146             << exit(FatalError);
00147     }
00148 
00149     Info<< nl << "Creating block mesh from\n    "
00150         << meshDictIoPtr->objectPath() << nl << endl;
00151 
00152     IOdictionary meshDict(meshDictIoPtr());
00153     blockMesh blocks(meshDict);
00154 
00155 
00156     if (args.optionFound("blockTopology"))
00157     {
00158         // Write mesh as edges.
00159         {
00160             fileName objMeshFile("blockTopology.obj");
00161 
00162             OFstream str(runTime.path()/objMeshFile);
00163 
00164             Info<< nl << "Dumping block structure as Lightwave obj format"
00165                 << " to " << objMeshFile << endl;
00166 
00167             blocks.writeTopology(str);
00168         }
00169 
00170         // Write centres of blocks
00171         {
00172             fileName objCcFile("blockCentres.obj");
00173 
00174             OFstream str(runTime.path()/objCcFile);
00175 
00176             Info<< nl << "Dumping block centres as Lightwave obj format"
00177                 << " to " << objCcFile << endl;
00178 
00179             const polyMesh& topo = blocks.topology();
00180 
00181             const pointField& cellCentres = topo.cellCentres();
00182 
00183             forAll(cellCentres, cellI)
00184             {
00185                 //point cc = b.blockShape().centre(b.points());
00186                 const point& cc = cellCentres[cellI];
00187 
00188                 str << "v " << cc.x() << ' ' << cc.y() << ' ' << cc.z() << nl;
00189             }
00190         }
00191 
00192         Info<< nl << "end" << endl;
00193 
00194         return 0;
00195     }
00196 
00197 
00198 
00199     Info<< nl << "Creating mesh from block mesh" << endl;
00200 
00201     wordList patchNames = blocks.patchNames();
00202     wordList patchTypes = blocks.patchTypes();
00203     word defaultFacesName = "defaultFaces";
00204     word defaultFacesType = emptyPolyPatch::typeName;
00205     wordList patchPhysicalTypes = blocks.patchPhysicalTypes();
00206 
00207     preservePatchTypes
00208     (
00209         runTime,
00210         runTime.constant(),
00211         polyMeshDir,
00212         patchNames,
00213         patchTypes,
00214         defaultFacesName,
00215         defaultFacesType,
00216         patchPhysicalTypes
00217     );
00218 
00219     polyMesh mesh
00220     (
00221         IOobject
00222         (
00223             regionName,
00224             runTime.constant(),
00225             runTime
00226         ),
00227         xferCopy(blocks.points()),           // could we re-use space?
00228         blocks.cells(),
00229         blocks.patches(),
00230         patchNames,
00231         patchTypes,
00232         defaultFacesName,
00233         defaultFacesType,
00234         patchPhysicalTypes
00235     );
00236 
00237 
00238     // Read in a list of dictionaries for the merge patch pairs
00239     if (meshDict.found("mergePatchPairs"))
00240     {
00241         List<Pair<word> > mergePatchPairs
00242         (
00243             meshDict.lookup("mergePatchPairs")
00244         );
00245 
00246 #       include "mergePatchPairs.H"
00247     }
00248     else
00249     {
00250         Info<< nl << "There are no merge patch pairs edges" << endl;
00251     }
00252 
00253 
00254     // Set any cellZones (note: cell labelling unaffected by above
00255     // mergePatchPairs)
00256 
00257     label nZones = blocks.numZonedBlocks();
00258 
00259     if (nZones > 0)
00260     {
00261         Info<< nl << "Adding cell zones" << endl;
00262 
00263         // Map from zoneName to cellZone index
00264         HashTable<label> zoneMap(nZones);
00265 
00266         // Cells per zone.
00267         List<DynamicList<label> > zoneCells(nZones);
00268 
00269         // Running cell counter
00270         label cellI = 0;
00271 
00272         // Largest zone so far
00273         label freeZoneI = 0;
00274 
00275         forAll(blocks, blockI)
00276         {
00277             const block& b = blocks[blockI];
00278             const labelListList& blockCells = b.cells();
00279             const word& zoneName = b.blockDef().zoneName();
00280 
00281             if (zoneName.size())
00282             {
00283                 HashTable<label>::const_iterator iter = zoneMap.find(zoneName);
00284 
00285                 label zoneI;
00286 
00287                 if (iter == zoneMap.end())
00288                 {
00289                     zoneI = freeZoneI++;
00290 
00291                     Info<< "    " << zoneI << '\t' << zoneName << endl;
00292 
00293                     zoneMap.insert(zoneName, zoneI);
00294                 }
00295                 else
00296                 {
00297                     zoneI = iter();
00298                 }
00299 
00300                 forAll(blockCells, i)
00301                 {
00302                     zoneCells[zoneI].append(cellI++);
00303                 }
00304             }
00305             else
00306             {
00307                 cellI += b.cells().size();
00308             }
00309         }
00310 
00311 
00312         List<cellZone*> cz(zoneMap.size());
00313 
00314         Info<< nl << "Writing cell zones as cellSets" << endl;
00315 
00316         forAllConstIter(HashTable<label>, zoneMap, iter)
00317         {
00318             label zoneI = iter();
00319 
00320             cz[zoneI] = new cellZone
00321             (
00322                 iter.key(),
00323                 zoneCells[zoneI].shrink(),
00324                 zoneI,
00325                 mesh.cellZones()
00326             );
00327 
00328             // Write as cellSet for ease of processing
00329             cellSet cset(mesh, iter.key(), zoneCells[zoneI].shrink());
00330             cset.write();
00331         }
00332 
00333         mesh.pointZones().setSize(0);
00334         mesh.faceZones().setSize(0);
00335         mesh.cellZones().setSize(0);
00336         mesh.addZones(List<pointZone*>(0), List<faceZone*>(0), cz);
00337     }
00338 
00339     // Set the precision of the points data to 10
00340     IOstream::defaultPrecision(10);
00341 
00342     Info << nl << "Writing polyMesh" << endl;
00343     mesh.removeFiles();
00344     if (!mesh.write())
00345     {
00346         FatalErrorIn(args.executable())
00347             << "Failed writing polyMesh."
00348             << exit(FatalError);
00349     }
00350 
00351     Info<< nl << "End" << endl;
00352 
00353     return 0;
00354 }
00355 
00356 
00357 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd