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>
34 if (
_data_mode == vtkXMLWriter::Ascii && compress)
36 WARN(
"Ascii data cannot be compressed, ignoring compression flag.");
40vtkSmartPointer<vtkUnstructuredGrid>
45 ERR(
"File '{:s}' does not exist.", file_name);
49 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
50 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
51 reader->SetFileName(file_name.c_str());
61 vtkUnstructuredGrid* vtkGrid = reader->GetOutput();
62 if (vtkGrid->GetNumberOfPoints() == 0)
64 ERR(
"Mesh '{:s}' contains zero points.", file_name);
71 bool const compute_element_neighbors)
73 vtkSmartPointer<vtkUnstructuredGrid> vtkGrid =
75 if (vtkGrid ==
nullptr)
80 std::string
const mesh_name(
83 vtkGrid, compute_element_neighbors, mesh_name);
87 bool const compute_element_neighbors)
91 ERR(
"File '{:s}' does not exist.", file_name);
95 vtkSmartPointer<vtkGenericDataObjectReader> reader =
96 vtkSmartPointer<vtkGenericDataObjectReader>::New();
97 reader->SetFileName(file_name.c_str());
101 if (reader->ReadOutputType() != 4)
103 ERR(
"Only VTK-files with dataset type \"Unstructured Grid\" are "
104 "currently supported.");
108 reader->ReadAllFieldsOn();
109 reader->ReadAllScalarsOn();
110 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
111 if (vtkGrid->GetNumberOfPoints() == 0)
113 ERR(
"Mesh '{:s}' contains zero points.", file_name);
117 std::string
const mesh_name(
120 vtkGrid, compute_element_neighbors, mesh_name);
125 std::string
const& file_name)
128 if (file_name_extension !=
".vtu")
130 OGS_FATAL(
"Expected a .vtu file for petsc output.");
133 auto const file_name_base = boost::erase_last_copy(file_name,
".vtu");
138 std::replace(basename.begin(), basename.end(),
'.',
'_');
154 auto const vtu_file_name =
169 ERR(
"writeMeshToFile(): Could not write mesh to '{:s}'.", file_name);
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
VtuInterface(const MeshLib::Mesh *mesh, 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)
int writeVtu(MeshLib::Mesh const &mesh, std::string const &file_name, int const data_mode)
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)