4 #ifndef DUNE_VTKWRITER_HH
5 #define DUNE_VTKWRITER_HH
17 #include <dune/common/exceptions.hh>
18 #include <dune/common/indent.hh>
19 #include <dune/common/iteratorfacades.hh>
20 #include <dune/common/path.hh>
21 #include <dune/common/shared_ptr.hh>
22 #include <dune/geometry/referenceelements.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 )
93 default: DUNE_THROW(NotImplemented,
"Add check for this partition type");
120 return ReferenceElements<DT,n>::general((*this)->type()).
position(0,0);
126 return gridView_.template begin< 0, VTK_Partition >();
131 return gridView_.template end< 0, VTK_Partition >();
150 public ForwardIteratorFacade<VertexIterator, const Entity, const Entity&, int>
152 GridCellIterator git;
153 GridCellIterator gend;
159 std::vector<bool> visited;
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),
190 visited[vertexmapper.
map(*git,cornerIndexDune,n)] =
true;
197 while(visited[vertexmapper.
map(*git,cornerIndexDune,n)])
200 if (git == gend)
return;
202 visited[vertexmapper.
map(*git,cornerIndexDune,n)] =
true;
211 return git == cit.git
212 && cornerIndexDune == cit.cornerIndexDune
213 && datamode == cit.datamode;
222 return cornerIndexDune;
227 return ReferenceElements<DT,n>::general(git->type())
235 gridView_.template end< 0, VTK_Partition >(),
236 datamode, *vertexmapper );
242 gridView_.template end< 0, VTK_Partition >(),
243 datamode, *vertexmapper );
262 public ForwardIteratorFacade<CornerIterator, const Entity, const Entity&, int>
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;
330 DUNE_THROW(IOError,
"VTKWriter: unsupported DataMode" << datamode);
338 gridView_.template end< 0, VTK_Partition >(),
339 datamode, *vertexmapper, number );
345 gridView_.template end< 0, VTK_Partition >(),
346 datamode, *vertexmapper, number );
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 <<
"]";
449 for (
int c=0; c<ncomps; ++c) {
450 std::stringstream compName;
453 compName <<
"[" << c <<
"]";
483 std::string
write (
const std::string &name,
515 std::string
pwrite (
const std::string & name,
const std::string & path,
const std::string & extendpath,
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 =
603 return concatPaths(path, name+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())
643 DUNE_THROW(IOError,
"Could not write to piece file " << pieceName);
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())
694 DUNE_THROW(IOError,
"Could not write to piecefile file " << fullname);
703 file.open(fullname.c_str());
704 if (! file.is_open())
705 DUNE_THROW(IOError,
"Could not write to parallel file " << fullname);
706 writeParallelHeader(file,name,relpiecepath, commSize );
732 void writeParallelHeader(std::ostream& s,
const std::string& piecename,
733 const std::string& piecepath,
const int commSize)
747 if ((*it)->ncomps()==1)
749 scalars = (*it)->name();
755 if ((*it)->ncomps()>1)
757 vectors = (*it)->name();
760 writer.beginPointData(scalars, vectors);
765 unsigned writecomps = (*it)->ncomps();
766 if(writecomps == 2) writecomps = 3;
767 writer.addArray<
float>((*it)->name(), writecomps);
769 writer.endPointData();
776 if ((*it)->ncomps()==1)
778 scalars = (*it)->name();
784 if ((*it)->ncomps()>1)
786 vectors = (*it)->name();
789 writer.beginCellData(scalars, vectors);
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)
821 VTK::VTUWriter writer(s,
outputtype, fileType);
824 vertexmapper =
new VertexMapper(
gridView_ );
827 number.resize(vertexmapper->
size());
828 for (std::vector<int>::size_type i=0; i<number.size(); i++) number[i] = -1;
833 writeAllData(writer);
837 if(writer.beginAppended())
838 writeAllData(writer);
839 writer.endAppended();
841 delete vertexmapper; number.clear();
844 void writeAllData(VTK::VTUWriter& writer) {
869 DUNE_THROW(IOError,
"VTKWriter: unsupported OutputType" <<
outputtype);
877 return "UnstructuredGrid";
891 for (
int i=0; i<it->template count<n>(); ++i)
896 int alpha = vertexmapper->
map(*it,i,n);
898 number[alpha] = nvertices++;
914 std::string scalars =
"";
916 if ((*it)->ncomps()==1)
918 scalars = (*it)->name();
921 std::string vectors =
"";
923 if ((*it)->ncomps()>1)
925 vectors = (*it)->name();
934 unsigned writecomps = (*it)->ncomps();
935 if(writecomps == 2) writecomps = 3;
936 shared_ptr<VTK::DataArrayWriter<float> > p
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)
959 std::string scalars =
"";
961 if ((*it)->ncomps()==1)
963 scalars = (*it)->name();
966 std::string vectors =
"";
968 if ((*it)->ncomps()>1)
970 vectors = (*it)->name();
979 unsigned writecomps = (*it)->ncomps();
980 if(writecomps == 2) writecomps = 3;
981 shared_ptr<VTK::DataArrayWriter<float> > p
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)
1003 shared_ptr<VTK::DataArrayWriter<float> > p
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++)
1029 shared_ptr<VTK::DataArrayWriter<int> > p1
1031 if(!p1->writeIsNoop())
1038 shared_ptr<VTK::DataArrayWriter<int> > p2
1040 if(!p2->writeIsNoop()) {
1044 offset += it->template count<n>();
1053 shared_ptr<VTK::DataArrayWriter<unsigned char> > p3
1055 if(!p3->writeIsNoop())
1082 std::vector<int> number;