MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
vtk.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_VTK
13#define MFEM_VTK
14
15#include <cstdint>
16
17#include "../fem/geom.hpp"
19
20namespace mfem
21{
22
23// Helpers for reading and writing VTK format
24
25/// @brief Helper class for converting between MFEM and VTK geometry types.
26///
27/// Note: The VTK element types defined are at: https://git.io/JvZLm
29{
30 /// @name VTK geometry types
31 ///@{
32 static const int POINT = 1;
33
34 /// @name Low-order (linear, straight-sided) VTK geometric types
35 ///@{
36 static const int SEGMENT = 3;
37 static const int TRIANGLE = 5;
38 static const int SQUARE = 9;
39 static const int TETRAHEDRON = 10;
40 static const int CUBE = 12;
41 static const int PRISM = 13;
42 static const int PYRAMID = 14;
43 ///@}
44
45 /// @name Legacy quadratic VTK geometric types
46 ///@{
47 static const int QUADRATIC_SEGMENT = 21;
48 static const int QUADRATIC_TRIANGLE = 22;
49 static const int BIQUADRATIC_SQUARE = 28;
50 static const int QUADRATIC_TETRAHEDRON = 24;
51 static const int TRIQUADRATIC_CUBE = 29;
52 static const int QUADRATIC_PRISM = 26;
53 static const int BIQUADRATIC_QUADRATIC_PRISM = 32;
54 static const int QUADRATIC_PYRAMID = 27;
55 ///@}
56
57 /// @name Arbitrary-order VTK geometric types
58 ///@{
59 static const int LAGRANGE_SEGMENT = 68;
60 static const int LAGRANGE_TRIANGLE = 69;
61 static const int LAGRANGE_SQUARE = 70;
62 static const int LAGRANGE_TETRAHEDRON = 71;
63 static const int LAGRANGE_CUBE = 72;
64 static const int LAGRANGE_PRISM = 73;
65 static const int LAGRANGE_PYRAMID = 74;
66 ///@}
67 ///@}
68
69 /// Permutation from MFEM's prism ordering to VTK's prism ordering.
70 static const int PrismMap[6];
71
72 /// @brief Permutation from MFEM's vertex ordering to VTK's vertex ordering.
73 /// @note If the MFEM and VTK orderings are the same, the vertex permutation
74 /// will be NULL.
76
77 /// Map from MFEM's Geometry::Type to linear VTK geometries.
78 static const int Map[Geometry::NUM_GEOMETRIES];
79 /// Map from MFEM's Geometry::Type to legacy quadratic VTK geometries/
81 /// Map from MFEM's Geometry::Type to arbitrary-order Lagrange VTK geometries
83
84 /// Given a VTK geometry type, return the corresponding MFEM Geometry::Type.
85 static Geometry::Type GetMFEMGeometry(int vtk_geom);
86 /// @brief Does the given VTK geometry type describe an arbitrary-order
87 /// Lagrange element?
88 static bool IsLagrange(int vtk_geom);
89 /// @brief Does the given VTK geometry type describe a legacy quadratic
90 /// element?
91 static bool IsQuadratic(int vtk_geom);
92 /// @brief For the given VTK geometry type and number of points, return the
93 /// order of the element.
94 static int GetOrder(int vtk_geom, int npoints);
95};
96
97/// Data array format for VTK and VTU files.
98enum class VTKFormat
99{
100 /// Data arrays will be written in ASCII format.
101 ASCII,
102 /// Data arrays will be written in binary format. Floating point numbers will
103 /// be output with 64 bits of precision.
104 BINARY,
105 /// Data arrays will be written in binary format. Floating point numbers will
106 /// be output with 32 bits of precision.
108};
109
110/// @brief Create the VTK element connectivity array for a given element
111/// geometry and refinement level.
112///
113/// The output array @a con will be such that, for the @a ith VTK node index,
114/// con[i] will contain the index of the corresponding node in MFEM ordering.
115void CreateVTKElementConnectivity(Array<int> &con, Geometry::Type geom,
116 int ref);
117
118/// @brief Outputs encoded binary data in the base 64 format needed by VTK.
119///
120/// The binary data will be base 64 encoded, and compressed if @a
121/// compression_level is not zero. The proper header will be prepended to the
122/// data.
123void WriteVTKEncodedCompressed(std::ostream &os, const void *bytes,
124 uint32_t nbytes, int compression_level);
125
126/// @brief Return the VTK node index of the barycentric point @a b in a
127/// triangle with refinement level @a ref.
128///
129/// The barycentric index @a b has three components, satisfying b[0] + b[1] +
130/// b[2] == ref.
131int BarycentricToVTKTriangle(int *b, int ref);
132
133/// Determine the byte order and return either "BigEndian" or "LittleEndian"
134const char *VTKByteOrder();
135
136/// @brief Write either ASCII data to the stream or binary data to the buffer
137/// depending on the given format.
138///
139/// If @a format is VTK::ASCII, write the canonical ASCII representation of @a
140/// val to the output stream. Subnormal floating point numbers are rounded to
141/// zero. Otherwise, append its raw binary data to the byte buffer @a buf.
142///
143/// Note that there are specializations for @a uint8_t (to write as a numeric
144/// value rather than a character), and for @a float and @a double values to use
145/// the precision specified by @a format.
146template <typename T>
147void WriteBinaryOrASCII(std::ostream &os, std::vector<char> &buf, const T &val,
148 const char *suffix, VTKFormat format)
149{
150 if (format == VTKFormat::ASCII) { os << val << suffix; }
151 else { bin_io::AppendBytes(buf, val); }
152}
153
154/// @brief Specialization of @ref WriteBinaryOrASCII for @a uint8_t to ensure
155/// ASCII output is numeric (rather than interpreting @a val as a character.)
156template <>
157void WriteBinaryOrASCII<uint8_t>(std::ostream &os, std::vector<char> &buf,
158 const uint8_t &val, const char *suffix,
159 VTKFormat format);
160
161/// @brief Specialization of @ref WriteBinaryOrASCII for @a double.
162///
163/// If @a format is equal to VTKFormat::BINARY32, @a val is converted to a @a
164/// float and written as 32 bits. Subnormals are rounded to zero in ASCII
165/// output.
166template <>
167void WriteBinaryOrASCII<double>(std::ostream &os, std::vector<char> &buf,
168 const double &val, const char *suffix,
169 VTKFormat format);
170
171/// @brief Specialization of @ref WriteBinaryOrASCII<T> for @a float.
172///
173/// If @a format is equal to VTKFormat::BINARY, @a val is converted to a @a
174/// double and written as 64 bits. Subnormals are rounded to zero in ASCII
175/// output.
176template <>
177void WriteBinaryOrASCII<float>(std::ostream &os, std::vector<char> &buf,
178 const float &val, const char *suffix,
179 VTKFormat format);
180
181/// @brief Encode in base 64 (and potentially compress) the given data, write it
182/// to the output stream (with a header) and clear the buffer.
183///
184/// @sa WriteVTKEncodedCompressed.
185void WriteBase64WithSizeAndClear(std::ostream &os, std::vector<char> &buf,
186 int compression_level);
187
188} // namespace mfem
189
190#endif
real_t b
Definition lissajous.cpp:42
void AppendBytes(std::vector< char > &vec, const T &val)
Append the binary representation of val to the byte buffer vec.
Definition binaryio.hpp:55
void WriteVTKEncodedCompressed(std::ostream &os, const void *bytes, uint32_t nbytes, int compression_level)
Outputs encoded binary data in the base 64 format needed by VTK.
Definition vtk.cpp:559
void WriteBinaryOrASCII< float >(std::ostream &os, std::vector< char > &buf, const float &val, const char *suffix, VTKFormat format)
Specialization of WriteBinaryOrASCII<T> for float.
Definition vtk.cpp:645
void WriteBase64WithSizeAndClear(std::ostream &os, std::vector< char > &buf, int compression_level)
Encode in base 64 (and potentially compress) the given data, write it to the output stream (with a he...
Definition vtk.cpp:654
void WriteBinaryOrASCII< double >(std::ostream &os, std::vector< char > &buf, const double &val, const char *suffix, VTKFormat format)
Specialization of WriteBinaryOrASCII for double.
Definition vtk.cpp:626
void WriteBinaryOrASCII< uint8_t >(std::ostream &os, std::vector< char > &buf, const uint8_t &val, const char *suffix, VTKFormat format)
Specialization of WriteBinaryOrASCII for uint8_t to ensure ASCII output is numeric (rather than inter...
Definition vtk.cpp:617
VTKFormat
Data array format for VTK and VTU files.
Definition vtk.hpp:99
@ ASCII
Data arrays will be written in ASCII format.
void WriteBinaryOrASCII(std::ostream &os, std::vector< char > &buf, const T &val, const char *suffix, VTKFormat format)
Write either ASCII data to the stream or binary data to the buffer depending on the given format.
Definition vtk.hpp:147
const char * VTKByteOrder()
Determine the byte order and return either "BigEndian" or "LittleEndian".
Definition vtk.cpp:602
void CreateVTKElementConnectivity(Array< int > &con, Geometry::Type geom, int ref)
Create the VTK element connectivity array for a given element geometry and refinement level.
Definition vtk.cpp:497
int BarycentricToVTKTriangle(int *b, int ref)
Return the VTK node index of the barycentric point b in a triangle with refinement level ref.
Definition vtk.cpp:161
Helper class for converting between MFEM and VTK geometry types.
Definition vtk.hpp:29
static bool IsQuadratic(int vtk_geom)
Does the given VTK geometry type describe a legacy quadratic element?
Definition vtk.cpp:90
static const int HighOrderMap[Geometry::NUM_GEOMETRIES]
Map from MFEM's Geometry::Type to arbitrary-order Lagrange VTK geometries.
Definition vtk.hpp:82
static const int BIQUADRATIC_QUADRATIC_PRISM
Definition vtk.hpp:53
static const int LAGRANGE_PRISM
Definition vtk.hpp:64
static const int QuadraticMap[Geometry::NUM_GEOMETRIES]
Map from MFEM's Geometry::Type to legacy quadratic VTK geometries/.
Definition vtk.hpp:80
static const int * VertexPermutation[Geometry::NUM_GEOMETRIES]
Permutation from MFEM's vertex ordering to VTK's vertex ordering.
Definition vtk.hpp:75
static const int LAGRANGE_TETRAHEDRON
Definition vtk.hpp:62
static const int LAGRANGE_TRIANGLE
Definition vtk.hpp:60
static const int QUADRATIC_TRIANGLE
Definition vtk.hpp:48
static const int PrismMap[6]
Permutation from MFEM's prism ordering to VTK's prism ordering.
Definition vtk.hpp:70
static const int SQUARE
Definition vtk.hpp:38
static const int CUBE
Definition vtk.hpp:40
static const int TRIANGLE
Definition vtk.hpp:37
static const int TETRAHEDRON
Definition vtk.hpp:39
static const int LAGRANGE_SQUARE
Definition vtk.hpp:61
static bool IsLagrange(int vtk_geom)
Does the given VTK geometry type describe an arbitrary-order Lagrange element?
Definition vtk.cpp:85
static const int POINT
Definition vtk.hpp:32
static const int PRISM
Definition vtk.hpp:41
static const int QUADRATIC_SEGMENT
Definition vtk.hpp:47
static const int SEGMENT
Definition vtk.hpp:36
static const int BIQUADRATIC_SQUARE
Definition vtk.hpp:49
static const int LAGRANGE_CUBE
Definition vtk.hpp:63
static Geometry::Type GetMFEMGeometry(int vtk_geom)
Given a VTK geometry type, return the corresponding MFEM Geometry::Type.
Definition vtk.cpp:46
static const int QUADRATIC_PRISM
Definition vtk.hpp:52
static const int LAGRANGE_SEGMENT
Definition vtk.hpp:59
static const int QUADRATIC_TETRAHEDRON
Definition vtk.hpp:50
static const int QUADRATIC_PYRAMID
Definition vtk.hpp:54
static const int LAGRANGE_PYRAMID
Definition vtk.hpp:65
static const int TRIQUADRATIC_CUBE
Definition vtk.hpp:51
static const int Map[Geometry::NUM_GEOMETRIES]
Map from MFEM's Geometry::Type to linear VTK geometries.
Definition vtk.hpp:78
static const int PYRAMID
Definition vtk.hpp:42
static int GetOrder(int vtk_geom, int npoints)
For the given VTK geometry type and number of points, return the order of the element.
Definition vtk.cpp:96