dune-grid  2.8.0
functionwriter.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_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
5 #define DUNE_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
6 
7 #include <cstddef>
8 #include <memory>
9 #include <string>
10 #include <typeinfo>
11 #include <vector>
12 
13 #include <dune/common/exceptions.hh>
14 #include <dune/common/fvector.hh>
15 
16 #include <dune/geometry/referenceelements.hh>
17 
22 
23 namespace Dune
24 {
27 
28  namespace VTK {
29 
31  template<typename Cell_>
33  typedef typename Cell_::Geometry::ctype DF;
34  static const unsigned mydim = Cell_::mydimension;
35  typedef ReferenceElements<DF, mydim> Refelems;
36 
37  public:
38  typedef FieldVector<DF, mydim> Domain;
39  typedef Cell_ Cell;
40 
42  virtual std::string name() const = 0;
43 
45  virtual unsigned ncomps() const = 0;
46 
48  virtual void addArray(PVTUWriter& writer) = 0;
50  virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) = 0;
52 
57  virtual void write(const Cell& /* cell */, const Domain& /* xl */) {
58  DUNE_THROW(NotImplemented, "FunctionWriterBase::write(const Cell&, "
59  "const Domain&): Either the derived class " <<
60  typeid(*this).name() << " failed to implement this method "
61  "or this method is not meant to be called on the derived "
62  "class and was called in error.");
63  }
65 
69  virtual void write(const Cell& cell, unsigned cornerIndex) {
70  write(cell,
71  Refelems::general(cell.type()).position(cornerIndex, mydim));
72  }
74  virtual void endWrite() = 0;
76  virtual ~FunctionWriterBase() {}
77  };
78 
80  //
81  // A Generic Function writer for VTKFunctions
82  //
83 
85  template<typename Func>
87  : public FunctionWriterBase<typename Func::Entity>
88  {
90  std::shared_ptr<const Func> func;
91  VTK::Precision precision_;
92  std::shared_ptr<DataArrayWriter> arraywriter;
93 
94  public:
95  VTKFunctionWriter(const std::shared_ptr<const Func>& func_,
97  : func(func_), precision_(prec)
98  { }
99 
101  virtual std::string name() const { return func->name(); }
102 
104  virtual unsigned ncomps() const {
105  if(func->ncomps() == 2) return 3;
106  else return func->ncomps();
107  }
108 
110  virtual void addArray(PVTUWriter& writer) {
111  writer.addArray(name(), ncomps(), precision_);
112  }
113 
115  virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
116  arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
117  nitems, precision_));
118  return !arraywriter->writeIsNoop();
119  }
120 
122  virtual void write(const typename Base::Cell& cell,
123  const typename Base::Domain& xl) {
124  for(int d = 0; d < func->ncomps(); ++d)
125  arraywriter->write(func->evaluate(d, cell, xl));
126  for(unsigned d = func->ncomps(); d < ncomps(); ++d)
127  arraywriter->write(0);
128  }
129 
131  virtual void endWrite() {
132  arraywriter.reset();
133  }
134  };
135 
137  //
138  // Writers for the grid information
139  //
140 
142  template<typename Cell>
144  : public FunctionWriterBase<Cell>
145  {
147 
148  VTK::Precision precision_;
149  std::shared_ptr<DataArrayWriter> arraywriter;
150 
151  public:
153  : precision_(prec)
154  {}
155 
157  virtual std::string name() const { return "Coordinates"; }
158 
160  virtual unsigned ncomps() const { return 3; }
161 
163  virtual void addArray(PVTUWriter& writer) {
164  writer.addArray(name(), ncomps(), precision_);
165  }
166 
168  virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
169  arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
170  nitems, precision_));
171  return !arraywriter->writeIsNoop();
172  }
174  virtual void write(const typename Base::Cell& cell,
175  const typename Base::Domain& xl) {
176  FieldVector<typename Base::Cell::Geometry::ctype, Base::Cell::Geometry::coorddimension> xg
177  = cell.geometry().global(xl);
178  for(unsigned d = 0; d < 3 && d < Base::Cell::Geometry::coorddimension; ++d)
179  arraywriter->write(xg[d]);
180  for(unsigned d = Base::Cell::Geometry::coorddimension; d < 3; ++d)
181  arraywriter->write(0);
182  }
184  virtual void endWrite() {
185  arraywriter.reset();
186  }
187  };
188 
190  template<typename IteratorFactory>
192  : public FunctionWriterBase<typename IteratorFactory::Cell>
193  {
195  static const unsigned mydim = Base::Cell::mydimension;
196 
197  const IteratorFactory& factory;
198  std::shared_ptr<DataArrayWriter> arraywriter;
199  std::vector<unsigned> pointIndices;
200 
201  public:
203  ConformingConnectivityWriter(const IteratorFactory& factory_)
204  : factory(factory_)
205  { }
206 
208  virtual std::string name() const { return "connectivity"; }
209 
211  virtual unsigned ncomps() const { return 1; }
212 
214  virtual void addArray(PVTUWriter& writer) {
215  writer.addArray(name(), ncomps(), Precision::int32);
216  }
217 
219  virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
220  arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
221  nitems, Precision::int32));
222  if(arraywriter->writeIsNoop())
223  return false;
224 
226  pointIndices.resize(factory.indexSet().size(mydim));
227  const typename IteratorFactory::PointIterator& pend =
228  factory.endPoints();
229  typename IteratorFactory::PointIterator pit = factory.beginPoints();
230  unsigned counter = 0;
231  while(pit != pend) {
232  pointIndices[factory.indexSet().subIndex
233  (pit->cell(), pit->duneIndex(), mydim)] = counter;
234  ++counter;
235  ++pit;
236  }
237  return true;
238  }
240  virtual void write(const typename Base::Cell& cell, unsigned cornerIndex)
241  {
242  // if pointIndices is empty, we're in writeIsNoop mode
243  if(pointIndices.size() == 0)
244  return;
245  arraywriter->write(pointIndices[factory.indexSet().subIndex
246  (cell, cornerIndex, mydim)]);
247  }
249  virtual void endWrite() {
250  arraywriter.reset();
251  pointIndices.clear();
252  }
253  };
254 
256  template<typename Cell>
258  : public FunctionWriterBase<Cell>
259  {
260  std::shared_ptr<DataArrayWriter> arraywriter;
261  unsigned counter;
262 
263  public:
265  virtual std::string name() const { return "connectivity"; }
266 
268  virtual unsigned ncomps() const { return 1; }
269 
271  virtual void addArray(PVTUWriter& writer) {
272  writer.addArray(name(), ncomps(), Precision::int32);
273  }
274 
276  virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
277  arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
278  nitems, Precision::int32));
279  counter = 0;
280  return !arraywriter->writeIsNoop();
281  }
283  virtual void write(const Cell& /* cell */, unsigned /* cornerIndex */)
284  {
285  arraywriter->write(counter);
286  ++counter;
287  }
289  virtual void endWrite() {
290  arraywriter.reset();
291  }
292  };
293 
295  template<typename Cell>
297  : public FunctionWriterBase<Cell>
298  {
300 
301  std::shared_ptr<DataArrayWriter> arraywriter;
302  unsigned offset;
303 
304  public:
306  virtual std::string name() const { return "offsets"; }
307 
309  virtual unsigned ncomps() const { return 1; }
310 
312  virtual void addArray(PVTUWriter& writer) {
313  writer.addArray(name(), ncomps(), Precision::int32);
314  }
315 
317  virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
318  arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
319  nitems, Precision::int32));
320  offset = 0;
321  return !arraywriter->writeIsNoop();
322  }
324  virtual void write(const Cell& cell, const typename Base::Domain&) {
325  offset += cell.geometry().corners();
326  arraywriter->write(offset);
327  }
329  virtual void endWrite() {
330  arraywriter.reset();
331  }
332  };
333 
335  template<typename Cell>
337  : public FunctionWriterBase<Cell>
338  {
340 
341  std::shared_ptr<DataArrayWriter> arraywriter;
342 
343  public:
345  virtual std::string name() const { return "types"; }
346 
348  virtual unsigned ncomps() const { return 1; }
349 
351  virtual void addArray(PVTUWriter& writer) {
352  writer.addArray(name(), ncomps(), Precision::uint8);
353  }
354 
356  virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
357  arraywriter.reset(writer.makeArrayWriter
358  ( name(), ncomps(), nitems, Precision::uint8));
359  return !arraywriter->writeIsNoop();
360  }
362  virtual void write(const Cell& cell, const typename Base::Domain&) {
363  arraywriter->write(geometryType(cell.type()));
364  }
366  virtual void endWrite() {
367  arraywriter.reset();
368  }
369  };
370 
371  } // namespace VTK
372 
374 
375 } // namespace Dune
376 
377 #endif // DUNE_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
Common stuff for the VTKWriter.
Data array writers for the VTKWriter.
Include standard header files.
Definition: agrid.hh:58
Precision
which precision to use when writing out data to vtk files
Definition: common.hh:269
GeometryType geometryType(const Dune::GeometryType &t)
mapping from GeometryType to VTKGeometryType
Definition: common.hh:149
Base class for function writers.
Definition: functionwriter.hh:32
virtual std::string name() const =0
return name
virtual unsigned ncomps() const =0
return number of components of the vector
virtual void write(const Cell &, const Domain &)
write at the given position
Definition: functionwriter.hh:57
Cell_ Cell
Definition: functionwriter.hh:39
virtual void addArray(PVTUWriter &writer)=0
add this field to the given parallel writer
virtual void endWrite()=0
signal end of writing
FieldVector< DF, mydim > Domain
Definition: functionwriter.hh:38
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)=0
start writing with the given writer
virtual ~FunctionWriterBase()
destructor
Definition: functionwriter.hh:76
virtual void write(const Cell &cell, unsigned cornerIndex)
write at the given corner
Definition: functionwriter.hh:69
Base class for function writers.
Definition: functionwriter.hh:88
VTKFunctionWriter(const std::shared_ptr< const Func > &func_, VTK::Precision prec=VTK::Precision::float32)
Definition: functionwriter.hh:95
virtual std::string name() const
return name
Definition: functionwriter.hh:101
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:104
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:115
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:110
virtual void write(const typename Base::Cell &cell, const typename Base::Domain &xl)
write at the given position
Definition: functionwriter.hh:122
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:131
writer for the Coordinates array
Definition: functionwriter.hh:145
virtual void write(const typename Base::Cell &cell, const typename Base::Domain &xl)
write at the given position
Definition: functionwriter.hh:174
virtual std::string name() const
return name
Definition: functionwriter.hh:157
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:184
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:160
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:163
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:168
CoordinatesWriter(VTK::Precision prec=VTK::Precision::float32)
Definition: functionwriter.hh:152
writer for the connectivity array in conforming mode
Definition: functionwriter.hh:193
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:214
virtual std::string name() const
return name
Definition: functionwriter.hh:208
virtual void write(const typename Base::Cell &cell, unsigned cornerIndex)
write at the given corner
Definition: functionwriter.hh:240
ConformingConnectivityWriter(const IteratorFactory &factory_)
create a writer with the given iteratorfactory
Definition: functionwriter.hh:203
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:211
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:219
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:249
writer for the connectivity array in nonconforming mode
Definition: functionwriter.hh:259
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:268
virtual std::string name() const
return name
Definition: functionwriter.hh:265
virtual void write(const Cell &, unsigned)
write at the given corner
Definition: functionwriter.hh:283
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:271
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:276
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:289
writer for the offsets array
Definition: functionwriter.hh:298
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:312
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:329
virtual std::string name() const
return name
Definition: functionwriter.hh:306
virtual void write(const Cell &cell, const typename Base::Domain &)
write at the given position
Definition: functionwriter.hh:324
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:317
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:309
writer for the types array
Definition: functionwriter.hh:338
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:348
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:356
virtual std::string name() const
return name
Definition: functionwriter.hh:345
virtual void write(const Cell &cell, const typename Base::Domain &)
write at the given position
Definition: functionwriter.hh:362
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:351
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:366
Dump a .vtu/.vtp files contents to a stream.
Definition: pvtuwriter.hh:60
void addArray(const std::string &name, unsigned ncomps, Precision prec)
Add an array to the output file.
Definition: pvtuwriter.hh:205
Dump a .vtu/.vtp files contents to a stream.
Definition: vtuwriter.hh:96
DataArrayWriter * makeArrayWriter(const std::string &name, unsigned ncomps, unsigned nitems, Precision prec)
acquire a DataArrayWriter
Definition: vtuwriter.hh:378