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.");
51 bool const compute_element_neighbors)
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);
78 std::string
const mesh_name(
81 vtkGrid, compute_element_neighbors, mesh_name);
85 bool const compute_element_neighbors)
89 ERR(
"File '{:s}' does not exist.", file_name);
93 vtkSmartPointer<vtkGenericDataObjectReader> reader =
94 vtkSmartPointer<vtkGenericDataObjectReader>::New();
95 reader->SetFileName(file_name.c_str());
99 if (reader->ReadOutputType() != 4)
101 ERR(
"Only VTK-files with dataset type \"Unstructured Grid\" are "
102 "currently supported.");
106 reader->ReadAllFieldsOn();
107 reader->ReadAllScalarsOn();
108 vtkUnstructuredGrid* vtkGrid = reader->GetUnstructuredGridOutput();
109 if (vtkGrid->GetNumberOfPoints() == 0)
111 ERR(
"Mesh '{:s}' contains zero points.", file_name);
115 std::string
const mesh_name(
118 vtkGrid, compute_element_neighbors, mesh_name);
123 std::string
const& file_name)
126 if (file_name_extension !=
".vtu")
128 OGS_FATAL(
"Expected a .vtu file for petsc output.");
131 auto const file_name_base = boost::erase_last_copy(file_name,
".vtu");
136 std::replace(basename.begin(), basename.end(),
'.',
'_');
152 auto const vtu_file_name =
167 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)
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)