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-2023 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 {
65  timeSelector::addOptions_singleTime(); // Single-time options
66 
68  (
69  "Add point/face/cell Zones from similarly named point/face/cell Sets"
70  );
71 
73  (
74  "noFlipMap",
75  "Ignore orientation of faceSet"
76  );
77 
78  #include "addRegionOption.H"
79  #include "setRootCase.H"
80  #include "createTime.H"
81 
82  const bool noFlipMap = args.found("noFlipMap");
83 
84  // Allow override of time from specified time options, or no-op
86 
87  #include "createNamedPolyMesh.H"
88 
89  const fileName setsSubPath(mesh.meshDir()/"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  for (const IOobject& io : objects.csorted<pointSet>())
109  {
110  // Not in memory. Load it.
111  pointSet set(io);
113 
114  // The original number of zones
115  const label nOrigZones = mesh.pointZones().size();
116 
117  // Get existing or create new empty zone
118  pointZone& zn = mesh.pointZones()(set.name());
119 
120  if (nOrigZones == mesh.pointZones().size())
121  {
122  Info<< "Overwriting contents of existing pointZone "
123  << zn.index()
124  << " with that of set " << set.name() << "." << endl;
125  }
126  else
127  {
128  Info<< "Adding set " << set.name() << " as a pointZone." << endl;
129  }
130 
131  zn = pointLabels;
132 
135  }
136 
137 
138  wordHashSet slaveCellSets;
139 
140  for (const IOobject& io : objects.csorted<faceSet>())
141  {
142  // Not in memory. Load it.
143  faceSet set(io);
145 
146  DynamicList<label> addressing(set.size());
147  DynamicList<bool> flipMap(set.size());
148 
149  if (noFlipMap)
150  {
151  // No flip map.
152  forAll(faceLabels, i)
153  {
154  label facei = faceLabels[i];
155  addressing.append(facei);
156  flipMap.append(false);
157  }
158  }
159  else
160  {
161  const word setName(set.name() + "SlaveCells");
162 
163  Info<< "Trying to load cellSet " << setName
164  << " to find out the slave side of the zone." << nl
165  << "If you do not care about the flipMap"
166  << " (i.e. do not use the sideness)" << nl
167  << "use the -noFlipMap command line option."
168  << endl;
169 
170  // Load corresponding cells
171  cellSet cells(mesh, setName);
172 
173  // Store setName to exclude from cellZones further on
174  slaveCellSets.insert(setName);
175 
176  forAll(faceLabels, i)
177  {
178  label facei = faceLabels[i];
179 
180  bool flip = false;
181 
182  if (mesh.isInternalFace(facei))
183  {
184  if
185  (
186  cells.found(mesh.faceOwner()[facei])
187  && !cells.found(mesh.faceNeighbour()[facei])
188  )
189  {
190  flip = false;
191  }
192  else if
193  (
194  !cells.found(mesh.faceOwner()[facei])
195  && cells.found(mesh.faceNeighbour()[facei])
196  )
197  {
198  flip = true;
199  }
200  else
201  {
203  << "One of owner or neighbour of internal face "
204  << facei << " should be in cellSet " << cells.name()
205  << " to be able to determine orientation." << endl
206  << "Face:" << facei
207  << " own:" << mesh.faceOwner()[facei]
208  << " OwnInCellSet:"
209  << cells.found(mesh.faceOwner()[facei])
210  << " nei:" << mesh.faceNeighbour()[facei]
211  << " NeiInCellSet:"
212  << cells.found(mesh.faceNeighbour()[facei])
213  << abort(FatalError);
214  }
215  }
216  else
217  {
218  if (cells.found(mesh.faceOwner()[facei]))
219  {
220  flip = false;
221  }
222  else
223  {
224  flip = true;
225  }
226  }
227 
228  addressing.append(facei);
229  flipMap.append(flip);
230  }
231  }
232 
233  // The original number of zones
234  const label nOrigZones = mesh.faceZones().size();
235 
236  // Get existing or create new empty zone
237  faceZone& zn = mesh.faceZones()(set.name());
238 
239  if (nOrigZones == mesh.faceZones().size())
240  {
241  Info<< "Overwriting contents of existing faceZone "
242  << zn.index()
243  << " with that of set " << set.name() << "." << endl;
244  }
245  else
246  {
247  Info<< "Adding set " << set.name() << " as a faceZone." << endl;
248  }
249 
250  zn.resetAddressing
251  (
252  addressing.shrink(),
253  flipMap.shrink()
254  );
255 
258  }
259 
260 
261 
262  for (const IOobject& io : objects.csorted<cellSet>())
263  {
264  if (!slaveCellSets.found(io.name()))
265  {
266  // Not in memory. Load it.
267  cellSet set(io);
268  labelList cellLabels(set.sortedToc());
269 
270  // The original number of zones
271  const label nOrigZones = mesh.cellZones().size();
272 
273  cellZone& zn = mesh.cellZones()(set.name());
274 
275  if (nOrigZones == mesh.cellZones().size())
276  {
277  Info<< "Overwriting contents of existing cellZone "
278  << zn.index()
279  << " with that of set " << set.name() << "." << endl;
280  }
281  else
282  {
283  Info<< "Adding set " << set.name() << " as a cellZone." << endl;
284  }
285 
286  zn = cellLabels;
287 
290  }
291  }
292 
293 
294  Info<< "Writing mesh." << endl;
295 
296  if (!mesh.write())
297  {
299  << "Failed writing polyMesh."
300  << exit(FatalError);
301  }
302 
303  Info<< "End\n" << endl;
304 
305  return 0;
306 }
307 
308 
309 // ************************************************************************* //
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
A class for handling file names.
Definition: fileName.H:72
A list of face labels.
Definition: faceSet.H:47
List of IOobjects with searching and retrieving facilities. Implemented as a HashTable, so the various sorted methods should be used if traversing in parallel.
Definition: IOobjectList.H:55
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:859
A set of point labels.
Definition: pointSet.H:47
labelList pointLabels(nPoints, -1)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:521
bool found(const Key &key) const
Same as contains()
Definition: HashTable.H:1370
virtual const labelList & faceNeighbour() const
Return face neighbour.
Definition: polyMesh.C:1122
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:411
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool found(const T &val, label pos=0) const
Same as contains()
Definition: UList.H:888
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:374
static bool setTimeIfPresent(Time &runTime, const argList &args, const bool forceInitial=false)
Set the runTime based on -constant (if present), -time (value), or -latestTime.
Definition: timeSelector.C:314
Required Classes.
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:232
labelList faceLabels(nFaceLabels)
bool isInternalFace(const label faceIndex) const noexcept
Return true if given face label is internal to the mesh.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
Required Classes.
writeOption writeOpt() const noexcept
Get the write option.
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const cellShapeList & cells
word findInstance(const fileName &directory, const word &name=word::null, IOobjectOption::readOption rOpt=IOobjectOption::MUST_READ, const word &stopInstance=word::null, const bool constant_fallback=true) const
Return time instance (location) of directory containing the file name (eg, used in reading mesh data)...
Definition: Time.C:725
A class for handling words, derived from Foam::string.
Definition: word.H:63
static void addOptions_singleTime()
Add single-time timeSelector options to argList::validOptions()
Definition: timeSelector.C:146
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1116
static const word null
An empty word.
Definition: word.H:84
virtual void resetAddressing(faceZone &&zn)
Move reset addressing and flip map from another zone.
Definition: faceZone.C:477
errorManip< error > abort(error &err)
Definition: errorManip.H:139
virtual bool write(const bool writeOnProc=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:1113
label index() const noexcept
The index of this zone in the zone list.
const faceZoneMesh & faceZones() const noexcept
Return face zone mesh.
Definition: polyMesh.H:671
A subset of mesh cells.
Definition: cellZone.H:58
const fileName & instance() const noexcept
Read access to instance path component.
Definition: IOobjectI.H:266
const pointZoneMesh & pointZones() const noexcept
Return point zone mesh.
Definition: polyMesh.H:663
A subset of mesh points.
Definition: pointZone.H:61
A collection of cell labels.
Definition: cellSet.H:47
fileName meshDir() const
Return the local mesh directory (dbDir()/meshSubDir)
Definition: polyMesh.C:841
Automatically write from objectRegistry::writeObject()
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:679
List< label > sortedToc(const UList< bool > &bools)
Return the (sorted) values corresponding to &#39;true&#39; entries.
Definition: BitOps.C:195
messageStream Info
Information stream (stdout output on master, null elsewhere)
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:60
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Foam::argList args(argc, argv)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
Namespace for OpenFOAM.