checkNonMappableCellConnections.C 9.24 KB
Newer Older
Franjo's avatar
Franjo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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

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

#include "checkNonMappableCellConnections.H"
#include "polyMeshGenModifier.H"
#include "helperFunctions.H"
#include "meshSurfaceEngine.H"

# ifdef USE_OMP
#include <omp.h>
# endif

//#define DEBUGCheck

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

namespace Foam
{

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

void checkNonMappableCellConnections::findCellTypes()
{
    const faceListPMG& faces = mesh_.faces();
    const cellListPMG& cells = mesh_.cells();
    const labelList& owner = mesh_.owner();

    cellType_.setSize(cells.size());
    cellType_ = INTERNALCELL;

    //- find boundary cells
    const PtrList<boundaryPatch>& boundaries = mesh_.boundaries();
    forAll(boundaries, patchI)
    {
        const label start = boundaries[patchI].patchStart();
        const label end = start + boundaries[patchI].patchSize();

        for(label faceI=start;faceI<end;++faceI)
            cellType_[owner[faceI]] = BNDCELL;
    }

    //- find boundary cells with all vertices at the boundary
    meshSurfaceEngine mse(mesh_);
    const labelList& bp = mse.bp();

    # ifdef USE_OMP
    # pragma omp parallel for schedule(dynamic, 1000)
    # endif
    for(label cellI=cells.size()-1;cellI>=0;--cellI)
    {
        if( cellType_[cellI] & INTERNALCELL )
            continue;

        const cell& c = cells[cellI];

        //- mark boundary cells with all vertices at the boundary
        const labelList cellPoints = c.labels(faces);
        bool allBoundary(true);
        forAll(cellPoints, cpI)
        {
            if( bp[cellPoints[cpI]] < 0 )
            {
                allBoundary = false;
                break;
            }
        }

        if( allBoundary )
        {
            cellType_[cellI] |= ALLBNDVERTEXCELL;
        }
        else
        {
            continue;
        }

        //- check if the internal faces are connected into a single group
        //- over their edges
        DynList<label> internalFaces;
        forAll(c, fI)
        {
            if( c[fI] < mesh_.nInternalFaces() )
            {
                internalFaces.append(c[fI]);
            }
            else if( mesh_.faceIsInProcPatch(c[fI]) != -1 )
            {
                internalFaces.append(c[fI]);
            }
        }

        Map<label> faceGroup(internalFaces.size());
        label nGroup(0);
        forAll(internalFaces, i)
        {
            if( faceGroup.found(internalFaces[i]) )
                continue;

            DynList<label> front;
            front.append(internalFaces[i]);
            faceGroup.insert(internalFaces[i], nGroup);

            while( front.size() )
            {
                const label fLabel = front.removeLastElement();

                forAll(internalFaces, j)
                {
                    const label nei = internalFaces[j];
                    if( faceGroup.found(nei) )
                        continue;

                    if( help::shareAnEdge(faces[fLabel], faces[nei]) )
                    {
                        front.append(nei);
                        faceGroup.insert(nei, nGroup);
                    }
                }
            }

            ++nGroup;
        }

        if( nGroup > 1 )
            cellType_[cellI] |= INTERNALFACEGROUP;
    }
}

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

checkNonMappableCellConnections::checkNonMappableCellConnections
(
    polyMeshGen& mesh
)
:
    mesh_(mesh),
    cellType_()
{
}

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

checkNonMappableCellConnections::~checkNonMappableCellConnections()
{
}

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

void checkNonMappableCellConnections::findCells(labelHashSet& badCells)
{
    badCells.clear();

    //- classify cell types
    findCellTypes();

    //- select ALLBNDVERTEXCELL and INTERNALFACEGROUP cells
    //- with at least one INTERNALCELL neighbour
    //- these cells do not need to stay in the mesh
    const cellListPMG& cells = mesh_.cells();
    const labelList& owner = mesh_.owner();
    const labelList& neighbour = mesh_.neighbour();
    const label nInternalFaces = mesh_.nInternalFaces();
    const PtrList<processorBoundaryPatch>& procBoundaries = mesh_.procBoundaries();

    labelListList otherProcType;
    if( Pstream::parRun() )
    {
        //- exchange cell types at processor boundaries
        otherProcType.setSize(procBoundaries.size());

        //- send data to other processors
        forAll(procBoundaries, patchI)
        {
            label start = procBoundaries[patchI].patchStart();
            labelList patchCellType(procBoundaries[patchI].patchSize());

            forAll(patchCellType, faceI)
                patchCellType[faceI] = cellType_[owner[start++]];

            OPstream toOtherProc
            (
207
                UPstream::commsTypes::blocking,
Franjo's avatar
Franjo committed
208 209 210 211 212 213 214 215 216 217 218 219
                procBoundaries[patchI].neiProcNo(),
                patchCellType.byteSize()
            );

            toOtherProc << patchCellType;
        }

        //- receive data from other processors
        forAll(procBoundaries, patchI)
        {
            IPstream fromOtherProc
            (
220
                UPstream::commsTypes::blocking,
Franjo's avatar
Franjo committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
                procBoundaries[patchI].neiProcNo()
            );

            labelList& otherTypes = otherProcType[patchI];
            fromOtherProc >> otherTypes;
        }
    }

    # ifdef USE_OMP
    # pragma omp parallel for schedule(dynamic, 40)
    # endif
    for(label cellI=cellType_.size()-1;cellI>=0;--cellI)
    {
        if( cellType_[cellI] & INTERNALFACEGROUP )
        {
            # ifdef USE_OMP
            # pragma omp critical
            # endif
            badCells.insert(cellI);
        }
        else if( cellType_[cellI] & (ALLBNDVERTEXCELL+INTERNALFACEGROUP) )
        {
            //- mark cells which have only one internal neighbour
            const cell& c = cells[cellI];

            bool hasInternalNeighbour(false);
            label nNeiCells(0);

            forAll(c, fI)
            {
                const label faceI = c[fI];

                if( faceI < nInternalFaces )
                {
                    ++nNeiCells;

                    label nei = neighbour[c[fI]];
                    if( nei == cellI )
                        nei = owner[c[fI]];

                    if( cellType_[nei] & INTERNALCELL )
                    {
                        hasInternalNeighbour = true;
                        break;
                    }
                }
                else if( mesh_.faceIsInProcPatch(faceI) != -1 )
                {
                    ++nNeiCells;

                    const label patchI = mesh_.faceIsInProcPatch(faceI);
                    const label j = faceI - procBoundaries[patchI].patchStart();

                    if( otherProcType[patchI][j] & INTERNALCELL )
                    {
                        hasInternalNeighbour = true;
                        break;
                    }
                }
            }

            if( hasInternalNeighbour || (nNeiCells == 1) )
            {
                # ifdef USE_OMP
                # pragma omp critical
                # endif
                badCells.insert(cellI);
            }
        }
    }
}

bool checkNonMappableCellConnections::removeCells()
{
    labelHashSet badCells;

    label nRemoved;
    bool changed(false);

    do
    {
        findCells(badCells);

        nRemoved = badCells.size();
        reduce(nRemoved, sumOp<label>());

        Info << "Found " << nRemoved << " non-mappable cells" << endl;

        if( nRemoved != 0 )
        {
            boolList removeCell(mesh_.cells().size(), false);
            forAllConstIter(labelHashSet, badCells, it)
                removeCell[it.key()] = true;

            polyMeshGenModifier(mesh_).removeCells(removeCell);

            changed = true;
        }
    } while( nRemoved );

    return changed;
}

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

} // End namespace Foam

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