tetMeshGenerator.C 10.2 KB
Newer Older
franjo's avatar
franjo committed
1 2
/*---------------------------------------------------------------------------*\
  =========                 |
Tomislav Lugaric's avatar
Tomislav Lugaric committed
3
  \\      /  F ield         | cfMesh: A library for mesh generation
franjo's avatar
franjo committed
4
   \\    /   O peration     |
Tomislav Lugaric's avatar
Tomislav Lugaric committed
5 6
    \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
     \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
franjo's avatar
franjo committed
7 8
-------------------------------------------------------------------------------
License
Tomislav Lugaric's avatar
Tomislav Lugaric committed
9
    This file is part of cfMesh.
franjo's avatar
franjo committed
10

Tomislav Lugaric's avatar
Tomislav Lugaric committed
11
    cfMesh is free software; you can redistribute it and/or modify it
franjo's avatar
franjo committed
12
    under the terms of the GNU General Public License as published by the
Tomislav Lugaric's avatar
Tomislav Lugaric committed
13
    Free Software Foundation; either version 3 of the License, or (at your
franjo's avatar
franjo committed
14 15
    option) any later version.

Tomislav Lugaric's avatar
Tomislav Lugaric committed
16
    cfMesh is distributed in the hope that it will be useful, but WITHOUT
franjo's avatar
franjo committed
17 18 19 20 21
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
Tomislav Lugaric's avatar
Tomislav Lugaric committed
22
    along with cfMesh.  If not, see <http://www.gnu.org/licenses/>.
franjo's avatar
franjo committed
23 24 25 26 27 28 29 30 31 32 33 34 35

Description

\*---------------------------------------------------------------------------*/

#include "tetMeshGenerator.H"
#include "triSurf.H"
#include "demandDrivenData.H"
#include "Time.H"
#include "meshOctreeCreator.H"
#include "tetMeshExtractorOctree.H"
#include "meshSurfaceEngine.H"
#include "meshSurfaceMapper.H"
36
#include "edgeExtractor.H"
franjo's avatar
franjo committed
37 38 39 40 41 42 43 44
#include "meshSurfaceEdgeExtractorNonTopo.H"
#include "surfaceMorpherCells.H"
#include "meshOptimizer.H"
#include "meshSurfaceOptimizer.H"
#include "topologicalCleaner.H"
#include "boundaryLayers.H"
#include "renameBoundaryPatches.H"
#include "checkMeshDict.H"
45
#include "triSurfacePatchManipulator.H"
46
#include "refineBoundaryLayers.H"
Franjo's avatar
Franjo committed
47
#include "triSurfaceMetaData.H"
48 49
#include "polyMeshGenGeometryModification.H"
#include "surfaceMeshGeometryModification.H"
franjo's avatar
franjo committed
50 51 52 53 54 55 56 57 58 59 60 61 62

//#define DEBUG

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

// * * * * * * * * * * * * Private member functions  * * * * * * * * * * * * //

void tetMeshGenerator::createTetMesh()
{
    //- create tet Mesh from octree and Delaunay tets
63
    tetMeshExtractorOctree tme(*octreePtr_, meshDict_, mesh_);
franjo's avatar
franjo committed
64 65 66 67 68 69 70 71 72 73 74

    tme.createMesh();
}

void tetMeshGenerator::surfacePreparation()
{
    //- removes unnecessary cells and morph the boundary
    //- such that there is only one boundary face per cell
    //- It also checks topology of cells after morphing is performed
    do
    {
75
        surfaceMorpherCells* cmPtr = new surfaceMorpherCells(mesh_);
franjo's avatar
franjo committed
76
        cmPtr->morphMesh();
77
        deleteDemandDrivenData(cmPtr);
franjo's avatar
franjo committed
78
    }
79
    while( topologicalCleaner(mesh_).cleanTopology() );
franjo's avatar
franjo committed
80 81 82 83 84
}

void tetMeshGenerator::mapMeshToSurface()
{
    //- calculate mesh surface
85
    meshSurfaceEngine* msePtr = new meshSurfaceEngine(mesh_);
franjo's avatar
franjo committed
86 87

    //- map mesh surface on the geometry surface
88
    meshSurfaceMapper(*msePtr, *octreePtr_).mapVerticesOntoSurface();
89

franjo's avatar
franjo committed
90
    //- untangle surface faces
91
    meshSurfaceOptimizer(*msePtr, *octreePtr_).untangleSurface();
franjo's avatar
franjo committed
92

93
    deleteDemandDrivenData(msePtr);
franjo's avatar
franjo committed
94 95
}

96 97 98 99 100 101 102 103 104 105
void tetMeshGenerator::extractPatches()
{
    edgeExtractor extractor(mesh_, *octreePtr_);

    Info << "Extracting edges" << endl;
    extractor.extractEdges();

    extractor.updateMeshPatches();
}

