dune-vtk  0.2
vtkstructuredgridwriter.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 VtkStructuredGridWriter<GV,DC>
21  ::writeSerialFile (std::ofstream& out) const
22 {
23  std::vector<pos_type> offsets; // pos => offset
24  this->writeHeader(out, "StructuredGrid");
25 
26  auto const& wholeExtent = dataCollector_->wholeExtent();
27  out << "<StructuredGrid WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\">\n";
28 
29  dataCollector_->writeLocalPiece([&out](auto const& extent) {
30  out << "<Piece Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\">\n";
31  });
32 
33  // Write point coordinates
34  out << "<Points>\n";
35  this->writePoints(out, offsets);
36  out << "</Points>\n";
37 
38  // Write data associated with grid points
39  out << "<PointData" << this->getNames(pointData_) << ">\n";
40  for (auto const& v : pointData_)
41  this->writeData(out, offsets, v, Super::POINT_DATA);
42  out << "</PointData>\n";
43 
44  // Write data associated with grid cells
45  out << "<CellData" << this->getNames(cellData_) << ">\n";
46  for (auto const& v : cellData_)
47  this->writeData(out, offsets, v, Super::CELL_DATA);
48  out << "</CellData>\n";
49 
50  out << "</Piece>\n";
51  out << "</StructuredGrid>\n";
52 
53  this->writeAppended(out, offsets);
54  out << "</VTKFile>";
55 }
56 
57 
58 template <class GV, class DC>
59 void VtkStructuredGridWriter<GV,DC>
60  ::writeParallelFile (std::ofstream& out, std::string const& pfilename, int /*size*/) const
61 {
62  this->writeHeader(out, "PStructuredGrid");
63 
64  auto const& wholeExtent = dataCollector_->wholeExtent();
65  out << "<PStructuredGrid"
66  << " GhostLevel=\"" << dataCollector_->ghostLevel() << "\""
67  << " WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\""
68  << ">\n";
69 
70  // Write points
71  out << "<PPoints>\n";
72  out << "<PDataArray"
73  << " type=\"" << to_string(datatype_) << "\""
74  << " NumberOfComponents=\"3\""
75  << " />\n";
76  out << "</PPoints>\n";
77 
78  // Write data associated with grid points
79  out << "<PPointData" << this->getNames(pointData_) << ">\n";
80  for (auto const& v : pointData_) {
81  out << "<PDataArray"
82  << " Name=\"" << v.name() << "\""
83  << " type=\"" << to_string(v.dataType()) << "\""
84  << " NumberOfComponents=\"" << v.numComponents() << "\""
85  << " />\n";
86  }
87  out << "</PPointData>\n";
88 
89  // Write data associated with grid cells
90  out << "<PCellData" << this->getNames(cellData_) << ">\n";
91  for (auto const& v : cellData_) {
92  out << "<PDataArray"
93  << " Name=\"" << v.name() << "\""
94  << " type=\"" << to_string(v.dataType()) << "\""
95  << " NumberOfComponents=\"" << v.numComponents() << "\""
96  << " />\n";
97  }
98  out << "</PCellData>\n";
99 
100  // Write piece file references
101  dataCollector_->writePieces([&out,pfilename,ext=this->fileExtension()](int p, auto const& extent, bool write_extent)
102  {
103  std::string piece_source = pfilename + "_p" + std::to_string(p) + "." + ext;
104  out << "<Piece Source=\"" << piece_source << "\"";
105  if (write_extent)
106  out << " Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\"";
107  out << " />\n";
108  });
109 
110  out << "</PStructuredGrid>\n";
111  out << "</VTKFile>";
112 }
113 
114 
115 template <class GV, class DC>
116 void VtkStructuredGridWriter<GV,DC>
117  ::writeGridAppended (std::ofstream& out, std::vector<std::uint64_t>& blocks) const
118 {
119  assert(is_a(format_, Vtk::FormatTypes::APPENDED) && "Function should by called only in appended mode!\n");
120 
121  // write points
122  Vtk::mapDataTypes<std::is_floating_point, std::is_integral>(datatype_, headertype_,
123  [&](auto f, auto h) {
124  using F = typename decltype(f)::type;
125  using H = typename decltype(h)::type;
126  blocks.push_back(this->template writeValuesAppended<H>(out, dataCollector_->template points<F>()));
127  });
128 }
129 
130 } // end namespace Dune
Definition: datacollectorinterface.hh:9
std::string to_string(Vtk::FormatTypes type)
Definition: types.cc:12
@ APPENDED
Definition: types.hh:25
constexpr bool is_a(E a, Integer b)
Definition: enum.hh:12
std::string join(InputIter first, InputIter end, std::string sep=" ")
Definition: string.hh:110