contourFilter.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) 2019 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 // OpenFOAM includes
29 #include "contourFilter.H"
30 #include "runTimePostProcessing.H"
32 
33 // VTK includes
34 #include "vtkActor.h"
35 #include "vtkCellDataToPointData.h"
36 #include "vtkCompositeDataGeometryFilter.h"
37 #include "vtkCompositeDataSet.h"
38 #include "vtkCompositePolyDataMapper.h"
39 #include "vtkContourFilter.h"
40 #include "vtkMultiPieceDataSet.h"
41 #include "vtkPolyData.h"
42 #include "vtkPolyDataMapper.h"
43 #include "vtkRenderer.h"
44 #include "vtkSmartPointer.h"
45 
46 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 namespace functionObjects
51 {
52 namespace runTimePostPro
53 {
56 }
57 }
58 }
59 
60 
61 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
62 
64 (
65  const runTimePostProcessing& parent,
66  const dictionary& dict,
67  const HashPtrTable<Function1<vector>>& colours
68 )
69 :
70  volumeFilter(parent, dict, colours),
71  fieldVisualisationBase(dict, colours),
72  colourFieldName_(dict.get<word>("colourField")),
73  values_()
74 {
75  dict.readEntry("values", values_);
76 
77  // Extra safety
78  if (values_.empty())
79  {
80  values_.resize(1);
81  values_.first() = Zero;
82  }
83 }
84 
85 
86 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
87 
90 (
91  const scalar position,
92  vtkRenderer* renderer
93 )
94 {
95  if (!visible_)
96  {
97  return false;
98  }
99 
100  if (needsCollective())
101  {
102  Info<< type() << " : Not available for collective operation" << endl;
103  return false;
104  }
105 
106  DebugInfo << " Adding iso-surface" << endl;
107 
108  // Bookkeeping for vtkUnstructuredGrid
109  vtk::vtuAdaptor adaptor;
110  vtkSmartPointer<vtkMultiPieceDataSet> multiPiece = mesh(adaptor);
111 
112 
113  // Add (scalar/vector) field.
114  // - always need field(s) for glyphs or colourByField:
115 
116  int nCmpt = 0;
117  {
118  const auto* ioptr =
119  parent().mesh().cfindObject<regIOobject>(fieldName_);
120 
121  if (!nCmpt)
122  {
123  nCmpt = addDimField<scalar>
124  (
125  multiPiece, adaptor, ioptr, fieldName_
126  );
127  }
128  if (!nCmpt)
129  {
130  nCmpt = addDimField<vector>
131  (
132  multiPiece, adaptor, ioptr, fieldName_
133  );
134  }
135  }
136 
137 
138  // If the input is vector, need magnitude
139 
140  word magFieldName = fieldName_;
141 
142  if (nCmpt == 3)
143  {
144  addMagField(fieldName_, multiPiece);
145  magFieldName = "mag(" + fieldName_ + ")";
146  }
147 
148  // Colouring
149  nCmpt = 0;
150  if (colourBy_ == cbField && fieldName_ != colourFieldName_)
151  {
152  const auto* ioptr =
153  parent().mesh().cfindObject<regIOobject>(fieldName_);
154 
155  if (!nCmpt)
156  {
157  nCmpt = addDimField<scalar>
158  (
159  multiPiece, adaptor, ioptr, colourFieldName_
160  );
161  }
162  if (!nCmpt)
163  {
164  nCmpt = addDimField<vector>
165  (
166  multiPiece, adaptor, ioptr, colourFieldName_
167  );
168  }
169  }
170 
171 
172  // Now have a multi-piece dataset that is one of the following:
173  //
174  // - one-piece per processor (OpenFOAM = parallel, VTK=parallel)
175 
176 
177  // Re-query field information - we may have stored it differently
178  // than the original source.
179 
180  fieldSummary fieldInfo = queryFieldSummary(magFieldName, multiPiece);
181  fieldInfo.reduce();
182 
183  fieldSummary colourFieldInfo =
184  queryFieldSummary(colourFieldName_, multiPiece);
185  colourFieldInfo.reduce();
186 
187 
188  DebugInfo
189  << " Field " << fieldName_ << ' ' << fieldInfo.info() << nl
190  << " Field " << colourFieldName_ << ' ' << colourFieldInfo.info()
191  << endl;
192 
193 
194  // Not rendered on this processor?
195  // This is where we stop, but could also have an MPI barrier
196  if (!renderer)
197  {
198  return true;
199  }
200 
201 
202  // Rendering
203  {
205 
207 
208  // CellData - Need a cell->point filter
209  if (!fieldInfo.hasPointData() || !colourFieldInfo.hasPointData())
210  {
212  cellToPoint->SetInputData(multiPiece);
213 
214  contour->SetInputConnection(cellToPoint->GetOutputPort());
215  }
216  else
217  {
218  contour->SetInputData(multiPiece);
219  }
220 
221  contour->SetNumberOfContours(values_.size());
222  forAll(values_, valuei)
223  {
224  contour->SetValue(valuei, values_[valuei]);
225  }
226 
227  contour->SetInputArrayToProcess
228  (
229  0, // index: scalars(0)
230  0, // port
231  0, // connection
232  vtkDataObject::FIELD_ASSOCIATION_POINTS,
233  magFieldName.c_str()
234  );
235 
236  contour->Modified();
237  contour->Update();
238 
240 
241  polyData->SetInputConnection(contour->GetOutputPort());
242  polyData->Update();
243 
245  mapper->SetInputConnection(polyData->GetOutputPort());
246 
247  if (representation_ == rtGlyph)
248  {
249  addGlyphs
250  (
251  position,
252  colourFieldName_, colourFieldInfo, // scaling
253  colourFieldName_, colourFieldInfo, // colouring
254  maxGlyphLength_,
255  polyData->GetOutput(),
256  surfaceActor_,
257  renderer
258  );
259  }
260  else
261  {
262  setField
263  (
264  position,
265  colourFieldName_,
267  mapper,
268  renderer
269  );
270 
271  surfaceActor_->SetMapper(mapper);
272 
273  setRepresentation(surfaceActor_);
274 
275  renderer->AddActor(surfaceActor_);
276  }
277  }
278 
279  return true;
280 }
281 
282 
285 (
286  const scalar position,
287  vtkRenderer* renderer
288 )
289 {
290  if (visible_)
291  {
292  // Live source
293  if (addGeometry(position, renderer))
294  {
295  return;
296  }
297 
299  << "Unsupported for OpenFOAM parallel and VTK serial"
300  << endl;
301  }
302 }
303 
304 
306 {
307  return true;
308 }
309 
310 
311 // ************************************************************************* //
Foam::functionObjects::runTimePostPro::contourFilter::addGeometryToScene
virtual void addGeometryToScene(const scalar position, vtkRenderer *renderer)
Add cutting planes to scene (using simulation source)
Definition: contourFilter.C:285
Foam::functionObjects::runTimePostPro::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surface, contourFilter, dictionary)
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::functionObjects::runTimePostPro::fieldVisualisationBase::fieldSummary
General field characteristics.
Definition: fieldVisualisationBase.H:172
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::expressions::patchExpr::POINT_DATA
Point data.
Definition: patchExprFwd.H:60
Foam::cellToPoint
A topoSetPointSource to select points based on usage in cells.
Definition: cellToPoint.H:83
Foam::functionObjects::fieldInfo
Helper class to store a wordRe and label used by Foam::functionObjects::fieldSelection.
Definition: fieldInfo.H:56
Foam::functionObjects::runTimePostPro::fieldVisualisationBase::fieldSummary::reduce
void reduce()
Parallel reduction. A no-op if Pstream::parRun() is false.
Definition: fieldVisualisationBase.C:363
Foam::functionObjects::runTimePostPro::contourFilter::addGeometry
bool addGeometry(const scalar position, vtkRenderer *renderer)
Add cutting planes to scene (using simulation source)
Definition: contourFilter.C:90
vtkSmartPointer
Definition: runTimePostProcessing.H:148
contourFilter.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:56
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::functionObjects::runTimePostPro::contourFilter
Iso-surface contours of OpenFOAM volume fields.
Definition: contourFilter.H:89
Foam::functionObjects::runTimePostPro::contourFilter::clear
virtual bool clear()
Add cutting planes to scene (using simulation source)
Definition: contourFilter.C:305
Foam::functionObjects::runTimePostProcessing
Generate images during run-time.
Definition: runTimePostProcessing.H:170
Foam::functionObjects::runTimePostPro::surface
Visualisation of surface data with additional routines for handling parallel distributed data.
Definition: surface.H:180
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::functionObjects::runTimePostPro::fieldVisualisationBase::fieldSummary::info
InfoProxy< fieldSummary > info() const
Definition: fieldVisualisationBase.H:213
setField
surfacesMesh setField(triSurfaceToAgglom)
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::functionObjects::runTimePostPro::fieldVisualisationBase
Base class for scene objects.
Definition: fieldVisualisationBase.H:125
runTimePostProcessing.H
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::objectRegistry::cfindObject
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:390
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>.
Definition: HashPtrTable.H:54
Foam::functionObjects::runTimePostPro::defineTypeName
defineTypeName(contourFilter)
Foam::vtk::vtuAdaptor
Bookkeeping for vtkUnstructuredGrid.
Definition: foamVtkVtuAdaptor.H:78
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::functionObjects::runTimePostPro::contourFilter::contourFilter
contourFilter(const contourFilter &)=delete
No copy construct.
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::functionObjects::runTimePostPro::fieldVisualisationBase::fieldSummary::hasPointData
bool hasPointData() const
True if there is a POINT_DATA association.
Definition: fieldVisualisationBase.H:208
Foam::functionObjects::runTimePostProcessing::mesh
const fvMesh & mesh() const
Reference to the underlying OpenFOAM mesh.
Definition: runTimePostProcessing.H:259
Foam::functionObjects::runTimePostPro::volumeFilter
Visualisation of OpenFOAM volume fields as surface data using a VTK filter cascade.
Definition: volumeFilter.H:74