Dune Core Modules (2.4.1)

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