subsetToPatch.C 4.91 KB
Newer Older
Franjo's avatar
Franjo committed
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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
Andrew Heather's avatar
Andrew Heather committed
9
    This file is part of OpenFOAM.
Franjo's avatar
Franjo committed
10

Andrew Heather's avatar
Andrew Heather committed
11 12 13 14
    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.
Franjo's avatar
Franjo committed
15

Andrew Heather's avatar
Andrew Heather committed
16
    OpenFOAM 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
Andrew Heather's avatar
Andrew Heather committed
22
    along with OpenFOAM.  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 36 37 38 39 40 41 42

Description
    Creates surface patches from surface subsets

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

#include "argList.H"
#include "triSurf.H"
#include "demandDrivenData.H"

using namespace Foam;

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

void makePatchFromSubset
(
    triSurf& origSurf,
    const DynList<word>& subsetNames
)
{
Andrew Heather's avatar
Andrew Heather committed
43
    // create new list of patches
Franjo's avatar
Franjo committed
44 45 46 47 48
    geometricSurfacePatchList newPatches
    (
        origSurf.patches().size() + subsetNames.size()
    );

Andrew Heather's avatar
Andrew Heather committed
49
    // set names of the new patches
Franjo's avatar
Franjo committed
50 51 52 53 54 55 56
    forAll(origSurf.patches(), patchI)
        newPatches[patchI].name() = origSurf.patches()[patchI].name();

    forAll(subsetNames, subsetI)
        newPatches[origSurf.patches().size()+subsetI].name() =
            subsetNames[subsetI];

Andrew Heather's avatar
Andrew Heather committed
57
    // create new triangles
Franjo's avatar
Franjo committed
58 59
    LongList<labelledTri> newTriangles(origSurf.facets());

Andrew Heather's avatar
Andrew Heather committed
60
    // set patches for all triangles
Franjo's avatar
Franjo committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    forAll(subsetNames, subsetI)
    {
        const label subsetID = origSurf.facetSubsetIndex(subsetNames[subsetI]);

        labelLongList subsetFaces;
        origSurf.facetsInSubset(subsetID, subsetFaces);

        const label regionI = origSurf.patches().size() + subsetI;

        forAll(subsetFaces, fI)
        {
            newTriangles[subsetFaces[fI]].region() = regionI;
        }
    }

Andrew Heather's avatar
Andrew Heather committed
76
    // remove patches with no elements
Franjo's avatar
Franjo committed
77 78 79 80 81 82 83 84
    labelList nTrianglesInPatch(newPatches.size(), 0);
    forAll(newTriangles, triI)
        ++nTrianglesInPatch[newTriangles[triI].region()];

    Map<label> newPatchLabel;
    label counter(0);
    forAll(nTrianglesInPatch, patchI)
    {
Andrew Heather's avatar
Andrew Heather committed
85
        if (nTrianglesInPatch[patchI])
Franjo's avatar
Franjo committed
86 87 88 89 90 91 92
            newPatchLabel.insert(patchI, counter++);
    }

    geometricSurfacePatchList copyPatches(counter);
    counter = 0;
    forAll(newPatches, patchI)
    {
Andrew Heather's avatar
Andrew Heather committed
93
        if (newPatchLabel.found(patchI))
Franjo's avatar
Franjo committed
94 95 96 97 98 99 100 101
        {
            copyPatches[newPatchLabel[patchI]].name() =
                newPatches[patchI].name();
        }
    }

    newPatches = copyPatches;

Andrew Heather's avatar
Andrew Heather committed
102
    // renumber the patches in the list of triangles
Franjo's avatar
Franjo committed
103 104 105 106
    forAll(newTriangles, triI)
        newTriangles[triI].region() =
            newPatchLabel[newTriangles[triI].region()];

Andrew Heather's avatar
Andrew Heather committed
107
    // delete subsets converted to patches
Franjo's avatar
Franjo committed
108 109 110 111 112 113
    forAll(subsetNames, subsetI)
    {
        const label subsetID = origSurf.facetSubsetIndex(subsetNames[subsetI]);
        origSurf.removeFacetSubset(subsetID);
    }

Andrew Heather's avatar
Andrew Heather committed
114
    // update subsets
Franjo's avatar
Franjo committed
115 116 117 118 119 120 121 122 123 124 125 126 127
    origSurf.updateFacetsSubsets(newPatchLabel);
}


int main(int argc, char *argv[])
{
    argList::noParallel();
    argList::validArgs.clear();

    argList::validArgs.append("input surface file");
    argList::validArgs.append("subset");
    argList args(argc, argv);

Mark Olesen's avatar
Mark Olesen committed
128 129
    fileName inFileName(args[1]);
    word subsetName(args[2]);
Franjo's avatar
Franjo committed
130 131 132 133 134

    triSurf* origSurfPtr = new triSurf(inFileName);

    DynList<word> subsetNames;
    const label subsetID = origSurfPtr->facetSubsetIndex(subsetName);
Andrew Heather's avatar
Andrew Heather committed
135
    if (subsetID >= 0)
Franjo's avatar
Franjo committed
136
    {
Andrew Heather's avatar
Andrew Heather committed
137 138 139
        Warning
            << "Subset " << subsetName
            << " checking subsets containing this string!" << endl;
Franjo's avatar
Franjo committed
140 141 142 143 144 145 146 147 148

        DynList<label> existingSubsets;
        origSurfPtr->facetSubsetIndices(existingSubsets);

        forAll(existingSubsets, subsetI)
        {
            const word sName =
                origSurfPtr->facetSubsetName(existingSubsets[subsetI]);

Andrew Heather's avatar
Andrew Heather committed
149
            if (sName.substr(0, subsetName.size()) == subsetName)
Franjo's avatar
Franjo committed
150 151 152 153 154
            {
                subsetNames.append(sName);
            }
        }

Andrew Heather's avatar
Andrew Heather committed
155
        Info<< "Converting " << subsetNames.size() << " subsets" << endl;
Franjo's avatar
Franjo committed
156 157 158 159 160 161 162 163 164 165
    }
    else
    {
        subsetNames.append(subsetName);
    }

    makePatchFromSubset(*origSurfPtr, subsetNames);
    origSurfPtr->writeSurface(inFileName);
    deleteDemandDrivenData(origSurfPtr);

Andrew Heather's avatar
Andrew Heather committed
166
    Info<< "End\n" << endl;
Franjo's avatar
Franjo committed
167 168 169
    return 0;
}

Andrew Heather's avatar
Andrew Heather committed
170

Franjo's avatar
Franjo committed
171
// ************************************************************************* //