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-------------------------------------------------------------------------------
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 surfaceSplitByPatch
29
30Group
31 grpSurfaceUtilities
32
33Description
34 Writes surface regions to separate files.
35
36Usage
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"
61
62using namespace Foam;
63
64// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65
66int main(int argc, char *argv[])
67{
68 argList::addNote
69 (
70 "Write surface mesh regions to separate files"
71 );
72 argList::noParallel();
73 argList::addOption
74 (
75 "patches",
76 "wordRes",
77 "Specify single patch or multiple patches to extract\n"
78 "Eg, 'top' or '( front \".*back\" )'"
79 );
80 argList::addOption
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 (
126 stringListOps::findMatching
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// ************************************************************************* //
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:330
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:124
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
bool readListIfPresent(const word &optName, List< T > &list) const
Definition: argListI.H:394
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
A class for handling file names.
Definition: fileName.H:76
const word & name() const
The patch/zone name.
A surface zone on a MeshedSurface.
Definition: surfZone.H:59
label size() const
The size of this zone in the face list.
Definition: surfZone.H:140
labelRange range() const
The start/size range of this zone.
Definition: surfZone.H:152
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:54
A class for handling words, derived from Foam::string.
Definition: word.H:68
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
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Foam::argList args(argc, argv)
Operations on lists of strings.
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:238