16#include <vtkGenericDataObjectReader.h>
18#include <vtkXMLUnstructuredGridReader.h>
19#include <vtkXMLUnstructuredGridWriter.h>
21#include <vtkXMLPUnstructuredGridWriter.h>
23#include <vtkSmartPointer.h>
24#include <vtkUnstructuredGrid.h>
26#include <boost/algorithm/string/erase.hpp>
42 : _mesh(mesh), _data_mode(dataMode), _use_compressor(compress)
44 if (
_data_mode == vtkXMLWriter::Ascii && compress)
46 WARN(
"Ascii data cannot be compressed, ignoring compression flag.");
50vtkSmartPointer<vtkUnstructuredGrid>
55 ERR(
"File '{:s}' does not exist.", file_name);
59 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
60 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
61 reader->SetFileName(file_name.c_str());
71 vtkUnstructuredGrid* vtkGrid = reader->GetOutput();
72 if (vtkGrid->GetNumberOfPoints() == 0)
74 ERR(
"Mesh '{:s}' contains zero points.", file_name);
81 bool const compute_element_neighbors)
83 vtkSmartPointer<vtkUnstructuredGrid> vtkGrid =
85 if (vtkGrid ==
nullptr)
90 std::string
const mesh_name(
93 vtkGrid, compute_element_neighbors, mesh_name);
97 bool const compute_element_neighbors)
101 ERR(
"File '{:s}' does not exist.", file_name);
105 vtkSmartPointer<vtkGenericDataObjectReader> reader =
106 vtkSmartPointer<vtkGenericDataObjectReader>::New();
107 reader->SetFileName(file_name.c_str());
111 if (reader->ReadOutputType() != 4)
113 ERR(
"Only VTK-files with dataset type \"Unstructured Grid\" are "
114 "currently supported.");
118 reader->ReadAllFieldsOn();
119 reader->ReadAllScalarsOn();
120 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
121 if (vtkGrid->GetNumberOfPoints() == 0)
123 ERR(
"Mesh '{:s}' contains zero points.", file_name);
127 std::string
const mesh_name(
130 vtkGrid, compute_element_neighbors, mesh_name);
135 std::string
const& file_name)
138 if (file_name_extension !=
".vtu")
140 OGS_FATAL(
"Expected a .vtu file for petsc output.");
143 auto const file_name_base = boost::erase_last_copy(file_name,
".vtu");
148 std::replace(basename.begin(), basename.end(),
'.',
'_');
164 auto const vtu_file_name =
179 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)
Definition of the Mesh class.
VtkMappedMeshSource is a source class to transform OGS meshes into complete vtkUnstructuredGrids....
Definition of the VtkMeshConverter class.
Implementation of the VtuInterface class.
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)
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)