surfaceSplitByTopology.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) 2021 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  surfaceSplitByTopology
29 
30 Group
31  grpSurfaceUtilities
32 
33 Description
34  Strips any baffle parts of a surface.
35 
36  A baffle region is one which is reached by walking from an open edge, and
37  stopping when a multiply connected edge is reached.
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #include "argList.H"
42 #include "triSurface.H"
43 
44 using namespace Foam;
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 int main(int argc, char *argv[])
49 {
51  (
52  "Strips any baffle parts of a surface.\n"
53  "A baffle region is one which is reached by walking from an open edge,"
54  " and stopping when a multiply connected edge is reached."
55  );
56 
58  argList::validOptions.clear();
59  argList::addArgument("input", "The input surface file");
60  argList::addArgument("output", "The output surface file");
61  argList args(argc, argv);
62 
63  const auto surfFileName = args.get<fileName>(1);
64  Info<< "Reading surface from " << surfFileName << endl;
65 
66  const auto outFileName = args.get<fileName>(2);
67  const fileName outFileBaseName = outFileName.lessExt();
68  const word outExtension = outFileName.ext();
69 
70  // Load surface
71  triSurface surf(surfFileName);
72 
73  bool anyZoneRemoved = false;
74 
75  label iterationNo = 0;
76  label iterationLimit = 10;
77 
78  Info<< "Splitting off baffle parts " << endl;
79 
80  do
81  {
82  anyZoneRemoved = false;
83 
85 
86  const labelListList& edFaces = surf.edgeFaces();
87  const labelListList& faceEds = surf.faceEdges();
88 
89  boolList multipleEdges(edFaces.size(), false);
90 
91  forAll(multipleEdges, i)
92  {
93  if (edFaces[i].size() > 2)
94  {
95  multipleEdges[i] = true;
96  }
97  }
98 
99  label nZones = surf.markZones(multipleEdges, faceZone);
100 
101  if (nZones < 2)
102  {
103  break;
104  }
105 
106  boolList nonBaffle(faceZone.size(), true);
107  boolList baffle(faceZone.size(), true);
108  labelList pointMap;
110 
111 
112  for (label z = 0; z < nZones; z++)
113  {
114  bool keepZone = true;
115 
116  forAll(faceZone, f)
117  {
118  if (faceZone[f] == z)
119  {
120  forAll(faceEds[f], fe)
121  {
122  if (edFaces[faceEds[f][fe]].size() < 2)
123  {
124  keepZone = false;
125 
126  anyZoneRemoved = true;
127 
128  break;
129  }
130  }
131  }
132 
133  if (!keepZone)
134  {
135  break;
136  }
137  }
138 
139  forAll(faceZone, f)
140  {
141  if (faceZone[f] == z)
142  {
143  nonBaffle[f] = keepZone;
144  baffle[f] = !keepZone;
145  }
146  }
147  }
148 
149  Info<< " Iteration " << iterationNo << endl;
150 
151  triSurface baffleSurf = surf.subsetMesh(baffle, pointMap, faceMap);
152 
153  if (baffleSurf.size())
154  {
155  fileName bafflePartFileName =
156  outFileBaseName
157  + "_bafflePart_"
158  + name(iterationNo)
159  + "." + outExtension;
160 
161  Info<< " Writing baffle part to " << bafflePartFileName << endl;
162 
163  baffleSurf.write(bafflePartFileName);
164  }
165 
166  surf = surf.subsetMesh(nonBaffle, pointMap, faceMap);
167 
168  if (iterationNo == iterationLimit)
169  {
171  << "Iteration limit of " << iterationLimit << "reached" << endl;
172  }
173 
174  iterationNo++;
175 
176  } while (anyZoneRemoved && iterationNo < iterationLimit);
177 
178  Info<< "Writing new surface to " << outFileName << endl;
179 
180  surf.write(outFileName);
181 
183 
184  const labelListList& edFaces = surf.edgeFaces();
185 
186  boolList multipleEdges(edFaces.size(), false);
187 
188  forAll(multipleEdges, i)
189  {
190  if (edFaces[i].size() > 2)
191  {
192  multipleEdges[i] = true;
193  }
194  }
195 
196  label nZones = surf.markZones(multipleEdges, faceZone);
197 
198  Info<< "Splitting remaining multiply connected parts" << endl;
199 
200  for (label z = 0; z < nZones; z++)
201  {
202  boolList include(faceZone.size(), false);
203 
204  forAll(faceZone, f)
205  {
206  if (faceZone[f] == z)
207  {
208  include[f] = true;
209  }
210  }
211 
212  triSurface zoneSurf = surf.subsetMesh(include);
213 
214  fileName remainingPartFileName =
215  outFileBaseName
216  + "_multiplePart_"
217  + name(z)
218  + "." + outExtension;
219 
220  Info<< " Writing multiple part "
221  << z << " to " << remainingPartFileName << endl;
222 
223  zoneSurf.write(remainingPartFileName);
224  }
225 
226  Info << "End\n" << endl;
227 
228  return 0;
229 }
230 
231 
232 // ************************************************************************* //
triSurface subsetMesh(const UList< bool > &include, labelList &pointMap, labelList &faceMap) const
Return a new surface subsetted on the selected faces.
Definition: triSurface.C:872
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
A class for handling file names.
Definition: fileName.H:72
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void noParallel()
Remove the parallel options.
Definition: argList.C:584
void write(Ostream &os) const
Write to Ostream in simple OpenFOAM format.
Definition: triSurfaceIO.C:331
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
word ext() const
Return file name extension (part after last .)
Definition: wordI.H:171
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:118
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
labelList f(nPoints)
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
#define WarningInFunction
Report a warning using Foam::Warning.
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:351
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:238
messageStream Info
Information stream (stdout output on master, null elsewhere)
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:60
Triangulated surface description with patch information.
Definition: triSurface.H:71
Foam::argList args(argc, argv)
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:255
Namespace for OpenFOAM.