10 #include <dune/geometry/referenceelements.hh>
11 #include <dune/geometry/type.hh>
19 template <
class GV,
class DC>
20 void VtkUnstructuredGridWriter<GV,DC>
21 ::writeSerialFile (std::ofstream& out)
const
23 std::vector<pos_type> offsets;
24 this->writeHeader(out,
"UnstructuredGrid");
25 out <<
"<UnstructuredGrid>\n";
28 <<
" NumberOfPoints=\"" << dataCollector_->numPoints() <<
"\""
29 <<
" NumberOfCells=\"" << dataCollector_->numCells() <<
"\""
34 this->writePoints(out, offsets);
39 writeCells(out, offsets);
40 writePointIds(out, offsets);
44 out <<
"<PointData" << this->getNames(pointData_) <<
">\n";
45 for (
auto const& v : pointData_)
46 this->writeData(out, offsets, v, Super::POINT_DATA);
47 out <<
"</PointData>\n";
50 out <<
"<CellData" << this->getNames(cellData_) <<
">\n";
51 for (
auto const& v : cellData_)
52 this->writeData(out, offsets, v, Super::CELL_DATA);
53 out <<
"</CellData>\n";
56 out <<
"</UnstructuredGrid>\n";
58 this->writeAppended(out, offsets);
63 template <
class GV,
class DC>
64 void VtkUnstructuredGridWriter<GV,DC>
65 ::writeParallelFile (std::ofstream& out, std::string
const& pfilename,
int size)
const
67 this->writeHeader(out,
"PUnstructuredGrid");
68 out <<
"<PUnstructuredGrid GhostLevel=\"0\">\n";
73 <<
" type=\"" <<
to_string(datatype_) <<
"\""
74 <<
" NumberOfComponents=\"3\""
76 out <<
"</PPoints>\n";
79 out <<
"<PPointData" << this->getNames(pointData_) <<
">\n";
80 for (
auto const& v : pointData_) {
82 <<
" Name=\"" << v.name() <<
"\""
83 <<
" type=\"" <<
to_string(v.dataType()) <<
"\""
84 <<
" NumberOfComponents=\"" << v.numComponents() <<
"\""
87 out <<
"</PPointData>\n";
90 out <<
"<PCellData" << this->getNames(cellData_) <<
">\n";
91 for (
auto const& v : cellData_) {
93 <<
" Name=\"" << v.name() <<
"\""
94 <<
" type=\"" <<
to_string(v.dataType()) <<
"\""
95 <<
" NumberOfComponents=\"" << v.numComponents() <<
"\""
98 out <<
"</PCellData>\n";
101 for (
int p = 0; p < size; ++p) {
102 std::string piece_source = pfilename +
"_p" +
std::to_string(p) +
"." + this->fileExtension();
103 out <<
"<Piece Source=\"" << piece_source <<
"\" />\n";
106 out <<
"</PUnstructuredGrid>\n";
111 template <
class GV,
class DC>
112 void VtkUnstructuredGridWriter<GV,DC>
113 ::writeTimeseriesSerialFile (std::ofstream& out,
114 std::string
const& filenameMesh,
115 std::vector<std::pair<double, std::string>>
const& timesteps,
116 std::vector<std::uint64_t>
const& blocks)
const
120 std::vector<std::vector<pos_type>> offsets(timesteps.size());
121 this->writeHeader(out,
"UnstructuredGrid");
122 out <<
"<UnstructuredGrid"
126 for (
auto const& timestep : timesteps)
127 out << timestep.first << (++i % 6 != 0 ?
' ' :
'\n');
132 <<
" NumberOfPoints=\"" << dataCollector_->numPoints() <<
"\""
133 <<
" NumberOfCells=\"" << dataCollector_->numCells() <<
"\""
138 for (std::size_t i = 0; i < timesteps.size(); ++i) {
139 this->writePoints(out, offsets[i], i);
141 out <<
"</Points>\n";
145 for (std::size_t i = 0; i < timesteps.size(); ++i) {
146 writeCells(out, offsets[i], i);
147 writePointIds(out, offsets[i], i);
151 const std::size_t shift = offsets[0].size();
154 out <<
"<PointData" << this->getNames(pointData_) <<
">\n";
155 for (std::size_t i = 0; i < timesteps.size(); ++i) {
156 for (
auto const& v : pointData_)
157 this->writeData(out, offsets[i], v, Super::POINT_DATA, i);
159 out <<
"</PointData>\n";
162 out <<
"<CellData" << this->getNames(cellData_) <<
">\n";
163 for (std::size_t i = 0; i < timesteps.size(); ++i) {
164 for (
auto const& v : cellData_)
165 this->writeData(out, offsets[i], v, Super::CELL_DATA, i);
167 out <<
"</CellData>\n";
170 out <<
"</UnstructuredGrid>\n";
172 out <<
"<AppendedData encoding=\"raw\">\n_";
173 pos_type appended_pos = out.tellp();
176 std::ifstream file_mesh(filenameMesh, std::ios_base::in | std::ios_base::binary);
177 out << file_mesh.rdbuf();
178 assert( std::uint64_t(out.tellp()) == std::accumulate(blocks.begin(), std::next(blocks.begin(),shift), std::uint64_t(appended_pos)) );
182 for (
auto const& timestep : timesteps) {
183 std::ifstream file(timestep.second, std::ios_base::in | std::ios_base::binary);
186 out <<
"</AppendedData>\n";
192 for (std::size_t i = 0; i < timesteps.size(); ++i) {
194 auto const& off = offsets[i];
197 for (std::size_t j = 0; j < shift; ++j) {
199 out <<
'"' << offset <<
'"';
200 offset += pos_type(blocks[j]);
204 std::size_t j = shift;
205 for (std::size_t i = 0; i < timesteps.size(); ++i) {
206 auto const& off = offsets[i];
208 for (std::size_t k = shift; k < off.size(); ++k) {
210 out <<
'"' << offset <<
'"';
211 offset += pos_type(blocks[j++]);
217 template <
class GV,
class DC>
218 void VtkUnstructuredGridWriter<GV,DC>
219 ::writeTimeseriesParallelFile (std::ofstream& out,
220 std::string
const& pfilename,
222 std::vector<std::pair<double, std::string>>
const& timesteps)
const
224 this->writeHeader(out,
"PUnstructuredGrid");
225 out <<
"<PUnstructuredGrid GhostLevel=\"0\""
229 for (
auto const& timestep : timesteps)
230 out << timestep.first << (++i % 6 != 0 ?
' ' :
'\n');
235 out <<
"<PPoints>\n";
237 <<
" type=\"" <<
to_string(datatype_) <<
"\""
238 <<
" NumberOfComponents=\"3\""
240 out <<
"</PPoints>\n";
243 out <<
"<PPointData" << this->getNames(pointData_) <<
">\n";
244 for (std::size_t i = 0; i < timesteps.size(); ++i) {
245 for (
auto const& v : pointData_) {
247 <<
" Name=\"" << v.name() <<
"\""
248 <<
" type=\"" <<
to_string(v.dataType()) <<
"\""
249 <<
" NumberOfComponents=\"" << v.numComponents() <<
"\""
250 <<
" TimeStep=\"" << i <<
"\""
254 out <<
"</PPointData>\n";
257 out <<
"<PCellData" << this->getNames(cellData_) <<
">\n";
258 for (std::size_t i = 0; i < timesteps.size(); ++i) {
259 for (
auto const& v : cellData_) {
261 <<
" Name=\"" << v.name() <<
"\""
262 <<
" type=\"" <<
to_string(v.dataType()) <<
"\""
263 <<
" NumberOfComponents=\"" << v.numComponents() <<
"\""
264 <<
" TimeStep=\"" << i <<
"\""
268 out <<
"</PCellData>\n";
271 for (
int p = 0; p < size; ++p) {
272 std::string piece_source = pfilename +
"_p" +
std::to_string(p) +
"." + this->fileExtension();
273 out <<
"<Piece Source=\"" << piece_source <<
"\" />\n";
276 out <<
"</PUnstructuredGrid>\n";
281 template <
class GV,
class DC>
282 void VtkUnstructuredGridWriter<GV,DC>
283 ::writeCells (std::ofstream& out, std::vector<pos_type>& offsets,
284 std::optional<std::size_t> timestep)
const
287 auto cells = dataCollector_->cells();
288 out <<
"<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"";
290 out <<
" TimeStep=\"" << *timestep <<
"\"";
292 this->writeValuesAscii(out, cells.connectivity);
293 out <<
"</DataArray>\n";
295 out <<
"<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"";
297 out <<
" TimeStep=\"" << *timestep <<
"\"";
299 this->writeValuesAscii(out, cells.offsets);
300 out <<
"</DataArray>\n";
302 out <<
"<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\"";
304 out <<
" TimeStep=\"" << *timestep <<
"\"";
306 this->writeValuesAscii(out, cells.types);
307 out <<
"</DataArray>\n";
310 out <<
"<DataArray type=\"Int64\" Name=\"connectivity\" format=\"appended\"";
312 out <<
" TimeStep=\"" << *timestep <<
"\"";
314 offsets.push_back(out.tellp());
315 out << std::string(std::numeric_limits<std::uint64_t>::digits10 + 2,
' ');
318 out <<
"<DataArray type=\"Int64\" Name=\"offsets\" format=\"appended\"";
320 out <<
" TimeStep=\"" << *timestep <<
"\"";
322 offsets.push_back(out.tellp());
323 out << std::string(std::numeric_limits<std::uint64_t>::digits10 + 2,
' ');
326 out <<
"<DataArray type=\"UInt8\" Name=\"types\" format=\"appended\"";
328 out <<
" TimeStep=\"" << *timestep <<
"\"";
330 offsets.push_back(out.tellp());
331 out << std::string(std::numeric_limits<std::uint64_t>::digits10 + 2,
' ');
337 template <
class GV,
class DC>
338 void VtkUnstructuredGridWriter<GV,DC>
339 ::writePointIds (std::ofstream& out,
340 std::vector<pos_type>& offsets,
341 std::optional<std::size_t> timestep)
const
343 auto ids = dataCollector_->pointIds();
348 out <<
"<DataArray type=\"UInt64\" Name=\"global_point_ids\" format=\"ascii\"";
350 out <<
" TimeStep=\"" << *timestep <<
"\"";
352 this->writeValuesAscii(out, ids);
353 out <<
"</DataArray>\n";
356 out <<
"<DataArray type=\"UInt64\" Name=\"global_point_ids\" format=\"appended\"";
358 out <<
" TimeStep=\"" << *timestep <<
"\"";
360 offsets.push_back(out.tellp());
361 out << std::string(std::numeric_limits<std::uint64_t>::digits10 + 2,
' ');
367 template <
class GV,
class DC>
368 void VtkUnstructuredGridWriter<GV,DC>
369 ::writeGridAppended (std::ofstream& out, std::vector<std::uint64_t>& blocks)
const
373 Vtk::mapDataTypes<std::is_floating_point, std::is_integral>(datatype_, headertype_,
374 [&](
auto f,
auto h) {
375 using F =
typename decltype(f)::type;
376 using H =
typename decltype(h)::type;
379 blocks.push_back(this->
template writeValuesAppended<H>(out, dataCollector_->template points<F>()));
382 auto cells = dataCollector_->cells();
383 blocks.push_back(this->
template writeValuesAppended<H>(out, cells.connectivity));
384 blocks.push_back(this->
template writeValuesAppended<H>(out, cells.offsets));
385 blocks.push_back(this->
template writeValuesAppended<H>(out, cells.types));
388 auto ids = dataCollector_->pointIds();
390 blocks.push_back(this->
template writeValuesAppended<H>(out, ids));
std::string to_string(Vtk::FormatTypes type)
Definition: types.cc:12
constexpr bool is_a(E a, Integer b)
Definition: enum.hh:12