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