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

snappyHexMesh.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     snappyHexMesh
00027 
00028 Description
00029     Automatic split hex mesher. Refines and snaps to surface.
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "argList.H"
00034 #include "Time.H"
00035 #include "fvMesh.H"
00036 #include "autoRefineDriver.H"
00037 #include "autoSnapDriver.H"
00038 #include "autoLayerDriver.H"
00039 #include "searchableSurfaces.H"
00040 #include "refinementSurfaces.H"
00041 #include "shellSurfaces.H"
00042 #include "decompositionMethod.H"
00043 #include "fvMeshDistribute.H"
00044 #include "wallPolyPatch.H"
00045 #include "refinementParameters.H"
00046 #include "snapParameters.H"
00047 #include "layerParameters.H"
00048 
00049 
00050 using namespace Foam;
00051 
00052 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00053 
00054 // Check writing tolerance before doing any serious work
00055 scalar getMergeDistance(const polyMesh& mesh, const scalar mergeTol)
00056 {
00057     const boundBox& meshBb = mesh.bounds();
00058     scalar mergeDist = mergeTol * meshBb.mag();
00059     scalar writeTol = std::pow
00060     (
00061         scalar(10.0),
00062        -scalar(IOstream::defaultPrecision())
00063     );
00064 
00065     Info<< nl
00066         << "Overall mesh bounding box  : " << meshBb << nl
00067         << "Relative tolerance         : " << mergeTol << nl
00068         << "Absolute matching distance : " << mergeDist << nl
00069         << endl;
00070 
00071     if (mesh.time().writeFormat() == IOstream::ASCII && mergeTol < writeTol)
00072     {
00073         FatalErrorIn("getMergeDistance(const polyMesh&, const scalar)")
00074             << "Your current settings specify ASCII writing with "
00075             << IOstream::defaultPrecision() << " digits precision." << endl
00076             << "Your merging tolerance (" << mergeTol << ") is finer than this."
00077             << endl
00078             << "Please change your writeFormat to binary"
00079             << " or increase the writePrecision" << endl
00080             << "or adjust the merge tolerance (-mergeTol)."
00081             << exit(FatalError);
00082     }
00083 
00084     return mergeDist;
00085 }
00086 
00087 
00088 // Write mesh and additional information
00089 void writeMesh
00090 (
00091     const string& msg,
00092     const meshRefinement& meshRefiner,
00093     const label debug
00094 )
00095 {
00096     const fvMesh& mesh = meshRefiner.mesh();
00097 
00098     meshRefiner.printMeshInfo(debug, msg);
00099     Info<< "Writing mesh to time " << meshRefiner.timeName() << endl;
00100 
00101     meshRefiner.write(meshRefinement::MESH|meshRefinement::SCALARLEVELS, "");
00102     if (debug & meshRefinement::OBJINTERSECTIONS)
00103     {
00104         meshRefiner.write
00105         (
00106             meshRefinement::OBJINTERSECTIONS,
00107             mesh.time().path()/meshRefiner.timeName()
00108         );
00109     }
00110     Info<< "Written mesh in = "
00111         << mesh.time().cpuTimeIncrement() << " s." << endl;
00112 }
00113 
00114 
00115 
00116 int main(int argc, char *argv[])
00117 {
00118     argList::validOptions.insert("overwrite", "");
00119 #   include "setRootCase.H"
00120 #   include "createTime.H"
00121     runTime.functionObjects().off();
00122 #   include "createMesh.H"
00123 
00124     Info<< "Read mesh in = "
00125         << runTime.cpuTimeIncrement() << " s" << endl;
00126 
00127     const bool overwrite = args.optionFound("overwrite");
00128 
00129 
00130     // Check patches and faceZones are synchronised
00131     mesh.boundaryMesh().checkParallelSync(true);
00132     meshRefinement::checkCoupledFaceZones(mesh);
00133 
00134 
00135     // Read decomposePar dictionary
00136     IOdictionary decomposeDict
00137     (
00138         IOobject
00139         (
00140             "decomposeParDict",
00141             runTime.system(),
00142             mesh,
00143             IOobject::MUST_READ,
00144             IOobject::NO_WRITE
00145         )
00146     );
00147 
00148     // Read meshing dictionary
00149     IOdictionary meshDict
00150     (
00151        IOobject
00152        (
00153             "snappyHexMeshDict",
00154             runTime.system(),
00155             mesh,
00156             IOobject::MUST_READ,
00157             IOobject::NO_WRITE
00158        )
00159     );
00160 
00161     // all surface geometry
00162     const dictionary& geometryDict = meshDict.subDict("geometry");
00163 
00164     // refinement parameters
00165     const dictionary& refineDict = meshDict.subDict("castellatedMeshControls");
00166 
00167     // mesh motion and mesh quality parameters
00168     const dictionary& motionDict = meshDict.subDict("meshQualityControls");
00169 
00170     // snap-to-surface parameters
00171     const dictionary& snapDict = meshDict.subDict("snapControls");
00172 
00173     // layer addition parameters
00174     const dictionary& layerDict = meshDict.subDict("addLayersControls");
00175 
00176 
00177     const scalar mergeDist = getMergeDistance
00178     (
00179         mesh,
00180         readScalar(meshDict.lookup("mergeTolerance"))
00181     );
00182 
00183 
00184 
00185     // Debug
00186     // ~~~~~
00187 
00188     const label debug(readLabel(meshDict.lookup("debug")));
00189     if (debug > 0)
00190     {
00191         meshRefinement::debug = debug;
00192         autoRefineDriver::debug = debug;
00193         autoSnapDriver::debug = debug;
00194         autoLayerDriver::debug = debug;
00195     }        
00196 
00197 
00198     // Read geometry
00199     // ~~~~~~~~~~~~~
00200 
00201     searchableSurfaces allGeometry
00202     (
00203         IOobject
00204         (
00205             "abc",                      // dummy name
00206             //mesh.time().constant(),     // instance
00207             mesh.time().findInstance("triSurface", word::null),// instance
00208             "triSurface",               // local
00209             mesh.time(),                // registry
00210             IOobject::MUST_READ,
00211             IOobject::NO_WRITE
00212         ),
00213         geometryDict
00214     );
00215 
00216 
00217     // Read refinement surfaces
00218     // ~~~~~~~~~~~~~~~~~~~~~~~~
00219 
00220     Info<< "Reading refinement surfaces." << endl;
00221     refinementSurfaces surfaces
00222     (
00223         allGeometry,
00224         refineDict.subDict("refinementSurfaces")
00225     );
00226     Info<< "Read refinement surfaces in = "
00227         << mesh.time().cpuTimeIncrement() << " s" << nl << endl;
00228 
00229 
00230     // Read refinement shells
00231     // ~~~~~~~~~~~~~~~~~~~~~~
00232 
00233     Info<< "Reading refinement shells." << endl;
00234     shellSurfaces shells
00235     (
00236         allGeometry,
00237         refineDict.subDict("refinementRegions")
00238     );
00239     Info<< "Read refinement shells in = "
00240         << mesh.time().cpuTimeIncrement() << " s" << nl << endl;
00241 
00242 
00243     Info<< "Setting refinement level of surface to be consistent"
00244         << " with shells." << endl;
00245     surfaces.setMinLevelFields(shells);
00246     Info<< "Checked shell refinement in = "
00247         << mesh.time().cpuTimeIncrement() << " s" << nl << endl;
00248 
00249 
00250     // Refinement engine
00251     // ~~~~~~~~~~~~~~~~~
00252 
00253     Info<< nl
00254         << "Determining initial surface intersections" << nl
00255         << "-----------------------------------------" << nl
00256         << endl;
00257 
00258     // Main refinement engine
00259     meshRefinement meshRefiner
00260     (
00261         mesh,
00262         mergeDist,          // tolerance used in sorting coordinates
00263         overwrite,          // overwrite mesh files?
00264         surfaces,           // for surface intersection refinement
00265         shells              // for volume (inside/outside) refinement
00266     );
00267     Info<< "Calculated surface intersections in = "
00268         << mesh.time().cpuTimeIncrement() << " s" << nl << endl;
00269 
00270     // Some stats
00271     meshRefiner.printMeshInfo(debug, "Initial mesh");
00272 
00273     meshRefiner.write
00274     (
00275         debug&meshRefinement::OBJINTERSECTIONS,
00276         mesh.time().path()/meshRefiner.timeName()
00277     );
00278 
00279 
00280     // Add all the surface regions as patches
00281     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00282 
00283     labelList globalToPatch;
00284     {
00285         Info<< nl
00286             << "Adding patches for surface regions" << nl
00287             << "----------------------------------" << nl
00288             << endl;
00289 
00290         // From global region number to mesh patch.
00291         globalToPatch.setSize(surfaces.nRegions(), -1);
00292 
00293         Info<< "Patch\tRegion" << nl
00294             << "-----\t------"
00295             << endl;
00296 
00297         const labelList& surfaceGeometry = surfaces.surfaces();
00298         forAll(surfaceGeometry, surfI)
00299         {
00300             label geomI = surfaceGeometry[surfI];
00301 
00302             const wordList& regNames = allGeometry.regionNames()[geomI];
00303 
00304             Info<< surfaces.names()[surfI] << ':' << nl << nl;
00305 
00306             forAll(regNames, i)
00307             {
00308                 label patchI = meshRefiner.addMeshedPatch
00309                 (
00310                     regNames[i],
00311                     wallPolyPatch::typeName
00312                 );
00313 
00314                 Info<< patchI << '\t' << regNames[i] << nl;
00315 
00316                 globalToPatch[surfaces.globalRegion(surfI, i)] = patchI;
00317             }
00318 
00319             Info<< nl;
00320         }
00321         Info<< "Added patches in = "
00322             << mesh.time().cpuTimeIncrement() << " s" << nl << endl;
00323     }
00324 
00325 
00326     // Parallel
00327     // ~~~~~~~~
00328 
00329     // Decomposition
00330     autoPtr<decompositionMethod> decomposerPtr
00331     (
00332         decompositionMethod::New
00333         (
00334             decomposeDict,
00335             mesh
00336         )
00337     );
00338     decompositionMethod& decomposer = decomposerPtr();
00339 
00340     if (Pstream::parRun() && !decomposer.parallelAware())
00341     {
00342         FatalErrorIn(args.executable())
00343             << "You have selected decomposition method "
00344             << decomposer.typeName
00345             << " which is not parallel aware." << endl
00346             << "Please select one that is (hierarchical, parMetis)"
00347             << exit(FatalError);
00348     }
00349 
00350     // Mesh distribution engine (uses tolerance to reconstruct meshes)
00351     fvMeshDistribute distributor(mesh, mergeDist);
00352 
00353 
00354 
00355 
00356 
00357     // Now do the real work -refinement -snapping -layers
00358     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00359 
00360     Switch wantRefine(meshDict.lookup("castellatedMesh"));
00361     Switch wantSnap(meshDict.lookup("snap"));
00362     Switch wantLayers(meshDict.lookup("addLayers"));
00363 
00364     if (wantRefine)
00365     {
00366         autoRefineDriver refineDriver
00367         (
00368             meshRefiner,
00369             decomposer,
00370             distributor,
00371             globalToPatch
00372         );
00373 
00374         // Refinement parameters
00375         refinementParameters refineParams(refineDict);
00376 
00377         if (!overwrite)
00378         {
00379             const_cast<Time&>(mesh.time())++;
00380         }
00381 
00382         refineDriver.doRefine(refineDict, refineParams, wantSnap, motionDict);
00383 
00384         writeMesh
00385         (
00386             "Refined mesh",
00387             meshRefiner,
00388             debug
00389         );
00390     }
00391 
00392     if (wantSnap)
00393     {
00394         autoSnapDriver snapDriver
00395         (
00396             meshRefiner,
00397             globalToPatch
00398         );
00399 
00400         // Snap parameters
00401         snapParameters snapParams(snapDict);
00402 
00403         if (!overwrite)
00404         {
00405             const_cast<Time&>(mesh.time())++;
00406         }
00407 
00408         snapDriver.doSnap(snapDict, motionDict, snapParams);
00409 
00410         writeMesh
00411         (
00412             "Snapped mesh",
00413             meshRefiner,
00414             debug
00415         );
00416     }
00417 
00418     if (wantLayers)
00419     {
00420         autoLayerDriver layerDriver(meshRefiner);
00421 
00422         // Layer addition parameters
00423         layerParameters layerParams(layerDict, mesh.boundaryMesh());
00424 
00425         if (!overwrite)
00426         {
00427             const_cast<Time&>(mesh.time())++;
00428         }
00429 
00430         layerDriver.doLayers
00431         (
00432             layerDict,
00433             motionDict,
00434             layerParams,
00435             decomposer,
00436             distributor
00437         );
00438 
00439         writeMesh
00440         (
00441             "Layer mesh",
00442             meshRefiner,
00443             debug
00444         );
00445     }
00446 
00447 
00448     Info<< "Finished meshing in = "
00449         << runTime.elapsedCpuTime() << " s." << endl;
00450 
00451     Info<< "End\n" << endl;
00452 
00453     return(0);
00454 }
00455 
00456 
00457 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd