172
185
git(x), gend(end), datamode(dm), cornerIndexDune(0),
173
186
vertexmapper(vm), visited(vm.size(), false),
176
if (datamode == VTK::conforming && git != gend)
177
visited[vertexmapper.map(*git,cornerIndexDune,n)] = true;
189
if (datamode == VTK::conforming && git != gend)
190
visited[vertexmapper.map(*git,cornerIndexDune,n)] = true;
179
192
void increment ()
196
case VTK::conforming :
197
while(visited[vertexmapper.map(*git,cornerIndexDune,n)])
183
case VTK::conforming:
184
while(visited[vertexmapper.map(*git,cornerIndexDune,n)])
187
if (git == gend) return;
189
visited[vertexmapper.map(*git,cornerIndexDune,n)] = true;
191
case VTK::nonconforming:
192
199
basicIncrement();
200
if (git == gend) return;
202
visited[vertexmapper.map(*git,cornerIndexDune,n)] = true;
204
case VTK::nonconforming :
196
209
bool equals (const VertexIterator & cit) const
198
return git == cit.git
199
&& cornerIndexDune == cit.cornerIndexDune
200
&& datamode == cit.datamode;
211
return git == cit.git
212
&& cornerIndexDune == cit.cornerIndexDune
213
&& datamode == cit.datamode;
202
215
const Entity& dereference() const
206
219
//! index of vertex within the entity, in Dune-numbering
207
220
int localindex () const
209
return cornerIndexDune;
222
return cornerIndexDune;
211
224
//! position of vertex inside the entity
212
225
const FieldVector<DT,n> & position () const
214
return GenericReferenceElements<DT,n>::general(git->type())
215
.position(cornerIndexDune,n);
227
return ReferenceElements<DT,n>::general(git->type())
228
.position(cornerIndexDune,n);
219
232
VertexIterator vertexBegin () const
221
234
return VertexIterator( gridView_.template begin< 0, VTK_Partition >(),
284
297
cornerIndexVTK = 0;
287
while( (git != gend) && (git->partitionType() != InteriorEntity) )
300
while( (git != gend) && skipEntity( git->partitionType() ) )
291
304
bool equals (const CornerIterator & cit) const
293
return git == cit.git
294
&& cornerIndexVTK == cit.cornerIndexVTK
295
&& datamode == cit.datamode;
306
return git == cit.git
307
&& cornerIndexVTK == cit.cornerIndexVTK
308
&& datamode == cit.datamode;
297
310
const Entity& dereference() const
301
314
//! Process-local consecutive zero-starting vertex id
303
316
* This method returns the number of this corners associated vertex, in
304
317
* the numbering given by the iteration order of VertexIterator.
310
case VTK::conforming:
312
number[vertexmapper.map(*git,VTK::renumber(*git,cornerIndexVTK),
314
case VTK::nonconforming:
315
return offset + VTK::renumber(*git,cornerIndexVTK);
317
DUNE_THROW(IOError,"VTKWriter: unsupported DataMode" << datamode);
323
case VTK::conforming :
325
number[vertexmapper.map(*git,VTK::renumber(*git,cornerIndexVTK),
327
case VTK::nonconforming :
328
return offset + VTK::renumber(*git,cornerIndexVTK);
330
DUNE_THROW(IOError,"VTKWriter: unsupported DataMode" << datamode);
322
335
CornerIterator cornerBegin () const
324
337
return CornerIterator( gridView_.template begin< 0, VTK_Partition >(),
325
338
gridView_.template end< 0, VTK_Partition >(),
326
339
datamode, *vertexmapper, number );
329
342
CornerIterator cornerEnd () const
331
344
return CornerIterator( gridView_.template end< 0, VTK_Partition >(),
332
345
gridView_.template end< 0, VTK_Partition >(),
333
346
datamode, *vertexmapper, number );
338
351
* @brief Construct a VTKWriter working on a specific GridView.
341
354
* @param gridView The gridView the grid functions live on. (E. g. a LevelGridView.)
342
355
* @param dm The data mode.
344
357
explicit VTKWriter ( const GridView &gridView,
345
358
VTK::DataMode dm = VTK::conforming )
346
: gridView_( gridView ),
359
: gridView_( gridView ),
660
675
const std::string& extendpath,
661
676
VTK::OutputType ot, const int commRank,
662
677
const int commSize )
679
// make data mode visible to private functions
682
// do some magic because paraview can only cope with relative pathes to piece files
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);
689
// write this processes .vtu/.vtp piece file
690
std::string fullname = getParallelPieceName(name, piecepath, commRank,
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);
697
gridView_.comm().barrier();
699
// if we are rank 0, write .pvtu/.pvtp parallel header
700
fullname = getParallelHeaderName(name, path, commSize);
664
// make data mode visible to private functions
667
// do some magic because paraview can only cope with relative pathes to piece files
669
std::string piecepath = concatPaths(path, extendpath);
670
std::string relpiecepath = relativePath(path, piecepath);
672
// write this processes .vtu/.vtp piece file
673
std::string fullname = getParallelPieceName(name, piecepath, commRank,
675
file.open(fullname.c_str(),std::ios::binary);
703
file.open(fullname.c_str());
676
704
if (! file.is_open())
677
DUNE_THROW(IOError, "Could not write to piecefile file " << fullname);
705
DUNE_THROW(IOError, "Could not write to parallel file " << fullname);
706
writeParallelHeader(file,name,relpiecepath, commSize );
680
gridView_.comm().barrier();
682
// if we are rank 0, write .pvtu/.pvtp parallel header
683
fullname = getParallelHeaderName(name, path, commSize);
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();
709
gridView_.comm().barrier();
697
714
//! write header file in parallel case to stream
715
732
void writeParallelHeader(std::ostream& s, const std::string& piecename,
716
733
const std::string& piecepath, const int commSize)
718
VTK::FileType fileType =
719
(n == 1) ? VTK::polyData : VTK::unstructuredGrid;
721
VTK::PVTUWriter writer(s, fileType);
728
for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end();
730
if ((*it)->ncomps()==1)
732
scalars = (*it)->name();
736
for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end();
738
if ((*it)->ncomps()>1)
740
vectors = (*it)->name();
743
writer.beginPointData(scalars, vectors);
745
for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end();
748
unsigned writecomps = (*it)->ncomps();
749
if(writecomps == 2) writecomps = 3;
750
writer.addArray<float>((*it)->name(), writecomps);
752
writer.endPointData();
757
for (FunctionIterator it=celldata.begin(); it!=celldata.end();
759
if ((*it)->ncomps()==1)
761
scalars = (*it)->name();
765
for (FunctionIterator it=celldata.begin(); it!=celldata.end();
767
if ((*it)->ncomps()>1)
769
vectors = (*it)->name();
772
writer.beginCellData(scalars, vectors);
774
for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it) {
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 )
789
const std::string& fullname = getParallelPieceName(piecename,
792
writer.addPiece(fullname);
735
VTK::FileType fileType =
736
(n == 1) ? VTK::polyData : VTK::unstructuredGrid;
738
VTK::PVTUWriter writer(s, fileType);
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 )
806
const std::string& fullname = getParallelPieceName(piecename,
809
writer.addPiece(fullname);
798
815
//! write data file to stream
799
816
void writeDataFile (std::ostream& s)
818
VTK::FileType fileType =
819
(n == 1) ? VTK::polyData : VTK::unstructuredGrid;
821
VTK::VTUWriter writer(s, outputtype, fileType);
823
// Grid characteristics
824
vertexmapper = new VertexMapper( gridView_ );
825
if (datamode == VTK::conforming)
801
VTK::FileType fileType =
802
(n == 1) ? VTK::polyData : VTK::unstructuredGrid;
804
VTK::VTUWriter writer(s, outputtype, fileType);
806
// Grid characteristics
807
vertexmapper = new VertexMapper( gridView_ );
808
if (datamode == VTK::conforming)
810
number.resize(vertexmapper->size());
811
for (std::vector<int>::size_type i=0; i<number.size(); i++) number[i] = -1;
813
countEntities(nvertices, ncells, ncorners);
815
writer.beginMain(ncells, nvertices);
827
number.resize(vertexmapper->size());
828
for (std::vector<int>::size_type i=0; i<number.size(); i++) number[i] = -1;
830
countEntities(nvertices, ncells, ncorners);
832
writer.beginMain(ncells, nvertices);
833
writeAllData(writer);
836
// write appended binary data section
837
if(writer.beginAppended())
816
838
writeAllData(writer);
819
// write appended binary data section
820
if(writer.beginAppended())
821
writeAllData(writer);
822
writer.endAppended();
824
delete vertexmapper; number.clear();
839
writer.endAppended();
841
delete vertexmapper; number.clear();
827
844
void writeAllData(VTK::VTUWriter& writer) {
842
859
std::string getFormatString() const
844
if (outputtype==VTK::ascii)
846
if (outputtype==VTK::base64)
848
if (outputtype==VTK::appendedraw)
850
if (outputtype==VTK::appendedbase64)
852
DUNE_THROW(IOError, "VTKWriter: unsupported OutputType" << outputtype);
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);
855
872
std::string getTypeString() const
860
return "UnstructuredGrid";
877
return "UnstructuredGrid";
863
880
//! count the vertices, cells and corners
864
881
virtual void countEntities(int &nvertices, int &ncells, int &ncorners)
886
for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
869
for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
889
// because of the use of vertexmapper->map(), this iteration must be
890
// in the order of Dune's numbering.
891
for (int i=0; i<it->template count<n>(); ++i)
872
// because of the use of vertexmapper->map(), this iteration must be
873
// in the order of Dune's numbering.
874
for (int i=0; i<it->template count<n>(); ++i)
877
if (datamode == VTK::conforming)
879
int alpha = vertexmapper->map(*it,i,n);
881
number[alpha] = nvertices++;
894
if (datamode == VTK::conforming)
896
int alpha = vertexmapper->map(*it,i,n);
898
number[alpha] = nvertices++;
891
908
//! write cell data
892
909
virtual void writeCellData(VTK::VTUWriter& writer)
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();
929
writer.beginCellData(scalars, vectors);
930
for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
894
if(celldata.size() == 0)
897
std::string scalars = "";
898
for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
899
if ((*it)->ncomps()==1)
901
scalars = (*it)->name();
904
std::string vectors = "";
905
for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
906
if ((*it)->ncomps()>1)
908
vectors = (*it)->name();
912
writer.beginCellData(scalars, vectors);
913
for (FunctionIterator it=celldata.begin(); it!=celldata.end(); ++it)
915
// vtk file format: a vector data always should have 3 comps (with
916
// 3rd comp = 0 in 2D case)
917
unsigned writecomps = (*it)->ncomps();
918
if(writecomps == 2) writecomps = 3;
919
shared_ptr<VTK::DataArrayWriter<float> > p
920
(writer.makeArrayWriter<float>((*it)->name(), writecomps,
922
if(!p->writeIsNoop())
923
for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
925
for (int j=0; j<(*it)->ncomps(); j++)
926
p->write((*it)->evaluate(j,*i,i.position()));
927
// vtk file format: a vector data always should have 3 comps
928
// (with 3rd comp = 0 in 2D case)
929
for (unsigned j=(*it)->ncomps(); j < writecomps; ++j)
933
writer.endCellData();
932
// vtk file format: a vector data always should have 3 comps (with
933
// 3rd comp = 0 in 2D case)
934
unsigned writecomps = (*it)->ncomps();
935
if(writecomps == 2) writecomps = 3;
936
shared_ptr<VTK::DataArrayWriter<float> > p
937
(writer.makeArrayWriter<float>((*it)->name(), writecomps,
939
if(!p->writeIsNoop())
940
for (CellIterator i=cellBegin(); i!=cellEnd(); ++i)
942
for (int j=0; j<(*it)->ncomps(); j++)
943
p->write((*it)->evaluate(j,*i,i.position()));
944
// vtk file format: a vector data always should have 3 comps
945
// (with 3rd comp = 0 in 2D case)
946
for (unsigned j=(*it)->ncomps(); j < writecomps; ++j)
950
writer.endCellData();
936
953
//! write vertex data
937
954
virtual void writeVertexData(VTK::VTUWriter& writer)
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();
974
writer.beginPointData(scalars, vectors);
975
for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
939
if(vertexdata.size() == 0)
942
std::string scalars = "";
943
for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
944
if ((*it)->ncomps()==1)
946
scalars = (*it)->name();
949
std::string vectors = "";
950
for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
951
if ((*it)->ncomps()>1)
953
vectors = (*it)->name();
957
writer.beginPointData(scalars, vectors);
958
for (FunctionIterator it=vertexdata.begin(); it!=vertexdata.end(); ++it)
960
// vtk file format: a vector data always should have 3 comps (with
961
// 3rd comp = 0 in 2D case)
962
unsigned writecomps = (*it)->ncomps();
963
if(writecomps == 2) writecomps = 3;
964
shared_ptr<VTK::DataArrayWriter<float> > p
965
(writer.makeArrayWriter<float>((*it)->name(), writecomps,
967
if(!p->writeIsNoop())
968
for (VertexIterator vit=vertexBegin(); vit!=vertexEnd(); ++vit)
970
for (int j=0; j<(*it)->ncomps(); j++)
971
p->write((*it)->evaluate(j,*vit,vit.position()));
972
// vtk file format: a vector data always should have 3 comps
973
// (with 3rd comp = 0 in 2D case)
974
for (unsigned j=(*it)->ncomps(); j < writecomps; ++j)
978
writer.endPointData();
977
// vtk file format: a vector data always should have 3 comps (with
978
// 3rd comp = 0 in 2D case)
979
unsigned writecomps = (*it)->ncomps();
980
if(writecomps == 2) writecomps = 3;
981
shared_ptr<VTK::DataArrayWriter<float> > p
982
(writer.makeArrayWriter<float>((*it)->name(), writecomps,
984
if(!p->writeIsNoop())
985
for (VertexIterator vit=vertexBegin(); vit!=vertexEnd(); ++vit)
987
for (int j=0; j<(*it)->ncomps(); j++)
988
p->write((*it)->evaluate(j,*vit,vit.position()));
989
// vtk file format: a vector data always should have 3 comps
990
// (with 3rd comp = 0 in 2D case)
991
for (unsigned j=(*it)->ncomps(); j < writecomps; ++j)
995
writer.endPointData();
981
998
//! write the positions of vertices
982
999
virtual void writeGridPoints(VTK::VTUWriter& writer)
984
writer.beginPoints();
1001
writer.beginPoints();
986
shared_ptr<VTK::DataArrayWriter<float> > p
987
(writer.makeArrayWriter<float>("Coordinates", 3, nvertices));
988
if(!p->writeIsNoop()) {
989
VertexIterator vEnd = vertexEnd();
990
for (VertexIterator vit=vertexBegin(); vit!=vEnd; ++vit)
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++)
1003
shared_ptr<VTK::DataArrayWriter<float> > p
1004
(writer.makeArrayWriter<float>("Coordinates", 3, nvertices));
1005
if(!p->writeIsNoop()) {
1006
VertexIterator vEnd = vertexEnd();
1007
for (VertexIterator vit=vertexBegin(); vit!=vEnd; ++vit)
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++)
999
// free the VTK::DataArrayWriter before touching the stream
1016
// free the VTK::DataArrayWriter before touching the stream
1005
1022
//! write the connectivity array
1006
1023
virtual void writeGridCells(VTK::VTUWriter& writer)
1008
writer.beginCells();
1012
shared_ptr<VTK::DataArrayWriter<int> > p1
1013
(writer.makeArrayWriter<int>("connectivity", 1, ncorners));
1014
if(!p1->writeIsNoop())
1015
for (CornerIterator it=cornerBegin(); it!=cornerEnd(); ++it)
1021
shared_ptr<VTK::DataArrayWriter<int> > p2
1022
(writer.makeArrayWriter<int>("offsets", 1, ncells));
1023
if(!p2->writeIsNoop()) {
1025
for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
1027
offset += it->template count<n>();
1036
shared_ptr<VTK::DataArrayWriter<unsigned char> > p3
1037
(writer.makeArrayWriter<unsigned char>("types", 1, ncells));
1038
if(!p3->writeIsNoop())
1039
for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
1041
int vtktype = VTK::geometryType(it->type());
1025
writer.beginCells();
1029
shared_ptr<VTK::DataArrayWriter<int> > p1
1030
(writer.makeArrayWriter<int>("connectivity", 1, ncorners));
1031
if(!p1->writeIsNoop())
1032
for (CornerIterator it=cornerBegin(); it!=cornerEnd(); ++it)
1038
shared_ptr<VTK::DataArrayWriter<int> > p2
1039
(writer.makeArrayWriter<int>("offsets", 1, ncells));
1040
if(!p2->writeIsNoop()) {
1042
for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
1044
offset += it->template count<n>();
1053
shared_ptr<VTK::DataArrayWriter<unsigned char> > p3
1054
(writer.makeArrayWriter<unsigned char>("types", 1, ncells));
1055
if(!p3->writeIsNoop())
1056
for (CellIterator it=cellBegin(); it!=cellEnd(); ++it)
1058
int vtktype = VTK::geometryType(it->type());
1050
1067
// the list of registered functions
1051
1068
std::list<VTKFunctionPtr> celldata;
1052
1069
std::list<VTKFunctionPtr> vertexdata;
1056
1072
GridView gridView_;
1058
1074
// temporary grid information