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 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 
51 using namespace Foam;
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 void printMesh(const Time& runTime, const polyMesh& mesh)
56 {
57  Info<< "Time:" << runTime.timeName()
58  << " cells:" << mesh.globalData().nTotalCells()
59  << " faces:" << mesh.globalData().nTotalFaces()
60  << " points:" << mesh.globalData().nTotalPoints()
61  << " patches:" << mesh.boundaryMesh().size()
62  << " bb:" << mesh.bounds() << nl;
63 }
64 
65 
66 template<class ZoneType>
67 void removeZone
68 (
70  const word& setName
71 )
72 {
73  label zoneID = zones.findZoneID(setName);
74 
75  if (zoneID != -1)
76  {
77  Info<< "Removing zone " << setName << " at index " << zoneID << endl;
78  // Shuffle to last position
79  labelList oldToNew(zones.size());
80  label newI = 0;
81  forAll(oldToNew, i)
82  {
83  if (i != zoneID)
84  {
85  oldToNew[i] = newI++;
86  }
87  }
88  oldToNew[zoneID] = newI;
89  zones.reorder(oldToNew);
90  // Remove last element
91  zones.setSize(zones.size()-1);
92  zones.clearAddressing();
93  if (!zones.write())
94  {
95  WarningInFunction << "Failed writing zone " << setName << endl;
96  }
97  fileHandler().flush();
98  }
99 }
100 
101 
102 // Physically remove a set
103 void removeSet
104 (
105  const polyMesh& mesh,
106  const word& setType,
107  const word& setName
108 )
109 {
110  // Remove the file
111  IOobjectList objects
112  (
113  mesh,
115  (
116  polyMesh::meshSubDir/"sets",
117  word::null,
120  ),
121  polyMesh::meshSubDir/"sets"
122  );
123 
124  if (objects.found(setName))
125  {
126  // Remove file
127  fileName object = objects[setName]->objectPath();
128  Info<< "Removing file " << object << endl;
129  rm(object);
130  }
131 
132  // See if zone
133  if (setType == cellZoneSet::typeName)
134  {
135  removeZone
136  (
137  const_cast<cellZoneMesh&>(mesh.cellZones()),
138  setName
139  );
140  }
141  else if (setType == faceZoneSet::typeName)
142  {
143  removeZone
144  (
145  const_cast<faceZoneMesh&>(mesh.faceZones()),
146  setName
147  );
148  }
149  else if (setType == pointZoneSet::typeName)
150  {
151  removeZone
152  (
153  const_cast<pointZoneMesh&>(mesh.pointZones()),
154  setName
155  );
156  }
157 }
158 
159 
160 polyMesh::readUpdateState meshReadUpdate(polyMesh& mesh)
161 {
163 
164  switch(stat)
165  {
166  case polyMesh::UNCHANGED:
167  {
168  Info<< " mesh not changed." << endl;
169  break;
170  }
172  {
173  Info<< " points moved; topology unchanged." << endl;
174  break;
175  }
177  {
178  Info<< " topology changed; patches unchanged." << nl
179  << " ";
180  printMesh(mesh.time(), mesh);
181  break;
182  }
184  {
185  Info<< " topology changed and patches changed." << nl
186  << " ";
187  printMesh(mesh.time(), mesh);
188 
189  break;
190  }
191  default:
192  {
194  << "Illegal mesh update state "
195  << stat << abort(FatalError);
196  break;
197  }
198  }
199  return stat;
200 }
201 
202 
203 
204 int main(int argc, char *argv[])
205 {
207  (
208  "Operates on cellSets/faceSets/pointSets through a dictionary,"
209  " normally system/topoSetDict"
210  );
211 
212  timeSelector::addOptions(true, false); // constant(true), zero(false)
213 
214  argList::addOption("dict", "file", "Alternative topoSetDict");
215 
216  #include "addRegionOption.H"
218  (
219  "noSync",
220  "Do not synchronise selection across coupled patches"
221  );
222 
223  #include "setRootCase.H"
224  #include "createTime.H"
225 
227 
228  #include "createNamedPolyMesh.H"
229 
230  const bool noSync = args.found("noSync");
231 
232  const word dictName("topoSetDict");
233  #include "setSystemMeshDictionaryIO.H"
234 
235  Info<< "Reading " << dictIO.name() << nl << endl;
236 
237  IOdictionary topoSetDict(dictIO);
238 
239  // Read set construct info from dictionary
240  PtrList<dictionary> actions(topoSetDict.lookup("actions"));
241 
242  forAll(timeDirs, timeI)
243  {
244  runTime.setTime(timeDirs[timeI], timeI);
245  Info<< "Time = " << runTime.timeName() << endl;
246 
247  // Optionally re-read mesh
248  meshReadUpdate(mesh);
249 
250  // Execute all actions
251  for (const dictionary& dict : actions)
252  {
253  const word setName(dict.get<word>("name"));
254  const word setType(dict.get<word>("type"));
255 
256  const topoSetSource::setAction action =
258 
259  autoPtr<topoSet> currentSet;
260  if
261  (
262  action == topoSetSource::NEW
263  || action == topoSetSource::CLEAR
264  )
265  {
266  currentSet = topoSet::New(setType, mesh, setName, 16384);
267  Info<< "Created " << currentSet().type() << ' '
268  << setName << endl;
269  }
270  else if (action == topoSetSource::REMOVE)
271  {
272  //?
273  }
274  else
275  {
276  currentSet = topoSet::New
277  (
278  setType,
279  mesh,
280  setName,
282  );
283  Info<< "Read set " << currentSet().type() << ' '
284  << setName << " with size "
285  << returnReduce(currentSet().size(), sumOp<label>())
286  << endl;
287  }
288 
289 
290  // Handle special actions (clear, invert) locally,
291  // the other actions through sources.
292  switch (action)
293  {
294  case topoSetSource::NEW:
295  case topoSetSource::ADD:
297  {
298  const word sourceType(dict.get<word>("source"));
299 
300  Info<< " Applying source " << sourceType << endl;
302  (
303  sourceType,
304  mesh,
305  dict.optionalSubDict("sourceInfo")
306  );
307 
308  source().applyToSet(action, currentSet());
309  // Synchronize for coupled patches.
310  if (!noSync) currentSet().sync(mesh);
311  if (!currentSet().write())
312  {
314  << "Failed writing set "
315  << currentSet().objectPath() << endl;
316  }
317  fileHandler().flush();
318  }
319  break;
320 
322  {
323  const word sourceType(dict.get<word>("source"));
324 
325  Info<< " Applying source " << sourceType << endl;
327  (
328  sourceType,
329  mesh,
330  dict.optionalSubDict("sourceInfo")
331  );
332 
333  // Backup current set.
334  autoPtr<topoSet> oldSet
335  (
337  (
338  setType,
339  mesh,
340  currentSet().name() + "_old2",
341  currentSet()
342  )
343  );
344 
345  currentSet().clear();
346  source().applyToSet(topoSetSource::NEW, currentSet());
347 
348  // Combine new value of currentSet with old one.
349  currentSet().subset(oldSet());
350  // Synchronize for coupled patches.
351  if (!noSync) currentSet().sync(mesh);
352  if (!currentSet().write())
353  {
355  << "Failed writing set "
356  << currentSet().objectPath() << endl;
357  }
358  fileHandler().flush();
359  }
360  break;
361 
363  Info<< " Clearing " << currentSet().type() << endl;
364  currentSet().clear();
365  if (!currentSet().write())
366  {
368  << "Failed writing set "
369  << currentSet().objectPath() << endl;
370  }
371  fileHandler().flush();
372  break;
373 
375  Info<< " Inverting " << currentSet().type() << endl;
376  currentSet().invert(currentSet().maxSize(mesh));
377  if (!currentSet().write())
378  {
380  << "Failed writing set "
381  << currentSet().objectPath() << endl;
382  }
383  fileHandler().flush();
384  break;
385 
387  Info<< " Removing set" << endl;
388  removeSet(mesh, setType, setName);
389  break;
390 
391  default:
393  << "Unhandled action " << action << endl;
394  break;
395  }
396 
397  if (currentSet.valid())
398  {
399  Info<< " "
400  << currentSet().type() << ' '
401  << currentSet().name() << " now size "
402  << returnReduce(currentSet().size(), sumOp<label>())
403  << endl;
404  }
405  }
406  }
407 
408  Info<< "\nEnd\n" << endl;
409 
410  return 0;
411 }
412 
413 
414 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::topoSetSource::ADD
Add elements to the set.
Definition: topoSetSource.H:101
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::topoSetSource::CLEAR
Clear the set, possibly creating it.
Definition: topoSetSource.H:105
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
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::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::polyMesh::POINTS_MOVED
Definition: polyMesh.H:94
faceZoneSet.H
globalMeshData.H
Foam::globalMeshData::nTotalPoints
label nTotalPoints() const
Return total number of points in decomposed mesh. Not.
Definition: globalMeshData.H:381
Foam::polyMesh::meshSubDir
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:315
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:785
Foam::fileOperation::flush
virtual void flush() const
Forcibly wait until all output done. Flush any cached data.
Definition: fileOperation.C:977
Foam::globalMeshData::nTotalCells
label nTotalCells() const
Return total number of cells in decomposed mesh.
Definition: globalMeshData.H:394
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:413
dictName
const word dictName("blockMeshDict")
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::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:994
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1170
Foam::autoPtr::valid
bool valid() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:148
IOobjectList.H
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:435
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::topoSetSource::setAction
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:99
Foam::globalMeshData::nTotalFaces
label nTotalFaces() const
Return total number of faces in decomposed mesh. Not.
Definition: globalMeshData.H:388
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
Foam::topoSet::New
static autoPtr< topoSet > New(const word &setType, const polyMesh &mesh, const word &name, readOption r=MUST_READ, writeOption w=NO_WRITE)
Return a pointer to a toposet read from file.
Definition: topoSet.C:54
Foam::topoSetSource::NEW
Create a new set and ADD elements to it.
Definition: topoSetSource.H:106
Foam::Enum::get
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:86
polyMesh.H
setSystemMeshDictionaryIO.H
createNamedPolyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::sumOp
Definition: ops.H:213
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::polyMesh::faceZones
const faceZoneMesh & faceZones() const
Return face zone mesh.
Definition: polyMesh.H:477
Foam::fvMesh::readUpdate
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:519
Foam::polyMesh::TOPO_PATCH_CHANGE
Definition: polyMesh.H:96
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
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
argList.H
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
addRegionOption.H
Foam::ZoneMesh
A list of mesh zones.
Definition: cellZoneMeshFwd.H:44
Foam::polyMesh::UNCHANGED
Definition: polyMesh.H:93
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::polyMesh::TOPO_CHANGE
Definition: polyMesh.H:95
Foam::timeSelector::selectIfPresent
static instantList selectIfPresent(Time &runTime, const argList &args)
Definition: timeSelector.C:272
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::topoSetSource::SUBSET
Subset with elements in the set.
Definition: topoSetSource.H:103
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::topoSetSource::SUBTRACT
Subtract elements from the set.
Definition: topoSetSource.H:102
zoneID
const labelIOList & zoneID
Definition: interpolatedFaces.H:22
dictIO
IOobject dictIO
Definition: setConstantMeshDictionaryIO.H:1
Foam::topoSetSource::New
static autoPtr< topoSetSource > New(const word &topoSetSourceType, const polyMesh &mesh, const dictionary &dict)
Return a reference to the selected topoSetSource.
Definition: topoSetSource.C:109
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::ZoneMesh::findZoneID
label findZoneID(const word &zoneName) const
Find zone index given a name, return -1 if not found.
Definition: ZoneMesh.C:484
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::polyMesh::readUpdateState
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:91
Foam::polyMesh::bounds
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:441
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
IOdictionary.H
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
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
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:108
cellZoneSet.H
Foam::List< label >
Foam::Time::setTime
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:1006
topoSetSource.H
Foam::topoSetSource::REMOVE
Remove the set (from the file system)
Definition: topoSetSource.H:107
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
timeSelector.H
createTime.H
Foam::dictionary::optionalSubDict
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:645
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:35
Foam::autoPtr::clear
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:178
Foam::topoSetSource::INVERT
Invert the elements in the set.
Definition: topoSetSource.H:104
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:248
Foam::ZoneMesh::clearAddressing
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:618
Foam::polyMesh::globalData
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1234
Foam::argList::addOption
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:336
args
Foam::argList args(argc, argv)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:298
pointZoneSet.H
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
Foam::topoSetSource::actionNames
static const Enum< setAction > actionNames
The setActions text representations.
Definition: topoSetSource.H:113