Forward compatibility: flex
[foam-extend-3.2.git] / applications / utilities / mesh / generation / cfMesh / subsetToPatch / subsetToPatch.C
blobf1d4d3e08374d968792533e76cf5d725977e91b7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Creates surface patches from surface subsets
27 \*---------------------------------------------------------------------------*/
29 #include "argList.H"
30 #include "objectRegistry.H"
31 #include "foamTime.H"
32 #include "triSurf.H"
33 #include "demandDrivenData.H"
35 using namespace Foam;
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 void makePatchFromSubset
41     triSurf& origSurf,
42     const DynList<word>& subsetNames
45     //- create new list of patches
46     geometricSurfacePatchList newPatches
47     (
48         origSurf.patches().size() + subsetNames.size()
49     );
51     //- set names of the new patches
52     forAll(origSurf.patches(), patchI)
53         newPatches[patchI].name() = origSurf.patches()[patchI].name();
55     forAll(subsetNames, subsetI)
56         newPatches[origSurf.patches().size()+subsetI].name() =
57             subsetNames[subsetI];
59     //- create new triangles
60     LongList<labelledTri> newTriangles(origSurf.facets());
62     //- set patches for all triangles
63     forAll(subsetNames, subsetI)
64     {
65         const label subsetID = origSurf.facetSubsetIndex(subsetNames[subsetI]);
67         labelLongList subsetFaces;
68         origSurf.facetsInSubset(subsetID, subsetFaces);
70         const label regionI = origSurf.patches().size() + subsetI;
72         forAll(subsetFaces, fI)
73         {
74             newTriangles[subsetFaces[fI]].region() = regionI;
75         }
76     }
78     //- remove patches with no elements
79     labelList nTrianglesInPatch(newPatches.size(), 0);
80     forAll(newTriangles, triI)
81         ++nTrianglesInPatch[newTriangles[triI].region()];
83     Map<label> newPatchLabel;
84     label counter(0);
85     forAll(nTrianglesInPatch, patchI)
86     {
87         if( nTrianglesInPatch[patchI] )
88             newPatchLabel.insert(patchI, counter++);
89     }
91     geometricSurfacePatchList copyPatches(counter);
92     counter = 0;
93     forAll(newPatches, patchI)
94     {
95         if( newPatchLabel.found(patchI) )
96         {
97             copyPatches[newPatchLabel[patchI]].name() =
98                 newPatches[patchI].name();
99         }
100     }
102     newPatches = copyPatches;
104     //- renumber the patches in the list of triangles
105     forAll(newTriangles, triI)
106         newTriangles[triI].region() =
107             newPatchLabel[newTriangles[triI].region()];
109     //- delete subsets converted to patches
110     forAll(subsetNames, subsetI)
111     {
112         const label subsetID = origSurf.facetSubsetIndex(subsetNames[subsetI]);
113         origSurf.removeFacetSubset(subsetID);
114     }
116     //- update subsets
117     origSurf.updateFacetsSubsets(newPatchLabel);
121 int main(int argc, char *argv[])
123     argList::noParallel();
124     argList::validArgs.clear();
126     argList::validArgs.append("input surface file");
127     argList::validArgs.append("subset");
128     argList args(argc, argv);
130     fileName inFileName(args.args()[1]);
131     word subsetName(args.args()[2]);
133     triSurf* origSurfPtr = new triSurf(inFileName);
135     DynList<word> subsetNames;
136     const label subsetID = origSurfPtr->facetSubsetIndex(subsetName);
137     if( subsetID >= 0 )
138     {
139         Warning << "Subset " << subsetName
140         << " checking subsets containing this string!" << endl;
142         DynList<label> existingSubsets;
143         origSurfPtr->facetSubsetIndices(existingSubsets);
145         forAll(existingSubsets, subsetI)
146         {
147             const word sName =
148                 origSurfPtr->facetSubsetName(existingSubsets[subsetI]);
150             if( sName.substr(0, subsetName.size()) == subsetName )
151             {
152                 subsetNames.append(sName);
153             }
154         }
156         Info << "Converting " << subsetNames.size() << " subsets" << endl;
157     }
158     else
159     {
160         subsetNames.append(subsetName);
161     }
163     makePatchFromSubset(*origSurfPtr, subsetNames);
164     origSurfPtr->writeSurface(inFileName);
165     deleteDemandDrivenData(origSurfPtr);
167     Info << "End\n" << endl;
168     return 0;
171 // ************************************************************************* //