topoSet.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 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  Executes the sequence of topoSet actions specified in the topoSetDict.
29 
30  Note:
31  topoSet has been superseded by createZones and is now deprecated.
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #include "argList.H"
36 #include "Time.H"
37 #include "polyMesh.H"
38 #include "topoSetSource.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 #include "systemDict.H"
46 
47 using namespace Foam;
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 void printMesh(const Time& runTime, const polyMesh& mesh)
52 {
53  Info<< "Time:" << runTime.name()
54  << " cells:" << mesh.globalData().nTotalCells()
55  << " faces:" << mesh.globalData().nTotalFaces()
56  << " points:" << mesh.globalData().nTotalPoints()
57  << " patches:" << mesh.boundaryMesh().size()
58  << " bb:" << mesh.bounds() << nl;
59 }
60 
61 
62 template<class ZonesType>
63 void removeZone(ZonesType& zones, const word& setName)
64 {
65  label zoneID = zones.findIndex(setName);
66 
67  if (zoneID != -1)
68  {
69  Info<< "Removing zone " << setName << " at index " << zoneID << endl;
70  // Shuffle to last position
71  labelList oldToNew(zones.size());
72  label newI = 0;
73  forAll(oldToNew, i)
74  {
75  if (i != zoneID)
76  {
77  oldToNew[i] = newI++;
78  }
79  }
80  oldToNew[zoneID] = newI;
81  zones.reorder(oldToNew);
82  // Remove last element
83  zones.setSize(zones.size()-1);
84  zones.clearAddressing();
85  zones.write();
86  fileHandler().flush();
87  }
88 }
89 
90 
91 // Physically remove a set
92 void removeSet
93 (
94  const polyMesh& mesh,
95  const word& setType,
96  const word& setName
97 )
98 {
99  // Remove the file
101  (
102  mesh,
104  (
105  polyMesh::meshSubDir/"sets",
106  word::null,
109  ),
110  polyMesh::meshSubDir/"sets"
111  );
112 
113  if (objects.found(setName))
114  {
115  // Remove file
116  const fileName object = objects[setName]->objectPath(false);
117  Info<< "Removing file " << object << endl;
118  rm(object);
119  }
120 
121  // See if zone
122  if (setType == cellZoneSet::typeName)
123  {
124  removeZone
125  (
126  const_cast<cellZoneList&>(mesh.cellZones()),
127  setName
128  );
129  }
130  else if (setType == faceZoneSet::typeName)
131  {
132  removeZone
133  (
134  const_cast<faceZoneList&>(mesh.faceZones()),
135  setName
136  );
137  }
138  else if (setType == pointZoneSet::typeName)
139  {
140  removeZone
141  (
142  const_cast<pointZoneList&>(mesh.pointZones()),
143  setName
144  );
145  }
146 }
147 
148 
149 polyMesh::readUpdateState meshReadUpdate(polyMesh& mesh)
150 {
152 
153  switch(stat)
154  {
155  case polyMesh::UNCHANGED:
156  {
157  Info<< " mesh not changed." << endl;
158  break;
159  }
161  {
162  Info<< " points moved; topology unchanged." << endl;
163  break;
164  }
166  {
167  Info<< " topology changed; patches unchanged." << nl
168  << " ";
169  printMesh(mesh.time(), mesh);
170  break;
171  }
173  {
174  Info<< " topology changed and patches changed." << nl
175  << " ";
176  printMesh(mesh.time(), mesh);
177 
178  break;
179  }
180  default:
181  {
183  << "Illegal mesh update state "
184  << stat << abort(FatalError);
185  break;
186  }
187  }
188  return stat;
189 }
190 
191 
192 
193 int main(int argc, char *argv[])
194 {
195  timeSelector::addOptions(true, false);
196  #include "addDictOption.H"
197  #include "addMeshOption.H"
198  #include "addRegionOption.H"
200  (
201  "noSync",
202  "do not synchronise selection across coupled patches"
203  );
204 
205  #include "setRootCase.H"
206 
207  Warning
208  << nl
209  << "topoSet has been superseded by createZones"
210  " and is now deprecated."
211  << nl << endl;
212 
213  #include "createTime.H"
214 
216 
217  #include "createSpecifiedPolyMesh.H"
218 
219  const bool noSync = args.optionFound("noSync");
220 
221  const dictionary topoSetDict(systemDict("topoSetDict", args, mesh));
222 
223  // Read set construct info from dictionary
224  PtrList<dictionary> actions(topoSetDict.lookup("actions"));
225 
226  forAll(timeDirs, timei)
227  {
228  runTime.setTime(timeDirs[timei], timei);
229  Info<< "Time = " << runTime.userTimeName() << endl;
230 
231  // Optionally re-read mesh
232  meshReadUpdate(mesh);
233 
234  // Execute all actions
235  forAll(actions, i)
236  {
237  const dictionary& dict = actions[i];
238 
239  const word setName(dict.lookup("name"));
240  const word actionName(dict.lookup("action"));
241  const word setType(dict.lookup("type"));
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  // Handle special actions (clear, invert) locally, rest through
280  // sources.
281  switch (action)
282  {
283  case topoSetSource::NEW:
284  case topoSetSource::ADD:
286  {
287  Info<< " Applying source " << word(dict.lookup("source"))
288  << endl;
290  (
291  dict.lookup("source"),
292  mesh,
293  dict.optionalSubDict("sourceInfo")
294  );
295 
296  source().applyToSet(action, currentSet());
297  // Synchronise for coupled patches.
298  if (!noSync) currentSet().sync(mesh);
299  currentSet().write();
300  fileHandler().flush();
301  }
302  break;
303 
305  {
306  Info<< " Applying source " << word(dict.lookup("source"))
307  << endl;
309  (
310  dict.lookup("source"),
311  mesh,
312  dict.optionalSubDict("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  // Synchronise for coupled patches.
333  if (!noSync) currentSet().sync(mesh);
334  currentSet().write();
335  fileHandler().flush();
336  }
337  break;
338 
340  Info<< " Clearing " << currentSet().type() << endl;
341  currentSet().clear();
342  currentSet().write();
343  fileHandler().flush();
344  break;
345 
347  Info<< " Inverting " << currentSet().type() << endl;
348  currentSet().invert(currentSet().maxSize(mesh));
349  currentSet().write();
350  fileHandler().flush();
351  break;
352 
354  Info<< " Removing set" << endl;
355  removeSet(mesh, setType, setName);
356  break;
357 
358 
359  default:
361  << "Unhandled action " << action << endl;
362  break;
363  }
364 
365  if (currentSet.valid())
366  {
367  Info<< " " << currentSet().type() << " "
368  << currentSet().name()
369  << " now size "
370  << returnReduce(currentSet().size(), sumOp<label>())
371  << endl;
372  }
373  }
374  }
375 
376  Info<< "End\n" << endl;
377 
378  return 0;
379 }
380 
381 
382 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:53
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:974
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: Time.C:647
word userTimeName() const
Return current user time name with units.
Definition: Time.C:845
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
void clear()
Delete object (if the pointer is valid) and set pointer to.
Definition: autoPtrI.H:126
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:740
const dictionary & optionalSubDict(const word &) const
Find and return a sub-dictionary if found.
Definition: dictionary.C:927
const word & name() const
Return const reference to name.
A class for handling file names.
Definition: fileName.H:82
virtual void flush() const
Forcibly wait until all output done. Flush any cached data.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:420
readUpdateState readUpdate(const stitchType stitch=stitchType::geometric)
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:866
label nTotalFaces() const
Return total number of faces in decomposed mesh. Not.
label nTotalPoints() const
Return total number of points in decomposed mesh. Not.
label nTotalCells() const
Return total number of cells in decomposed mesh.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const pointZoneList & pointZones() const
Return point zones.
Definition: polyMesh.H:437
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:994
const cellZoneList & cellZones() const
Return cell zones.
Definition: polyMesh.H:449
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:90
@ TOPO_PATCH_CHANGE
Definition: polyMesh.H:94
const globalMeshData & globalData() const
Return parallel info.
Definition: polyMesh.C:1521
const faceZoneList & faceZones() const
Return face zones.
Definition: polyMesh.H:443
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:401
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:273
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:407
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:283
static setAction toAction(const word &actionName)
Convert string to action.
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:83
static autoPtr< topoSetSource > New(const word &topoSetSourceType, const polyMesh &mesh, const dictionary &dict)
Return a reference to the selected topoSetSource.
Definition: topoSetSource.C:61
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:45
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
int main(int argc, char *argv[])
Definition: financialFoam.C:44
static instantList timeDirs
Definition: globalFoam.H:44
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
const fileOperation & fileHandler()
Get current file handler.
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
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1017
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError
IOdictionary systemDict(const word &dictName, const argList &args, const objectRegistry &ob, const word &regionName=polyMesh::defaultRegion, const fileName &path=fileName::null)
Definition: systemDict.C:93
static const char nl
Definition: Ostream.H:267
messageStream Warning
objects
dictionary dict
Foam::argList args(argc, argv)