Dune Core Modules (2.10.0)

common.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5
6#ifndef DUNE_GRID_IO_FILE_VTK_COMMON_HH
7#define DUNE_GRID_IO_FILE_VTK_COMMON_HH
8
9#include <limits>
10#include <sstream>
11#include <string>
12#include <cstdint>
13
15#include <dune/geometry/type.hh>
17
25namespace Dune
26{
29
30 namespace VTK {
31
33 //
34 // VTKOptions
35 //
36
38
52 // //! Output to the file is compressed inline binary.
53 // binarycompressed,
54 // //! Output is compressed and appended to the file.
55 // compressedappended
56 };
58
67 enum DataMode {
69
75
82 };
83
85 //
86 // PrintType
87 //
88
90
94 template<typename T>
95 struct PrintType {
97 typedef T Type;
98 };
99
100 template<>
101 struct PrintType<unsigned char> {
102 typedef unsigned Type;
103 };
104
105 template<>
106 struct PrintType<signed char> {
107 typedef int Type;
108 };
109
110 template<>
111 struct PrintType<char> {
112 typedef std::conditional<std::numeric_limits<char>::is_signed,
113 int, unsigned>::type
114 Type;
115 };
116
118 //
119 // VTK::GeometryType related stuff
120 //
121
123
133 vertex = 1,
134 line = 3,
135 triangle = 5,
136 polygon = 7,
137 quadrilateral = 9,
138 tetrahedron = 10,
139 hexahedron = 12,
140 prism = 13,
141 pyramid = 14,
142 polyhedron = 42
143 };
144
146
152 {
153 if (t.isVertex()) return vertex;
154 if (t.isLine()) return line;
155 if (t.isTriangle()) return triangle;
156 if (t.isQuadrilateral()) return quadrilateral;
157 if (t.isTetrahedron()) return tetrahedron;
158 if (t.isPyramid()) return pyramid;
159 if (t.isPrism()) return prism;
160 if (t.isHexahedron()) return hexahedron;
161
162 if (t.isNone() )
163 {
164 if( t.dim() == 2 ) return polygon;
165 if( t.dim() == 3 ) return polyhedron;
166 }
167
168 DUNE_THROW(IOError,"VTKWriter: unsupported GeometryType " << t);
169 }
170
172 //
173 // Functions for transforming the index of a corner inside an entity
174 // between Dune and VTK
175 //
176
178
186 inline int renumber(const Dune::GeometryType &t, int i)
187 {
188 static const int quadRenumbering[4] = {0,1,3,2};
189 static const int cubeRenumbering[8] = {0,1,3,2,4,5,7,6};
190 static const int prismRenumbering[6] = {0,2,1,3,5,4};
191 static const int pyramidRenumbering[5] = {0,1,3,2,4};
192
193 if (t.isQuadrilateral()) return quadRenumbering[i];
194 if (t.isPyramid()) return pyramidRenumbering[i];
195 if (t.isPrism()) return prismRenumbering[i];
196 if (t.isHexahedron()) return cubeRenumbering[i];
197
198 return i;
199 }
200
202
216 template<typename T>
217 int renumber(const T& t, int i)
218 {
219 return renumber(t.type(), i);
220 }
221
223 //
224 // Determine Endianness
225 //
226
228
232 inline std::string getEndiannessString()
233 {
234 short i = 1;
235 if (reinterpret_cast<char*>(&i)[1] == 1)
236 return "BigEndian";
237 else
238 return "LittleEndian";
239 }
240
242 //
243 // which type of vtkfile to write
244 //
245
247
252 enum FileType {
257 };
258
259
261 //
262 // which precision to use when writing out data
263 //
264
266
271 enum class Precision {
272 int32,
273 uint8,
274 uint32,
275 float32,
276 float64
277 };
278
280 inline std::string toString(Precision p)
281 {
282 switch(p)
283 {
284 case Precision::float32:
285 return "Float32";
286 case Precision::float64:
287 return "Float64";
288 case Precision::uint32:
289 return "UInt32";
290 case Precision::uint8:
291 return "UInt8";
292 case Precision::int32:
293 return "Int32";
294 default:
295 DUNE_THROW(Dune::NotImplemented, "Unknown precision type");
296 }
297 }
298
300 inline std::size_t typeSize(Precision p)
301 {
302 switch(p)
303 {
304 case Precision::float32:
305 return sizeof(float);
306 case Precision::float64:
307 return sizeof(double);
308 case Precision::uint32:
309 return sizeof(std::uint32_t);
310 case Precision::uint8:
311 return sizeof(std::uint8_t);
312 case Precision::int32:
313 return sizeof(std::int32_t);
314 default:
315 DUNE_THROW(Dune::NotImplemented, "Unknown precision type");
316 }
317 }
318
320
328 {
329
330 public:
331
333 enum class Type {
336 scalar,
338 vector,
340 tensor
341 };
342
344 FieldInfo(std::string name, Type type, std::size_t size, Precision prec = Precision::float32)
345 : _name(name)
346 , _type(type)
347 , _size(size)
348 , _prec(prec)
349 {}
350
352 std::string name() const
353 {
354 return _name;
355 }
356
358 Type type() const
359 {
360 return _type;
361 }
362
364 std::size_t size() const
365 {
366 return _size;
367 }
368
371 {
372 return _prec;
373 }
374
375 private:
376
377 std::string _name;
378 Type _type;
379 std::size_t _size;
380 Precision _prec;
381
382 };
383
384
385 } // namespace VTK
386
388
389} // namespace Dune
390
391#endif // DUNE_GRID_IO_FILE_VTK_COMMON_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
constexpr bool isPyramid() const
Return true if entity is a pyramid.
Definition: type.hh:304
constexpr bool isTetrahedron() const
Return true if entity is a tetrahedron.
Definition: type.hh:299
constexpr bool isPrism() const
Return true if entity is a prism.
Definition: type.hh:309
constexpr bool isVertex() const
Return true if entity is a vertex.
Definition: type.hh:279
constexpr unsigned int dim() const
Return dimension of the type.
Definition: type.hh:360
constexpr bool isTriangle() const
Return true if entity is a triangle.
Definition: type.hh:289
constexpr bool isLine() const
Return true if entity is a line segment.
Definition: type.hh:284
constexpr bool isQuadrilateral() const
Return true if entity is a quadrilateral.
Definition: type.hh:294
constexpr bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:355
constexpr bool isHexahedron() const
Return true if entity is a hexahedron.
Definition: type.hh:314
Default exception class for I/O errors.
Definition: exceptions.hh:231
Default exception for dummy implementations.
Definition: exceptions.hh:263
Descriptor struct for VTK fields.
Definition: common.hh:328
std::size_t size() const
The number of components in the data field.
Definition: common.hh:364
Precision precision() const
The precision used for the output of the data field.
Definition: common.hh:370
Type
VTK data type.
Definition: common.hh:333
FieldInfo(std::string name, Type type, std::size_t size, Precision prec=Precision::float32)
Create a FieldInfo instance with the given name, type and size.
Definition: common.hh:344
Type type() const
The type of the data field.
Definition: common.hh:358
std::string name() const
The name of the data field.
Definition: common.hh:352
A few common exception classes.
Traits for type conversions and type information.
Precision
which precision to use when writing out data to vtk files
Definition: common.hh:271
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:43
@ ascii
Output to the file is in ascii.
Definition: common.hh:45
@ appendedraw
Output is to the file is appended raw binary.
Definition: common.hh:49
@ appendedbase64
Output is to the file is appended base64 binary.
Definition: common.hh:51
@ base64
Output to the file is inline base64 binary.
Definition: common.hh:47
int renumber(const Dune::GeometryType &t, int i)
renumber VTK <-> Dune
Definition: common.hh:186
FileType
which type of VTK file to write
Definition: common.hh:252
@ polyData
for .vtp files (PolyData)
Definition: common.hh:254
@ unstructuredGrid
for .vtu files (UnstructuredGrid)
Definition: common.hh:256
std::string toString(Precision p)
map precision to VTK type name
Definition: common.hh:280
DataMode
Whether to produce conforming or non-conforming output.
Definition: common.hh:67
@ conforming
Output conforming data.
Definition: common.hh:73
@ nonconforming
Output non-conforming data.
Definition: common.hh:81
std::size_t typeSize(Precision p)
map precision to byte size
Definition: common.hh:300
GeometryType geometryType(const Dune::GeometryType &t)
mapping from GeometryType to VTKGeometryType
Definition: common.hh:151
std::string getEndiannessString()
determine endianness of this C++ implementation
Definition: common.hh:232
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
determine a type to safely put another type into a stream
Definition: common.hh:95
T Type
type to convert T to before putting it into a stream with <<
Definition: common.hh:97
A unique label for each type of element that can occur in a grid.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 27, 23:30, 2024)