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