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-2016 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
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 // ************************************************************************* //
void addPatch(const word &patchName)
Add to back of patch list.
const labelList & featureToEdge() const
From index into featureEdge to index into meshedges,.
Definition: boundaryMesh.H:238
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
void read(const polyMesh &)
Read from boundaryMesh of polyMesh.
Definition: boundaryMesh.C:472
void setFeatureEdges(const scalar minCos)
Set featureEdges, edgeToFeature, featureSegments according.
Like polyPatch but without reference to mesh. patchIdentifier::index is not used. Used in boundaryMes...
Definition: boundaryPatch.H:57
virtual autoPtr< polyPatch > clone(const polyBoundaryMesh &bm) const
Construct and return a clone, resetting the boundary mesh.
Definition: polyPatch.H:219
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
void markFaces(const labelList &protectedEdges, const label facei, boolList &visited) const
Unit conversion functions.
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
Addressing for all faces on surface of mesh. Can either be read from polyMesh or from triSurface...
Definition: boundaryMesh.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
scalar degToRad(const scalar deg)
Conversion from degrees to radians.
void changePatchType(const word &patchName, const word &type)
Change patch.
Various functions to operate on Lists.
dynamicFvMesh & mesh
dimensionedScalar cos(const dimensionedScalar &ds)
A mesh which allows changes in the patch distribution of the boundary faces. The change in patching i...
label start() const
Return start label of this patch in the polyMesh face list.
Definition: polyPatch.H:300
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
A class for handling words, derived from string.
Definition: word.H:59
const labelList & meshFace() const
Label of original face in polyMesh (before patchify(...))
Definition: boundaryMesh.H:220
const labelListList & featureSegments() const
Lists of connected featureEdges. Indices into featureEdges.
Definition: boundaryMesh.H:250
const word & name() const
Return name.
const bMesh & mesh() const
Definition: boundaryMesh.H:202
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurence of given element and return index,.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:177
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:32
label nEdges() const
Return number of edges in patch.
label nFaces() const
void setSize(const label)
Reset size of List.
Definition: List.C:295
label patchi
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:62
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:772
messageStream Info
const PtrList< boundaryPatch > & patches() const
Definition: boundaryMesh.H:213
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
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
Foam::argList args(argc, argv)
PrimitivePatch< face, List, const pointField > bMesh
Holder of faceList and points. (v.s. e.g. primitivePatch which references points) ...
Definition: bMesh.H:45
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:870
Namespace for OpenFOAM.
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29