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-2025 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 "addNoOverwriteOption.H"
86  #include "addMeshOption.H"
87  #include "addRegionOption.H"
88  #include "setRootCase.H"
91 
93 
94  const word oldInstance = mesh.pointsInstance();
95 
96  const dictionary collapseDict(systemDict("collapseDict", args, mesh));
97 
98  #include "setNoOverwrite.H"
99 
100  const bool collapseFaces = args.optionFound("collapseFaces");
101  const bool collapseFaceSet = args.optionFound("collapseFaceSet");
102 
103  if (collapseFaces && collapseFaceSet)
104  {
106  << "Both face zone collapsing and face collapsing have been"
107  << "selected. Choose only one of:" << nl
108  << " -collapseFaces" << nl
109  << " -collapseFaceSet <faceSet>"
110  << abort(FatalError);
111  }
112 
113 
114  // maintain indirectPatchFaces if it is there (default) or force
115  // (if collapseFaceSet option provided)
116  word faceSetName("indirectPatchFaces");
118 
119  if (args.optionReadIfPresent("collapseFaceSet", faceSetName))
120  {
121  readFlag = IOobject::MUST_READ;
122  }
123 
124 
125 
126  labelIOList pointPriority
127  (
128  IOobject
129  (
130  "pointPriority",
131  runTime.name(),
132  runTime,
135  ),
137  );
138  forAll(timeDirs, timeI)
139  {
140  runTime.setTime(timeDirs[timeI], timeI);
141 
142  Info<< "Time = " << runTime.userTimeName() << endl;
143 
144  autoPtr<polyMeshFilter> meshFilterPtr;
145 
146  label nBadFaces = 0;
147 
148  faceSet indirectPatchFaces
149  (
150  mesh,
151  faceSetName,
152  readFlag,
154  );
155  Info<< "Read faceSet " << indirectPatchFaces.name()
156  << " with "
157  << returnReduce(indirectPatchFaces.size(), sumOp<label>())
158  << " faces" << endl;
159 
160 
161  {
162  meshFilterPtr.set
163  (
164  new polyMeshFilter(mesh, pointPriority, collapseDict)
165  );
166  polyMeshFilter& meshFilter = meshFilterPtr();
167 
168  // newMesh will be empty until it is filtered
169  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
170 
171  // Filter small edges only. This reduces the number of faces so that
172  // the face filtering is sped up.
173  nBadFaces = meshFilter.filterEdges(0);
174  {
175  polyTopoChange meshMod(newMesh());
176 
177  meshMod.changeMesh(mesh);
178 
179  polyMeshFilter::copySets(newMesh(), mesh);
180  }
181 
182  pointPriority = meshFilter.pointPriority();
183  }
184 
185  if (collapseFaceSet)
186  {
187  meshFilterPtr.reset
188  (
189  new polyMeshFilter(mesh, pointPriority, collapseDict)
190  );
191  polyMeshFilter& meshFilter = meshFilterPtr();
192 
193  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
194 
195  // Filter faces. Pass in the number of bad faces that are present
196  // from the previous edge filtering to use as a stopping criterion.
197  meshFilter.filter(indirectPatchFaces);
198  {
199  polyTopoChange meshMod(newMesh);
200 
201  meshMod.changeMesh(mesh);
202 
203  polyMeshFilter::copySets(newMesh(), mesh);
204  }
205 
206  pointPriority = meshFilter.pointPriority();
207  }
208 
209  if (collapseFaces)
210  {
211  meshFilterPtr.reset
212  (
213  new polyMeshFilter(mesh, pointPriority, collapseDict)
214  );
215  polyMeshFilter& meshFilter = meshFilterPtr();
216 
217  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
218 
219  // Filter faces. Pass in the number of bad faces that are present
220  // from the previous edge filtering to use as a stopping criterion.
221  meshFilter.filter(nBadFaces);
222  {
223  polyTopoChange meshMod(newMesh);
224 
225  meshMod.changeMesh(mesh);
226 
227  polyMeshFilter::copySets(newMesh(), mesh);
228  }
229 
230  pointPriority = meshFilter.pointPriority();
231  }
232 
233  // Write resulting mesh
234  if (!overwrite)
235  {
236  runTime++;
237  }
238  else
239  {
240  mesh.setInstance(oldInstance);
241  }
242 
243  Info<< nl << "Writing collapsed mesh to time "
244  << runTime.name() << nl << endl;
245 
246  mesh.write();
247  pointPriority.write();
248  }
249 
250  Info<< nl << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
251  << " ClockTime = " << runTime.elapsedClockTime() << " s"
252  << nl << endl;
253 
254  Info<< "End\n" << endl;
255 
256  return 0;
257 }
258 
259 
260 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
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
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:255
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
A list of face labels.
Definition: faceSet.H:51
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1785
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.
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:988
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:91
Direct mesh changes based on v1.3 polyTopoChange syntax.
label nPoints() const
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
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
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
IOdictionary systemDict(const word &dictName, const argList &args, const objectRegistry &ob, const word &regionName=polyMesh::defaultRegion, const fileName &path=fileName::null)
Definition: systemDict.C:93
static const char nl
Definition: Ostream.H:267
static const label labelMin
Definition: label.H:61
const bool overwrite
Definition: setNoOverwrite.H:1
Foam::argList args(argc, argv)