FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 Usage
42 
43  - setsToZones [OPTIONS]
44 
45  @param -noFlipMap \n
46  No automatic face flipping.
47 
48  @param -noZero \n
49  Ignore timestep 0.
50 
51  @param -constant \n
52  Include the constant directory.
53 
54  @param -time <time>\n
55  Apply only to specific time.
56 
57  @param -latestTime \n
58  Only apply to latest time step.
59 
60  @param -case <dir>\n
61  Case directory.
62 
63  @param -parallel \n
64  Run in parallel.
65 
66  @param -help \n
67  Display help message.
68 
69  @param -doc \n
70  Display Doxygen API documentation page for this application.
71 
72  @param -srcDoc \n
73  Display Doxygen source documentation page for this application.
74 
75 \*---------------------------------------------------------------------------*/
76 
77 #include <OpenFOAM/argList.H>
78 #include <OpenFOAM/Time.H>
79 #include <OpenFOAM/polyMesh.H>
80 #include <OpenFOAM/IStringStream.H>
81 #include <meshTools/cellSet.H>
82 #include <meshTools/faceSet.H>
83 #include <meshTools/pointSet.H>
84 #include <OpenFOAM/OFstream.H>
85 #include <OpenFOAM/IFstream.H>
86 #include <OpenFOAM/IOobjectList.H>
87 #include <OpenFOAM/SortableList.H>
88 
89 using namespace Foam;
90 
91 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
92 
93 // Main program:
94 
95 int main(int argc, char *argv[])
96 {
97  argList::validOptions.insert("noFlipMap", "");
98 
100 # include <OpenFOAM/addTimeOptions.H>
101 # include <OpenFOAM/setRootCase.H>
102 # include <OpenFOAM/createTime.H>
103 
104  bool noFlipMap = args.optionFound("noFlipMap");
105 
106  // Get times list
107  instantList Times = runTime.times();
108 
109  label startTime = Times.size()-1;
110  label endTime = Times.size();
111 
112  // check -time and -latestTime options
113 # include <OpenFOAM/checkTimeOption.H>
114 
115  runTime.setTime(Times[startTime], startTime);
116 
118 
119  // Search for list of objects for the time of the mesh
120  IOobjectList objects
121  (
122  mesh,
124  polyMesh::meshSubDir/"sets"
125  );
126 
127  Info<< "Searched : " << mesh.pointsInstance()/polyMesh::meshSubDir/"sets"
128  << nl
129  << "Found : " << objects.names() << nl
130  << endl;
131 
132 
133  IOobjectList pointObjects(objects.lookupClass(pointSet::typeName));
134 
135  //Pout<< "pointSets:" << pointObjects.names() << endl;
136 
137  for
138  (
139  IOobjectList::const_iterator iter = pointObjects.begin();
140  iter != pointObjects.end();
141  ++iter
142  )
143  {
144  // Not in memory. Load it.
145  pointSet set(*iter());
146  SortableList<label> pointLabels(set.toc());
147 
148  label zoneID = mesh.pointZones().findZoneID(set.name());
149  if (zoneID == -1)
150  {
151  Info<< "Adding set " << set.name() << " as a pointZone." << endl;
152  label sz = mesh.pointZones().size();
153  mesh.pointZones().setSize(sz+1);
155  (
156  sz,
157  new pointZone
158  (
159  set.name(), //name
160  pointLabels, //addressing
161  sz, //index
162  mesh.pointZones() //pointZoneMesh
163  )
164  );
167  }
168  else
169  {
170  Info<< "Overwriting contents of existing pointZone " << zoneID
171  << " with that of set " << set.name() << "." << endl;
172  mesh.pointZones()[zoneID] = pointLabels;
175  }
176  }
177 
178 
179 
180  IOobjectList faceObjects(objects.lookupClass(faceSet::typeName));
181 
182  HashSet<word> slaveCellSets;
183 
184  //Pout<< "faceSets:" << faceObjects.names() << endl;
185 
186  for
187  (
188  IOobjectList::const_iterator iter = faceObjects.begin();
189  iter != faceObjects.end();
190  ++iter
191  )
192  {
193  // Not in memory. Load it.
194  faceSet set(*iter());
195  SortableList<label> faceLabels(set.toc());
196 
197  DynamicList<label> addressing(set.size());
198  DynamicList<bool> flipMap(set.size());
199 
200  if (!noFlipMap)
201  {
202  word setName(set.name() + "SlaveCells");
203 
204  Info<< "Trying to load cellSet " << setName
205  << " to find out the slave side of the zone." << nl
206  << "If you do not care about the flipMap"
207  << " (i.e. do not use the sideness)" << nl
208  << "use the -noFlipMap command line option."
209  << endl;
210 
211  // Load corresponding cells
212  cellSet cells(mesh, setName);
213 
214  // Store setName to exclude from cellZones further on
215  slaveCellSets.insert(setName);
216 
217  forAll(faceLabels, i)
218  {
219  label faceI = faceLabels[i];
220 
221  bool flip = false;
222 
223  if (mesh.isInternalFace(faceI))
224  {
225  if
226  (
227  cells.found(mesh.faceOwner()[faceI])
228  && !cells.found(mesh.faceNeighbour()[faceI])
229  )
230  {
231  flip = false;
232  }
233  else if
234  (
235  !cells.found(mesh.faceOwner()[faceI])
236  && cells.found(mesh.faceNeighbour()[faceI])
237  )
238  {
239  flip = true;
240  }
241  else
242  {
244  << "One of owner or neighbour of internal face "
245  << faceI << " should be in cellSet " << cells.name()
246  << " to be able to determine orientation." << endl
247  << "Face:" << faceI
248  << " own:" << mesh.faceOwner()[faceI]
249  << " OwnInCellSet:"
250  << cells.found(mesh.faceOwner()[faceI])
251  << " nei:" << mesh.faceNeighbour()[faceI]
252  << " NeiInCellSet:"
253  << cells.found(mesh.faceNeighbour()[faceI])
254  << abort(FatalError);
255  }
256  }
257  else
258  {
259  if (cells.found(mesh.faceOwner()[faceI]))
260  {
261  flip = false;
262  }
263  else
264  {
265  flip = true;
266  }
267  }
268 
269  addressing.append(faceI);
270  flipMap.append(flip);
271  }
272  }
273  else
274  {
275  // No flip map.
276  forAll(faceLabels, i)
277  {
278  label faceI = faceLabels[i];
279  addressing.append(faceI);
280  flipMap.append(false);
281  }
282  }
283 
284  label zoneID = mesh.faceZones().findZoneID(set.name());
285  if (zoneID == -1)
286  {
287  Info<< "Adding set " << set.name() << " as a faceZone." << endl;
288  label sz = mesh.faceZones().size();
289  mesh.faceZones().setSize(sz+1);
290  mesh.faceZones().set
291  (
292  sz,
293  new faceZone
294  (
295  set.name(), //name
296  addressing.shrink(), //addressing
297  flipMap.shrink(), //flipmap
298  sz, //index
299  mesh.faceZones() //pointZoneMesh
300  )
301  );
304  }
305  else
306  {
307  Info<< "Overwriting contents of existing faceZone " << zoneID
308  << " with that of set " << set.name() << "." << endl;
309  mesh.faceZones()[zoneID].resetAddressing
310  (
311  addressing.shrink(),
312  flipMap.shrink()
313  );
316  }
317  }
318 
319 
320 
321  IOobjectList cellObjects(objects.lookupClass(cellSet::typeName));
322 
323  //Pout<< "cellSets:" << cellObjects.names() << endl;
324 
325  for
326  (
327  IOobjectList::const_iterator iter = cellObjects.begin();
328  iter != cellObjects.end();
329  ++iter
330  )
331  {
332  if (!slaveCellSets.found(iter.key()))
333  {
334  // Not in memory. Load it.
335  cellSet set(*iter());
336  SortableList<label> cellLabels(set.toc());
337 
338  label zoneID = mesh.cellZones().findZoneID(set.name());
339  if (zoneID == -1)
340  {
341  Info<< "Adding set " << set.name() << " as a cellZone." << endl;
342  label sz = mesh.cellZones().size();
343  mesh.cellZones().setSize(sz+1);
344  mesh.cellZones().set
345  (
346  sz,
347  new cellZone
348  (
349  set.name(), //name
350  cellLabels, //addressing
351  sz, //index
352  mesh.cellZones() //pointZoneMesh
353  )
354  );
357  }
358  else
359  {
360  Info<< "Overwriting contents of existing cellZone " << zoneID
361  << " with that of set " << set.name() << "." << endl;
362  mesh.cellZones()[zoneID] = cellLabels;
365  }
366  }
367  }
368 
369 
370 
371  Info<< "Writing mesh." << endl;
372 
373  if (!mesh.write())
374  {
376  << "Failed writing polyMesh."
377  << exit(FatalError);
378  }
379 
380  Info<< nl << "End" << endl;
381 
382  return 0;
383 }
384 
385 
386 // ************************ vim: set sw=4 sts=4 et: ************************ //