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 | Copyright (C) 2011-2017 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  topoSet
26 
27 Description
28  Operates on cellSets/faceSets/pointSets through a dictionary.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "argList.H"
33 #include "Time.H"
34 #include "polyMesh.H"
35 #include "topoSetSource.H"
36 #include "globalMeshData.H"
37 #include "timeSelector.H"
38 #include "IOobjectList.H"
39 #include "cellZoneSet.H"
40 #include "faceZoneSet.H"
41 #include "pointZoneSet.H"
42 #include "IOdictionary.H"
43 #include "collatedFileOperation.H"
44 
45 using namespace Foam;
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 void printMesh(const Time& runTime, const polyMesh& mesh)
50 {
51  Info<< "Time:" << runTime.timeName()
52  << " cells:" << mesh.globalData().nTotalCells()
53  << " faces:" << mesh.globalData().nTotalFaces()
54  << " points:" << mesh.globalData().nTotalPoints()
55  << " patches:" << mesh.boundaryMesh().size()
56  << " bb:" << mesh.bounds() << nl;
57 }
58 
59 
60 template<class ZoneType>
61 void removeZone
62 (
64  const word& setName
65 )
66 {
67  label zoneID = zones.findZoneID(setName);
68 
69  if (zoneID != -1)
70  {
71  Info<< "Removing zone " << setName << " at index " << zoneID << endl;
72  // Shuffle to last position
73  labelList oldToNew(zones.size());
74  label newI = 0;
75  forAll(oldToNew, i)
76  {
77  if (i != zoneID)
78  {
79  oldToNew[i] = newI++;
80  }
81  }
82  oldToNew[zoneID] = newI;
83  zones.reorder(oldToNew);
84  // Remove last element
85  zones.setSize(zones.size()-1);
86  zones.clearAddressing();
87  zones.write();
88  }
89 }
90 
91 
92 // Physically remove a set
93 void removeSet
94 (
95  const polyMesh& mesh,
96  const word& setType,
97  const word& setName
98 )
99 {
100  // Remove the file
101  IOobjectList objects
102  (
103  mesh,
104  mesh.time().findInstance
105  (
106  polyMesh::meshSubDir/"sets",
107  word::null,
109  mesh.facesInstance()
110  ),
111  polyMesh::meshSubDir/"sets"
112  );
113 
114  if (objects.found(setName))
115  {
116  // Remove file
117  fileName object = objects[setName]->objectPath();
118  Info<< "Removing file " << object << endl;
119  rm(object);
120  }
121 
122  // See if zone
123  if (setType == cellZoneSet::typeName)
124  {
125  removeZone
126  (
127  const_cast<cellZoneMesh&>(mesh.cellZones()),
128  setName
129  );
130  }
131  else if (setType == faceZoneSet::typeName)
132  {
133  removeZone
134  (
135  const_cast<faceZoneMesh&>(mesh.faceZones()),
136  setName
137  );
138  }
139  else if (setType == pointZoneSet::typeName)
140  {
141  removeZone
142  (
143  const_cast<pointZoneMesh&>(mesh.pointZones()),
144  setName
145  );
146  }
147 }
148 
149 
150 polyMesh::readUpdateState meshReadUpdate(polyMesh& mesh)
151 {
153 
154  switch(stat)
155  {
156  case polyMesh::UNCHANGED:
157  {
158  Info<< " mesh not changed." << endl;
159  break;
160  }
162  {
163  Info<< " points moved; topology unchanged." << endl;
164  break;
165  }
167  {
168  Info<< " topology changed; patches unchanged." << nl
169  << " ";
170  printMesh(mesh.time(), mesh);
171  break;
172  }
174  {
175  Info<< " topology changed and patches changed." << nl
176  << " ";
177  printMesh(mesh.time(), mesh);
178 
179  break;
180  }
181  default:
182  {
184  << "Illegal mesh update state "
185  << stat << abort(FatalError);
186  break;
187  }
188  }
189  return stat;
190 }
191 
192 
193 
194 int main(int argc, char *argv[])
195 {
196  // Specific to topoSet/setSet: quite often we want to block upon writing
197  // a set so we can immediately re-read it. So avoid use of threading
198  // for set writing.
200 
201  timeSelector::addOptions(true, false);
202  #include "addDictOption.H"
203  #include "addRegionOption.H"
205  (
206  "noSync",
207  "do not synchronise selection across coupled patches"
208  );
209 
210  #include "setRootCase.H"
211  #include "createTime.H"
212 
213  instantList timeDirs = timeSelector::selectIfPresent(runTime, args);
214 
215  #include "createNamedPolyMesh.H"
216 
217  const bool noSync = args.optionFound("noSync");
218 
219  const word dictName("topoSetDict");
220  #include "setSystemMeshDictionaryIO.H"
221 
222  Info<< "Reading " << dictName << "\n" << endl;
223 
224  IOdictionary topoSetDict(dictIO);
225 
226  // Read set construct info from dictionary
227  PtrList<dictionary> actions(topoSetDict.lookup("actions"));
228 
229  forAll(timeDirs, timeI)
230  {
231  runTime.setTime(timeDirs[timeI], timeI);
232  Info<< "Time = " << runTime.timeName() << endl;
233 
234  // Optionally re-read mesh
235  meshReadUpdate(mesh);
236 
237  // Execute all actions
238  forAll(actions, i)
239  {
240  const dictionary& dict = actions[i];
241 
242  const word setName(dict.lookup("name"));
243  const word actionName(dict.lookup("action"));
244  const word setType(dict.lookup("type"));
245 
246 
248  (
249  actionName
250  );
251 
252  autoPtr<topoSet> currentSet;
253  if
254  (
255  (action == topoSetSource::NEW)
256  || (action == topoSetSource::CLEAR)
257  )
258  {
259  currentSet = topoSet::New(setType, mesh, setName, 10000);
260  Info<< "Created " << currentSet().type() << " "
261  << setName << endl;
262  }
263  else if (action == topoSetSource::REMOVE)
264  {
265  //?
266  }
267  else
268  {
269  currentSet = topoSet::New
270  (
271  setType,
272  mesh,
273  setName,
275  );
276  Info<< "Read set " << currentSet().type() << " "
277  << setName << " with size "
278  << returnReduce(currentSet().size(), sumOp<label>())
279  << endl;
280  }
281 
282 
283 
284  // Handle special actions (clear, invert) locally, rest through
285  // sources.
286  switch (action)
287  {
288  case topoSetSource::NEW:
289  case topoSetSource::ADD:
291  {
292  Info<< " Applying source " << word(dict.lookup("source"))
293  << endl;
295  (
296  dict.lookup("source"),
297  mesh,
298  dict.subDict("sourceInfo")
299  );
300 
301  source().applyToSet(action, currentSet());
302  // Synchronize for coupled patches.
303  if (!noSync) currentSet().sync(mesh);
304  currentSet().write();
305  }
306  break;
307 
309  {
310  Info<< " Applying source " << word(dict.lookup("source"))
311  << endl;
313  (
314  dict.lookup("source"),
315  mesh,
316  dict.subDict("sourceInfo")
317  );
318 
319  // Backup current set.
320  autoPtr<topoSet> oldSet
321  (
323  (
324  setType,
325  mesh,
326  currentSet().name() + "_old2",
327  currentSet()
328  )
329  );
330 
331  currentSet().clear();
332  source().applyToSet(topoSetSource::NEW, currentSet());
333 
334  // Combine new value of currentSet with old one.
335  currentSet().subset(oldSet());
336  // Synchronize for coupled patches.
337  if (!noSync) currentSet().sync(mesh);
338  currentSet().write();
339  }
340  break;
341 
343  Info<< " Clearing " << currentSet().type() << endl;
344  currentSet().clear();
345  currentSet().write();
346  break;
347 
349  Info<< " Inverting " << currentSet().type() << endl;
350  currentSet().invert(currentSet().maxSize(mesh));
351  currentSet().write();
352  break;
353 
355  Info<< " Removing set" << endl;
356  removeSet(mesh, setType, setName);
357  break;
358 
359 
360  default:
362  << "Unhandled action " << action << endl;
363  break;
364  }
365 
366  if (currentSet.valid())
367  {
368  Info<< " " << currentSet().type() << " "
369  << currentSet().name()
370  << " now size "
371  << returnReduce(currentSet().size(), sumOp<label>())
372  << endl;
373  }
374  }
375  }
376 
377  Info<< "End\n" << endl;
378 
379  return 0;
380 }
381 
382 
383 // ************************************************************************* //
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:424
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
void clearAddressing()
Clear addressing.
Definition: ZoneMesh.C:387
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:69
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:50
const faceZoneMesh & faceZones() const
Return face zone mesh.
Definition: polyMesh.H:466
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:801
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
Definition: PtrList.C:197
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
label nTotalPoints() const
Return total number of points in decomposed mesh. Not.
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:312
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:46
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Return the location of "dir" containing the file "name".
Definition: findInstance.C:82
label nTotalCells() const
Return total number of cells in decomposed mesh.
A list of mesh zones.
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:644
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:52
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:692
void clear()
Delete object (if the pointer is valid) and set pointer to.
Definition: autoPtrI.H:126
label nTotalFaces() const
Return total number of faces in decomposed mesh. Not.
dynamicFvMesh & mesh
label findZoneID(const word &zoneName) const
Find zone index given a name.
Definition: ZoneMesh.C:341
const pointZoneMesh & pointZones() const
Return point zone mesh.
Definition: polyMesh.H:460
A class for handling words, derived from string.
Definition: word.H:59
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:472
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:82
static const word null
An empty word.
Definition: word.H:77
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1182
const word dictName("particleTrackDict")
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:288
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:856
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
static const char nl
Definition: Ostream.H:262
const Time & time() const
Return time.
static autoPtr< topoSetSource > New(const word &topoSetSourceType, const polyMesh &mesh, const dictionary &dict)
Return a reference to the selected topoSetSource.
Definition: topoSetSource.C:74
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
IOobject dictIO(dictName, runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE)
static float maxThreadFileBufferSize
Max size of thread buffer size. This is the overall size of.
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:430
#define WarningInFunction
Report a warning using Foam::Warning.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:63
messageStream Info
static setAction toAction(const word &actionName)
Convert string to action.
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:85
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:88
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in.
Definition: polyMeshIO.C:71
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:984
virtual bool write(const bool valid=true) const
Write using setting from DB.
Foam::argList args(argc, argv)
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576