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-2022 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 argList::addNote
66 (
67 "Add point/face/cell Zones from similarly named point/face/cell Sets"
68 );
69
70 timeSelector::addOptions(true, false); // constant(true), zero(false)
71 argList::addBoolOption
72 (
73 "noFlipMap",
74 "Ignore orientation of faceSet"
75 );
76
77 #include "addRegionOption.H"
78 #include "addTimeOptions.H"
79 #include "setRootCase.H"
80 #include "createTime.H"
81
82 const bool noFlipMap = args.found("noFlipMap");
83
84 // Get times list
85 (void)timeSelector::selectIfPresent(runTime, args);
86
87 #include "createNamedPolyMesh.H"
88
89 const fileName setsSubPath(mesh.dbDir()/polyMesh::meshSubDir/"sets");
90
91 // Search for list of objects for the time of the mesh
92 word setsInstance = runTime.findInstance
93 (
94 setsSubPath,
96 IOobject::MUST_READ,
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.sorted<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.sorted<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.
152 forAll(faceLabels, i)
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
176 forAll(faceLabels, i)
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.sorted<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// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
bool found(const Key &key) const
Return true if hashed entry is found in table.
Definition: HashTableI.H:100
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:59
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
A collection of cell labels.
Definition: cellSet.H:54
A subset of mesh cells.
Definition: cellZone.H:65
A list of face labels.
Definition: faceSet.H:54
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:67
virtual void resetAddressing(const labelUList &addr, const bool flipMapValue)
Reset addressing - use uniform flip map value.
Definition: faceZone.C:444
A class for handling file names.
Definition: fileName.H:76
A set of point labels.
Definition: pointSet.H:54
A subset of mesh points.
Definition: pointZone.H:68
A class for handling words, derived from Foam::string.
Definition: word.H:68
label index() const noexcept
The index of this zone in the zone list.
dynamicFvMesh & mesh
engineTime & runTime
Required Variables.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
const cellShapeList & cells
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition: BitOps.C:38
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
errorManip< error > abort(error &err)
Definition: errorManip.H:144
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
labelList pointLabels(nPoints, -1)
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333