dune-vtk 2.12
Loading...
Searching...
No Matches
vtkwriterbase.hh
Go to the documentation of this file.
1#pragma once
2
3#include <iosfwd>
4#include <map>
5#include <memory>
6#include <optional>
7#include <string>
8#include <type_traits>
9#include <vector>
10
13#include <dune/vtk/types.hh>
15
16namespace Dune::Vtk
17{
19
23 template <class GV, class DC>
25 : public Vtk::FileWriter
26 {
27 template <class> friend class TimeseriesWriter;
28 template <class> friend class PvdWriter;
29
30 static_assert(not std::is_reference_v<DC> and not std::is_const_v<DC>, "Passing const or reference types as DataCollector is not supported");
31
32 public:
33 using GridView = GV;
34 using DataCollector = DC;
35
36 protected:
38 using Domain = typename GridView::template Codim<0>::Entity::Geometry::GlobalCoordinate;
39 using pos_type = typename std::ostream::pos_type;
40
45
46 public:
48
61 VtkWriterBase (GridView const& gridView,
65 : VtkWriterBase(std::make_shared<DataCollector>(gridView), format, datatype, headertype)
66 {}
67
73 : VtkWriterBase(stackobject_to_shared_ptr(dataCollector), format, datatype, headertype)
74 {}
75
77 VtkWriterBase (DataCollector&& dataCollector,
81 : VtkWriterBase(std::make_shared<DataCollector>(std::move(dataCollector)), format, datatype, headertype)
82 {}
83
89 : dataCollector_(std::move(dataCollector))
90 {
91 setFormat(format);
92 setDatatype(datatype);
93 setHeadertype(headertype);
94 }
95
96
98
104 virtual std::string write (std::string const& fn, std::optional<std::string> dir = {}) const override;
105
107
117 template <class Function>
119 {
120 pointData_.emplace_back(std::forward<Function>(fct), info);
121 return *this;
122 }
123
124 template <class Function,
127 {
128 pointData_.emplace_back(std::forward<Function>(fct),
130 return *this;
131 }
132
134 {
136 Vtk::FieldInfo{fct->name(), fct->ncomps(), dataTypeOf(fct->precision()), rangeTypeOf(fct->ncomps())});
137 return *this;
138 }
139
141 {
142 pointData_.emplace_back(fct);
143 return *this;
144 }
145
147
156 template <class Function>
158 {
159 cellData_.emplace_back(std::forward<Function>(fct), info);
160 return *this;
161 }
162
163 template <class Function,
165 VtkWriterBase& addCellData (Function&& fct, std::string name, int size = dimRange<R>())
166 {
167 cellData_.emplace_back(std::forward<Function>(fct),
169 return *this;
170 }
171
173 {
174 cellData_.emplace_back(VTKFunctionWrapper<GridView>(fct),
175 Vtk::FieldInfo{fct->name(), fct->ncomps(), dataTypeOf(fct->precision()), rangeTypeOf(fct->ncomps())});
176 return *this;
177 }
178
180 {
181 cellData_.emplace_back(fct);
182 return *this;
183 }
184
185 // Sets the VTK file format
187 {
188 format_ = format;
189
192#if HAVE_VTK_ZLIB
194#else
195 std::cout << "Dune is compiled without ZLIB. Falling back to BINARY VTK output!\n";
197#endif
198 } else {
200 }
201
202 }
203
206 {
207 datatype_ = datatype;
208 }
209
212 {
213 headertype_ = datatype;
214 }
215
218 void setCompressor (Vtk::CompressorTypes compressor, int level = -1)
219 {
220 compressor_ = compressor;
221 compression_level = level;
222 VTK_ASSERT(level >= -1 && level <= 9);
223
226 }
227
228 private:
230 virtual void writeSerialFile (std::ofstream& out) const = 0;
231
235 virtual void writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const = 0;
236
238 virtual std::string fileExtension () const = 0;
239
241 virtual void writeGridAppended (std::ofstream& out, std::vector<std::uint64_t>& blocks) const = 0;
242
243 protected:
244 // Write the point or cell values given by the grid function `fct` to the
245 // output stream `out`. In case of binary format, append the streampos of XML
246 // attributes "offset" to the vector `offsets`.
247 void writeData (std::ofstream& out,
248 std::vector<pos_type>& offsets,
249 VtkFunction const& fct,
250 PositionTypes type,
251 std::optional<std::size_t> timestep = {}) const;
252
253 // Write point-data and cell-data in raw/compressed format to output stream
255
256 // Write the coordinates of the vertices to the output stream `out`. In case
257 // of binary format, appends the streampos of XML attributes "offset" to the
258 // vector `offsets`.
259 void writePoints (std::ofstream& out,
260 std::vector<pos_type>& offsets,
261 std::optional<std::size_t> timestep = {}) const;
262
263 // Write Appended section and fill-in offset values to XML attributes
264 void writeAppended (std::ofstream& out, std::vector<pos_type> const& offsets) const;
265
266 // Write the `values` in blocks (possibly compressed) to the output
267 // stream `out`. Return the written block size.
268 template <class HeaderType, class FloatType>
270
271 // Write the `values` in a space and newline separated list of ascii representations.
272 // The precision is controlled by the datatype and numerical_limits::digits10.
273 template <class T>
274 void writeValuesAscii (std::ofstream& out, std::vector<T> const& values) const;
275
276 // Write the XML file header of a VTK file `<VTKFile ...>`
277 void writeHeader (std::ofstream& out, std::string const& type) const;
278
281
282 // Returns endianness
284 {
285 short i = 1;
286 return (reinterpret_cast<char*>(&i)[1] == 1 ? "BigEndian" : "LittleEndian");
287 }
288
289 // provide accessor to \ref fileExtension virtual method
291 {
292 return fileExtension();
293 }
294
295 // Returns the VTK file format initialized in the constructor
297 {
298 return format_;
299 }
300
301 // Returns the global datatype used for coordinates and other global float values
303 {
304 return datatype_;
305 }
306
307 // Return the MPI communicator of the grid.
308 auto comm () const
309 {
310 return dataCollector_->comm();
311 }
312
313 protected:
315
320
321 // attached data
324
325 std::size_t const block_size = 1024*32;
326 int compression_level = -1; // in [0,9], -1 ... use default value
327 };
328
329
330 template <class Writer>
332 {
333 template <class GV, class DC>
335 static std::uint8_t test(...); // fall-back overload
336
337 static constexpr bool value = sizeof(test(std::declval<Writer>())) > sizeof(std::uint8_t);
338 };
339
340} // end namespace Dune::Vtk
341
342#include "vtkwriterbase.impl.hh"
#define VTK_ASSERT(cond)
check if condition cond holds; otherwise, throw a VtkError.
Definition errors.hh:29
STL namespace.
Definition writer.hh:16
FormatTypes
Type used for representing the output format.
Definition types.hh:19
CompressorTypes
Definition types.hh:148
Vtk::DataTypes dataTypeOf()
Definition types.hh:78
DataTypes
Definition types.hh:51
Vtk::RangeTypes rangeTypeOf(Dune::VTK::FieldInfo::Type t)
Definition types.cc:55
const char * name()
int size() const
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Definition filewriter.hh:9
Definition function.hh:18
A Vtk::GridFunction is a function-like object that can be bound to a grid element an that provides an...
Definition gridfunction.hh:85
Type erasure for Legacy VTKFunction.
Definition vtkfunctionwrapper.hh:12
File-Writer for ParaView .pvd files.
Definition pvdwriter.hh:20
File-Writer for Vtk timeseries .vtu files.
Definition timeserieswriter.hh:25
Definition types.hh:248
std::string const & name() const
The name of the data field.
Definition types.hh:267
Base class for file writers for the Vtk XML file formats.
Definition vtkwriterbase.hh:26
std::string getEndian() const
Definition vtkwriterbase.hh:283
VtkWriterBase & addPointData(const Vtk::GridFunction< GridView > &fct)
Definition vtkwriterbase.hh:140
void writeData(std::ofstream &out, std::vector< pos_type > &offsets, VtkFunction const &fct, PositionTypes type, std::optional< std::size_t > timestep={}) const
Definition vtkwriterbase.impl.hh:82
void setHeadertype(Vtk::DataTypes datatype)
Sets the integer type used in binary data headers.
Definition vtkwriterbase.hh:211
VtkWriterBase & addPointData(Function &&fct, std::string name, int size=dimRange< R >())
Definition vtkwriterbase.hh:126
std::vector< VtkFunction > pointData_
Definition vtkwriterbase.hh:322
VtkWriterBase & addPointData(const std::shared_ptr< VTKFunction< GridView > const > &fct)
Definition vtkwriterbase.hh:133
DC DataCollector
Definition vtkwriterbase.hh:34
void writeHeader(std::ofstream &out, std::string const &type) const
Definition vtkwriterbase.impl.hh:204
std::uint64_t writeValuesAppended(std::ofstream &out, std::vector< FloatType > const &values) const
Definition vtkwriterbase.impl.hh:289
Vtk::DataTypes datatype_
Definition vtkwriterbase.hh:317
Dune::Vtk::GridFunction< GridView > VtkFunction
Definition vtkwriterbase.hh:37
PositionTypes
Definition vtkwriterbase.hh:41
@ POINT_DATA
Definition vtkwriterbase.hh:42
@ CELL_DATA
Definition vtkwriterbase.hh:43
auto comm() const
Definition vtkwriterbase.hh:308
typename std::ostream::pos_type pos_type
Definition vtkwriterbase.hh:39
Vtk::FormatTypes format_
Definition vtkwriterbase.hh:316
std::vector< VtkFunction > cellData_
Definition vtkwriterbase.hh:323
void setDatatype(Vtk::DataTypes datatype)
Sets the global datatype used for coordinates and other global float values.
Definition vtkwriterbase.hh:205
void setCompressor(Vtk::CompressorTypes compressor, int level=-1)
Definition vtkwriterbase.hh:218
VtkWriterBase & addCellData(const Vtk::GridFunction< GridView > &fct)
Definition vtkwriterbase.hh:179
typename GridView::template Codim< 0 >::Entity::Geometry::GlobalCoordinate Domain
Definition vtkwriterbase.hh:38
VtkWriterBase & addCellData(const std::shared_ptr< VTKFunction< GridView > const > &fct)
Definition vtkwriterbase.hh:172
void writePoints(std::ofstream &out, std::vector< pos_type > &offsets, std::optional< std::size_t > timestep={}) const
Definition vtkwriterbase.impl.hh:109
VtkWriterBase & addPointData(Function &&fct, const Vtk::FieldInfo &info)
Attach point data to the writer.
Definition vtkwriterbase.hh:118
GV GridView
Definition vtkwriterbase.hh:33
VtkWriterBase(GridView const &gridView, Vtk::FormatTypes format=Vtk::FormatTypes::BINARY, Vtk::DataTypes datatype=Vtk::DataTypes::FLOAT32, Vtk::DataTypes headertype=Vtk::DataTypes::UINT32)
Constructor, passes the gridView to the DataCollector.
Definition vtkwriterbase.hh:61
std::size_t const block_size
Definition vtkwriterbase.hh:325
void setFormat(Vtk::FormatTypes format)
Definition vtkwriterbase.hh:186
virtual std::string write(std::string const &fn, std::optional< std::string > dir={}) const override
Write the attached data to the file.
Definition vtkwriterbase.impl.hh:28
Vtk::FormatTypes getFormat() const
Definition vtkwriterbase.hh:296
Vtk::DataTypes headertype_
Definition vtkwriterbase.hh:318
std::string getFileExtension() const
Definition vtkwriterbase.hh:290
void writeAppended(std::ofstream &out, std::vector< pos_type > const &offsets) const
Definition vtkwriterbase.impl.hh:156
Vtk::CompressorTypes compressor_
Definition vtkwriterbase.hh:319
int compression_level
Definition vtkwriterbase.hh:326
VtkWriterBase(DataCollector &&dataCollector, Vtk::FormatTypes format=Vtk::FormatTypes::BINARY, Vtk::DataTypes datatype=Vtk::DataTypes::FLOAT32, Vtk::DataTypes headertype=Vtk::DataTypes::UINT32)
Constructor, stores the passed DataCollector.
Definition vtkwriterbase.hh:77
void writeValuesAscii(std::ofstream &out, std::vector< T > const &values) const
Definition vtkwriterbase.impl.hh:192
VtkWriterBase(std::shared_ptr< DataCollector > dataCollector, Vtk::FormatTypes format=Vtk::FormatTypes::BINARY, Vtk::DataTypes datatype=Vtk::DataTypes::FLOAT32, Vtk::DataTypes headertype=Vtk::DataTypes::UINT32)
Constructor, stores the passed DataCollector.
Definition vtkwriterbase.hh:85
VtkWriterBase(DataCollector &dataCollector, Vtk::FormatTypes format=Vtk::FormatTypes::BINARY, Vtk::DataTypes datatype=Vtk::DataTypes::FLOAT32, Vtk::DataTypes headertype=Vtk::DataTypes::UINT32)
Constructor, wraps the passed DataCollector in a non-destroying shared_ptr.
Definition vtkwriterbase.hh:69
Vtk::DataTypes getDatatype() const
Definition vtkwriterbase.hh:302
VtkWriterBase & addCellData(Function &&fct, const Vtk::FieldInfo &info)
Attach cell data to the writer.
Definition vtkwriterbase.hh:157
void writeDataAppended(std::ofstream &out, std::vector< std::uint64_t > &blocks) const
Definition vtkwriterbase.impl.hh:132
std::string getNames(std::vector< VtkFunction > const &data) const
Return PointData/CellData attributes for the name of the first scalar/vector/tensor DataArray.
Definition vtkwriterbase.impl.hh:356
std::shared_ptr< DataCollector > dataCollector_
Definition vtkwriterbase.hh:314
VtkWriterBase & addCellData(Function &&fct, std::string name, int size=dimRange< R >())
Definition vtkwriterbase.hh:165
Definition vtkwriterbase.hh:332
static std::uint8_t test(...)
static std::uint16_t test(VtkWriterBase< GV, DC > const &)
static constexpr bool value
Definition vtkwriterbase.hh:337