franjo's avatar
franjo committed
106 107
void tetMeshGenerator::mapEdgesAndCorners()
{
108
    meshSurfaceEdgeExtractorNonTopo(mesh_, *octreePtr_);
franjo's avatar
franjo committed
109 110 111 112
}

void tetMeshGenerator::optimiseMeshSurface()
{
113 114
    meshSurfaceEngine mse(mesh_);
    meshSurfaceOptimizer(mse, *octreePtr_).optimizeSurface();
franjo's avatar
franjo committed
115 116
}

117
void tetMeshGenerator::generateBoundaryLayers()
franjo's avatar
franjo committed
118
{
119
    if( meshDict_.found("boundaryLayers") )
franjo's avatar
franjo committed
120
    {
121 122 123
        boundaryLayers bl(mesh_);

        const dictionary& bndLayers = meshDict_.subDict("boundaryLayers");
124

125
        if( bndLayers.found("nLayers") )
126
        {
127 128 129 130
            const label nLayers = readLabel(bndLayers.lookup("nLayers"));

            if( nLayers > 0 )
                bl.addLayerForAllPatches();
131
        }
132
        else if( bndLayers.found("patchBoundaryLayers") )
133
        {
134 135 136
            const dictionary& patchLayers =
                bndLayers.subDict("patchBoundaryLayers");
            const wordList createLayers = patchLayers.toc();
franjo's avatar
franjo committed
137

138 139 140
            forAll(createLayers, patchI)
                bl.addLayerForPatch(createLayers[patchI]);
        }
franjo's avatar
franjo committed
141 142 143 144 145 146
    }
}

