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-2016 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 "cellSet.H"
37 #include "faceSet.H"
38 #include "pointSet.H"
39 #include "globalMeshData.H"
40 #include "timeSelector.H"
41 #include "IOobjectList.H"
42 #include "cellZoneSet.H"
43 #include "faceZoneSet.H"
44 #include "pointZoneSet.H"
45 
46 using namespace Foam;
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 void printMesh(const Time& runTime, const polyMesh& mesh)
51 {
52  Info<< "Time:" << runTime.timeName()
53  << " cells:" << mesh.globalData().nTotalCells()
54  << " faces:" << mesh.globalData().nTotalFaces()
55  << " points:" << mesh.globalData().nTotalPoints()
56  << " patches:" << mesh.boundaryMesh().size()
57  << " bb:" << mesh.bounds() << nl;
58 }
59 
60 
61 template<class ZoneType>
62 void removeZone
63 (
65  const word& setName
66 )
67 {
68  label zoneID = zones.findZoneID(setName);
69 
70  if (zoneID != -1)
71  {
72  Info<< "Removing zone " << setName << " at index " << zoneID << endl;
73  // Shuffle to last position
74  labelList oldToNew(zones.size());
75  label newI = 0;
76  forAll(oldToNew, i)
77  {
78  if (i != zoneID)
79  {
80  oldToNew[i] = newI++;
81  }
82  }
83  oldToNew[zoneID] = newI;
84  zones.reorder(oldToNew);
85  // Remove last element
86  zones.setSize(zones.size()-1);
87  zones.clearAddressing();
88  zones.write();
89  }
90 }
91 
92 
93 // Physically remove a set
94 void removeSet
95 (
96  const polyMesh& mesh,
97  const word& setType,
98  const word& setName
99 )
100 {
101  // Remove the file
102  IOobjectList objects
103  (
104  mesh,
105  mesh.time().findInstance
106  (
107  polyMesh::meshSubDir/"sets",
108  word::null,
110  mesh.facesInstance()
111  ),
112  polyMesh::meshSubDir/"sets"
113  );
114 
115  if (objects.found(setName))
116  {
117  // Remove file
118  fileName object = objects[setName]->objectPath();
119  Info<< "Removing file " << object << endl;
120  rm(object);
121  }
122 
123  // See if zone
124  if (setType == cellZoneSet::typeName)
125  {
126  removeZone
127  (
128  const_cast<cellZoneMesh&>(mesh.cellZones()),
129  setName
130  );
131  }
132  else if (setType == faceZoneSet::typeName)
133  {
134  removeZone
135  (
136  const_cast<faceZoneMesh&>(mesh.faceZones()),
137  setName
138  );
139  }
140  else if (setType == pointZoneSet::typeName)
141  {
142  removeZone
143  (
144  const_cast<pointZoneMesh&>(mesh.pointZones()),
145  setName
146  );
147  }
148 }
149 
150 
151 polyMesh::readUpdateState meshReadUpdate(polyMesh& mesh)
152 {
154 
155  switch(stat)
156  {
157  case polyMesh::UNCHANGED:
158  {
159  Info<< " mesh not changed." << endl;
160  break;
161  }
163  {
164  Info<< " points moved; topology unchanged." << endl;
165  break;
166  }
168  {
169  Info<< " topology changed; patches unchanged." << nl
170  << " ";
171  printMesh(mesh.time(), mesh);
172  break;
173  }
175  {
176  Info<< " topology changed and patches changed." << nl
177  << " ";
178  printMesh(mesh.time(), mesh);
179 
180  break;
181  }
182  default:
183  {
185  << "Illegal mesh update state "
186  << stat << abort(FatalError);
187  break;
188  }
189  }
190  return stat;
191 }
192 
193 
194 
195 int main(int argc, char *argv[])
196 {
197  timeSelector::addOptions(true, false);
198  #include "addDictOption.H"
199  #include "addRegionOption.H"
201  (
202  "noSync",
203  "do not synchronise selection across coupled patches"
204  );
205 
206  #include "setRootCase.H"
207  #include "createTime.H"
208 
209  instantList timeDirs = timeSelector::selectIfPresent(runTime, args);
210 
211  #include "createNamedPolyMesh.H"
212 
213  const bool noSync = args.optionFound("noSync");
214 
215  const word dictName("topoSetDict");
216  #include "setSystemMeshDictionaryIO.H"
217 
218  Info<< "Reading " << dictName << "\n" << endl;
219 
220  IOdictionary topoSetDict(dictIO);
221 
222  // Read set construct info from dictionary
223  PtrList<dictionary> actions(topoSetDict.lookup("actions"));
224 
225  forAll(timeDirs, timeI)
226  {
227  runTime.setTime(timeDirs[timeI], timeI);
228  Info<< "Time = " << runTime.timeName() << endl;
229 
230  // Optionally re-read mesh
231  meshReadUpdate(mesh);
232 
233  // Execute all actions
234  forAll(actions, i)
235  {
236  const dictionary& dict = actions[i];
237 
238  const word setName(dict.lookup("name"));
239  const word actionName(dict.lookup("action"));
240  const word setType(dict.lookup("type"));
241 
242 
244  (
245  actionName
246  );
247 
248  autoPtr<topoSet> currentSet;
249  if
250  (
251  (action == topoSetSource::NEW)
252  || (action == topoSetSource::CLEAR)
253  )
254  {
255  currentSet = topoSet::New(setType, mesh, setName, 10000);
256  Info<< "Created " << currentSet().type() << " "
257  << setName << endl;
258  }
259  else if (action == topoSetSource::REMOVE)
260  {
261  //?
262  }
263  else
264  {
265  currentSet = topoSet::New
266  (
267  setType,
268  mesh,
269  setName,
271  );
272  Info<< "Read set " << currentSet().type() << " "
273  << setName << " with size "
274  << returnReduce(currentSet().size(), sumOp<label>())
275  << endl;
276  }
277 
278 
279 
280  // Handle special actions (clear, invert) locally, rest through
281  // sources.
282  switch (action)
283  {
284  case topoSetSource::NEW:
285  case topoSetSource::ADD:
287  {
288  Info<< " Applying source " << word(dict.lookup("source"))
289  << endl;
291  (
292  dict.lookup("source"),
293  mesh,
294  dict.subDict("sourceInfo")
295  );
296 
297  source().applyToSet(action, currentSet());
298  // Synchronize for coupled patches.
299  if (!noSync) currentSet().sync(mesh);
300  currentSet().write();
301  }
302  break;
303 
305  {
306  Info<< " Applying source " << word(dict.lookup("source"))
307  << endl;
309  (
310  dict.lookup("source"),
311  mesh,
312  dict.subDict("sourceInfo")
313  );
314 
315  // Backup current set.
316  autoPtr<topoSet> oldSet
317  (
319  (
320  setType,
321  mesh,
322  currentSet().name() + "_old2",
323  currentSet()
324  )
325  );
326 
327  currentSet().clear();
328  source().applyToSet(topoSetSource::NEW, currentSet());
329 
330  // Combine new value of currentSet with old one.
331  currentSet().subset(oldSet());
332  // Synchronize for coupled patches.
333  if (!noSync) currentSet().sync(mesh);
334  currentSet().write();
335  }
336  break;
337 
339  Info<< " Clearing " << currentSet().type() << endl;
340  currentSet().clear();
341  currentSet().write();
342  break;
343 
345  Info<< " Inverting " << currentSet().type() << endl;
346  currentSet().invert(currentSet().maxSize(mesh));
347  currentSet().write();
348  break;
349 
351  Info<< " Removing set" << endl;
352  removeSet(mesh, setType, setName);
353  break;
354 
355 
356  default:
358  << "Unhandled action " << action << endl;
359  break;
360  }
361 
362  if (currentSet.valid())
363  {
364  Info<< " " << currentSet().type() << " "
365  << currentSet().name()
366  << " now size "
367  << returnReduce(currentSet().size(), sumOp<label>())
368  << endl;
369  }
370  }
371  }
372 
373  Info<< "End\n" << endl;
374 
375  return 0;
376 }
377 
378 
379 // ************************************************************************* //
const Time & time() const
Return time.
#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 boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:427
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:778
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:309
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:633
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
A list of mesh zones.
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:715
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
void clear()
Delete object (if the pointer is valid) and set pointer to NULL.
Definition: autoPtrI.H:126
label findZoneID(const word &zoneName) const
Find zone index given a name.
Definition: ZoneMesh.C:341
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:38
dynamicFvMesh & mesh
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
A class for handling words, derived from string.
Definition: word.H:59
label nTotalFaces() const
Return total number of faces in decomposed mesh. Not.
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:82
static const word null
An empty word.
Definition: word.H:77
label nTotalPoints() const
Return total number of points in decomposed mesh. Not.
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:284
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:924
errorManip< error > abort(error &err)
Definition: errorManip.H:131
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set).
Definition: autoPtrI.H:83
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1140
static const char nl
Definition: Ostream.H:262
const pointZoneMesh & pointZones() const
Return point zone mesh.
Definition: polyMesh.H:457
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)
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:469
#define WarningInFunction
Report a warning using Foam::Warning.
word dictName("noiseDict")
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:62
virtual bool write() const
Write using setting from DB.
messageStream Info
static setAction toAction(const word &actionName)
Convert string to action.
const faceZoneMesh & faceZones() const
Return face zone mesh.
Definition: polyMesh.H:463
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:53
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:83
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:65
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:819
Foam::argList args(argc, argv)
label nTotalCells() const
Return total number of cells in decomposed mesh.
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.
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451