improveMeshQuality.C 4.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | cfMesh: A library for mesh generation
   \\    /   O peration     |
    \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
     \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
-------------------------------------------------------------------------------
License
    This file is part of cfMesh.

    cfMesh is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the
    Free Software Foundation; either version 3 of the License, or (at your
    option) any later version.

    cfMesh is distributed in the hope that it will be useful, but WITHOUT
    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
    along with cfMesh.  If not, see <http://www.gnu.org/licenses/>.

Description
Franjo's avatar
Franjo committed
25 26
    Performs point relocations in the mesh (smoothing) in order to
    improve quality measures. It does not make the mesh invalied.
27 28 29 30 31

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

#include "argList.H"
#include "polyMeshGenModifier.H"
Franjo's avatar
Franjo committed
32
#include "meshOptimizer.H"
33 34 35 36 37 38 39

using namespace Foam;

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

int main(int argc, char *argv[])
{
Franjo's avatar
Franjo committed
40 41 42 43 44 45 46 47
    argList::validArgs.clear();

    argList::validOptions.insert("nLoops", "int");
    argList::validOptions.insert("nIterations", "int");
    argList::validOptions.insert("nSurfaceIterations", "int");
    argList::validOptions.insert("qualityThreshold", "scalar");
    argList::validOptions.insert("constrainedCellsSet", "word");

48 49 50
#   include "setRootCase.H"
#   include "createTime.H"

Franjo's avatar
Franjo committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    //- read the settings
    label nIterations(50);
    label nLoops(10);
    label nSurfaceIterations(2);
    scalar qualityThreshold(0.1);

    if( args.options().found("nLoops") )
    {
        nLoops = readLabel(IStringStream(args.options()["nLoops"])());
    }
    else
    {
        Info << "Default number of loops is 10" << endl;
    }

    if( args.options().found("nIterations") )
    {
        nIterations =
            readLabel(IStringStream(args.options()["nIterations"])());
    }
    else
    {
        Info << "Default number of iterations is 50" << endl;
    }

    if( args.options().found("nSurfaceIterations") )
    {
        nSurfaceIterations =
            readLabel(IStringStream(args.options()["nSurfaceIterations"])());
    }
    else
    {
        Info << "Default number of surface iterations is 2" << endl;
    }

    if( args.options().found("qualityThreshold") )
    {
        qualityThreshold =
            readScalar(IStringStream(args.options()["qualityThreshold"])());
    }
    else
    {
        Info << "Using default quality threshold 0.1" << endl;
    }

    word constrainedCellSet;

    if( args.options().found("constrainedCellsSet") )
    {
        constrainedCellSet = args.options()["constrainedCellsSet"];
    }
    else
    {
Franjo's avatar
Franjo committed
104
        Info << "No constraints applied on the smoothing procedure" << endl;
Franjo's avatar
Franjo committed
105 106 107
    }

    //- load the mesh from disk
108 109 110
    polyMeshGen pmg(runTime);
    pmg.read();

Franjo's avatar
Franjo committed
111 112 113 114
    //- construct the smoother
    meshOptimizer mOpt(pmg);

    if( !constrainedCellSet.empty() )
Franjo's avatar
Franjo committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    {
        //- lock cells in constrainedCellSet
        mOpt.lockCellsInSubset(constrainedCellSet);

        //- find boundary faces which shall be locked
        labelLongList lockedBndFaces, selectedCells;

        const label sId = pmg.cellSubsetIndex(constrainedCellSet);
        pmg.cellsInSubset(sId, selectedCells);

        boolList activeCell(pmg.cells().size(), false);
        forAll(selectedCells, i)
            activeCell[selectedCells[i]] = true;
    }

    //- clear geometry information before volume smoothing
    pmg.clearAddressingData();
Franjo's avatar
Franjo committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    //- perform optimisation using the laplace smoother and
    mOpt.optimizeMeshFV
    (
        nLoops,
        nLoops,
        nIterations,
        nSurfaceIterations
    );

    //- perform optimisation of worst quality faces
    mOpt.optimizeMeshFVBestQuality(nLoops, qualityThreshold);

    //- check the mesh again and untangl bad regions if any of them exist
    mOpt.untangleMeshFV(nLoops, nIterations, nSurfaceIterations);
147 148 149 150 151 152 153 154 155

    Info << "Writing mesh" << endl;
    pmg.write();

    Info << "End\n" << endl;
    return 0;
}

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