setsToZones.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  setsToZones
26 
27 Description
28  Add pointZones/faceZones/cellZones to the mesh from similar named
29  pointSets/faceSets/cellSets.
30 
31  There is one catch: for faceZones you also need to specify a flip
32  condition which basically denotes the side of the face. In this app
33  it reads a cellSet (xxxCells if 'xxx' is the name of the faceSet) which
34  is the masterCells of the zone.
35  There are lots of situations in which this will go wrong but it is the
36  best I can think of for now.
37 
38  If one is not interested in sideNess specify the -noFlipMap
39  command line option.
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #include "argList.H"
44 #include "timeSelector.H"
45 #include "Time.H"
46 #include "polyMesh.H"
47 #include "IStringStream.H"
48 #include "cellSet.H"
49 #include "faceSet.H"
50 #include "pointSet.H"
51 #include "OFstream.H"
52 #include "IFstream.H"
53 #include "IOobjectList.H"
54 #include "SortableList.H"
55 
56 using namespace Foam;
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 
61 int main(int argc, char *argv[])
62 {
63  timeSelector::addOptions(true, false);
65  (
66  "add point/face/cell Zones from similar named point/face/cell Sets"
67  );
68 
70  (
71  "noFlipMap",
72  "ignore orientation of faceSet"
73  );
74 
75  #include "addRegionOption.H"
76  #include "setRootCase.H"
77  #include "createTime.H"
78 
79  const bool noFlipMap = args.optionFound("noFlipMap");
80 
81  // Get times list
83 
84  #include "createNamedPolyMesh.H"
85 
86  const fileName setsSubPath(mesh.dbDir()/polyMesh::meshSubDir/"sets");
87 
88  // Search for list of objects for the time of the mesh
89  word setsInstance = runTime.findInstance
90  (
91  setsSubPath,
92  word::null,
94  mesh.facesInstance()
95  );
96 
97  IOobjectList objects(mesh, setsInstance, polyMesh::meshSubDir/"sets");
98 
99  Info<< "Searched : " << setsInstance/setsSubPath
100  << nl
101  << "Found : " << objects.names() << nl
102  << endl;
103 
104 
105  IOobjectList pointObjects(objects.lookupClass(pointSet::typeName));
106 
107  // Pout<< "pointSets:" << pointObjects.names() << endl;
108 
109  forAllConstIter(IOobjectList, pointObjects, iter)
110  {
111  // Not in memory. Load it.
112  pointSet set(*iter());
113  SortableList<label> pointLabels(set.toc());
114 
115  label zoneID = mesh.pointZones().findZoneID(set.name());
116  if (zoneID == -1)
117  {
118  Info<< "Adding set " << set.name() << " as a pointZone." << endl;
119  label sz = mesh.pointZones().size();
120  mesh.pointZones().setSize(sz+1);
121  mesh.pointZones().set
122  (
123  sz,
124  new pointZone
125  (
126  set.name(), // name
127  pointLabels, // addressing
128  sz, // index
129  mesh.pointZones() // meshPointZones
130  )
131  );
132  mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
133  mesh.pointZones().instance() = mesh.facesInstance();
134  }
135  else
136  {
137  Info<< "Overwriting contents of existing pointZone " << zoneID
138  << " with that of set " << set.name() << "." << endl;
139  mesh.pointZones()[zoneID] = pointLabels;
140  mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
141  mesh.pointZones().instance() = mesh.facesInstance();
142  }
143  }
144 
145 
146 
147  IOobjectList faceObjects(objects.lookupClass(faceSet::typeName));
148 
149  HashSet<word> slaveCellSets;
150 
151  // Pout<< "faceSets:" << faceObjects.names() << endl;
152 
153  forAllConstIter(IOobjectList, faceObjects, iter)
154  {
155  // Not in memory. Load it.
156  faceSet set(*iter());
157  SortableList<label> faceLabels(set.toc());
158 
159  DynamicList<label> addressing(set.size());
160  DynamicList<bool> flipMap(set.size());
161 
162  if (noFlipMap)
163  {
164  // No flip map.
165  forAll(faceLabels, i)
166  {
167  label facei = faceLabels[i];
168  addressing.append(facei);
169  flipMap.append(false);
170  }
171  }
172  else
173  {
174  const word setName(set.name() + "SlaveCells");
175 
176  Info<< "Trying to load cellSet " << setName
177  << " to find out the slave side of the zone." << nl
178  << "If you do not care about the flipMap"
179  << " (i.e. do not use the sideness)" << nl
180  << "use the -noFlipMap command line option."
181  << endl;
182 
183  // Load corresponding cells
184  cellSet cells(mesh, setName);
185 
186  // Store setName to exclude from cellZones further on
187  slaveCellSets.insert(setName);
188 
189  forAll(faceLabels, i)
190  {
191  label facei = faceLabels[i];
192 
193  bool flip = false;
194 
195  if (mesh.isInternalFace(facei))
196  {
197  if
198  (
199  cells.found(mesh.faceOwner()[facei])
200  && !cells.found(mesh.faceNeighbour()[facei])
201  )
202  {
203  flip = false;
204  }
205  else if
206  (
207  !cells.found(mesh.faceOwner()[facei])
208  && cells.found(mesh.faceNeighbour()[facei])
209  )
210  {
211  flip = true;
212  }
213  else
214  {
216  << "One of owner or neighbour of internal face "
217  << facei << " should be in cellSet " << cells.name()
218  << " to be able to determine orientation." << endl
219  << "Face:" << facei
220  << " own:" << mesh.faceOwner()[facei]
221  << " OwnInCellSet:"
222  << cells.found(mesh.faceOwner()[facei])
223  << " nei:" << mesh.faceNeighbour()[facei]
224  << " NeiInCellSet:"
225  << cells.found(mesh.faceNeighbour()[facei])
226  << abort(FatalError);
227  }
228  }
229  else
230  {
231  if (cells.found(mesh.faceOwner()[facei]))
232  {
233  flip = false;
234  }
235  else
236  {
237  flip = true;
238  }
239  }
240 
241  addressing.append(facei);
242  flipMap.append(flip);
243  }
244  }
245 
246  label zoneID = mesh.faceZones().findZoneID(set.name());
247  if (zoneID == -1)
248  {
249  Info<< "Adding set " << set.name() << " as a faceZone." << endl;
250  label sz = mesh.faceZones().size();
251  mesh.faceZones().setSize(sz+1);
252  mesh.faceZones().set
253  (
254  sz,
255  new faceZone
256  (
257  set.name(), // name
258  addressing.shrink(), // addressing
259  flipMap.shrink(), // flipmap
260  sz, // index
261  mesh.faceZones() // meshFaceZones
262  )
263  );
264  mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
265  mesh.faceZones().instance() = mesh.facesInstance();
266  }
267  else
268  {
269  Info<< "Overwriting contents of existing faceZone " << zoneID
270  << " with that of set " << set.name() << "." << endl;
271  mesh.faceZones()[zoneID].resetAddressing
272  (
273  addressing.shrink(),
274  flipMap.shrink()
275  );
276  mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
277  mesh.faceZones().instance() = mesh.facesInstance();
278  }
279  }
280 
281 
282 
283  IOobjectList cellObjects(objects.lookupClass(cellSet::typeName));
284 
285  // Pout<< "cellSets:" << cellObjects.names() << endl;
286 
287  forAllConstIter(IOobjectList, cellObjects, iter)
288  {
289  if (!slaveCellSets.found(iter.key()))
290  {
291  // Not in memory. Load it.
292  cellSet set(*iter());
293  SortableList<label> cellLabels(set.toc());
294 
295  label zoneID = mesh.cellZones().findZoneID(set.name());
296  if (zoneID == -1)
297  {
298  Info<< "Adding set " << set.name() << " as a cellZone." << endl;
299  label sz = mesh.cellZones().size();
300  mesh.cellZones().setSize(sz+1);
301  mesh.cellZones().set
302  (
303  sz,
304  new cellZone
305  (
306  set.name(), // name
307  cellLabels, // addressing
308  sz, // index
309  mesh.cellZones() // meshCellZones
310  )
311  );
312  mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
313  mesh.cellZones().instance() = mesh.facesInstance();
314  }
315  else
316  {
317  Info<< "Overwriting contents of existing cellZone " << zoneID
318  << " with that of set " << set.name() << "." << endl;
319  mesh.cellZones()[zoneID] = cellLabels;
320  mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
321  mesh.cellZones().instance() = mesh.facesInstance();
322  }
323  }
324  }
325 
326 
327 
328  Info<< "Writing mesh." << endl;
329 
330  if (!mesh.write())
331  {
333  << "Failed writing polyMesh."
334  << exit(FatalError);
335  }
336 
337  Info<< "End\n" << endl;
338 
339  return 0;
340 }
341 
342 
343 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
A HashTable with keys but without contents.
Definition: HashSet.H:62
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:111
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:113
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:53
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: SortableList.H:55
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:159
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
A collection of cell labels.
Definition: cellSet.H:51
A subset of mesh cells.
Definition: cellZone.H:64
A list of face labels.
Definition: faceSet.H:51
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:68
A class for handling file names.
Definition: fileName.H:82
A set of point labels.
Definition: pointSet.H:51
A subset of mesh points. The labels of points in the zone can be obtained from the addressing() list.
Definition: pointZone.H:65
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:271
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:283
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int main(int argc, char *argv[])
Definition: financialFoam.C:44
const cellShapeList & cells
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
error FatalError
static const char nl
Definition: Ostream.H:260
labelList pointLabels(nPoints, -1)
objects
Foam::argList args(argc, argv)