vtkWriteUpdate.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) 2018-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "vtkWrite.H"
29 #include "cellBitSet.H"
30 #include "topoSetCellSource.H"
31 
32 // * * * * * * * * * * * * * * Local Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  // A limited selection of actions
37  const Enum<topoSetSource::setAction> actionNames
38  ({
39  { topoSetSource::NEW, "use" }, // Reuse NEW for "use" action name
40  { topoSetSource::ADD, "add" },
41  { topoSetSource::SUBTRACT, "subtract" },
42  { topoSetSource::SUBSET, "subset" },
43  { topoSetSource::INVERT, "invert" },
44  });
45 }
46 
47 
48 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
49 
50 bool Foam::functionObjects::vtkWrite::updateSubset
51 (
52  fvMeshSubset& subsetter
53 ) const
54 {
55  if (selection_.empty())
56  {
57  return false;
58  }
59 
60  const fvMesh& mesh = subsetter.baseMesh();
61 
62  // Start with all cells unselected
63  cellBitSet cellsToSelect(mesh, false);
64 
65  for (const entry& dEntry : selection_)
66  {
67  if (!dEntry.isDict())
68  {
70  << "Ignoring non-dictionary entry "
71  << dEntry << endl;
72  continue;
73  }
74 
75  const dictionary& dict = dEntry.dict();
76 
77  const auto action = actionNames.get("action", dict);
78 
79  // Handle manually
80  if (action == topoSetSource::INVERT)
81  {
82  cellsToSelect.invert(mesh.nCells());
83  continue;
84  }
85 
86  auto source = topoSetCellSource::New
87  (
88  dict.get<word>("source"),
89  mesh,
90  dict.optionalSubDict("sourceInfo")
91  );
92  source->verbose(false);
93 
94  switch (action)
95  {
96  case topoSetSource::NEW: // "use"
97  case topoSetSource::ADD:
99  if (topoSetSource::NEW == action)
100  {
101  // "use": only use this selection (clear + ADD)
102  // NEW is handled like ADD in applyToSet()
103  cellsToSelect.reset();
104  }
105  source->applyToSet(action, cellsToSelect);
106  break;
107 
109  {
110  cellBitSet other(mesh, false);
111  source->applyToSet(topoSetSource::NEW, other);
112 
113  cellsToSelect.subset(other);
114  }
115  break;
116 
117  default:
118  // Should already have been caught
120  << "Ignoring unhandled action '"
121  << actionNames[action] << "'" << endl;
122  break;
123  }
124  }
125 
126  subsetter.setCellSubset(cellsToSelect.addressing());
127 
128  return true;
129 }
130 
131 
132 Foam::labelList Foam::functionObjects::vtkWrite::getSelectedPatches
133 (
134  const polyBoundaryMesh& patches
135 ) const
136 {
137  DynamicList<label> patchIDs(patches.size());
138 
139  for (const polyPatch& pp : patches)
140  {
141  if (isType<emptyPolyPatch>(pp))
142  {
143  continue;
144  }
145  else if (isA<processorPolyPatch>(pp))
146  {
147  break; // No processor patches
148  }
149 
150  if
151  (
152  selectPatches_.size()
153  ? selectPatches_.match(pp.name())
154  : true
155  )
156  {
157  patchIDs.append(pp.index());
158  }
159  }
160 
161  return patchIDs.shrink();
162 }
163 
164 
165 bool Foam::functionObjects::vtkWrite::update()
166 {
167  if
168  (
169  meshState_ == polyMesh::UNCHANGED
170  && (meshes_.size() == meshSubsets_.size())
171  && (meshes_.size() == vtuMappings_.size())
172  )
173  {
174  return false;
175  }
176 
177  meshSubsets_.resize(meshes_.size());
178  vtuMappings_.resize(meshes_.size());
179 
180  label regioni = 0;
181  for (const word& regionName : meshes_.sortedToc())
182  {
183  const fvMesh& mesh = *(meshes_[regionName]);
184 
185  if (meshSubsets_.set(regioni))
186  {
187  meshSubsets_[regioni].clear();
188  }
189  else
190  {
191  // Mesh subsetting, or pass through
192  meshSubsets_.set(regioni, new fvMeshSubset(mesh));
193  }
194 
195  if (vtuMappings_.set(regioni))
196  {
197  // Trigger change for vtk cells too
198  vtuMappings_[regioni].clear();
199  }
200  else
201  {
202  // VTU sizing and decomposition information
203  vtuMappings_.set
204  (
205  regioni,
206  new vtk::vtuCells(writeOpts_, decompose_)
207  );
208  }
209 
210  ++regioni;
211  }
212 
213  regioni = 0;
214  for (auto& subsetter : meshSubsets_)
215  {
216  updateSubset(subsetter);
217  vtuMappings_[regioni].reset(subsetter.mesh());
218  ++regioni;
219  }
220 
221  meshState_ = polyMesh::UNCHANGED;
222  return true;
223 }
224 
225 
226 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
227 
228 bool Foam::functionObjects::vtkWrite::readSelection(const dictionary& dict)
229 {
230  meshSubsets_.clear();
231  vtuMappings_.clear();
232  meshState_ = polyMesh::TOPO_CHANGE;
233 
234  // All possible meshes
235  meshes_ = time_.lookupClass<fvMesh>();
236 
237  selectRegions_.clear();
238  dict.readIfPresent("regions", selectRegions_);
239 
240  if (selectRegions_.empty())
241  {
242  selectRegions_.resize(1);
243  selectRegions_.first() =
244  dict.getOrDefault<word>("region", polyMesh::defaultRegion);
245  }
246 
247  // Restrict to specified meshes
248  meshes_.filterKeys(selectRegions_);
249 
250  if (meshes_.empty())
251  {
253  << "No mesh regions selected for function object " << name()
254  << nl;
255  }
256 
257  selectPatches_.clear();
258  dict.readIfPresent("patches", selectPatches_);
259 
260  selectFields_.clear();
261  dict.readEntry("fields", selectFields_);
262  selectFields_.uniq();
263 
264  // Actions to define selection
265  selection_ = dict.subOrEmptyDict("selection");
266 
267  return true;
268 }
269 
270 
272 {
273  meshState_ = polyMesh::TOPO_CHANGE;
274 }
275 
276 
278 {
279  // Only move to worse states
280  if (meshState_ == polyMesh::UNCHANGED)
281  {
282  meshState_ = polyMesh::POINTS_MOVED;
283  }
284 }
285 
286 
287 // ************************************************************************* //
Foam::topoSetSource::ADD
Add elements to current set.
Definition: topoSetSource.H:103
cellBitSet.H
vtkWrite.H
Foam::polyMesh::POINTS_MOVED
Definition: polyMesh.H:93
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:318
Foam::functionObjects::vtkWrite::movePoints
virtual void movePoints(const polyMesh &mesh)
Update for mesh point-motion.
Definition: vtkWriteUpdate.C:277
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::topoSetSource::NEW
Create a new set and ADD elements to it.
Definition: topoSetSource.H:104
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
regionName
Foam::word regionName
Definition: createNamedDynamicFvMesh.H:1
Foam::functionObjects::vtkWrite::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Definition: vtkWriteUpdate.C:271
Foam::polyMesh::UNCHANGED
Definition: polyMesh.H:92
Foam::polyMesh::TOPO_CHANGE
Definition: polyMesh.H:94
Foam::topoSetCellSource::New
static autoPtr< topoSetCellSource > New(const word &sourceType, const polyMesh &mesh, const dictionary &dict)
Return a reference to the selected source type.
Definition: topoSetCellSource.C:62
topoSetCellSource.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::topoSetSource::SUBSET
Union of elements with current set.
Definition: topoSetSource.H:108
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::topoSetSource::SUBTRACT
Subtract elements from current set.
Definition: topoSetSource.H:105
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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:63
patches
const polyBoundaryMesh & patches
Definition: convertProcessorPatches.H:65
Foam::topoSetSource::INVERT
Invert the elements in the current set.
Definition: topoSetSource.H:109
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:161
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::actionNames
const Enum< topoSetSource::setAction > actionNames({ { topoSetSource::NEW, "use" }, { topoSetSource::ADD, "add" }, { topoSetSource::SUBTRACT, "subtract" }, { topoSetSource::SUBSET, "subset" }, { topoSetSource::INVERT, "invert" }, })
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328