dune-grid  2.4
vtkwriter.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_VTKWRITER_HH
5 #define DUNE_VTKWRITER_HH
6 
7 #include <cstring>
8 #include <iostream>
9 #include <string>
10 #include <fstream>
11 #include <sstream>
12 #include <iomanip>
13 
14 #include <vector>
15 #include <list>
16 
17 #include <dune/common/deprecated.hh>
18 #include <dune/common/typetraits.hh>
19 #include <dune/common/exceptions.hh>
20 #include <dune/common/std/memory.hh>
21 #include <dune/common/indent.hh>
22 #include <dune/common/iteratorfacades.hh>
23 #include <dune/common/path.hh>
24 #include <dune/common/shared_ptr.hh>
25 #include <dune/geometry/referenceelements.hh>
34 
48 namespace Dune
49 {
50  // Forward-declaration here, so the class can be friend of VTKWriter
51  template <class GridView>
52  class VTKSequenceWriterBase;
53 
62  template< class GridView >
63  class VTKWriter {
64 
65  // VTKSequenceWriterBase needs getSerialPieceName
66  // and getParallelHeaderName
68 
69  // extract types
70  typedef typename GridView::Grid Grid;
71  typedef typename GridView::ctype DT;
72  enum { n = GridView::dimension };
73  enum { w = GridView::dimensionworld };
74 
75  typedef typename GridView::template Codim< 0 >::Entity Cell;
76  typedef typename GridView::template Codim< n >::Entity Vertex;
77  typedef Cell Entity;
78 
79  typedef typename GridView::IndexSet IndexSet;
80 
81  static const PartitionIteratorType VTK_Partition = InteriorBorder_Partition;
82  //static const PartitionIteratorType VTK_Partition = All_Partition;
83 
84  typedef typename GridView::template Codim< 0 >
85  ::template Partition< VTK_Partition >::Iterator
86  GridCellIterator;
87  typedef typename GridView::template Codim< n >
88  ::template Partition< VTK_Partition >::Iterator
89  GridVertexIterator;
90 
91  typedef typename GridCellIterator::Reference EntityReference;
92 
93  typedef typename GridView::template Codim< 0 >
94  ::Entity::Geometry::LocalCoordinate Coordinate;
95 
96  typedef MultipleCodimMultipleGeomTypeMapper< GridView, MCMGVertexLayout > VertexMapper;
97 
98  // return true if entity should be skipped in Vertex and Corner iterator
99  static bool skipEntity( const PartitionType entityType )
100  {
101  switch( VTK_Partition )
102  {
103  // for All_Partition no entity has to be skipped
104  case All_Partition: return false;
105  case InteriorBorder_Partition: return ( entityType != InteriorEntity );
106  default: DUNE_THROW(NotImplemented,"Add check for this partition type");
107  }
108  return false ;
109  }
110 
111  public:
112 
114  typedef shared_ptr< const VTKFunction > VTKFunctionPtr;
115 
116  protected:
117 
119 
123  {
124 
125  public:
126 
128 
131  {
132 
134  virtual void bind(const Entity& e) const = 0;
135 
137  virtual void unbind() const = 0;
138 
140 
143  virtual void write(const Coordinate& pos, Writer& w, std::size_t count) const = 0;
144 
146  {}
147 
148  };
149 
151  template<typename F>
153  : public FunctionWrapperBase
154  {
155 
156  template<typename F_>
158  : _f(std::forward<F_>(f))
159  {}
160 
161  virtual void bind(const Entity& e) const
162  {
163  _f.bind(e);
164  }
165 
166  virtual void unbind() const
167  {
168  _f.unbind();
169  }
170 
171  virtual void write(const Coordinate& pos, Writer& w, std::size_t count) const
172  {
173  auto r = _f(pos);
174  // we need to do different things here depending on whether r supports indexing into it or not.
175  do_write(w,r,count,is_indexable<decltype(r)>());
176  }
177 
178  private:
179 
180  template<typename R>
181  void do_write(Writer& w, const R& r, std::size_t count, std::true_type) const
182  {
183  for (std::size_t i = 0; i < count; ++i)
184  w.write(r[i]);
185  }
186 
187  template<typename R>
188  void do_write(Writer& w, const R& r, std::size_t count, std::false_type) const
189  {
190  assert(count == 1);
191  w.write(r);
192  }
193 
194  F _f;
195  };
196 
199  : public FunctionWrapperBase
200  {
201  VTKFunctionWrapper(const VTKFunctionPtr& f)
202  : _f(f)
203  , _entity(nullptr)
204  {}
205 
206  virtual void bind(const Entity& e) const
207  {
208  _entity = &e;
209  }
210 
211  virtual void unbind() const
212  {
213  _entity = nullptr;
214  }
215 
216  virtual void write(const Coordinate& pos, Writer& w, std::size_t count) const
217  {
218  for (std::size_t i = 0; i < count; ++i)
219  w.write(_f->evaluate(i,*_entity,pos));
220  }
221 
222  private:
223 
224  VTKFunctionPtr _f;
225  mutable const Entity* _entity;
226 
227  };
228 
230  template<typename F>
232  : _f(Dune::Std::make_unique<FunctionWrapper<F> >(std::forward<F>(f)))
233  , _fieldInfo(fieldInfo)
234  {}
235 
237  explicit VTKLocalFunction (const VTKFunctionPtr& vtkFunctionPtr)
238  : _f(Dune::Std::make_unique<VTKFunctionWrapper>(vtkFunctionPtr))
239  , _fieldInfo(
240  vtkFunctionPtr->name(),
241  vtkFunctionPtr->ncomps() > 1 ? VTK::FieldInfo::Type::vector : VTK::FieldInfo::Type::scalar,
242  vtkFunctionPtr->ncomps()
243  )
244  {}
245 
247  std::string name() const
248  {
249  return fieldInfo().name();
250  }
251 
253  const VTK::FieldInfo& fieldInfo() const
254  {
255  return _fieldInfo;
256  }
257 
259  void bind(const Entity& e) const
260  {
261  _f->bind(e);
262  }
263 
265  void unbind() const
266  {
267  _f->unbind();
268  }
269 
271  void write(const Coordinate& pos, Writer& w) const
272  {
273  _f->write(pos,w,fieldInfo().size());
274  }
275 
276  std::shared_ptr<FunctionWrapperBase> _f;
278 
279  };
280 
281  typedef typename std::list<VTKLocalFunction>::const_iterator FunctionIterator;
282 
284 
289  class CellIterator : public GridCellIterator
290  {
291  public:
293  CellIterator(const GridCellIterator & x) : GridCellIterator(x) {}
296  const FieldVector<DT,n> position() const
297  {
298  return ReferenceElements<DT,n>::general((*this)->type()).position(0,0);
299  }
300  };
301 
303  {
304  return gridView_.template begin< 0, VTK_Partition >();
305  }
306 
308  {
309  return gridView_.template end< 0, VTK_Partition >();
310  }
311 
313 
328  public ForwardIteratorFacade<VertexIterator, const Entity, EntityReference, int>
329  {
330  GridCellIterator git;
331  GridCellIterator gend;
332  VTK::DataMode datamode;
333  // Index of the currently visited corner within the current element.
334  // NOTE: this is in Dune-numbering, in contrast to CornerIterator.
335  int cornerIndexDune;
336  const VertexMapper & vertexmapper;
337  std::vector<bool> visited;
338  // in conforming mode, for each vertex id (as obtained by vertexmapper)
339  // hold its number in the iteration order (VertexIterator)
340  int offset;
341 
342  // hide operator ->
343  void operator->();
344  protected:
346  {
347  if( git == gend )
348  return;
349  ++cornerIndexDune;
350  const int numCorners = git->subEntities(n);
351  if( cornerIndexDune == numCorners )
352  {
353  offset += numCorners;
354  cornerIndexDune = 0;
355 
356  ++git;
357  while( (git != gend) && skipEntity( git->partitionType() ) )
358  ++git;
359  }
360  }
361  public:
362  VertexIterator(const GridCellIterator & x,
363  const GridCellIterator & end,
364  const VTK::DataMode & dm,
365  const VertexMapper & vm) :
366  git(x), gend(end), datamode(dm), cornerIndexDune(0),
367  vertexmapper(vm), visited(vm.size(), false),
368  offset(0)
369  {
370  if (datamode == VTK::conforming && git != gend)
371  visited[vertexmapper.subIndex(*git,cornerIndexDune,n)] = true;
372  }
373  void increment ()
374  {
375  switch (datamode)
376  {
377  case VTK::conforming :
378  while(visited[vertexmapper.subIndex(*git,cornerIndexDune,n)])
379  {
380  basicIncrement();
381  if (git == gend) return;
382  }
383  visited[vertexmapper.subIndex(*git,cornerIndexDune,n)] = true;
384  break;
385  case VTK::nonconforming :
386  basicIncrement();
387  break;
388  }
389  }
390  bool equals (const VertexIterator & cit) const
391  {
392  return git == cit.git
393  && cornerIndexDune == cit.cornerIndexDune
394  && datamode == cit.datamode;
395  }
396  EntityReference dereference() const
397  {
398  return *git;
399  }
401  int localindex () const
402  {
403  return cornerIndexDune;
404  }
406  const FieldVector<DT,n> & position () const
407  {
408  return ReferenceElements<DT,n>::general(git->type())
409  .position(cornerIndexDune,n);
410  }
411  };
412 
414  {
415  return VertexIterator( gridView_.template begin< 0, VTK_Partition >(),
416  gridView_.template end< 0, VTK_Partition >(),
417  datamode, *vertexmapper );
418  }
419 
421  {
422  return VertexIterator( gridView_.template end< 0, VTK_Partition >(),
423  gridView_.template end< 0, VTK_Partition >(),
424  datamode, *vertexmapper );
425  }
426 
428 
443  public ForwardIteratorFacade<CornerIterator, const Entity, EntityReference, int>
444  {
445  GridCellIterator git;
446  GridCellIterator gend;
447  VTK::DataMode datamode;
448  // Index of the currently visited corner within the current element.
449  // NOTE: this is in VTK-numbering, in contrast to VertexIterator.
450  int cornerIndexVTK;
451  const VertexMapper & vertexmapper;
452  // in conforming mode, for each vertex id (as obtained by vertexmapper)
453  // hold its number in the iteration order of VertexIterator (*not*
454  // CornerIterator)
455  const std::vector<int> & number;
456  // holds the number of corners of all the elements we have seen so far,
457  // excluding the current element
458  int offset;
459 
460  // hide operator ->
461  void operator->();
462  public:
463  CornerIterator(const GridCellIterator & x,
464  const GridCellIterator & end,
465  const VTK::DataMode & dm,
466  const VertexMapper & vm,
467  const std::vector<int> & num) :
468  git(x), gend(end), datamode(dm), cornerIndexVTK(0),
469  vertexmapper(vm),
470  number(num), offset(0) {}
471  void increment ()
472  {
473  if( git == gend )
474  return;
475  ++cornerIndexVTK;
476  const int numCorners = git->subEntities(n);
477  if( cornerIndexVTK == numCorners )
478  {
479  offset += numCorners;
480  cornerIndexVTK = 0;
481 
482  ++git;
483  while( (git != gend) && skipEntity( git->partitionType() ) )
484  ++git;
485  }
486  }
487  bool equals (const CornerIterator & cit) const
488  {
489  return git == cit.git
490  && cornerIndexVTK == cit.cornerIndexVTK
491  && datamode == cit.datamode;
492  }
493  EntityReference dereference() const
494  {
495  return *git;
496  }
498 
502  int id () const
503  {
504  switch (datamode)
505  {
506  case VTK::conforming :
507  return
508  number[vertexmapper.subIndex(*git,VTK::renumber(*git,cornerIndexVTK),
509  n)];
510  case VTK::nonconforming :
511  return offset + VTK::renumber(*git,cornerIndexVTK);
512  default :
513  DUNE_THROW(IOError,"VTKWriter: unsupported DataMode" << datamode);
514  }
515  }
516  };
517 
519  {
520  return CornerIterator( gridView_.template begin< 0, VTK_Partition >(),
521  gridView_.template end< 0, VTK_Partition >(),
522  datamode, *vertexmapper, number );
523  }
524 
526  {
527  return CornerIterator( gridView_.template end< 0, VTK_Partition >(),
528  gridView_.template end< 0, VTK_Partition >(),
529  datamode, *vertexmapper, number );
530  }
531 
532  public:
540  explicit VTKWriter ( const GridView &gridView,
542  : gridView_( gridView ),
543  datamode( dm )
544  { }
545 
550  void addCellData (const VTKFunctionPtr & p)
551  {
552  celldata.push_back(VTKLocalFunction(p));
553  }
554 
555  template<typename F>
556  void addCellData(F&& f, VTK::FieldInfo vtkFieldInfo)
557  {
558  celldata.push_back(VTKLocalFunction(std::forward<F>(f),vtkFieldInfo));
559  }
560 
566  void addCellData (VTKFunction* p) DUNE_DEPRECATED_MSG("Don't pass raw pointers, use the version with shared_ptr")
567  {
569  }
570 
586  template<class V>
587  void addCellData (const V& v, const std::string &name, int ncomps = 1)
588  {
589  typedef P0VTKFunction<GridView, V> Function;
590  for (int c=0; c<ncomps; ++c) {
591  std::stringstream compName;
592  compName << name;
593  if (ncomps>1)
594  compName << "[" << c << "]";
595  VTKFunction* p = new Function(gridView_, v, compName.str(), ncomps, c);
597  }
598  }
599 
605  void addVertexData (VTKFunction* p) DUNE_DEPRECATED_MSG("Don't pass raw pointers, use the version with shared_ptr")
606  {
608  }
609 
614  void addVertexData (const VTKFunctionPtr & p)
615  {
616  vertexdata.push_back(VTKLocalFunction(p));
617  }
618 
619  template<typename F>
620  void addVertexData(F&& f, VTK::FieldInfo vtkFieldInfo)
621  {
622  vertexdata.push_back(VTKLocalFunction(std::forward<F>(f),vtkFieldInfo));
623  }
624 
625 
641  template<class V>
642  void addVertexData (const V& v, const std::string &name, int ncomps=1)
643  {
644  typedef P1VTKFunction<GridView, V> Function;
645  for (int c=0; c<ncomps; ++c) {
646  std::stringstream compName;
647  compName << name;
648  if (ncomps>1)
649  compName << "[" << c << "]";
650  VTKFunction* p = new Function(gridView_, v, compName.str(), ncomps, c);
652  }
653  }
654 
656  void clear ()
657  {
658  celldata.clear();
659  vertexdata.clear();
660  }
661 
663  virtual ~VTKWriter ()
664  {
665  this->clear();
666  }
667 
679  std::string write ( const std::string &name,
680  VTK::OutputType type = VTK::ascii )
681  {
682  return write( name, type, gridView_.comm().rank(), gridView_.comm().size() );
683  }
684 
711  std::string pwrite ( const std::string & name, const std::string & path, const std::string & extendpath,
712  VTK::OutputType type = VTK::ascii )
713  {
714  return pwrite( name, path, extendpath, type, gridView_.comm().rank(), gridView_.comm().size() );
715  }
716 
717  protected:
719 
730  std::string getParallelPieceName(const std::string& name,
731  const std::string& path,
732  int commRank, int commSize) const
733  {
734  std::ostringstream s;
735  if(path.size() > 0) {
736  s << path;
737  if(path[path.size()-1] != '/')
738  s << '/';
739  }
740  s << 's' << std::setw(4) << std::setfill('0') << commSize << '-';
741  s << 'p' << std::setw(4) << std::setfill('0') << commRank << '-';
742  s << name;
743  if(GridView::dimension > 1)
744  s << ".vtu";
745  else
746  s << ".vtp";
747  return s.str();
748  }
749 
751 
761  std::string getParallelHeaderName(const std::string& name,
762  const std::string& path,
763  int commSize) const
764  {
765  std::ostringstream s;
766  if(path.size() > 0) {
767  s << path;
768  if(path[path.size()-1] != '/')
769  s << '/';
770  }
771  s << 's' << std::setw(4) << std::setfill('0') << commSize << '-';
772  s << name;
773  if(GridView::dimension > 1)
774  s << ".pvtu";
775  else
776  s << ".pvtp";
777  return s.str();
778  }
779 
781 
793  std::string getSerialPieceName(const std::string& name,
794  const std::string& path) const
795  {
796  static const std::string extension =
797  GridView::dimension == 1 ? ".vtp" : ".vtu";
798 
799  return concatPaths(path, name+extension);
800  }
801 
817  std::string write ( const std::string &name,
818  VTK::OutputType type,
819  const int commRank,
820  const int commSize )
821  {
822  // in the parallel case, just use pwrite, it has all the necessary
823  // stuff, so we don't need to reimplement it here.
824  if(commSize > 1)
825  return pwrite(name, "", "", type, commRank, commSize);
826 
827  // make data mode visible to private functions
828  outputtype = type;
829 
830  // generate filename for process data
831  std::string pieceName = getSerialPieceName(name, "");
832 
833  // write process data
834  std::ofstream file;
835  file.exceptions(std::ios_base::badbit | std::ios_base::failbit |
836  std::ios_base::eofbit);
837  // check if file can be opened
838  try {
839  file.open( pieceName.c_str(), std::ios::binary );
840  }
841  catch(...) {
842  std::cerr << "Filename: " << pieceName << " could not be opened" << std::endl;
843  throw;
844  }
845  if (! file.is_open())
846  DUNE_THROW(IOError, "Could not write to piece file " << pieceName);
847  writeDataFile( file );
848  file.close();
849 
850  return pieceName;
851  }
852 
854 
877  std::string pwrite(const std::string& name, const std::string& path,
878  const std::string& extendpath,
879  VTK::OutputType ot, const int commRank,
880  const int commSize )
881  {
882  // make data mode visible to private functions
883  outputtype=ot;
884 
885  // do some magic because paraview can only cope with relative pathes to piece files
886  std::ofstream file;
887  file.exceptions(std::ios_base::badbit | std::ios_base::failbit |
888  std::ios_base::eofbit);
889  std::string piecepath = concatPaths(path, extendpath);
890  std::string relpiecepath = relativePath(path, piecepath);
891 
892  // write this processes .vtu/.vtp piece file
893  std::string fullname = getParallelPieceName(name, piecepath, commRank,
894  commSize);
895  // check if file can be opened
896  try {
897  file.open(fullname.c_str(),std::ios::binary);
898  }
899  catch(...) {
900  std::cerr << "Filename: " << fullname << " could not be opened" << std::endl;
901  throw;
902  }
903  if (! file.is_open())
904  DUNE_THROW(IOError, "Could not write to piecefile file " << fullname);
905  writeDataFile(file);
906  file.close();
907  gridView_.comm().barrier();
908 
909  // if we are rank 0, write .pvtu/.pvtp parallel header
910  fullname = getParallelHeaderName(name, path, commSize);
911  if( commRank ==0 )
912  {
913  file.open(fullname.c_str());
914  if (! file.is_open())
915  DUNE_THROW(IOError, "Could not write to parallel file " << fullname);
916  writeParallelHeader(file,name,relpiecepath, commSize );
917  file.close();
918  }
919  gridView_.comm().barrier();
920  return fullname;
921  }
922 
923  private:
925 
942  void writeParallelHeader(std::ostream& s, const std::string& piecename,
943  const std::string& piecepath, const int commSize)
944  {
945  VTK::FileType fileType =
947 
948  VTK::PVTUWriter writer(s, fileType);
949 
950  writer.beginMain();
951 
952  // PPointData
953  {
954  std::string scalars, vectors;
955  std::tie(scalars,vectors) = getDataNames(vertexdata);
956  writer.beginPointData(scalars, vectors);
957  }
958  for (auto it = vertexdata.begin(),
959  end = vertexdata.end();
960  it != end;
961  ++it)
962  {
963  unsigned writecomps = it->fieldInfo().size();
964  if(writecomps == 2) writecomps = 3;
965  writer.addArray<float>(it->name(), writecomps);
966  }
967  writer.endPointData();
968 
969  // PCellData
970  {
971  std::string scalars, vectors;
972  std::tie(scalars,vectors) = getDataNames(celldata);
973  writer.beginCellData(scalars, vectors);
974  }
975  for (auto it = celldata.begin(),
976  end = celldata.end();
977  it != end;
978  ++it)
979  {
980  unsigned writecomps = it->fieldInfo().size();
981  if(writecomps == 2) writecomps = 3;
982  writer.addArray<float>(it->name(), writecomps);
983  }
984  writer.endCellData();
985 
986  // PPoints
987  writer.beginPoints();
988  writer.addArray<float>("Coordinates", 3);
989  writer.endPoints();
990 
991  // Pieces
992  for( int i = 0; i < commSize; ++i )
993  {
994  const std::string& fullname = getParallelPieceName(piecename,
995  piecepath, i,
996  commSize);
997  writer.addPiece(fullname);
998  }
999 
1000  writer.endMain();
1001  }
1002 
1004  void writeDataFile (std::ostream& s)
1005  {
1006  VTK::FileType fileType =
1007  (n == 1) ? VTK::polyData : VTK::unstructuredGrid;
1008 
1009  VTK::VTUWriter writer(s, outputtype, fileType);
1010 
1011  // Grid characteristics
1012  vertexmapper = new VertexMapper( gridView_ );
1013  if (datamode == VTK::conforming)
1014  {
1015  number.resize(vertexmapper->size());
1016  for (std::vector<int>::size_type i=0; i<number.size(); i++) number[i] = -1;
1017  }
1019 
1020  writer.beginMain(ncells, nvertices);
1021  writeAllData(writer);
1022  writer.endMain();
1023 
1024  // write appended binary data section
1025  if(writer.beginAppended())
1026  writeAllData(writer);
1027  writer.endAppended();
1028 
1029  delete vertexmapper; number.clear();
1030  }
1031 
1032  void writeAllData(VTK::VTUWriter& writer) {
1033  // PointData
1034  writeVertexData(writer);
1035 
1036  // CellData
1037  writeCellData(writer);
1038 
1039  // Points
1040  writeGridPoints(writer);
1041 
1042  // Cells
1043  writeGridCells(writer);
1044  }
1045 
1046  protected:
1047  std::string getFormatString() const
1048  {
1049  if (outputtype==VTK::ascii)
1050  return "ascii";
1051  if (outputtype==VTK::base64)
1052  return "binary";
1054  return "appended";
1056  return "appended";
1057  DUNE_THROW(IOError, "VTKWriter: unsupported OutputType" << outputtype);
1058  }
1059 
1060  std::string getTypeString() const
1061  {
1062  if (n==1)
1063  return "PolyData";
1064  else
1065  return "UnstructuredGrid";
1066  }
1067 
1069  virtual void countEntities(int &nvertices, int &ncells, int &ncorners)
1070  {
1071  nvertices = 0;
1072  ncells = 0;
1073  ncorners = 0;
1074  for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
1075  {
1076  ncells++;
1077  // because of the use of vertexmapper->map(), this iteration must be
1078  // in the order of Dune's numbering.
1079  const int subEntities = it->subEntities(n);
1080  for (int i=0; i<subEntities; ++i)
1081  {
1082  ncorners++;
1083  if (datamode == VTK::conforming)
1084  {
1085  int alpha = vertexmapper->subIndex(*it,i,n);
1086  if (number[alpha]<0)
1087  number[alpha] = nvertices++;
1088  }
1089  else
1090  {
1091  nvertices++;
1092  }
1093  }
1094  }
1095  }
1096 
1097  template<typename T>
1098  std::tuple<std::string,std::string> getDataNames(const T& data) const
1099  {
1100  std::string scalars = "";
1101  for (auto it = data.begin(),
1102  end = data.end();
1103  it != end;
1104  ++it)
1105  if (it->fieldInfo().type() == VTK::FieldInfo::Type::scalar)
1106  {
1107  scalars = it->name();
1108  break;
1109  }
1110 
1111  std::string vectors = "";
1112  for (auto it = data.begin(),
1113  end = data.end();
1114  it != end;
1115  ++it)
1116  if (it->fieldInfo().type() == VTK::FieldInfo::Type::vector)
1117  {
1118  vectors = it->name();
1119  break;
1120  }
1121  return std::make_tuple(scalars,vectors);
1122  }
1123 
1124  template<typename Data, typename Iterator>
1125  void writeData(VTK::VTUWriter& writer, const Data& data, const Iterator begin, const Iterator end, int nentries)
1126  {
1127  for (auto it = data.begin(),
1128  iend = data.end();
1129  it != iend;
1130  ++it)
1131  {
1132  const auto& f = *it;
1133  VTK::FieldInfo fieldInfo = f.fieldInfo();
1134  std::size_t writecomps = fieldInfo.size();
1135  switch (fieldInfo.type())
1136  {
1138  break;
1140  // vtk file format: a vector data always should have 3 comps (with
1141  // 3rd comp = 0 in 2D case)
1142  if (writecomps > 3)
1143  DUNE_THROW(IOError,"Cannot write VTK vectors with more than 3 components (components was " << writecomps << ")");
1144  writecomps = 3;
1145  break;
1147  DUNE_THROW(NotImplemented,"VTK output for tensors not implemented yet");
1148  }
1149  shared_ptr<VTK::DataArrayWriter<float> > p
1150  (writer.makeArrayWriter<float>(f.name(), writecomps, nentries));
1151  if(!p->writeIsNoop())
1152  for (Iterator eit = begin; eit!=end; ++eit)
1153  {
1154  const Entity & e = *eit;
1155  f.bind(e);
1156  f.write(eit.position(),*p);
1157  f.unbind();
1158  // vtk file format: a vector data always should have 3 comps
1159  // (with 3rd comp = 0 in 2D case)
1160  for (std::size_t j=fieldInfo.size(); j < writecomps; ++j)
1161  p->write(0.0);
1162  }
1163  }
1164  }
1165 
1167  virtual void writeCellData(VTK::VTUWriter& writer)
1168  {
1169  if(celldata.size() == 0)
1170  return;
1171 
1172  std::string scalars, vectors;
1173  std::tie(scalars,vectors) = getDataNames(celldata);
1174 
1175  writer.beginCellData(scalars, vectors);
1177  writer.endCellData();
1178  }
1179 
1181  virtual void writeVertexData(VTK::VTUWriter& writer)
1182  {
1183  if(vertexdata.size() == 0)
1184  return;
1185 
1186  std::string scalars, vectors;
1187  std::tie(scalars,vectors) = getDataNames(vertexdata);
1188 
1189  writer.beginPointData(scalars, vectors);
1191  writer.endPointData();
1192  }
1193 
1195  virtual void writeGridPoints(VTK::VTUWriter& writer)
1196  {
1197  writer.beginPoints();
1198 
1199  shared_ptr<VTK::DataArrayWriter<float> > p
1200  (writer.makeArrayWriter<float>("Coordinates", 3, nvertices));
1201  if(!p->writeIsNoop()) {
1202  VertexIterator vEnd = vertexEnd();
1203  for (VertexIterator vit=vertexBegin(); vit!=vEnd; ++vit)
1204  {
1205  int dimw=w;
1206  for (int j=0; j<std::min(dimw,3); j++)
1207  p->write((*vit).geometry().corner(vit.localindex())[j]);
1208  for (int j=std::min(dimw,3); j<3; j++)
1209  p->write(0.0);
1210  }
1211  }
1212  // free the VTK::DataArrayWriter before touching the stream
1213  p.reset();
1214 
1215  writer.endPoints();
1216  }
1217 
1219  virtual void writeGridCells(VTK::VTUWriter& writer)
1220  {
1221  writer.beginCells();
1222 
1223  // connectivity
1224  {
1225  shared_ptr<VTK::DataArrayWriter<int> > p1
1226  (writer.makeArrayWriter<int>("connectivity", 1, ncorners));
1227  if(!p1->writeIsNoop())
1228  for (CornerIterator it=cornerBegin(); it!=cornerEnd(); ++it)
1229  p1->write(it.id());
1230  }
1231 
1232  // offsets
1233  {
1234  shared_ptr<VTK::DataArrayWriter<int> > p2
1235  (writer.makeArrayWriter<int>("offsets", 1, ncells));
1236  if(!p2->writeIsNoop()) {
1237  int offset = 0;
1238  for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
1239  {
1240  offset += it->subEntities(n);
1241  p2->write(offset);
1242  }
1243  }
1244  }
1245 
1246  // types
1247  if (n>1)
1248  {
1249  shared_ptr<VTK::DataArrayWriter<unsigned char> > p3
1250  (writer.makeArrayWriter<unsigned char>("types", 1, ncells));
1251  if(!p3->writeIsNoop())
1252  for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
1253  {
1254  int vtktype = VTK::geometryType(it->type());
1255  p3->write(vtktype);
1256  }
1257  }
1258 
1259  writer.endCells();
1260  }
1261 
1262  protected:
1263  // the list of registered functions
1264  std::list<VTKLocalFunction> celldata;
1265  std::list<VTKLocalFunction> vertexdata;
1266 
1267  // the grid
1269 
1270  // temporary grid information
1271  int ncells;
1274  private:
1275  VertexMapper* vertexmapper;
1276  // in conforming mode, for each vertex id (as obtained by vertexmapper)
1277  // hold its number in the iteration order (VertexIterator)
1278  std::vector<int> number;
1279  VTK::DataMode datamode;
1280  protected:
1282  };
1283 
1284 }
1285 
1286 #endif
virtual void bind(const Entity &e) const
Bind data set to grid entity - must be called before evaluating (i.e. calling write()) ...
Definition: vtkwriter.hh:206
EntityReference dereference() const
Definition: vtkwriter.hh:396
void increment()
Definition: vtkwriter.hh:471
for .vtu files (UnstructuredGrid)
Definition: common.hh:294
Grid::ctype ctype
type used for coordinates in grid
Definition: common/gridview.hh:127
std::string name() const
The name of the data field.
Definition: common.hh:330
A base class for grid functions with any return type and dimension.
Definition: function.hh:38
CornerIterator(const GridCellIterator &x, const GridCellIterator &end, const VTK::DataMode &dm, const VertexMapper &vm, const std::vector< int > &num)
Definition: vtkwriter.hh:463
void addVertexData(VTKFunction *p)
Add a grid function that lives on the vertices of the grid to the visualization.
Definition: vtkwriter.hh:605
for .vtp files (PolyData)
Definition: common.hh:292
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:40
Base class for polymorphic container of underlying data set.
Definition: vtkwriter.hh:130
Type type() const
The type of the data field.
Definition: common.hh:336
VertexIterator vertexEnd() const
Definition: vtkwriter.hh:420
Dump a .vtu/.vtp files contents to a stream.
Definition: pvtuwriter.hh:60
void beginCells()
start section for the grid cells/PolyData lines
Definition: vtuwriter.hh:272
virtual void bind(const Entity &e) const =0
Bind data set to grid entity - must be called before evaluating (i.e. calling write()) ...
Base class to write pvd-files which contains a list of all collected vtk-files.
Definition: vtksequencewriterbase.hh:32
void addVertexData(const V &v, const std::string &name, int ncomps=1)
Add a grid function (represented by container) that lives on the vertices of the grid to the visualiz...
Definition: vtkwriter.hh:642
CornerIterator cornerEnd() const
Definition: vtkwriter.hh:525
const CollectiveCommunication & comm() const
obtain collective communication object
Definition: common/gridview.hh:249
Traits::Grid Grid
type of the grid
Definition: common/gridview.hh:77
virtual ~FunctionWrapperBase()
Definition: vtkwriter.hh:145
VTKFunctionWrapper(const VTKFunctionPtr &f)
Definition: vtkwriter.hh:201
FileType
which type of VTK file to write
Definition: common.hh:290
virtual void unbind() const =0
Unbind data set from current grid entity - mostly here for performance and symmetry reasons...
Output conforming data.
Definition: common.hh:70
VTKLocalFunction(const VTKFunctionPtr &vtkFunctionPtr)
Construct a VTKLocalFunction for a legacy VTKFunction.
Definition: vtkwriter.hh:237
virtual void writeGridPoints(VTK::VTUWriter &writer)
write the positions of vertices
Definition: vtkwriter.hh:1195
void addCellData(VTKFunction *p)
Add a grid function that lives on the cells of the grid to the visualization.
Definition: vtkwriter.hh:566
VertexIterator(const GridCellIterator &x, const GridCellIterator &end, const VTK::DataMode &dm, const VertexMapper &vm)
Definition: vtkwriter.hh:362
The dimension of the grid.
Definition: common/gridview.hh:130
Traits::IndexSet IndexSet
type of the index set
Definition: common/gridview.hh:80
virtual void writeVertexData(VTK::VTUWriter &writer)
write vertex data
Definition: vtkwriter.hh:1181
Include standard header files.
Definition: agrid.hh:59
virtual void write(const Coordinate &pos, Writer &w, std::size_t count) const =0
Evaluate data set at local position pos inside the current entity and write result to w...
std::string pwrite(const std::string &name, const std::string &path, const std::string &extendpath, VTK::OutputType type=VTK::ascii)
write output (interface might change later)
Definition: vtkwriter.hh:711
CornerIterator cornerBegin() const
Definition: vtkwriter.hh:518
VTK::FieldInfo _fieldInfo
Definition: vtkwriter.hh:277
std::string name() const
Returns the name of the data set.
Definition: vtkwriter.hh:247
Iterate over the elements' corners.
Definition: vtkwriter.hh:442
std::list< VTKLocalFunction >::const_iterator FunctionIterator
Definition: vtkwriter.hh:281
std::string getTypeString() const
Definition: vtkwriter.hh:1060
all interior entities
Definition: gridenums.hh:29
PartitionIteratorType
Parameter to be used for the parallel level- and leaf iterators.
Definition: gridenums.hh:134
base class for data array writers
Definition: dataarraywriter.hh:53
DataArrayWriter< T > * makeArrayWriter(const std::string &name, unsigned ncomps, unsigned nitems)
aquire a DataArrayWriter
Definition: vtuwriter.hh:379
void increment()
Definition: vtkwriter.hh:373
std::string write(const std::string &name, VTK::OutputType type, const int commRank, const int commSize)
write output (interface might change later)
Definition: vtkwriter.hh:817
Functions for VTK output.
const VTK::FieldInfo & fieldInfo() const
Returns the VTK::FieldInfo for the data set.
Definition: vtkwriter.hh:253
Ouput is to the file is appended raw binary.
Definition: common.hh:46
int size() const
Return total number of entities in the entity set managed by the mapper.
Definition: mcmgmapper.hh:205
VTK::DataArrayWriter< float > Writer
Definition: vtkwriter.hh:127
Mapper for multiple codim and multiple geometry types.
Take a vector and interpret it as cell data for the VTKWriter.
Definition: function.hh:88
Iterator over the grids elements.
Definition: vtkwriter.hh:289
Ouput is to the file is appended base64 binary.
Definition: common.hh:48
VTKWriter(const GridView &gridView, VTK::DataMode dm=VTK::conforming)
Construct a VTKWriter working on a specific GridView.
Definition: vtkwriter.hh:540
CellIterator cellBegin() const
Definition: vtkwriter.hh:302
virtual void writeGridCells(VTK::VTUWriter &writer)
write the connectivity array
Definition: vtkwriter.hh:1219
void basicIncrement()
Definition: vtkwriter.hh:345
void writeData(VTK::VTUWriter &writer, const Data &data, const Iterator begin, const Iterator end, int nentries)
Definition: vtkwriter.hh:1125
Index subIndex(const typename GV::template Codim< 0 >::Entity &e, int i, unsigned int codim) const
Map subentity of codim 0 entity to array index.
Definition: mcmgmapper.hh:190
vector-valued field (always 3D, will be padded if necessary)
Type erasure implementation for functions conforming to the dune-functions LocalFunction interface...
Definition: vtkwriter.hh:152
Common stuff for the VTKWriter.
DataMode
Whether to produce conforming or non-conforming output.
Definition: common.hh:64
GridView gridView_
Definition: vtkwriter.hh:1268
std::string getParallelHeaderName(const std::string &name, const std::string &path, int commSize) const
return name of a parallel header file
Definition: vtkwriter.hh:761
void clear()
clear list of registered functions
Definition: vtkwriter.hh:656
virtual ~VTKWriter()
destructor
Definition: vtkwriter.hh:663
virtual void write(T data)=0
write one data element
int localindex() const
index of vertex within the entity, in Dune-numbering
Definition: vtkwriter.hh:401
Data array writers for the VTKWriter.
void addCellData(const V &v, const std::string &name, int ncomps=1)
Add a grid function (represented by container) that lives on the cells of the grid to the visualizati...
Definition: vtkwriter.hh:587
all entities
Definition: gridenums.hh:139
VTK::OutputType outputtype
Definition: vtkwriter.hh:1281
std::shared_ptr< FunctionWrapperBase > _f
Definition: vtkwriter.hh:276
virtual void write(const Coordinate &pos, Writer &w, std::size_t count) const
Evaluate data set at local position pos inside the current entity and write result to w...
Definition: vtkwriter.hh:216
EntityReference dereference() const
Definition: vtkwriter.hh:493
void addCellData(const VTKFunctionPtr &p)
Add a grid function that lives on the cells of the grid to the visualization.
Definition: vtkwriter.hh:550
Output to the file is inline base64 binary.
Definition: common.hh:44
void addCellData(F &&f, VTK::FieldInfo vtkFieldInfo)
Definition: vtkwriter.hh:556
std::list< VTKLocalFunction > celldata
Definition: vtkwriter.hh:1264
GeometryType geometryType(const Dune::GeometryType &t)
mapping from GeometryType to VTKGeometryType
Definition: common.hh:195
std::list< VTKLocalFunction > vertexdata
Definition: vtkwriter.hh:1265
CellIterator cellEnd() const
Definition: vtkwriter.hh:307
virtual void unbind() const
Unbind data set from current grid entity - mostly here for performance and symmetry reasons...
Definition: vtkwriter.hh:166
std::string write(const std::string &name, VTK::OutputType type=VTK::ascii)
write output (interface might change later)
Definition: vtkwriter.hh:679
STL namespace.
bool equals(const CornerIterator &cit) const
Definition: vtkwriter.hh:487
std::string getFormatString() const
Definition: vtkwriter.hh:1047
VertexIterator vertexBegin() const
Definition: vtkwriter.hh:413
Grid view abstract base class.
Definition: common/gridview.hh:58
Type erasure wrapper for VTK data sets.
Definition: vtkwriter.hh:122
void endPointData()
finish PointData section
Definition: vtuwriter.hh:180
Output to the file is in ascii.
Definition: common.hh:42
std::string getParallelPieceName(const std::string &name, const std::string &path, int commRank, int commSize) const
return name of a parallel piece file
Definition: vtkwriter.hh:730
Type erasure implementation for legacy VTKFunctions.
Definition: vtkwriter.hh:198
int min(const DofVectorPointer< int > &dofVector)
Definition: dofvector.hh:346
shared_ptr< const VTKFunction > VTKFunctionPtr
Definition: vtkwriter.hh:114
int id() const
Process-local consecutive zero-starting vertex id.
Definition: vtkwriter.hh:502
bool equals(const VertexIterator &cit) const
Definition: vtkwriter.hh:390
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
const FieldVector< DT, n > & position() const
position of vertex inside the entity
Definition: vtkwriter.hh:406
Dune::VTKFunction< GridView > VTKFunction
Definition: vtkwriter.hh:113
tensor field (always 3x3)
std::tuple< std::string, std::string > getDataNames(const T &data) const
Definition: vtkwriter.hh:1098
interior and border entities
Definition: gridenums.hh:136
The dimension of the world the grid lives in.
Definition: common/gridview.hh:134
std::string getSerialPieceName(const std::string &name, const std::string &path) const
return name of a serial piece file
Definition: vtkwriter.hh:793
PartitionType
Attributes used in the generic overlap model.
Definition: gridenums.hh:28
virtual void unbind() const
Unbind data set from current grid entity - mostly here for performance and symmetry reasons...
Definition: vtkwriter.hh:211
void beginPointData(const std::string &scalars="", const std::string &vectors="")
start PointData section
Definition: vtuwriter.hh:165
virtual void bind(const Entity &e) const
Bind data set to grid entity - must be called before evaluating (i.e. calling write()) ...
Definition: vtkwriter.hh:161
virtual void writeCellData(VTK::VTUWriter &writer)
write cell data
Definition: vtkwriter.hh:1167
int ncorners
Definition: vtkwriter.hh:1273
void beginCellData(const std::string &scalars="", const std::string &vectors="")
start CellData section
Definition: vtuwriter.hh:203
std::size_t size() const
The number of components in the data field.
Definition: common.hh:342
void write(const Coordinate &pos, Writer &w) const
Write the value of the data set at local coordinate pos to the writer w.
Definition: vtkwriter.hh:271
void unbind() const
Unbind the data set from the currently bound entity.
Definition: vtkwriter.hh:265
Descriptor struct for VTK fields.
Definition: common.hh:306
void addVertexData(F &&f, VTK::FieldInfo vtkFieldInfo)
Definition: vtkwriter.hh:620
virtual void write(const Coordinate &pos, Writer &w, std::size_t count) const
Evaluate data set at local position pos inside the current entity and write result to w...
Definition: vtkwriter.hh:171
Dump a .vtu/.vtp files contents to a stream.
Definition: vtuwriter.hh:96
VTKLocalFunction(F &&f, VTK::FieldInfo fieldInfo)
Construct a VTKLocalFunction for a dune-functions style LocalFunction.
Definition: vtkwriter.hh:231
virtual void countEntities(int &nvertices, int &ncells, int &ncorners)
count the vertices, cells and corners
Definition: vtkwriter.hh:1069
Iterate over the grid's vertices.
Definition: vtkwriter.hh:327
int renumber(const Dune::GeometryType &t, int i)
renumber VTK <-> Dune
Definition: common.hh:224
void bind(const Entity &e) const
Bind the data set to grid entity e.
Definition: vtkwriter.hh:259
std::string pwrite(const std::string &name, const std::string &path, const std::string &extendpath, VTK::OutputType ot, const int commRank, const int commSize)
write output; interface might change later
Definition: vtkwriter.hh:877
CellIterator(const GridCellIterator &x)
construct a CellIterator from the gridview's Iterator.
Definition: vtkwriter.hh:293
Output non-conforming data.
Definition: common.hh:78
const FieldVector< DT, n > position() const
Definition: vtkwriter.hh:296
Writer for the ouput of grid functions in the vtk format.Writes arbitrary grid functions (living on c...
Definition: vtkwriter.hh:63
int nvertices
Definition: vtkwriter.hh:1272
FunctionWrapper(F_ &&f)
Definition: vtkwriter.hh:157
int ncells
Definition: vtkwriter.hh:1271
void endPoints()
finish section for the point coordinates
Definition: vtuwriter.hh:247
void addVertexData(const VTKFunctionPtr &p)
Add a grid function that lives on the vertices of the grid to the visualization.
Definition: vtkwriter.hh:614
Take a vector and interpret it as point data for the VTKWriter.
Definition: function.hh:186
void endCellData()
finish CellData section
Definition: vtuwriter.hh:218