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-2015 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 "addOverwriteOption.H"
84  #include "setRootCase.H"
85  #include "createTime.H"
86 
87  runTime.functionObjects().off();
89 
90  #include "createMesh.H"
91 
92  const word oldInstance = mesh.pointsInstance();
93 
94  const bool overwrite = args.optionFound("overwrite");
95 
96  const bool collapseFaces = args.optionFound("collapseFaces");
97  const bool collapseFaceSet = args.optionFound("collapseFaceSet");
98 
99  if (collapseFaces && collapseFaceSet)
100  {
102  << "Both face zone collapsing and face collapsing have been"
103  << "selected. Choose only one of:" << nl
104  << " -collapseFaces" << nl
105  << " -collapseFaceSet <faceSet>"
106  << abort(FatalError);
107  }
108 
109 
110  // maintain indirectPatchFaces if it is there (default) or force
111  // (if collapseFaceSet option provided)
112  word faceSetName("indirectPatchFaces");
114 
115  if (args.optionReadIfPresent("collapseFaceSet", faceSetName))
116  {
117  readFlag = IOobject::MUST_READ;
118  }
119 
120 
121 
122  labelIOList pointPriority
123  (
124  IOobject
125  (
126  "pointPriority",
127  runTime.timeName(),
128  runTime,
131  ),
133  );
134  forAll(timeDirs, timeI)
135  {
136  runTime.setTime(timeDirs[timeI], timeI);
137 
138  Info<< "Time = " << runTime.timeName() << endl;
139 
140  autoPtr<polyMeshFilter> meshFilterPtr;
141 
142  label nBadFaces = 0;
143 
144  faceSet indirectPatchFaces
145  (
146  mesh,
147  faceSetName,
148  readFlag,
150  );
151  Info<< "Read faceSet " << indirectPatchFaces.name()
152  << " with "
153  << returnReduce(indirectPatchFaces.size(), sumOp<label>())
154  << " faces" << endl;
155 
156 
157  {
158  meshFilterPtr.set(new polyMeshFilter(mesh, pointPriority));
159  polyMeshFilter& meshFilter = meshFilterPtr();
160 
161  // newMesh will be empty until it is filtered
162  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
163 
164  // Filter small edges only. This reduces the number of faces so that
165  // the face filtering is sped up.
166  nBadFaces = meshFilter.filterEdges(0);
167  {
168  polyTopoChange meshMod(newMesh());
169 
170  meshMod.changeMesh(mesh, false);
171 
172  polyMeshFilter::copySets(newMesh(), mesh);
173  }
174 
175  pointPriority = meshFilter.pointPriority();
176  }
177 
178  if (collapseFaceSet)
179  {
180  meshFilterPtr.reset(new polyMeshFilter(mesh, pointPriority));
181  polyMeshFilter& meshFilter = meshFilterPtr();
182 
183  const autoPtr<fvMesh>& newMesh = meshFilter.filteredMesh();
184 
185  // Filter faces. Pass in the number of bad faces that are present
186  // from the previous edge filtering to use as a stopping criterion.
187  meshFilter.filter(indirectPatchFaces);
188  {
189  polyTopoChange meshMod(newMesh);
190 
191  meshMod.changeMesh(mesh, false);
192 
193  polyMeshFilter::copySets(newMesh(), mesh);
194  }
195 
196  pointPriority = meshFilter.pointPriority();
197  }
198 
199  if (collapseFaces)
200  {
201  meshFilterPtr.reset(new polyMeshFilter(mesh, pointPriority));
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(nBadFaces);
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  // Write resulting mesh
221  if (!overwrite)
222  {
223  runTime++;
224  }
225  else
226  {
227  mesh.setInstance(oldInstance);
228  }
229 
230  Info<< nl << "Writing collapsed mesh to time "
231  << runTime.timeName() << nl << endl;
232 
233  mesh.write();
234  pointPriority.write();
235  }
236 
237  Info<< nl << "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
238  << " ClockTime = " << runTime.elapsedClockTime() << " s"
239  << nl << endl;
240 
241  Info<< "End\n" << endl;
242 
243  return 0;
244 }
245 
246 
247 // ************************************************************************* //
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:86
Foam::IOobject::AUTO_WRITE
@ AUTO_WRITE
Definition: IOobject.H:117
Foam::argList::addOption
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:108
Foam::argList::addNote
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:139
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
addOverwriteOption.H
polyTopoChange.H
Foam::polyTopoChange
Direct mesh changes based on v1.3 polyTopoChange syntax.
Definition: polyTopoChange.H:97
Foam::argList::addBoolOption
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:98
Foam::faceSet
A list of face labels.
Definition: faceSet.H:48
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:108
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::fvMesh::write
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:873
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:772
Foam::polyMeshFilter::filterEdges
label filterEdges(polyMesh &newMesh, scalarField &newMeshMinEdgeLen, labelList &origToCurrentPointMap)
Definition: polyMeshFilter.C:413
Foam::polyMeshFilter
Remove the edges and faces of a polyMesh whilst satisfying the given mesh quality criteria.
Definition: polyMeshFilter.H:62
Foam::primitiveMesh::nPoints
label nPoints() const
Definition: primitiveMeshI.H:35
Foam::labelMin
static const label labelMin
Definition: label.H:61
Foam::label
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
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::polyMeshFilter::filteredMesh
const autoPtr< fvMesh > & filteredMesh() const
Return reference to the filtered mesh. Does not check if the.
Definition: polyMeshFilter.C:1107
argList.H
Foam::polyMeshFilter::filter
label filter(const label nOriginalBadFaces)
Filter edges and faces.
Definition: polyMeshFilter.C:958
faceSet.H
Foam::autoPtr::set
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
main
int main(int argc, char *argv[])
Definition: postCalc.C:54
Foam::timeSelector::selectIfPresent
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:284
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
polyMeshFilter.H
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::polyMeshFilter::pointPriority
const autoPtr< labelList > & pointPriority() const
Return the new pointPriority list.
Definition: polyMeshFilter.C:1114
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::polyMesh::setInstance
void setInstance(const fileName &)
Set the instance for mesh files.
Definition: polyMeshIO.C:32
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
setRootCase.H
timeDirs
static instantList timeDirs
Definition: globalFoam.H:44
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::sumOp
Definition: ops.H:162
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
createMesh.H
Foam::IOList< label >
Foam::polyMeshFilter::copySets
static void copySets(const polyMesh &oldMesh, const polyMesh &newMesh)
Definition: polyMeshFilterTemplates.C:71
timeSelector.H
createTime.H
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::autoPtr::reset
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
args
Foam::argList args(argc, argv)
Foam::IOobject::READ_IF_PRESENT
@ READ_IF_PRESENT
Definition: IOobject.H:110
Foam::argList::optionReadIfPresent
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198