4#ifndef DUNE_VTKWRITER_HH
5#define DUNE_VTKWRITER_HH
22#include <dune/geometry/referenceelements.hh>
24#include <dune/grid/common/gridenums.hh>
28#include <dune/grid/io/file/vtk/pvtuwriter.hh>
29#include <dune/grid/io/file/vtk/streams.hh>
30#include <dune/grid/io/file/vtk/vtuwriter.hh>
59 template<
class Gr
idView >
67 typedef typename GridView::template Codim< 0 >::Entity Cell;
68 typedef typename GridView::template Codim< n >::Entity Vertex;
76 typedef typename GridView::template Codim< 0 >
77 ::template Partition< VTK_Partition >::Iterator
79 typedef typename GridView::template Codim< n >
80 ::template Partition< VTK_Partition >::Iterator
88 switch( VTK_Partition )
103 typedef typename std::list<VTKFunctionPtr>::const_iterator FunctionIterator;
124 CellIterator cellBegin()
const
126 return gridView_.template begin< 0, VTK_Partition >();
129 CellIterator cellEnd()
const
131 return gridView_.template end< 0, VTK_Partition >();
152 GridCellIterator git;
153 GridCellIterator gend;
159 std::vector<bool> visited;
164 void basicIncrement ()
169 const int numCorners = git->template count< n >();
170 if( cornerIndexDune == numCorners )
172 offset += numCorners;
176 while( (git != gend) && skipEntity( git->partitionType() ) )
182 const GridCellIterator & end,
185 git(x), gend(end), datamode(dm), cornerIndexDune(0),
186 vertexmapper(vm), visited(vm.
size(),
false),
189 if (datamode == VTK::conforming && git != gend)
190 visited[vertexmapper.
map(*git,cornerIndexDune,n)] =
true;
196 case VTK::conforming :
197 while(visited[vertexmapper.
map(*git,cornerIndexDune,n)])
200 if (git == gend)
return;
202 visited[vertexmapper.
map(*git,cornerIndexDune,n)] =
true;
204 case VTK::nonconforming :
211 return git == cit.git
212 && cornerIndexDune == cit.cornerIndexDune
213 && datamode == cit.datamode;
215 const Entity& dereference()
const
222 return cornerIndexDune;
228 .position(cornerIndexDune,n);
232 VertexIterator vertexBegin ()
const
234 return VertexIterator( gridView_.template begin< 0, VTK_Partition >(),
235 gridView_.template end< 0, VTK_Partition >(),
236 datamode, *vertexmapper );
239 VertexIterator vertexEnd ()
const
241 return VertexIterator( gridView_.template end< 0, VTK_Partition >(),
242 gridView_.template end< 0, VTK_Partition >(),
243 datamode, *vertexmapper );
264 GridCellIterator git;
265 GridCellIterator gend;
274 const std::vector<int> & number;
281 const GridCellIterator & end,
284 const std::vector<int> & num) :
285 git(x), gend(end), datamode(dm), cornerIndexVTK(0),
287 number(num), offset(0) {}
293 const int numCorners = git->template count< n >();
294 if( cornerIndexVTK == numCorners )
296 offset += numCorners;
300 while( (git != gend) && skipEntity( git->partitionType() ) )
306 return git == cit.git
307 && cornerIndexVTK == cit.cornerIndexVTK
308 && datamode == cit.datamode;
310 const Entity& dereference()
const
323 case VTK::conforming :
325 number[vertexmapper.
map(*git,VTK::renumber(*git,cornerIndexVTK),
327 case VTK::nonconforming :
328 return offset + VTK::renumber(*git,cornerIndexVTK);
335 CornerIterator cornerBegin ()
const
337 return CornerIterator( gridView_.template begin< 0, VTK_Partition >(),
338 gridView_.template end< 0, VTK_Partition >(),
339 datamode, *vertexmapper, number );
342 CornerIterator cornerEnd ()
const
344 return CornerIterator( gridView_.template end< 0, VTK_Partition >(),
345 gridView_.template end< 0, VTK_Partition >(),
346 datamode, *vertexmapper, number );
359 : gridView_( gridView ),
369 celldata.push_back(p);
398 void addCellData (
const V& v,
const std::string &name,
int ncomps = 1)
401 for (
int c=0; c<ncomps; ++c) {
402 std::stringstream compName;
405 compName <<
"[" << c <<
"]";
427 vertexdata.push_back(p);
449 for (
int c=0; c<ncomps; ++c) {
450 std::stringstream compName;
453 compName <<
"[" << c <<
"]";
483 std::string
write (
const std::string &name,
486 return write( name, type, gridView_.
comm().rank(), gridView_.
comm().size() );
515 std::string
pwrite (
const std::string & name,
const std::string & path,
const std::string & extendpath,
518 return pwrite( name, path, extendpath, type, gridView_.
comm().rank(), gridView_.
comm().size() );
535 const std::string& path,
536 int commRank,
int commSize)
const
538 std::ostringstream s;
539 if(path.size() > 0) {
541 if(path[path.size()-1] !=
'/')
544 s <<
's' << std::setw(4) << std::setfill(
'0') << commSize <<
'-';
545 s <<
'p' << std::setw(4) << std::setfill(
'0') << commRank <<
'-';
566 const std::string& path,
569 std::ostringstream s;
570 if(path.size() > 0) {
572 if(path[path.size()-1] !=
'/')
575 s <<
's' << std::setw(4) << std::setfill(
'0') << commSize <<
'-';
598 const std::string& path)
const
600 static const std::string extension =
621 std::string
write (
const std::string &name,
629 return pwrite(name,
"",
"", type, commRank, commSize);
639 file.exceptions(std::ios_base::badbit | std::ios_base::failbit |
640 std::ios_base::eofbit);
641 file.open( pieceName.c_str(), std::ios::binary );
642 if (! file.is_open())
644 writeDataFile( file );
674 std::string
pwrite(
const std::string& name,
const std::string& path,
675 const std::string& extendpath,
684 file.exceptions(std::ios_base::badbit | std::ios_base::failbit |
685 std::ios_base::eofbit);
686 std::string piecepath =
concatPaths(path, extendpath);
687 std::string relpiecepath =
relativePath(path, piecepath);
692 file.open(fullname.c_str(),std::ios::binary);
693 if (! file.is_open())
697 gridView_.
comm().barrier();
703 file.open(fullname.c_str());
704 if (! file.is_open())
706 writeParallelHeader(file,name,relpiecepath, commSize );
709 gridView_.
comm().barrier();
732 void writeParallelHeader(std::ostream& s,
const std::string& piecename,
733 const std::string& piecepath,
const int commSize)
736 (n == 1) ? VTK::polyData : VTK::unstructuredGrid;
745 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end();
747 if ((*it)->ncomps()==1)
749 scalars = (*it)->name();
753 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end();
755 if ((*it)->ncomps()>1)
757 vectors = (*it)->name();
760 writer.beginPointData(scalars, vectors);
762 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end();
765 unsigned writecomps = (*it)->ncomps();
766 if(writecomps == 2) writecomps = 3;
767 writer.addArray<
float>((*it)->name(), writecomps);
769 writer.endPointData();
774 for (FunctionIterator it=celldata.begin(); it!=celldata.end();
776 if ((*it)->ncomps()==1)
778 scalars = (*it)->name();
782 for (FunctionIterator it=celldata.begin(); it!=celldata.end();
784 if ((*it)->ncomps()>1)
786 vectors = (*it)->name();
789 writer.beginCellData(scalars, vectors);
791 for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it) {
792 unsigned writecomps = (*it)->ncomps();
793 if(writecomps == 2) writecomps = 3;
794 writer.addArray<
float>((*it)->name(), writecomps);
796 writer.endCellData();
799 writer.beginPoints();
800 writer.addArray<
float>(
"Coordinates", 3);
804 for(
int i = 0; i < commSize; ++i )
809 writer.addPiece(fullname);
816 void writeDataFile (std::ostream& s)
818 VTK::FileType fileType =
819 (n == 1) ? VTK::polyData : VTK::unstructuredGrid;
821 VTK::VTUWriter writer(s, outputtype, fileType);
824 vertexmapper =
new VertexMapper( gridView_ );
825 if (datamode == VTK::conforming)
827 number.resize(vertexmapper->
size());
828 for (std::vector<int>::size_type i=0; i<number.size(); i++) number[i] = -1;
832 writer.beginMain(ncells, nvertices);
833 writeAllData(writer);
837 if(writer.beginAppended())
838 writeAllData(writer);
839 writer.endAppended();
841 delete vertexmapper; number.clear();
844 void writeAllData(VTK::VTUWriter& writer) {
859 std::string getFormatString()
const
861 if (outputtype==VTK::ascii)
863 if (outputtype==VTK::base64)
865 if (outputtype==VTK::appendedraw)
867 if (outputtype==VTK::appendedbase64)
869 DUNE_THROW(IOError,
"VTKWriter: unsupported OutputType" << outputtype);
872 std::string getTypeString()
const
877 return "UnstructuredGrid";
891 for (
int i=0; i<it->template count<n>(); ++i)
894 if (datamode == VTK::conforming)
896 int alpha = vertexmapper->
map(*it,i,n);
898 number[alpha] = nvertices++;
911 if(celldata.size() == 0)
914 std::string scalars =
"";
915 for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
916 if ((*it)->ncomps()==1)
918 scalars = (*it)->name();
921 std::string vectors =
"";
922 for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
923 if ((*it)->ncomps()>1)
925 vectors = (*it)->name();
930 for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
934 unsigned writecomps = (*it)->ncomps();
935 if(writecomps == 2) writecomps = 3;
939 if(!p->writeIsNoop())
942 for (
int j=0; j<(*it)->ncomps(); j++)
943 p->write((*it)->evaluate(j,*i,i.position()));
946 for (
unsigned j=(*it)->ncomps(); j < writecomps; ++j)
956 if(vertexdata.size() == 0)
959 std::string scalars =
"";
960 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
961 if ((*it)->ncomps()==1)
963 scalars = (*it)->name();
966 std::string vectors =
"";
967 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
968 if ((*it)->ncomps()>1)
970 vectors = (*it)->name();
975 for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
979 unsigned writecomps = (*it)->ncomps();
980 if(writecomps == 2) writecomps = 3;
984 if(!p->writeIsNoop())
987 for (
int j=0; j<(*it)->ncomps(); j++)
988 p->write((*it)->evaluate(j,*vit,vit.position()));
991 for (
unsigned j=(*it)->ncomps(); j < writecomps; ++j)
1005 if(!p->writeIsNoop()) {
1010 for (
int j=0; j<std::min(dimw,3); j++)
1011 p->write(vit->geometry().corner(vit.localindex())[j]);
1012 for (
int j=std::min(dimw,3); j<3; j++)
1031 if(!p1->writeIsNoop())
1040 if(!p2->writeIsNoop()) {
1044 offset += it->template count<n>();
1055 if(!p3->writeIsNoop())
1058 int vtktype = VTK::geometryType(it->type());
1068 std::list<VTKFunctionPtr> celldata;
1069 std::list<VTKFunctionPtr> vertexdata;
1079 VertexMapper* vertexmapper;
1082 std::vector<int> number;
vector space out of a tensor product of fields.
Definition: fvector.hh:92
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:142
Base class template for function classes.
Definition: function.hh:28
Grid view abstract base class.
Definition: gridview.hh:57
Traits::Grid Grid
type of the grid
Definition: gridview.hh:67
const CollectiveCommunication & comm() const
obtain collective communication object
Definition: gridview.hh:232
Traits::IndexSet IndexSet
type of the index set
Definition: gridview.hh:70
@ dimensionworld
The dimension of the world the grid lives in.
Definition: gridview.hh:124
Grid::ctype ctype
type used for coordinates in grid
Definition: gridview.hh:117
@ dimension
The dimension of the grid.
Definition: gridview.hh:120
Default exception class for I/O errors.
Definition: exceptions.hh:257
Implementation class for a multiple codim and multiple geometry type mapper.
Definition: mcmgmapper.hh:104
int map(const EntityType &e) const
Map entity to array index.
Definition: mcmgmapper.hh:153
int size() const
Return total number of entities in the entity set managed by the mapper.
Definition: mcmgmapper.hh:181
Default exception for dummy implementations.
Definition: exceptions.hh:289
Take a vector and interpret it as cell data for the VTKWriter.
Definition: function.hh:89
Take a vector and interpret it as point data for the VTKWriter.
Definition: function.hh:192
A base class for grid functions with any return type and dimension.
Definition: function.hh:38
Iterator over the grids elements.
Definition: vtkwriter.hh:112
CellIterator(const GridCellIterator &x)
construct a CellIterator from the gridview's Iterator.
Definition: vtkwriter.hh:115
const FieldVector< DT, n > position() const
Definition: vtkwriter.hh:118
Iterate over the elements' corners.
Definition: vtkwriter.hh:263
int id() const
Process-local consecutive zero-starting vertex id.
Definition: vtkwriter.hh:319
Iterate over the grid's vertices.
Definition: vtkwriter.hh:151
const FieldVector< DT, n > & position() const
position of vertex inside the entity
Definition: vtkwriter.hh:225
int localindex() const
index of vertex within the entity, in Dune-numbering
Definition: vtkwriter.hh:220
Writer for the ouput of grid functions in the vtk format.
Definition: vtkwriter.hh:60
void addVertexData(const VTKFunctionPtr &p)
Add a grid function that lives on the vertices of the grid to the visualization.
Definition: vtkwriter.hh:425
void clear()
clear list of registered functions
Definition: vtkwriter.hh:460
std::string write(const std::string &name, VTK::OutputType type=VTK::ascii)
write output (interface might change later)
Definition: vtkwriter.hh:483
std::string getParallelHeaderName(const std::string &name, const std::string &path, int commSize) const
return name of a parallel header file
Definition: vtkwriter.hh:565
std::string getSerialPieceName(const std::string &name, const std::string &path) const
return name of a serial piece file
Definition: vtkwriter.hh:597
void addCellData(const VTKFunctionPtr &p)
Add a grid function that lives on the cells of the grid to the visualization.
Definition: vtkwriter.hh:367
virtual void writeCellData(VTK::VTUWriter &writer)
write cell data
Definition: vtkwriter.hh:909
void addVertexData(VTKFunction *p)
Add a grid function that lives on the vertices of the grid to the visualization.
Definition: vtkwriter.hh:416
VTKWriter(const GridView &gridView, VTK::DataMode dm=VTK::conforming)
Construct a VTKWriter working on a specific GridView.
Definition: vtkwriter.hh:357
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:534
virtual void countEntities(int &nvertices, int &ncells, int &ncorners)
count the vertices, cells and corners
Definition: vtkwriter.hh:881
virtual void writeGridCells(VTK::VTUWriter &writer)
write the connectivity array
Definition: vtkwriter.hh:1023
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:446
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:621
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:398
virtual void writeGridPoints(VTK::VTUWriter &writer)
write the positions of vertices
Definition: vtkwriter.hh:999
virtual void writeVertexData(VTK::VTUWriter &writer)
write vertex data
Definition: vtkwriter.hh:954
virtual ~VTKWriter()
destructor
Definition: vtkwriter.hh:467
void addCellData(VTKFunction *p)
Add a grid function that lives on the cells of the grid to the visualization.
Definition: vtkwriter.hh:377
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:674
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:515
Dump a .vtu/.vtp files contents to a stream.
Definition: pvtuwriter.hh:60
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
A reference counting smart pointer.
Definition: shared_ptr.hh:64
Common stuff for the VTKWriter.
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:40
FileType
which type of VTK file to write
Definition: common.hh:290
DataMode
Whether to produce conforming or non-conforming output.
Definition: common.hh:64
Data array writers for the VTKWriter.
A few common exception classes.
Functions for VTK output.
void reset()
Decrease the reference count by one and free the memory if the reference count has reached 0.
Definition: shared_ptr.hh:354
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
std::string relativePath(const std::string &newbase, const std::string &p)
compute a relative path between two paths
Definition: path.cc:151
std::string concatPaths(const std::string &base, const std::string &p)
concatenate two paths
Definition: path.cc:30
Utility class for handling nested indentation in output.
This file implements iterator facade classes for writing stl conformant iterators.
Mapper for multiple codim and multiple geometry types.
Dune namespace.
Definition: alignment.hh:14
PartitionIteratorType
Parameter to be used for the parallel level- and leaf iterators.
Definition: gridenums.hh:130
@ All_Partition
all entities
Definition: gridenums.hh:135
@ InteriorBorder_Partition
interior and border entities
Definition: gridenums.hh:132
PartitionType
Attributes used in the generic overlap model.
Definition: gridenums.hh:24
@ InteriorEntity
all interior entities
Definition: gridenums.hh:25
Utilities for handling filesystem paths.
This file implements the class shared_ptr (a reference counting pointer), for those systems that don'...
static const ReferenceElement< ctype, dim > & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:568