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 | Copyright (C) 2011-2016 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 "Time.H"
45 #include "polyMesh.H"
46 #include "IStringStream.H"
47 #include "cellSet.H"
48 #include "faceSet.H"
49 #include "pointSet.H"
50 #include "OFstream.H"
51 #include "IFstream.H"
52 #include "IOobjectList.H"
53 #include "SortableList.H"
54 #include "timeSelector.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 "addTimeOptions.H"
77  #include "setRootCase.H"
78  #include "createTime.H"
79 
80  const bool noFlipMap = args.optionFound("noFlipMap");
81 
82  // Get times list
83  (void)timeSelector::selectIfPresent(runTime, args);
84 
85  #include "createNamedPolyMesh.H"
86 
87  // Search for list of objects for the time of the mesh
88  word setsInstance = runTime.findInstance
89  (
90  polyMesh::meshSubDir/"sets",
91  word::null,
94  );
95 
96  IOobjectList objects(mesh, setsInstance, polyMesh::meshSubDir/"sets");
97 
98  Info<< "Searched : " << setsInstance/polyMesh::meshSubDir/"sets"
99  << nl
100  << "Found : " << objects.names() << nl
101  << endl;
102 
103 
104  IOobjectList pointObjects(objects.lookupClass(pointSet::typeName));
105 
106  //Pout<< "pointSets:" << pointObjects.names() << endl;
107 
108  forAllConstIter(IOobjectList, pointObjects, iter)
109  {
110  // Not in memory. Load it.
111  pointSet set(*iter());
112  SortableList<label> pointLabels(set.toc());
113 
114  label zoneID = mesh.pointZones().findZoneID(set.name());
115  if (zoneID == -1)
116  {
117  Info<< "Adding set " << set.name() << " as a pointZone." << endl;
118  label sz = mesh.pointZones().size();
119  mesh.pointZones().setSize(sz+1);
121  (
122  sz,
123  new pointZone
124  (
125  set.name(), //name
126  pointLabels, //addressing
127  sz, //index
128  mesh.pointZones() //pointZoneMesh
129  )
130  );
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;
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().findZoneID(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  sz, //index
260  mesh.faceZones() //pointZoneMesh
261  )
262  );
265  }
266  else
267  {
268  Info<< "Overwriting contents of existing faceZone " << zoneID
269  << " with that of set " << set.name() << "." << endl;
270  mesh.faceZones()[zoneID].resetAddressing
271  (
272  addressing.shrink(),
273  flipMap.shrink()
274  );
277  }
278  }
279 
280 
281 
282  IOobjectList cellObjects(objects.lookupClass(cellSet::typeName));
283 
284  //Pout<< "cellSets:" << cellObjects.names() << endl;
285 
286  forAllConstIter(IOobjectList, cellObjects, iter)
287  {
288  if (!slaveCellSets.found(iter.key()))
289  {
290  // Not in memory. Load it.
291  cellSet set(*iter());
292  SortableList<label> cellLabels(set.toc());
293 
294  label zoneID = mesh.cellZones().findZoneID(set.name());
295  if (zoneID == -1)
296  {
297  Info<< "Adding set " << set.name() << " as a cellZone." << endl;
298  label sz = mesh.cellZones().size();
299  mesh.cellZones().setSize(sz+1);
300  mesh.cellZones().set
301  (
302  sz,
303  new cellZone
304  (
305  set.name(), //name
306  cellLabels, //addressing
307  sz, //index
308  mesh.cellZones() //pointZoneMesh
309  )
310  );
313  }
314  else
315  {
316  Info<< "Overwriting contents of existing cellZone " << zoneID
317  << " with that of set " << set.name() << "." << endl;
318  mesh.cellZones()[zoneID] = cellLabels;
321  }
322  }
323  }
324 
325 
326 
327  Info<< "Writing mesh." << endl;
328 
329  if (!mesh.write())
330  {
332  << "Failed writing polyMesh."
333  << exit(FatalError);
334  }
335 
336  Info<< "End\n" << endl;
337 
338  return 0;
339 }
340 
341 
342 // ************************************************************************* //
A HashTable with keys but without contents.
Definition: HashSet.H:59
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
A list of face labels.
Definition: faceSet.H:48
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:50
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A set of point labels.
Definition: pointSet.H:48
error FatalError
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:778
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:68
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:309
labelList pointLabels(nPoints,-1)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool isInternalFace(const label faceIndex) const
Return true if given face label is internal to the mesh.
writeOption writeOpt() const
Definition: IOobject.H:314
label findZoneID(const word &zoneName) const
Find zone index given a name.
Definition: ZoneMesh.C:341
dynamicFvMesh & mesh
const cellShapeList & cells
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
A class for handling words, derived from string.
Definition: word.H:59
bool set(const label) const
Is element set.
Definition: PtrListI.H:65
static const word null
An empty word.
Definition: word.H:77
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:284
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
static const char nl
Definition: Ostream.H:262
const pointZoneMesh & pointZones() const
Return point zone mesh.
Definition: polyMesh.H:457
A subset of mesh cells.
Definition: cellZone.H:61
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:469
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1023
A subset of mesh points. The labels of points in the zone can be obtained from the addressing() list...
Definition: pointZone.H:62
A collection of cell labels.
Definition: cellSet.H:48
messageStream Info
const faceZoneMesh & faceZones() const
Return face zone mesh.
Definition: polyMesh.H:463
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:83
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:64
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:124
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1017
Foam::argList args(argc, argv)
const fileName & instance() const
Definition: IOobject.H:337
virtual bool write() const
Write mesh using IO settings from time.
Definition: fvMesh.C:870
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
Namespace for OpenFOAM.
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29