autoPatch.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 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 "repatchMesh.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 repatchMesh& rMesh, labelList& markedEdges)
48 {
49  markedEdges.setSize(rMesh.mesh().nEdges());
50 
51  label markedI = 0;
52 
53  forAll(rMesh.featureSegments(), i)
54  {
55  const labelList& segment = rMesh.featureSegments()[i];
56 
57  forAll(segment, j)
58  {
59  label featEdgeI = segment[j];
60 
61  label meshEdgeI = rMesh.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"
79  #include "createPolyMesh.H"
80  const word oldInstance = mesh.pointsInstance();
81 
82  Info<< "Mesh read in = "
83  << runTime.cpuTimeIncrement()
84  << " s\n" << endl << endl;
85 
86 
87  const scalar featureAngle = args.argRead<scalar>(1);
88  const bool overwrite = args.optionFound("overwrite");
89 
90  const scalar minCos = Foam::cos(degToRad(featureAngle));
91 
92  Info<< "Feature:" << featureAngle << endl
93  << "minCos :" << minCos << endl
94  << endl;
95 
96  //
97  // Use repatchMesh to reuse all the featureEdge stuff in there.
98  //
99 
100  repatchMesh rMesh;
101  rMesh.read(mesh);
102 
103  // Set feature angle (calculate feature edges)
104  rMesh.setFeatureEdges(minCos);
105 
106  // Collect all feature edges as edge labels
107  labelList markedEdges;
108 
109  collectFeatureEdges(rMesh, markedEdges);
110 
111 
112 
113  // (new) patch ID for every face in mesh.
114  labelList patchIDs(rMesh.mesh().size(), -1);
115 
116  //
117  // Fill patchIDs with values for every face by floodfilling without
118  // crossing feature edge.
119  //
120 
121  // Current patch number.
122  label newPatchi = rMesh.patches().size();
123 
124  label suffix = 0;
125 
126  while (true)
127  {
128  // Find first unset face.
129  label unsetFacei = findIndex(patchIDs, -1);
130 
131  if (unsetFacei == -1)
132  {
133  // All faces have patchID set. Exit.
134  break;
135  }
136 
137  // Found unset face. Create patch for it.
138  word patchName;
139  do
140  {
141  patchName = "auto" + name(suffix++);
142  }
143  while (rMesh.findPatchID(patchName) != -1);
144 
145  rMesh.addPatch(patchName);
146 
147  rMesh.changePatchType(patchName, "patch");
148 
149 
150  // Fill visited with all faces reachable from unsetFacei.
151  boolList visited(rMesh.mesh().size());
152 
153  rMesh.markFaces(markedEdges, unsetFacei, visited);
154 
155 
156  // Assign all visited faces to current patch
157  label nVisited = 0;
158 
159  forAll(visited, facei)
160  {
161  if (visited[facei])
162  {
163  nVisited++;
164 
165  patchIDs[facei] = newPatchi;
166  }
167  }
168 
169  Info<< "Assigned " << nVisited << " faces to patch " << patchName
170  << endl << endl;
171 
172  newPatchi++;
173  }
174 
175 
176 
177  const PtrList<repatchPatch>& patches = rMesh.patches();
178 
179  // Create new list of patches with old ones first
180  List<polyPatch*> newPatchPtrList(patches.size());
181 
182  newPatchi = 0;
183 
184  // Copy old patches
185  forAll(mesh.boundaryMesh(), patchi)
186  {
187  const polyPatch& patch = mesh.boundaryMesh()[patchi];
188 
189  newPatchPtrList[newPatchi] =
190  patch.clone
191  (
192  mesh.boundaryMesh(),
193  newPatchi,
194  patch.size(),
195  patch.start()
196  ).ptr();
197 
198  newPatchi++;
199  }
200 
201  // Add new ones with empty size.
202  for (label patchi = newPatchi; patchi < patches.size(); patchi++)
203  {
204  const repatchPatch& bp = patches[patchi];
205 
206  newPatchPtrList[newPatchi] = polyPatch::New
207  (
208  polyPatch::typeName,
209  bp.name(),
210  0,
211  mesh.nFaces(),
212  newPatchi,
213  mesh.boundaryMesh()
214  ).ptr();
215 
216  newPatchi++;
217  }
218 
219  if (!overwrite)
220  {
221  runTime++;
222  }
223 
224 
225  // Change patches
226  repatchPolyTopoChanger polyMeshRepatcher(mesh);
227  polyMeshRepatcher.changePatches(newPatchPtrList);
228 
229 
230  // Change face ordering
231 
232  // Since rMesh read from mesh there is one to one mapping so we don't
233  // have to do the geometric stuff.
234  const labelList& meshFace = rMesh.meshFace();
235 
236  forAll(patchIDs, facei)
237  {
238  label meshFacei = meshFace[facei];
239 
240  polyMeshRepatcher.changePatchID(meshFacei, patchIDs[facei]);
241  }
242 
243  polyMeshRepatcher.repatch();
244 
245  // Write resulting mesh
246  if (overwrite)
247  {
248  mesh.setInstance(oldInstance);
249  }
250 
251  // Set the precision of the points data to 10
253 
254  mesh.write();
255 
256  Info<< "End\n" << endl;
257 
258  return 0;
259 }
260 
261 
262 // ************************************************************************* //
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:458
void setSize(const label)
Reset size of List.
Definition: List.C:281
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:65
label nEdges() const
Return number of edges in patch.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
static void noParallel()
Remove the parallel options.
Definition: argList.C:175
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:183
const word & name() const
Return name.
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:70
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
label start() const
Return start label of this patch in the polyMesh face list.
Definition: polyPatch.H:280
const polyBoundaryMesh & boundaryMesh() const
Return boundaryMesh reference.
Definition: polyPatch.C:270
virtual autoPtr< polyPatch > clone(const polyBoundaryMesh &bm) const
Construct and return a clone, resetting the boundary mesh.
Definition: polyPatch.H:215
Addressing for all faces on surface of mesh. Can either be read from polyMesh or from triSurface....
Definition: repatchMesh.H:58
void addPatch(const word &patchName)
Add to back of patch list.
Definition: repatchMesh.C:1141
void setFeatureEdges(const scalar minCos)
Set featureEdges, edgeToFeature, featureSegments according.
Definition: repatchMesh.C:905
const rMesh & mesh() const
Access the boundary mesh.
Definition: repatchMesh.H:180
const labelList & featureToEdge() const
From index into featureEdge to index into meshedges,.
Definition: repatchMesh.H:216
const labelList & meshFace() const
Label of original face in polyMesh (before patchify(...))
Definition: repatchMesh.H:198
label findPatchID(const word &patchName) const
Get index of patch by name.
Definition: repatchMesh.C:1127
void markFaces(const labelList &protectedEdges, const label facei, boolList &visited) const
Definition: repatchMesh.C:1285
const labelListList & featureSegments() const
Lists of connected featureEdges. Indices into featureEdges.
Definition: repatchMesh.H:228
void read(const polyMesh &)
Read from repatchMesh of polyMesh.
Definition: repatchMesh.C:348
void changePatchType(const word &patchName, const word &type)
Change patch.
Definition: repatchMesh.C:1234
const PtrList< repatchPatch > & patches() const
Access the patches.
Definition: repatchMesh.H:192
Like polyPatch but without reference to mesh. patchIdentifier::index is not used. Used in repatchMesh...
Definition: repatchPatch.H:60
A mesh which allows changes in the patch distribution of the boundary faces. The change in patching i...
A class for handling words, derived from string.
Definition: word.H:62
int main(int argc, char *argv[])
Definition: financialFoam.C:44
label patchi
const fvPatchList & patches
Namespace for OpenFOAM.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
dimensionedScalar cos(const dimensionedScalar &ds)
scalar degToRad(const scalar deg)
Conversion from degrees to radians.
Foam::argList args(argc, argv)
Unit conversion functions.