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-2023 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 -exclude-patches NAME | LIST
49  Exclude single or multiple patches (name or regex) from extracting.
50  For example,
51  \verbatim
52  -exclude-patches '( 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  "exclude-patches",
83  "wordRes",
84  "Exclude single or multiple patches (name or regex) from extracting.\n"
85  "Eg, 'outlet' or '( inlet \".*Wall\" )'"
86  );
87  argList::addOptionCompat("exclude-patches", {"excludePatches", 2306});
88 
89  argList::addArgument("input", "The input surface file");
90 
91  argList args(argc, argv);
92 
93  const auto surfName = args.get<fileName>(1);
94 
95  const fileName surfBase(surfName.lessExt());
96 
97  const word extension(surfName.ext());
98 
99 
100  Info<< nl
101  << "Read surface from " << surfName << " ..." << nl << endl;
102 
103  meshedSurface surf(surfName);
104 
105  const surfZoneList& zones = surf.surfZones();
106 
107  Info<< " " << surf.size() << " faces with "
108  << zones.size() << " zones" << nl << nl;
109 
110 
111  wordRes includePatches, excludePatches;
112 
113  if (args.readListIfPresent<wordRe>("patches", includePatches))
114  {
115  Info<< "Including patches " << flatOutput(includePatches)
116  << nl << endl;
117  }
118  if (args.readListIfPresent<wordRe>("exclude-patches", excludePatches))
119  {
120  Info<< "Excluding patches " << flatOutput(excludePatches)
121  << nl << endl;
122  }
123 
124  // Identity if both include/exclude lists are empty
125  const labelList zoneIndices
126  (
128  (
129  zones,
130  includePatches,
131  excludePatches,
133  )
134  );
135 
136 
137  Info<< "Writing regions to "
138  << zoneIndices.size() << " separate files ..." << nl << endl;
139 
140 
141  // Faces to subset
142  bitSet includeMap(surf.size());
143 
144  for (const label zonei : zoneIndices)
145  {
146  const surfZone& zn = zones[zonei];
147 
148  includeMap.reset();
149  includeMap.set(zn.range());
150 
151  word patchName(zn.name());
152 
153  if (patchName.empty())
154  {
155  // In case people expect the same names as with triSurface
156  patchName = geometricSurfacePatch::defaultName(zonei);
157  }
158 
159  fileName outFile(surfBase + '_' + patchName + '.' + extension);
160 
161  Info<< " Zone " << zonei << " (" << zn.size() << " faces) "
162  << patchName
163  << " to file " << outFile << nl;
164 
165  // Subset and write
166  surf.subsetMesh(includeMap).write(outFile);
167  }
168 
169  Info<< "\nEnd\n" << endl;
170 
171  return 0;
172 }
173 
174 
175 // ************************************************************************* //
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
const word & name() const
The patch/zone name.
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition: List.H:489
A surface zone on a MeshedSurface.
Definition: surfZone.H:52
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
label size() const
The size of this zone in the face list.
Definition: surfZone.H:162
static void noParallel()
Remove the parallel options.
Definition: argList.C:584
static void addOptionCompat(const word &optName, std::pair< const char *, int > compat)
Specify an alias for the option name.
Definition: argList.C:418
Operations on lists of strings.
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:340
static word defaultName(const label n=-1)
Default patch name: "patch" or "patchN".
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
labelList findMatching(const StringListType &input, const wordRes::filter &pred, AccessOp aop=identityOp())
Return ids for items with matching names.
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
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:385
labelRange range() const
The start/size range of this zone.
Definition: surfZone.H:178
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
bool readListIfPresent(const word &optName, List< T > &list) const
If named option is present, get a List of values treating a single entry like a list of size 1...
Definition: argListI.H:387
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:351
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::argList args(argc, argv)
Namespace for OpenFOAM.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225