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::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::autoPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:109
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::IOobject::AUTO_WRITE
Definition: IOobject.H:129
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:895
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
addOverwriteOption.H
polyTopoChange.H
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:785
Foam::polyTopoChange
Direct mesh changes based on v1.3 polyTopoChange syntax.
Definition: polyTopoChange.H:99
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:413
dictName
const word dictName("blockMeshDict")
Foam::faceSet
A list of face labels.
Definition: faceSet.H:51
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:302
setSystemMeshDictionaryIO.H
Foam::sumOp
Definition: ops.H:213
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::Time::printExecutionTime
Ostream & printExecutionTime(OSstream &os) const
Print the elapsed ExecutionTime (cpu-time), ClockTime.
Definition: TimeIO.C:603
Foam::polyMesh::pointsInstance
const fileName & pointsInstance() const
Return the current instance directory for points.
Definition: polyMesh.C:815
Foam::polyMeshFilter
Remove the edges and faces of a polyMesh whilst satisfying the given mesh quality criteria.
Definition: polyMeshFilter.H:65
Foam::argList::noFunctionObjects
static void noFunctionObjects(bool addWithOption=false)
Remove '-noFunctionObjects' option and ignore any occurrences.
Definition: argList.C:454
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::polyMeshFilter::filteredMesh
const autoPtr< fvMesh > & filteredMesh() const
Return reference to the filtered mesh. Does not check if the.
Definition: polyMeshFilter.C:1121
argList.H
Foam::polyMeshFilter::filter
label filter(const label nOriginalBadFaces)
Filter edges and faces.
Definition: polyMeshFilter.C:972
faceSet.H
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
Foam::timeSelector::selectIfPresent
static instantList selectIfPresent(Time &runTime, const argList &args)
Definition: timeSelector.C:272
Foam::FatalError
error FatalError
createNamedMesh.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
polyMeshFilter.H
dictIO
IOobject dictIO
Definition: setConstantMeshDictionaryIO.H:1
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::polyMeshFilter::pointPriority
const autoPtr< labelList > & pointPriority() const
Return the new pointPriority list.
Definition: polyMeshFilter.C:1128
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:325
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:108
Foam::List< instant >
Foam::Time::setTime
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:1006
Foam::primitiveMesh::nPoints
label nPoints() const
Number of mesh points.
Definition: primitiveMeshI.H:37
Foam::IOList< label >
timeSelector.H
createTime.H
Foam::labelMin
constexpr label labelMin
Definition: label.H:60
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:118
Foam::argList::addOption
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:336
args
Foam::argList args(argc, argv)
Foam::polyMesh::setInstance
void setInstance(const fileName &instance, const IOobject::writeOption wOpt=IOobject::AUTO_WRITE)
Set the instance for mesh files.
Definition: polyMeshIO.C:36
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157
Foam::IOobject::MUST_READ
Definition: IOobject.H:120