vtkstructuredgridwriter.impl.hh 3.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once

#include <iomanip>
#include <iostream>
#include <iterator>
#include <fstream>
#include <sstream>
#include <string>

#include <dune/geometry/referenceelements.hh>
#include <dune/geometry/type.hh>

13
14
15
#include <dune/vtk/utility/enum.hh>
#include <dune/vtk/utility/filesystem.hh>
#include <dune/vtk/utility/string.hh>
16

17
namespace Dune {
18
19
20

template <class GV, class DC>
void VtkStructuredGridWriter<GV,DC>
21
  ::writeSerialFile (std::ofstream& out) const
22
23
{
  std::vector<pos_type> offsets; // pos => offset
24
  this->writeHeader(out, "StructuredGrid");
Praetorius, Simon's avatar
Praetorius, Simon committed
25

26
  auto const& wholeExtent = dataCollector_->wholeExtent();
Stenger, Florian's avatar
Stenger, Florian committed
27
  out << "<StructuredGrid WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\">\n";
28

29
  dataCollector_->writeLocalPiece([&out](auto const& extent) {
Stenger, Florian's avatar
Stenger, Florian committed
30
    out << "<Piece Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\">\n";
31
32
  });

33
34
35
36
37
  // Write point coordinates
  out << "<Points>\n";
  this->writePoints(out, offsets);
  out << "</Points>\n";

Praetorius, Simon's avatar
Praetorius, Simon committed
38
39
40
41
42
  // Write data associated with grid points
  out << "<PointData" << this->getNames(pointData_) << ">\n";
  for (auto const& v : pointData_)
    this->writeData(out, offsets, v, Super::POINT_DATA);
  out << "</PointData>\n";
43

Praetorius, Simon's avatar
Praetorius, Simon committed
44
45
46
47
48
  // Write data associated with grid cells
  out << "<CellData" << this->getNames(cellData_) << ">\n";
  for (auto const& v : cellData_)
    this->writeData(out, offsets, v, Super::CELL_DATA);
  out << "</CellData>\n";
49
50
51
52

  out << "</Piece>\n";
  out << "</StructuredGrid>\n";

53
  this->writeAppended(out, offsets);
54
55
56
57
58
59
  out << "</VTKFile>";
}


template <class GV, class DC>
void VtkStructuredGridWriter<GV,DC>
60
  ::writeParallelFile (std::ofstream& out, std::string const& pfilename, int /*size*/) const
61
{
62
  this->writeHeader(out, "PStructuredGrid");
Praetorius, Simon's avatar
Praetorius, Simon committed
63

64
  auto const& wholeExtent = dataCollector_->wholeExtent();
Praetorius, Simon's avatar
Praetorius, Simon committed
65
  out << "<PStructuredGrid"
66
      << " GhostLevel=\"" << dataCollector_->ghostLevel() << "\""
Stenger, Florian's avatar
Stenger, Florian committed
67
      << " WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\""
Praetorius, Simon's avatar
Praetorius, Simon committed
68
69
      << ">\n";

70
71
72
73
74
75
76
77
  // Write points
  out << "<PPoints>\n";
  out << "<PDataArray"
      << " type=\"" << to_string(datatype_) << "\""
      << " NumberOfComponents=\"3\""
      << " />\n";
  out << "</PPoints>\n";

Praetorius, Simon's avatar
Praetorius, Simon committed
78
79
80
81
82
83
84
85
  // Write data associated with grid points
  out << "<PPointData" << this->getNames(pointData_) << ">\n";
  for (auto const& v : pointData_) {
    out << "<PDataArray"
        << " Name=\"" << v.name() << "\""
        << " type=\"" << to_string(v.type()) << "\""
        << " NumberOfComponents=\"" << v.ncomps() << "\""
        << " />\n";
86
  }
Praetorius, Simon's avatar
Praetorius, Simon committed
87
88
89
90
91
92
93
94
95
96
  out << "</PPointData>\n";

  // Write data associated with grid cells
  out << "<PCellData" << this->getNames(cellData_) << ">\n";
  for (auto const& v : cellData_) {
    out << "<PDataArray"
        << " Name=\"" << v.name() << "\""
        << " type=\"" << to_string(v.type()) << "\""
        << " NumberOfComponents=\"" << v.ncomps() << "\""
        << " />\n";
97
  }
Praetorius, Simon's avatar
Praetorius, Simon committed
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
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
103
104
105
    std::string piece_source = pfilename + "_p" + std::to_string(p) + "." + ext;
    out << "<Piece Source=\"" << piece_source << "\"";
    if (write_extent)
Stenger, Florian's avatar
Stenger, Florian committed
106
      out << " Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\"";
107
108
    out << " />\n";
  });
109

110
  out << "</PStructuredGrid>\n";
111
112
113
  out << "</VTKFile>";
}

114
115
116
117
118
119
120
121

template <class GV, class DC>
void VtkStructuredGridWriter<GV,DC>
  ::writeGridAppended (std::ofstream& out, std::vector<std::uint64_t>& blocks) const
{
  assert(is_a(format_, Vtk::APPENDED) && "Function should by called only in appended mode!\n");

  // write points
122
123
124
125
126
127
  Vtk::mapDataTypes<std::is_floating_point, std::is_integral>(datatype_, headertype_,
  [&](auto f, auto h) {
    using F = typename decltype(f)::type;
    using H = typename decltype(h)::type;
    blocks.push_back(this->template writeValuesAppended<H>(out, dataCollector_->template points<F>()));
  });
128
129
}

130
} // end namespace Dune