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