collapseEdges.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  collapseEdges
29 
30 Group
31  grpMeshAdvancedUtilities
32 
33 Description
34  Collapses short edges and combines edges that are in line.
35 
36  - collapse short edges. Length of edges to collapse provided as argument.
37  - merge two edges if they are in line. Maximum angle provided as argument.
38  - remove unused points.
39  - collapse faces:
40  - with small areas to a single point
41  - that have a high aspect ratio (i.e. sliver face) to a single edge
42 
43  Optionally checks the resulting mesh for bad faces and reduces the desired
44  face length factor for those faces attached to the bad faces.
45 
46  When collapsing an edge with one point on the boundary it will leave
47  the boundary point intact. When both points inside it chooses random. When
48  both points on boundary random again.
49 
50 Usage
51  - collapseEdges [OPTION]
52 
53 \*---------------------------------------------------------------------------*/
54 
55 #include "argList.H"
56 #include "Time.H"
57 #include "timeSelector.H"
58 #include "polyTopoChange.H"
59 #include "fvMesh.H"
60 #include "polyMeshFilter.H"
61 #include "faceSet.H"
62 
63 using namespace Foam;
64 
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66 
67 int main(int argc, char *argv[])
68 {
70  (
71  "Collapses small edges to a point.\n"
72  "Optionally collapse small faces to a point and thin faces to an edge."
73  );
74  timeSelector::addOptions(true, false); // constant(true), zero(false)
76  (
77  "collapseFaces",
78  "Collapse small and sliver faces as well as small edges"
79  );
80 
82  (
83  "collapseFaceSet",
84  "faceSet",
85  "Collapse faces that are in the supplied face set"
86  );
87 
88  argList::addOption("dict", "file", "Alternative collapseDict");
89 
90  #include "addOverwriteOption.H"
91 
92  argList::noFunctionObjects(); // Never use function objects
93 
94  #include "setRootCase.H"
95  #include "createTime.H"
96 
98 
99  #include "createNamedMesh.H"
100 
101  const word oldInstance = mesh.pointsInstance();
102 
103  const word dictName("collapseDict");
104  #include "setSystemMeshDictionaryIO.H"
105 
106  Info<< "Reading " << dictIO.name() << nl << endl;
107 
108  IOdictionary collapseDict(dictIO);
109 
110  const bool overwrite = args.found("overwrite");
111 
112  const bool collapseFaces = args.found("collapseFaces");
113  const bool collapseFaceSet = args.found("collapseFaceSet");
114 
115  if (collapseFaces && collapseFaceSet)
116  {
118  << "Both face zone collapsing and face collapsing have been"
119  << "selected. Choose only one of:" << nl
120  << " -collapseFaces" << nl
121  << " -collapseFaceSet <faceSet>"
122  << abort(FatalError);
123  }
124 
125 
126  // maintain indirectPatchFaces if it is there (default) or force
127  // (if collapseFaceSet option provided)
128  word faceSetName("indirectPatchFaces");
130 
131  if (args.readIfPresent("collapseFaceSet", faceSetName))
132  {
133  readFlag = IOobject::MUST_READ;
134  }
135 
136 
137  labelIOList pointPriority
138  (
139  IOobject
140  (
141  "pointPriority",
142  runTime.timeName(),
143  runTime,
146  ),
148  );
149  forAll(timeDirs, timeI)
150  {
151  runTime.setTime(timeDirs[timeI], timeI);
152 
153  Info<< "Time = " << runTime.timeName() << endl;
154 
155  autoPtr<polyMeshFilter> meshFilterPtr;
156 
157  label nBadFaces = 0;
158 
159  faceSet indirectPatchFaces
160  (
161  mesh,
162  faceSetName,
163  readFlag,
165  );
166  Info<< "Read faceSet " << indirectPatchFaces.name()
167  << " with "
168  << returnReduce(indirectPatchFaces.size(), sumOp<label>())
169  << " faces" << endl;
170 
171 
172  {
173  meshFilterPtr.reset
174  (
175  new polyMeshFilter(mesh, pointPriority, collapseDict)
176  );
177  polyMeshFilter& meshFilter = meshFilterPtr();
178 
179  // newMesh will be empty until it is filtered
180  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
181 
182  // Filter small edges only. This reduces the number of faces so that
183  // the face filtering is sped up.
184  nBadFaces = meshFilter.filterEdges(0);
185  {
186  polyTopoChange meshMod(newMesh());
187 
188  meshMod.changeMesh(mesh, false);
189 
190  polyMeshFilter::copySets(newMesh(), mesh);
191  }
192 
193  pointPriority = *(meshFilter.pointPriority());
194  }
195 
196  if (collapseFaceSet)
197  {
198  meshFilterPtr.reset
199  (
200  new polyMeshFilter(mesh, pointPriority, collapseDict)
201  );
202  polyMeshFilter& meshFilter = meshFilterPtr();
203 
204  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
205 
206  // Filter faces. Pass in the number of bad faces that are present
207  // from the previous edge filtering to use as a stopping criterion.
208  meshFilter.filter(indirectPatchFaces);
209  {
210  polyTopoChange meshMod(newMesh());
211 
212  meshMod.changeMesh(mesh, false);
213 
214  polyMeshFilter::copySets(newMesh(), mesh);
215  }
216 
217  pointPriority = *(meshFilter.pointPriority());
218  }
219 
220  if (collapseFaces)
221  {
222  meshFilterPtr.reset
223  (
224  new polyMeshFilter(mesh, pointPriority, collapseDict)
225  );
226  polyMeshFilter& meshFilter = meshFilterPtr();
227 
228  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
229 
230  // Filter faces. Pass in the number of bad faces that are present
231  // from the previous edge filtering to use as a stopping criterion.
232  meshFilter.filter(nBadFaces);
233  {
234  polyTopoChange meshMod(newMesh());
235 
236  meshMod.changeMesh(mesh, false);
237 
238  polyMeshFilter::copySets(newMesh(), mesh);
239  }
240 
241  pointPriority = *(meshFilter.pointPriority());
242  }
243 
244  // Write resulting mesh
245  if (!overwrite)
246  {
247  ++runTime;
248  }
249  else
250  {
251  mesh.setInstance(oldInstance);
252  }
253 
254  Info<< nl << "Writing collapsed mesh to time "
255  << runTime.timeName() << nl << endl;
256 
257  mesh.write();
258  pointPriority.write();
259  }
260 
261  Info<< nl;
263 
264  Info<< "End\n" << endl;
265 
266  return 0;
267 }
268 
269 
270 // ************************************************************************* //
static void noFunctionObjects(bool addWithOption=false)
Remove &#39;-noFunctionObjects&#39; option and ignore any occurrences.
Definition: argList.C:547
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
label filter(const label nOriginalBadFaces)
Filter edges and faces.
A list of face labels.
Definition: faceSet.H:47
const autoPtr< labelList > & pointPriority() const
Return the new pointPriority list.
label nPoints() const noexcept
Number of mesh points.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
const word dictName("faMeshDefinition")
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Required Classes.
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:374
constexpr label labelMin
Definition: label.H:54
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:37
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:853
dynamicFvMesh & mesh
A class for handling words, derived from Foam::string.
Definition: word.H:63
Reading is optional [identical to LAZY_READ].
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:385
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times - as per select0() - otherwise return just the cu...
Definition: timeSelector.C:291
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:902
Remove the edges and faces of a polyMesh whilst satisfying the given mesh quality criteria...
errorManip< error > abort(error &err)
Definition: errorManip.H:139
virtual bool write(const bool writeOnProc=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1113
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
Ostream & printExecutionTime(OSstream &os) const
Print the elapsed ExecutionTime (cpu-time), ClockTime.
Definition: TimeIO.C:607
void setInstance(const fileName &instance, const IOobjectOption::writeOption wOpt=IOobject::AUTO_WRITE)
Set the instance for mesh files.
Definition: polyMeshIO.C:29
Direct mesh changes based on v1.3 polyTopoChange syntax.
Automatically write from objectRegistry::writeObject()
messageStream Info
Information stream (stdout output on master, null elsewhere)
IOobject dictIO
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:316
const autoPtr< fvMesh > & filteredMesh() const
Return reference to the filtered mesh. Does not check if the.
List< label > labelList
A List of labels.
Definition: List.H:62
Foam::argList args(argc, argv)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:101
Namespace for OpenFOAM.
readOption
Enumeration defining read preferences.