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-2024 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().findIndex(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  mesh.pointZones() // pointZones
129  )
130  );
131  mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
132  mesh.pointZones().instance() = mesh.facesInstance();
133  }
134  else
135  {
136  Info<< "Overwriting contents of existing pointZone " << zoneID
137  << " with that of set " << set.name() << "." << endl;
138  mesh.pointZones()[zoneID] = pointLabels;
139  mesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
140  mesh.pointZones().instance() = mesh.facesInstance();
141  }
142  }
143 
144 
145 
146  IOobjectList faceObjects(objects.lookupClass(faceSet::typeName));
147 
148  HashSet<word> slaveCellSets;
149 
150  // Pout<< "faceSets:" << faceObjects.names() << endl;
151 
152  forAllConstIter(IOobjectList, faceObjects, iter)
153  {
154  // Not in memory. Load it.
155  faceSet set(*iter());
156  SortableList<label> faceLabels(set.toc());
157 
158  DynamicList<label> addressing(set.size());
159  DynamicList<bool> flipMap(set.size());
160 
161  if (noFlipMap)
162  {
163  // No flip map.
164  forAll(faceLabels, i)
165  {
166  label facei = faceLabels[i];
167  addressing.append(facei);
168  flipMap.append(false);
169  }
170  }
171  else
172  {
173  const word setName(set.name() + "SlaveCells");
174 
175  Info<< "Trying to load cellSet " << setName
176  << " to find out the slave side of the zone." << nl
177  << "If you do not care about the flipMap"
178  << " (i.e. do not use the sideness)" << nl
179  << "use the -noFlipMap command line option."
180  << endl;
181 
182  // Load corresponding cells
183  cellSet cells(mesh, setName);
184 
185  // Store setName to exclude from cellZones further on
186  slaveCellSets.insert(setName);
187 
188  forAll(faceLabels, i)
189  {
190  label facei = faceLabels[i];
191 
192  bool flip = false;
193 
194  if (mesh.isInternalFace(facei))
195  {
196  if
197  (
198  cells.found(mesh.faceOwner()[facei])
199  && !cells.found(mesh.faceNeighbour()[facei])
200  )
201  {
202  flip = false;
203  }
204  else if
205  (
206  !cells.found(mesh.faceOwner()[facei])
207  && cells.found(mesh.faceNeighbour()[facei])
208  )
209  {
210  flip = true;
211  }
212  else
213  {
215  << "One of owner or neighbour of internal face "
216  << facei << " should be in cellSet " << cells.name()
217  << " to be able to determine orientation." << endl
218  << "Face:" << facei
219  << " own:" << mesh.faceOwner()[facei]
220  << " OwnInCellSet:"
221  << cells.found(mesh.faceOwner()[facei])
222  << " nei:" << mesh.faceNeighbour()[facei]
223  << " NeiInCellSet:"
224  << cells.found(mesh.faceNeighbour()[facei])
225  << abort(FatalError);
226  }
227  }
228  else
229  {
230  if (cells.found(mesh.faceOwner()[facei]))
231  {
232  flip = false;
233  }
234  else
235  {
236  flip = true;
237  }
238  }
239 
240  addressing.append(facei);
241  flipMap.append(flip);
242  }
243  }
244 
245  label zoneID = mesh.faceZones().findIndex(set.name());
246  if (zoneID == -1)
247  {
248  Info<< "Adding set " << set.name() << " as a faceZone." << endl;
249  label sz = mesh.faceZones().size();
250  mesh.faceZones().setSize(sz+1);
251  mesh.faceZones().set
252  (
253  sz,
254  new faceZone
255  (
256  set.name(), // name
257  addressing.shrink(), // addressing
258  flipMap.shrink(), // flipmap
259  mesh.faceZones() // faceZones
260  )
261  );
262  mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
263  mesh.faceZones().instance() = mesh.facesInstance();
264  }
265  else
266  {
267  Info<< "Overwriting contents of existing faceZone " << zoneID
268  << " with that of set " << set.name() << "." << endl;
269  mesh.faceZones()[zoneID].resetAddressing
270  (
271  addressing.shrink(),
272  flipMap.shrink()
273  );
274  mesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
275  mesh.faceZones().instance() = mesh.facesInstance();
276  }
277  }
278 
279 
280 
281  IOobjectList cellObjects(objects.lookupClass(cellSet::typeName));
282 
283  // Pout<< "cellSets:" << cellObjects.names() << endl;
284 
285  forAllConstIter(IOobjectList, cellObjects, iter)
286  {
287  if (!slaveCellSets.found(iter.key()))
288  {
289  // Not in memory. Load it.
290  cellSet set(*iter());
291  SortableList<label> cellLabels(set.toc());
292 
293  label zoneID = mesh.cellZones().findIndex(set.name());
294  if (zoneID == -1)
295  {
296  Info<< "Adding set " << set.name() << " as a cellZone." << endl;
297  label sz = mesh.cellZones().size();
298  mesh.cellZones().setSize(sz+1);
299  mesh.cellZones().set
300  (
301  sz,
302  new cellZone
303  (
304  set.name(), // name
305  cellLabels, // addressing
306  mesh.cellZones() // cellZones
307  )
308  );
309  mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
310  mesh.cellZones().instance() = mesh.facesInstance();
311  }
312  else
313  {
314  Info<< "Overwriting contents of existing cellZone " << zoneID
315  << " with that of set " << set.name() << "." << endl;
316  mesh.cellZones()[zoneID] = cellLabels;
317  mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
318  mesh.cellZones().instance() = mesh.facesInstance();
319  }
320  }
321  }
322 
323 
324 
325  Info<< "Writing mesh." << endl;
326 
327  if (!mesh.write())
328  {
330  << "Failed writing polyMesh."
331  << exit(FatalError);
332  }
333 
334  Info<< "End\n" << endl;
335 
336  return 0;
337 }
338 
339 
340 // ************************************************************************* //
#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:109
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:138
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:58
A list of face labels.
Definition: faceSet.H:51
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:59
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:59
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:270
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:334
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:257
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
error FatalError
static const char nl
Definition: Ostream.H:266
labelList pointLabels(nPoints, -1)
objects
Foam::argList args(argc, argv)