Skip to content
Snippets Groups Projects
pairPatchAgglomeration.C 16.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • sergio's avatar
    sergio committed
    /*---------------------------------------------------------------------------*\
      =========                 |
      \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
       \\    /   O peration     |
    
    OpenFOAM bot's avatar
    OpenFOAM bot committed
        \\  /    A nd           | www.openfoam.com
    
         \\/     M anipulation  |
    -------------------------------------------------------------------------------
    
    OpenFOAM bot's avatar
    OpenFOAM bot committed
        Copyright (C) 2011-2016 OpenFOAM Foundation
    
        Copyright (C) 2016-2020 OpenCFD Ltd.
    
    sergio's avatar
    sergio committed
    -------------------------------------------------------------------------------
    License
        This file is part of OpenFOAM.
    
        OpenFOAM 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.
    
        OpenFOAM 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 OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
    
    \*---------------------------------------------------------------------------*/
    
    #include "pairPatchAgglomeration.H"
    #include "meshTools.H"
    
    andy's avatar
    andy committed
    #include "unitConversion.H"
    
    sergio's avatar
    sergio committed
    
    // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
    
    void Foam::pairPatchAgglomeration::compactLevels(const label nCreatedLevels)
    {
        nFaces_.setSize(nCreatedLevels);
        restrictAddressing_.setSize(nCreatedLevels);
        patchLevels_.setSize(nCreatedLevels);
    }
    
    
    bool Foam::pairPatchAgglomeration::continueAgglomerating
    (
    
        const label nLocal,
        const label nLocalOld
    
    sergio's avatar
    sergio committed
    )
    {
    
        // Keep agglomerating
        // - if global number of faces is still changing
        // - and if local number of faces still too large (on any processor)
        //       or if global number of faces still too large
    
        label nGlobal = returnReduce(nLocal, sumOp<label>());
        label nGlobalOld = returnReduce(nLocalOld, sumOp<label>());
    
        return
        (
            returnReduce(nLocal > nFacesInCoarsestLevel_, orOp<bool>())
         || nGlobal > nGlobalFacesInCoarsestLevel_
        )
        && nGlobal != nGlobalOld;
    
    void Foam::pairPatchAgglomeration::setLevel0EdgeWeights()
    
    sergio's avatar
    sergio committed
    {
        const bPatch& coarsePatch = patchLevels_[0];
        forAll(coarsePatch.edges(), i)
        {
    
    andy's avatar
    andy committed
            if (coarsePatch.isInternalEdge(i))
    
    sergio's avatar
    sergio committed
            {
                scalar edgeLength =
                    coarsePatch.edges()[i].mag(coarsePatch.localPoints());
    
                const labelList& eFaces = coarsePatch.edgeFaces()[i];
    
                if (eFaces.size() == 2)
                {
                    scalar cosI =
    
    andy's avatar
    andy committed
                        coarsePatch.faceNormals()[eFaces[0]]
                      & coarsePatch.faceNormals()[eFaces[1]];
    
    sergio's avatar
    sergio committed
    
                    const edge edgeCommon = edge(eFaces[0], eFaces[1]);
    
    
    andy's avatar
    andy committed
                    if (facePairWeight_.found(edgeCommon))
    
    sergio's avatar
    sergio committed
                    {
                        facePairWeight_[edgeCommon] += edgeLength;
                    }
                    else
                    {
                        facePairWeight_.insert(edgeCommon, edgeLength);
                    }
    
    
                    if (mag(cosI) < Foam::cos(degToRad(featureAngle_)))
    
    sergio's avatar
    sergio committed
                    {
                        facePairWeight_[edgeCommon] = -1.0;
                    }
                }
    
    sergio's avatar
    sergio committed
                {
    
                    forAll(eFaces, j)
                    {
                        for (label k = j+1; k<eFaces.size(); k++)
                        {
                            facePairWeight_.insert
                            (
                                edge(eFaces[j], eFaces[k]),
                                -1.0
                            );
                        }
                    }
    
    sergio's avatar
    sergio committed
                }
            }
        }
    }
    
    
    void Foam::pairPatchAgglomeration::setEdgeWeights
    (
        const label fineLevelIndex
    )
    {
        const bPatch& coarsePatch = patchLevels_[fineLevelIndex];
        const labelList& fineToCoarse = restrictAddressing_[fineLevelIndex];
    
    andy's avatar
    andy committed
        const label nCoarseI =  max(fineToCoarse) + 1;
        labelListList coarseToFine(invertOneToMany(nCoarseI, fineToCoarse));
    
        edgeHashSet fineFeaturedFaces(coarsePatch.nEdges()/10);
    
    sergio's avatar
    sergio committed
    
        // Map fine faces with featured edge into coarse faces
    
        forAllConstIters(facePairWeight_, iter)
    
    sergio's avatar
    sergio committed
        {
            if (iter() == -1.0)
            {
                const edge e = iter.key();
                const edge edgeFeatured
                (
                    fineToCoarse[e[0]],
                    fineToCoarse[e[1]]
                );
                fineFeaturedFaces.insert(edgeFeatured);
            }
        }
    
    
    Andrew Heather's avatar
    Andrew Heather committed
        // Clean old weights
    
    sergio's avatar
    sergio committed
        facePairWeight_.clear();
        facePairWeight_.resize(coarsePatch.nEdges());
    
        forAll(coarsePatch.edges(), i)
        {
    
    andy's avatar
    andy committed
            if (coarsePatch.isInternalEdge(i))
    
    sergio's avatar
    sergio committed
            {
                scalar edgeLength =
                    coarsePatch.edges()[i].mag(coarsePatch.localPoints());
    
                const labelList& eFaces = coarsePatch.edgeFaces()[i];
    
                if (eFaces.size() == 2)
                {
                    const edge edgeCommon = edge(eFaces[0], eFaces[1]);
    
    andy's avatar
    andy committed
                    if (facePairWeight_.found(edgeCommon))
    
    sergio's avatar
    sergio committed
                    {
                        facePairWeight_[edgeCommon] += edgeLength;
                    }
                    else
                    {
                        facePairWeight_.insert(edgeCommon, edgeLength);
                    }
                    // If the fine 'pair' faces was featured edge so it is
                    // the coarse 'pair'
                    if (fineFeaturedFaces.found(edgeCommon))
                    {
                        facePairWeight_[edgeCommon] = -1.0;
                    }
                }
    
    sergio's avatar
    sergio committed
                {
    
                    // Set edge as barrier by setting weight to -1
                    forAll(eFaces, j)
                    {
                        for (label k = j+1; k<eFaces.size(); k++)
                        {
                            facePairWeight_.insert
                            (
                                edge(eFaces[j], eFaces[k]),
                                -1.0
                            );
                        }
                    }
    
    sergio's avatar
    sergio committed
                }
            }
        }
    }
    
    
    // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
    
    Foam::pairPatchAgglomeration::pairPatchAgglomeration
    (
    
        const faceList& faces,
        const pointField& points,
    
        const dictionary& controlDict
    
    sergio's avatar
    sergio committed
    )
    :
        mergeLevels_
        (
    
            controlDict.getOrDefault<label>("mergeLevels", 2)
    
    sergio's avatar
    sergio committed
        ),
        maxLevels_(50),
        nFacesInCoarsestLevel_
        (
    
            controlDict.get<label>("nFacesInCoarsestLevel")
    
    sergio's avatar
    sergio committed
        ),
    
        nGlobalFacesInCoarsestLevel_(labelMax),
        //(
    
        //    controlDict.get<label>("nGlobalFacesInCoarsestLevel")
    
    sergio's avatar
    sergio committed
        featureAngle_
        (
    
            controlDict.getOrDefault<scalar>("featureAngle", 0)
    
    sergio's avatar
    sergio committed
        ),
        nFaces_(maxLevels_),
        restrictAddressing_(maxLevels_),
    
        restrictTopBottomAddressing_(identity(faces.size())),
    
    sergio's avatar
    sergio committed
        patchLevels_(maxLevels_),
    
        facePairWeight_(faces.size())
    
    sergio's avatar
    sergio committed
    {
        // Set base fine patch
    
        patchLevels_.set(0, new bPatch(faces, points));
    
    sergio's avatar
    sergio committed
    
        // Set number of faces for the base patch
    
        nFaces_[0] = faces.size();
    
    sergio's avatar
    sergio committed
    
        // Set edge weights for level 0
    
        setLevel0EdgeWeights();
    }
    
    
    Foam::pairPatchAgglomeration::pairPatchAgglomeration
    (
    
        const faceList& faces,
        const pointField& points,
    
        const label mergeLevels,
        const label maxLevels,
        const label nFacesInCoarsestLevel,          // local number of cells
        const label nGlobalFacesInCoarsestLevel,    // global number of cells
        const scalar featureAngle
    )
    :
        mergeLevels_(mergeLevels),
        maxLevels_(maxLevels),
        nFacesInCoarsestLevel_(nFacesInCoarsestLevel),
        nGlobalFacesInCoarsestLevel_(nGlobalFacesInCoarsestLevel),
        featureAngle_(featureAngle),
        nFaces_(maxLevels_),
        restrictAddressing_(maxLevels_),
    
        restrictTopBottomAddressing_(identity(faces.size())),
    
        patchLevels_(maxLevels_),
    
        facePairWeight_(faces.size())
    
    {
        // Set base fine patch
    
        patchLevels_.set(0, new bPatch(faces, points));
    
    
        // Set number of faces for the base patch
    
        nFaces_[0] = faces.size();
    
    
        // Set edge weights for level 0
        setLevel0EdgeWeights();
    
    sergio's avatar
    sergio committed
    // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
    
    Foam::pairPatchAgglomeration::~pairPatchAgglomeration()
    {}
    
    
    sergio's avatar
    sergio committed
    // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
    
    
    const Foam::pairPatchAgglomeration::bPatch&
    Foam::pairPatchAgglomeration::patchLevel
    
    sergio's avatar
    sergio committed
    (
        const label i
    ) const
    {
        return patchLevels_[i];
    }
    
    
    void Foam::pairPatchAgglomeration::mapBaseToTopAgglom
    (
        const label fineLevelIndex
    )
    {
        const labelList& fineToCoarse = restrictAddressing_[fineLevelIndex];
    
        forAll(restrictTopBottomAddressing_, i)
    
    sergio's avatar
    sergio committed
        {
            restrictTopBottomAddressing_[i] =
                fineToCoarse[restrictTopBottomAddressing_[i]];
        }
    }
    
    
    bool Foam::pairPatchAgglomeration::agglomeratePatch
    (
        const bPatch& patch,
        const labelList& fineToCoarse,
        const label fineLevelIndex
    )
    {
        if (min(fineToCoarse) == -1)
        {
    
            FatalErrorInFunction
                << "min(fineToCoarse) == -1" << exit(FatalError);
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
        if (fineToCoarse.size() == 0)
        {
    
    sergio's avatar
    sergio committed
        if (fineToCoarse.size() != patch.size())
        {
    
            FatalErrorInFunction
                << "restrict map does not correspond to fine level. " << endl
    
    sergio's avatar
    sergio committed
                << " Sizes: restrictMap: " << fineToCoarse.size()
                << " nEqns: " << patch.size()
                << abort(FatalError);
        }
    
    
        const label nCoarseI =  max(fineToCoarse) + 1;
    
    sergio's avatar
    sergio committed
        List<face> patchFaces(nCoarseI);
    
    
    sergio's avatar
    sergio committed
        // Patch faces per agglomeration
    
    andy's avatar
    andy committed
        labelListList coarseToFine(invertOneToMany(nCoarseI, fineToCoarse));
    
    sergio's avatar
    sergio committed
    
        for (label coarseI = 0; coarseI < nCoarseI; coarseI++)
        {
            const labelList& fineFaces = coarseToFine[coarseI];
    
            // Construct single face
            indirectPrimitivePatch upp
            (
                IndirectList<face>(patch, fineFaces),
                patch.points()
            );
    
            if (upp.edgeLoops().size() != 1)
            {
                if (fineFaces.size() == 2)
                {
                    const edge e(fineFaces[0], fineFaces[1]);
                    facePairWeight_[e] = -1.0;
                }
                else if (fineFaces.size() == 3)
                {
                    const edge e(fineFaces[0], fineFaces[1]);
                    const edge e1(fineFaces[0], fineFaces[2]);
                    const edge e2(fineFaces[2], fineFaces[1]);
                    facePairWeight_[e] = -1.0;
                    facePairWeight_[e1] = -1.0;
                    facePairWeight_[e2] = -1.0;
                }
                return false;
            }
    
            patchFaces[coarseI] = face
            (
                renumber
                (
                    upp.meshPoints(),
                    upp.edgeLoops()[0]
                )
            );
        }
    
        patchLevels_.set
        (
            fineLevelIndex,
            new bPatch
            (
                SubList<face>(patchFaces, nCoarseI, 0),
                patch.points()
            )
        );
    
    sergio's avatar
    sergio committed
        return true;
    }
    
    
    
    void Foam::pairPatchAgglomeration::agglomerate()
    
    sergio's avatar
    sergio committed
    {
        label nPairLevels = 0;
    
        label nCreatedLevels = 1; // 0 level is the base patch
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
        label nCoarseFaces = 0;
        label nCoarseFacesOld = 0;
    
    sergio's avatar
    sergio committed
    
        while (nCreatedLevels < maxLevels_)
        {
            const bPatch& patch = patchLevels_[nCreatedLevels - 1];
    
            // Agglomerate locally
            tmp<labelField> tfinalAgglom;
    
            bool createdLevel = false;
            while (!createdLevel)
            {
                // Agglomerate locally using edge weights
                // - calculates nCoarseFaces; returns fine to coarse addressing
                tfinalAgglom = agglomerateOneLevel(nCoarseFaces, patch);
    
                if (nCoarseFaces == 0)
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
                {
    
                    break;
                }
                else
                {
                    // Attempt to create coarse face addressing
                    // - returns true if successful; otherwise resets edge weights
                    //   and assume try again...
                    createdLevel = agglomeratePatch
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
                    (
    
                        patch,
                        tfinalAgglom,
                        nCreatedLevels
                    );
                }
            }
    
            if (createdLevel)
            {
                restrictAddressing_.set(nCreatedLevels, tfinalAgglom);
    
                mapBaseToTopAgglom(nCreatedLevels);
    
                setEdgeWeights(nCreatedLevels);
    
                if (nPairLevels % mergeLevels_)
                {
                    combineLevels(nCreatedLevels);
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
                }
    
                nFaces_[nCreatedLevels] = nCoarseFaces;
            }
    
            // Check to see if we need to continue agglomerating
            // - Note: performs parallel reductions
            if (!continueAgglomerating(nCoarseFaces, nCoarseFacesOld))
    
    sergio's avatar
    sergio committed
            {
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
                break;
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
            nCoarseFacesOld = nCoarseFaces;
    
    sergio's avatar
    sergio committed
        }
    
    sergio's avatar
    sergio committed
    }
    
    
    Foam::tmp<Foam::labelField> Foam::pairPatchAgglomeration::agglomerateOneLevel
    (
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
        label& nCoarseFaces,
    
    sergio's avatar
    sergio committed
        const bPatch& patch
    )
    {
        const label nFineFaces = patch.size();
    
        tmp<labelField> tcoarseCellMap(new labelField(nFineFaces, -1));
    
        labelField& coarseCellMap = tcoarseCellMap.ref();
    
    sergio's avatar
    sergio committed
    
        const labelListList& faceFaces = patch.faceFaces();
    
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
        nCoarseFaces = 0;
    
    andy's avatar
    andy committed
        forAll(faceFaces, facei)
    
    sergio's avatar
    sergio committed
        {
            const labelList& fFaces = faceFaces[facei];
    
            if (coarseCellMap[facei] < 0)
            {
                label matchFaceNo = -1;
                label matchFaceNeibNo = -1;
                scalar maxFaceWeight = -GREAT;
    
    
                // Check faces to find ungrouped neighbour with largest face weight
    
    sergio's avatar
    sergio committed
                forAll(fFaces, i)
                {
                    label faceNeig = fFaces[i];
                    const edge edgeCommon = edge(facei, faceNeig);
                    if
                    (
                        facePairWeight_[edgeCommon] > maxFaceWeight
    
    andy's avatar
    andy committed
                     && coarseCellMap[faceNeig] < 0
                     && facePairWeight_[edgeCommon] != -1.0
    
    sergio's avatar
    sergio committed
                    )
                    {
                        // Match found. Pick up all the necessary data
                        matchFaceNo = facei;
                        matchFaceNeibNo = faceNeig;
                        maxFaceWeight = facePairWeight_[edgeCommon];
                    }
                }
    
                if (matchFaceNo >= 0)
                {
                    // Make a new group
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
                    coarseCellMap[matchFaceNo] = nCoarseFaces;
                    coarseCellMap[matchFaceNeibNo] = nCoarseFaces;
                    nCoarseFaces++;
    
    sergio's avatar
    sergio committed
                }
                else
                {
                    // No match. Find the best neighbouring cluster and
                    // put the cell there
                    label clusterMatchFaceNo = -1;
                    scalar clusterMaxFaceCoeff = -GREAT;
    
                    forAll(fFaces, i)
                    {
                        label faceNeig = fFaces[i];
                        const edge edgeCommon = edge(facei, faceNeig);
                        if
                        (
                            facePairWeight_[edgeCommon] > clusterMaxFaceCoeff
    
                         && facePairWeight_[edgeCommon] != -1.0
                         && coarseCellMap[faceNeig] >= 0
    
    sergio's avatar
    sergio committed
                        )
                        {
                            clusterMatchFaceNo = faceNeig;
                            clusterMaxFaceCoeff = facePairWeight_[edgeCommon];
                        }
                    }
    
    
                    if (clusterMatchFaceNo > 0)
    
    sergio's avatar
    sergio committed
                    {
                        // Add the cell to the best cluster
                        coarseCellMap[facei] = coarseCellMap[clusterMatchFaceNo];
                    }
                    else
    
                        // If not create single-cell "clusters" for each
    
    Sergio Ferraris's avatar
    Sergio Ferraris committed
                        coarseCellMap[facei] = nCoarseFaces;
    
    sergio's avatar
    sergio committed
                    }
                }
            }
        }
    
        // Check that all faces are part of clusters,
        for (label facei=0; facei<nFineFaces; facei++)
        {
            if (coarseCellMap[facei] < 0)
            {
    
                FatalErrorInFunction
    
                    << " face " << facei
                    << " is not part of a cluster"
                    << exit(FatalError);
    
    sergio's avatar
    sergio committed
            }
        }
    
        return tcoarseCellMap;
    }
    
    
    sergio's avatar
    sergio committed
    void Foam::pairPatchAgglomeration::combineLevels(const label curLevel)
    {
        label prevLevel = curLevel - 1;
    
        // Set the previous level nCells to the current
        nFaces_[prevLevel] = nFaces_[curLevel];
    
        // Map the restrictAddressing from the coarser level into the previous
        // finer level
    
        const labelList& curResAddr = restrictAddressing_[curLevel];
        labelList& prevResAddr = restrictAddressing_[prevLevel];
    
        forAll(prevResAddr, i)
        {
            prevResAddr[i] = curResAddr[prevResAddr[i]];
        }
    
        // Delete the restrictAddressing for the coarser level
    
        restrictAddressing_.set(curLevel, nullptr);
    
        patchLevels_.set(prevLevel, patchLevels_.set(curLevel, nullptr));
    
    sergio's avatar
    sergio committed
    }
    
    sergio's avatar
    sergio committed
    // ************************************************************************* //