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  Copyright (C) 2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  setsToZones
29 
30 Group
31  grpMeshManipulationUtilities
32 
33 Description
34  Add pointZones/faceZones/cellZones to the mesh from similar named
35  pointSets/faceSets/cellSets.
36 
37  There is one catch: for faceZones you also need to specify a flip
38  condition which basically denotes the side of the face. In this app
39  it reads a cellSet (xxxCells if 'xxx' is the name of the faceSet) which
40  is the masterCells of the zone.
41  There are lots of situations in which this will go wrong but it is the
42  best I can think of for now.
43 
44  If one is not interested in sideNess specify the -noFlipMap
45  command line option.
46 
47 \*---------------------------------------------------------------------------*/
48 
49 #include "argList.H"
50 #include "Time.H"
51 #include "polyMesh.H"
52 #include "cellSet.H"
53 #include "faceSet.H"
54 #include "pointSet.H"
55 #include "IOobjectList.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  labelList pointLabels(set.sortedToc());
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 
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  labelList faceLabels(set.sortedToc());
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  labelList cellLabels(set.sortedToc());
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
@ AUTO_WRITE
Definition: IOobject.H:190
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Definition: BitOps.C:30
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Definition: fvMesh.C:1034
Foam::DynamicList< label >
Foam::polyMesh::dbDir
virtual const fileName & dbDir() const
Definition: polyMesh.C:822
Foam::pointZone
A subset of mesh points.
Definition: pointZone.H:61
Foam::faceZone::resetAddressing
virtual void resetAddressing(const labelUList &addr, const bool flipMapValue)
Definition: faceZone.C:437
Foam::polyMesh::meshSubDir
static word meshSubDir
Definition: polyMesh.H:317
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::polyMesh::facesInstance
const fileName & facesInstance() const
Definition: polyMesh.C:845
Foam::faceSet
A list of face labels.
Definition: faceSet.H:47
IOobjectList.H
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
polyMesh.H
Foam::HashSet
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:73
addTimeOptions.H
createNamedPolyMesh.H
Required Variables.
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
Foam::cellZone
A subset of mesh cells.
Definition: cellZone.H:58
Foam::faceZone
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:60
Foam::Info
messageStream Info
Foam::polyMesh::cellZones
const cellZoneMesh & cellZones() const noexcept
Definition: polyMesh.H:488
argList.H
Foam::polyMesh::faceOwner
virtual const labelList & faceOwner() const
Definition: polyMesh.C:1100
faceSet.H
addRegionOption.H
Foam::polyMesh::faceZones
const faceZoneMesh & faceZones() const noexcept
Definition: polyMesh.H:482
Foam::timeSelector::selectIfPresent
static instantList selectIfPresent(Time &runTime, const argList &args)
Definition: timeSelector.C:259
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam
Definition: atmBoundaryLayer.C:26
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Foam::cellSet
A collection of cell labels.
Definition: cellSet.H:47
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:51
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:790
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::zoneIdentifier::index
label index() const noexcept
Definition: zoneIdentifier.H:131
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Definition: argList.C:317
Time.H
setRootCase.H
Foam::primitiveMesh::isInternalFace
bool isInternalFace(const label faceIndex) const noexcept
Definition: primitiveMeshI.H:96
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::nl
constexpr char nl
Definition: Ostream.H:424
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::pointSet
A set of point labels.
Definition: pointSet.H:47
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Definition: timeSelector.C:95
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::HashSet::insert
bool insert(const Key &key)
Definition: HashSet.H:191
Foam::word::null
static const word null
Definition: word.H:78
timeSelector.H
Foam::polyMesh::pointZones
const pointZoneMesh & pointZones() const noexcept
Definition: polyMesh.H:476
createTime.H
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
cellSet.H
args
Foam::argList args(argc, argv)
pointLabels
labelList pointLabels(nPoints, -1)
Foam::polyMesh::faceNeighbour
virtual const labelList & faceNeighbour() const
Definition: polyMesh.C:1106
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181
pointSet.H