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-2024 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 "repatcher.H"
38 #include "OFstream.H"
39 #include "ListOps.H"
40 
41 using namespace Foam;
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 // Get all feature edges.
46 void collectFeatureEdges(const repatchMesh& rMesh, labelList& markedEdges)
47 {
48  markedEdges.setSize(rMesh.mesh().nEdges());
49 
50  label markedI = 0;
51 
52  forAll(rMesh.featureSegments(), i)
53  {
54  const labelList& segment = rMesh.featureSegments()[i];
55 
56  forAll(segment, j)
57  {
58  label featEdgeI = segment[j];
59 
60  label meshEdgeI = rMesh.featureToEdge()[featEdgeI];
61 
62  markedEdges[markedI++] = meshEdgeI;
63  }
64  }
65  markedEdges.setSize(markedI);
66 }
67 
68 
69 
70 int main(int argc, char *argv[])
71 {
72  #include "addOverwriteOption.H"
74  argList::validArgs.append("feature angle[0-180]");
75 
76  #include "setRootCase.H"
78  #include "createPolyMesh.H"
79  const word oldInstance = mesh.pointsInstance();
80 
81  Info<< "Mesh read in = "
82  << runTime.cpuTimeIncrement()
83  << " s\n" << endl << endl;
84 
85 
86  const scalar featureAngle = degToRad(args.argRead<scalar>(1));
87  const bool overwrite = args.optionFound("overwrite");
88 
89  const scalar minCos = Foam::cos(featureAngle);
90 
91  Info<< "Feature:" << radToDeg(featureAngle) << endl
92  << "minCos :" << minCos << endl
93  << endl;
94 
95  //
96  // Use repatchMesh to reuse all the featureEdge stuff in there.
97  //
98 
99  repatchMesh rMesh;
100  rMesh.read(mesh);
101 
102  // Set feature angle (calculate feature edges)
103  rMesh.setFeatureEdges(minCos);
104 
105  // Collect all feature edges as edge labels
106  labelList markedEdges;
107 
108  collectFeatureEdges(rMesh, markedEdges);
109 
110 
111 
112  // (new) patch ID for every face in mesh.
113  labelList patchIDs(rMesh.mesh().size(), -1);
114 
115  //
116  // Fill patchIDs with values for every face by floodfilling without
117  // crossing feature edge.
118  //
119 
120  // Current patch number.
121  label newPatchi = rMesh.patches().size();
122 
123  label suffix = 0;
124 
125  while (true)
126  {
127  // Find first unset face.
128  label unsetFacei = findIndex(patchIDs, -1);
129 
130  if (unsetFacei == -1)
131  {
132  // All faces have patchID set. Exit.
133  break;
134  }
135 
136  // Found unset face. Create patch for it.
137  word patchName;
138  do
139  {
140  patchName = "auto" + name(suffix++);
141  }
142  while (rMesh.findIndex(patchName) != -1);
143 
144  rMesh.addPatch(patchName);
145 
146  rMesh.changePatchType(patchName, "patch");
147 
148 
149  // Fill visited with all faces reachable from unsetFacei.
150  boolList visited(rMesh.mesh().size());
151 
152  rMesh.markFaces(markedEdges, unsetFacei, visited);
153 
154 
155  // Assign all visited faces to current patch
156  label nVisited = 0;
157 
158  forAll(visited, facei)
159  {
160  if (visited[facei])
161  {
162  nVisited++;
163 
164  patchIDs[facei] = newPatchi;
165  }
166  }
167 
168  Info<< "Assigned " << nVisited << " faces to patch " << patchName
169  << endl << endl;
170 
171  newPatchi++;
172  }
173 
174 
175 
176  const PtrList<repatchPatch>& patches = rMesh.patches();
177 
178  // Create new list of patches with old ones first
179  List<polyPatch*> newPatchPtrList(patches.size());
180 
181  newPatchi = 0;
182 
183  // Copy old patches
184  forAll(mesh.boundaryMesh(), patchi)
185  {
186  const polyPatch& patch = mesh.boundaryMesh()[patchi];
187 
188  newPatchPtrList[newPatchi] =
189  patch.clone
190  (
191  mesh.boundaryMesh(),
192  newPatchi,
193  patch.size(),
194  patch.start()
195  ).ptr();
196 
197  newPatchi++;
198  }
199 
200  // Add new ones with empty size.
201  for (label patchi = newPatchi; patchi < patches.size(); patchi++)
202  {
203  const repatchPatch& bp = patches[patchi];
204 
205  newPatchPtrList[newPatchi] = polyPatch::New
206  (
207  polyPatch::typeName,
208  bp.name(),
209  0,
210  mesh.nFaces(),
211  newPatchi,
212  mesh.boundaryMesh()
213  ).ptr();
214 
215  newPatchi++;
216  }
217 
218  if (!overwrite)
219  {
220  runTime++;
221  }
222 
223 
224  // Change patches
225  repatcher polyMeshRepatcher(mesh);
226  polyMeshRepatcher.changePatches(newPatchPtrList);
227 
228 
229  // Change face ordering
230 
231  // Since rMesh read from mesh there is one to one mapping so we don't
232  // have to do the geometric stuff.
233  const labelList& meshFace = rMesh.meshFace();
234 
235  forAll(patchIDs, facei)
236  {
237  label meshFacei = meshFace[facei];
238 
239  polyMeshRepatcher.changePatchID(meshFacei, patchIDs[facei]);
240  }
241 
242  polyMeshRepatcher.repatch();
243 
244  // Write resulting mesh
245  if (overwrite)
246  {
247  mesh.setInstance(oldInstance);
248  }
249 
250  // Set the precision of the points data to 10
252 
253  mesh.write();
254 
255  Info<< "End\n" << endl;
256 
257  return 0;
258 }
259 
260 
261 // ************************************************************************* //
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 UList.
Definition: UListI.H:311
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
label findIndex(const word &patchName) const
Get index of patch by name.
Definition: repatchMesh.C:1127
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
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...
Definition: repatcher.H:53
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.
scalar radToDeg(const scalar rad)
Convert radians to degrees.
scalar degToRad(const scalar deg)
Convert degrees to radians.
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:257
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)
Foam::argList args(argc, argv)