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