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 | Copyright (C) 2011-2016 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 
57 using namespace Foam;
58 
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 
61 int main(int argc, char *argv[])
62 {
63  timeSelector::addOptions(true, false);
65  (
66  "Collapses small edges to a point.\n"
67  "Optionally collapse small faces to a point and thin faces to an edge."
68  );
69 
71  (
72  "collapseFaces",
73  "Collapse small and sliver faces as well as small edges"
74  );
75 
77  (
78  "collapseFaceSet",
79  "faceSet",
80  "Collapse faces that are in the supplied face set"
81  );
82 
83  #include "addDictOption.H"
84  #include "addOverwriteOption.H"
85  #include "setRootCase.H"
86  #include "createTime.H"
87 
88  runTime.functionObjects().off();
89  instantList timeDirs = timeSelector::selectIfPresent(runTime, args);
90 
91  #include "createMesh.H"
92 
93  const word oldInstance = mesh.pointsInstance();
94 
95  const word dictName("collapseDict");
97 
98  Info<< "Reading " << dictName << nl << endl;
99 
100  IOdictionary collapseDict(dictIO);
101 
102  const bool overwrite = args.optionFound("overwrite");
103 
104  const bool collapseFaces = args.optionFound("collapseFaces");
105  const bool collapseFaceSet = args.optionFound("collapseFaceSet");
106 
107  if (collapseFaces && collapseFaceSet)
108  {
110  << "Both face zone collapsing and face collapsing have been"
111  << "selected. Choose only one of:" << nl
112  << " -collapseFaces" << nl
113  << " -collapseFaceSet <faceSet>"
114  << abort(FatalError);
115  }
116 
117 
118  // maintain indirectPatchFaces if it is there (default) or force
119  // (if collapseFaceSet option provided)
120  word faceSetName("indirectPatchFaces");
122 
123  if (args.optionReadIfPresent("collapseFaceSet", faceSetName))
124  {
125  readFlag = IOobject::MUST_READ;
126  }
127 
128 
129 
130  labelIOList pointPriority
131  (
132  IOobject
133  (
134  "pointPriority",
135  runTime.timeName(),
136  runTime,
139  ),
141  );
142  forAll(timeDirs, timeI)
143  {
144  runTime.setTime(timeDirs[timeI], timeI);
145 
146  Info<< "Time = " << runTime.timeName() << endl;
147 
148  autoPtr<polyMeshFilter> meshFilterPtr;
149 
150  label nBadFaces = 0;
151 
152  faceSet indirectPatchFaces
153  (
154  mesh,
155  faceSetName,
156  readFlag,
158  );
159  Info<< "Read faceSet " << indirectPatchFaces.name()
160  << " with "
161  << returnReduce(indirectPatchFaces.size(), sumOp<label>())
162  << " faces" << endl;
163 
164 
165  {
166  meshFilterPtr.set
167  (
168  new polyMeshFilter(mesh, pointPriority, collapseDict)
169  );
170  polyMeshFilter& meshFilter = meshFilterPtr();
171 
172  // newMesh will be empty until it is filtered
173  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
174 
175  // Filter small edges only. This reduces the number of faces so that
176  // the face filtering is sped up.
177  nBadFaces = meshFilter.filterEdges(0);
178  {
179  polyTopoChange meshMod(newMesh());
180 
181  meshMod.changeMesh(mesh, false);
182 
183  polyMeshFilter::copySets(newMesh(), mesh);
184  }
185 
186  pointPriority = meshFilter.pointPriority();
187  }
188 
189  if (collapseFaceSet)
190  {
191  meshFilterPtr.reset
192  (
193  new polyMeshFilter(mesh, pointPriority, collapseDict)
194  );
195  polyMeshFilter& meshFilter = meshFilterPtr();
196 
197  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
198 
199  // Filter faces. Pass in the number of bad faces that are present
200  // from the previous edge filtering to use as a stopping criterion.
201  meshFilter.filter(indirectPatchFaces);
202  {
203  polyTopoChange meshMod(newMesh);
204 
205  meshMod.changeMesh(mesh, false);
206 
207  polyMeshFilter::copySets(newMesh(), mesh);
208  }
209 
210  pointPriority = meshFilter.pointPriority();
211  }
212 
213  if (collapseFaces)
214  {
215  meshFilterPtr.reset
216  (
217  new polyMeshFilter(mesh, pointPriority, collapseDict)
218  );
219  polyMeshFilter& meshFilter = meshFilterPtr();
220 
221  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
222 
223  // Filter faces. Pass in the number of bad faces that are present
224  // from the previous edge filtering to use as a stopping criterion.
225  meshFilter.filter(nBadFaces);
226  {
227  polyTopoChange meshMod(newMesh);
228 
229  meshMod.changeMesh(mesh, false);
230 
231  polyMeshFilter::copySets(newMesh(), mesh);
232  }
233 
234  pointPriority = meshFilter.pointPriority();
235  }
236 
237  // Write resulting mesh
238  if (!overwrite)
239  {
240  runTime++;
241  }
242  else
243  {
244  mesh.setInstance(oldInstance);
245  }
246 
247  Info<< nl << "Writing collapsed mesh to time "
248  << runTime.timeName() << nl << endl;
249 
250  mesh.write();
251  pointPriority.write();
252  }
253 
254  Info<< nl << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
255  << " ClockTime = " << runTime.elapsedClockTime() << " s"
256  << nl << endl;
257 
258  Info<< "End\n" << endl;
259 
260  return 0;
261 }
262 
263 
264 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
label filter(const label nOriginalBadFaces)
Filter edges and faces.
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
A list of face labels.
Definition: faceSet.H:48
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
const autoPtr< labelList > & pointPriority() const
Return the new pointPriority list.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
const autoPtr< fvMesh > & filteredMesh() const
Return reference to the filtered mesh. Does not check if the.
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
dynamicFvMesh & mesh
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
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:93
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:284
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:262
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
IOobject dictIO(dictName, runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE)
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:32
word dictName("noiseDict")
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:772
Direct mesh changes based on v1.3 polyTopoChange syntax.
label nPoints() const
messageStream Info
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:53
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:83
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:124
Foam::argList args(argc, argv)
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:870
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.