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-------------------------------------------------------------------------------
11License
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
27Application
28 collapseEdges
29
30Group
31 grpMeshAdvancedUtilities
32
33Description
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
50Usage
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
63using namespace Foam;
64
65// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66
67int main(int argc, char *argv[])
68{
69 argList::addNote
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)
75 argList::addBoolOption
76 (
77 "collapseFaces",
78 "Collapse small and sliver faces as well as small edges"
79 );
80
81 argList::addOption
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
97 instantList timeDirs = timeSelector::selectIfPresent(runTime, args);
98
99 #include "createNamedMesh.H"
100
101 const word oldInstance = mesh.pointsInstance();
102
103 const word dictName("collapseDict");
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");
129 IOobject::readOption readFlag = IOobject::READ_IF_PRESENT;
130
131 if (args.readIfPresent("collapseFaceSet", faceSetName))
132 {
133 readFlag = IOobject::MUST_READ;
134 }
135
136
137 labelIOList pointPriority
138 (
140 (
141 "pointPriority",
142 runTime.timeName(),
143 runTime,
144 IOobject::READ_IF_PRESENT,
145 IOobject::AUTO_WRITE
146 ),
147 labelList(mesh.nPoints(), labelMin)
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,
164 IOobject::AUTO_WRITE
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;
262 runTime.printExecutionTime(Info);
263
264 Info<< "End\n" << endl;
265
266 return 0;
267}
268
269
270// ************************************************************************* //
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:57
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
readOption
Enumeration defining the read options.
Definition: IOobject.H:177
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:323
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
A list of face labels.
Definition: faceSet.H:54
Remove the edges and faces of a polyMesh whilst satisfying the given mesh quality criteria.
const autoPtr< labelList > & pointPriority() const
Return the new pointPriority list.
label filter(const label nOriginalBadFaces)
Filter edges and faces.
const autoPtr< fvMesh > & filteredMesh() const
Return reference to the filtered mesh. Does not check if the.
Direct mesh changes based on v1.3 polyTopoChange syntax.
A class for handling words, derived from Foam::string.
Definition: word.H:68
dynamicFvMesh & mesh
engineTime & runTime
Required Variables.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
const word dictName("faMeshDefinition")
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce (copy) and return value.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
IOobject dictIO
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333