Dune Core Modules (2.3.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;
43 typedef typename Refinement::IndexVector IndexVector;
44 typedef typename Refinement::ElementIterator SubElementIterator;
45 typedef typename Refinement::VertexIterator SubVertexIterator;
46
47 typedef typename Base::CellIterator CellIterator;
48 typedef typename Base::FunctionIterator FunctionIterator;
49 using Base::cellBegin;
50 using Base::cellEnd;
51 using Base::celldata;
52 using Base::ncells;
53 using Base::ncorners;
54 using Base::nvertices;
55 using Base::outputtype;
56 using Base::vertexBegin;
57 using Base::vertexEnd;
58 using Base::vertexdata;
59
60 public:
73 explicit SubsamplingVTKWriter (const GridView &gridView,
74 unsigned int level_, bool coerceToSimplex_ = false)
75 : Base(gridView, VTK::nonconforming)
76 , level(level_), coerceToSimplex(coerceToSimplex_)
77 { }
78
79 private:
80 GeometryType subsampledGeometryType(GeometryType geometryType) {
81 if(geometryType.isCube() && !coerceToSimplex) { /* nothing */ }
82 else geometryType.makeSimplex(dim);
83 return geometryType;
84 }
85
86 protected:
88 virtual void countEntities(int &nvertices, int &ncells, int &ncorners);
89
91 virtual void writeCellData(VTK::VTUWriter& writer);
92
94 virtual void writeVertexData(VTK::VTUWriter& writer);
95
97 virtual void writeGridPoints(VTK::VTUWriter& writer);
98
100 virtual void writeGridCells(VTK::VTUWriter& writer);
101
102 public:
104
105 private:
106 // hide addVertexData -- adding vertex data directly without a VTKFunction
107 // currently does not work since the P1VectorWrapper used for that uses a
108 // nearest-neighbour search to find the value for the given point. See
109 // FS#676.
110 template<class V>
111 void addVertexData (const V& v, const std::string &name, int ncomps=1);
112
113 unsigned int level;
114 bool coerceToSimplex;
115 };
116
118 template <class GridView>
119 void SubsamplingVTKWriter<GridView>::countEntities(int &nvertices, int &ncells, int &ncorners)
120 {
121 nvertices = 0;
122 ncells = 0;
123 ncorners = 0;
124 for (CellIterator it=this->cellBegin(); it!=cellEnd(); ++it)
125 {
126 Refinement &refinement = buildRefinement<dim, ctype>(it->type(), subsampledGeometryType(it->type()));
127
128 ncells += refinement.nElements(level);
129 nvertices += refinement.nVertices(level);
130 ncorners += refinement.nElements(level) * refinement.eBegin(level).vertexIndices().size();
131 }
132 }
133
135 template <class GridView>
137 {
138 if(celldata.size() == 0)
139 return;
140
141 std::string scalars = "";
142 for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
143 if ((*it)->ncomps()==1)
144 {
145 scalars = (*it)->name();
146 break;
147 }
148 std::string vectors = "";
149 for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
150 if ((*it)->ncomps()>1)
151 {
152 vectors = (*it)->name();
153 break;
154 }
155
156 writer.beginCellData(scalars, vectors);
157 for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
158 {
159 // vtk file format: a vector data always should have 3 comps (with 3rd
160 // comp = 0 in 2D case)
161 unsigned writecomps = (*it)->ncomps();
162 if(writecomps == 2) writecomps = 3;
163
165 (writer.makeArrayWriter<float>((*it)->name(), writecomps, ncells));
166 if(!p->writeIsNoop())
167 for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
168 {
169 Refinement &refinement =
170 buildRefinement<dim, ctype>(i->type(),
171 subsampledGeometryType(i->type()));
172 for(SubElementIterator sit = refinement.eBegin(level),
173 send = refinement.eEnd(level);
174 sit != send; ++sit)
175 {
176 for (int j=0; j<(*it)->ncomps(); j++)
177 p->write((*it)->evaluate(j,*i,sit.coords()));
178 // expand 2D-Vectors to 3D
179 for(unsigned j = (*it)->ncomps(); j < writecomps; j++)
180 p->write(0.0);
181 }
182 }
183 }
184 writer.endCellData();
185 }
186
188 template <class GridView>
190 {
191 if(vertexdata.size() == 0)
192 return;
193
194 std::string scalars = "";
195 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
196 if ((*it)->ncomps()==1)
197 {
198 scalars = (*it)->name();
199 break;
200 }
201 std::string vectors = "";
202 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
203 if ((*it)->ncomps()>1)
204 {
205 vectors = (*it)->name();
206 break;
207 }
208
209 writer.beginPointData(scalars, vectors);
210 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
211 {
212 // vtk file format: a vector data always should have 3 comps (with 3rd
213 // comp = 0 in 2D case)
214 unsigned writecomps = (*it)->ncomps();
215 if(writecomps == 2) writecomps = 3;
216
218 (writer.makeArrayWriter<float>((*it)->name(), writecomps, nvertices));
219 if(!p->writeIsNoop())
220 for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
221 {
222 Refinement &refinement =
223 buildRefinement<dim, ctype>(i->type(),
224 subsampledGeometryType(i->type()));
225 for(SubVertexIterator sit = refinement.vBegin(level),
226 send = refinement.vEnd(level);
227 sit != send; ++sit)
228 {
229 for (int j=0; j<(*it)->ncomps(); j++)
230 p->write((*it)->evaluate(j,*i,sit.coords()));
231 // vtk file format: a vector data always should have 3 comps (with
232 // 3rd comp = 0 in 2D case)
233 for(unsigned j = (*it)->ncomps(); j < writecomps; j++)
234 p->write(0.0);
235 }
236 }
237 }
238 writer.endPointData();
239 }
240
242 template <class GridView>
244 {
245 writer.beginPoints();
246
248 (writer.makeArrayWriter<float>("Coordinates", 3, nvertices));
249 if(!p->writeIsNoop())
250 for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
251 {
252 Refinement &refinement =
253 buildRefinement<dim, ctype>(i->type(),
254 subsampledGeometryType(i->type()));
255 for(SubVertexIterator sit = refinement.vBegin(level),
256 send = refinement.vEnd(level);
257 sit != send; ++sit)
258 {
259 FieldVector<ctype, dimw> coords = i->geometry().global(sit.coords());
260 for (int j=0; j<std::min(int(dimw),3); j++)
261 p->write(coords[j]);
262 for (int j=std::min(int(dimw),3); j<3; j++)
263 p->write(0.0);
264 }
265 }
266 // free the VTK::DataArrayWriter before touching the stream
267 p.reset();
268
269 writer.endPoints();
270 }
271
273 template <class GridView>
275 {
276 writer.beginCells();
277
278 // connectivity
279 {
281 (writer.makeArrayWriter<int>("connectivity", 1, ncorners));
282 // The offset within the index numbering
283 if(!p1->writeIsNoop()) {
284 int offset = 0;
285 for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
286 {
287 GeometryType coercedToType = subsampledGeometryType(i->type());
288 Refinement &refinement =
289 buildRefinement<dim, ctype>(i->type(), coercedToType);
290 for(SubElementIterator sit = refinement.eBegin(level),
291 send = refinement.eEnd(level);
292 sit != send; ++sit)
293 {
294 IndexVector indices = sit.vertexIndices();
295 for(unsigned int ii = 0; ii < indices.size(); ++ii)
296 p1->write(offset+indices[VTK::renumber(coercedToType, ii)]);
297 }
298 offset += refinement.nVertices(level);
299 }
300 }
301 }
302
303 // offsets
304 {
306 (writer.makeArrayWriter<int>("offsets", 1, ncells));
307 if(!p2->writeIsNoop()) {
308 // The offset into the connectivity array
309 int offset = 0;
310 for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
311 {
312 Refinement &refinement =
313 buildRefinement<dim, ctype>(i->type(),
314 subsampledGeometryType(i->type()));
315 unsigned int verticesPerCell =
316 refinement.eBegin(level).vertexIndices().size();
317 for(int element = 0; element < refinement.nElements(level);
318 ++element)
319 {
320 offset += verticesPerCell;
321 p2->write(offset);
322 }
323 }
324 }
325 }
326
327 // types
328 if (dim>1)
329 {
331 (writer.makeArrayWriter<unsigned char>("types", 1, ncells));
332 if(!p3->writeIsNoop())
333 for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
334 {
335 GeometryType coerceTo = subsampledGeometryType(it->type());
336 Refinement &refinement =
337 buildRefinement<dim, ctype>(it->type(), coerceTo);
338 int vtktype = VTK::geometryType(coerceTo);
339 for(int i = 0; i < refinement.nElements(level); ++i)
340 p3->write(vtktype);
341 }
342 }
343
344 writer.endCells();
345 }
346}
347
348#endif // DUNE_SUBSAMPLINGVTKWRITER_HH
vector space out of a tensor product of fields.
Definition: fvector.hh:92
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:57
@ dimensionworld
The dimension of the world the grid lives in.
Definition: gridview.hh:124
@ dimension
The dimension of the grid.
Definition: gridview.hh:120
Writer for the output of subsampled grid functions in the vtk format.
Definition: subsamplingvtkwriter.hh:37
virtual void writeGridPoints(VTK::VTUWriter &writer)
write the positions of vertices
Definition: subsamplingvtkwriter.hh:243
SubsamplingVTKWriter(const GridView &gridView, unsigned int level_, bool coerceToSimplex_=false)
Construct a SubsamplingVTKWriter working on a specific GridView.
Definition: subsamplingvtkwriter.hh:73
virtual void writeVertexData(VTK::VTUWriter &writer)
write vertex data
Definition: subsamplingvtkwriter.hh:189
virtual void countEntities(int &nvertices, int &ncells, int &ncorners)
count the vertices, cells and corners
Definition: subsamplingvtkwriter.hh:119
virtual void writeCellData(VTK::VTUWriter &writer)
write cell data
Definition: subsamplingvtkwriter.hh:136
virtual void writeGridCells(VTK::VTUWriter &writer)
write the connectivity array
Definition: subsamplingvtkwriter.hh:274
Iterator over the grids elements.
Definition: vtkwriter.hh:112
Writer for the ouput of grid functions in the vtk format.
Definition: vtkwriter.hh:60
void addVertexData(VTKFunction *p)
Add a grid function that lives on the vertices of the grid to the visualization.
Definition: vtkwriter.hh:416
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.
A reference counting smart pointer.
Definition: shared_ptr.hh:64
This file contains the virtual wrapper around refinement.
void reset()
Decrease the reference count by one and free the memory if the reference count has reached 0.
Definition: shared_ptr.hh:354
Utility class for handling nested indentation in output.
Dune namespace.
Definition: alignment.hh:14
A unique label for each type of element that can occur in a grid.
Provides file i/o for the visualization toolkit.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)