surfaceSplitByPatch.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-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  surfaceSplitByPatch
29 
30 Group
31  grpSurfaceUtilities
32 
33 Description
34  Writes surface regions to separate files.
35 
36 Usage
37  \b surfaceSplitByPatch [OPTION]
38 
39  Options:
40  - \par -patches NAME | LIST
41  Specify single patch or multiple patches (name or regex) to extract
42  For example,
43  \verbatim
44  -patches top
45  -patches '( front \".*back\" )'
46  \endverbatim
47 
48  - \par -excludePatches NAME | LIST
49  Exclude single or multiple patches (name or regex) from extracting.
50  For example,
51  \verbatim
52  -excludePatches '( inlet_1 inlet_2 "proc.*")'
53  \endverbatim
54 
55 \*---------------------------------------------------------------------------*/
56 
57 #include "argList.H"
58 #include "MeshedSurfaces.H"
59 #include "stringListOps.H"
60 #include "geometricSurfacePatch.H"
61 
62 using namespace Foam;
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 int main(int argc, char *argv[])
67 {
69  (
70  "Write surface mesh regions to separate files"
71  );
74  (
75  "patches",
76  "wordRes",
77  "Specify single patch or multiple patches to extract\n"
78  "Eg, 'top' or '( front \".*back\" )'"
79  );
81  (
82  "excludePatches",
83  "wordRes",
84  "Exclude single or multiple patches (name or regex) from extracting.\n"
85  "Eg, 'outlet' or '( inlet \".*Wall\" )'"
86  );
87 
88  argList::addArgument("input", "The input surface file");
89 
90  argList args(argc, argv);
91 
92  const auto surfName = args.get<fileName>(1);
93 
94  const fileName surfBase(surfName.lessExt());
95 
96  const word extension(surfName.ext());
97 
98 
99  Info<< nl
100  << "Read surface from " << surfName << " ..." << nl << endl;
101 
102  meshedSurface surf(surfName);
103 
104  const surfZoneList& zones = surf.surfZones();
105 
106  Info<< " " << surf.size() << " faces with "
107  << zones.size() << " zones" << nl << nl;
108 
109 
110  wordRes includePatches, excludePatches;
111 
112  if (args.readListIfPresent<wordRe>("patches", includePatches))
113  {
114  Info<< "Including patches " << flatOutput(includePatches)
115  << nl << endl;
116  }
117  if (args.readListIfPresent<wordRe>("excludePatches", excludePatches))
118  {
119  Info<< "Excluding patches " << flatOutput(excludePatches)
120  << nl << endl;
121  }
122 
123  // Identity if both include/exclude lists are empty
124  const labelList zoneIndices
125  (
127  (
128  zones,
129  includePatches,
130  excludePatches,
132  )
133  );
134 
135 
136  Info<< "Writing regions to "
137  << zoneIndices.size() << " separate files ..." << nl << endl;
138 
139 
140  // Faces to subset
141  bitSet includeMap(surf.size());
142 
143  for (const label zonei : zoneIndices)
144  {
145  const surfZone& zn = zones[zonei];
146 
147  includeMap.reset();
148  includeMap.set(zn.range());
149 
150  word patchName(zn.name());
151 
152  if (patchName.empty())
153  {
154  // In case people expect the same names as with triSurface
155  patchName = geometricSurfacePatch::defaultName(zonei);
156  }
157 
158  fileName outFile(surfBase + '_' + patchName + '.' + extension);
159 
160  Info<< " Zone " << zonei << " (" << zn.size() << " faces) "
161  << patchName
162  << " to file " << outFile << nl;
163 
164  // Subset and write
165  surf.subsetMesh(includeMap).write(outFile);
166  }
167 
168  Info<< "\nEnd\n" << endl;
169 
170  return 0;
171 }
172 
173 
174 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
Foam::surfZone::range
labelRange range() const
Definition: surfZone.H:148
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::geometricSurfacePatch::defaultName
static word defaultName(const label n=-1)
Definition: geometricSurfacePatch.H:73
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:119
Foam::argList::readListIfPresent
bool readListIfPresent(const word &optName, List< T > &list) const
Definition: argListI.H:387
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::argList::get
T get(const label index) const
Definition: argListI.H:271
geometricSurfacePatch.H
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:78
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Definition: argList.C:294
Foam::Info
messageStream Info
argList.H
Foam::nameOp
Definition: word.H:247
Foam::stringListOps::findMatching
labelList findMatching(const StringListType &input, const wordRes &allow, const wordRes &deny=wordRes(), AccessOp aop=noOp())
Foam::surfZone::size
label size() const
Definition: surfZone.H:136
Foam
Definition: atmBoundaryLayer.C:26
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Definition: FlatOutput.H:217
Foam::nl
constexpr char nl
Definition: Ostream.H:424
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::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:52
MeshedSurfaces.H
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:47
Foam::List::set
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
Definition: List.H:337
Foam::surfZoneIdentifier::name
const word & name() const
Definition: surfZoneIdentifier.H:137
stringListOps.H
Operations on lists of strings.
Foam::argList::noParallel
static void noParallel()
Definition: argList.C:503
Foam::MeshedSurface
A surface geometry mesh with zone information, not to be confused with the similarly named surfaceMes...
Definition: triSurfaceTools.H:76
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)