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-2018 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
84 
85  #include "createNamedPolyMesh.H"
86 
87  const fileName setsSubPath(mesh.dbDir()/polyMesh::meshSubDir/"sets");
88 
89  // Search for list of objects for the time of the mesh
90  word setsInstance = runTime.findInstance
91  (
92  setsSubPath,
93  word::null,
96  );
97 
98  IOobjectList objects(mesh, setsInstance, polyMesh::meshSubDir/"sets");
99 
100  Info<< "Searched : " << setsInstance/setsSubPath
101  << nl
102  << "Found : " << objects.names() << nl
103  << endl;
104 
105 
106  IOobjectList pointObjects(objects.lookupClass(pointSet::typeName));
107 
108  // Pout<< "pointSets:" << pointObjects.names() << endl;
109 
110  forAllConstIter(IOobjectList, pointObjects, iter)
111  {
112  // Not in memory. Load it.
113  pointSet set(*iter());
114  SortableList<label> pointLabels(set.toc());
115 
116  label zoneID = mesh.pointZones().findZoneID(set.name());
117  if (zoneID == -1)
118  {
119  Info<< "Adding set " << set.name() << " as a pointZone." << endl;
120  label sz = mesh.pointZones().size();
121  mesh.pointZones().setSize(sz+1);
123  (
124  sz,
125  new pointZone
126  (
127  set.name(), // name
128  pointLabels, // addressing
129  sz, // index
130  mesh.pointZones() // pointZoneMesh
131  )
132  );
135  }
136  else
137  {
138  Info<< "Overwriting contents of existing pointZone " << zoneID
139  << " with that of set " << set.name() << "." << endl;
140  mesh.pointZones()[zoneID] = pointLabels;
143  }
144  }
145 
146 
147 
148  IOobjectList faceObjects(objects.lookupClass(faceSet::typeName));
149 
150  HashSet<word> slaveCellSets;
151 
152  // Pout<< "faceSets:" << faceObjects.names() << endl;
153 
154  forAllConstIter(IOobjectList, faceObjects, iter)
155  {
156  // Not in memory. Load it.
157  faceSet set(*iter());
158  SortableList<label> faceLabels(set.toc());
159 
160  DynamicList<label> addressing(set.size());
161  DynamicList<bool> flipMap(set.size());
162 
163  if (noFlipMap)
164  {
165  // No flip map.
166  forAll(faceLabels, i)
167  {
168  label facei = faceLabels[i];
169  addressing.append(facei);
170  flipMap.append(false);
171  }
172  }
173  else
174  {
175  const word setName(set.name() + "SlaveCells");
176 
177  Info<< "Trying to load cellSet " << setName
178  << " to find out the slave side of the zone." << nl
179  << "If you do not care about the flipMap"
180  << " (i.e. do not use the sideness)" << nl
181  << "use the -noFlipMap command line option."
182  << endl;
183 
184  // Load corresponding cells
185  cellSet cells(mesh, setName);
186 
187  // Store setName to exclude from cellZones further on
188  slaveCellSets.insert(setName);
189 
190  forAll(faceLabels, i)
191  {
192  label facei = faceLabels[i];
193 
194  bool flip = false;
195 
196  if (mesh.isInternalFace(facei))
197  {
198  if
199  (
200  cells.found(mesh.faceOwner()[facei])
201  && !cells.found(mesh.faceNeighbour()[facei])
202  )
203  {
204  flip = false;
205  }
206  else if
207  (
208  !cells.found(mesh.faceOwner()[facei])
209  && cells.found(mesh.faceNeighbour()[facei])
210  )
211  {
212  flip = true;
213  }
214  else
215  {
217  << "One of owner or neighbour of internal face "
218  << facei << " should be in cellSet " << cells.name()
219  << " to be able to determine orientation." << endl
220  << "Face:" << facei
221  << " own:" << mesh.faceOwner()[facei]
222  << " OwnInCellSet:"
223  << cells.found(mesh.faceOwner()[facei])
224  << " nei:" << mesh.faceNeighbour()[facei]
225  << " NeiInCellSet:"
226  << cells.found(mesh.faceNeighbour()[facei])
227  << abort(FatalError);
228  }
229  }
230  else
231  {
232  if (cells.found(mesh.faceOwner()[facei]))
233  {
234  flip = false;
235  }
236  else
237  {
238  flip = true;
239  }
240  }
241 
242  addressing.append(facei);
243  flipMap.append(flip);
244  }
245  }
246 
247  label zoneID = mesh.faceZones().findZoneID(set.name());
248  if (zoneID == -1)
249  {
250  Info<< "Adding set " << set.name() << " as a faceZone." << endl;
251  label sz = mesh.faceZones().size();
252  mesh.faceZones().setSize(sz+1);
253  mesh.faceZones().set
254  (
255  sz,
256  new faceZone
257  (
258  set.name(), // name
259  addressing.shrink(), // addressing
260  flipMap.shrink(), // flipmap
261  sz, // index
262  mesh.faceZones() // pointZoneMesh
263  )
264  );
267  }
268  else
269  {
270  Info<< "Overwriting contents of existing faceZone " << zoneID
271  << " with that of set " << set.name() << "." << endl;
272  mesh.faceZones()[zoneID].resetAddressing
273  (
274  addressing.shrink(),
275  flipMap.shrink()
276  );
279  }
280  }
281 
282 
283 
284  IOobjectList cellObjects(objects.lookupClass(cellSet::typeName));
285 
286  // Pout<< "cellSets:" << cellObjects.names() << endl;
287 
288  forAllConstIter(IOobjectList, cellObjects, iter)
289  {
290  if (!slaveCellSets.found(iter.key()))
291  {
292  // Not in memory. Load it.
293  cellSet set(*iter());
294  SortableList<label> cellLabels(set.toc());
295 
296  label zoneID = mesh.cellZones().findZoneID(set.name());
297  if (zoneID == -1)
298  {
299  Info<< "Adding set " << set.name() << " as a cellZone." << endl;
300  label sz = mesh.cellZones().size();
301  mesh.cellZones().setSize(sz+1);
302  mesh.cellZones().set
303  (
304  sz,
305  new cellZone
306  (
307  set.name(), // name
308  cellLabels, // addressing
309  sz, // index
310  mesh.cellZones() // pointZoneMesh
311  )
312  );
315  }
316  else
317  {
318  Info<< "Overwriting contents of existing cellZone " << zoneID
319  << " with that of set " << set.name() << "." << endl;
320  mesh.cellZones()[zoneID] = cellLabels;
323  }
324  }
325  }
326 
327 
328 
329  Info<< "Writing mesh." << endl;
330 
331  if (!mesh.write())
332  {
334  << "Failed writing polyMesh."
335  << exit(FatalError);
336  }
337 
338  Info<< "End\n" << endl;
339 
340  return 0;
341 }
342 
343 
344 // ************************************************************************* //
A HashTable with keys but without contents.
Definition: HashSet.H:59
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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 class for handling file names.
Definition: fileName.H:79
A list of face labels.
Definition: faceSet.H:48
bool set(const label) const
Is element set.
Definition: PtrListI.H:65
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:50
const faceZoneMesh & faceZones() const
Return face zone mesh.
Definition: polyMesh.H:476
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:808
A set of point labels.
Definition: pointSet.H:48
labelList pointLabels(nPoints, -1)
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1175
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:80
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:312
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Return the location of "dir" containing the file "name".
Definition: Time.C:649
virtual const fileName & dbDir() const
Override the objectRegistry dbDir for a single-region case.
Definition: polyMesh.C:783
virtual bool write(const bool write=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1035
dynamicFvMesh & mesh
const cellShapeList & cells
label findZoneID(const word &zoneName) const
Find zone index given a name.
Definition: ZoneMesh.C:341
const pointZoneMesh & pointZones() const
Return point zone mesh.
Definition: polyMesh.H:470
A class for handling words, derived from string.
Definition: word.H:59
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:482
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1169
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:283
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
bool isInternalFace(const label faceIndex) const
Return true if given face label is internal to the mesh.
static const char nl
Definition: Ostream.H:265
objects
A subset of mesh cells.
Definition: cellZone.H:61
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
writeOption writeOpt() const
Definition: IOobject.H:355
const fileName & instance() const
Definition: IOobject.H:378
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
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:117
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:158
Foam::argList args(argc, argv)
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.