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

autoHexMeshDriver.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 \*----------------------------------------------------------------------------*/
00026 
00027 #include "autoHexMeshDriver.H"
00028 #include "fvMesh.H"
00029 #include "Time.H"
00030 #include "boundBox.H"
00031 #include "globalIndex.H"
00032 #include "wallPolyPatch.H"
00033 #include "mapDistributePolyMesh.H"
00034 #include "cellSet.H"
00035 #include "syncTools.H"
00036 #include "motionSmoother.H"
00037 #include "refinementParameters.H"
00038 #include "snapParameters.H"
00039 #include "layerParameters.H"
00040 #include "autoRefineDriver.H"
00041 #include "autoSnapDriver.H"
00042 #include "autoLayerDriver.H"
00043 #include "triSurfaceMesh.H"
00044 
00045 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00046 
00047 namespace Foam
00048 {
00049     defineTypeNameAndDebug(autoHexMeshDriver, 0);
00050 }
00051 
00052 
00053 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00054 
00055 // Check writing tolerance before doing any serious work
00056 Foam::scalar Foam::autoHexMeshDriver::getMergeDistance(const scalar mergeTol)
00057  const
00058 {
00059     const boundBox& meshBb = mesh_.bounds();
00060     scalar mergeDist = mergeTol * meshBb.mag();
00061     scalar writeTol = std::pow
00062     (
00063         scalar(10.0),
00064        -scalar(IOstream::defaultPrecision())
00065     );
00066 
00067     Info<< nl
00068         << "Overall mesh bounding box  : " << meshBb << nl
00069         << "Relative tolerance         : " << mergeTol << nl
00070         << "Absolute matching distance : " << mergeDist << nl
00071         << endl;
00072 
00073     if (mesh_.time().writeFormat() == IOstream::ASCII && mergeTol < writeTol)
00074     {
00075         FatalErrorIn("autoHexMeshDriver::getMergeDistance(const scalar) const")
00076             << "Your current settings specify ASCII writing with "
00077             << IOstream::defaultPrecision() << " digits precision." << endl
00078             << "Your merging tolerance (" << mergeTol << ") is finer than this."
00079             << endl
00080             << "Please change your writeFormat to binary"
00081             << " or increase the writePrecision" << endl
00082             << "or adjust the merge tolerance (-mergeTol)."
00083             << exit(FatalError);
00084     }
00085 
00086     return mergeDist;
00087 }
00088 
00089 
00091 //void Foam::autoHexMeshDriver::orientOutside
00092 //(
00093 //    PtrList<searchableSurface>& shells
00094 //)
00095 //{
00096 //    // Determine outside point.
00097 //    boundBox overallBb = boundBox::invertedBox;
00098 //
00099 //    bool hasSurface = false;
00100 //
00101 //    forAll(shells, shellI)
00102 //    {
00103 //        if (isA<triSurfaceMesh>(shells[shellI]))
00104 //        {
00105 //            const triSurfaceMesh& shell =
00106 //                refCast<const triSurfaceMesh>(shells[shellI]);
00107 //
00108 //            hasSurface = true;
00109 //
00110 //            boundBox shellBb(shell.localPoints(), false);
00111 //
00112 //            overallBb.min() = min(overallBb.min(), shellBb.min());
00113 //            overallBb.max() = max(overallBb.max(), shellBb.max());
00114 //        }
00115 //    }
00116 //
00117 //    if (hasSurface)
00118 //    {
00119 //        const point outsidePt = 2 * overallBb.span();
00120 //
00121 //        //Info<< "Using point " << outsidePt << " to orient shells" << endl;
00122 //
00123 //        forAll(shells, shellI)
00124 //        {
00125 //            if (isA<triSurfaceMesh>(shells[shellI]))
00126 //            {
00127 //                triSurfaceMesh& shell =
00128 //                  refCast<triSurfaceMesh>(shells[shellI]);
00129 //
00130 //                if (!refinementSurfaces::isSurfaceClosed(shell))
00131 //                {
00132 //                    FatalErrorIn("orientOutside(PtrList<searchableSurface>&)")
00133 //                        << "Refinement shell "
00134 //                        << shell.searchableSurface::name()
00135 //                        << " is not closed." << exit(FatalError);
00136 //                }
00137 //
00138 //                refinementSurfaces::orientSurface(outsidePt, shell);
00139 //            }
00140 //        }
00141 //    }
00142 //}
00143 
00144 
00145 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00146 
00147 // Construct from components
00148 Foam::autoHexMeshDriver::autoHexMeshDriver
00149 (
00150     fvMesh& mesh,
00151     const bool overwrite,
00152     const dictionary& dict,
00153     const dictionary& decomposeDict
00154 )
00155 :
00156     mesh_(mesh),
00157     dict_(dict),
00158     debug_(readLabel(dict_.lookup("debug"))),
00159     mergeDist_(getMergeDistance(readScalar(dict_.lookup("mergeTolerance"))))
00160 {
00161     if (debug_ > 0)
00162     {
00163         meshRefinement::debug = debug_;
00164         autoHexMeshDriver::debug = debug_;
00165         autoRefineDriver::debug = debug;
00166         autoSnapDriver::debug = debug;
00167         autoLayerDriver::debug = debug;
00168     }
00169 
00170     refinementParameters refineParams(dict, 1);
00171 
00172     Info<< "Overall cell limit                         : "
00173         << refineParams.maxGlobalCells() << endl;
00174     Info<< "Per processor cell limit                   : "
00175         << refineParams.maxLocalCells() << endl;
00176     Info<< "Minimum number of cells to refine          : "
00177         << refineParams.minRefineCells() << endl;
00178     Info<< "Curvature                                  : "
00179         << refineParams.curvature() << nl << endl;
00180     Info<< "Layers between different refinement levels : "
00181         << refineParams.nBufferLayers() << endl;
00182 
00183     PtrList<dictionary> shellDicts(dict_.lookup("refinementShells"));
00184 
00185     PtrList<dictionary> surfaceDicts(dict_.lookup("surfaces"));
00186 
00187 
00188     // Read geometry
00189     // ~~~~~~~~~~~~~
00190 
00191     {
00192         Info<< "Reading all geometry." << endl;
00193 
00194         // Construct dictionary with all shells and all refinement surfaces
00195         dictionary geometryDict;
00196 
00197         forAll(shellDicts, shellI)
00198         {
00199             dictionary shellDict = shellDicts[shellI];
00200             const word name(shellDict.lookup("name"));
00201             shellDict.remove("name");
00202             shellDict.remove("level");
00203             shellDict.remove("refineInside");
00204             geometryDict.add(name, shellDict);
00205         }
00206 
00207         forAll(surfaceDicts, surfI)
00208         {
00209             dictionary surfDict = surfaceDicts[surfI];
00210             const word name(string::validate<word>(surfDict.lookup("file")));
00211             surfDict.remove("file");
00212             surfDict.remove("regions");
00213             if (!surfDict.found("name"))
00214             {
00215                 surfDict.add("name", name);
00216             }
00217             surfDict.add("type", triSurfaceMesh::typeName);
00218             geometryDict.add(name, surfDict);
00219         }
00220 
00221         allGeometryPtr_.reset
00222         (
00223             new searchableSurfaces
00224             (
00225                 IOobject
00226                 (
00227                     "abc",                                      // dummy name
00228                     mesh_.time().findInstance("triSurface", word::null),// inst
00229                     "triSurface",                               // local
00230                     mesh_.time(),                               // registry
00231                     IOobject::MUST_READ,
00232                     IOobject::NO_WRITE
00233                 ),
00234                 geometryDict
00235             )
00236         );
00237 
00238         Info<< "Read geometry in = "
00239             << mesh_.time().cpuTimeIncrement() << " s" << endl;
00240     }
00241 
00242 
00243     // Read refinement surfaces
00244     // ~~~~~~~~~~~~~~~~~~~~~~~~
00245 
00246     {
00247         Info<< "Reading surfaces and constructing search trees." << endl;
00248 
00249         surfacesPtr_.reset
00250         (
00251             new refinementSurfaces
00252             (
00253                 allGeometryPtr_(),
00254                 surfaceDicts
00255             )
00256         );
00257         Info<< "Read surfaces in = "
00258             << mesh_.time().cpuTimeIncrement() << " s" << endl;
00259     }
00260 
00261     // Read refinement shells
00262     // ~~~~~~~~~~~~~~~~~~~~~~
00263 
00264     {
00265         Info<< "Reading refinement shells." << endl;
00266         shellsPtr_.reset
00267         (
00268             new shellSurfaces
00269             (
00270                 allGeometryPtr_(),
00271                 shellDicts
00272             )
00273         );
00274         Info<< "Read refinement shells in = "
00275             << mesh_.time().cpuTimeIncrement() << " s" << endl;
00276 
00278         //Info<< "Orienting triSurface shells so point far away is outside."
00279         //    << endl;
00280         //orientOutside(shells_);
00281         //Info<< "Oriented shells in = "
00282         //    << mesh_.time().cpuTimeIncrement() << " s" << endl;
00283 
00284         Info<< "Setting refinement level of surface to be consistent"
00285             << " with shells." << endl;
00286         surfacesPtr_().setMinLevelFields(shells());
00287         Info<< "Checked shell refinement in = "
00288             << mesh_.time().cpuTimeIncrement() << " s" << endl;
00289     }
00290 
00291 
00292     // Check faceZones are synchronised
00293     meshRefinement::checkCoupledFaceZones(mesh_);
00294 
00295 
00296     // Refinement engine
00297     // ~~~~~~~~~~~~~~~~~
00298 
00299     {
00300         Info<< nl
00301             << "Determining initial surface intersections" << nl
00302             << "-----------------------------------------" << nl
00303             << endl;
00304 
00305         // Main refinement engine
00306         meshRefinerPtr_.reset
00307         (
00308             new meshRefinement
00309             (
00310                 mesh,
00311                 mergeDist_,         // tolerance used in sorting coordinates
00312                 overwrite,
00313                 surfaces(),
00314                 shells()
00315             )
00316         );
00317         Info<< "Calculated surface intersections in = "
00318             << mesh_.time().cpuTimeIncrement() << " s" << endl;
00319 
00320         // Some stats
00321         meshRefinerPtr_().printMeshInfo(debug_, "Initial mesh");
00322 
00323         meshRefinerPtr_().write
00324         (
00325             debug_&meshRefinement::OBJINTERSECTIONS,
00326             mesh_.time().path()/meshRefinerPtr_().timeName()
00327         );
00328     }
00329 
00330 
00331     // Add all the surface regions as patches
00332     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00333 
00334     {
00335         Info<< nl
00336             << "Adding patches for surface regions" << nl
00337             << "----------------------------------" << nl
00338             << endl;
00339 
00340         // From global region number to mesh patch.
00341         globalToPatch_.setSize(surfaces().nRegions(), -1);
00342 
00343         Info<< "Patch\tRegion" << nl
00344             << "-----\t------"
00345             << endl;
00346 
00347         const labelList& surfaceGeometry = surfaces().surfaces();
00348         forAll(surfaceGeometry, surfI)
00349         {
00350             label geomI = surfaceGeometry[surfI];
00351 
00352             const wordList& regNames = allGeometryPtr_().regionNames()[geomI];
00353 
00354             Info<< surfaces().names()[surfI] << ':' << nl << nl;
00355 
00356             forAll(regNames, i)
00357             {
00358                 label patchI = meshRefinerPtr_().addMeshedPatch
00359                 (
00360                     regNames[i],
00361                     wallPolyPatch::typeName
00362                 );
00363 
00364                 Info<< patchI << '\t' << regNames[i] << nl;
00365 
00366                 globalToPatch_[surfaces().globalRegion(surfI, i)] = patchI;
00367             }
00368 
00369             Info<< nl;
00370         }
00371         Info<< "Added patches in = "
00372             << mesh_.time().cpuTimeIncrement() << " s" << nl << endl;
00373     }
00374 
00375 
00380     //
00381     //labelList namedSurfaces(surfaces().getNamedSurfaces());
00382     //if (namedSurfaces.size())
00383     //{
00384     //    Info<< nl
00385     //        << "Introducing cyclics for faceZones" << nl
00386     //        << "---------------------------------" << nl
00387     //        << endl;
00388     //
00389     //    // From surface to cyclic patch
00390     //    surfaceToCyclicPatch_.setSize(surfaces().size(), -1);
00391     //
00392     //    Info<< "Patch\tZone" << nl
00393     //        << "----\t-----"
00394     //        << endl;
00395     //
00396     //    forAll(namedSurfaces, i)
00397     //    {
00398     //        label surfI = namedSurfaces[i];
00399     //
00400     //        surfaceToCyclicPatch_[surfI] = meshRefinement::addPatch
00401     //        (
00402     //            mesh,
00403     //            surfaces().faceZoneNames()[surfI],
00404     //            cyclicPolyPatch::typeName
00405     //        );
00406     //
00407     //        Info<< surfaceToCyclicPatch_[surfI] << '\t'
00408     //            << surfaces().faceZoneNames()[surfI] << nl << endl;
00409     //    }
00410     //    Info<< "Added cyclic patches in = "
00411     //        << mesh_.time().cpuTimeIncrement() << " s" << endl;
00412     //}
00413 
00414 
00415     // Parallel
00416     // ~~~~~~~~
00417 
00418     {
00419         // Decomposition
00420         decomposerPtr_ = decompositionMethod::New
00421         (
00422             decomposeDict,
00423             mesh_
00424         );
00425         decompositionMethod& decomposer = decomposerPtr_();
00426 
00427 
00428         if (Pstream::parRun() && !decomposer.parallelAware())
00429         {
00430             FatalErrorIn("autoHexMeshDriver::autoHexMeshDriver"
00431                 "(const IOobject&, fvMesh&)")
00432                 << "You have selected decomposition method "
00433                 << decomposer.typeName
00434                 << " which is not parallel aware." << endl
00435                 << "Please select one that is (parMetis, hierarchical)"
00436                 << exit(FatalError);
00437         }
00438 
00439         // Mesh distribution engine (uses tolerance to reconstruct meshes)
00440         distributorPtr_.reset(new fvMeshDistribute(mesh_, mergeDist_));
00441     }
00442 }
00443 
00444 
00445 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00446 
00447 void Foam::autoHexMeshDriver::writeMesh(const string& msg) const
00448 {
00449     const meshRefinement& meshRefiner = meshRefinerPtr_();
00450 
00451     meshRefiner.printMeshInfo(debug_, msg);
00452     Info<< "Writing mesh to time " << meshRefiner.timeName() << endl;
00453 
00454     meshRefiner.write(meshRefinement::MESH|meshRefinement::SCALARLEVELS, "");
00455     if (debug_ & meshRefinement::OBJINTERSECTIONS)
00456     {
00457         meshRefiner.write
00458         (
00459             meshRefinement::OBJINTERSECTIONS,
00460             mesh_.time().path()/meshRefiner.timeName()
00461         );
00462     }
00463     Info<< "Written mesh in = "
00464         << mesh_.time().cpuTimeIncrement() << " s." << endl;
00465 }
00466 
00467 
00468 void Foam::autoHexMeshDriver::doMesh()
00469 {
00470     Switch wantRefine(dict_.lookup("doRefine"));
00471     Switch wantSnap(dict_.lookup("doSnap"));
00472     Switch wantLayers(dict_.lookup("doLayers"));
00473 
00474     Info<< "Do refinement : " << wantRefine << nl
00475         << "Do snapping   : " << wantSnap << nl
00476         << "Do layers     : " << wantLayers << nl
00477         << endl;
00478 
00479     if (wantRefine)
00480     {
00481         const dictionary& motionDict = dict_.subDict("motionDict");
00482 
00483         autoRefineDriver refineDriver
00484         (
00485             meshRefinerPtr_(),
00486             decomposerPtr_(),
00487             distributorPtr_(),
00488             globalToPatch_
00489         );
00490 
00491         // Get all the refinement specific params
00492         refinementParameters refineParams(dict_, 1);
00493 
00494         refineDriver.doRefine(dict_, refineParams, wantSnap, motionDict);
00495 
00496         // Write mesh
00497         writeMesh("Refined mesh");
00498     }
00499 
00500     if (wantSnap)
00501     {
00502         const dictionary& snapDict = dict_.subDict("snapDict");
00503         const dictionary& motionDict = dict_.subDict("motionDict");
00504 
00505         autoSnapDriver snapDriver
00506         (
00507             meshRefinerPtr_(),
00508             globalToPatch_
00509         );
00510 
00511         // Get all the snapping specific params
00512         snapParameters snapParams(snapDict, 1);
00513 
00514         snapDriver.doSnap(snapDict, motionDict, snapParams);
00515 
00516         // Write mesh.
00517         writeMesh("Snapped mesh");
00518     }
00519 
00520     if (wantLayers)
00521     {
00522         const dictionary& motionDict = dict_.subDict("motionDict");
00523         const dictionary& shrinkDict = dict_.subDict("shrinkDict");
00524         PtrList<dictionary> surfaceDicts(dict_.lookup("surfaces"));
00525 
00526         autoLayerDriver layerDriver(meshRefinerPtr_());
00527 
00528         // Get all the layer specific params
00529         layerParameters layerParams
00530         (
00531             surfaceDicts,
00532             surfacesPtr_(),
00533             globalToPatch_,
00534             shrinkDict,
00535             mesh_.boundaryMesh()
00536         );
00537 
00538         layerDriver.doLayers
00539         (
00540             shrinkDict,
00541             motionDict,
00542             layerParams,
00543             decomposerPtr_(),
00544             distributorPtr_()
00545         );
00546 
00547         // Write mesh.
00548         writeMesh("Layer mesh");
00549     }
00550 }
00551 
00552 
00553 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd