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