autoPatch.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM 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 OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  autoPatch
26 
27 Description
28  Divides external faces into patches based on (user supplied) feature
29  angle.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #include "argList.H"
34 #include "polyMesh.H"
35 #include "Time.H"
36 #include "boundaryMesh.H"
37 #include "repatchPolyTopoChanger.H"
38 #include "unitConversion.H"
39 #include "OFstream.H"
40 #include "ListOps.H"
41 
42 using namespace Foam;
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 // Get all feature edges.
47 void collectFeatureEdges(const boundaryMesh& bMesh, labelList& markedEdges)
48 {
49  markedEdges.setSize(bMesh.mesh().nEdges());
50 
51  label markedI = 0;
52 
53  forAll(bMesh.featureSegments(), i)
54  {
55  const labelList& segment = bMesh.featureSegments()[i];
56 
57  forAll(segment, j)
58  {
59  label featEdgeI = segment[j];
60 
61  label meshEdgeI = bMesh.featureToEdge()[featEdgeI];
62 
63  markedEdges[markedI++] = meshEdgeI;
64  }
65  }
66  markedEdges.setSize(markedI);
67 }
68 
69 
70 
71 int main(int argc, char *argv[])
72 {
73  #include "addOverwriteOption.H"
75  argList::validArgs.append("feature angle[0-180]");
76 
77  #include "setRootCase.H"
78  #include "createTime.H"
79  runTime.functionObjects().off();
80  #include "createPolyMesh.H"
81  const word oldInstance = mesh.pointsInstance();
82 
83  Info<< "Mesh read in = "
84  << runTime.cpuTimeIncrement()
85  << " s\n" << endl << endl;
86 
87 
88  const scalar featureAngle = args.argRead<scalar>(1);
89  const bool overwrite = args.optionFound("overwrite");
90 
91  const scalar minCos = Foam::cos(degToRad(featureAngle));
92 
93  Info<< "Feature:" << featureAngle << endl
94  << "minCos :" << minCos << endl
95  << endl;
96 
97  //
98  // Use boundaryMesh to reuse all the featureEdge stuff in there.
99  //
100 
102  bMesh.read(mesh);
103 
104  // Set feature angle (calculate feature edges)
105  bMesh.setFeatureEdges(minCos);
106 
107  // Collect all feature edges as edge labels
108  labelList markedEdges;
109 
110  collectFeatureEdges(bMesh, markedEdges);
111 
112 
113 
114  // (new) patch ID for every face in mesh.
115  labelList patchIDs(bMesh.mesh().size(), -1);
116 
117  //
118  // Fill patchIDs with values for every face by floodfilling without
119  // crossing feature edge.
120  //
121 
122  // Current patch number.
123  label newPatchI = bMesh.patches().size();
124 
125  label suffix = 0;
126 
127  while (true)
128  {
129  // Find first unset face.
130  label unsetFaceI = findIndex(patchIDs, -1);
131 
132  if (unsetFaceI == -1)
133  {
134  // All faces have patchID set. Exit.
135  break;
136  }
137 
138  // Found unset face. Create patch for it.
139  word patchName;
140  do
141  {
142  patchName = "auto" + name(suffix++);
143  }
144  while (bMesh.findPatchID(patchName) != -1);
145 
146  bMesh.addPatch(patchName);
147 
148  bMesh.changePatchType(patchName, "patch");
149 
150 
151  // Fill visited with all faces reachable from unsetFaceI.
152  boolList visited(bMesh.mesh().size());
153 
154  bMesh.markFaces(markedEdges, unsetFaceI, visited);
155 
156 
157  // Assign all visited faces to current patch
158  label nVisited = 0;
159 
160  forAll(visited, faceI)
161  {
162  if (visited[faceI])
163  {
164  nVisited++;
165 
166  patchIDs[faceI] = newPatchI;
167  }
168  }
169 
170  Info<< "Assigned " << nVisited << " faces to patch " << patchName
171  << endl << endl;
172 
173  newPatchI++;
174  }
175 
176 
177 
178  const PtrList<boundaryPatch>& patches = bMesh.patches();
179 
180  // Create new list of patches with old ones first
181  List<polyPatch*> newPatchPtrList(patches.size());
182 
183  newPatchI = 0;
184 
185  // Copy old patches
186  forAll(mesh.boundaryMesh(), patchI)
187  {
188  const polyPatch& patch = mesh.boundaryMesh()[patchI];
189 
190  newPatchPtrList[newPatchI] =
191  patch.clone
192  (
193  mesh.boundaryMesh(),
194  newPatchI,
195  patch.size(),
196  patch.start()
197  ).ptr();
198 
199  newPatchI++;
200  }
201 
202  // Add new ones with empty size.
203  for (label patchI = newPatchI; patchI < patches.size(); patchI++)
204  {
205  const boundaryPatch& bp = patches[patchI];
206 
207  newPatchPtrList[newPatchI] = polyPatch::New
208  (
209  polyPatch::typeName,
210  bp.name(),
211  0,
212  mesh.nFaces(),
213  newPatchI,
215  ).ptr();
216 
217  newPatchI++;
218  }
219 
220  if (!overwrite)
221  {
222  runTime++;
223  }
224 
225 
226  // Change patches
227  repatchPolyTopoChanger polyMeshRepatcher(mesh);
228  polyMeshRepatcher.changePatches(newPatchPtrList);
229 
230 
231  // Change face ordering
232 
233  // Since bMesh read from mesh there is one to one mapping so we don't
234  // have to do the geometric stuff.
235  const labelList& meshFace = bMesh.meshFace();
236 
237  forAll(patchIDs, faceI)
238  {
239  label meshFaceI = meshFace[faceI];
240 
241  polyMeshRepatcher.changePatchID(meshFaceI, patchIDs[faceI]);
242  }
243 
244  polyMeshRepatcher.repatch();
245 
246  // Write resulting mesh
247  if (overwrite)
248  {
249  mesh.setInstance(oldInstance);
250  }
251 
252  // Set the precision of the points data to 10
254 
255  mesh.write();
256 
257  Info<< "End\n" << endl;
258 
259  return 0;
260 }
261 
262 
263 // ************************************************************************* //
Foam::argList::validArgs
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:143
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::findIndex
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurence of given element and return index,.
addOverwriteOption.H
Foam::PrimitivePatch::nEdges
label nEdges() const
Return number of edges in patch.
Definition: PrimitivePatchTemplate.H:299
unitConversion.H
Unit conversion functions.
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
polyMesh.H
OFstream.H
Foam::boundaryPatch
Like polyPatch but without reference to mesh. patchIdentifier::index is not used. Used in boundaryMes...
Definition: boundaryPatch.H:50
Foam::fvMesh::write
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:873
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:772
Foam::repatchPolyTopoChanger
A mesh which allows changes in the patch distribution of the boundary faces. The change in patching i...
Definition: repatchPolyTopoChanger.H:51
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::Info
messageStream Info
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
Foam::bMesh
PrimitivePatch< face, List, const pointField > bMesh
Holder of faceList and points. (v.s. e.g. primitivePatch which references points)
Definition: bMesh.H:45
argList.H
main
int main(int argc, char *argv[])
Definition: postCalc.C:54
Foam::PtrList
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
Foam::polyPatch::New
static autoPtr< polyPatch > New(const word &patchType, const word &name, const label size, const label start, const label index, const polyBoundaryMesh &bm)
Return a pointer to a new patch created on freestore from.
Definition: polyPatchNew.C:32
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::polyPatch::start
label start() const
Return start label of this patch in the polyMesh face list.
Definition: polyPatch.H:312
Foam::polyPatch::clone
virtual autoPtr< polyPatch > clone(const polyBoundaryMesh &bm) const
Construct and return a clone, resetting the boundary mesh.
Definition: polyPatch.H:231
Foam::polyMesh::setInstance
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:32
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::List::setSize
void setSize(const label)
Reset size of List.
setRootCase.H
Foam::primitiveMesh::nFaces
label nFaces() const
Definition: primitiveMeshI.H:58
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: contiguous.H:49
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
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
createTime.H
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
boundaryMesh.H
Foam::boundaryMesh
Addressing for all faces on surface of mesh. Can either be read from polyMesh or from triSurface....
Definition: boundaryMesh.H:59
Foam::argList::argRead
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:177
patches
patches[0]
Definition: createSingleCellMesh.H:36
ListOps.H
Various functions to operate on Lists.
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
args
Foam::argList args(argc, argv)
repatchPolyTopoChanger.H
Foam::patchIdentifier::name
const word & name() const
Return name.
Definition: patchIdentifier.H:109
createPolyMesh.H
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatchTemplate.H:88
Foam::degToRad
scalar degToRad(const scalar deg)
Conversion from degrees to radians.
Definition: unitConversion.H:45
Foam::cos
dimensionedScalar cos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:256