dune-vtk 2.12
Loading...
Searching...
No Matches
vtkwriterbase.impl.hh
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <iomanip>
5#include <iostream>
6#include <iterator>
7#include <fstream>
8#include <limits>
9#include <sstream>
10#include <string>
11
12#if HAVE_VTK_ZLIB
13#include <zlib.h>
14#endif
15
17#include <dune/geometry/type.hh>
18
23
24namespace Dune::Vtk {
25
26template <class GV, class DC>
27std::string VtkWriterBase<GV,DC>
28 ::write (std::string const& fn, std::optional<std::string> dir) const
29{
30 dataCollector_->update();
31
32 auto p = Vtk::Path(fn);
33 auto name = p.stem();
34 p.removeFilename();
35
36 Vtk::Path fn_dir = p;
37 Vtk::Path data_dir = dir ? Vtk::Path(*dir) : fn_dir;
38 Vtk::Path rel_dir = Vtk::relative(data_dir, fn_dir);
39
40 std::string serial_fn = data_dir.string() + '/' + name.string();
41 std::string parallel_fn = fn_dir.string() + '/' + name.string();
42 std::string rel_fn = rel_dir.string() + '/' + name.string();
43
44 if (comm().size() > 1)
45 serial_fn += "_p" + std::to_string(comm().rank());
46
47 std::string outputFilename;
48
49 { // write serial file
50 outputFilename = serial_fn + "." + fileExtension();
51 std::ofstream serial_out(outputFilename, std::ios_base::ate | std::ios::binary);
52 assert(serial_out.is_open());
53
54 serial_out.imbue(std::locale::classic());
55 serial_out << std::setprecision(datatype_ == Vtk::DataTypes::FLOAT32
58
59 writeSerialFile(serial_out);
60 }
61
62 if (comm().size() > 1 && comm().rank() == 0) {
63 // write parallel filee
64 outputFilename = parallel_fn + ".p" + fileExtension();
65 std::ofstream parallel_out(outputFilename, std::ios_base::ate | std::ios::binary);
66 assert(parallel_out.is_open());
67
68 parallel_out.imbue(std::locale::classic());
69 parallel_out << std::setprecision(datatype_ == Vtk::DataTypes::FLOAT32
72
73 writeParallelFile(parallel_out, rel_fn, comm().size());
74 }
75
76 return outputFilename;
77}
78
79
80template <class GV, class DC>
83 VtkFunction const& fct, PositionTypes type,
84 std::optional<std::size_t> timestep) const
85{
86 out << "<DataArray Name=\"" << fct.name() << "\" type=\"" << to_string(fct.dataType()) << "\""
87 << " NumberOfComponents=\"" << fct.numComponents() << "\" format=\"" << (format_ == Vtk::FormatTypes::ASCII ? "ascii\"" : "appended\"");
88 if (timestep)
89 out << " TimeStep=\"" << *timestep << "\"";
90
91 if (format_ == Vtk::FormatTypes::ASCII) {
92 out << ">\n";
93 if (type == POINT_DATA)
94 writeValuesAscii(out, dataCollector_->template pointData<double>(fct));
95 else
96 writeValuesAscii(out, dataCollector_->template cellData<double>(fct));
97 out << "</DataArray>\n";
98 } else {
99 out << " offset=";
100 offsets.push_back(out.tellp());
102 out << "/>\n";
103 }
105
106
107template <class GV, class DC>
110 std::optional<std::size_t> timestep) const
111{
112 out << "<DataArray type=\"" << to_string(datatype_) << "\""
113 << " NumberOfComponents=\"3\" format=\"" << (format_ == Vtk::FormatTypes::ASCII ? "ascii\"" : "appended\"");
114 if (timestep)
115 out << " TimeStep=\"" << *timestep << "\"";
116
117 if (format_ == Vtk::FormatTypes::ASCII) {
118 out << ">\n";
119 writeValuesAscii(out, dataCollector_->template points<double>());
120 out << "</DataArray>\n";
121 } else {
122 out << " offset=";
123 offsets.push_back(out.tellp());
125 out << "/>\n";
126 }
127}
128
129
130template <class GV, class DC>
133{
134 for (auto const& v : pointData_) {
135 Vtk::mapDataTypes<std::is_floating_point, std::is_integral>(v.dataType(), headertype_,
136 [&](auto f, auto h) {
137 using F = typename decltype(f)::type;
138 using H = typename decltype(h)::type;
139 blocks.push_back(this->template writeValuesAppended<H>(out, dataCollector_->template pointData<F>(v)));
140 });
141 }
142
143 for (auto const& v : cellData_) {
144 Vtk::mapDataTypes<std::is_floating_point, std::is_integral>(v.dataType(), headertype_,
145 [&](auto f, auto h) {
146 using F = typename decltype(f)::type;
147 using H = typename decltype(h)::type;
148 blocks.push_back(this->template writeValuesAppended<H>(out, dataCollector_->template cellData<F>(v)));
149 });
150 }
151}
152
153
154template <class GV, class DC>
157{
158 if (is_a(format_, Vtk::FormatTypes::APPENDED)) {
159 out << "<AppendedData encoding=\"raw\">\n_";
161 writeGridAppended(out, blocks);
162 writeDataAppended(out, blocks);
163 out << "</AppendedData>\n";
164 pos_type appended_pos = out.tellp();
165
166 pos_type offset = 0;
167 for (std::size_t i = 0; i < offsets.size(); ++i) {
168 out.seekp(offsets[i]);
169 out << '"' << offset << '"';
170 offset += pos_type(blocks[i]);
171 }
172
173 out.seekp(appended_pos);
174 }
175}
176
177
178namespace Impl {
179
180 template <class T, std::enable_if_t<(sizeof(T)>1), int> = 0>
181 inline T const& printable (T const& t) { return t; }
182
183 inline std::int16_t printable (std::int8_t c) { return std::int16_t(c); }
184 inline std::uint16_t printable (std::uint8_t c) { return std::uint16_t(c); }
185
186} // end namespace Impl
187
188
189template <class GV, class DC>
190 template <class T>
191void VtkWriterBase<GV,DC>
192 ::writeValuesAscii (std::ofstream& out, std::vector<T> const& values) const
193{
194 assert(is_a(format_, Vtk::FormatTypes::ASCII) && "Function should by called only in ascii mode!\n");
195 std::size_t i = 0;
196 for (auto const& v : values)
197 out << Impl::printable(v) << (++i % 6 != 0 ? ' ' : '\n');
198 if (i % 6 != 0)
199 out << '\n';
200}
201
202template <class GV, class DC>
204 ::writeHeader (std::ofstream& out, std::string const& type) const
205{
206 out << "<VTKFile"
207 << " type=\"" << type << "\""
208 << " version=\"1.0\""
209 << " header_type=\"" << to_string(headertype_) << "\"";
210 if (format_ != Vtk::FormatTypes::ASCII)
211 out << " byte_order=\"" << getEndian() << "\"";
212 if (compressor_ != Vtk::CompressorTypes::NONE)
213 out << " compressor=\"" << to_string(compressor_) << "\"";
214 out << ">\n";
215}
216
217
218namespace Impl {
219
220template <class T>
221std::uint64_t writeValuesToBuffer (std::size_t max_num_values, unsigned char* buffer,
222 std::vector<T> const& vec, std::size_t shift)
223{
224 std::size_t num_values = std::min(max_num_values, vec.size()-shift);
225 std::uint64_t bs = num_values*sizeof(T);
226 std::memcpy(buffer, (unsigned char*)(vec.data()+shift), std::size_t(bs));
227 return bs;
228}
229
230inline std::uint64_t compressBuffer_zlib ([[maybe_unused]] unsigned char const* buffer,
231 [[maybe_unused]] unsigned char* buffer_out,
232 [[maybe_unused]] std::uint64_t bs,
233 [[maybe_unused]] std::uint64_t cbs,
234 [[maybe_unused]] int level)
235{
236#if HAVE_VTK_ZLIB
237 uLongf uncompressed_space = uLongf(bs);
238 uLongf compressed_space = uLongf(cbs);
239
240 Bytef* out = reinterpret_cast<Bytef*>(buffer_out);
241 Bytef const* in = reinterpret_cast<Bytef const*>(buffer);
242
243 if (compress2(out, &compressed_space, in, uncompressed_space, level) != Z_OK) {
244 std::cerr << "Zlib error while compressing data.\n";
245 std::abort();
246 }
248 return compressed_space;
249#else
250 std::cerr << "Can not call writeCompressed without compression enabled!\n";
251 std::abort();
252 return 0;
253#endif
255
256inline std::uint64_t compressBuffer_lz4 (unsigned char const* /* buffer */, unsigned char* /* buffer_out */,
257 std::uint64_t /* bs */, std::uint64_t /* cbs */, int /* level */)
258{
259#if HAVE_VTK_LZ4
260 std::cerr << "LZ4 Compression not yet implemented" << std::endl;
261 std::abort();
262 return 0;
263#else
264 std::cerr << "LZ4 Compression not supported. Provide the LZ4 package to CMake." << std::endl;
265 std::abort();
266 return 0;
267#endif
268}
270inline std::uint64_t compressBuffer_lzma (unsigned char const* /* buffer */, unsigned char* /* buffer_out */,
271 std::uint64_t /* bs */, std::uint64_t /* cbs */, int /* level */)
272{
273#if HAVE_VTK_LZMA
274 std::cerr << "LZMA Compression not yet implemented" << std::endl;
275 std::abort();
276 return 0;
277#else
278 std::cerr << "LZMA Compression not supported. Provide the LZMA package to CMake." << std::endl;
279 std::abort();
280 return 0;
281#endif
282}
283
284} // end namespace Impl
285
286template <class GV, class DC>
287 template <class HeaderType, class FloatType>
288std::uint64_t VtkWriterBase<GV,DC>
289 ::writeValuesAppended (std::ofstream& out, std::vector<FloatType> const& values) const
290{
291 assert(is_a(format_, Vtk::FormatTypes::APPENDED) && "Function should by called only in appended mode!\n");
292 pos_type begin_pos = out.tellp();
293
294 HeaderType size = values.size() * sizeof(FloatType);
295
296 HeaderType num_full_blocks = size / block_size;
297 HeaderType last_block_size = size % block_size;
298 HeaderType num_blocks = num_full_blocks + (last_block_size > 0 ? 1 : 0);
299
300 // write block-size(s)
301 HeaderType zero = 0;
302 if (compressor_ != Vtk::CompressorTypes::NONE) {
303 out.write((char*)&num_blocks, sizeof(HeaderType));
304 out.write((char*)&block_size, sizeof(HeaderType));
305 out.write((char*)&last_block_size, sizeof(HeaderType));
306 for (HeaderType i = 0; i < num_blocks; ++i)
307 out.write((char*)&zero, sizeof(HeaderType));
308 } else {
309 out.write((char*)&size, sizeof(HeaderType));
310 }
311
312 HeaderType compressed_block_size = block_size + (block_size + 999)/1000 + 12;
313 std::vector<unsigned char> buffer(block_size);
315 if (compressor_ != Vtk::CompressorTypes::NONE)
316 buffer_out.resize(std::size_t(compressed_block_size));
317
318 std::size_t num_values = block_size / sizeof(FloatType);
319
320 std::vector<HeaderType> cbs(std::size_t(num_blocks), 0); // compressed block sizes
321 for (std::size_t i = 0; i < std::size_t(num_blocks); ++i) {
322 HeaderType bs = Impl::writeValuesToBuffer<FloatType>(num_values, buffer.data(), values, i*num_values);
323
324 switch (compressor_) {
326 out.write((char*)buffer.data(), bs);
327 break;
329 cbs[i] = Impl::compressBuffer_zlib(buffer.data(), buffer_out.data(), bs, compressed_block_size, compression_level);
330 out.write((char*)buffer_out.data(), cbs[i]);
331 break;
333 cbs[i] = Impl::compressBuffer_zlib(buffer.data(), buffer_out.data(), bs, compressed_block_size, compression_level);
334 out.write((char*)buffer_out.data(), cbs[i]);
335 break;
337 cbs[i] = Impl::compressBuffer_zlib(buffer.data(), buffer_out.data(), bs, compressed_block_size, compression_level);
338 out.write((char*)buffer_out.data(), cbs[i]);
339 break;
340 }
341 }
342
343 pos_type end_pos = out.tellp();
344 if (compressor_ != Vtk::CompressorTypes::NONE) {
345 out.seekp(begin_pos + std::streamoff(3*sizeof(HeaderType)));
346 out.write((char*)cbs.data(), std::streamsize(num_blocks*sizeof(HeaderType)));
347 out.seekp(end_pos);
348 }
349
350 return std::uint64_t(end_pos - begin_pos);
351}
352
353
354template <class GV, class DC>
357{
358 auto scalar = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.rangeType() == Vtk::RangeTypes::SCALAR; });
359 auto vector = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.rangeType() == Vtk::RangeTypes::VECTOR; });
360 auto tensor = std::find_if(data.begin(), data.end(), [](auto const& v) { return v.rangeType() == Vtk::RangeTypes::TENSOR; });
361 return (scalar != data.end() ? " Scalars=\"" + scalar->name() + "\"" : "")
362 + (vector != data.end() ? " Vectors=\"" + vector->name() + "\"" : "")
363 + (tensor != data.end() ? " Tensors=\"" + tensor->name() + "\"" : "");
364}
365
366} // end namespace Dune::Vtk
Macro for wrapping error checks and throwing exceptions.
Definition writer.hh:16
Path relative(Path const &a, Path const &b)
Find the path of a relative to directory of b
Definition filesystem.cc:172
constexpr bool is_a(E a, Integer b)
Definition enum.hh:9
std::string to_string(Vtk::FormatTypes)
Definition types.cc:11
const char * name()
int size() const
A Vtk::GridFunction is a function-like object that can be bound to a grid element an that provides an...
Definition gridfunction.hh:85
int numComponents() const
Return the number of components of the Range as it is written to the file.
Definition gridfunction.hh:178
std::string const & name() const
Return a name associated with the function.
Definition gridfunction.hh:166
Vtk::DataTypes dataType() const
Return the VTK Datatype associated with the functions range type.
Definition gridfunction.hh:204
Definition filesystem.hh:13
std::string string() const
Return the path as string.
Definition filesystem.cc:26
Base class for file writers for the Vtk XML file formats.
Definition vtkwriterbase.hh:26
PositionTypes
Definition vtkwriterbase.hh:41
typename std::ostream::pos_type pos_type
Definition vtkwriterbase.hh:39
T abort(T... args)
T classic(T... args)
T data(T... args)
T endl(T... args)
T find_if(T... args)
T imbue(T... args)
T is_open(T... args)
T memcpy(T... args)
T min(T... args)
T push_back(T... args)
T resize(T... args)
T seekp(T... args)
T setprecision(T... args)
T size(T... args)
T tellp(T... args)
T to_string(T... args)
T write(T... args)