6#include <vtkGenericDataObjectReader.h>
8#include <vtkXMLUnstructuredGridReader.h>
9#include <vtkXMLUnstructuredGridWriter.h>
11#include <vtkXMLPUnstructuredGridWriter.h>
13#include <vtkSmartPointer.h>
14#include <vtkUnstructuredGrid.h>
16#include <boost/algorithm/string/erase.hpp>
31 std::set<std::string>
const& output_variable_names,
39 if (
_data_mode == vtkXMLWriter::Ascii && compress)
41 WARN(
"Ascii data cannot be compressed, ignoring compression flag.");
45vtkSmartPointer<vtkUnstructuredGrid>
50 ERR(
"File '{:s}' does not exist.", file_name);
54 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
55 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
56 reader->SetFileName(file_name.c_str());
66 vtkUnstructuredGrid* vtkGrid = reader->GetOutput();
67 if (vtkGrid->GetNumberOfPoints() == 0)
69 ERR(
"Mesh '{:s}' contains zero points.", file_name);
76 bool const compute_element_neighbors)
78 vtkSmartPointer<vtkUnstructuredGrid> vtkGrid =
80 if (vtkGrid ==
nullptr)
85 std::string
const mesh_name(
88 vtkGrid, compute_element_neighbors, mesh_name);
92 bool const compute_element_neighbors)
96 ERR(
"File '{:s}' does not exist.", file_name);
100 vtkSmartPointer<vtkGenericDataObjectReader> reader =
101 vtkSmartPointer<vtkGenericDataObjectReader>::New();
102 reader->SetFileName(file_name.c_str());
106 if (reader->ReadOutputType() != 4)
108 ERR(
"Only VTK-files with dataset type \"Unstructured Grid\" are "
109 "currently supported.");
113 reader->ReadAllFieldsOn();
114 reader->ReadAllScalarsOn();
115 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
116 if (vtkGrid->GetNumberOfPoints() == 0)
118 ERR(
"Mesh '{:s}' contains zero points.", file_name);
122 std::string
const mesh_name(
125 vtkGrid, compute_element_neighbors, mesh_name);
130 std::string
const& file_name)
133 if (file_name_extension !=
".vtu")
135 OGS_FATAL(
"Expected a .vtu file for petsc output.");
138 auto const file_name_base = boost::erase_last_copy(file_name,
".vtu");
143 std::replace(basename.begin(), basename.end(),
'.',
'_');
159 auto const vtu_file_name =
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
std::set< std::string > _output_variable_names
VtuInterface(const MeshLib::Mesh *mesh, std::set< std::string > const &output_variable_names={}, int dataMode=vtkXMLWriter::Appended, bool compress=false)
Provide the mesh to write and set if compression should be used.
static MeshLib::Mesh * readVTUFile(std::string const &file_name, bool const compute_element_neighbors=false)
static MeshLib::Mesh * readVTKFile(std::string const &file_name, bool const compute_element_neighbors=false)
static vtkSmartPointer< vtkUnstructuredGrid > readVtuFileToVtkUnstructuredGrid(std::string const &file_name)
bool writeVTU(std::string const &file_name, const int num_partitions=1, const int rank=1)
bool writeToFile(std::filesystem::path const &file_path)
const MeshLib::Mesh * _mesh
static MeshLib::Mesh * convertUnstructuredGrid(vtkUnstructuredGrid *grid, bool const compute_element_neighbors=false, std::string const &mesh_name="vtkUnstructuredGrid")
Converts a vtkUnstructuredGrid object to a Mesh.
std::string getFileExtension(const std::string &path)
std::string extractPath(std::string const &pathname)
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
std::string extractBaseNameWithoutExtension(std::string const &pathname)
std::string joinPaths(std::string const &pathA, std::string const &pathB)
std::string extractBaseName(std::string const &pathname)
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)