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-2021 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 #include "namedDictionary.H"
51 
52 using namespace Foam;
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 void printMesh(const Time& runTime, const polyMesh& mesh)
57 {
58  Info<< "Time:" << runTime.timeName()
59  << " cells:" << mesh.globalData().nTotalCells()
60  << " faces:" << mesh.globalData().nTotalFaces()
61  << " points:" << mesh.globalData().nTotalPoints()
62  << " patches:" << mesh.boundaryMesh().size()
63  << " bb:" << mesh.bounds() << nl;
64 }
65 
66 
67 template<class ZoneType>
68 void removeZone
69 (
71  const word& setName
72 )
73 {
74  label zoneID = zones.findZoneID(setName);
75 
76  if (zoneID != -1)
77  {
78  Info<< "Removing zone " << setName << " at index " << zoneID << endl;
79  // Shuffle to last position
80  labelList oldToNew(zones.size());
81  label newI = 0;
82  forAll(oldToNew, i)
83  {
84  if (i != zoneID)
85  {
86  oldToNew[i] = newI++;
87  }
88  }
89  oldToNew[zoneID] = newI;
90  zones.reorder(oldToNew);
91  // Remove last element
92  zones.setSize(zones.size()-1);
93  zones.clearAddressing();
94  if (!zones.write())
95  {
96  WarningInFunction << "Failed writing zone " << setName << endl;
97  }
98  fileHandler().flush();
99  }
100 }
101 
102 
103 // Physically remove a set
104 void removeSet
105 (
106  const polyMesh& mesh,
107  const word& setType,
108  const word& setName
109 )
110 {
111  // Remove the file
112  IOobjectList objects
113  (
114  mesh,
116  (
117  polyMesh::meshSubDir/"sets",
118  word::null,
121  ),
122  polyMesh::meshSubDir/"sets"
123  );
124 
125  if (objects.found(setName))
126  {
127  // Remove file
128  fileName object = objects[setName]->objectPath();
129  Info<< "Removing file " << object << endl;
130  rm(object);
131  }
132 
133  // See if zone
134  if (setType == cellZoneSet::typeName)
135  {
136  removeZone
137  (
138  const_cast<cellZoneMesh&>(mesh.cellZones()),
139  setName
140  );
141  }
142  else if (setType == faceZoneSet::typeName)
143  {
144  removeZone
145  (
146  const_cast<faceZoneMesh&>(mesh.faceZones()),
147  setName
148  );
149  }
150  else if (setType == pointZoneSet::typeName)
151  {
152  removeZone
153  (
154  const_cast<pointZoneMesh&>(mesh.pointZones()),
155  setName
156  );
157  }
158 }
159 
160 
161 polyMesh::readUpdateState meshReadUpdate(polyMesh& mesh)
162 {
164 
165  switch(stat)
166  {
167  case polyMesh::UNCHANGED:
168  {
169  Info<< " mesh not changed." << endl;
170  break;
171  }
173  {
174  Info<< " points moved; topology unchanged." << endl;
175  break;
176  }
178  {
179  Info<< " topology changed; patches unchanged." << nl
180  << " ";
181  printMesh(mesh.time(), mesh);
182  break;
183  }
185  {
186  Info<< " topology changed and patches changed." << nl
187  << " ";
188  printMesh(mesh.time(), mesh);
189 
190  break;
191  }
192  default:
193  {
195  << "Illegal mesh update state "
196  << stat << abort(FatalError);
197  break;
198  }
199  }
200  return stat;
201 }
202 
203 
204 
205 int main(int argc, char *argv[])
206 {
208  (
209  "Operates on cellSets/faceSets/pointSets through a dictionary,"
210  " normally system/topoSetDict"
211  );
212 
213  timeSelector::addOptions(true, false); // constant(true), zero(false)
214 
215  argList::addOption("dict", "file", "Alternative topoSetDict");
216 
217  #include "addRegionOption.H"
219  (
220  "noSync",
221  "Do not synchronise selection across coupled patches"
222  );
223 
224  #include "setRootCase.H"
225  #include "createTime.H"
226 
228 
229  #include "createNamedPolyMesh.H"
230 
231  const bool noSync = args.found("noSync");
232 
233  const word dictName("topoSetDict");
234  #include "setSystemMeshDictionaryIO.H"
235 
236  Info<< "Reading " << dictIO.name() << nl << endl;
237 
238  IOdictionary topoSetDict(dictIO);
239 
240  // Read set construct info from dictionary
241  List<namedDictionary> actionEntries(topoSetDict.lookup("actions"));
242 
243  forAll(timeDirs, timeI)
244  {
245  runTime.setTime(timeDirs[timeI], timeI);
246  Info<< "Time = " << runTime.timeName() << endl;
247 
248  // Optionally re-read mesh
249  meshReadUpdate(mesh);
250 
251  // Execute all actions
252  for (const namedDictionary& actionEntry : actionEntries)
253  {
254  const dictionary& dict = actionEntry.dict();
255  if (dict.empty())
256  {
257  continue;
258  }
259  const word setName(dict.get<word>("name"));
260  const word setType(dict.get<word>("type"));
261 
262  const topoSetSource::setAction action =
264 
265  autoPtr<topoSet> currentSet;
266  if
267  (
268  action == topoSetSource::NEW
269  || action == topoSetSource::CLEAR
270  )
271  {
272  currentSet = topoSet::New(setType, mesh, setName, 16384);
273  Info<< "Created " << currentSet().type() << ' '
274  << setName << endl;
275  }
276  else if (action == topoSetSource::REMOVE)
277  {
278  //?
279  }
280  else
281  {
282  currentSet = topoSet::New
283  (
284  setType,
285  mesh,
286  setName,
288  );
289  Info<< "Read set " << currentSet().type() << ' '
290  << setName << " with size "
291  << returnReduce(currentSet().size(), sumOp<label>())
292  << endl;
293  }
294 
295 
296  // Handle special actions (clear, invert) locally,
297  // the other actions through sources.
298  switch (action)
299  {
300  case topoSetSource::NEW:
301  case topoSetSource::ADD:
303  {
304  const word sourceType(dict.get<word>("source"));
305 
306  Info<< " Applying source " << sourceType << endl;
308  (
309  sourceType,
310  mesh,
311  dict.optionalSubDict("sourceInfo")
312  );
313 
314  source().applyToSet(action, currentSet());
315  // Synchronize for coupled patches.
316  if (!noSync) currentSet().sync(mesh);
317  if (!currentSet().write())
318  {
320  << "Failed writing set "
321  << currentSet().objectPath() << endl;
322  }
323  fileHandler().flush();
324  }
325  break;
326 
328  {
329  const word sourceType(dict.get<word>("source"));
330 
331  Info<< " Applying source " << sourceType << endl;
333  (
334  sourceType,
335  mesh,
336  dict.optionalSubDict("sourceInfo")
337  );
338 
339  // Backup current set.
340  autoPtr<topoSet> oldSet
341  (
343  (
344  setType,
345  mesh,
346  currentSet().name() + "_old2",
347  currentSet()
348  )
349  );
350 
351  currentSet().clear();
352  source().applyToSet(topoSetSource::NEW, currentSet());
353 
354  // Combine new value of currentSet with old one.
355  currentSet().subset(oldSet());
356  // Synchronize for coupled patches.
357  if (!noSync) currentSet().sync(mesh);
358  if (!currentSet().write())
359  {
361  << "Failed writing set "
362  << currentSet().objectPath() << endl;
363  }
364  fileHandler().flush();
365  }
366  break;
367 
369  Info<< " Clearing " << currentSet().type() << endl;
370  currentSet().clear();
371  if (!currentSet().write())
372  {
374  << "Failed writing set "
375  << currentSet().objectPath() << endl;
376  }
377  fileHandler().flush();
378  break;
379 
381  Info<< " Inverting " << currentSet().type() << endl;
382  currentSet().invert(currentSet().maxSize(mesh));
383  if (!currentSet().write())
384  {
386  << "Failed writing set "
387  << currentSet().objectPath() << endl;
388  }
389  fileHandler().flush();
390  break;
391 
393  Info<< " Removing set" << endl;
394  removeSet(mesh, setType, setName);
395  break;
396 
397  default:
399  << "Unhandled action " << action << endl;
400  break;
401  }
402 
403  if (currentSet)
404  {
405  Info<< " "
406  << currentSet().type() << ' '
407  << currentSet().name() << " now size "
408  << returnReduce(currentSet().size(), sumOp<label>())
409  << endl;
410  }
411  }
412  }
413 
414  Info<< "\nEnd\n" << endl;
415 
416  return 0;
417 }
418 
419 
420 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::topoSetSource::ADD
@ ADD
Add elements to current set.
Definition: topoSetSource.H:99
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Foam::topoSetSource::CLEAR
@ CLEAR
Clear the set, possibly creating it.
Definition: topoSetSource.H:106
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:88
Foam::polyMesh::POINTS_MOVED
@ POINTS_MOVED
Definition: polyMesh.H:89
faceZoneSet.H
globalMeshData.H
dictName
const word dictName("faMeshDefinition")
Foam::polyMesh::meshSubDir
static word meshSubDir
Definition: polyMesh.H:317
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:773
Foam::fileOperation::flush
virtual void flush() const
Definition: fileOperation.C:1233
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::polyMesh::facesInstance
const fileName & facesInstance() const
Definition: polyMesh.C:845
Foam::rm
bool rm(const fileName &file)
Definition: POSIX.C:1282
Foam::fileHandler
const fileOperation & fileHandler()
Definition: fileOperation.C:1478
IOobjectList.H
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Definition: polyMesh.H:440
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::topoSetSource::setAction
setAction
Definition: topoSetSource.H:96
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:100
Foam::topoSet::New
static autoPtr< topoSet > New(const word &setType, const polyMesh &mesh, const word &name, readOption r=MUST_READ, writeOption w=NO_WRITE)
Definition: topoSet.C:48
Foam::topoSetSource::NEW
@ NEW
Create a new set and ADD elements to it.
Definition: topoSetSource.H:100
Foam::Enum::get
EnumType get(const word &enumName) const
Definition: Enum.C:68
polyMesh.H
setSystemMeshDictionaryIO.H
createNamedPolyMesh.H
Required Variables.
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:73
Foam::sumOp
Definition: ops.H:207
forAll
#define forAll(list, i)
Definition: stdFoam.H:349
Foam::fvMesh::readUpdate
virtual readUpdateState readUpdate()
Definition: fvMesh.C:641
Foam::polyMesh::TOPO_PATCH_CHANGE
@ TOPO_PATCH_CHANGE
Definition: polyMesh.H:91
namedDictionary.H
Foam::Info
messageStream Info
Foam::polyMesh::cellZones
const cellZoneMesh & cellZones() const noexcept
Definition: polyMesh.H:488
argList.H
Foam::namedDictionary
A tuple of keyType and dictionary, which can be used when reading named or unnamed dictionary entries...
Definition: namedDictionary.H:79
Foam::IOobject::READ_IF_PRESENT
@ READ_IF_PRESENT
Definition: IOobject.H:183
addRegionOption.H
Foam::ZoneMesh
A list of mesh zones.
Definition: cellZoneMeshFwd.H:38
Foam::polyMesh::UNCHANGED
@ UNCHANGED
Definition: polyMesh.H:88
Foam::polyMesh::faceZones
const faceZoneMesh & faceZones() const noexcept
Definition: polyMesh.H:482
Foam::polyMesh::TOPO_CHANGE
@ TOPO_CHANGE
Definition: polyMesh.H:90
Foam::timeSelector::selectIfPresent
static instantList selectIfPresent(Time &runTime, const argList &args)
Definition: timeSelector.C:259
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::topoSetSource::SUBSET
@ SUBSET
Union of elements with current set.
Definition: topoSetSource.H:104
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:119
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::topoSetSource::SUBTRACT
@ SUBTRACT
Subtract elements from current set.
Definition: topoSetSource.H:101
zoneID
const labelIOList & zoneID
Definition: interpolatedFaces.H:22
dictIO
IOobject dictIO
Definition: setConstantMeshDictionaryIO.H:1
Foam::globalMeshData::nTotalPoints
label nTotalPoints() const noexcept
Definition: globalMeshData.H:354
Foam::topoSetSource::New
static autoPtr< topoSetSource > New(const word &topoSetSourceType, const polyMesh &mesh, const dictionary &dict)
Definition: topoSetSource.C:103
Foam
Definition: atmBoundaryLayer.C:26
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:139
Foam::ZoneMesh::findZoneID
label findZoneID(const word &zoneName) const
Definition: ZoneMesh.C:512
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:51
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:790
Foam::polyMesh::readUpdateState
readUpdateState
Definition: polyMesh.H:86
Foam::polyMesh::bounds
const boundBox & bounds() const
Definition: polyMesh.H:446
Foam::IOobject::name
const word & name() const noexcept
Definition: IOobjectI.H:58
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Definition: argList.C:317
IOdictionary.H
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:49
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Definition: timeSelector.C:95
cellZoneSet.H
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::Time::setTime
virtual void setTime(const Time &t)
Definition: Time.C:996
topoSetSource.H
Foam::topoSetSource::REMOVE
@ REMOVE
Remove the set (from the file system)
Definition: topoSetSource.H:107
Foam::word::null
static const word null
Definition: word.H:78
timeSelector.H
Foam::polyMesh::pointZones
const pointZoneMesh & pointZones() const noexcept
Definition: polyMesh.H:476
createTime.H
Foam::dictionary::optionalSubDict
const dictionary & optionalSubDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.C:570
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Definition: foamVtkOutputTemplates.C:29
Foam::autoPtr::clear
void clear() noexcept
Definition: autoPtr.H:172
Foam::topoSetSource::INVERT
@ INVERT
Invert the elements in the current set.
Definition: topoSetSource.H:105
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
Foam::fvMesh::time
const Time & time() const
Definition: fvMesh.H:276
Foam::globalMeshData::nTotalCells
label nTotalCells() const noexcept
Definition: globalMeshData.H:367
Foam::ZoneMesh::clearAddressing
void clearAddressing()
Definition: ZoneMesh.C:702
Foam::polyMesh::globalData
const globalMeshData & globalData() const
Definition: polyMesh.C:1288
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Definition: argList.C:328
args
Foam::argList args(argc, argv)
WarningInFunction
#define WarningInFunction
Definition: messageStream.H:353
pointZoneSet.H
Foam::globalMeshData::nTotalFaces
label nTotalFaces() const noexcept
Definition: globalMeshData.H:361
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:181
Foam::topoSetSource::actionNames
static const Enum< setAction > actionNames
Definition: topoSetSource.H:114