All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2021 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"
87  #include "createTime.H"
88 
89  runTime.functionObjects().off();
90  instantList timeDirs = timeSelector::selectIfPresent(runTime, args);
91 
92  #include "createMeshNoChangers.H"
93 
94  const word oldInstance = mesh.pointsInstance();
95 
96  const dictionary collapseDict(systemDict("collapseDict", args, mesh));
97 
98  const bool overwrite = args.optionFound("overwrite");
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.timeName(),
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, false);
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, false);
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, false);
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.timeName() << 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:434
label filter(const label nOriginalBadFaces)
Filter edges and faces.
A list of face labels.
Definition: faceSet.H:48
const autoPtr< labelList > & pointPriority() const
Return the new pointPriority list.
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1658
fvMesh & mesh
readOption
Enumeration defining the read options.
Definition: IOobject.H:116
IOdictionary systemDict(const word &dictName, const argList &args, const objectRegistry &ob, const word &regionName=polyMesh::defaultRegion)
Definition: systemDict.C:92
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:882
A class for handling words, derived from string.
Definition: word.H:59
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
List< label > labelList
A List of labels.
Definition: labelList.H:56
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:283
Remove the edges and faces of a polyMesh whilst satisfying the given mesh quality criteria...
errorManip< error > abort(error &err)
Definition: errorManip.H:131
static const char nl
Definition: Ostream.H:260
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:77
Direct mesh changes based on v1.3 polyTopoChange syntax.
messageStream Info
label nPoints() const
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:159
const autoPtr< fvMesh > & filteredMesh() const
Return reference to the filtered mesh. Does not check if the.
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
static const label labelMin
Definition: label.H:61
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
Namespace for OpenFOAM.