Dune Core Modules (2.5.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
22namespace Dune
23{
35 template< class GridView >
37 : public VTKWriter<GridView>
38 {
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:
75 explicit SubsamplingVTKWriter (const GridView &gridView,
76 int level_, bool coerceToSimplex_ = false)
77 : Base(gridView, VTK::nonconforming)
78 , level(level_), coerceToSimplex(coerceToSimplex_)
79 {
80 if(level_ < 0) {
81 DUNE_THROW(Dune::IOError,"SubsamplingVTKWriter: Negative Subsampling " << level_ << " must not be used!");
82 }
83 }
84
85 private:
86 GeometryType subsampledGeometryType(GeometryType geometryType) {
87 if(geometryType.isCube() && !coerceToSimplex) { /* nothing */ }
88 else geometryType.makeSimplex(dim);
89 return geometryType;
90 }
91
92
93 template<typename SubIterator>
94 struct IteratorSelector
95 {};
96
97 SubElementIterator refinementBegin(const Refinement& refinement, int level, IteratorSelector<SubElementIterator>)
98 {
99 return refinement.eBegin(level);
100 }
101
102 SubVertexIterator refinementBegin(const Refinement& refinement, int level, IteratorSelector<SubVertexIterator>)
103 {
104 return refinement.vBegin(level);
105 }
106
107 SubElementIterator refinementEnd(const Refinement& refinement, int level, IteratorSelector<SubElementIterator>)
108 {
109 return refinement.eEnd(level);
110 }
111
112 SubVertexIterator refinementEnd(const Refinement& refinement, int level, IteratorSelector<SubVertexIterator>)
113 {
114 return refinement.vEnd(level);
115 }
116
117 template<typename Data, typename Iterator, typename SubIterator>
118 void writeData(VTK::VTUWriter& writer, const Data& data, const Iterator begin, const Iterator end, int nentries, IteratorSelector<SubIterator> sis)
119 {
120 for (auto it = data.begin(),
121 iend = data.end();
122 it != iend;
123 ++it)
124 {
125 const auto& f = *it;
126 VTK::FieldInfo fieldInfo = f.fieldInfo();
127 std::size_t writecomps = fieldInfo.size();
128 switch (fieldInfo.type())
129 {
131 break;
133 // vtk file format: a vector data always should have 3 comps (with
134 // 3rd comp = 0 in 2D case)
135 if (writecomps > 3)
136 DUNE_THROW(IOError,"Cannot write VTK vectors with more than 3 components (components was " << writecomps << ")");
137 writecomps = 3;
138 break;
140 DUNE_THROW(NotImplemented,"VTK output for tensors not implemented yet");
141 }
142 std::shared_ptr<VTK::DataArrayWriter<float> > p
143 (writer.makeArrayWriter<float>(f.name(), writecomps, nentries));
144 if(!p->writeIsNoop())
145 for (Iterator eit = begin; eit!=end; ++eit)
146 {
147 const Entity & e = *eit;
148 f.bind(e);
149 Refinement &refinement =
150 buildRefinement<dim, ctype>(eit->type(),
151 subsampledGeometryType(eit->type()));
152 for(SubIterator sit = refinementBegin(refinement,level,sis),
153 send = refinementEnd(refinement,level,sis);
154 sit != send;
155 ++sit)
156 {
157 f.write(sit.coords(),*p);
158 // expand 2D-Vectors to 3D for VTK format
159 for(unsigned j = f.fieldInfo().size(); j < writecomps; j++)
160 p->write(0.0);
161 }
162 f.unbind();
163 }
164 }
165 }
166
167
168 protected:
170 virtual void countEntities(int &nvertices, int &ncells, int &ncorners);
171
173 virtual void writeCellData(VTK::VTUWriter& writer);
174
176 virtual void writeVertexData(VTK::VTUWriter& writer);
177
179 virtual void writeGridPoints(VTK::VTUWriter& writer);
180
182 virtual void writeGridCells(VTK::VTUWriter& writer);
183
184 public:
186 using Base::addCellData;
187
188 private:
189 // hide addVertexData -- adding raw data directly without a VTKFunction
190 // currently does not make sense for subsampled meshes, as the higher order
191 // information is missing. See FS#676.
192 template<class V>
193 void addVertexData (const V& v, const std::string &name, int ncomps=1);
194 template<class V>
195 void addCellData (const V& v, const std::string &name, int ncomps=1);
196
197 int level;
198 bool coerceToSimplex;
199 };
200
202 template <class GridView>
203 void SubsamplingVTKWriter<GridView>::countEntities(int &nvertices, int &ncells, int &ncorners)
204 {
205 nvertices = 0;
206 ncells = 0;
207 ncorners = 0;
208 for (CellIterator it=this->cellBegin(); it!=cellEnd(); ++it)
209 {
210 Refinement &refinement = buildRefinement<dim, ctype>(it->type(), subsampledGeometryType(it->type()));
211
212 ncells += refinement.nElements(level);
213 nvertices += refinement.nVertices(level);
214 ncorners += refinement.nElements(level) * refinement.eBegin(level).vertexIndices().size();
215 }
216 }
217
218
220 template <class GridView>
222 {
223 if(celldata.size() == 0)
224 return;
225
226 // Find the names of the first scalar and vector data fields.
227 // These will be marked as the default fields (the ones that ParaView shows
228 // when the file has just been opened).
229 std::string defaultScalarField, defaultVectorField;
230 std::tie(defaultScalarField, defaultVectorField) = this->getDataNames(celldata);
231
232 writer.beginCellData(defaultScalarField, defaultVectorField);
233 writeData(writer,celldata,cellBegin(),cellEnd(),ncells,IteratorSelector<SubElementIterator>());
234 writer.endCellData();
235 }
236
238 template <class GridView>
240 {
241 if(vertexdata.size() == 0)
242 return;
243
244 // Find the names of the first scalar and vector data fields.
245 // These will be marked as the default fields (the ones that ParaView shows
246 // when the file has just been opened).
247 std::string defaultScalarField, defaultVectorField;
248 std::tie(defaultScalarField, defaultVectorField) = this->getDataNames(vertexdata);
249
250 writer.beginPointData(defaultScalarField, defaultVectorField);
251 writeData(writer,vertexdata,cellBegin(),cellEnd(),nvertices,IteratorSelector<SubVertexIterator>());
252 writer.endPointData();
253 }
254
256 template <class GridView>
258 {
259 writer.beginPoints();
260
261 std::shared_ptr<VTK::DataArrayWriter<float> > p
262 (writer.makeArrayWriter<float>("Coordinates", 3, nvertices));
263 if(!p->writeIsNoop())
264 for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
265 {
266 Refinement &refinement =
267 buildRefinement<dim, ctype>(i->type(),
268 subsampledGeometryType(i->type()));
269 for(SubVertexIterator sit = refinement.vBegin(level),
270 send = refinement.vEnd(level);
271 sit != send; ++sit)
272 {
273 FieldVector<ctype, dimw> coords = i->geometry().global(sit.coords());
274 for (int j=0; j<std::min(int(dimw),3); j++)
275 p->write(coords[j]);
276 for (int j=std::min(int(dimw),3); j<3; j++)
277 p->write(0.0);
278 }
279 }
280 // free the VTK::DataArrayWriter before touching the stream
281 p.reset();
282
283 writer.endPoints();
284 }
285
287 template <class GridView>
289 {
290 writer.beginCells();
291
292 // connectivity
293 {
294 std::shared_ptr<VTK::DataArrayWriter<int> > p1
295 (writer.makeArrayWriter<int>("connectivity", 1, ncorners));
296 // The offset within the index numbering
297 if(!p1->writeIsNoop()) {
298 int offset = 0;
299 for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
300 {
301 GeometryType coercedToType = subsampledGeometryType(i->type());
302 Refinement &refinement =
303 buildRefinement<dim, ctype>(i->type(), coercedToType);
304 for(SubElementIterator sit = refinement.eBegin(level),
305 send = refinement.eEnd(level);
306 sit != send; ++sit)
307 {
308 IndexVector indices = sit.vertexIndices();
309 for(unsigned int ii = 0; ii < indices.size(); ++ii)
310 p1->write(offset+indices[VTK::renumber(coercedToType, ii)]);
311 }
312 offset += refinement.nVertices(level);
313 }
314 }
315 }
316
317 // offsets
318 {
319 std::shared_ptr<VTK::DataArrayWriter<int> > p2
320 (writer.makeArrayWriter<int>("offsets", 1, ncells));
321 if(!p2->writeIsNoop()) {
322 // The offset into the connectivity array
323 int offset = 0;
324 for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
325 {
326 Refinement &refinement =
327 buildRefinement<dim, ctype>(i->type(),
328 subsampledGeometryType(i->type()));
329 unsigned int verticesPerCell =
330 refinement.eBegin(level).vertexIndices().size();
331 for(int element = 0; element < refinement.nElements(level);
332 ++element)
333 {
334 offset += verticesPerCell;
335 p2->write(offset);
336 }
337 }
338 }
339 }
340
341 // types
342 if (dim>1)
343 {
344 std::shared_ptr<VTK::DataArrayWriter<unsigned char> > p3
345 (writer.makeArrayWriter<unsigned char>("types", 1, ncells));
346 if(!p3->writeIsNoop())
347 for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
348 {
349 GeometryType coerceTo = subsampledGeometryType(it->type());
350 Refinement &refinement =
351 buildRefinement<dim, ctype>(it->type(), coerceTo);
352 int vtktype = VTK::geometryType(coerceTo);
353 for(int i = 0; i < refinement.nElements(level); ++i)
354 p3->write(vtktype);
355 }
356 }
357
358 writer.endCells();
359 }
360}
361
362#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:268
Grid view abstract base class.
Definition: gridview.hh:60
Default exception class for I/O errors.
Definition: exceptions.hh:229
Writer for the output of subsampled grid functions in the vtk format.
Definition: subsamplingvtkwriter.hh:38
SubsamplingVTKWriter(const GridView &gridView, int level_, bool coerceToSimplex_=false)
Construct a SubsamplingVTKWriter working on a specific GridView.
Definition: subsamplingvtkwriter.hh:75
virtual void writeGridPoints(VTK::VTUWriter &writer)
write the positions of vertices
Definition: subsamplingvtkwriter.hh:257
virtual void writeVertexData(VTK::VTUWriter &writer)
write vertex data
Definition: subsamplingvtkwriter.hh:239
virtual void countEntities(int &nvertices, int &ncells, int &ncorners)
count the vertices, cells and corners
Definition: subsamplingvtkwriter.hh:203
virtual void writeCellData(VTK::VTUWriter &writer)
write cell data
Definition: subsamplingvtkwriter.hh:221
virtual void writeGridCells(VTK::VTUWriter &writer)
write the connectivity array
Definition: subsamplingvtkwriter.hh:288
Iterator over the grids elements.
Definition: vtkwriter.hh:327
Writer for the ouput of grid functions in the vtk format.
Definition: vtkwriter.hh:87
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:632
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:588
@ 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
void endCellData()
finish CellData section
Definition: vtuwriter.hh:218
void beginCells()
start section for the grid cells/PolyData lines
Definition: vtuwriter.hh:272
DataArrayWriter< T > * makeArrayWriter(const std::string &name, unsigned ncomps, unsigned nitems)
acquire a DataArrayWriter
Definition: vtuwriter.hh:379
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:44
ElementIterator eBegin(int level) const
Get an ElementIterator.
Definition: virtualrefinement.cc:52
Codim< 0 >::SubEntityIterator ElementIterator
The ElementIterator of the VirtualRefinement.
Definition: virtualrefinement.hh:299
ElementIterator eEnd(int level) const
Get an ElementIterator.
Definition: virtualrefinement.cc:60
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:297
virtual int nElements(int level) const =0
Get the number of Elements.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
@ dimensionworld
The dimension of the world the grid lives in.
Definition: gridview.hh:132
@ dimension
The dimension of the grid.
Definition: gridview.hh:128
Utility class for handling nested indentation in output.
Dune namespace.
Definition: alignment.hh:11
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.111.3 (Nov 23, 23:29, 2024)