dune-vtk 2.12
Loading...
Searching...
No Matches
rectilineargridwriter.impl.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3#pragma once
4
5#include <iomanip>
6#include <iostream>
7#include <iterator>
8#include <fstream>
9#include <sstream>
10#include <string>
11
13#include <dune/geometry/type.hh>
14
18
19namespace Dune::Vtk {
20
21template <class GV, class DC>
22void RectilinearGridWriter<GV,DC>
23 ::writeSerialFile (std::ofstream& out) const
24{
25 std::vector<pos_type> offsets; // pos => offset
26 this->writeHeader(out, "RectilinearGrid");
27
28 auto const& wholeExtent = dataCollector_->wholeExtent();
29 out << "<RectilinearGrid"
30 << " WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\""
31 << ">\n";
32
33 dataCollector_->writeLocalPiece([&out](auto const& extent) {
34 out << "<Piece Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\">\n";
35 });
36
37 // Write point coordinates for x, y, and z ordinate
38 out << "<Coordinates>\n";
39 writeCoordinates(out, offsets);
40 out << "</Coordinates>\n";
41
42 // Write data associated with grid points
43 out << "<PointData" << this->getNames(pointData_) << ">\n";
44 for (auto const& v : pointData_)
45 this->writeData(out, offsets, v, Super::POINT_DATA);
46 out << "</PointData>\n";
47
48 // Write data associated with grid cells
49 out << "<CellData" << this->getNames(cellData_) << ">\n";
50 for (auto const& v : cellData_)
51 this->writeData(out, offsets, v, Super::CELL_DATA);
52 out << "</CellData>\n";
53
54 out << "</Piece>\n";
55 out << "</RectilinearGrid>\n";
56
57 this->writeAppended(out, offsets);
58 out << "</VTKFile>";
59}
60
61
62template <class GV, class DC>
63void RectilinearGridWriter<GV,DC>
64 ::writeParallelFile (std::ofstream& out, std::string const& pfilename, int /*size*/) const
65{
66 this->writeHeader(out, "PRectilinearGrid");
67
68 auto const& wholeExtent = dataCollector_->wholeExtent();
69 out << "<PRectilinearGrid"
70 << " GhostLevel=\"" << dataCollector_->ghostLevel() << "\""
71 << " WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\""
72 << ">\n";
73
74 // Write point coordinates for x, y, and z ordinate
75 out << "<PCoordinates>\n";
76 out << "<PDataArray Name=\"x\" type=\"" << to_string(datatype_) << "\" />\n";
77 out << "<PDataArray Name=\"y\" type=\"" << to_string(datatype_) << "\" />\n";
78 out << "<PDataArray Name=\"z\" type=\"" << to_string(datatype_) << "\" />\n";
79 out << "</PCoordinates>\n";
80
81 // Write data associated with grid points
82 out << "<PPointData" << this->getNames(pointData_) << ">\n";
83 for (auto const& v : pointData_) {
84 out << "<PDataArray"
85 << " Name=\"" << v.name() << "\""
86 << " type=\"" << to_string(v.dataType()) << "\""
87 << " NumberOfComponents=\"" << v.numComponents() << "\""
88 << " />\n";
89 }
90 out << "</PPointData>\n";
91
92 // Write data associated with grid cells
93 out << "<PCellData" << this->getNames(cellData_) << ">\n";
94 for (auto const& v : cellData_) {
95 out << "<PDataArray"
96 << " Name=\"" << v.name() << "\""
97 << " type=\"" << to_string(v.dataType()) << "\""
98 << " NumberOfComponents=\"" << v.numComponents() << "\""
99 << " />\n";
100 }
101 out << "</PCellData>\n";
102
103 // Write piece file references
104 dataCollector_->writePieces([&out,pfilename,ext=this->fileExtension()](int p, auto const& extent, bool write_extent)
105 {
106 std::string piece_source = pfilename + "_p" + std::to_string(p) + "." + ext;
107 out << "<Piece Source=\"" << piece_source << "\"";
108 if (write_extent)
109 out << " Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\"";
110 out << " />\n";
111 });
112
113 out << "</PRectilinearGrid>\n";
114 out << "</VTKFile>";
115}
116
117
118template <class GV, class DC>
119void RectilinearGridWriter<GV,DC>
120 ::writeCoordinates (std::ofstream& out, std::vector<pos_type>& offsets,
121 std::optional<std::size_t> timestep) const
122{
123 std::string names = "xyz";
124 if (format_ == Vtk::FormatTypes::ASCII) {
125 auto coordinates = dataCollector_->template coordinates<double>();
126 for (std::size_t d = 0; d < 3; ++d) {
127 out << "<DataArray type=\"" << to_string(datatype_) << "\" Name=\"" << names[d] << "\" format=\"ascii\"";
128 if (timestep)
129 out << " TimeStep=\"" << *timestep << "\"";
130 out << ">\n";
131 this->writeValuesAscii(out, coordinates[d]);
132 out << "</DataArray>\n";
133 }
134 }
135 else { // Vtk::FormatTypes::APPENDED format
136 for (std::size_t j = 0; j < 3; ++j) {
137 out << "<DataArray type=\"" << to_string(datatype_) << "\" Name=\"" << names[j] << "\" format=\"appended\"";
138 if (timestep)
139 out << " TimeStep=\"" << *timestep << "\"";
140 out << " offset=";
141 offsets.push_back(out.tellp());
143 out << "/>\n";
144 }
145 }
146}
147
148
149template <class GV, class DC>
150void RectilinearGridWriter<GV,DC>
151 ::writeGridAppended (std::ofstream& out, std::vector<std::uint64_t>& blocks) const
152{
153 assert(is_a(format_, Vtk::FormatTypes::APPENDED) && "Function should by called only in appended mode!\n");
154
155 // write coordinates along axis
156 Vtk::mapDataTypes<std::is_floating_point, std::is_integral>(datatype_, headertype_,
157 [&](auto f, auto h) {
158 using F = typename decltype(f)::type;
159 using H = typename decltype(h)::type;
160 auto coordinates = dataCollector_->template coordinates<F>();
161 blocks.push_back(this->template writeValuesAppended<H>(out, coordinates[0]));
162 blocks.push_back(this->template writeValuesAppended<H>(out, coordinates[1]));
163 blocks.push_back(this->template writeValuesAppended<H>(out, coordinates[2]));
164 });
165}
166
167} // end namespace Dune::Vtk
Definition datacollector.hh:19
constexpr bool is_a(E a, Integer b)
Definition enum.hh:11
std::string to_string(Vtk::FormatTypes)
Definition types.cc:13
std::string join(InputIter first, InputIter end, std::string sep=" ")
Join all entries in the range [first,end) into a single string separated by sep
Definition string.hh:111
T push_back(T... args)
T tellp(T... args)
T to_string(T... args)