collapseEdges.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  collapseEdges
26 
27 Description
28  Collapses short edges and combines edges that are in line.
29 
30  - collapse short edges. Length of edges to collapse provided as argument.
31  - merge two edges if they are in line. Maximum angle provided as argument.
32  - remove unused points.
33  - collapse faces:
34  - with small areas to a single point
35  - that have a high aspect ratio (i.e. sliver face) to a single edge
36 
37  Optionally checks the resulting mesh for bad faces and reduces the desired
38  face length factor for those faces attached to the bad faces.
39 
40  When collapsing an edge with one point on the boundary it will leave
41  the boundary point intact. When both points inside it chooses random. When
42  both points on boundary random again.
43 
44 Usage
45  - collapseEdges [OPTION]
46 
47 \*---------------------------------------------------------------------------*/
48 
49 #include "argList.H"
50 #include "Time.H"
51 #include "timeSelector.H"
52 #include "polyTopoChange.H"
53 #include "fvMesh.H"
54 #include "polyMeshFilter.H"
55 #include "faceSet.H"
56 #include "systemDict.H"
57 
58 using namespace Foam;
59 
60 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 
62 int main(int argc, char *argv[])
63 {
64  timeSelector::addOptions(true, false);
66  (
67  "Collapses small edges to a point.\n"
68  "Optionally collapse small faces to a point and thin faces to an edge."
69  );
70 
72  (
73  "collapseFaces",
74  "Collapse small and sliver faces as well as small edges"
75  );
76 
78  (
79  "collapseFaceSet",
80  "faceSet",
81  "Collapse faces that are in the supplied face set"
82  );
83 
84  #include "addDictOption.H"
85  #include "addOverwriteOption.H"
86  #include "setRootCase.H"
89 
90  #include "createMeshNoChangers.H"
91 
92  const word oldInstance = mesh.pointsInstance();
93 
94  const dictionary collapseDict(systemDict("collapseDict", args, mesh));
95 
96  const bool overwrite = args.optionFound("overwrite");
97 
98  const bool collapseFaces = args.optionFound("collapseFaces");
99  const bool collapseFaceSet = args.optionFound("collapseFaceSet");
100 
101  if (collapseFaces && collapseFaceSet)
102  {
104  << "Both face zone collapsing and face collapsing have been"
105  << "selected. Choose only one of:" << nl
106  << " -collapseFaces" << nl
107  << " -collapseFaceSet <faceSet>"
108  << abort(FatalError);
109  }
110 
111 
112  // maintain indirectPatchFaces if it is there (default) or force
113  // (if collapseFaceSet option provided)
114  word faceSetName("indirectPatchFaces");
116 
117  if (args.optionReadIfPresent("collapseFaceSet", faceSetName))
118  {
119  readFlag = IOobject::MUST_READ;
120  }
121 
122 
123 
124  labelIOList pointPriority
125  (
126  IOobject
127  (
128  "pointPriority",
129  runTime.name(),
130  runTime,
133  ),
134  labelList(mesh.nPoints(), labelMin)
135  );
136  forAll(timeDirs, timeI)
137  {
138  runTime.setTime(timeDirs[timeI], timeI);
139 
140  Info<< "Time = " << runTime.userTimeName() << endl;
141 
142  autoPtr<polyMeshFilter> meshFilterPtr;
143 
144  label nBadFaces = 0;
145 
146  faceSet indirectPatchFaces
147  (
148  mesh,
149  faceSetName,
150  readFlag,
152  );
153  Info<< "Read faceSet " << indirectPatchFaces.name()
154  << " with "
155  << returnReduce(indirectPatchFaces.size(), sumOp<label>())
156  << " faces" << endl;
157 
158 
159  {
160  meshFilterPtr.set
161  (
162  new polyMeshFilter(mesh, pointPriority, collapseDict)
163  );
164  polyMeshFilter& meshFilter = meshFilterPtr();
165 
166  // newMesh will be empty until it is filtered
167  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
168 
169  // Filter small edges only. This reduces the number of faces so that
170  // the face filtering is sped up.
171  nBadFaces = meshFilter.filterEdges(0);
172  {
173  polyTopoChange meshMod(newMesh());
174 
175  meshMod.changeMesh(mesh, false);
176 
177  polyMeshFilter::copySets(newMesh(), mesh);
178  }
179 
180  pointPriority = meshFilter.pointPriority();
181  }
182 
183  if (collapseFaceSet)
184  {
185  meshFilterPtr.reset
186  (
187  new polyMeshFilter(mesh, pointPriority, collapseDict)
188  );
189  polyMeshFilter& meshFilter = meshFilterPtr();
190 
191  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
192 
193  // Filter faces. Pass in the number of bad faces that are present
194  // from the previous edge filtering to use as a stopping criterion.
195  meshFilter.filter(indirectPatchFaces);
196  {
197  polyTopoChange meshMod(newMesh);
198 
199  meshMod.changeMesh(mesh, false);
200 
201  polyMeshFilter::copySets(newMesh(), mesh);
202  }
203 
204  pointPriority = meshFilter.pointPriority();
205  }
206 
207  if (collapseFaces)
208  {
209  meshFilterPtr.reset
210  (
211  new polyMeshFilter(mesh, pointPriority, collapseDict)
212  );
213  polyMeshFilter& meshFilter = meshFilterPtr();
214 
215  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
216 
217  // Filter faces. Pass in the number of bad faces that are present
218  // from the previous edge filtering to use as a stopping criterion.
219  meshFilter.filter(nBadFaces);
220  {
221  polyTopoChange meshMod(newMesh);
222 
223  meshMod.changeMesh(mesh, false);
224 
225  polyMeshFilter::copySets(newMesh(), mesh);
226  }
227 
228  pointPriority = meshFilter.pointPriority();
229  }
230 
231  // Write resulting mesh
232  if (!overwrite)
233  {
234  runTime++;
235  }
236  else
237  {
238  mesh.setInstance(oldInstance);
239  }
240 
241  Info<< nl << "Writing collapsed mesh to time "
242  << runTime.name() << nl << endl;
243 
244  mesh.write();
245  pointPriority.write();
246  }
247 
248  Info<< nl << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
249  << " ClockTime = " << runTime.elapsedClockTime() << " s"
250  << nl << endl;
251 
252  Info<< "End\n" << endl;
253 
254  return 0;
255 }
256 
257 
258 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
readOption
Enumeration defining the read options.
Definition: IOobject.H:117
virtual Ostream & write(const char)=0
Write character.
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:159
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
A list of face labels.
Definition: faceSet.H:51
Remove the edges and faces of a polyMesh whilst satisfying the given mesh quality criteria.
const autoPtr< labelList > & pointPriority() const
Return the new pointPriority list.
label filter(const label nOriginalBadFaces)
Filter edges and faces.
const autoPtr< fvMesh > & filteredMesh() const
Return reference to the filtered mesh. Does not check if the.
Direct mesh changes based on v1.3 polyTopoChange syntax.
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:283
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int main(int argc, char *argv[])
Definition: financialFoam.C:44
static instantList timeDirs
Definition: globalFoam.H:44
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
IOdictionary systemDict(const word &dictName, const argList &args, const objectRegistry &ob, const word &regionName=polyMesh::defaultRegion)
Definition: systemDict.C:92
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
error FatalError
static const char nl
Definition: Ostream.H:260
static const label labelMin
Definition: label.H:61
Foam::argList args(argc, argv)