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-2024 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 Usage
31 
32 \*---------------------------------------------------------------------------*/
33 
34 #include "argList.H"
35 #include "Time.H"
36 #include "polyMesh.H"
37 #include "topoSetSource.H"
38 #include "globalMeshData.H"
39 #include "timeSelector.H"
40 #include "IOobjectList.H"
41 #include "cellZoneSet.H"
42 #include "faceZoneSet.H"
43 #include "pointZoneSet.H"
44 #include "systemDict.H"
45 
46 using namespace Foam;
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 void printMesh(const Time& runTime, const polyMesh& mesh)
51 {
52  Info<< "Time:" << runTime.name()
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 ZonesType>
62 void removeZone(ZonesType& zones, const word& setName)
63 {
64  label zoneID = zones.findIndex(setName);
65 
66  if (zoneID != -1)
67  {
68  Info<< "Removing zone " << setName << " at index " << zoneID << endl;
69  // Shuffle to last position
70  labelList oldToNew(zones.size());
71  label newI = 0;
72  forAll(oldToNew, i)
73  {
74  if (i != zoneID)
75  {
76  oldToNew[i] = newI++;
77  }
78  }
79  oldToNew[zoneID] = newI;
80  zones.reorder(oldToNew);
81  // Remove last element
82  zones.setSize(zones.size()-1);
83  zones.clearAddressing();
84  zones.write();
85  fileHandler().flush();
86  }
87 }
88 
89 
90 // Physically remove a set
91 void removeSet
92 (
93  const polyMesh& mesh,
94  const word& setType,
95  const word& setName
96 )
97 {
98  // Remove the file
100  (
101  mesh,
102  mesh.time().findInstance
103  (
104  polyMesh::meshSubDir/"sets",
105  word::null,
107  mesh.facesInstance()
108  ),
109  polyMesh::meshSubDir/"sets"
110  );
111 
112  if (objects.found(setName))
113  {
114  // Remove file
115  const fileName object = objects[setName]->objectPath(false);
116  Info<< "Removing file " << object << endl;
117  rm(object);
118  }
119 
120  // See if zone
121  if (setType == cellZoneSet::typeName)
122  {
123  removeZone
124  (
125  const_cast<cellZoneList&>(mesh.cellZones()),
126  setName
127  );
128  }
129  else if (setType == faceZoneSet::typeName)
130  {
131  removeZone
132  (
133  const_cast<faceZoneList&>(mesh.faceZones()),
134  setName
135  );
136  }
137  else if (setType == pointZoneSet::typeName)
138  {
139  removeZone
140  (
141  const_cast<pointZoneList&>(mesh.pointZones()),
142  setName
143  );
144  }
145 }
146 
147 
148 polyMesh::readUpdateState meshReadUpdate(polyMesh& mesh)
149 {
151 
152  switch(stat)
153  {
154  case polyMesh::UNCHANGED:
155  {
156  Info<< " mesh not changed." << endl;
157  break;
158  }
160  {
161  Info<< " points moved; topology unchanged." << endl;
162  break;
163  }
165  {
166  Info<< " topology changed; patches unchanged." << nl
167  << " ";
168  printMesh(mesh.time(), mesh);
169  break;
170  }
172  {
173  Info<< " topology changed and patches changed." << nl
174  << " ";
175  printMesh(mesh.time(), mesh);
176 
177  break;
178  }
179  default:
180  {
182  << "Illegal mesh update state "
183  << stat << abort(FatalError);
184  break;
185  }
186  }
187  return stat;
188 }
189 
190 
191 
192 int main(int argc, char *argv[])
193 {
194  timeSelector::addOptions(true, false);
195  #include "addDictOption.H"
196  #include "addRegionOption.H"
198  (
199  "noSync",
200  "do not synchronise selection across coupled patches"
201  );
202 
203  #include "setRootCase.H"
204  #include "createTime.H"
205 
207 
208  #include "createNamedPolyMesh.H"
209 
210  const bool noSync = args.optionFound("noSync");
211 
212  const dictionary topoSetDict(systemDict("topoSetDict", args, mesh));
213 
214  // Read set construct info from dictionary
215  PtrList<dictionary> actions(topoSetDict.lookup("actions"));
216 
217  forAll(timeDirs, timei)
218  {
219  runTime.setTime(timeDirs[timei], timei);
220  Info<< "Time = " << runTime.userTimeName() << endl;
221 
222  // Optionally re-read mesh
223  meshReadUpdate(mesh);
224 
225  // Execute all actions
226  forAll(actions, i)
227  {
228  const dictionary& dict = actions[i];
229 
230  const word setName(dict.lookup("name"));
231  const word actionName(dict.lookup("action"));
232  const word setType(dict.lookup("type"));
233 
235  (
236  actionName
237  );
238 
239  autoPtr<topoSet> currentSet;
240  if
241  (
242  (action == topoSetSource::NEW)
243  || (action == topoSetSource::CLEAR)
244  )
245  {
246  currentSet = topoSet::New(setType, mesh, setName, 10000);
247  Info<< "Created " << currentSet().type() << " "
248  << setName << endl;
249  }
250  else if (action == topoSetSource::REMOVE)
251  {
252  //?
253  }
254  else
255  {
256  currentSet = topoSet::New
257  (
258  setType,
259  mesh,
260  setName,
262  );
263  Info<< "Read set " << currentSet().type() << " "
264  << setName << " with size "
265  << returnReduce(currentSet().size(), sumOp<label>())
266  << endl;
267  }
268 
269 
270  // Handle special actions (clear, invert) locally, rest through
271  // sources.
272  switch (action)
273  {
274  case topoSetSource::NEW:
275  case topoSetSource::ADD:
277  {
278  Info<< " Applying source " << word(dict.lookup("source"))
279  << endl;
281  (
282  dict.lookup("source"),
283  mesh,
284  dict.optionalSubDict("sourceInfo")
285  );
286 
287  source().applyToSet(action, currentSet());
288  // Synchronise for coupled patches.
289  if (!noSync) currentSet().sync(mesh);
290  currentSet().write();
291  fileHandler().flush();
292  }
293  break;
294 
296  {
297  Info<< " Applying source " << word(dict.lookup("source"))
298  << endl;
300  (
301  dict.lookup("source"),
302  mesh,
303  dict.optionalSubDict("sourceInfo")
304  );
305 
306  // Backup current set.
307  autoPtr<topoSet> oldSet
308  (
310  (
311  setType,
312  mesh,
313  currentSet().name() + "_old2",
314  currentSet()
315  )
316  );
317 
318  currentSet().clear();
319  source().applyToSet(topoSetSource::NEW, currentSet());
320 
321  // Combine new value of currentSet with old one.
322  currentSet().subset(oldSet());
323  // Synchronise for coupled patches.
324  if (!noSync) currentSet().sync(mesh);
325  currentSet().write();
326  fileHandler().flush();
327  }
328  break;
329 
331  Info<< " Clearing " << currentSet().type() << endl;
332  currentSet().clear();
333  currentSet().write();
334  fileHandler().flush();
335  break;
336 
338  Info<< " Inverting " << currentSet().type() << endl;
339  currentSet().invert(currentSet().maxSize(mesh));
340  currentSet().write();
341  fileHandler().flush();
342  break;
343 
345  Info<< " Removing set" << endl;
346  removeSet(mesh, setType, setName);
347  break;
348 
349 
350  default:
352  << "Unhandled action " << action << endl;
353  break;
354  }
355 
356  if (currentSet.valid())
357  {
358  Info<< " " << currentSet().type() << " "
359  << currentSet().name()
360  << " now size "
361  << returnReduce(currentSet().size(), sumOp<label>())
362  << endl;
363  }
364  }
365  }
366 
367  Info<< "End\n" << endl;
368 
369  return 0;
370 }
371 
372 
373 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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:985
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:658
word userTimeName() const
Return current user time name with units.
Definition: Time.C:856
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 keyword definitions, which are a keyword followed by any number of values (e....
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:710
const dictionary & optionalSubDict(const word &) const
Find and return a sub-dictionary if found.
Definition: dictionary.C:921
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.
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.
const Time & time() const
Return time.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const pointZoneList & pointZones() const
Return point zones.
Definition: polyMesh.H:440
const fileName & facesInstance() const
Return the current instance directory for faces.
Definition: polyMesh.C:971
const cellZoneList & cellZones() const
Return cell zones.
Definition: polyMesh.H:452
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:1515
const faceZoneList & faceZones() const
Return face zones.
Definition: polyMesh.H:446
readUpdateState readUpdate()
Update the mesh based on the mesh files saved in.
Definition: polyMeshIO.C:99
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:404
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:270
const boundBox & bounds() const
Return mesh bounding box.
Definition: polyMesh.H:410
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:70
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.
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
#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
IOdictionary systemDict(const word &dictName, const argList &args, const objectRegistry &ob, const word &regionName=polyMesh::defaultRegion)
Definition: systemDict.C:92
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
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)
error FatalError
static const char nl
Definition: Ostream.H:266
objects
dictionary dict
Foam::argList args(argc, argv)