Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / mesh / cfMesh / utilities / surfaceTools / renameBoundaryPatches / renameBoundaryPatches.C
blob1478bc9dca6bdbcdd68491c783e65ce13b61c722
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | cfMesh: A library for mesh generation
4    \\    /   O peration     |
5     \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6      \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9     This file is part of cfMesh.
11     cfMesh 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     cfMesh is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with cfMesh.  If not, see <http://www.gnu.org/licenses/>.
24 Description
26 \*---------------------------------------------------------------------------*/
28 #include "renameBoundaryPatches.H"
29 #include "demandDrivenData.H"
30 #include "IOdictionary.H"
32 #include <map>
34 // #define DEBUGSearch
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 namespace Foam
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 void renameBoundaryPatches::calculateNewBoundary()
45     Info << "Renaming boundary patches" << endl;
47     const dictionary& dict = meshDict_.subDict("renameBoundary");
49     std::map<word, label> patchToLabel;
50     forAll(mesh_.boundaries(), patchI)
51     {
52         patchToLabel.insert
53         (
54             std::pair<word, label>
55             (
56                 mesh_.boundaries()[patchI].patchName(),
57                 patchI
58             )
59         );
60     }
62     labelList patchToNew(mesh_.boundaries().size(), -1);
64     wordList newPatchNames(patchToNew.size());
65     wordList newPatchTypes(patchToNew.size());
66     std::map<word, label> newNameToPos;
67     label newPatchI(0);
69     //- read new patch names and types
70     if( dict.found("newPatchNames") )
71     {
72         PtrList<entry> patchesToRename;
74         if( dict.isDict("newPatchNames") )
75         {
76             const dictionary& newPatchNames = dict.subDict("newPatchNames");
77             const wordList keys = newPatchNames.toc();
79             patchesToRename.setSize(keys.size());
81             forAll(keys, patchI)
82                 patchesToRename.set
83                 (
84                     patchI,
85                     newPatchNames.lookupEntry
86                     (
87                         keys[patchI],
88                         false,
89                         false
90                     ).clone()
91                 );
92         }
93         else
94         {
95             PtrList<entry> copyPatchesToRename(dict.lookup("newPatchNames"));
96             patchesToRename.transfer(copyPatchesToRename);
97         }
99         forAll(patchesToRename, patchI)
100         {
101             const word patchName = patchesToRename[patchI].keyword();
103             const labelList matchedPatches = mesh_.findPatches(patchName);
105             if(matchedPatches.empty())
106             {
107                 Warning << "No matches for " << patchName << " found!!" << endl;
108                 continue;
109             }
111             if( !patchesToRename[patchI].isDict() )
112             {
113                 Warning << "Cannot rename patch " << patchName << endl;
114                 Warning << "This is due to incorrect settings! Exitting."
115                     << endl;
116                 return;
117             }
119             const dictionary pDict = patchesToRename[patchI].dict();
121             forAll(matchedPatches, matchI)
122             {
123                 word newName(mesh_.getPatchName(matchedPatches[matchI]));
125                 if( pDict.found("newName") )
126                     newName = word(pDict.lookup("newName"));
128                 if( newNameToPos.find(newName) != newNameToPos.end() )
129                 {
130                     //- patch with the same name already exists
131                     patchToNew[matchedPatches[matchI]] = newNameToPos[newName];
132                     continue;
133                 }
135                 //- add a new patch
136                 newNameToPos.insert(std::pair<word, label>(newName, newPatchI));
137                 newPatchNames[newPatchI] = newName;
138                 if( pDict.found("type") )
139                 {
140                     const word newType(pDict.lookup("type"));
141                     newPatchTypes[newPatchI] = newType;
142                 }
143                 else
144                 {
145                     newPatchTypes[newPatchI] = "wall";
146                 }
148                 patchToNew[matchedPatches[matchI]] = newPatchI;
149                 ++newPatchI;
150             }
151         }
152     }
154     word defaultName("walls");
155     if( dict.found("defaultName") )
156         defaultName = word(dict.lookup("defaultName"));
157     word defaultType("wall");
158     if( dict.found("defaultType") )
159         defaultType = word(dict.lookup("defaultType"));
161     if( dict.found("defaultName") && (newPatchI < patchToNew.size()) )
162     {
163         bool addPatch(false);
164         forAll(patchToNew, patchI)
165             if( patchToNew[patchI] == -1 )
166             {
167                 addPatch = true;
168                 break;
169             }
171         if( addPatch )
172         {
173             newNameToPos.insert(std::pair<word, label>(defaultName, newPatchI));
174             newPatchNames[newPatchI] = defaultName;
175             newPatchTypes[newPatchI] = defaultType;
176             ++newPatchI;
177         }
178     }
179     else
180     {
181         forAll(patchToNew, patchI)
182         {
183             if( patchToNew[patchI] != -1 )
184                 continue;
186             patchToNew[patchI] = newPatchI;
187             newPatchNames[newPatchI] = mesh_.boundaries()[patchI].patchName();
188             newPatchTypes[newPatchI] = mesh_.boundaries()[patchI].patchType();
189             ++newPatchI;
190         }
191     }
193     if( newPatchI == 0 )
194         return;
196     newPatchNames.setSize(newPatchI);
197     newPatchTypes.setSize(newPatchI);
199     //- start creating new boundary
200     VRWGraph newBoundaryFaces;
201     labelLongList newBoundaryOwners;
202     labelLongList newBoundaryPatches;
204     const PtrList<boundaryPatch>& boundaries = mesh_.boundaries();
205     const faceListPMG& faces = mesh_.faces();
206     const labelList& owner = mesh_.owner();
207     forAll(boundaries, patchI)
208     {
209         const boundaryPatch& wp = boundaries[patchI];
210         const label start = wp.patchStart();
211         const label end = start + wp.patchSize();
213         if( patchToNew[patchI] == -1 )
214         {
215             //- this patch is moved to the default patch
216             for(label faceI=start;faceI<end;++faceI)
217             {
218                 newBoundaryFaces.appendList(faces[faceI]);
219                 newBoundaryPatches.append(newPatchI-1);
220                 newBoundaryOwners.append(owner[faceI]);
221             }
222         }
223         else
224         {
225             //- this patch is renamed
226             for(label faceI=start;faceI<end;++faceI)
227             {
228                 newBoundaryFaces.appendList(faces[faceI]);
229                 newBoundaryPatches.append(patchToNew[patchI]);
230                 newBoundaryOwners.append(owner[faceI]);
231             }
232         }
233     }
235     //- execute the modifier
236     polyMeshGenModifier meshModifier(mesh_);
237     meshModifier.replaceBoundary
238     (
239         newPatchNames,
240         newBoundaryFaces,
241         newBoundaryOwners,
242         newBoundaryPatches
243     );
244     forAll(meshModifier.boundariesAccess(), patchI)
245         meshModifier.boundariesAccess()[patchI].patchType() =
246             newPatchTypes[patchI];
248     Info << "Finished renaming boundary patches" << endl;
251 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
253 renameBoundaryPatches::renameBoundaryPatches
255     polyMeshGen& mesh,
256     const IOdictionary& meshDict
259     mesh_(mesh),
260     meshDict_(meshDict)
262     if( meshDict.found("renameBoundary") )
263         calculateNewBoundary();
266 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
268 renameBoundaryPatches::~renameBoundaryPatches()
271 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
273 } // End namespace Foam
275 // ************************************************************************* //