topoSet.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) 2018-2022 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  topoSet
29 
30 Group
31  grpMeshManipulationUtilities
32 
33 Description
34  Operates on cellSets/faceSets/pointSets through a dictionary,
35  normally system/topoSetDict
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #include "argList.H"
40 #include "Time.H"
41 #include "polyMesh.H"
42 #include "topoSetSource.H"
43 #include "globalMeshData.H"
44 #include "timeSelector.H"
45 #include "IOobjectList.H"
46 #include "cellZoneSet.H"
47 #include "faceZoneSet.H"
48 #include "pointZoneSet.H"
49 #include "IOdictionary.H"
50 #include "namedDictionary.H"
51 
52 using namespace Foam;
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 void printMesh(const Time& runTime, const polyMesh& mesh)
57 {
58  Info<< "Time:" << runTime.timeName()
59  << " cells:" << mesh.globalData().nTotalCells()
60  << " faces:" << mesh.globalData().nTotalFaces()
61  << " points:" << mesh.globalData().nTotalPoints()
62  << " patches:" << mesh.boundaryMesh().size()
63  << " bb:" << mesh.bounds() << nl;
64 }
65 
66 
67 template<class ZoneType>
68 void removeZone
69 (
71  const word& setName
72 )
73 {
74  label zoneID = zones.findZoneID(setName);
75 
76  if (zoneID != -1)
77  {
78  Info<< "Removing zone " << setName << " at index " << zoneID << endl;
79  // Shuffle to last position
80  labelList oldToNew(zones.size());
81  label newI = 0;
82  forAll(oldToNew, i)
83  {
84  if (i != zoneID)
85  {
86  oldToNew[i] = newI++;
87  }
88  }
89  oldToNew[zoneID] = newI;
90  zones.reorder(oldToNew);
91  // Remove last element
92  zones.setSize(zones.size()-1);
93  zones.clearAddressing();
94  if (!zones.write())
95  {
96  WarningInFunction << "Failed writing zone " << setName << endl;
97  }
98  fileHandler().flush();
99  }
100 }
101 
102 
103 // Physically remove a set
104 void removeSet
105 (
106  const polyMesh& mesh,
107  const word& setType,
108  const word& setName
109 )
110 {
111  // Remove the file
112  IOobjectList objects
113  (
114  mesh,
116  (
117  polyMesh::meshSubDir/"sets",
118  word::null,
121  ),
122  polyMesh::meshSubDir/"sets"
123  );
124 
125  if (objects.found(setName))
126  {
127  // Remove file
128  fileName object = objects[setName]->objectPath();
129  Info<< "Removing file " << object << endl;
130  rm(object);
131  }
132 
133  // See if zone
134  if (setType == cellZoneSet::typeName)
135  {
136  removeZone
137  (
138  const_cast<cellZoneMesh&>(mesh.cellZones()),
139  setName
140  );
141  }
142  else if (setType == faceZoneSet::typeName)
143  {
144  removeZone
145  (
146  const_cast<faceZoneMesh&>(mesh.faceZones()),
147  setName
148  );
149  }
150  else if (setType == pointZoneSet::typeName)
151  {
152  removeZone
153  (
154  const_cast<pointZoneMesh&>(mesh.pointZones()),
155  setName
156  );
157  }
158 }
159 
160 
161 polyMesh::readUpdateState meshReadUpdate(polyMesh& mesh)
162 {
164 
165  switch(stat)
166  {
167  case polyMesh::UNCHANGED:
168  {
169  Info<< " mesh not changed." << endl;
170  break;
171  }
173  {
174  Info<< " points moved; topology unchanged." << endl;
175  break;
176  }
178  {
179  Info<< " topology changed; patches unchanged." << nl
180  << " ";
181  printMesh(mesh.time(), mesh);
182  break;
183  }
185  {
186  Info<< " topology changed and patches changed." << nl
187  << " ";
188  printMesh(mesh.time(), mesh);
189 
190  break;
191  }
192  default:
193  {
195  << "Illegal mesh update state "
196  << stat << abort(FatalError);
197  break;
198  }
199  }
200  return stat;
201 }
202 
203 
204 
205 int main(int argc, char *argv[])
206 {
208  (
209  "Operates on cellSets/faceSets/pointSets through a dictionary,"
210  " normally system/topoSetDict"
211  );
212 
213  timeSelector::addOptions(true, false); // constant(true), zero(false)
214 
215  argList::addOption("dict", "file", "Alternative topoSetDict");
216 
217  #include "addRegionOption.H"
219  (
220  "noSync",
221  "Do not synchronise selection across coupled patches"
222  );
223 
224  #include "setRootCase.H"
225  #include "createTime.H"
226 
228 
229  #include "createNamedPolyMesh.H"
230 
231  const bool noSync = args.found("noSync");
232 
233  const word dictName("topoSetDict");
234  #include "setSystemMeshDictionaryIO.H"
235 
236  Info<< "Reading " << dictIO.name() << nl << endl;
237 
238  IOdictionary topoSetDict(dictIO);
239 
240  // Read set construct info from dictionary
241  List<namedDictionary> actionEntries(topoSetDict.lookup("actions"));
242 
243  forAll(timeDirs, timei)
244  {
245  runTime.setTime(timeDirs[timei], timei);
246  Info<< "Time = " << runTime.timeName() << endl;
247 
248  // Optionally re-read mesh
249  meshReadUpdate(mesh);
250 
251  // Execute all actions
252  for (const namedDictionary& actionEntry : actionEntries)
253  {
254  const dictionary& dict = actionEntry.dict();
255  if (dict.empty())
256  {
257  continue;
258  }
259  const word setName(dict.get<word>("name"));
260  const word setType(dict.get<word>("type"));
261 
262  const topoSetSource::setAction action =
264 
265  autoPtr<topoSet> currentSet;
266 
267  switch (action)
268  {
269  case topoSetSource::NEW :
270  case topoSetSource::CLEAR :
271  {
272  currentSet = topoSet::New(setType, mesh, setName, 16384);
273  Info<< "Created "
274  << currentSet().type() << ' ' << setName << endl;
275  break;
276  }
277 
278  case topoSetSource::IGNORE :
279  continue; // Nothing to do
280  break;
281 
282  case topoSetSource::REMOVE :
283  // Nothing to load
284  break;
285 
286  default:
287  {
288  // Load set
289  currentSet = topoSet::New
290  (
291  setType,
292  mesh,
293  setName,
295  );
296 
297  Info<< "Read set "
298  << currentSet().type() << ' ' << setName
299  << " size:"
300  << returnReduce(currentSet().size(), sumOp<label>())
301  << endl;
302  }
303  }
304 
305  // Handle special actions (clear, invert) locally,
306  // the other actions through sources.
307  switch (action)
308  {
309  case topoSetSource::NEW :
310  case topoSetSource::ADD :
312  {
313  const word sourceType(dict.get<word>("source"));
314 
315  Info<< " Applying source " << sourceType << endl;
317  (
318  sourceType,
319  mesh,
320  dict.optionalSubDict("sourceInfo")
321  );
322 
323  source().applyToSet(action, currentSet());
324  // Synchronize for coupled patches.
325  if (!noSync) currentSet().sync(mesh);
326  if (!currentSet().write())
327  {
329  << "Failed writing set "
330  << currentSet().objectPath() << endl;
331  }
332  fileHandler().flush();
333  break;
334  }
335 
336  case topoSetSource::SUBSET :
337  {
338  const word sourceType(dict.get<word>("source"));
339 
340  Info<< " Applying source " << sourceType << endl;
342  (
343  sourceType,
344  mesh,
345  dict.optionalSubDict("sourceInfo")
346  );
347 
348  // Backup current set.
349  autoPtr<topoSet> oldSet
350  (
352  (
353  setType,
354  mesh,
355  currentSet().name() + "_old2",
356  currentSet()
357  )
358  );
359 
360  currentSet().clear();
361  source().applyToSet(topoSetSource::NEW, currentSet());
362 
363  // Combine new value of currentSet with old one.
364  currentSet().subset(oldSet());
365  // Synchronize for coupled patches.
366  if (!noSync) currentSet().sync(mesh);
367  if (!currentSet().write())
368  {
370  << "Failed writing set "
371  << currentSet().objectPath() << endl;
372  }
373  fileHandler().flush();
374 
375  break;
376  }
377 
378  case topoSetSource::CLEAR :
379  {
380  Info<< " Clearing " << currentSet().type() << endl;
381  currentSet().clear();
382  if (!currentSet().write())
383  {
385  << "Failed writing set "
386  << currentSet().objectPath() << endl;
387  }
388  fileHandler().flush();
389 
390  break;
391  }
392 
393  case topoSetSource::INVERT :
394  {
395  Info<< " Inverting " << currentSet().type() << endl;
396  currentSet().invert(currentSet().maxSize(mesh));
397  if (!currentSet().write())
398  {
400  << "Failed writing set "
401  << currentSet().objectPath() << endl;
402  }
403  fileHandler().flush();
404 
405  break;
406  }
407 
408  case topoSetSource::REMOVE :
409  {
410  Info<< " Removing set" << endl;
411  removeSet(mesh, setType, setName);
412 
413  break;
414  }
415 
416  default:
418  << "Unhandled action: "
419  << topoSetSource::actionNames[action] << endl;
420  }
421 
422  if (currentSet)
423  {
424  Info<< " "
425  << currentSet().type() << ' '
426  << currentSet().name() << " now size "
427  << returnReduce(currentSet().size(), sumOp<label>())
428  << endl;
429  }
430  }
431  }
432 
433  Info<< "\nEnd\n" << endl;
434 
435  return 0;
436 }
437 
438 
439 // ************************************************************************* //
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:68
dictionary dict
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:905
static const Enum< setAction > actionNames
The setActions enum text. Names: "new", add", "subtract", "subset", "invert", "clear", "remove", "list", "ignore".
A class for handling file names.
Definition: fileName.H:72
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
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:859
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
Create a new set and ADD elements to it.
Add elements to current set.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Clear the set, possibly creating it.
const word dictName("faMeshDefinition")
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
A list of mesh zones.
void reorder(const labelUList &oldToNew, const bool check=false)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: UPtrList.C:79
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:255
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
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
Invert the elements in the current set.
Required Classes.
Remove the set (from the file system)
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:360
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
label nTotalPoints() const noexcept
Total global number of mesh points. Not compensated for duplicate points!
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
const dictionary & optionalSubDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary, otherwise return this dictionary.
Definition: dictionary.C:560
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Required Classes.
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:715
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
label findZoneID(const word &zoneName) const
Find zone index by name, return -1 if not found.
Definition: ZoneMesh.C:718
const polyBoundaryMesh & boundaryMesh() const noexcept
Return boundary mesh.
Definition: polyMesh.H:609
A class for handling words, derived from Foam::string.
Definition: word.H:63
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
Union of elements with current set.
setAction
Enumeration defining various actions.
Reading is optional [identical to LAZY_READ].
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
static const word null
An empty word.
Definition: word.H:84
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:385
const globalMeshData & globalData() const
Return parallel info (demand-driven)
Definition: polyMesh.C:1311
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times - as per select0() - otherwise return just the cu...
Definition: timeSelector.C:291
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:902
errorManip< error > abort(error &err)
Definition: errorManip.H:139
static autoPtr< topoSet > New(const word &setType, const polyMesh &mesh, const word &name, IOobjectOption::readOption rOpt=IOobjectOption::MUST_READ, IOobjectOption::writeOption wOpt=IOobjectOption::NO_WRITE)
Return a pointer to a toposet read from file.
Definition: topoSet.C:48
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
const faceZoneMesh & faceZones() const noexcept
Return face zone mesh.
Definition: polyMesh.H:671
void setSize(const label newLen)
Same as resize()
Definition: PtrList.H:337
label nTotalCells() const noexcept
Total global number of mesh cells.
static autoPtr< topoSetSource > New(const word &topoSetSourceType, const polyMesh &mesh, const dictionary &dict)
Return a reference to the selected topoSetSource.
const pointZoneMesh & pointZones() const noexcept
Return point zone mesh.
Definition: polyMesh.H:663
Subtract elements from current set.
#define WarningInFunction
Report a warning using Foam::Warning.
A tuple of keyType and dictionary, which can be used when reading named or unnamed dictionary entries...
const cellZoneMesh & cellZones() const noexcept
Return cell zone mesh.
Definition: polyMesh.H:679
messageStream Info
Information stream (stdout output on master, null elsewhere)
label nTotalFaces() const noexcept
Total global number of mesh faces. Not compensated for duplicate faces!
const boundBox & bounds() const noexcept
Return mesh bounding box.
Definition: polyMesh.H:617
IOobject dictIO
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:91
"ignore" no-op action
Foam::argList args(argc, argv)
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:101
Namespace for OpenFOAM.
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: POSIX.C:1404