dune-vtk  0.2
vtkimagedatawriter.impl.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iomanip>
4 #include <iostream>
5 #include <iterator>
6 #include <fstream>
7 #include <sstream>
8 #include <string>
9 
10 #include <dune/geometry/referenceelements.hh>
11 #include <dune/geometry/type.hh>
12 
13 #include <dune/vtk/utility/enum.hh>
16 
17 namespace Dune {
18 
19 template <class GV, class DC>
20 void VtkImageDataWriter<GV,DC>
21  ::writeSerialFile (std::ofstream& out) const
22 {
23  std::vector<pos_type> offsets; // pos => offset
24  this->writeHeader(out, "ImageData");
25 
26  auto const& wholeExtent = dataCollector_->wholeExtent();
27  auto const& origin = dataCollector_->origin();
28  auto const& spacing = dataCollector_->spacing();
29  out << "<ImageData"
30  << " WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\""
31  << " Origin=\"" << Vtk::join(origin.begin(), origin.end()) << "\""
32  << " Spacing=\"" << Vtk::join(spacing.begin(), spacing.end()) << "\""
33  << ">\n";
34 
35  dataCollector_->writeLocalPiece([&out](auto const& extent) {
36  out << "<Piece Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\">\n";
37  });
38 
39  // Write data associated with grid points
40  out << "<PointData" << this->getNames(pointData_) << ">\n";
41  for (auto const& v : pointData_)
42  this->writeData(out, offsets, v, Super::POINT_DATA);
43  out << "</PointData>\n";
44 
45  // Write data associated with grid cells
46  out << "<CellData" << this->getNames(cellData_) << ">\n";
47  for (auto const& v : cellData_)
48  this->writeData(out, offsets, v, Super::CELL_DATA);
49  out << "</CellData>\n";
50 
51  out << "</Piece>\n";
52  out << "</ImageData>\n";
53 
54  this->writeAppended(out, offsets);
55  out << "</VTKFile>";
56 }
57 
58 
59 template <class GV, class DC>
60 void VtkImageDataWriter<GV,DC>
61  ::writeParallelFile (std::ofstream& out, std::string const& pfilename, int /*size*/) const
62 {
63  this->writeHeader(out, "PImageData");
64 
65  auto const& wholeExtent = dataCollector_->wholeExtent();
66  auto const& origin = dataCollector_->origin();
67  auto const& spacing = dataCollector_->spacing();
68  out << "<PImageData"
69  << " GhostLevel=\"" << dataCollector_->ghostLevel() << "\""
70  << " WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\""
71  << " Origin=\"" << Vtk::join(origin.begin(), origin.end()) << "\""
72  << " Spacing=\"" << Vtk::join(spacing.begin(), spacing.end()) << "\""
73  << ">\n";
74 
75  // Write data associated with grid points
76  out << "<PPointData" << this->getNames(pointData_) << ">\n";
77  for (auto const& v : pointData_) {
78  out << "<PDataArray"
79  << " Name=\"" << v.name() << "\""
80  << " type=\"" << to_string(v.dataType()) << "\""
81  << " NumberOfComponents=\"" << v.numComponents() << "\""
82  << " />\n";
83  }
84  out << "</PPointData>\n";
85 
86  // Write data associated with grid cells
87  out << "<PCellData" << this->getNames(cellData_) << ">\n";
88  for (auto const& v : cellData_) {
89  out << "<PDataArray"
90  << " Name=\"" << v.name() << "\""
91  << " type=\"" << to_string(v.dataType()) << "\""
92  << " NumberOfComponents=\"" << v.numComponents() << "\""
93  << " />\n";
94  }
95  out << "</PCellData>\n";
96 
97  // Write piece file references
98  dataCollector_->writePieces([&out,pfilename,ext=this->fileExtension()](int p, auto const& extent, bool write_extent)
99  {
100  std::string piece_source = pfilename + "_p" + std::to_string(p) + "." + ext;
101  out << "<Piece Source=\"" << piece_source << "\"";
102  if (write_extent)
103  out << " Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\"";
104  out << " />\n";
105  });
106 
107  out << "</PImageData>\n";
108  out << "</VTKFile>";
109 }
110 
111 } // end namespace Dune
Definition: datacollectorinterface.hh:9
std::string to_string(Vtk::FormatTypes type)
Definition: types.cc:12
std::string join(InputIter first, InputIter end, std::string sep=" ")
Definition: string.hh:110