void tetMeshGenerator::optimiseFinalMesh()
{
    //- final optimisation
147 148 149 150 151 152 153
    bool enforceConstraints(false);
    if( meshDict_.found("enforceGeometryConstraints") )
    {
        enforceConstraints =
            readBool(meshDict_.lookup("enforceGeometryConstraints"));
    }

Franjo's avatar
Franjo committed
154
    meshOptimizer optimizer(mesh_);
155 156
    if( enforceConstraints )
        optimizer.enforceConstraints();
franjo's avatar
franjo committed
157

158
    optimizer.optimizeSurface(*octreePtr_);
franjo's avatar
franjo committed
159

160 161
    optimizer.optimizeMeshFV();
    optimizer.optimizeLowQualityFaces();
162
    optimizer.optimizeBoundaryLayer(false);
163 164 165
    optimizer.untangleMeshFV();

    deleteDemandDrivenData(octreePtr_);
franjo's avatar
franjo committed
166

167 168
    mesh_.clearAddressingData();

169 170 171 172 173 174 175 176 177 178
    if( modSurfacePtr_ )
    {
        polyMeshGenGeometryModification meshMod(mesh_, meshDict_);

        //- revert the mesh into the original space
        meshMod.revertGeometryModification();

        //- delete modified surface mesh
        deleteDemandDrivenData(modSurfacePtr_);
    }
franjo's avatar
franjo committed
179 180
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
void tetMeshGenerator::projectSurfaceAfterBackScaling()
{
    if( !meshDict_.found("anisotropicSources") )
        return;

    deleteDemandDrivenData(octreePtr_);
    octreePtr_ = new meshOctree(*surfacePtr_);

    meshOctreeCreator
    (
        *octreePtr_,
        meshDict_
    ).createOctreeWithRefinedBoundary(20, 30);

    //- calculate mesh surface
    meshSurfaceEngine mse(mesh_);

    //- pre-map mesh surface
    meshSurfaceMapper mapper(mse, *octreePtr_);

    //- map mesh surface on the geometry surface
    mapper.mapVerticesOntoSurface();

    optimiseFinalMesh();
}

207 208 209 210 211 212 213 214 215 216
void tetMeshGenerator::refBoundaryLayers()
{
    if( meshDict_.isDict("boundaryLayers") )
    {
        refineBoundaryLayers refLayers(mesh_);

        refineBoundaryLayers::readSettings(meshDict_, refLayers);

        refLayers.refineLayers();

217 218 219 220 221 222
        labelLongList pointsInLayer;
        refLayers.pointsInBndLayer(pointsInLayer);

        meshOptimizer opt(mesh_);
        opt.lockPoints(pointsInLayer);
        opt.untangleBoundaryLayer();
223 224 225
    }
}

franjo's avatar
franjo committed
226 227
void tetMeshGenerator::replaceBoundaries()
{
228
    renameBoundaryPatches rbp(mesh_, meshDict_);
franjo's avatar
franjo committed
229 230 231 232
}

void tetMeshGenerator::renumberMesh()
{
233
    polyMeshGenModifier(mesh_).renumberMesh();
franjo's avatar
franjo committed
234 235 236 237
}

void tetMeshGenerator::generateMesh()
{
238
    if( controller_.runCurrentStep("templateGeneration") )
239
    {
240 241
        createTetMesh();
    }
franjo's avatar
franjo committed
242

243 244 245 246
    if( controller_.runCurrentStep("surfaceTopology") )
    {
        surfacePreparation();
    }
franjo's avatar
franjo committed
247

248 249 250 251
    if( controller_.runCurrentStep("surfaceProjection") )
    {
        mapMeshToSurface();
    }
franjo's avatar
franjo committed
252

253 254 255 256
    if( controller_.runCurrentStep("patchAssignment") )
    {
        extractPatches();
    }
franjo's avatar
franjo committed
257

258 259 260
    if( controller_.runCurrentStep("edgeExtraction") )
    {
        mapEdgesAndCorners();
franjo's avatar
franjo committed
261

262 263
        optimiseMeshSurface();
    }
franjo's avatar
franjo committed
264

265 266 267 268
    if( controller_.runCurrentStep("boundaryLayerGeneration") )
    {
        generateBoundaryLayers();
    }
franjo's avatar
franjo committed
269

270 271 272
    if( controller_.runCurrentStep("meshOptimisation") )
    {
        optimiseFinalMesh();
273

274 275
        projectSurfaceAfterBackScaling();
    }
276

277 278 279 280
    if( controller_.runCurrentStep("boundaryLayerRefinement") )
    {
        refBoundaryLayers();
    }
281

282
    renumberMesh();
franjo's avatar
franjo committed
283

284
    replaceBoundaries();
285

286
    controller_.workflowCompleted();
franjo's avatar
franjo committed
287 288 289 290 291
}

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

// Construct from Time
292
tetMeshGenerator::tetMeshGenerator(const Time& time)
293 294 295
:
    runTime_(time),
    surfacePtr_(NULL),
296
    modSurfacePtr_(NULL),
297 298 299
    meshDict_
    (
        IOobject
franjo's avatar
franjo committed
300
        (
301 302 303 304 305 306 307 308
            "meshDict",
            runTime_.system(),
            runTime_,
            IOobject::MUST_READ,
            IOobject::NO_WRITE
        )
    ),
    octreePtr_(NULL),
309 310
    mesh_(time),
    controller_(mesh_)
franjo's avatar
franjo committed
311
{
312
    try
franjo's avatar
franjo committed
313
    {
314 315 316 317
        if( true )
        {
            checkMeshDict cmd(meshDict_);
        }
franjo's avatar
franjo committed
318

319
        const fileName surfaceFile = meshDict_.lookup("surfaceFile");
franjo's avatar
franjo committed
320

321
        surfacePtr_ = new triSurf(runTime_.path()/surfaceFile);
franjo's avatar
franjo committed
322

323 324 325 326 327
        if( true )
        {
            //- save meta data with the mesh (surface mesh + its topology info)
            triSurfaceMetaData sMetaData(*surfacePtr_);
            const dictionary& surfMetaDict = sMetaData.metaData();
Franjo's avatar
Franjo committed
328

329 330 331
            mesh_.metaData().add("surfaceFile", surfaceFile, true);
            mesh_.metaData().add("surfaceMeta", surfMetaDict, true);
        }
Franjo's avatar
Franjo committed
332

333 334 335 336 337
        if( surfacePtr_->featureEdges().size() != 0 )
        {
            //- create surface patches based on the feature edges
            //- and update the meshDict based on the given data
            triSurfacePatchManipulator manipulator(*surfacePtr_);
338

339 340
            const triSurf* surfaceWithPatches =
                manipulator.surfaceWithPatches(&meshDict_);
341

342 343 344 345
            //- delete the old surface and assign the new one
            deleteDemandDrivenData(surfacePtr_);
            surfacePtr_ = surfaceWithPatches;
        }
franjo's avatar
franjo committed
346

347 348 349
        if( meshDict_.found("anisotropicSources") )
        {
            surfaceMeshGeometryModification surfMod(*surfacePtr_, meshDict_);
350

351
            modSurfacePtr_ = surfMod.modifyGeometry();
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366
            octreePtr_ = new meshOctree(*modSurfacePtr_);
        }
        else
        {
            octreePtr_ = new meshOctree(*surfacePtr_);
        }

        meshOctreeCreator(*octreePtr_, meshDict_).createOctreeBoxes();

        generateMesh();
    }
    catch(const std::string& message)
    {
        Info << message << endl;
367
    }
368
    catch(...)
369
    {
370 371 372 373
        WarningIn
        (
            "tetMeshGenerator::tetMeshGenerator(const Time&)"
        ) << "Meshing process terminated!" << endl;
374
    }
franjo's avatar
franjo committed
375 376 377 378 379 380
}

// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

tetMeshGenerator::~tetMeshGenerator()
{
381 382
    deleteDemandDrivenData(surfacePtr_);
    deleteDemandDrivenData(octreePtr_);
383
    deleteDemandDrivenData(modSurfacePtr_);
franjo's avatar
franjo committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

void tetMeshGenerator::writeMesh() const
{
    mesh_.write();
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// ************************************************************************* //