renameBoundaryPatches.C
Go to the documentation of this file.
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.
10 
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.
15 
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.
20 
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/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "renameBoundaryPatches.H"
29 #include "demandDrivenData.H"
30 #include "IOdictionary.H"
31 
32 #include <map>
33 
34 // #define DEBUGSearch
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
44 {
45  Info << "Renaming boundary patches" << endl;
46 
47  const dictionary& dict = meshDict_.subDict("renameBoundary");
48 
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  }
61 
62  labelList patchToNew(mesh_.boundaries().size(), -1);
63 
64  wordList newPatchNames(patchToNew.size());
65  wordList newPatchTypes(patchToNew.size());
66  std::map<word, label> newNameToPos;
67  label newPatchI(0);
68 
69  //- read new patch names and types
70  if( dict.found("newPatchNames") )
71  {
72  PtrList<entry> patchesToRename;
73 
74  if( dict.isDict("newPatchNames") )
75  {
76  const dictionary& newPatchNames = dict.subDict("newPatchNames");
77  const wordList keys = newPatchNames.toc();
78 
79  patchesToRename.setSize(keys.size());
80 
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  }
98 
99  forAll(patchesToRename, patchI)
100  {
101  const word patchName = patchesToRename[patchI].keyword();
102 
103  const labelList matchedPatches = mesh_.findPatches(patchName);
104 
105  if(matchedPatches.empty())
106  {
107  Warning << "No matches for " << patchName << " found!!" << endl;
108  continue;
109  }
110 
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  }
118 
119  const dictionary pDict = patchesToRename[patchI].dict();
120 
121  forAll(matchedPatches, matchI)
122  {
123  word newName(mesh_.getPatchName(matchedPatches[matchI]));
124 
125  if( pDict.found("newName") )
126  newName = word(pDict.lookup("newName"));
127 
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  }
134 
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  }
147 
148  patchToNew[matchedPatches[matchI]] = newPatchI;
149  ++newPatchI;
150  }
151  }
152  }
153 
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"));
160 
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  }
170 
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;
185 
186  patchToNew[patchI] = newPatchI;
187  newPatchNames[newPatchI] = mesh_.boundaries()[patchI].patchName();
188  newPatchTypes[newPatchI] = mesh_.boundaries()[patchI].patchType();
189  ++newPatchI;
190  }
191  }
192 
193  if( newPatchI == 0 )
194  return;
195 
196  newPatchNames.setSize(newPatchI);
197  newPatchTypes.setSize(newPatchI);
198 
199  //- start creating new boundary
200  VRWGraph newBoundaryFaces;
201  labelLongList newBoundaryOwners;
202  labelLongList newBoundaryPatches;
203 
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();
212 
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  }
234 
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];
247 
248  Info << "Finished renaming boundary patches" << endl;
249 }
250 
251 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
252 
254 (
255  polyMeshGen& mesh,
256  const IOdictionary& meshDict
257 )
258 :
259  mesh_(mesh),
260  meshDict_(meshDict)
261 {
262  if( meshDict.found("renameBoundary") )
263  calculateNewBoundary();
264 }
265 
266 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
267 
269 {}
270 
271 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
272 
273 } // End namespace Foam
274 
275 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
Foam::polyMeshGenFaces::owner
const labelList & owner() const
owner and neighbour cells for faces
Definition: polyMeshGenFacesI.H:67
Foam::dictionary::lookupEntry
const entry & lookupEntry(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream if present otherwise error.
Definition: dictionary.C:426
Foam::LongList::append
void append(const T &e)
Append an element at the end of the list.
Definition: LongListI.H:265
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::polyMeshGenFaces::findPatches
labelList findPatches(const word &patchName) const
return a list of patch indices corresponding to the given
Definition: polyMeshGenFaces.C:247
Foam::renameBoundaryPatches::~renameBoundaryPatches
~renameBoundaryPatches()
Definition: renameBoundaryPatches.C:268
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
renameBoundaryPatches.H
Foam::Warning
messageStream Warning
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::polyMeshGenModifier::replaceBoundary
void replaceBoundary(const wordList &patchNames, const VRWGraph &boundaryFaces, const labelLongList &faceOwners, const labelLongList &facePatches)
replace the boundary with new boundary faces
Definition: polyMeshGenModifierReplaceBoundary.C:42
Foam::polyMeshGen
Definition: polyMeshGen.H:46
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::polyMeshGenFaces::faces
const faceListPMG & faces() const
access to faces
Definition: polyMeshGenFacesI.H:43
Foam::boundaryPatch
Like polyPatch but without reference to mesh. patchIdentifier::index is not used. Used in boundaryMes...
Definition: boundaryPatch.H:50
Foam::polyMeshGenModifier::boundariesAccess
PtrList< boundaryPatch > & boundariesAccess()
access to boundary data
Definition: polyMeshGenModifier.H:131
Foam::LongList< label >
Foam::PtrList::set
bool set(const label) const
Is element set.
Foam::renameBoundaryPatches::mesh_
polyMeshGen & mesh_
mesh
Definition: renameBoundaryPatches.H:55
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::Info
messageStream Info
Foam::renameBoundaryPatches::renameBoundaryPatches
renameBoundaryPatches(const renameBoundaryPatches &)
Disallow default bitwise copy construct.
Foam::PtrList< entry >
Foam::polyMeshGenFaces::getPatchName
word getPatchName(const label patchID) const
return the name of a patch given its ID
Definition: polyMeshGenFaces.C:233
Foam::PtrList::transfer
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:200
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::renameBoundaryPatches::calculateNewBoundary
void calculateNewBoundary()
calculate new boundary and replace the existing one
Definition: renameBoundaryPatches.C:43
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::renameBoundaryPatches::meshDict_
const IOdictionary & meshDict_
dictionary containing relevant information
Definition: renameBoundaryPatches.H:58
Foam::polyMeshGenModifier
Definition: polyMeshGenModifier.H:52
IOdictionary.H
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::VRWGraph::appendList
void appendList(const ListType &l)
Append a list as a row at the end of the graph.
Definition: VRWGraphI.H:286
Foam::polyMeshGenFaces::boundaries
const PtrList< boundaryPatch > & boundaries() const
ordinary boundaries
Definition: polyMeshGenFacesI.H:111
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::PtrList::setSize
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:142
Foam::entry::clone
virtual autoPtr< entry > clone(const dictionary &parentDict) const =0
Construct on freestore as copy with reference to the.
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:697
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
Foam::faceListPMG
Definition: faceListPMG.H:50
Foam::VRWGraph
Definition: VRWGraph.H:101