10#include <kernel/geometry/conformal_mesh.hpp>
11#include <kernel/geometry/index_set.hpp>
12#include <kernel/geometry/mesh_part.hpp>
13#include <kernel/geometry/structured_mesh.hpp>
14#include <kernel/shape.hpp>
16#include <kernel/util/dist_file_io.hpp>
17#include <kernel/util/dist.hpp>
19#include <kernel/util/memory_arbiter.hpp>
20#include <kernel/util/pack.hpp>
21#include <kernel/util/string.hpp>
22#include <kernel/util/tiny_algebra.hpp>
32#include <kernel/geometry/cgal.hpp>
41 template<
typename Shape_>
45 struct VTKShape<Shape::Simplex<1>>
47 static constexpr std::uint32_t type = 3;
48 static inline int map(
int i)
55 struct VTKShape<Shape::Simplex<2>>
57 static constexpr std::uint32_t type = 5;
58 static inline int map(
int i)
65 struct VTKShape<Shape::Simplex<3>>
67 static constexpr std::uint32_t type = 10;
68 static inline int map(
int i)
75 struct VTKShape<Shape::Hypercube<1>>
77 static constexpr std::uint32_t type = 3;
78 static inline int map(
int i)
85 struct VTKShape<Shape::Hypercube<2>>
87 static constexpr std::uint32_t type = 9;
88 static inline int map(
int i)
91 return (i ^ ((i >> 1) & 1));
96 struct VTKShape<Shape::Hypercube<3>>
98 static constexpr std::uint32_t type = 12;
99 static inline int map(
int i)
102 return (i ^ ((i >> 1) & 1));
106 template<
typename T_>
110 struct VTKType<std::uint8_t>
112 static constexpr std::string_view type =
"UInt8";
116 struct VTKType<std::uint16_t>
118 static constexpr std::string_view type =
"UInt16";
122 struct VTKType<std::uint32_t>
124 static constexpr std::string_view type =
"UInt32";
128 struct VTKType<std::uint64_t>
130 static constexpr std::string_view type =
"UInt64";
134 struct VTKType<std::int8_t>
136 static constexpr std::string_view type =
"Int8";
140 struct VTKType<std::int16_t>
142 static constexpr std::string_view type =
"Int16";
146 struct VTKType<std::int32_t>
148 static constexpr std::string_view type =
"Int32";
152 struct VTKType<std::int64_t>
154 static constexpr std::string_view type =
"Int64";
158 struct VTKType<float>
160 static constexpr std::string_view type =
"Float32";
164 struct VTKType<double>
166 static constexpr std::string_view type =
"Float64";
194 template<
typename Mesh_,
int cell_dim_ = Mesh_::ShapeType::dimension>
214 static constexpr bool can_compress =
true;
216 static constexpr bool can_compress =
false;
221 using VarDeque = std::deque<std::pair<String, std::vector<double>>>;
268 const auto& vertex_set = mesh.get_vertex_set();
269 const auto& index_set = mesh.template get_index_set<cell_dim_, 0>();
274 for(
int j(0); j < vertex_set.num_coords; j++)
276 vert[j] = float(vertex_set[i][j]);
282 tuple[j] = index_set(i, j);
307 const auto& vertex_set = mesh.get_vertex_set();
308 const auto& vertex_target_set = part.template get_target_set<0>();
309 const auto& cell_target_set = part.template get_target_set<cell_dim_>();
314 for(
int j(0); j < vertex_set.num_coords; j++)
316 vert[j] = float(vertex_set[vertex_target_set[i]][j]);
323 const auto& index_set = part.template get_index_set<cell_dim_, 0>();
327 tuple[j] = index_set(i, j);
334 const auto& index_set = mesh.template get_index_set<cell_dim_, 0>();
343 const Index vertex = index_set(cell_target_set[i], j);
344 for(
Index k(0); k < vertex_target_set.get_num_entities(); k++)
346 if(vertex_target_set[k] == vertex)
356#if defined(FEAT_HAVE_CGAL) || defined(DOXYGEN)
368 template<
typename DT_>
374 XASSERTM(cell_dim_ == 2,
"CGAL Export only possible with surface mesh");
377 auto point = cw.
point(i);
380 vert[j] = float(point[j]);
387 for(;it != end; ++it)
460 template<
typename T_>
463 XASSERTM(data !=
nullptr,
"data array is nullptr");
467 d[i] = scaling_factor * double(data[i]);
488 template<
typename Vector_>
490 typename Vector_::ValueType* =
nullptr)
492 XASSERTM(this->_num_verts <= v.size(),
"invalid number of vector elements!");
497 d[i] = scaling_factor * double(vx[i]);
521 template<
typename T_>
525 const T_* y =
nullptr,
526 const T_* z =
nullptr,
527 double scaling_factor = 1.0)
529 XASSERTM(x !=
nullptr,
"x-data array is nullptr");
532 if(y !=
nullptr && z !=
nullptr)
536 d[(3 * i) + 0] = scaling_factor *
double(x[i]);
537 d[(3 * i) + 1] = scaling_factor *
double(y[i]);
538 d[(3 * i) + 2] = scaling_factor *
double(z[i]);
541 else if(y !=
nullptr)
545 d[(3 * i) + 0] = scaling_factor *
double(x[i]);
546 d[(3 * i) + 1] = scaling_factor *
double(y[i]);
547 d[(3 * i) + 2] = 0.0;
554 d[(3 * i) + 0] = scaling_factor *
double(x[i]);
555 d[(3 * i) + 1] = 0.0;
556 d[(3 * i) + 2] = 0.0;
582 template<
typename VectorType_>
584 typename VectorType_::ValueType* =
nullptr)
587 const auto v_view = v.elements_view_r();
591 for(
Index j(0); j < 3; ++j)
593 d[(
Index(3) * i) + j] =
double(0);
596 for(
int j(0); j < v.block_size; ++j)
598 d[(
Index(3) * i) +
Index(j)] = scaling_factor * double(v_view(i)[j]);
624 template<
typename DT_,
int dim_>
631 for(
Index j(0); j < 3; ++j)
633 d[(
Index(3) * i) + j] =
double(0);
636 for(
int j(0); j < dim_; ++j)
638 d[(
Index(3) * i) +
Index(j)] = scaling_factor * double(v[i][j]);
661 template<
typename T_>
664 XASSERTM(data !=
nullptr,
"data array is nullptr");
668 d[i] = scaling_factor * double(data[i]);
689 template<
typename Vector_>
691 typename Vector_::ValueType* =
nullptr)
693 XASSERTM(this->_num_cells <= v.size(),
"invalid number of vector elements!");
698 d[i] = scaling_factor * double(vx[i]);
722 template<
typename T_>
726 const T_* y =
nullptr,
727 const T_* z =
nullptr,
728 double scaling_factor = 1.0)
730 XASSERTM(x !=
nullptr,
"x-data array is nullptr");
733 if(y !=
nullptr && z !=
nullptr)
737 d[(3 * i) + 0] = scaling_factor *
double(x[i]);
738 d[(3 * i) + 1] = scaling_factor *
double(y[i]);
739 d[(3 * i) + 2] = scaling_factor *
double(z[i]);
742 else if(y !=
nullptr)
746 d[(3 * i) + 0] = scaling_factor *
double(x[i]);
747 d[(3 * i) + 1] = scaling_factor *
double(y[i]);
748 d[(3 * i) + 2] = 0.0;
755 d[(3 * i) + 0] = scaling_factor *
double(x[i]);
756 d[(3 * i) + 1] = 0.0;
757 d[(3 * i) + 2] = 0.0;
783 template<
typename VectorType_>
785 typename VectorType_::ValueType* =
nullptr)
788 const auto v_view = v.elements_view_r();
792 for(
Index j(0); j < 3; ++j)
794 d[(
Index(3) * i) + j] =
double(0);
797 for(
int j(0); j < v.block_size; ++j)
799 d[(
Index(3) * i) +
Index(j)] = scaling_factor * double(v_view(i)[j]);
825 template<
typename DT_,
int dim_>
832 for(
Index j(0); j < 3; ++j)
834 d[(
Index(3) * i) + j] =
double(0);
837 for(
int j(0); j < dim_; ++j)
839 d[(
Index(3) * i) +
Index(j)] = scaling_factor * double(v[i][j]);
854 String vtu_name(filename +
".vtu");
855 std::ofstream ofs(vtu_name.c_str());
856 if(!(ofs.is_open() && ofs.good()))
858 throw FileError(
"Failed to create '" + vtu_name +
"'");
887 void write(
const String& filename,
const int rank,
const int nparts)
897 XASSERTM((rank >= 0) && (rank < nparts),
"Invalid rank");
900 std::vector<double> rank_array((std::size_t(
_num_cells)),
double(rank));
904 const std::size_t ndigits =
Math::ilog10(std::size_t(nparts - 1));
916 String pvtu_name(filename +
".pvtu");
917 std::ofstream ofs(pvtu_name.c_str());
918 if(!(ofs.is_open() && ofs.good()))
920 throw FileError(
"Failed to create '" + pvtu_name +
"'");
924 std::size_t p = filename.find_last_of(
"\\/");
925 String file_title = filename.substr(p == FEAT::String::npos ? 0 : ++p);
949 std::vector<double> rank_array(std::size_t(
_num_cells),
double(comm.
rank()));
956 std::stringstream stream;
960 String pattern = filename +
"." +
String(ndigits,
'*') +
".vtu";
972 String pvtu_name(filename +
".pvtu");
973 std::ofstream ofs(pvtu_name.c_str());
974 if(!(ofs.is_open() && ofs.good()))
976 throw FileError(
"Failed to create '" + pvtu_name +
"'");
980 std::size_t p = filename.find_last_of(
"\\/");
981 String file_title = filename.substr(p == FEAT::String::npos ? 0 : ++p);
1001 os << R
"(<VTKFile type="UnstructuredGrid" version="0.1" byte_order=")";
1002 os << (_little_endian() ? "LittleEndian" :
"BigEndian") <<
"\"";
1006#ifndef FEAT_HAVE_ZLIB
1007 XABORTM(
"Requested compression in ExportVTK, but ZLib is not available!");
1009 os <<
" compressor=\"vtkZLibDataCompressor\"";
1017 os <<
"<UnstructuredGrid>\n";
1018 os <<
"<Piece NumberOfPoints=\"" <<
_num_verts <<
"\" NumberOfCells=\"" <<
_num_cells <<
"\">\n";
1023 os <<
"<PointData>\n";
1033 os <<
"</PointData>\n";
1039 os <<
"<CellData>\n";
1051 os <<
"</CellData>\n";
1059 os << R
"(<DataArray type="Float32" NumberOfComponents="3" format="ascii" Name="test">)" << "\n";
1068 os <<
" " << vertex[j];
1072 os <<
"</DataArray>\n";
1078 std::pair<String, std::vector<float>> vertices(
"", {});
1088 vertices.second.push_back(
static_cast<float>(vertex[j]));
1094 os <<
"</Points>\n";
1100 os << R
"(<DataArray type="UInt32" Name="connectivity">)" << "\n";
1105 os << cell[VTKShapeType::map(0)];
1108 os <<
" " << cell[VTKShapeType::map(j)];
1112 os <<
"</DataArray>\n";
1117 std::pair<String, std::vector<std::uint32_t>> connectivity(
"connectivity", {});
1126 connectivity.second.push_back(
static_cast<std::uint32_t
>(cell[VTKShapeType::map(j)]));
1135 os << R
"(<DataArray type="UInt32" Name="offsets">)" << "\n";
1140 os <<
"</DataArray>\n";
1145 std::pair<String, std::vector<std::uint32_t>> offsets(
"offsets", {});
1150 offsets.second.push_back(
static_cast<std::uint32_t
>((i + 1) *
verts_per_cell));
1158 os << R
"(<DataArray type="UInt32" Name="types">)" << "\n";
1161 os << VTKShapeType::type <<
"\n";
1163 os <<
"</DataArray>\n";
1168 std::pair<String, std::vector<std::uint32_t>> types(
"types", {});
1173 types.second.push_back(VTKShapeType::type);
1182 os <<
"</UnstructuredGrid>\n";
1183 os <<
"</VTKFile>\n";
1201 os << R
"(<VTKFile type="PUnstructuredGrid" version="0.1" byte_order=")";
1202 os << (_little_endian() ? "LittleEndian" :
"BigEndian") <<
"\"";
1206#ifndef FEAT_HAVE_ZLIB
1207 XABORTM(
"Requested compression in ExportVTK, but ZLib is not available!");
1209 os <<
" compressor=\"vtkZLibDataCompressor\"";
1215 os <<
"<PUnstructuredGrid GhostLevel=\"0\">\n";
1220 os <<
"<PPointData>\n";
1225 os <<
"<PDataArray type=\"Float64\" Name=\"" << vertex_scalar.first <<
"\" />\n";
1230 os <<
"<PDataArray type=\"Float64\" Name=\"" << vertex_vector.first;
1231 os <<
"\" NumberOfComponents=\"3\" />\n";
1234 os <<
"</PPointData>\n";
1240 os <<
"<PCellData>\n";
1244 os <<
"<PDataArray type=\"Float64\" Name=\"" << cell_scalar.first <<
"\" />\n";
1249 os <<
"<PDataArray type=\"Float64\" Name=\"" << cell_vector.first;
1250 os <<
"\" NumberOfComponents=\"3\" />\n";
1252 os <<
"</PCellData>\n";
1256 os <<
"<PPoints>\n";
1257 os <<
"<PDataArray type=\"Float32\" NumberOfComponents=\"3\" />\n";
1258 os <<
"</PPoints>\n";
1261 const std::size_t ndigits =
Math::ilog10(std::size_t(nparts - 1));
1264 for(
int i(0); i < nparts; ++i)
1266 os <<
"<Piece Source=\"" << file_title <<
"." <<
stringify(i).
pad_front(ndigits,
'0');
1267 os <<
".vtu\" />\n";
1271 os <<
"</PUnstructuredGrid>\n";
1272 os <<
"</VTKFile>\n";
1285 std::uint32_t num_bytes;
1287 explicit UncompressedHeader(std::uint32_t number_of_bytes) : num_bytes(number_of_bytes)
1299 std::uint32_t blocks = 1;
1300 std::uint32_t blocksize;
1301 std::uint32_t last_blocksize;
1302 std::uint32_t compressed_blocksizes[1];
1304 explicit CompressedHeader(std::uint32_t uncompressed_bytes, std::uint32_t compressed_bytes) :
1305 blocksize(uncompressed_bytes),
1306 last_blocksize(uncompressed_bytes),
1307 compressed_blocksizes{compressed_bytes}
1326 template<
typename T_>
1334 std::string_view format =
_ascii_output ?
"ascii" :
"binary";
1335 std::string_view type = Intern::VTKType<T_>::type;
1338 os <<
"<DataArray type=\"" << type <<
"\" format=\"" << format <<
"\"";
1339 if(data.first !=
"")
1341 os <<
" Name = \"" << data.first <<
"\"";
1343 if(number_of_components != 0)
1345 os <<
" NumberOfComponents=\"" << number_of_components <<
"\"";
1351 Index num_blocks = data.second.size() / number_of_components;
1352 for(
Index i(0); i < num_blocks; ++i)
1354 for(
Index j(0); j < number_of_components; j++)
1357 os << ((j == number_of_components - 1) ?
"\n" :
" ");
1363 auto uncompressed_size =
static_cast<std::uint32_t
>(data.second.size() *
sizeof(T_));
1366 Pack::Type pack_type = Pack::deduct_type<T_>() | Pack::Type::Mask_Z;
1369 std::vector<Pack::u8> buffer(buffer_size, 0);
1370 std::size_t compressed_size =
Pack::encode(buffer.data(), data.second.data(), buffer_size, data.second.size(), pack_type,
false);
1373 CompressedHeader header(uncompressed_size,
static_cast<std::uint32_t
>(compressed_size));
1374 const auto* header_begin =
reinterpret_cast<const Pack::u8*
>(&header);
1377 const auto* data_begin =
reinterpret_cast<const Pack::u8*
>(buffer.data());
1378 const auto* data_end = data_begin + compressed_size;
1385 UncompressedHeader header(
static_cast<std::uint32_t
>(data.second.size() *
sizeof(T_)));
1387 const auto* header_begin =
reinterpret_cast<const Pack::u8*
>(&header);
1388 const auto* header_end = header_begin +
sizeof(header);
1390 const auto* data_begin =
reinterpret_cast<const Pack::u8*
>(data.second.data());
1391 const auto* data_end = data_begin + header.num_bytes;
1397 os <<
"</DataArray>\n";
1401 bool _little_endian()
const
1404 void* xv = (
void*)&x;
1405 auto* x16 = (int16_t*)xv;
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
int size() const
Returns the size of this communicator.
int rank() const
Returns the rank of this process in this communicator.
static void write_sequence(std::stringstream &stream, const String &pattern, const Dist::Comm &comm, bool truncate=true)
Writes a rank-indexed text file sequence.
Base class for file related errors.
Wrapper for the CGAL Library.
CGALVerticesAroundFaceAdjactor< DT_ > vertices_around_face() const
Returns an adjactor for vertices around faces of the mesh.
PointType point(std::uint32_t idx) const
Returns the point of the surface mesh with the given index.
VTK exporter class template.
ExportVTK(const MeshType &mesh, const MeshPart< MeshType > &part, int var_prec=0)
Constructor.
virtual ~ExportVTK()=default
destructor
VarDeque _vertex_scalars
vertex variable list
void add_vertex_scalar(const String &name, const Vector_ &v, double scaling_factor=1.0, typename Vector_::ValueType *=nullptr)
Adds a scalar vertex variable to the exporter.
void add_vertex_vector(const String &name, const T_ *x, const T_ *y=nullptr, const T_ *z=nullptr, double scaling_factor=1.0)
Adds a vector-field vertex variable to the exporter.
Index _num_verts
number of vertices in mesh
void add_cell_vector(const String &name, const std::vector< Tiny::Vector< DT_, dim_ > > &v, double scaling_factor=1.0)
Adds a vector-field cell variable given by a std::vector of Tiny::Vector to the exporter.
std::function< void(Index, Tiny::Vector< float, num_coords > &)> _vertices
Accessor function for vertices.
bool _use_compression
compressed output flag. Only applicable if _binary_output is true
static constexpr int num_coords
Coordinates per vertex. The VTK documentation states that this is always three.
ExportVTK & operator=(ExportVTK &&other)=default
Move-assignment constructor.
int _var_prec
precision of variables
void write(const String &filename, const Dist::Comm &comm)
Writes out the data to a parallel XML-PVTU file.
void write(const String &filename, const int rank, const int nparts)
Writes out the data to a parallel XML-PVTU file.
void enable_ascii_output(bool flag)
Enable or disable binary output.
void clear()
Clears all vertex and cell variables in the exporter.
void write(const String &filename) const
Writes out the data to a serial XML-VTU file.
typename MeshType::ShapeType ShapeType
our shape type
void write_vtu(std::ostream &os) const
Writes out the mesh and variable data in serial XML-VTU format.
typename Shape::FaceTraits< ShapeType, cell_dim_ >::ShapeType CellShapeType
shape type of exported cells
std::function< void(Index, IndexTuple< verts_per_cell > &)> _cells
Accessor function for cells.
VarDeque _vertex_vectors
vertex field list
void write_pvtu(std::ostream &os, const String &file_title, const int nparts) const
Writes out the partition data in parallel XML-PVTU format.
ExportVTK(const ExportVTK &other)=default
Copy constructor.
Index _num_cells
number of cells in mesh
void add_cell_vector(const String &name, const T_ *x, const T_ *y=nullptr, const T_ *z=nullptr, double scaling_factor=1.0)
Adds a vector-field cell variable to the exporter.
void add_vertex_vector(const String &name, const std::vector< Tiny::Vector< DT_, dim_ > > &v, double scaling_factor=1.0)
Adds a vector-field vertex variable given by a std::vector of Tiny::Vector to the exporter.
void add_vertex_vector(const String &name, const VectorType_ &v, double scaling_factor=1.0, typename VectorType_::ValueType *=nullptr)
Adds a vector-field vertex variable given by a LAFEM::***VectorBlocked to the exporter.
ExportVTK(ExportVTK &&other) noexcept=default
Move constructor.
VarDeque _cell_scalars
cell variable list
Intern::VTKShape< CellShapeType > VTKShapeType
our VTK shape type
ExportVTK & operator=(const ExportVTK &other)=default
Copy-assignment constructor.
ExportVTK(const MeshType &mesh, int var_prec=0)
Constructor.
void enable_compression(bool flag)
Enable or disable compression.
void add_cell_scalar(const String &name, const T_ *data, double scaling_factor=1.0)
Adds a scalar cell variable to the exporter.
VarDeque _cell_vectors
cell field list
void add_cell_vector(const String &name, const VectorType_ &v, double scaling_factor=1.0, typename VectorType_::ValueType *=nullptr)
Adds a vector-field cell variable given by a LAFEM::***VectorBlocked to the exporter.
bool _ascii_output
ascii output flag
void add_vertex_scalar(const String &name, const T_ *data, double scaling_factor=1.0)
Adds a scalar vertex variable to the exporter.
ExportVTK(const Geometry::CGALWrapper< DT_ > &cw, int var_prec=0)
Constructor.
void _write_data_array(std::ostream &os, const std::pair< String, std::vector< T_ > > &data, Index number_of_components=0) const
Writes an DataArray XML-node to the give output stream.
std::deque< std::pair< String, std::vector< double > > > VarDeque
our variable container
void add_cell_scalar(const String &name, const Vector_ &v, double scaling_factor=1.0, typename Vector_::ValueType *=nullptr)
Adds a scalar cell variable to the exporter.
static constexpr int verts_per_cell
Vertices per cell.
Class template for partial meshes.
bool has_topology() const
Checks if this MeshPart has a mesh topology.
String class implementation.
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
Tiny Vector class template.
CUDA_HOST_DEVICE void format(DataType alpha=DataType(0))
Formats the vector.
@ other
generic/other permutation strategy
T_ ilog10(T_ x)
Computes the integral base-10 logarithm of an integer, i.e. its number of non-zero decimal digits.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
std::size_t encode(void *buf, const T_ *src, const std::size_t buf_size, const std::size_t count, const Pack::Type pack_type, bool swap_bytes, double tolerance)
Encodes an array into a packed buffer.
std::size_t estimate_size(const std::size_t count, const Pack::Type type, const double tolerance)
Computes the estimated (upper bound) pack buffer size for an array.
String base64_encode(const Pack::u8 *begin, const Pack::u8 *end)
Encodes bytes into a Base64 string.
std::uint8_t u8
8-bit unsigned integer type
String stringify(const T_ &item)
Converts an item into a String.
static constexpr int version_major
FEAT major version number.
static constexpr int version_patch
FEAT patch version number (month)
static constexpr int version_minor
FEAT minor version number (year)
String stringify_fp_sci(DataType_ value, int precision=0, int width=0, bool sign=false)
Prints a floating point value to a string in scientific notation.
std::uint64_t Index
Index data type.
Index Tuple class template.
Face traits tag struct template.