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