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 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:165
Foam::IOobject::AUTO_WRITE
@ AUTO_WRITE
Definition: IOobject.H:190
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Definition: fvMesh.C:1034
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:88
addOverwriteOption.H
dictName
const word dictName("faMeshDefinition")
polyTopoChange.H
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:773
Foam::polyTopoChange
Direct mesh changes based on v1.3 polyTopoChange syntax.
Definition: polyTopoChange.H:95
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::faceSet
A list of face labels.
Definition: faceSet.H:47
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Definition: argListI.H:316
setSystemMeshDictionaryIO.H
Foam::sumOp
Definition: ops.H:207
Foam::primitiveMesh::nPoints
label nPoints() const noexcept
Definition: primitiveMeshI.H:30
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
Foam::Time::printExecutionTime
Ostream & printExecutionTime(OSstream &os) const
Definition: TimeIO.C:611
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Definition: polyMesh.C:839
Foam::polyMeshFilter
Remove the edges and faces of a polyMesh whilst satisfying the given mesh quality criteria.
Definition: polyMeshFilter.H:61
Foam::argList::noFunctionObjects
static void noFunctionObjects(bool addWithOption=false)
Definition: argList.C:466
Foam::Info
messageStream Info
Foam::polyMeshFilter::filteredMesh
const autoPtr< fvMesh > & filteredMesh() const
Definition: polyMeshFilter.C:1114
argList.H
Foam::polyMeshFilter::filter
label filter(const label nOriginalBadFaces)
Definition: polyMeshFilter.C:965
faceSet.H
Foam::IOobject::READ_IF_PRESENT
@ READ_IF_PRESENT
Definition: IOobject.H:183
Foam::timeSelector::selectIfPresent
static instantList selectIfPresent(Time &runTime, const argList &args)
Definition: timeSelector.C:259
Foam::FatalError
error FatalError
createNamedMesh.H
Required Variables.
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
polyMeshFilter.H
dictIO
IOobject dictIO
Definition: setConstantMeshDictionaryIO.H:1
fvMesh.H
Foam
Definition: atmBoundaryLayer.C:26
Foam::polyMeshFilter::pointPriority
const autoPtr< labelList > & pointPriority() const
Definition: polyMeshFilter.C:1121
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Foam::autoPtr::reset
void reset(autoPtr< T > &&other) noexcept
Foam::IOobject::name
const word & name() const noexcept
Definition: IOobjectI.H:58
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Definition: argList.C:317
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:49
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Definition: timeSelector.C:95
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::Time::setTime
virtual void setTime(const Time &t)
Definition: Time.C:996
Foam::IOList< label >
timeSelector.H
createTime.H
Foam::labelMin
constexpr label labelMin
Definition: label.H:54
Foam::IOobject::readOption
readOption
Definition: IOobject.H:179
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Definition: argList.C:328
args
Foam::argList args(argc, argv)
Foam::polyMesh::setInstance
void setInstance(const fileName &instance, const IOobject::writeOption wOpt=IOobject::AUTO_WRITE)
Definition: polyMeshIO.C:29
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181