Loading...
Searching...
No Matches
setsToZones.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-2017 OpenFOAM Foundation
9 Copyright (C) 2021-2023 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 setsToZones
29
30Group
31 grpMeshManipulationUtilities
32
33Description
34 Add pointZones/faceZones/cellZones to the mesh from similar named
35 pointSets/faceSets/cellSets.
36
37 There is one catch: for faceZones you also need to specify a flip
38 condition which basically denotes the side of the face. In this app
39 it reads a cellSet (xxxCells if 'xxx' is the name of the faceSet) which
40 is the masterCells of the zone.
41 There are lots of situations in which this will go wrong but it is the
42 best I can think of for now.
43
44 If one is not interested in sideNess specify the -noFlipMap
45 command line option.
46
47\*---------------------------------------------------------------------------*/
48
49#include "argList.H"
50#include "Time.H"
51#include "polyMesh.H"
52#include "cellSet.H"
53#include "faceSet.H"
54#include "pointSet.H"
55#include "IOobjectList.H"
56#include "timeSelector.H"
57
58using namespace Foam;
59
60// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61
62
63int main(int argc, char *argv[])
64{
65 timeSelector::addOptions_singleTime(); // Single-time options
66
68 (
69 "Add point/face/cell Zones from similarly named point/face/cell Sets"
70 );
71
73 (
74 "noFlipMap",
75 "Ignore orientation of faceSet"
76 );
77
78 #include "addRegionOption.H"
79 #include "setRootCase.H"
80 #include "createTime.H"
81
82 const bool noFlipMap = args.found("noFlipMap");
83
84 // Allow override of time from specified time options, or no-op
86
87 #include "createNamedPolyMesh.H"
88
89 const fileName setsSubPath(mesh.meshDir()/"sets");
90
91 // Search for list of objects for the time of the mesh
92 word setsInstance = runTime.findInstance
93 (
94 setsSubPath,
97 mesh.facesInstance()
98 );
99
100 IOobjectList objects(mesh, setsInstance, polyMesh::meshSubDir/"sets");
101
102 Info<< "Searched : " << setsInstance/setsSubPath
103 << nl
104 << "Found : " << objects.names() << nl
105 << endl;
106
107
108 for (const IOobject& io : objects.csorted<pointSet>())
109 {
110 // Not in memory. Load it.
111 pointSet set(io);
112 labelList pointLabels(set.sortedToc());
113
114 // The original number of zones
115 const label nOrigZones = mesh.pointZones().size();
116
117 // Get existing or create new empty zone
118 pointZone& zn = mesh.pointZones()(set.name());
119
120 if (nOrigZones == mesh.pointZones().size())
121 {
122 Info<< "Overwriting contents of existing pointZone "
123 << zn.index()
124 << " with that of set " << set.name() << "." << endl;
125 }
126 else
127 {
128 Info<< "Adding set " << set.name() << " as a pointZone." << endl;
129 }
130
131 zn = pointLabels;
132
133 mesh.pointZones().writeOpt(IOobject::AUTO_WRITE);
134 mesh.pointZones().instance() = mesh.facesInstance();
135 }
136
137
138 wordHashSet slaveCellSets;
139
140 for (const IOobject& io : objects.csorted<faceSet>())
141 {
142 // Not in memory. Load it.
143 faceSet set(io);
144 labelList faceLabels(set.sortedToc());
145
146 DynamicList<label> addressing(set.size());
147 DynamicList<bool> flipMap(set.size());
148
149 if (noFlipMap)
150 {
151 // No flip map.
153 {
154 label facei = faceLabels[i];
155 addressing.append(facei);
156 flipMap.append(false);
157 }
158 }
159 else
160 {
161 const word setName(set.name() + "SlaveCells");
162
163 Info<< "Trying to load cellSet " << setName
164 << " to find out the slave side of the zone." << nl
165 << "If you do not care about the flipMap"
166 << " (i.e. do not use the sideness)" << nl
167 << "use the -noFlipMap command line option."
168 << endl;
169
170 // Load corresponding cells
171 cellSet cells(mesh, setName);
172
173 // Store setName to exclude from cellZones further on
174 slaveCellSets.insert(setName);
175
177 {
178 label facei = faceLabels[i];
179
180 bool flip = false;
181
182 if (mesh.isInternalFace(facei))
183 {
184 if
185 (
186 cells.found(mesh.faceOwner()[facei])
187 && !cells.found(mesh.faceNeighbour()[facei])
188 )
189 {
190 flip = false;
191 }
192 else if
193 (
194 !cells.found(mesh.faceOwner()[facei])
195 && cells.found(mesh.faceNeighbour()[facei])
196 )
197 {
198 flip = true;
199 }
200 else
201 {
203 << "One of owner or neighbour of internal face "
204 << facei << " should be in cellSet " << cells.name()
205 << " to be able to determine orientation." << endl
206 << "Face:" << facei
207 << " own:" << mesh.faceOwner()[facei]
208 << " OwnInCellSet:"
209 << cells.found(mesh.faceOwner()[facei])
210 << " nei:" << mesh.faceNeighbour()[facei]
211 << " NeiInCellSet:"
212 << cells.found(mesh.faceNeighbour()[facei])
213 << abort(FatalError);
214 }
215 }
216 else
217 {
218 if (cells.found(mesh.faceOwner()[facei]))
219 {
220 flip = false;
221 }
222 else
223 {
224 flip = true;
225 }
226 }
227
228 addressing.append(facei);
229 flipMap.append(flip);
230 }
231 }
232
233 // The original number of zones
234 const label nOrigZones = mesh.faceZones().size();
235
236 // Get existing or create new empty zone
237 faceZone& zn = mesh.faceZones()(set.name());
238
239 if (nOrigZones == mesh.faceZones().size())
240 {
241 Info<< "Overwriting contents of existing faceZone "
242 << zn.index()
243 << " with that of set " << set.name() << "." << endl;
244 }
245 else
246 {
247 Info<< "Adding set " << set.name() << " as a faceZone." << endl;
248 }
249
251 (
252 addressing.shrink(),
253 flipMap.shrink()
254 );
255
256 mesh.faceZones().writeOpt(IOobject::AUTO_WRITE);
257 mesh.faceZones().instance() = mesh.facesInstance();
258 }
259
260
261
262 for (const IOobject& io : objects.csorted<cellSet>())
263 {
264 if (!slaveCellSets.found(io.name()))
265 {
266 // Not in memory. Load it.
267 cellSet set(io);
268 labelList cellLabels(set.sortedToc());
269
270 // The original number of zones
271 const label nOrigZones = mesh.cellZones().size();
272
273 cellZone& zn = mesh.cellZones()(set.name());
274
275 if (nOrigZones == mesh.cellZones().size())
276 {
277 Info<< "Overwriting contents of existing cellZone "
278 << zn.index()
279 << " with that of set " << set.name() << "." << endl;
280 }
281 else
282 {
283 Info<< "Adding set " << set.name() << " as a cellZone." << endl;
284 }
285
286 zn = cellLabels;
287
288 mesh.cellZones().writeOpt(IOobject::AUTO_WRITE);
289 mesh.cellZones().instance() = mesh.facesInstance();
290 }
291 }
292
293
294 Info<< "Writing mesh." << endl;
295
296 if (!mesh.write())
297 {
299 << "Failed writing polyMesh."
300 << exit(FatalError);
301 }
302
303 Info<< "End\n" << endl;
304
305 return 0;
306}
307
308
309// ************************************************************************* //
Required Classes.
labelList faceLabels(nFaceLabels)
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition HashSet.H:229
bool found(const Key &key) const
Same as contains().
Definition HashTable.H:1370
List of IOobjects with searching and retrieving facilities. Implemented as a HashTable,...
@ MUST_READ
Reading required.
@ AUTO_WRITE
Automatically write from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition argList.C:389
static void addNote(const string &note)
Add extra notes for the usage information.
Definition argList.C:477
A collection of cell labels.
Definition cellSet.H:50
A subset of mesh cells.
Definition cellZone.H:61
A list of face labels.
Definition faceSet.H:50
A subset of mesh faces organised as a primitive patch.
Definition faceZone.H:63
virtual void resetAddressing(faceZone &&zn)
Move reset addressing and flip map from another zone.
Definition faceZone.C:500
A class for handling file names.
Definition fileName.H:75
A set of point labels.
Definition pointSet.H:50
A subset of mesh points.
Definition pointZone.H:64
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh").
Definition polyMesh.H:411
static void addOptions_singleTime()
Add single-time timeSelector options to argList::validOptions().
static bool setTimeIfPresent(Time &runTime, const argList &args, const bool forceInitial=false)
Set the runTime based on -constant (if present), -time (value), or -latestTime.
A class for handling words, derived from Foam::string.
Definition word.H:66
static const word null
An empty word.
Definition word.H:84
label index() const noexcept
The index of this zone in the zone list.
dynamicFvMesh & mesh
engineTime & runTime
Required Classes.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
const auto & io
const cellShapeList & cells
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition BitOps.C:35
Namespace for OpenFOAM.
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition HashSet.H:80
List< label > labelList
A List of labels.
Definition List.H:62
messageStream Info
Information stream (stdout output on master, null elsewhere).
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
labelList pointLabels(nPoints, -1)
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299