dune-vtk 2.12
Loading...
Searching...
No Matches
imagedatawriter.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 ImageDataWriter<GV,DC>
23 ::writeSerialFile (std::ofstream& out) const
24{
25 std::vector<pos_type> offsets; // pos => offset
26 this->writeHeader(out, "ImageData");
27
28 auto const& wholeExtent = dataCollector_->wholeExtent();
29 auto const& origin = dataCollector_->origin();
30 auto const& spacing = dataCollector_->spacing();
31 out << "<ImageData"
32 << " WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\""
33 << " Origin=\"" << Vtk::join(origin.begin(), origin.end()) << "\""
34 << " Spacing=\"" << Vtk::join(spacing.begin(), spacing.end()) << "\""
35 << ">\n";
36
37 dataCollector_->writeLocalPiece([&out](auto const& extent) {
38 out << "<Piece Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\">\n";
39 });
40
41 // Write data associated with grid points
42 out << "<PointData" << this->getNames(pointData_) << ">\n";
43 for (auto const& v : pointData_)
44 this->writeData(out, offsets, v, Super::POINT_DATA);
45 out << "</PointData>\n";
46
47 // Write data associated with grid cells
48 out << "<CellData" << this->getNames(cellData_) << ">\n";
49 for (auto const& v : cellData_)
50 this->writeData(out, offsets, v, Super::CELL_DATA);
51 out << "</CellData>\n";
52
53 out << "</Piece>\n";
54 out << "</ImageData>\n";
55
56 this->writeAppended(out, offsets);
57 out << "</VTKFile>";
58}
59
60
61template <class GV, class DC>
62void ImageDataWriter<GV,DC>
63 ::writeParallelFile (std::ofstream& out, std::string const& pfilename, int /*size*/) const
64{
65 this->writeHeader(out, "PImageData");
66
67 auto const& wholeExtent = dataCollector_->wholeExtent();
68 auto const& origin = dataCollector_->origin();
69 auto const& spacing = dataCollector_->spacing();
70 out << "<PImageData"
71 << " GhostLevel=\"" << dataCollector_->ghostLevel() << "\""
72 << " WholeExtent=\"" << Vtk::join(wholeExtent.begin(), wholeExtent.end()) << "\""
73 << " Origin=\"" << Vtk::join(origin.begin(), origin.end()) << "\""
74 << " Spacing=\"" << Vtk::join(spacing.begin(), spacing.end()) << "\""
75 << ">\n";
76
77 // Write data associated with grid points
78 out << "<PPointData" << this->getNames(pointData_) << ">\n";
79 for (auto const& v : pointData_) {
80 out << "<PDataArray"
81 << " Name=\"" << v.name() << "\""
82 << " type=\"" << to_string(v.dataType()) << "\""
83 << " NumberOfComponents=\"" << v.numComponents() << "\""
84 << " />\n";
85 }
86 out << "</PPointData>\n";
87
88 // Write data associated with grid cells
89 out << "<PCellData" << this->getNames(cellData_) << ">\n";
90 for (auto const& v : cellData_) {
91 out << "<PDataArray"
92 << " Name=\"" << v.name() << "\""
93 << " type=\"" << to_string(v.dataType()) << "\""
94 << " NumberOfComponents=\"" << v.numComponents() << "\""
95 << " />\n";
96 }
97 out << "</PCellData>\n";
98
99 // Write piece file references
100 dataCollector_->writePieces([&out,pfilename,ext=this->fileExtension()](int p, auto const& extent, bool write_extent)
101 {
102 std::string piece_source = pfilename + "_p" + std::to_string(p) + "." + ext;
103 out << "<Piece Source=\"" << piece_source << "\"";
104 if (write_extent)
105 out << " Extent=\"" << Vtk::join(extent.begin(), extent.end()) << "\"";
106 out << " />\n";
107 });
108
109 out << "</PImageData>\n";
110 out << "</VTKFile>";
111}
112
113} // end namespace Dune::Vtk
Definition datacollector.hh:19
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 to_string(T... args)