Dune Core Modules (2.6.0)

subsamplingvtkwriter.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_SUBSAMPLINGVTKWRITER_HH
5 #define DUNE_SUBSAMPLINGVTKWRITER_HH
6 
7 #include <ostream>
8 #include <memory>
9 
10 #include <dune/common/indent.hh>
11 #include <dune/geometry/type.hh>
14 #include <dune/grid/io/file/vtk/vtuwriter.hh>
15 
22 namespace Dune
23 {
35  template< class GridView >
37  : public VTKWriter<GridView>
38  {
39  typedef VTKWriter<GridView> Base;
40  enum { dim = GridView::dimension };
41  enum { dimw = GridView::dimensionworld };
42  typedef typename GridView::Grid::ctype ctype;
43  typedef typename GridView::template Codim< 0 >::Entity Entity;
45  typedef typename Refinement::IndexVector IndexVector;
46  typedef typename Refinement::ElementIterator SubElementIterator;
47  typedef typename Refinement::VertexIterator SubVertexIterator;
48 
49  typedef typename Base::CellIterator CellIterator;
50  typedef typename Base::FunctionIterator FunctionIterator;
51  using Base::cellBegin;
52  using Base::cellEnd;
53  using Base::celldata;
54  using Base::ncells;
55  using Base::ncorners;
56  using Base::nvertices;
57  using Base::outputtype;
58  using Base::vertexBegin;
59  using Base::vertexEnd;
60  using Base::vertexdata;
61 
62  public:
77  explicit SubsamplingVTKWriter (const GridView &gridView,
78  Dune::RefinementIntervals intervals_, bool coerceToSimplex_ = false)
79  : Base(gridView, VTK::nonconforming)
80  , intervals(intervals_), coerceToSimplex(coerceToSimplex_)
81  {
82  if(intervals_.intervals() < 1) {
83  DUNE_THROW(Dune::IOError,"SubsamplingVTKWriter: Negative Subsampling " << intervals_.intervals() << " must not be used!");
84  }
85  }
98  DUNE_DEPRECATED_MSG("SubsampligVTKWriter(GV,int,bool) is deprecated, use SubsamplingVTKWriter(GV,Dune::refinement{Intervals|Levels}(int),bool)")
99  explicit SubsamplingVTKWriter (const GridView &gridView,
100  int level_, bool coerceToSimplex_ = false)
101  : SubsamplingVTKWriter(gridView, Dune::refinementIntervals(1<<level_), coerceToSimplex_)
102  { }
103 
104  private:
105  GeometryType subsampledGeometryType(GeometryType geometryType)
106  {
107  return (geometryType.isCube() && !coerceToSimplex ? geometryType : GeometryTypes::simplex(dim));
108  }
109 
110  template<typename SubIterator>
111  struct IteratorSelector
112  {};
113 
114  SubElementIterator refinementBegin(const Refinement& refinement, Dune::RefinementIntervals intervals, IteratorSelector<SubElementIterator>)
115  {
116  return refinement.eBegin(intervals);
117  }
118 
119  SubVertexIterator refinementBegin(const Refinement& refinement, Dune::RefinementIntervals intervals, IteratorSelector<SubVertexIterator>)
120  {
121  return refinement.vBegin(intervals);
122  }
123 
124  SubElementIterator refinementEnd(const Refinement& refinement, Dune::RefinementIntervals intervals, IteratorSelector<SubElementIterator>)
125  {
126  return refinement.eEnd(intervals);
127  }
128 
129  SubVertexIterator refinementEnd(const Refinement& refinement, Dune::RefinementIntervals intervals, IteratorSelector<SubVertexIterator>)
130  {
131  return refinement.vEnd(intervals);
132  }
133 
134  template<typename Data, typename Iterator, typename SubIterator>
135  void writeData(VTK::VTUWriter& writer, const Data& data, const Iterator begin, const Iterator end, int nentries, IteratorSelector<SubIterator> sis)
136  {
137  for (auto it = data.begin(),
138  iend = data.end();
139  it != iend;
140  ++it)
141  {
142  const auto& f = *it;
143  VTK::FieldInfo fieldInfo = f.fieldInfo();
144  std::size_t writecomps = fieldInfo.size();
145  switch (fieldInfo.type())
146  {
148  break;
150  // vtk file format: a vector data always should have 3 comps (with
151  // 3rd comp = 0 in 2D case)
152  if (writecomps > 3)
153  DUNE_THROW(IOError,"Cannot write VTK vectors with more than 3 components (components was " << writecomps << ")");
154  writecomps = 3;
155  break;
157  DUNE_THROW(NotImplemented,"VTK output for tensors not implemented yet");
158  }
159  std::shared_ptr<VTK::DataArrayWriter<float> > p
160  (writer.makeArrayWriter<float>(f.name(), writecomps, nentries));
161  if(!p->writeIsNoop())
162  for (Iterator eit = begin; eit!=end; ++eit)
163  {
164  const Entity & e = *eit;
165  f.bind(e);
166  Refinement &refinement =
167  buildRefinement<dim, ctype>(eit->type(),
168  subsampledGeometryType(eit->type()));
169  for(SubIterator sit = refinementBegin(refinement,intervals,sis),
170  send = refinementEnd(refinement,intervals,sis);
171  sit != send;
172  ++sit)
173  {
174  f.write(sit.coords(),*p);
175  // expand 2D-Vectors to 3D for VTK format
176  for(unsigned j = f.fieldInfo().size(); j < writecomps; j++)
177  p->write(0.0);
178  }
179  f.unbind();
180  }
181  }
182  }
183 
184 
185  protected:
187  virtual void countEntities(int &nvertices, int &ncells, int &ncorners);
188 
190  virtual void writeCellData(VTK::VTUWriter& writer);
191 
193  virtual void writeVertexData(VTK::VTUWriter& writer);
194 
196  virtual void writeGridPoints(VTK::VTUWriter& writer);
197 
199  virtual void writeGridCells(VTK::VTUWriter& writer);
200 
201  public:
202  using Base::addVertexData;
203  using Base::addCellData;
204 
205  private:
206  // hide addVertexData -- adding raw data directly without a VTKFunction
207  // currently does not make sense for subsampled meshes, as the higher order
208  // information is missing. See FS#676.
209  template<class V>
210  void addVertexData (const V& v, const std::string &name, int ncomps=1);
211  template<class V>
212  void addCellData (const V& v, const std::string &name, int ncomps=1);
213 
214  Dune::RefinementIntervals intervals;
215  bool coerceToSimplex;
216  };
217 
219  template <class GridView>
220  void SubsamplingVTKWriter<GridView>::countEntities(int &nvertices, int &ncells, int &ncorners)
221  {
222  nvertices = 0;
223  ncells = 0;
224  ncorners = 0;
225  for (CellIterator it=this->cellBegin(); it!=cellEnd(); ++it)
226  {
227  Refinement &refinement = buildRefinement<dim, ctype>(it->type(), subsampledGeometryType(it->type()));
228 
229  ncells += refinement.nElements(intervals);
230  nvertices += refinement.nVertices(intervals);
231  ncorners += refinement.nElements(intervals) * refinement.eBegin(intervals).vertexIndices().size();
232  }
233  }
234 
235 
237  template <class GridView>
239  {
240  if(celldata.size() == 0)
241  return;
242 
243  // Find the names of the first scalar and vector data fields.
244  // These will be marked as the default fields (the ones that ParaView shows
245  // when the file has just been opened).
246  std::string defaultScalarField, defaultVectorField;
247  std::tie(defaultScalarField, defaultVectorField) = this->getDataNames(celldata);
248 
249  writer.beginCellData(defaultScalarField, defaultVectorField);
250  writeData(writer,celldata,cellBegin(),cellEnd(),ncells,IteratorSelector<SubElementIterator>());
251  writer.endCellData();
252  }
253 
255  template <class GridView>
257  {
258  if(vertexdata.size() == 0)
259  return;
260 
261  // Find the names of the first scalar and vector data fields.
262  // These will be marked as the default fields (the ones that ParaView shows
263  // when the file has just been opened).
264  std::string defaultScalarField, defaultVectorField;
265  std::tie(defaultScalarField, defaultVectorField) = this->getDataNames(vertexdata);
266 
267  writer.beginPointData(defaultScalarField, defaultVectorField);
268  writeData(writer,vertexdata,cellBegin(),cellEnd(),nvertices,IteratorSelector<SubVertexIterator>());
269  writer.endPointData();
270  }
271 
273  template <class GridView>
275  {
276  writer.beginPoints();
277 
278  std::shared_ptr<VTK::DataArrayWriter<float> > p
279  (writer.makeArrayWriter<float>("Coordinates", 3, nvertices));
280  if(!p->writeIsNoop())
281  for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
282  {
283  Refinement &refinement =
284  buildRefinement<dim, ctype>(i->type(),
285  subsampledGeometryType(i->type()));
286  for(SubVertexIterator sit = refinement.vBegin(intervals),
287  send = refinement.vEnd(intervals);
288  sit != send; ++sit)
289  {
290  FieldVector<ctype, dimw> coords = i->geometry().global(sit.coords());
291  for (int j=0; j<std::min(int(dimw),3); j++)
292  p->write(coords[j]);
293  for (int j=std::min(int(dimw),3); j<3; j++)
294  p->write(0.0);
295  }
296  }
297  // free the VTK::DataArrayWriter before touching the stream
298  p.reset();
299 
300  writer.endPoints();
301  }
302 
304  template <class GridView>
306  {
307  writer.beginCells();
308 
309  // connectivity
310  {
311  std::shared_ptr<VTK::DataArrayWriter<int> > p1
312  (writer.makeArrayWriter<int>("connectivity", 1, ncorners));
313  // The offset within the index numbering
314  if(!p1->writeIsNoop()) {
315  int offset = 0;
316  for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
317  {
318  GeometryType coercedToType = subsampledGeometryType(i->type());
319  Refinement &refinement =
320  buildRefinement<dim, ctype>(i->type(), coercedToType);
321  for(SubElementIterator sit = refinement.eBegin(intervals),
322  send = refinement.eEnd(intervals);
323  sit != send; ++sit)
324  {
325  IndexVector indices = sit.vertexIndices();
326  for(unsigned int ii = 0; ii < indices.size(); ++ii)
327  p1->write(offset+indices[VTK::renumber(coercedToType, ii)]);
328  }
329  offset += refinement.nVertices(intervals);
330  }
331  }
332  }
333 
334  // offsets
335  {
336  std::shared_ptr<VTK::DataArrayWriter<int> > p2
337  (writer.makeArrayWriter<int>("offsets", 1, ncells));
338  if(!p2->writeIsNoop()) {
339  // The offset into the connectivity array
340  int offset = 0;
341  for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
342  {
343  Refinement &refinement =
344  buildRefinement<dim, ctype>(i->type(),
345  subsampledGeometryType(i->type()));
346  unsigned int verticesPerCell =
347  refinement.eBegin(intervals).vertexIndices().size();
348  for(int element = 0; element < refinement.nElements(intervals);
349  ++element)
350  {
351  offset += verticesPerCell;
352  p2->write(offset);
353  }
354  }
355  }
356  }
357 
358  // types
359  if (dim>1)
360  {
361  std::shared_ptr<VTK::DataArrayWriter<unsigned char> > p3
362  (writer.makeArrayWriter<unsigned char>("types", 1, ncells));
363  if(!p3->writeIsNoop())
364  for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
365  {
366  GeometryType coerceTo = subsampledGeometryType(it->type());
367  Refinement &refinement =
368  buildRefinement<dim, ctype>(it->type(), coerceTo);
369  int vtktype = VTK::geometryType(coerceTo);
370  for(int i = 0; i < refinement.nElements(intervals); ++i)
371  p3->write(vtktype);
372  }
373  }
374 
375  writer.endCells();
376  }
377 }
378 
379 #endif // DUNE_SUBSAMPLINGVTKWRITER_HH
vector space out of a tensor product of fields.
Definition: fvector.hh:93
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:277
Grid view abstract base class.
Definition: gridview.hh:60
Default exception class for I/O errors.
Definition: exceptions.hh:229
Holds the number of refined intervals per axis needed for virtual and static refinement.
Definition: base.cc:92
Writer for the output of subsampled grid functions in the vtk format.
Definition: subsamplingvtkwriter.hh:38
void addVertexData(const std::shared_ptr< const VTKFunction > &p)
Add a grid function that lives on the vertices of the grid to the visualization.
Definition: vtkwriter.hh:634
void addCellData(const std::shared_ptr< const VTKFunction > &p)
Add a grid function that lives on the cells of the grid to the visualization.
Definition: vtkwriter.hh:587
virtual void writeGridPoints(VTK::VTUWriter &writer)
write the positions of vertices
Definition: subsamplingvtkwriter.hh:274
virtual void writeVertexData(VTK::VTUWriter &writer)
write vertex data
Definition: subsamplingvtkwriter.hh:256
SubsamplingVTKWriter(const GridView &gridView, Dune::RefinementIntervals intervals_, bool coerceToSimplex_=false)
Construct a SubsamplingVTKWriter working on a specific GridView.
Definition: subsamplingvtkwriter.hh:77
virtual void countEntities(int &nvertices, int &ncells, int &ncorners)
count the vertices, cells and corners
Definition: subsamplingvtkwriter.hh:220
virtual void writeCellData(VTK::VTUWriter &writer)
write cell data
Definition: subsamplingvtkwriter.hh:238
virtual void writeGridCells(VTK::VTUWriter &writer)
write the connectivity array
Definition: subsamplingvtkwriter.hh:305
Iterator over the grids elements.
Definition: vtkwriter.hh:326
Writer for the ouput of grid functions in the vtk format.
Definition: vtkwriter.hh:88
void addVertexData(const std::shared_ptr< const VTKFunction > &p)
Add a grid function that lives on the vertices of the grid to the visualization.
Definition: vtkwriter.hh:634
void addCellData(const std::shared_ptr< const VTKFunction > &p)
Add a grid function that lives on the cells of the grid to the visualization.
Definition: vtkwriter.hh:587
@ tensor
tensor field (always 3x3)
@ vector
vector-valued field (always 3D, will be padded if necessary)
Dump a .vtu/.vtp files contents to a stream.
Definition: vtuwriter.hh:96
DataArrayWriter< T > * makeArrayWriter(const std::string &name, unsigned ncomps, unsigned nitems)
acquire a DataArrayWriter
Definition: vtuwriter.hh:379
void endCellData()
finish CellData section
Definition: vtuwriter.hh:218
void beginCells()
start section for the grid cells/PolyData lines
Definition: vtuwriter.hh:272
void endPointData()
finish PointData section
Definition: vtuwriter.hh:180
void beginCellData(const std::string &scalars="", const std::string &vectors="")
start CellData section
Definition: vtuwriter.hh:203
void beginPointData(const std::string &scalars="", const std::string &vectors="")
start PointData section
Definition: vtuwriter.hh:165
void endPoints()
finish section for the point coordinates
Definition: vtuwriter.hh:247
void endCells()
start section for the grid cells/PolyData lines
Definition: vtuwriter.hh:283
void beginPoints()
start section for the point coordinates
Definition: vtuwriter.hh:236
VirtualRefinement base class.
Definition: virtualrefinement.hh:292
VertexIterator vEnd(int level) const
Get a VertexIterator.
Definition: virtualrefinement.cc:51
ElementIterator eBegin(int level) const
Get an ElementIterator.
Definition: virtualrefinement.cc:66
Codim< 0 >::SubEntityIterator ElementIterator
The ElementIterator of the VirtualRefinement.
Definition: virtualrefinement.hh:299
ElementIterator eEnd(int level) const
Get an ElementIterator.
Definition: virtualrefinement.cc:81
virtual int nVertices(int level) const =0
Get the number of Vertices.
VertexIterator vBegin(int level) const
Get a VertexIterator.
Definition: virtualrefinement.cc:36
std::vector< int > IndexVector
The IndexVector of the VirtualRefinement.
Definition: virtualrefinement.hh:312
Codim< dimension >::SubEntityIterator VertexIterator
The VertexIterator of the VirtualRefinement.
Definition: virtualrefinement.hh:295
virtual int nElements(int level) const =0
Get the number of Elements.
#define DUNE_DEPRECATED_MSG(text)
Mark some entity as deprecated.
Definition: deprecated.hh:169
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
@ dimensionworld
The dimension of the world the grid lives in.
Definition: gridview.hh:131
@ dimension
The dimension of the grid.
Definition: gridview.hh:127
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:696
RefinementIntervals refinementIntervals(int intervals)
Creates a RefinementIntervals object.
Definition: base.cc:106
Utility class for handling nested indentation in output.
Dune namespace.
Definition: alignedallocator.hh:10
Static tag representing a codimension.
Definition: dimension.hh:22
A unique label for each type of element that can occur in a grid.
This file contains the virtual wrapper around refinement.
Provides file i/o for the visualization toolkit.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 3, 22:32, 2024)