4 #ifndef DUNE_VTKWRITER_HH
5 #define DUNE_VTKWRITER_HH
17 #include <dune/common/deprecated.hh>
18 #include <dune/common/exceptions.hh>
19 #include <dune/common/indent.hh>
20 #include <dune/common/iteratorfacades.hh>
21 #include <dune/common/path.hh>
22 #include <dune/common/shared_ptr.hh>
23 #include <dune/geometry/referenceelements.hh>
60 template<
class Gr
idView >
68 typedef typename GridView::template Codim< 0 >::Entity Cell;
69 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
107 return GenericReferenceElements<DT,n>::general((*this)->type()).
position(0,0);
113 return gridView_.template begin< 0, VTK_Partition >();
118 return gridView_.template end< 0, VTK_Partition >();
137 public ForwardIteratorFacade<VertexIterator, const Entity, const Entity&, int>
139 GridCellIterator git;
140 GridCellIterator gend;
146 std::vector<bool> visited;
156 const int numCorners = git->template count< n >();
157 if( cornerIndexDune == numCorners )
159 offset += numCorners;
163 while( (git != gend) && (git->partitionType() !=
InteriorEntity) )
169 const GridCellIterator & end,
172 git(x), gend(end), datamode(dm), cornerIndexDune(0),
173 vertexmapper(vm), visited(vm.size(), false),
177 visited[vertexmapper.
map(*git,cornerIndexDune,n)] =
true;
184 while(visited[vertexmapper.
map(*git,cornerIndexDune,n)])
187 if (git == gend)
return;
189 visited[vertexmapper.
map(*git,cornerIndexDune,n)] =
true;
198 return git == cit.git
199 && cornerIndexDune == cit.cornerIndexDune
200 && datamode == cit.datamode;
209 return cornerIndexDune;
214 return GenericReferenceElements<DT,n>::general(git->type())
221 return VertexIterator( gridView_.template begin< 0, VTK_Partition >(),
222 gridView_.template end< 0, VTK_Partition >(),
223 datamode, *vertexmapper );
228 return VertexIterator( gridView_.template end< 0, VTK_Partition >(),
229 gridView_.template end< 0, VTK_Partition >(),
230 datamode, *vertexmapper );
249 public ForwardIteratorFacade<CornerIterator, const Entity, const Entity&, int>
251 GridCellIterator git;
252 GridCellIterator gend;
261 const std::vector<int> & number;
268 const GridCellIterator & end,
271 const std::vector<int> & num) :
272 git(x), gend(end), datamode(dm), cornerIndexVTK(0),
274 number(num), offset(0) {}
280 const int numCorners = git->template count< n >();
281 if( cornerIndexVTK == numCorners )
283 offset += numCorners;
287 while( (git != gend) && (git->partitionType() !=
InteriorEntity) )
293 return git == cit.git
294 && cornerIndexVTK == cit.cornerIndexVTK
295 && datamode == cit.datamode;
317 DUNE_THROW(IOError,
"VTKWriter: unsupported DataMode" << datamode);
324 return CornerIterator( gridView_.template begin< 0, VTK_Partition >(),
325 gridView_.template end< 0, VTK_Partition >(),
326 datamode, *vertexmapper, number );
331 return CornerIterator( gridView_.template end< 0, VTK_Partition >(),
332 gridView_.template end< 0, VTK_Partition >(),
333 datamode, *vertexmapper, number );
346 : gridView_( gridView ),
385 void addCellData (
const V& v,
const std::string &name,
int ncomps = 1)
388 for (
int c=0;c<ncomps;++c) {
389 std::stringstream compName;
392 compName <<
"[" << c <<
"]";
393 VTKFunction* p =
new Function(gridView_, v, compName.str(), ncomps, c);
436 for (
int c=0;c<ncomps;++c) {
437 std::stringstream compName;
440 compName <<
"[" << c <<
"]";
441 VTKFunction* p =
new Function(gridView_, v, compName.str(), ncomps, c);
470 std::string
write (
const std::string &name,
473 return write( name, type, gridView_.
comm().rank(), gridView_.
comm().size() );
502 std::string
pwrite (
const std::string & name,
const std::string & path,
const std::string & extendpath,
505 return pwrite( name, path, extendpath, type, gridView_.
comm().rank(), gridView_.
comm().size() );
522 const std::string& path,
523 int commRank,
int commSize)
const
525 std::ostringstream s;
526 if(path.size() > 0) {
528 if(path[path.size()-1] !=
'/')
531 s <<
's' << std::setw(4) << std::setfill(
'0') << commSize <<
'-';
532 s <<
'p' << std::setw(4) << std::setfill(
'0') << commRank <<
'-';
553 const std::string& path,
556 std::ostringstream s;
557 if(path.size() > 0) {
559 if(path[path.size()-1] !=
'/')
562 s <<
's' << std::setw(4) << std::setfill(
'0') << commSize <<
'-';
585 const std::string& path)
const
587 static const std::string extension =
590 return concatPaths(path, name+extension);
608 std::string
write (
const std::string &name,
616 return pwrite(name,
"",
"", type, commRank, commSize);
626 file.open( pieceName.c_str(), std::ios::binary );
627 if (! file.is_open())
628 DUNE_THROW(IOError,
"Could not write to piece file " << pieceName);
629 writeDataFile( file );
659 std::string
pwrite(
const std::string& name,
const std::string& path,
660 const std::string& extendpath,
669 std::string piecepath = concatPaths(path, extendpath);
670 std::string relpiecepath = relativePath(path, piecepath);
675 file.open(fullname.c_str(),std::ios::binary);
676 if (! file.is_open())
677 DUNE_THROW(IOError,
"Could not write to piecefile file " << fullname);
680 gridView_.
comm().barrier();
686 file.open(fullname.c_str());
687 if (! file.is_open())
688 DUNE_THROW(IOError,
"Could not write to parallel file " << fullname);
689 writeParallelHeader(file,name,relpiecepath, commSize );
692 gridView_.
comm().barrier();
715 void writeParallelHeader(std::ostream& s,
const std::string& piecename,
716 const std::string& piecepath,
const int commSize)
730 if ((*it)->ncomps()==1)
732 scalars = (*it)->name();
738 if ((*it)->ncomps()>1)
740 vectors = (*it)->name();
743 writer.beginPointData(scalars, vectors);
748 unsigned writecomps = (*it)->ncomps();
749 if(writecomps == 2) writecomps = 3;
750 writer.addArray<
float>((*it)->name(), writecomps);
752 writer.endPointData();
759 if ((*it)->ncomps()==1)
761 scalars = (*it)->name();
767 if ((*it)->ncomps()>1)
769 vectors = (*it)->name();
772 writer.beginCellData(scalars, vectors);
775 unsigned writecomps = (*it)->ncomps();
776 if(writecomps == 2) writecomps = 3;
777 writer.addArray<
float>((*it)->name(), writecomps);
779 writer.endCellData();
782 writer.beginPoints();
783 writer.addArray<
float>(
"Coordinates", 3);
787 for(
int i = 0; i < commSize; ++i )
792 writer.addPiece(fullname);
799 void writeDataFile (std::ostream& s)
804 VTK::VTUWriter writer(s,
outputtype, fileType);
807 vertexmapper =
new VertexMapper( gridView_ );
810 number.resize(vertexmapper->
size());
811 for (std::vector<int>::size_type i=0; i<number.size(); i++) number[i] = -1;
816 writeAllData(writer);
820 if(writer.beginAppended())
821 writeAllData(writer);
822 writer.endAppended();
824 delete vertexmapper; number.clear();
827 void writeAllData(VTK::VTUWriter& writer) {
852 DUNE_THROW(IOError,
"VTKWriter: unsupported OutputType" <<
outputtype);
860 return "UnstructuredGrid";
874 for (
int i=0; i<it->template count<n>(); ++i)
879 int alpha = vertexmapper->
map(*it,i,n);
881 number[alpha] = nvertices++;
897 std::string scalars =
"";
899 if ((*it)->ncomps()==1)
901 scalars = (*it)->name();
904 std::string vectors =
"";
906 if ((*it)->ncomps()>1)
908 vectors = (*it)->name();
917 unsigned writecomps = (*it)->ncomps();
918 if(writecomps == 2) writecomps = 3;
919 shared_ptr<VTK::DataArrayWriter<float> > p
922 if(!p->writeIsNoop())
925 for (
int j=0; j<(*it)->ncomps(); j++)
926 p->write((*it)->evaluate(j,*i,i.position()));
929 for (
unsigned j=(*it)->ncomps(); j < writecomps; ++j)
942 std::string scalars =
"";
944 if ((*it)->ncomps()==1)
946 scalars = (*it)->name();
949 std::string vectors =
"";
951 if ((*it)->ncomps()>1)
953 vectors = (*it)->name();
962 unsigned writecomps = (*it)->ncomps();
963 if(writecomps == 2) writecomps = 3;
964 shared_ptr<VTK::DataArrayWriter<float> > p
967 if(!p->writeIsNoop())
970 for (
int j=0; j<(*it)->ncomps(); j++)
971 p->write((*it)->evaluate(j,*vit,vit.position()));
974 for (
unsigned j=(*it)->ncomps(); j < writecomps; ++j)
986 shared_ptr<VTK::DataArrayWriter<float> > p
988 if(!p->writeIsNoop()) {
993 for (
int j=0; j<
std::min(dimw,3); j++)
994 p->write(vit->geometry().corner(vit.localindex())[j]);
995 for (
int j=std::min(dimw,3); j<3; j++)
1012 shared_ptr<VTK::DataArrayWriter<int> > p1
1014 if(!p1->writeIsNoop())
1021 shared_ptr<VTK::DataArrayWriter<int> > p2
1023 if(!p2->writeIsNoop()) {
1027 offset += it->template count<n>();
1036 shared_ptr<VTK::DataArrayWriter<unsigned char> > p3
1038 if(!p3->writeIsNoop())
1067 std::vector<int> number;